dsearch: filter-matches option
[exim.git] / src / src / lookups / dsearch.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
3386088d 5/* Copyright (c) University of Cambridge 1995 - 2015 */
0756eb3c
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8/* The idea for this code came from Matthew Byng-Maddick, but his original has
0f2cbd1b
MH
9been heavily reworked a lot for Exim 4 (and it now uses stat() (more precisely:
10lstat()) rather than a directory scan). */
0756eb3c
PH
11
12
13#include "../exim.h"
14#include "lf_functions.h"
0756eb3c
PH
15
16
17
18/*************************************************
19* Open entry point *
20*************************************************/
21
22/* See local README for interface description. We open the directory to test
23whether it exists and whether it is searchable. However, we don't need to keep
0f2cbd1b 24it open, because the "search" can be done by a call to lstat() rather than
0756eb3c
PH
25actually scanning through the list of files. */
26
e6d225ae 27static void *
d447dbd1 28dsearch_open(const uschar * dirname, uschar ** errmsg)
0756eb3c 29{
54a2a2a9 30DIR * dp = exim_opendir(dirname);
36b600bb 31if (!dp)
0756eb3c
PH
32 {
33 int save_errno = errno;
34 *errmsg = string_open_failed(errno, "%s for directory search", dirname);
35 errno = save_errno;
36 return NULL;
37 }
38closedir(dp);
39return (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
48integer as this gives warnings on 64-bit systems. */
49
36b600bb 50static BOOL
d447dbd1
JH
51dsearch_check(void * handle, const uschar * filename, int modemask,
52 uid_t * owners, gid_t * owngroups, uschar ** errmsg)
0756eb3c
PH
53{
54handle = handle;
129a5d13
JH
55if (*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);
59return FALSE;
0756eb3c
PH
60}
61
62
63/*************************************************
64* Find entry point *
65*************************************************/
66
a5dc727a 67#define RET_FULL BIT(0)
4aaeadde
JH
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)
a5dc727a 73
0f2cbd1b 74/* See local README for interface description. We use lstat() instead of
0756eb3c
PH
75scanning the directory, as it is hopefully faster to let the OS do the scanning
76for us. */
77
13e70f55 78static int
d447dbd1 79dsearch_find(void * handle, const uschar * dirname, const uschar * keystring,
67a57a5a
JH
80 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
81 const uschar * opts)
0756eb3c
PH
82{
83struct stat statbuf;
84int save_errno;
13e70f55 85uschar * filename;
a5dc727a 86unsigned flags = 0;
0756eb3c
PH
87
88handle = handle; /* Keep picky compilers happy */
89length = length;
90do_cache = do_cache;
91
92if (Ustrchr(keystring, '/') != 0)
93 {
94 *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
95 keystring);
96 return DEFER;
97 }
98
a5dc727a
JH
99if (opts)
100 {
101 int sep = ',';
102 uschar * ele;
103
104 while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
105 if (Ustrcmp(ele, "ret=full") == 0)
106 flags |= RET_FULL;
4aaeadde
JH
107 else if (Ustrncmp(ele, "filter=", 7) == 0)
108 {
109 ele += 7;
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 ".." */
116 }
a5dc727a
JH
117 }
118
13e70f55 119filename = string_sprintf("%s/%s", dirname, keystring);
4aaeadde
JH
120if ( 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]
129 ) ) ) )
0756eb3c 130 {
36b600bb
JH
131 /* Since the filename exists in the filesystem, we can return a
132 non-tainted result. */
a5dc727a 133 *result = string_copy_taint(flags & RET_FULL ? filename : keystring, FALSE);
0756eb3c
PH
134 return OK;
135 }
136
137if (errno == ENOENT) return FAIL;
138
139save_errno = errno;
0f2cbd1b 140*errmsg = string_sprintf("%s: lstat failed", filename);
0756eb3c
PH
141errno = save_errno;
142return DEFER;
143}
144
145
146/*************************************************
147* Close entry point *
148*************************************************/
149
150/* See local README for interface description */
151
152void
e6d225ae 153static dsearch_close(void *handle)
0756eb3c
PH
154{
155handle = handle; /* Avoid compiler warning */
156}
157
6545de78
PP
158
159/*************************************************
160* Version reporting entry point *
161*************************************************/
162
163/* See local README for interface description. */
164
165#include "../version.h"
166
167void
168dsearch_version_report(FILE *f)
169{
170#ifdef DYNLOOKUP
171fprintf(f, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR);
172#endif
173}
174
175
e6d225ae
DW
176static lookup_info _lookup_info = {
177 US"dsearch", /* lookup name */
178 lookup_absfile, /* uses absolute file name */
179 dsearch_open, /* open function */
180 dsearch_check, /* check function */
181 dsearch_find, /* find function */
182 dsearch_close, /* close function */
183 NULL, /* no tidy function */
6545de78
PP
184 NULL, /* no quoting function */
185 dsearch_version_report /* version reporting */
e6d225ae
DW
186};
187
188#ifdef DYNLOOKUP
189#define dsearch_lookup_module_info _lookup_module_info
190#endif
191
192static lookup_info *_lookup_list[] = { &_lookup_info };
193lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
194
0756eb3c 195/* End of lookups/dsearch.c */