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