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