Commit | Line | Data |
---|---|---|
0756eb3c PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
0a49a7a4 | 5 | /* Copyright (c) University of Cambridge 1995 - 2009 */ |
0756eb3c PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
8 | #include "../exim.h" | |
9 | #include "lf_functions.h" | |
0756eb3c PH |
10 | |
11 | #include <rpcsvc/nis.h> | |
12 | ||
13 | ||
14 | /************************************************* | |
15 | * Open entry point * | |
16 | *************************************************/ | |
17 | ||
18 | /* See local README for interface description. */ | |
19 | ||
e6d225ae | 20 | static void * |
0756eb3c PH |
21 | nisplus_open(uschar *filename, uschar **errmsg) |
22 | { | |
23 | return (void *)(1); /* Just return something non-null */ | |
24 | } | |
25 | ||
26 | ||
27 | ||
28 | /************************************************* | |
29 | * Find entry point * | |
30 | *************************************************/ | |
31 | ||
32 | /* See local README for interface description. The format of queries for a | |
33 | NIS+ search is | |
34 | ||
35 | [field=value,...],table-name | |
36 | or | |
37 | [field=value,...],table-name:result-field-name | |
38 | ||
39 | in other words, a normal NIS+ "indexed name", with an optional result field | |
40 | name tagged on the end after a colon. If there is no result-field name, the | |
41 | yield is the concatenation of all the fields, preceded by their names and an | |
42 | equals sign. */ | |
43 | ||
e6d225ae | 44 | static int |
0756eb3c | 45 | nisplus_find(void *handle, uschar *filename, uschar *query, int length, |
14b3c5bc | 46 | uschar **result, uschar **errmsg, uint *do_cache) |
0756eb3c PH |
47 | { |
48 | int i; | |
49 | int ssize = 0; | |
50 | int offset = 0; | |
51 | int error_error = FAIL; | |
52 | uschar *field_name = NULL; | |
53 | nis_result *nrt = NULL; | |
54 | nis_result *nre = NULL; | |
55 | nis_object *tno, *eno; | |
56 | struct entry_obj *eo; | |
57 | struct table_obj *ta; | |
58 | uschar *p = query + length; | |
59 | uschar *yield = NULL; | |
60 | ||
61 | do_cache = do_cache; /* Placate picky compilers */ | |
62 | ||
63 | /* Search backwards for a colon to see if a result field name | |
64 | has been given. */ | |
65 | ||
66 | while (p > query && p[-1] != ':') p--; | |
67 | ||
68 | if (p > query) | |
69 | { | |
70 | field_name = p; | |
71 | p[-1] = 0; | |
72 | } | |
73 | else p = query + length; | |
74 | ||
75 | /* Now search backwards to find the comma that starts the | |
76 | table name. */ | |
77 | ||
78 | while (p > query && p[-1] != ',') p--; | |
79 | if (p <= query) | |
80 | { | |
81 | *errmsg = US"NIS+ query malformed"; | |
82 | error_error = DEFER; | |
83 | goto NISPLUS_EXIT; | |
84 | } | |
85 | ||
86 | /* Look up the data for the table, in order to get the field names, | |
87 | check that we got back a table, and set up pointers so the field | |
88 | names can be scanned. */ | |
89 | ||
90 | nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE); | |
91 | if (nrt->status != NIS_SUCCESS) | |
92 | { | |
93 | *errmsg = string_sprintf("NIS+ error accessing %s table: %s", p, | |
94 | nis_sperrno(nrt->status)); | |
95 | if (nrt->status != NIS_NOTFOUND && nrt->status != NIS_NOSUCHTABLE) | |
96 | error_error = DEFER; | |
97 | goto NISPLUS_EXIT; | |
98 | } | |
99 | tno = nrt->objects.objects_val; | |
100 | if (tno->zo_data.zo_type != TABLE_OBJ) | |
101 | { | |
102 | *errmsg = string_sprintf("NIS+ error: %s is not a table", p); | |
103 | goto NISPLUS_EXIT; | |
104 | } | |
105 | ta = &(tno->zo_data.objdata_u.ta_data); | |
106 | ||
107 | /* Now look up the entry in the table, check that we got precisely one | |
108 | object and that it is a table entry. */ | |
109 | ||
110 | nre = nis_list(CS query, EXPAND_NAME, NULL, NULL); | |
111 | if (nre->status != NIS_SUCCESS) | |
112 | { | |
113 | *errmsg = string_sprintf("NIS+ error accessing entry %s: %s", | |
114 | query, nis_sperrno(nre->status)); | |
115 | goto NISPLUS_EXIT; | |
116 | } | |
117 | if (nre->objects.objects_len > 1) | |
118 | { | |
119 | *errmsg = string_sprintf("NIS+ returned more than one object for %s", | |
120 | query); | |
121 | goto NISPLUS_EXIT; | |
122 | } | |
123 | else if (nre->objects.objects_len < 1) | |
124 | { | |
125 | *errmsg = string_sprintf("NIS+ returned no data for %s", query); | |
126 | goto NISPLUS_EXIT; | |
127 | } | |
128 | eno = nre->objects.objects_val; | |
129 | if (eno->zo_data.zo_type != ENTRY_OBJ) | |
130 | { | |
131 | *errmsg = string_sprintf("NIS+ error: %s is not an entry", query); | |
132 | goto NISPLUS_EXIT; | |
133 | } | |
134 | ||
135 | /* Scan the columns in the entry and in the table. If a result field | |
136 | was given, look for that field; otherwise concatenate all the fields | |
137 | with their names. */ | |
138 | ||
139 | eo = &(eno->zo_data.objdata_u.en_data); | |
140 | for (i = 0; i < eo->en_cols.en_cols_len; i++) | |
141 | { | |
142 | table_col *tc = ta->ta_cols.ta_cols_val + i; | |
143 | entry_col *ec = eo->en_cols.en_cols_val + i; | |
144 | int len = ec->ec_value.ec_value_len; | |
145 | uschar *value = US ec->ec_value.ec_value_val; | |
146 | ||
147 | /* The value may be NULL for a zero-length field. Turn this into an | |
148 | empty string for consistency. Remove trailing whitespace and zero | |
149 | bytes. */ | |
150 | ||
151 | if (value == NULL) value = US""; else | |
152 | while (len > 0 && (value[len-1] == 0 || isspace(value[len-1]))) | |
153 | len--; | |
154 | ||
155 | /* Concatenate all fields if no specific one selected */ | |
156 | ||
157 | if (field_name == NULL) | |
158 | { | |
159 | yield = string_cat(yield, &ssize, &offset,US tc->tc_name, | |
160 | Ustrlen(tc->tc_name)); | |
161 | yield = string_cat(yield, &ssize, &offset, US"=", 1); | |
162 | ||
163 | /* Quote the value if it contains spaces or is empty */ | |
164 | ||
165 | if (value[0] == 0 || Ustrchr(value, ' ') != NULL) | |
166 | { | |
167 | int j; | |
168 | yield = string_cat(yield, &ssize, &offset, US"\"", 1); | |
169 | for (j = 0; j < len; j++) | |
170 | { | |
171 | if (value[j] == '\"' || value[j] == '\\') | |
172 | yield = string_cat(yield, &ssize, &offset, US"\\", 1); | |
173 | yield = string_cat(yield, &ssize, &offset, value+j, 1); | |
174 | } | |
175 | yield = string_cat(yield, &ssize, &offset, US"\"", 1); | |
176 | } | |
177 | else yield = string_cat(yield, &ssize, &offset, value, len); | |
178 | ||
179 | yield = string_cat(yield, &ssize, &offset, US" ", 1); | |
180 | } | |
181 | ||
182 | /* When the specified field is found, grab its data and finish */ | |
183 | ||
184 | else if (Ustrcmp(field_name, tc->tc_name) == 0) | |
185 | { | |
186 | yield = string_copyn(value, len); | |
187 | goto NISPLUS_EXIT; | |
188 | } | |
189 | } | |
190 | ||
191 | /* Error if a field name was specified and we didn't find it; if no | |
192 | field name, ensure the concatenated data is zero-terminated. */ | |
193 | ||
194 | if (field_name != NULL) | |
195 | *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name, | |
196 | query); | |
197 | else | |
198 | { | |
199 | yield[offset] = 0; | |
200 | store_reset(yield + offset + 1); | |
201 | } | |
202 | ||
203 | /* Restore the colon in the query, and free result store before | |
204 | finishing. */ | |
205 | ||
206 | NISPLUS_EXIT: | |
207 | if (field_name != NULL) field_name[-1] = ':'; | |
208 | if (nrt != NULL) nis_freeresult(nrt); | |
209 | if (nre != NULL) nis_freeresult(nre); | |
210 | ||
211 | if (yield != NULL) | |
212 | { | |
213 | *result = yield; | |
214 | return OK; | |
215 | } | |
216 | ||
217 | return error_error; /* FAIL or DEFER */ | |
218 | } | |
219 | ||
220 | ||
221 | ||
222 | /************************************************* | |
223 | * Quote entry point * | |
224 | *************************************************/ | |
225 | ||
226 | /* The only quoting that is necessary for NIS+ is to double any doublequote | |
227 | characters. No options are recognized. | |
228 | ||
229 | Arguments: | |
230 | s the string to be quoted | |
231 | opt additional option text or NULL if none | |
232 | ||
233 | Returns: the processed string or NULL for a bad option | |
234 | */ | |
235 | ||
e6d225ae | 236 | static uschar * |
0756eb3c PH |
237 | nisplus_quote(uschar *s, uschar *opt) |
238 | { | |
239 | int count = 0; | |
240 | uschar *quoted; | |
241 | uschar *t = s; | |
242 | ||
243 | if (opt != NULL) return NULL; /* No options recognized */ | |
244 | ||
245 | while (*t != 0) if (*t++ == '\"') count++; | |
246 | if (count == 0) return s; | |
247 | ||
248 | t = quoted = store_get(Ustrlen(s) + count + 1); | |
249 | ||
250 | while (*s != 0) | |
251 | { | |
252 | *t++ = *s; | |
253 | if (*s++ == '\"') *t++ = '\"'; | |
254 | } | |
255 | ||
256 | *t = 0; | |
257 | return quoted; | |
258 | } | |
259 | ||
6545de78 PP |
260 | |
261 | /************************************************* | |
262 | * Version reporting entry point * | |
263 | *************************************************/ | |
264 | ||
265 | /* See local README for interface description. */ | |
266 | ||
267 | #include "../version.h" | |
268 | ||
269 | void | |
270 | nisplus_version_report(FILE *f) | |
271 | { | |
272 | #ifdef DYNLOOKUP | |
273 | fprintf(f, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR); | |
274 | #endif | |
275 | } | |
276 | ||
277 | ||
e6d225ae DW |
278 | static lookup_info _lookup_info = { |
279 | US"nisplus", /* lookup name */ | |
280 | lookup_querystyle, /* query-style lookup */ | |
281 | nisplus_open, /* open function */ | |
282 | NULL, /* check function */ | |
283 | nisplus_find, /* find function */ | |
284 | NULL, /* no close function */ | |
285 | NULL, /* no tidy function */ | |
6545de78 PP |
286 | nisplus_quote, /* quoting function */ |
287 | nisplus_version_report /* version reporting */ | |
e6d225ae DW |
288 | }; |
289 | ||
290 | #ifdef DYNLOOKUP | |
291 | #define nisplus_lookup_module_info _lookup_module_info | |
292 | #endif | |
293 | ||
294 | static lookup_info *_lookup_list[] = { &_lookup_info }; | |
295 | lookup_module_info nisplus_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 }; | |
0756eb3c PH |
296 | |
297 | /* End of lookups/nisplus.c */ |