1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 #include "lf_functions.h"
11 #include <rpcsvc/nis.h>
14 /*************************************************
16 *************************************************/
18 /* See local README for interface description. */
21 nisplus_open(const uschar
* filename
, uschar
** errmsg
)
23 return (void *)(1); /* Just return something non-null */
28 /*************************************************
30 *************************************************/
32 /* See local README for interface description. The format of queries for a
35 [field=value,...],table-name
37 [field=value,...],table-name:result-field-name
39 in other words, a normal NIS+ "indexed name", with an optional result field
40 name tagged on the end after a colon. If there is no result-field name, the
41 yield is the concatenation of all the fields, preceded by their names and an
45 nisplus_find(void * handle
, const uschar
* filename
, const uschar
* query
,
46 int length
, uschar
** result
, uschar
** errmsg
, uint
* do_cache
,
49 int error_error
= FAIL
;
50 const uschar
* field_name
= NULL
;
51 nis_result
*nrt
= NULL
;
52 nis_result
*nre
= NULL
;
53 nis_object
*tno
, *eno
;
56 const uschar
* p
= query
+ length
;
57 gstring
* yield
= NULL
;
59 do_cache
= do_cache
; /* Placate picky compilers */
61 /* Search backwards for a colon to see if a result field name
64 while (p
> query
&& p
[-1] != ':') p
--;
66 if (p
> query
) /* get the query without the result-field */
68 uint len
= p
-1 - query
;
70 query
= string_copyn(query
, len
);
76 /* Now search backwards to find the comma that starts the
79 while (p
> query
&& p
[-1] != ',') p
--;
82 *errmsg
= US
"NIS+ query malformed";
87 /* Look up the data for the table, in order to get the field names,
88 check that we got back a table, and set up pointers so the field
89 names can be scanned. */
91 nrt
= nis_lookup(CS p
, EXPAND_NAME
| NO_CACHE
);
92 if (nrt
->status
!= NIS_SUCCESS
)
94 *errmsg
= string_sprintf("NIS+ error accessing %s table: %s", p
,
95 nis_sperrno(nrt
->status
));
96 if (nrt
->status
!= NIS_NOTFOUND
&& nrt
->status
!= NIS_NOSUCHTABLE
)
100 tno
= nrt
->objects
.objects_val
;
101 if (tno
->zo_data
.zo_type
!= TABLE_OBJ
)
103 *errmsg
= string_sprintf("NIS+ error: %s is not a table", p
);
106 ta
= &tno
->zo_data
.objdata_u
.ta_data
;
108 /* Now look up the entry in the table, check that we got precisely one
109 object and that it is a table entry. */
111 nre
= nis_list(CS query
, EXPAND_NAME
, NULL
, NULL
);
112 if (nre
->status
!= NIS_SUCCESS
)
114 *errmsg
= string_sprintf("NIS+ error accessing entry %s: %s",
115 query
, nis_sperrno(nre
->status
));
118 if (nre
->objects
.objects_len
> 1)
120 *errmsg
= string_sprintf("NIS+ returned more than one object for %s",
124 else if (nre
->objects
.objects_len
< 1)
126 *errmsg
= string_sprintf("NIS+ returned no data for %s", query
);
129 eno
= nre
->objects
.objects_val
;
130 if (eno
->zo_data
.zo_type
!= ENTRY_OBJ
)
132 *errmsg
= string_sprintf("NIS+ error: %s is not an entry", query
);
136 /* Scan the columns in the entry and in the table. If a result field
137 was given, look for that field; otherwise concatenate all the fields
140 eo
= &(eno
->zo_data
.objdata_u
.en_data
);
141 for (int i
= 0; i
< eo
->en_cols
.en_cols_len
; i
++)
143 table_col
*tc
= ta
->ta_cols
.ta_cols_val
+ i
;
144 entry_col
*ec
= eo
->en_cols
.en_cols_val
+ i
;
145 int len
= ec
->ec_value
.ec_value_len
;
146 uschar
*value
= US ec
->ec_value
.ec_value_val
;
148 /* The value may be NULL for a zero-length field. Turn this into an
149 empty string for consistency. Remove trailing whitespace and zero
152 if (!value
) value
= US
"";
154 while (len
> 0 && (value
[len
-1] == 0 || isspace(value
[len
-1])))
157 /* Concatenate all fields if no specific one selected */
161 yield
= string_cat (yield
, US tc
->tc_name
);
162 yield
= string_catn(yield
, US
"=", 1);
164 /* Quote the value if it contains spaces or is empty */
166 if (value
[0] == 0 || Ustrchr(value
, ' ') != NULL
)
168 yield
= string_catn(yield
, US
"\"", 1);
169 for (int j
= 0; j
< len
; j
++)
171 if (value
[j
] == '\"' || value
[j
] == '\\')
172 yield
= string_catn(yield
, US
"\\", 1);
173 yield
= string_catn(yield
, value
+j
, 1);
175 yield
= string_catn(yield
, US
"\"", 1);
178 yield
= string_catn(yield
, value
, len
);
180 yield
= string_catn(yield
, US
" ", 1);
183 /* When the specified field is found, grab its data and finish */
185 else if (Ustrcmp(field_name
, tc
->tc_name
) == 0)
187 yield
= string_catn(yield
, value
, len
);
192 /* Error if a field name was specified and we didn't find it; if no
193 field name, ensure the concatenated data is zero-terminated. */
196 *errmsg
= string_sprintf("NIS+ field %s not found for %s", field_name
,
199 gstring_release_unused(yield
);
201 /* Free result store before finishing. */
204 if (nrt
) nis_freeresult(nrt
);
205 if (nre
) nis_freeresult(nre
);
209 *result
= string_from_gstring(yield
);
213 return error_error
; /* FAIL or DEFER */
218 /*************************************************
219 * Quote entry point *
220 *************************************************/
222 /* The only quoting that is necessary for NIS+ is to double any doublequote
223 characters. No options are recognized.
226 s the string to be quoted
227 opt additional option text or NULL if none
229 Returns: the processed string or NULL for a bad option
233 nisplus_quote(uschar
*s
, uschar
*opt
)
239 if (opt
!= NULL
) return NULL
; /* No options recognized */
241 while (*t
!= 0) if (*t
++ == '\"') count
++;
242 if (count
== 0) return s
;
244 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1, is_tainted(s
));
249 if (*s
++ == '\"') *t
++ = '\"';
257 /*************************************************
258 * Version reporting entry point *
259 *************************************************/
261 /* See local README for interface description. */
263 #include "../version.h"
266 nisplus_version_report(FILE *f
)
269 fprintf(f
, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR
);
274 static lookup_info _lookup_info
= {
275 .name
= US
"nisplus", /* lookup name */
276 .type
= lookup_querystyle
, /* query-style lookup */
277 .open
= nisplus_open
, /* open function */
278 .check
= NULL
, /* check function */
279 .find
= nisplus_find
, /* find function */
280 .close
= NULL
, /* no close function */
281 .tidy
= NULL
, /* no tidy function */
282 .quote
= nisplus_quote
, /* quoting function */
283 .version_report
= nisplus_version_report
/* version reporting */
287 #define nisplus_lookup_module_info _lookup_module_info
290 static lookup_info
*_lookup_list
[] = { &_lookup_info
};
291 lookup_module_info nisplus_lookup_module_info
= { LOOKUP_MODULE_INFO_MAGIC
, _lookup_list
, 1 };
293 /* End of lookups/nisplus.c */