Debug: feed startup "whats supported" info through normal debug channel
[exim.git] / src / src / lookups / dsearch.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* Copyright (c) The Exim Maintainers 2020 - 2021 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
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). */
12
13
14 #include "../exim.h"
15 #include "lf_functions.h"
16
17
18
19 /*************************************************
20 *              Open entry point                  *
21 *************************************************/
22
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. */
27
28 static void *
29 dsearch_open(const uschar * dirname, uschar ** errmsg)
30 {
31 DIR * dp = exim_opendir(dirname);
32 if (!dp)
33   {
34   *errmsg = string_open_failed("%s for directory search", dirname);
35   return NULL;
36   }
37 closedir(dp);
38 return (void *)(-1);
39 }
40
41
42 /*************************************************
43 *             Check entry point                  *
44 *************************************************/
45
46 /* The handle will always be (void *)(-1), but don't try casting it to an
47 integer as this gives warnings on 64-bit systems. */
48
49 static BOOL
50 dsearch_check(void * handle, const uschar * filename, int modemask,
51   uid_t * owners, gid_t * owngroups, uschar ** errmsg)
52 {
53 handle = handle;
54 if (*filename == '/')
55   return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
56     "dsearch", errmsg) == 0;
57 *errmsg = string_sprintf("dirname '%s' for dsearch is not absolute", filename);
58 return FALSE;
59 }
60
61
62 /*************************************************
63 *              Find entry point                  *
64 *************************************************/
65
66 #define RET_FULL        BIT(0)
67 #define FILTER_TYPE     BIT(1)
68 #define FILTER_ALL      BIT(1)
69 #define FILTER_FILE     BIT(2)
70 #define FILTER_DIR      BIT(3)
71 #define FILTER_SUBDIR   BIT(4)
72
73 /* See local README for interface description. We use lstat() instead of
74 scanning the directory, as it is hopefully faster to let the OS do the scanning
75 for us. */
76
77 static int
78 dsearch_find(void * handle, const uschar * dirname, const uschar * keystring,
79   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
80   const uschar * opts)
81 {
82 struct stat statbuf;
83 int save_errno;
84 uschar * filename;
85 unsigned flags = 0;
86
87 if (Ustrchr(keystring, '/') != 0)
88   {
89   *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
90     keystring);
91   return DEFER;
92   }
93
94 if (opts)
95   {
96   int sep = ',';
97   uschar * ele;
98
99   while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
100     if (Ustrcmp(ele, "ret=full") == 0)
101       flags |= RET_FULL;
102     else if (Ustrncmp(ele, "filter=", 7) == 0)
103       {
104       ele += 7;
105       if (Ustrcmp(ele, "file") == 0)
106         flags |= FILTER_TYPE | FILTER_FILE;
107       else if (Ustrcmp(ele, "dir") == 0)
108         flags |= FILTER_TYPE | FILTER_DIR;
109       else if (Ustrcmp(ele, "subdir") == 0)
110         flags |= FILTER_TYPE | FILTER_SUBDIR;   /* like dir but not "." or ".." */
111       }
112   }
113
114 filename = string_sprintf("%s/%s", dirname, keystring);
115 if (  Ulstat(filename, &statbuf) >= 0
116    && (  !(flags & FILTER_TYPE)
117       || (flags & FILTER_FILE && S_ISREG(statbuf.st_mode))
118       || (  flags & (FILTER_DIR | FILTER_SUBDIR)
119          && S_ISDIR(statbuf.st_mode)
120          && (  flags & FILTER_DIR
121             || keystring[0] != '.'
122             || keystring[1] && keystring[1] != '.'
123    )  )  )  )
124   {
125   /* Since the filename exists in the filesystem, we can return a
126   non-tainted result. */
127   *result = string_copy_taint(flags & RET_FULL ? filename : keystring, FALSE);
128   return OK;
129   }
130
131 if (errno == ENOENT || errno == 0) return FAIL;
132
133 save_errno = errno;
134 *errmsg = string_sprintf("%s: lstat: %s", filename, strerror(errno));
135 errno = save_errno;
136 return DEFER;
137 }
138
139
140 /*************************************************
141 *              Close entry point                 *
142 *************************************************/
143
144 /* See local README for interface description */
145
146 void
147 static dsearch_close(void *handle)
148 {
149 handle = handle;   /* Avoid compiler warning */
150 }
151
152
153 /*************************************************
154 *         Version reporting entry point          *
155 *************************************************/
156
157 /* See local README for interface description. */
158
159 #include "../version.h"
160
161 gstring *
162 dsearch_version_report(gstring * g)
163 {
164 #ifdef DYNLOOKUP
165 g = string_fmt_append(g, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
166 #endif
167 return g;
168 }
169
170
171 static lookup_info _lookup_info = {
172   .name = US"dsearch",                  /* lookup name */
173   .type = lookup_absfile,               /* uses absolute file name */
174   .open = dsearch_open,                 /* open function */
175   .check = dsearch_check,               /* check function */
176   .find = dsearch_find,                 /* find function */
177   .close = dsearch_close,               /* close function */
178   .tidy = NULL,                         /* no tidy function */
179   .quote = NULL,                        /* no quoting function */
180   .version_report = dsearch_version_report         /* version reporting */
181 };
182
183 #ifdef DYNLOOKUP
184 #define dsearch_lookup_module_info _lookup_module_info
185 #endif
186
187 static lookup_info *_lookup_list[] = { &_lookup_info };
188 lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
189
190 /* End of lookups/dsearch.c */