1 /* $Cambridge: exim/src/src/lookups/dsearch.c,v 1.6 2009/11/16 19:50:38 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
10 /* The idea for this code came from Matthew Byng-Maddick, but his original has
11 been heavily reworked a lot for Exim 4 (and it now uses stat() (more precisely:
12 lstat()) rather than a directory scan). */
16 #include "lf_functions.h"
20 /*************************************************
22 *************************************************/
24 /* See local README for interface description. We open the directory to test
25 whether it exists and whether it is searchable. However, we don't need to keep
26 it open, because the "search" can be done by a call to lstat() rather than
27 actually scanning through the list of files. */
30 dsearch_open(uschar *dirname, uschar **errmsg)
32 DIR *dp = opendir(CS dirname);
35 int save_errno = errno;
36 *errmsg = string_open_failed(errno, "%s for directory search", dirname);
45 /*************************************************
47 *************************************************/
49 /* The handle will always be (void *)(-1), but don't try casting it to an
50 integer as this gives warnings on 64-bit systems. */
53 static dsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
54 gid_t *owngroups, uschar **errmsg)
57 return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
58 "dsearch", errmsg) == 0;
62 /*************************************************
64 *************************************************/
66 /* See local README for interface description. We use lstat() instead of
67 scanning the directory, as it is hopefully faster to let the OS do the scanning
71 static dsearch_find(void *handle, uschar *dirname, uschar *keystring, int length,
72 uschar **result, uschar **errmsg, BOOL *do_cache)
76 uschar filename[PATH_MAX];
78 handle = handle; /* Keep picky compilers happy */
82 if (Ustrchr(keystring, '/') != 0)
84 *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
89 if (!string_format(filename, sizeof(filename), "%s/%s", dirname, keystring))
91 *errmsg = US"path name too long";
95 if (Ulstat(filename, &statbuf) >= 0)
97 *result = string_copy(keystring);
101 if (errno == ENOENT) return FAIL;
104 *errmsg = string_sprintf("%s: lstat failed", filename);
110 /*************************************************
111 * Close entry point *
112 *************************************************/
114 /* See local README for interface description */
117 static dsearch_close(void *handle)
119 handle = handle; /* Avoid compiler warning */
123 /*************************************************
124 * Version reporting entry point *
125 *************************************************/
127 /* See local README for interface description. */
129 #include "../version.h"
132 dsearch_version_report(FILE *f)
135 fprintf(f, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
140 static lookup_info _lookup_info = {
141 US"dsearch", /* lookup name */
142 lookup_absfile, /* uses absolute file name */
143 dsearch_open, /* open function */
144 dsearch_check, /* check function */
145 dsearch_find, /* find function */
146 dsearch_close, /* close function */
147 NULL, /* no tidy function */
148 NULL, /* no quoting function */
149 dsearch_version_report /* version reporting */
153 #define dsearch_lookup_module_info _lookup_module_info
156 static lookup_info *_lookup_list[] = { &_lookup_info };
157 lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
159 /* End of lookups/dsearch.c */