SPDX: license tags (mostly by guesswork)
[exim.git] / src / src / lookups / dsearch.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2015 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-only */
9
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). */
13
14
15 #include "../exim.h"
16 #include "lf_functions.h"
17
18
19
20 /*************************************************
21 *              Open entry point                  *
22 *************************************************/
23
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. */
28
29 static void *
30 dsearch_open(const uschar * dirname, uschar ** errmsg)
31 {
32 DIR * dp = exim_opendir(dirname);
33 if (!dp)
34   {
35   *errmsg = string_open_failed("%s for directory search", dirname);
36   return NULL;
37   }
38 closedir(dp);
39 return (void *)(-1);
40 }
41
42
43 /*************************************************
44 *             Check entry point                  *
45 *************************************************/
46
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. */
49
50 static BOOL
51 dsearch_check(void * handle, const uschar * filename, int modemask,
52   uid_t * owners, gid_t * owngroups, uschar ** errmsg)
53 {
54 handle = handle;
55 if (*filename == '/')
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);
59 return FALSE;
60 }
61
62
63 /*************************************************
64 *              Find entry point                  *
65 *************************************************/
66
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)
73
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
76 for us. */
77
78 static int
79 dsearch_find(void * handle, const uschar * dirname, const uschar * keystring,
80   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
81   const uschar * opts)
82 {
83 struct stat statbuf;
84 int save_errno;
85 uschar * filename;
86 unsigned flags = 0;
87
88 if (Ustrchr(keystring, '/') != 0)
89   {
90   *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
91     keystring);
92   return DEFER;
93   }
94
95 if (opts)
96   {
97   int sep = ',';
98   uschar * ele;
99
100   while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
101     if (Ustrcmp(ele, "ret=full") == 0)
102       flags |= RET_FULL;
103     else if (Ustrncmp(ele, "filter=", 7) == 0)
104       {
105       ele += 7;
106       if (Ustrcmp(ele, "file") == 0)
107         flags |= FILTER_TYPE | FILTER_FILE;
108       else if (Ustrcmp(ele, "dir") == 0)
109         flags |= FILTER_TYPE | FILTER_DIR;
110       else if (Ustrcmp(ele, "subdir") == 0)
111         flags |= FILTER_TYPE | FILTER_SUBDIR;   /* like dir but not "." or ".." */
112       }
113   }
114
115 filename = string_sprintf("%s/%s", dirname, keystring);
116 if (  Ulstat(filename, &statbuf) >= 0
117    && (  !(flags & FILTER_TYPE)
118       || (flags & FILTER_FILE && S_ISREG(statbuf.st_mode))
119       || (  flags & (FILTER_DIR | FILTER_SUBDIR)
120          && S_ISDIR(statbuf.st_mode)
121          && (  flags & FILTER_DIR
122             || keystring[0] != '.'
123             || keystring[1] && keystring[1] != '.'
124    )  )  )  )
125   {
126   /* Since the filename exists in the filesystem, we can return a
127   non-tainted result. */
128   *result = string_copy_taint(flags & RET_FULL ? filename : keystring, GET_UNTAINTED);
129   return OK;
130   }
131
132 if (errno == ENOENT || errno == 0) return FAIL;
133
134 save_errno = errno;
135 *errmsg = string_sprintf("%s: lstat: %s", filename, strerror(errno));
136 errno = save_errno;
137 return DEFER;
138 }
139
140
141 /*************************************************
142 *              Close entry point                 *
143 *************************************************/
144
145 /* See local README for interface description */
146
147 void
148 static dsearch_close(void *handle)
149 {
150 handle = handle;   /* Avoid compiler warning */
151 }
152
153
154 /*************************************************
155 *         Version reporting entry point          *
156 *************************************************/
157
158 /* See local README for interface description. */
159
160 #include "../version.h"
161
162 gstring *
163 dsearch_version_report(gstring * g)
164 {
165 #ifdef DYNLOOKUP
166 g = string_fmt_append(g, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
167 #endif
168 return g;
169 }
170
171
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 */
182 };
183
184 #ifdef DYNLOOKUP
185 #define dsearch_lookup_module_info _lookup_module_info
186 #endif
187
188 static lookup_info *_lookup_list[] = { &_lookup_info };
189 lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
190
191 /* End of lookups/dsearch.c */