Commit | Line | Data |
---|---|---|
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 |
9 | been heavily reworked a lot for Exim 4 (and it now uses stat() (more precisely: |
10 | lstat()) 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 | |
23 | whether it exists and whether it is searchable. However, we don't need to keep | |
0f2cbd1b | 24 | it open, because the "search" can be done by a call to lstat() rather than |
0756eb3c PH |
25 | actually scanning through the list of files. */ |
26 | ||
e6d225ae | 27 | static void * |
0756eb3c PH |
28 | dsearch_open(uschar *dirname, uschar **errmsg) |
29 | { | |
30 | DIR *dp = opendir(CS dirname); | |
31 | if (dp == NULL) | |
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 | } | |
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 | BOOL | |
e6d225ae | 51 | static dsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners, |
0756eb3c PH |
52 | gid_t *owngroups, uschar **errmsg) |
53 | { | |
54 | handle = handle; | |
55 | return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups, | |
56 | "dsearch", errmsg) == 0; | |
57 | } | |
58 | ||
59 | ||
60 | /************************************************* | |
61 | * Find entry point * | |
62 | *************************************************/ | |
63 | ||
0f2cbd1b | 64 | /* See local README for interface description. We use lstat() instead of |
0756eb3c PH |
65 | scanning the directory, as it is hopefully faster to let the OS do the scanning |
66 | for us. */ | |
67 | ||
218c95cc JH |
68 | static int |
69 | dsearch_find(void *handle, uschar *dirname, const uschar *keystring, int length, | |
14b3c5bc | 70 | uschar **result, uschar **errmsg, uint *do_cache) |
0756eb3c PH |
71 | { |
72 | struct stat statbuf; | |
73 | int save_errno; | |
218c95cc | 74 | uschar * filename; |
0756eb3c PH |
75 | |
76 | handle = handle; /* Keep picky compilers happy */ | |
77 | length = length; | |
78 | do_cache = do_cache; | |
79 | ||
80 | if (Ustrchr(keystring, '/') != 0) | |
81 | { | |
82 | *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s", | |
83 | keystring); | |
84 | return DEFER; | |
85 | } | |
86 | ||
218c95cc | 87 | filename = string_sprintf("%s/%s", dirname, keystring); |
0f2cbd1b | 88 | if (Ulstat(filename, &statbuf) >= 0) |
0756eb3c PH |
89 | { |
90 | *result = string_copy(keystring); | |
91 | return OK; | |
92 | } | |
93 | ||
94 | if (errno == ENOENT) return FAIL; | |
95 | ||
96 | save_errno = errno; | |
0f2cbd1b | 97 | *errmsg = string_sprintf("%s: lstat failed", filename); |
0756eb3c PH |
98 | errno = save_errno; |
99 | return DEFER; | |
100 | } | |
101 | ||
102 | ||
103 | /************************************************* | |
104 | * Close entry point * | |
105 | *************************************************/ | |
106 | ||
107 | /* See local README for interface description */ | |
108 | ||
109 | void | |
e6d225ae | 110 | static dsearch_close(void *handle) |
0756eb3c PH |
111 | { |
112 | handle = handle; /* Avoid compiler warning */ | |
113 | } | |
114 | ||
6545de78 PP |
115 | |
116 | /************************************************* | |
117 | * Version reporting entry point * | |
118 | *************************************************/ | |
119 | ||
120 | /* See local README for interface description. */ | |
121 | ||
122 | #include "../version.h" | |
123 | ||
124 | void | |
125 | dsearch_version_report(FILE *f) | |
126 | { | |
127 | #ifdef DYNLOOKUP | |
128 | fprintf(f, "Library version: dsearch: Exim version %s\n", EXIM_VERSION_STR); | |
129 | #endif | |
130 | } | |
131 | ||
132 | ||
e6d225ae DW |
133 | static lookup_info _lookup_info = { |
134 | US"dsearch", /* lookup name */ | |
135 | lookup_absfile, /* uses absolute file name */ | |
136 | dsearch_open, /* open function */ | |
137 | dsearch_check, /* check function */ | |
138 | dsearch_find, /* find function */ | |
139 | dsearch_close, /* close function */ | |
140 | NULL, /* no tidy function */ | |
6545de78 PP |
141 | NULL, /* no quoting function */ |
142 | dsearch_version_report /* version reporting */ | |
e6d225ae DW |
143 | }; |
144 | ||
145 | #ifdef DYNLOOKUP | |
146 | #define dsearch_lookup_module_info _lookup_module_info | |
147 | #endif | |
148 | ||
149 | static lookup_info *_lookup_list[] = { &_lookup_info }; | |
150 | lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 }; | |
151 | ||
0756eb3c | 152 | /* End of lookups/dsearch.c */ |