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