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