1 /* $Cambridge: exim/src/src/lookups/nisplus.c,v 1.5 2009/11/16 19:50:38 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "lf_functions.h"
14 /* We can't just compile this code and allow the library mechanism to omit the
15 functions if they are not wanted, because we need to have the NIS+ header
16 available for compiling. Therefore, compile these functions only if
17 LOOKUP_NISPLUS is defined. However, some compilers don't like compiling empty
18 modules, so keep them happy with a dummy when skipping the rest. Make it
19 reference itself to stop picky compilers complaining that it is unused, and put
20 in a dummy argument to stop even pickier compilers complaining about infinite
23 #ifndef LOOKUP_NISPLUS
24 static void dummy(int x) { dummy(x-1); }
28 #include <rpcsvc/nis.h>
31 /*************************************************
33 *************************************************/
35 /* See local README for interface description. */
38 nisplus_open(uschar *filename, uschar **errmsg)
40 return (void *)(1); /* Just return something non-null */
45 /*************************************************
47 *************************************************/
49 /* See local README for interface description. The format of queries for a
52 [field=value,...],table-name
54 [field=value,...],table-name:result-field-name
56 in other words, a normal NIS+ "indexed name", with an optional result field
57 name tagged on the end after a colon. If there is no result-field name, the
58 yield is the concatenation of all the fields, preceded by their names and an
62 nisplus_find(void *handle, uschar *filename, uschar *query, int length,
63 uschar **result, uschar **errmsg, BOOL *do_cache)
68 int error_error = FAIL;
69 uschar *field_name = NULL;
70 nis_result *nrt = NULL;
71 nis_result *nre = NULL;
72 nis_object *tno, *eno;
75 uschar *p = query + length;
78 do_cache = do_cache; /* Placate picky compilers */
80 /* Search backwards for a colon to see if a result field name
83 while (p > query && p[-1] != ':') p--;
90 else p = query + length;
92 /* Now search backwards to find the comma that starts the
95 while (p > query && p[-1] != ',') p--;
98 *errmsg = US"NIS+ query malformed";
103 /* Look up the data for the table, in order to get the field names,
104 check that we got back a table, and set up pointers so the field
105 names can be scanned. */
107 nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE);
108 if (nrt->status != NIS_SUCCESS)
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)
116 tno = nrt->objects.objects_val;
117 if (tno->zo_data.zo_type != TABLE_OBJ)
119 *errmsg = string_sprintf("NIS+ error: %s is not a table", p);
122 ta = &(tno->zo_data.objdata_u.ta_data);
124 /* Now look up the entry in the table, check that we got precisely one
125 object and that it is a table entry. */
127 nre = nis_list(CS query, EXPAND_NAME, NULL, NULL);
128 if (nre->status != NIS_SUCCESS)
130 *errmsg = string_sprintf("NIS+ error accessing entry %s: %s",
131 query, nis_sperrno(nre->status));
134 if (nre->objects.objects_len > 1)
136 *errmsg = string_sprintf("NIS+ returned more than one object for %s",
140 else if (nre->objects.objects_len < 1)
142 *errmsg = string_sprintf("NIS+ returned no data for %s", query);
145 eno = nre->objects.objects_val;
146 if (eno->zo_data.zo_type != ENTRY_OBJ)
148 *errmsg = string_sprintf("NIS+ error: %s is not an entry", query);
152 /* Scan the columns in the entry and in the table. If a result field
153 was given, look for that field; otherwise concatenate all the fields
156 eo = &(eno->zo_data.objdata_u.en_data);
157 for (i = 0; i < eo->en_cols.en_cols_len; i++)
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;
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
168 if (value == NULL) value = US""; else
169 while (len > 0 && (value[len-1] == 0 || isspace(value[len-1])))
172 /* Concatenate all fields if no specific one selected */
174 if (field_name == NULL)
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);
180 /* Quote the value if it contains spaces or is empty */
182 if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
185 yield = string_cat(yield, &ssize, &offset, US"\"", 1);
186 for (j = 0; j < len; j++)
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);
192 yield = string_cat(yield, &ssize, &offset, US"\"", 1);
194 else yield = string_cat(yield, &ssize, &offset, value, len);
196 yield = string_cat(yield, &ssize, &offset, US" ", 1);
199 /* When the specified field is found, grab its data and finish */
201 else if (Ustrcmp(field_name, tc->tc_name) == 0)
203 yield = string_copyn(value, len);
208 /* Error if a field name was specified and we didn't find it; if no
209 field name, ensure the concatenated data is zero-terminated. */
211 if (field_name != NULL)
212 *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
217 store_reset(yield + offset + 1);
220 /* Restore the colon in the query, and free result store before
224 if (field_name != NULL) field_name[-1] = ':';
225 if (nrt != NULL) nis_freeresult(nrt);
226 if (nre != NULL) nis_freeresult(nre);
234 return error_error; /* FAIL or DEFER */
239 /*************************************************
240 * Quote entry point *
241 *************************************************/
243 /* The only quoting that is necessary for NIS+ is to double any doublequote
244 characters. No options are recognized.
247 s the string to be quoted
248 opt additional option text or NULL if none
250 Returns: the processed string or NULL for a bad option
254 nisplus_quote(uschar *s, uschar *opt)
260 if (opt != NULL) return NULL; /* No options recognized */
262 while (*t != 0) if (*t++ == '\"') count++;
263 if (count == 0) return s;
265 t = quoted = store_get(Ustrlen(s) + count + 1);
270 if (*s++ == '\"') *t++ = '\"';
277 #endif /* LOOKUP_NISPLUS */
279 /* End of lookups/nisplus.c */