1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* The idea for this code came from Matthew Byng-Maddick, but his original has
9 been heavily reworked a lot for Exim 4 (and it now uses stat() (more precisely:
10 lstat()) rather than a directory scan). */
14 #include "lf_functions.h"
18 /*************************************************
20 *************************************************/
22 /* See local README for interface description. We open the directory to test
23 whether it exists and whether it is searchable. However, we don't need to keep
24 it open, because the "search" can be done by a call to lstat() rather than
25 actually scanning through the list of files. */
28 dsearch_open(uschar *dirname, uschar **errmsg)
30 DIR *dp = opendir(CS dirname);
33 int save_errno = errno;
34 *errmsg = string_open_failed(errno, "%s for directory search", dirname);
43 /*************************************************
45 *************************************************/
47 /* The handle will always be (void *)(-1), but don't try casting it to an
48 integer as this gives warnings on 64-bit systems. */
51 dsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
52 gid_t *owngroups, uschar **errmsg)
55 return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
56 "dsearch", errmsg) == 0;
60 /*************************************************
62 *************************************************/
64 /* See local README for interface description. We use lstat() instead of
65 scanning the directory, as it is hopefully faster to let the OS do the scanning
69 dsearch_find(void *handle, uschar *dirname, const uschar *keystring, int length,
70 uschar **result, uschar **errmsg, uint *do_cache)
76 handle = handle; /* Keep picky compilers happy */
80 if (Ustrchr(keystring, '/') != 0)
82 *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
87 filename = string_sprintf("%s/%s", dirname, keystring);
88 if (Ulstat(filename, &statbuf) >= 0)
90 /* Since the filename exists in the filesystem, we can return a
91 non-tainted result. */
92 *result = string_copy_taint(keystring, FALSE);
96 if (errno == ENOENT) return FAIL;
99 *errmsg = string_sprintf("%s: lstat failed", filename);
105 /*************************************************
106 * Close entry point *
107 *************************************************/
109 /* See local README for interface description */
112 static dsearch_close(void *handle)
114 handle = handle; /* Avoid compiler warning */
118 /*************************************************
119 * Version reporting entry point *
120 *************************************************/
122 /* See local README for interface description. */
124 #include "../version.h"
127 dsearch_version_report(FILE *f)
130 fprintf(f, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
135 static lookup_info _lookup_info = {
136 US"dsearch", /* lookup name */
137 lookup_absfile, /* uses absolute file name */
138 dsearch_open, /* open function */
139 dsearch_check, /* check function */
140 dsearch_find, /* find function */
141 dsearch_close, /* close function */
142 NULL, /* no tidy function */
143 NULL, /* no quoting function */
144 dsearch_version_report /* version reporting */
148 #define dsearch_lookup_module_info _lookup_module_info
151 static lookup_info *_lookup_list[] = { &_lookup_info };
152 lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
154 /* End of lookups/dsearch.c */