Add dynamic lookup support
[exim.git] / src / src / lookups / dsearch.c
1 /* $Cambridge: exim/src/src/lookups/dsearch.c,v 1.6 2009/11/16 19:50:38 nm4 Exp $ */
2
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
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(uschar *dirname, uschar **errmsg)
31 {
32 DIR *dp = opendir(CS dirname);
33 if (dp == NULL)
34 {
35 int save_errno = errno;
36 *errmsg = string_open_failed(errno, "%s for directory search", dirname);
37 errno = save_errno;
38 return NULL;
39 }
40 closedir(dp);
41 return (void *)(-1);
42 }
43
44
45 /*************************************************
46 * Check entry point *
47 *************************************************/
48
49 /* The handle will always be (void *)(-1), but don't try casting it to an
50 integer as this gives warnings on 64-bit systems. */
51
52 BOOL
53 static dsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
54 gid_t *owngroups, uschar **errmsg)
55 {
56 handle = handle;
57 return lf_check_file(-1, filename, S_IFDIR, modemask, owners, owngroups,
58 "dsearch", errmsg) == 0;
59 }
60
61
62 /*************************************************
63 * Find entry point *
64 *************************************************/
65
66 /* See local README for interface description. We use lstat() instead of
67 scanning the directory, as it is hopefully faster to let the OS do the scanning
68 for us. */
69
70 int
71 static dsearch_find(void *handle, uschar *dirname, uschar *keystring, int length,
72 uschar **result, uschar **errmsg, BOOL *do_cache)
73 {
74 struct stat statbuf;
75 int save_errno;
76 uschar filename[PATH_MAX];
77
78 handle = handle; /* Keep picky compilers happy */
79 length = length;
80 do_cache = do_cache;
81
82 if (Ustrchr(keystring, '/') != 0)
83 {
84 *errmsg = string_sprintf("key for dsearch lookup contains a slash: %s",
85 keystring);
86 return DEFER;
87 }
88
89 if (!string_format(filename, sizeof(filename), "%s/%s", dirname, keystring))
90 {
91 *errmsg = US"path name too long";
92 return DEFER;
93 }
94
95 if (Ulstat(filename, &statbuf) >= 0)
96 {
97 *result = string_copy(keystring);
98 return OK;
99 }
100
101 if (errno == ENOENT) return FAIL;
102
103 save_errno = errno;
104 *errmsg = string_sprintf("%s: lstat failed", filename);
105 errno = save_errno;
106 return DEFER;
107 }
108
109
110 /*************************************************
111 * Close entry point *
112 *************************************************/
113
114 /* See local README for interface description */
115
116 void
117 static dsearch_close(void *handle)
118 {
119 handle = handle; /* Avoid compiler warning */
120 }
121
122 static lookup_info _lookup_info = {
123 US"dsearch", /* lookup name */
124 lookup_absfile, /* uses absolute file name */
125 dsearch_open, /* open function */
126 dsearch_check, /* check function */
127 dsearch_find, /* find function */
128 dsearch_close, /* close function */
129 NULL, /* no tidy function */
130 NULL /* no quoting function */
131 };
132
133 #ifdef DYNLOOKUP
134 #define dsearch_lookup_module_info _lookup_module_info
135 #endif
136
137 static lookup_info *_lookup_list[] = { &_lookup_info };
138 lookup_module_info dsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
139
140 /* End of lookups/dsearch.c */