Version reporting & module ABI change.
[exim.git] / src / src / lookups / nisplus.c
1 /* $Cambridge: exim/src/src/lookups/nisplus.c,v 1.5 2009/11/16 19:50:38 nm4 Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
9
10 #include "../exim.h"
11 #include "lf_functions.h"
12
13 #include <rpcsvc/nis.h>
14
15
16 /*************************************************
17 *              Open entry point                  *
18 *************************************************/
19
20 /* See local README for interface description. */
21
22 static void *
23 nisplus_open(uschar *filename, uschar **errmsg)
24 {
25 return (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
35 NIS+ search is
36
37   [field=value,...],table-name
38 or
39   [field=value,...],table-name:result-field-name
40
41 in other words, a normal NIS+ "indexed name", with an optional result field
42 name tagged on the end after a colon. If there is no result-field name, the
43 yield is the concatenation of all the fields, preceded by their names and an
44 equals sign. */
45
46 static int
47 nisplus_find(void *handle, uschar *filename, uschar *query, int length,
48   uschar **result, uschar **errmsg, BOOL *do_cache)
49 {
50 int i;
51 int ssize = 0;
52 int offset = 0;
53 int error_error = FAIL;
54 uschar *field_name = NULL;
55 nis_result *nrt = NULL;
56 nis_result *nre = NULL;
57 nis_object *tno, *eno;
58 struct entry_obj *eo;
59 struct table_obj *ta;
60 uschar *p = query + length;
61 uschar *yield = NULL;
62
63 do_cache = do_cache;   /* Placate picky compilers */
64
65 /* Search backwards for a colon to see if a result field name
66 has been given. */
67
68 while (p > query && p[-1] != ':') p--;
69
70 if (p > query)
71   {
72   field_name = p;
73   p[-1] = 0;
74   }
75 else p = query + length;
76
77 /* Now search backwards to find the comma that starts the
78 table name. */
79
80 while (p > query && p[-1] != ',') p--;
81 if (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,
89 check that we got back a table, and set up pointers so the field
90 names can be scanned. */
91
92 nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE);
93 if (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   }
101 tno = nrt->objects.objects_val;
102 if (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   }
107 ta = &(tno->zo_data.objdata_u.ta_data);
108
109 /* Now look up the entry in the table, check that we got precisely one
110 object and that it is a table entry. */
111
112 nre = nis_list(CS query, EXPAND_NAME, NULL, NULL);
113 if (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   }
119 if (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   }
125 else if (nre->objects.objects_len < 1)
126   {
127   *errmsg = string_sprintf("NIS+ returned no data for %s", query);
128   goto NISPLUS_EXIT;
129   }
130 eno = nre->objects.objects_val;
131 if (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
138 was given, look for that field; otherwise concatenate all the fields
139 with their names. */
140
141 eo = &(eno->zo_data.objdata_u.en_data);
142 for (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
194 field name, ensure the concatenated data is zero-terminated. */
195
196 if (field_name != NULL)
197   *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
198     query);
199 else
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
206 finishing. */
207
208 NISPLUS_EXIT:
209 if (field_name != NULL) field_name[-1] = ':';
210 if (nrt != NULL) nis_freeresult(nrt);
211 if (nre != NULL) nis_freeresult(nre);
212
213 if (yield != NULL)
214   {
215   *result = yield;
216   return OK;
217   }
218
219 return 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
229 characters. No options are recognized.
230
231 Arguments:
232   s          the string to be quoted
233   opt        additional option text or NULL if none
234
235 Returns:     the processed string or NULL for a bad option
236 */
237
238 static uschar *
239 nisplus_quote(uschar *s, uschar *opt)
240 {
241 int count = 0;
242 uschar *quoted;
243 uschar *t = s;
244
245 if (opt != NULL) return NULL;    /* No options recognized */
246
247 while (*t != 0) if (*t++ == '\"') count++;
248 if (count == 0) return s;
249
250 t = quoted = store_get(Ustrlen(s) + count + 1);
251
252 while (*s != 0)
253   {
254   *t++ = *s;
255   if (*s++ == '\"') *t++ = '\"';
256   }
257
258 *t = 0;
259 return quoted;
260 }
261
262
263 /*************************************************
264 *         Version reporting entry point          *
265 *************************************************/
266
267 /* See local README for interface description. */
268
269 #include "../version.h"
270
271 void
272 nisplus_version_report(FILE *f)
273 {
274 #ifdef DYNLOOKUP
275 fprintf(f, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR);
276 #endif
277 }
278
279
280 static lookup_info _lookup_info = {
281   US"nisplus",                   /* lookup name */
282   lookup_querystyle,             /* query-style lookup */
283   nisplus_open,                  /* open function */
284   NULL,                          /* check function */
285   nisplus_find,                  /* find function */
286   NULL,                          /* no close function */
287   NULL,                          /* no tidy function */
288   nisplus_quote,                 /* quoting function */
289   nisplus_version_report         /* version reporting */
290 };
291
292 #ifdef DYNLOOKUP
293 #define nisplus_lookup_module_info _lookup_module_info
294 #endif
295
296 static lookup_info *_lookup_list[] = { &_lookup_info };
297 lookup_module_info nisplus_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
298
299 /* End of lookups/nisplus.c */