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