1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
9 /* The idea for this code came from Matthew Byng-Maddick, but his original has
10 been heavily reworked a lot for Exim 4 (and it now uses stat() (more precisely:
11 lstat()) rather than a directory scan). */
15 #include "lf_functions.h"
19 /*************************************************
21 *************************************************/
23 /* See local README for interface description. We open the directory to test
24 whether it exists and whether it is searchable. However, we don't need to keep
25 it open, because the "search" can be done by a call to lstat() rather than
26 actually scanning through the list of files. */
29 dsearch_open(const uschar * dirname, uschar ** errmsg)
31 DIR * dp = exim_opendir(dirname);
34 int save_errno = errno;
35 *errmsg = string_open_failed(errno, "%s for directory search", dirname);
44 /*************************************************
46 *************************************************/
48 /* The handle will always be (void *)(-1), but don't try casting it to an
49 integer as this gives warnings on 64-bit systems. */
52 dsearch_check(void * handle, const uschar * filename, int modemask,
53 uid_t * owners, gid_t * owngroups, uschar ** errmsg)
57 return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
58 "dsearch", errmsg) == 0;
59 *errmsg = string_sprintf("dirname '%s' for dsearch is not absolute", filename);
64 /*************************************************
66 *************************************************/
68 #define RET_FULL BIT(0)
69 #define FILTER_TYPE BIT(1)
70 #define FILTER_ALL BIT(1)
71 #define FILTER_FILE BIT(2)
72 #define FILTER_DIR BIT(3)
73 #define FILTER_SUBDIR BIT(4)
75 /* See local README for interface description. We use lstat() instead of
76 scanning the directory, as it is hopefully faster to let the OS do the scanning
80 dsearch_find(void * handle, const uschar * dirname, const uschar * keystring,
81 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
89 if (Ustrchr(keystring, '/') != 0)
91 *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
101 while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
102 if (Ustrcmp(ele, "ret=full") == 0)
104 else if (Ustrncmp(ele, "filter=", 7) == 0)
107 if (Ustrcmp(ele, "file") == 0)
108 flags |= FILTER_TYPE | FILTER_FILE;
109 else if (Ustrcmp(ele, "dir") == 0)
110 flags |= FILTER_TYPE | FILTER_DIR;
111 else if (Ustrcmp(ele, "subdir") == 0)
112 flags |= FILTER_TYPE | FILTER_SUBDIR; /* like dir but not "." or ".." */
116 filename = string_sprintf("%s/%s", dirname, keystring);
117 if ( Ulstat(filename, &statbuf) >= 0
118 && ( !(flags & FILTER_TYPE)
119 || (flags & FILTER_FILE && S_ISREG(statbuf.st_mode))
120 || ( flags & (FILTER_DIR | FILTER_SUBDIR)
121 && S_ISDIR(statbuf.st_mode)
122 && ( flags & FILTER_DIR
123 || keystring[0] != '.'
124 || keystring[1] && keystring[1] != '.'
127 /* Since the filename exists in the filesystem, we can return a
128 non-tainted result. */
129 *result = string_copy_taint(flags & RET_FULL ? filename : keystring, FALSE);
133 if (errno == ENOENT || errno == 0) return FAIL;
136 *errmsg = string_sprintf("%s: lstat: %s", filename, strerror(errno));
142 /*************************************************
143 * Close entry point *
144 *************************************************/
146 /* See local README for interface description */
149 static dsearch_close(void *handle)
151 handle = handle; /* Avoid compiler warning */
155 /*************************************************
156 * Version reporting entry point *
157 *************************************************/
159 /* See local README for interface description. */
161 #include "../version.h"
164 dsearch_version_report(FILE *f)
167 fprintf(f, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
172 static lookup_info _lookup_info = {
173 .name = US"dsearch", /* lookup name */
174 .type = lookup_absfile, /* uses absolute file name */
175 .open = dsearch_open, /* open function */
176 .check = dsearch_check, /* check function */
177 .find = dsearch_find, /* find function */
178 .close = dsearch_close, /* close function */
179 .tidy = NULL, /* no tidy function */
180 .quote = NULL, /* no quoting function */
181 .version_report = dsearch_version_report /* version reporting */
185 #define dsearch_lookup_module_info _lookup_module_info
188 static lookup_info *_lookup_list[] = { &_lookup_info };
189 lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
191 /* End of lookups/dsearch.c */