Commit | Line | Data |
---|---|---|
0756eb3c PH |
1 | $Cambridge: exim/src/src/lookups/README,v 1.1 2004/10/07 13:10:01 ph10 Exp $ |
2 | ||
3 | LOOKUPS | |
4 | ------- | |
5 | ||
6 | Each lookup type is implemented by 6 functions, xxx_open(), xxx_check(), | |
7 | xxx_find(), xxx_close(), xxx_tidy(), and xxx_quote(), where xxx is the name of | |
8 | the lookup type (e.g. lsearch, dbm, or whatever). | |
9 | ||
10 | The xxx_check(), xxx_close(), xxx_tidy(), and xxx_quote() functions need not | |
11 | exist. There is a table in drtables.c which links the lookup names to the | |
12 | various sets of functions, with NULL entries for any that don't exist. When | |
13 | the code for a lookup type is omitted from the binary, all its entries are | |
14 | NULL. | |
15 | ||
16 | One of the fields in the table contains flags describing the kind of lookup. | |
17 | These are | |
18 | ||
19 | lookup_querystyle | |
20 | ||
21 | This is a "query style" lookup without a file name, as opposed to the "single | |
22 | key" style, where the key is associated with a "file name". | |
23 | ||
24 | lookup_absfile | |
25 | ||
26 | For single key lookups, this means that the file name must be an absolute path. | |
27 | It is set for lsearch and dbm, but not for NIS, for example. | |
28 | ||
29 | When a single-key lookup file is opened, the handle returned by the xxx_open() | |
30 | function is saved, along with the file name and lookup type, in a tree. The | |
31 | xxx_close() function is not called when the first lookup is completed. If there | |
32 | are subsequent lookups of the same type that quote the same file name, | |
33 | xxx_open() isn't called; instead the cached handle is re-used. | |
34 | ||
35 | Exim calls the function search_tidyup() at strategic points in its processing | |
36 | (e.g. after all routing and directing has been done) and this function walks | |
37 | the tree and calls the xxx_close() functions for all the cached handles. | |
38 | ||
39 | Query-style lookups don't have the concept of an open file that can be cached | |
40 | this way. Of course, the local code for the lookup can manage its own caching | |
41 | information in any way it pleases. This means that the xxx_close() | |
42 | function, even it it exists, is never called. However, if an xxx_tidy() | |
43 | function exists, it is called once whenever Exim calls search_tidyup(). | |
44 | ||
45 | A single-key lookup type may also have an xxx_tidy() function, which is called | |
46 | by search_tidyup() after all cached handles have been closed via the | |
47 | xxx_close() function. | |
48 | ||
49 | The lookup functions are wrapped into a special store pool (POOL_SEARCH). You | |
50 | can safely use store_get to allocate store for your handle caching. The store | |
51 | will be reset after all xxx_tidy() functions are called. | |
52 | ||
53 | The function interfaces are as follows: | |
54 | ||
55 | ||
56 | xxx_open() | |
57 | ---------- | |
58 | ||
59 | This function is called to initiate the lookup. For things that involve files | |
60 | it should do a real open; for other kinds of lookup it may do nothing at all. | |
61 | The arguments are: | |
62 | ||
63 | uschar *filename the name of the "file" to open, for non-query-style | |
64 | lookups; NULL for query-style lookups | |
65 | uschar **errmsg where to put an error message if there is a problem | |
66 | ||
67 | The yield of xxx_open() is a void * value representing the open file or | |
68 | database. For real files is is normally the FILE or DBM value. For other | |
69 | kinds of lookup, if there is no natural value to use, (-1) is recommended. | |
70 | The value should not be NULL (or 0) as that is taken to indicate failure of | |
71 | the xxx_open() function. For single-key lookups, the handle is cached along | |
72 | with the filename and type, and may be used for several lookups. | |
73 | ||
74 | ||
75 | xxx_check() | |
76 | ----------- | |
77 | ||
78 | If this function exists, it is called after a successful open to check on the | |
79 | ownership and mode of the file(s). The arguments are: | |
80 | ||
81 | void *handle the handle passed back from xxx_open() | |
82 | uschar *filename the filename passed to xxx_open() | |
83 | int modemask mode bits that must not be set | |
84 | int *owners permitted owners of the file(s) | |
85 | int *owngroups permitted group owners of the file(s) | |
86 | uschar **errmsg where to put an error message if there is a problem | |
87 | ||
88 | In the owners and owngroups vectors, the first element is the count of the | |
89 | remaining elements. There is a common function that can be called to check | |
90 | a file: | |
91 | ||
92 | int search_check_file(int fd, char *filename, int modemask, int *owners, | |
93 | int *owngroups, char *type, char **errmsg); | |
94 | ||
95 | If fd is >= 0, it is checked using fstat(), and filename is used only in | |
96 | error messages. If fd is < 0 then filename is checked using stat(). The yield | |
97 | is zero if all is well, +1 if the mode or uid or gid is wrong, or -1 if the | |
98 | stat() fails. | |
99 | ||
100 | The yield of xxx_check() is TRUE if all is well, FALSE otherwise. The | |
101 | function should not close the file(s) on failure. That is done from outside | |
102 | by calling the xxx_close() function. | |
103 | ||
104 | ||
105 | xxx_find() | |
106 | ---------- | |
107 | ||
108 | This is called to search an open file/database. The result is OK, FAIL, or | |
109 | DEFER. The arguments are: | |
110 | ||
111 | void *handle the handle passed back from xxx_open() | |
112 | uschar *filename the filename passed to xxx_open() (NULL for querystyle) | |
113 | uschar *keyquery the key to look up, or query to process, zero-terminated | |
114 | int length the length of the key | |
115 | uschar **result point to the yield, in dynamic store, on success | |
116 | uschar **errmsg where to put an error message on failure; | |
117 | this is initially set to "", and should be left | |
118 | as that for a standard "entry not found" error | |
119 | BOOL *do_cache the lookup should set this to FALSE when it changes data. | |
120 | This is TRUE by default. When set to FALSE the cache tree | |
121 | of the current search handle will be cleaned and the | |
122 | current result will NOT be cached. Currently the mysql | |
123 | and pgsql lookups use this when UPDATE/INSERT queries are | |
124 | executed. | |
125 | ||
126 | Even though the key is zero-terminated, the length is passed because in the | |
127 | common case it has been computed already and is often needed. | |
128 | ||
129 | ||
130 | xxx_close() | |
131 | ----------- | |
132 | ||
133 | This is called for single-key lookups when a file is finished with. There is no | |
134 | yield, and the only argument is the handle that was passed back from | |
135 | xxx_open(). It is not called for query style lookups. | |
136 | ||
137 | ||
138 | xxx_tidy() | |
139 | ---------- | |
140 | ||
141 | This function is called once at the end of search_tidyup() for every lookup | |
142 | type for which it exists. | |
143 | ||
144 | ||
145 | xxx_quote() | |
146 | ----------- | |
147 | ||
148 | This is called by the string expansion code for expansions of the form | |
149 | ${quote_xxx:<string>}, if it exists. If not, the expansion code makes no change | |
150 | to the string. The function must apply any quoting rules that are specific to | |
151 | the lookup, and return a pointer to the revised string. If quoting is not | |
152 | needed, it can return its single argument, which is a uschar *. This function | |
153 | does NOT use the POOL_SEARCH store, because it's usually never called from any | |
154 | lookup code. | |
155 | ||
156 | **** |