Module loading working on FreeBSD (and unbreak).
[exim.git] / src / src / lookups / nisplus.c
CommitLineData
0a49a7a4 1/* $Cambridge: exim/src/src/lookups/nisplus.c,v 1.5 2009/11/16 19:50:38 nm4 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
0a49a7a4 7/* Copyright (c) University of Cambridge 1995 - 2009 */
0756eb3c
PH
8/* See the file NOTICE for conditions of use and distribution. */
9
10#include "../exim.h"
11#include "lf_functions.h"
0756eb3c
PH
12
13#include <rpcsvc/nis.h>
14
15
16/*************************************************
17* Open entry point *
18*************************************************/
19
20/* See local README for interface description. */
21
e6d225ae 22static void *
0756eb3c
PH
23nisplus_open(uschar *filename, uschar **errmsg)
24{
25return (void *)(1); /* Just return something non-null */
26}
27
28
29
30/*************************************************
31* Find entry point *
32*************************************************/
33
34/* See local README for interface description. The format of queries for a
35NIS+ search is
36
37 [field=value,...],table-name
38or
39 [field=value,...],table-name:result-field-name
40
41in other words, a normal NIS+ "indexed name", with an optional result field
42name tagged on the end after a colon. If there is no result-field name, the
43yield is the concatenation of all the fields, preceded by their names and an
44equals sign. */
45
e6d225ae 46static int
0756eb3c
PH
47nisplus_find(void *handle, uschar *filename, uschar *query, int length,
48 uschar **result, uschar **errmsg, BOOL *do_cache)
49{
50int i;
51int ssize = 0;
52int offset = 0;
53int error_error = FAIL;
54uschar *field_name = NULL;
55nis_result *nrt = NULL;
56nis_result *nre = NULL;
57nis_object *tno, *eno;
58struct entry_obj *eo;
59struct table_obj *ta;
60uschar *p = query + length;
61uschar *yield = NULL;
62
63do_cache = do_cache; /* Placate picky compilers */
64
65/* Search backwards for a colon to see if a result field name
66has been given. */
67
68while (p > query && p[-1] != ':') p--;
69
70if (p > query)
71 {
72 field_name = p;
73 p[-1] = 0;
74 }
75else p = query + length;
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 }
107ta = &(tno->zo_data.objdata_u.ta_data);
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);
142for (i = 0; i < eo->en_cols.en_cols_len; i++)
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
153 if (value == NULL) value = US""; else
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
159 if (field_name == NULL)
160 {
161 yield = string_cat(yield, &ssize, &offset,US tc->tc_name,
162 Ustrlen(tc->tc_name));
163 yield = string_cat(yield, &ssize, &offset, US"=", 1);
164
165 /* Quote the value if it contains spaces or is empty */
166
167 if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
168 {
169 int j;
170 yield = string_cat(yield, &ssize, &offset, US"\"", 1);
171 for (j = 0; j < len; j++)
172 {
173 if (value[j] == '\"' || value[j] == '\\')
174 yield = string_cat(yield, &ssize, &offset, US"\\", 1);
175 yield = string_cat(yield, &ssize, &offset, value+j, 1);
176 }
177 yield = string_cat(yield, &ssize, &offset, US"\"", 1);
178 }
179 else yield = string_cat(yield, &ssize, &offset, value, len);
180
181 yield = string_cat(yield, &ssize, &offset, US" ", 1);
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 {
188 yield = string_copyn(value, len);
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
196if (field_name != NULL)
197 *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
198 query);
199else
200 {
201 yield[offset] = 0;
202 store_reset(yield + offset + 1);
203 }
204
205/* Restore the colon in the query, and free result store before
206finishing. */
207
208NISPLUS_EXIT:
209if (field_name != NULL) field_name[-1] = ':';
210if (nrt != NULL) nis_freeresult(nrt);
211if (nre != NULL) nis_freeresult(nre);
212
213if (yield != NULL)
214 {
215 *result = yield;
216 return OK;
217 }
218
219return error_error; /* FAIL or DEFER */
220}
221
222
223
224/*************************************************
225* Quote entry point *
226*************************************************/
227
228/* The only quoting that is necessary for NIS+ is to double any doublequote
229characters. No options are recognized.
230
231Arguments:
232 s the string to be quoted
233 opt additional option text or NULL if none
234
235Returns: the processed string or NULL for a bad option
236*/
237
e6d225ae 238static uschar *
0756eb3c
PH
239nisplus_quote(uschar *s, uschar *opt)
240{
241int count = 0;
242uschar *quoted;
243uschar *t = s;
244
245if (opt != NULL) return NULL; /* No options recognized */
246
247while (*t != 0) if (*t++ == '\"') count++;
248if (count == 0) return s;
249
250t = quoted = store_get(Ustrlen(s) + count + 1);
251
252while (*s != 0)
253 {
254 *t++ = *s;
255 if (*s++ == '\"') *t++ = '\"';
256 }
257
258*t = 0;
259return quoted;
260}
261
e6d225ae
DW
262static lookup_info _lookup_info = {
263 US"nisplus", /* lookup name */
264 lookup_querystyle, /* query-style lookup */
265 nisplus_open, /* open function */
266 NULL, /* check function */
267 nisplus_find, /* find function */
268 NULL, /* no close function */
269 NULL, /* no tidy function */
270 nisplus_quote /* quoting function */
271};
272
273#ifdef DYNLOOKUP
274#define nisplus_lookup_module_info _lookup_module_info
275#endif
276
277static lookup_info *_lookup_list[] = { &_lookup_info };
278lookup_module_info nisplus_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
0756eb3c
PH
279
280/* End of lookups/nisplus.c */