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(const uschar * dirname, uschar ** errmsg)
30 DIR * dp = exim_opendir(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, const uschar * filename, int modemask,
52 uid_t * owners, gid_t * owngroups, uschar ** errmsg)
56 return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
57 "dsearch", errmsg) == 0;
58 *errmsg = string_sprintf("dirname '%s' for dsearch is not absolute", filename);
63 /*************************************************
65 *************************************************/
67 #define RET_FULL BIT(0)
68 #define FILTER_TYPE BIT(1)
69 #define FILTER_ALL BIT(1)
70 #define FILTER_FILE BIT(2)
71 #define FILTER_DIR BIT(3)
72 #define FILTER_SUBDIR BIT(4)
74 /* See local README for interface description. We use lstat() instead of
75 scanning the directory, as it is hopefully faster to let the OS do the scanning
79 dsearch_find(void * handle, const uschar * dirname, const uschar * keystring,
80 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
88 handle = handle; /* Keep picky compilers happy */
92 if (Ustrchr(keystring, '/') != 0)
94 *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
104 while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
105 if (Ustrcmp(ele, "ret=full") == 0)
107 else if (Ustrncmp(ele, "filter=", 7) == 0)
110 if (Ustrcmp(ele, "file") == 0)
111 flags |= FILTER_TYPE | FILTER_FILE;
112 else if (Ustrcmp(ele, "dir") == 0)
113 flags |= FILTER_TYPE | FILTER_DIR;
114 else if (Ustrcmp(ele, "subdir") == 0)
115 flags |= FILTER_TYPE | FILTER_SUBDIR; /* like dir but not "." or ".." */
119 filename = string_sprintf("%s/%s", dirname, keystring);
120 if ( Ulstat(filename, &statbuf) >= 0
121 && ( !(flags & FILTER_TYPE)
122 || (flags & FILTER_FILE && S_ISREG(statbuf.st_mode))
123 || ( flags & (FILTER_DIR | FILTER_SUBDIR)
124 && S_ISDIR(statbuf.st_mode)
125 && ( flags & FILTER_DIR
126 || keystring[0] != '.'
127 || keystring[1] != '.'
128 || keystring[1] && keystring[2]
131 /* Since the filename exists in the filesystem, we can return a
132 non-tainted result. */
133 *result = string_copy_taint(flags & RET_FULL ? filename : keystring, FALSE);
137 if (errno == ENOENT) return FAIL;
140 *errmsg = string_sprintf("%s: lstat failed", filename);
146 /*************************************************
147 * Close entry point *
148 *************************************************/
150 /* See local README for interface description */
153 static dsearch_close(void *handle)
155 handle = handle; /* Avoid compiler warning */
159 /*************************************************
160 * Version reporting entry point *
161 *************************************************/
163 /* See local README for interface description. */
165 #include "../version.h"
168 dsearch_version_report(FILE *f)
171 fprintf(f, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
176 static lookup_info _lookup_info = {
177 .name = US"dsearch", /* lookup name */
178 .type = lookup_absfile, /* uses absolute file name */
179 .open = dsearch_open, /* open function */
180 .check = dsearch_check, /* check function */
181 .find = dsearch_find, /* find function */
182 .close = dsearch_close, /* close function */
183 .tidy = NULL, /* no tidy function */
184 .quote = NULL, /* no quoting function */
185 .version_report = dsearch_version_report /* version reporting */
189 #define dsearch_lookup_module_info _lookup_module_info
192 static lookup_info *_lookup_list[] = { &_lookup_info };
193 lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
195 /* End of lookups/dsearch.c */