Update version number and copyright year.
[exim.git] / src / src / lookups / nisplus.c
CommitLineData
184e8823 1/* $Cambridge: exim/src/src/lookups/nisplus.c,v 1.4 2007/01/08 10:50:19 ph10 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
184e8823 7/* Copyright (c) University of Cambridge 1995 - 2007 */
0756eb3c
PH
8/* See the file NOTICE for conditions of use and distribution. */
9
10#include "../exim.h"
11#include "lf_functions.h"
12#include "nisplus.h"
13
14/* We can't just compile this code and allow the library mechanism to omit the
15functions if they are not wanted, because we need to have the NIS+ header
16available for compiling. Therefore, compile these functions only if
17LOOKUP_NISPLUS is defined. However, some compilers don't like compiling empty
18modules, so keep them happy with a dummy when skipping the rest. Make it
19reference itself to stop picky compilers complaining that it is unused, and put
20in a dummy argument to stop even pickier compilers complaining about infinite
21loops. */
22
23#ifndef LOOKUP_NISPLUS
24static void dummy(int x) { dummy(x-1); }
25#else
26
27
28#include <rpcsvc/nis.h>
29
30
31/*************************************************
32* Open entry point *
33*************************************************/
34
35/* See local README for interface description. */
36
37void *
38nisplus_open(uschar *filename, uschar **errmsg)
39{
40return (void *)(1); /* Just return something non-null */
41}
42
43
44
45/*************************************************
46* Find entry point *
47*************************************************/
48
49/* See local README for interface description. The format of queries for a
50NIS+ search is
51
52 [field=value,...],table-name
53or
54 [field=value,...],table-name:result-field-name
55
56in other words, a normal NIS+ "indexed name", with an optional result field
57name tagged on the end after a colon. If there is no result-field name, the
58yield is the concatenation of all the fields, preceded by their names and an
59equals sign. */
60
61int
62nisplus_find(void *handle, uschar *filename, uschar *query, int length,
63 uschar **result, uschar **errmsg, BOOL *do_cache)
64{
65int i;
66int ssize = 0;
67int offset = 0;
68int error_error = FAIL;
69uschar *field_name = NULL;
70nis_result *nrt = NULL;
71nis_result *nre = NULL;
72nis_object *tno, *eno;
73struct entry_obj *eo;
74struct table_obj *ta;
75uschar *p = query + length;
76uschar *yield = NULL;
77
78do_cache = do_cache; /* Placate picky compilers */
79
80/* Search backwards for a colon to see if a result field name
81has been given. */
82
83while (p > query && p[-1] != ':') p--;
84
85if (p > query)
86 {
87 field_name = p;
88 p[-1] = 0;
89 }
90else p = query + length;
91
92/* Now search backwards to find the comma that starts the
93table name. */
94
95while (p > query && p[-1] != ',') p--;
96if (p <= query)
97 {
98 *errmsg = US"NIS+ query malformed";
99 error_error = DEFER;
100 goto NISPLUS_EXIT;
101 }
102
103/* Look up the data for the table, in order to get the field names,
104check that we got back a table, and set up pointers so the field
105names can be scanned. */
106
107nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE);
108if (nrt->status != NIS_SUCCESS)
109 {
110 *errmsg = string_sprintf("NIS+ error accessing %s table: %s", p,
111 nis_sperrno(nrt->status));
112 if (nrt->status != NIS_NOTFOUND && nrt->status != NIS_NOSUCHTABLE)
113 error_error = DEFER;
114 goto NISPLUS_EXIT;
115 }
116tno = nrt->objects.objects_val;
117if (tno->zo_data.zo_type != TABLE_OBJ)
118 {
119 *errmsg = string_sprintf("NIS+ error: %s is not a table", p);
120 goto NISPLUS_EXIT;
121 }
122ta = &(tno->zo_data.objdata_u.ta_data);
123
124/* Now look up the entry in the table, check that we got precisely one
125object and that it is a table entry. */
126
127nre = nis_list(CS query, EXPAND_NAME, NULL, NULL);
128if (nre->status != NIS_SUCCESS)
129 {
130 *errmsg = string_sprintf("NIS+ error accessing entry %s: %s",
131 query, nis_sperrno(nre->status));
132 goto NISPLUS_EXIT;
133 }
134if (nre->objects.objects_len > 1)
135 {
136 *errmsg = string_sprintf("NIS+ returned more than one object for %s",
137 query);
138 goto NISPLUS_EXIT;
139 }
140else if (nre->objects.objects_len < 1)
141 {
142 *errmsg = string_sprintf("NIS+ returned no data for %s", query);
143 goto NISPLUS_EXIT;
144 }
145eno = nre->objects.objects_val;
146if (eno->zo_data.zo_type != ENTRY_OBJ)
147 {
148 *errmsg = string_sprintf("NIS+ error: %s is not an entry", query);
149 goto NISPLUS_EXIT;
150 }
151
152/* Scan the columns in the entry and in the table. If a result field
153was given, look for that field; otherwise concatenate all the fields
154with their names. */
155
156eo = &(eno->zo_data.objdata_u.en_data);
157for (i = 0; i < eo->en_cols.en_cols_len; i++)
158 {
159 table_col *tc = ta->ta_cols.ta_cols_val + i;
160 entry_col *ec = eo->en_cols.en_cols_val + i;
161 int len = ec->ec_value.ec_value_len;
162 uschar *value = US ec->ec_value.ec_value_val;
163
164 /* The value may be NULL for a zero-length field. Turn this into an
165 empty string for consistency. Remove trailing whitespace and zero
166 bytes. */
167
168 if (value == NULL) value = US""; else
169 while (len > 0 && (value[len-1] == 0 || isspace(value[len-1])))
170 len--;
171
172 /* Concatenate all fields if no specific one selected */
173
174 if (field_name == NULL)
175 {
176 yield = string_cat(yield, &ssize, &offset,US tc->tc_name,
177 Ustrlen(tc->tc_name));
178 yield = string_cat(yield, &ssize, &offset, US"=", 1);
179
180 /* Quote the value if it contains spaces or is empty */
181
182 if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
183 {
184 int j;
185 yield = string_cat(yield, &ssize, &offset, US"\"", 1);
186 for (j = 0; j < len; j++)
187 {
188 if (value[j] == '\"' || value[j] == '\\')
189 yield = string_cat(yield, &ssize, &offset, US"\\", 1);
190 yield = string_cat(yield, &ssize, &offset, value+j, 1);
191 }
192 yield = string_cat(yield, &ssize, &offset, US"\"", 1);
193 }
194 else yield = string_cat(yield, &ssize, &offset, value, len);
195
196 yield = string_cat(yield, &ssize, &offset, US" ", 1);
197 }
198
199 /* When the specified field is found, grab its data and finish */
200
201 else if (Ustrcmp(field_name, tc->tc_name) == 0)
202 {
203 yield = string_copyn(value, len);
204 goto NISPLUS_EXIT;
205 }
206 }
207
208/* Error if a field name was specified and we didn't find it; if no
209field name, ensure the concatenated data is zero-terminated. */
210
211if (field_name != NULL)
212 *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
213 query);
214else
215 {
216 yield[offset] = 0;
217 store_reset(yield + offset + 1);
218 }
219
220/* Restore the colon in the query, and free result store before
221finishing. */
222
223NISPLUS_EXIT:
224if (field_name != NULL) field_name[-1] = ':';
225if (nrt != NULL) nis_freeresult(nrt);
226if (nre != NULL) nis_freeresult(nre);
227
228if (yield != NULL)
229 {
230 *result = yield;
231 return OK;
232 }
233
234return error_error; /* FAIL or DEFER */
235}
236
237
238
239/*************************************************
240* Quote entry point *
241*************************************************/
242
243/* The only quoting that is necessary for NIS+ is to double any doublequote
244characters. No options are recognized.
245
246Arguments:
247 s the string to be quoted
248 opt additional option text or NULL if none
249
250Returns: the processed string or NULL for a bad option
251*/
252
253uschar *
254nisplus_quote(uschar *s, uschar *opt)
255{
256int count = 0;
257uschar *quoted;
258uschar *t = s;
259
260if (opt != NULL) return NULL; /* No options recognized */
261
262while (*t != 0) if (*t++ == '\"') count++;
263if (count == 0) return s;
264
265t = quoted = store_get(Ustrlen(s) + count + 1);
266
267while (*s != 0)
268 {
269 *t++ = *s;
270 if (*s++ == '\"') *t++ = '\"';
271 }
272
273*t = 0;
274return quoted;
275}
276
277#endif /* LOOKUP_NISPLUS */
278
279/* End of lookups/nisplus.c */