constification
[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 *
0756eb3c
PH
21nisplus_open(uschar *filename, uschar **errmsg)
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
36a7aaa4 45nisplus_find(void *handle, uschar *filename, const uschar *query, int length,
14b3c5bc 46 uschar **result, uschar **errmsg, uint *do_cache)
0756eb3c 47{
0756eb3c 48int error_error = FAIL;
36a7aaa4 49const uschar * field_name = NULL;
0756eb3c
PH
50nis_result *nrt = NULL;
51nis_result *nre = NULL;
52nis_object *tno, *eno;
53struct entry_obj *eo;
54struct table_obj *ta;
36a7aaa4 55const uschar * p = query + length;
acec9514 56gstring * yield = NULL;
0756eb3c
PH
57
58do_cache = do_cache; /* Placate picky compilers */
59
60/* Search backwards for a colon to see if a result field name
61has been given. */
62
63while (p > query && p[-1] != ':') p--;
64
36a7aaa4 65if (p > query) /* get the query without the result-field */
0756eb3c 66 {
36a7aaa4 67 uint len = p-1 - query;
0756eb3c 68 field_name = p;
36a7aaa4
JH
69 query = string_copyn(query, len);
70 p = query + len;
0756eb3c 71 }
36a7aaa4
JH
72else
73 p = query + length;
0756eb3c
PH
74
75/* Now search backwards to find the comma that starts the
76table name. */
77
78while (p > query && p[-1] != ',') p--;
79if (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,
87check that we got back a table, and set up pointers so the field
88names can be scanned. */
89
90nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE);
91if (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 }
99tno = nrt->objects.objects_val;
100if (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 }
36a7aaa4 105ta = &tno->zo_data.objdata_u.ta_data;
0756eb3c
PH
106
107/* Now look up the entry in the table, check that we got precisely one
108object and that it is a table entry. */
109
110nre = nis_list(CS query, EXPAND_NAME, NULL, NULL);
111if (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 }
117if (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 }
123else if (nre->objects.objects_len < 1)
124 {
125 *errmsg = string_sprintf("NIS+ returned no data for %s", query);
126 goto NISPLUS_EXIT;
127 }
128eno = nre->objects.objects_val;
129if (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
136was given, look for that field; otherwise concatenate all the fields
137with their names. */
138
139eo = &(eno->zo_data.objdata_u.en_data);
d7978c0f 140for (int i = 0; i < eo->en_cols.en_cols_len; i++)
0756eb3c
PH
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
36a7aaa4 157 if (!field_name)
0756eb3c 158 {
acec9514
JH
159 yield = string_cat (yield, tc->tc_name);
160 yield = string_catn(yield, US"=", 1);
0756eb3c
PH
161
162 /* Quote the value if it contains spaces or is empty */
163
164 if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
165 {
acec9514 166 yield = string_catn(yield, US"\"", 1);
d7978c0f 167 for (int j = 0; j < len; j++)
0756eb3c
PH
168 {
169 if (value[j] == '\"' || value[j] == '\\')
acec9514
JH
170 yield = string_catn(yield, US"\\", 1);
171 yield = string_catn(yield, value+j, 1);
0756eb3c 172 }
acec9514 173 yield = string_catn(yield, US"\"", 1);
0756eb3c 174 }
acec9514 175 else
8dcf2dc4 176 yield = string_catn(yield, value, len);
0756eb3c 177
acec9514 178 yield = string_catn(yield, US" ", 1);
0756eb3c
PH
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 {
acec9514 185 yield = string_catn(yield, value, len);
0756eb3c
PH
186 goto NISPLUS_EXIT;
187 }
188 }
189
190/* Error if a field name was specified and we didn't find it; if no
191field name, ensure the concatenated data is zero-terminated. */
192
acec9514 193if (field_name)
0756eb3c
PH
194 *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
195 query);
196else
acec9514 197 store_reset(yield->s + yield->ptr + 1);
0756eb3c 198
36a7aaa4 199/* Free result store before finishing. */
0756eb3c
PH
200
201NISPLUS_EXIT:
acec9514
JH
202if (nrt) nis_freeresult(nrt);
203if (nre) nis_freeresult(nre);
0756eb3c 204
acec9514 205if (yield)
0756eb3c 206 {
acec9514 207 *result = string_from_gstring(yield);
0756eb3c
PH
208 return OK;
209 }
210
211return error_error; /* FAIL or DEFER */
212}
213
214
215
216/*************************************************
217* Quote entry point *
218*************************************************/
219
220/* The only quoting that is necessary for NIS+ is to double any doublequote
221characters. No options are recognized.
222
223Arguments:
224 s the string to be quoted
225 opt additional option text or NULL if none
226
227Returns: the processed string or NULL for a bad option
228*/
229
e6d225ae 230static uschar *
0756eb3c
PH
231nisplus_quote(uschar *s, uschar *opt)
232{
233int count = 0;
234uschar *quoted;
235uschar *t = s;
236
237if (opt != NULL) return NULL; /* No options recognized */
238
239while (*t != 0) if (*t++ == '\"') count++;
240if (count == 0) return s;
241
242t = quoted = store_get(Ustrlen(s) + count + 1);
243
244while (*s != 0)
245 {
246 *t++ = *s;
247 if (*s++ == '\"') *t++ = '\"';
248 }
249
250*t = 0;
251return quoted;
252}
253
6545de78
PP
254
255/*************************************************
256* Version reporting entry point *
257*************************************************/
258
259/* See local README for interface description. */
260
261#include "../version.h"
262
263void
264nisplus_version_report(FILE *f)
265{
266#ifdef DYNLOOKUP
267fprintf(f, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR);
268#endif
269}
270
271
e6d225ae
DW
272static lookup_info _lookup_info = {
273 US"nisplus", /* lookup name */
274 lookup_querystyle, /* query-style lookup */
275 nisplus_open, /* open function */
276 NULL, /* check function */
277 nisplus_find, /* find function */
278 NULL, /* no close function */
279 NULL, /* no tidy function */
6545de78
PP
280 nisplus_quote, /* quoting function */
281 nisplus_version_report /* version reporting */
e6d225ae
DW
282};
283
284#ifdef DYNLOOKUP
285#define nisplus_lookup_module_info _lookup_module_info
286#endif
287
288static lookup_info *_lookup_list[] = { &_lookup_info };
289lookup_module_info nisplus_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
0756eb3c
PH
290
291/* End of lookups/nisplus.c */