SPDX: license tags (mostly by guesswork)
[exim.git] / src / src / lookups / nisplus.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
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-only */
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(const 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, const uschar * filename, const uschar * query,
48   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
49   const uschar * opts)
50 {
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;
56 struct entry_obj *eo;
57 struct table_obj *ta;
58 const uschar * p = query + length;
59 gstring * yield = NULL;
60
61 do_cache = do_cache;   /* Placate picky compilers */
62
63 /* Search backwards for a colon to see if a result field name
64 has been given. */
65
66 while (p > query && p[-1] != ':') p--;
67
68 if (p > query)          /* get the query without the result-field */
69   {
70   uint len = p-1 - query;
71   field_name = p;
72   query = string_copyn(query, len);
73   p = query + len;
74   }
75 else
76   p = query + length;
77
78 /* Now search backwards to find the comma that starts the
79 table name. */
80
81 while (p > query && p[-1] != ',') p--;
82 if (p <= query)
83   {
84   *errmsg = US"NIS+ query malformed";
85   error_error = DEFER;
86   goto NISPLUS_EXIT;
87   }
88
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. */
92
93 nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE);
94 if (nrt->status != NIS_SUCCESS)
95   {
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)
99     error_error = DEFER;
100   goto NISPLUS_EXIT;
101   }
102 tno = nrt->objects.objects_val;
103 if (tno->zo_data.zo_type != TABLE_OBJ)
104   {
105   *errmsg = string_sprintf("NIS+ error: %s is not a table", p);
106   goto NISPLUS_EXIT;
107   }
108 ta = &tno->zo_data.objdata_u.ta_data;
109
110 /* Now look up the entry in the table, check that we got precisely one
111 object and that it is a table entry. */
112
113 nre = nis_list(CS query, EXPAND_NAME, NULL, NULL);
114 if (nre->status != NIS_SUCCESS)
115   {
116   *errmsg = string_sprintf("NIS+ error accessing entry %s: %s",
117     query, nis_sperrno(nre->status));
118   goto NISPLUS_EXIT;
119   }
120 if (nre->objects.objects_len > 1)
121   {
122   *errmsg = string_sprintf("NIS+ returned more than one object for %s",
123     query);
124   goto NISPLUS_EXIT;
125   }
126 else if (nre->objects.objects_len < 1)
127   {
128   *errmsg = string_sprintf("NIS+ returned no data for %s", query);
129   goto NISPLUS_EXIT;
130   }
131 eno = nre->objects.objects_val;
132 if (eno->zo_data.zo_type != ENTRY_OBJ)
133   {
134   *errmsg = string_sprintf("NIS+ error: %s is not an entry", query);
135   goto NISPLUS_EXIT;
136   }
137
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
140 with their names. */
141
142 eo = &(eno->zo_data.objdata_u.en_data);
143 for (int i = 0; i < eo->en_cols.en_cols_len; i++)
144   {
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;
149
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
152   bytes. */
153
154   if (!value) value = US"";
155   else
156     while (len > 0 && (value[len-1] == 0 || isspace(value[len-1])))
157       len--;
158
159   /* Concatenate all fields if no specific one selected */
160
161   if (!field_name)
162     {
163     yield = string_cat (yield, US tc->tc_name);
164     yield = string_catn(yield, US"=", 1);
165
166     /* Quote the value if it contains spaces or is empty */
167
168     if (value[0] == 0 || Ustrchr(value, ' ') != NULL)
169       {
170       yield = string_catn(yield, US"\"", 1);
171       for (int j = 0; j < len; j++)
172         {
173         if (value[j] == '\"' || value[j] == '\\')
174           yield = string_catn(yield, US"\\", 1);
175         yield = string_catn(yield, value+j, 1);
176         }
177       yield = string_catn(yield, US"\"", 1);
178       }
179     else
180       yield = string_catn(yield, value, len);
181
182     yield = string_catn(yield, US" ", 1);
183     }
184
185   /* When the specified field is found, grab its data and finish */
186
187   else if (Ustrcmp(field_name, tc->tc_name) == 0)
188     {
189     yield = string_catn(yield, value, len);
190     goto NISPLUS_EXIT;
191     }
192   }
193
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. */
196
197 if (field_name)
198   *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name,
199     query);
200 else
201   gstring_release_unused(yield);
202
203 /* Free result store before finishing. */
204
205 NISPLUS_EXIT:
206 if (nrt) nis_freeresult(nrt);
207 if (nre) nis_freeresult(nre);
208
209 if (yield)
210   {
211   *result = string_from_gstring(yield);
212   return OK;
213   }
214
215 return error_error;      /* FAIL or DEFER */
216 }
217
218
219
220 /*************************************************
221 *               Quote entry point                *
222 *************************************************/
223
224 /* The only quoting that is necessary for NIS+ is to double any doublequote
225 characters. No options are recognized.
226
227 Arguments:
228   s          the string to be quoted
229   opt        additional option text or NULL if none
230   idx        lookup type index
231
232 Returns:     the processed string or NULL for a bad option
233 */
234
235 static uschar *
236 nisplus_quote(uschar * s, uschar * opt, unsigned idx)
237 {
238 int count = 0;
239 uschar * quoted, * t = s;
240
241 if (opt) return NULL;    /* No options recognized */
242
243 while (*t) if (*t++ == '\"') count++;
244
245 t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
246
247 while (*s)
248   {
249   *t++ = *s;
250   if (*s++ == '\"') *t++ = '\"';
251   }
252
253 *t = 0;
254 return quoted;
255 }
256
257
258 /*************************************************
259 *         Version reporting entry point          *
260 *************************************************/
261
262 /* See local README for interface description. */
263
264 #include "../version.h"
265
266 gstring *
267 nisplus_version_report(gstring * g)
268 {
269 #ifdef DYNLOOKUP
270 g = string_fmt_append(g, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR);
271 #endif
272 return g;
273 }
274
275
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 */
286 };
287
288 #ifdef DYNLOOKUP
289 #define nisplus_lookup_module_info _lookup_module_info
290 #endif
291
292 static lookup_info *_lookup_list[] = { &_lookup_info };
293 lookup_module_info nisplus_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
294
295 /* End of lookups/nisplus.c */