1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 #include "lf_functions.h"
13 #include <rpcsvc/nis.h>
16 /*************************************************
18 *************************************************/
20 /* See local README for interface description. */
23 nisplus_open(const uschar * filename, uschar ** errmsg)
25 return (void *)(1); /* Just return something non-null */
30 /*************************************************
32 *************************************************/
34 /* See local README for interface description. The format of queries for a
37 [field=value,...],table-name
39 [field=value,...],table-name:result-field-name
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
47 nisplus_find(void * handle, const uschar * filename, const uschar * query,
48 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
51 int error_error = FAIL;
52 const uschar * field_name = NULL;
53 nis_result *nrt = NULL;
54 nis_result *nre = NULL;
55 nis_object *tno, *eno;
58 const uschar * p = query + length;
59 gstring * yield = NULL;
61 do_cache = do_cache; /* Placate picky compilers */
63 /* Search backwards for a colon to see if a result field name
66 while (p > query && p[-1] != ':') p--;
68 if (p > query) /* get the query without the result-field */
70 uint len = p-1 - query;
72 query = string_copyn(query, len);
78 /* Now search backwards to find the comma that starts the
81 while (p > query && p[-1] != ',') p--;
84 *errmsg = US"NIS+ query malformed";
89 /* Look up the data for the table, in order to get the field names,
90 check that we got back a table, and set up pointers so the field
91 names can be scanned. */
93 nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE);
94 if (nrt->status != NIS_SUCCESS)
96 *errmsg = string_sprintf("NIS+ error accessing %s table: %s", p,
97 nis_sperrno(nrt->status));
98 if (nrt->status != NIS_NOTFOUND && nrt->status != NIS_NOSUCHTABLE)
102 tno = nrt->objects.objects_val;
103 if (tno->zo_data.zo_type != TABLE_OBJ)
105 *errmsg = string_sprintf("NIS+ error: %s is not a table", p);
108 ta = &tno->zo_data.objdata_u.ta_data;
110 /* Now look up the entry in the table, check that we got precisely one
111 object and that it is a table entry. */
113 nre = nis_list(CS query, EXPAND_NAME, NULL, NULL);
114 if (nre->status != NIS_SUCCESS)
116 *errmsg = string_sprintf("NIS+ error accessing entry %s: %s",
117 query, nis_sperrno(nre->status));
120 if (nre->objects.objects_len > 1)
122 *errmsg = string_sprintf("NIS+ returned more than one object for %s",
126 else if (nre->objects.objects_len < 1)
128 *errmsg = string_sprintf("NIS+ returned no data for %s", query);
131 eno = nre->objects.objects_val;
132 if (eno->zo_data.zo_type != ENTRY_OBJ)
134 *errmsg = string_sprintf("NIS+ error: %s is not an entry", query);
138 /* Scan the columns in the entry and in the table. If a result field
139 was given, look for that field; otherwise concatenate all the fields
142 eo = &(eno->zo_data.objdata_u.en_data);
143 for (int i = 0; i < eo->en_cols.en_cols_len; i++)
145 table_col *tc = ta->ta_cols.ta_cols_val + i;
146 entry_col *ec = eo->en_cols.en_cols_val + i;
147 int len = ec->ec_value.ec_value_len;
148 uschar *value = US ec->ec_value.ec_value_val;
150 /* The value may be NULL for a zero-length field. Turn this into an
151 empty string for consistency. Remove trailing whitespace and zero
154 if (!value) value = US"";
156 while (len > 0 && (value[len-1] == 0 || isspace(value[len-1])))
159 /* Concatenate all fields if no specific one selected */
163 yield = string_cat (yield, US tc->tc_name);
164 yield = string_catn(yield, US"=", 1);
166 /* Quote the value if it contains spaces or is empty */
168 if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
170 yield = string_catn(yield, US"\"", 1);
171 for (int j = 0; j < len; j++)
173 if (value[j] == '\"' || value[j] == '\\')
174 yield = string_catn(yield, US"\\", 1);
175 yield = string_catn(yield, value+j, 1);
177 yield = string_catn(yield, US"\"", 1);
180 yield = string_catn(yield, value, len);
182 yield = string_catn(yield, US" ", 1);
185 /* When the specified field is found, grab its data and finish */
187 else if (Ustrcmp(field_name, tc->tc_name) == 0)
189 yield = string_catn(yield, value, len);
194 /* Error if a field name was specified and we didn't find it; if no
195 field name, ensure the concatenated data is zero-terminated. */
198 *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
201 gstring_release_unused(yield);
203 /* Free result store before finishing. */
206 if (nrt) nis_freeresult(nrt);
207 if (nre) nis_freeresult(nre);
211 *result = string_from_gstring(yield);
215 return error_error; /* FAIL or DEFER */
220 /*************************************************
221 * Quote entry point *
222 *************************************************/
224 /* The only quoting that is necessary for NIS+ is to double any doublequote
225 characters. No options are recognized.
228 s the string to be quoted
229 opt additional option text or NULL if none
230 idx lookup type index
232 Returns: the processed string or NULL for a bad option
236 nisplus_quote(uschar * s, uschar * opt, unsigned idx)
239 uschar * quoted, * t = s;
241 if (opt) return NULL; /* No options recognized */
243 while (*t) if (*t++ == '\"') count++;
245 t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
250 if (*s++ == '\"') *t++ = '\"';
258 /*************************************************
259 * Version reporting entry point *
260 *************************************************/
262 /* See local README for interface description. */
264 #include "../version.h"
267 nisplus_version_report(gstring * g)
270 g = string_fmt_append(g, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR);
276 static lookup_info _lookup_info = {
277 .name = US"nisplus", /* lookup name */
278 .type = lookup_querystyle, /* query-style lookup */
279 .open = nisplus_open, /* open function */
280 .check = NULL, /* check function */
281 .find = nisplus_find, /* find function */
282 .close = NULL, /* no close function */
283 .tidy = NULL, /* no tidy function */
284 .quote = nisplus_quote, /* quoting function */
285 .version_report = nisplus_version_report /* version reporting */
289 #define nisplus_lookup_module_info _lookup_module_info
292 static lookup_info *_lookup_list[] = { &_lookup_info };
293 lookup_module_info nisplus_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
295 /* End of lookups/nisplus.c */