1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
10 #include "lf_functions.h"
12 #include <rpcsvc/nis.h>
15 /*************************************************
17 *************************************************/
19 /* See local README for interface description. */
22 nisplus_open(const uschar
* filename
, uschar
** errmsg
)
24 return (void *)(1); /* Just return something non-null */
29 /*************************************************
31 *************************************************/
33 /* See local README for interface description. The format of queries for a
36 [field=value,...],table-name
38 [field=value,...],table-name:result-field-name
40 in other words, a normal NIS+ "indexed name", with an optional result field
41 name tagged on the end after a colon. If there is no result-field name, the
42 yield is the concatenation of all the fields, preceded by their names and an
46 nisplus_find(void * handle
, const uschar
* filename
, const uschar
* query
,
47 int length
, uschar
** result
, uschar
** errmsg
, uint
* do_cache
,
50 int error_error
= FAIL
;
51 const uschar
* field_name
= NULL
;
52 nis_result
*nrt
= NULL
;
53 nis_result
*nre
= NULL
;
54 nis_object
*tno
, *eno
;
57 const uschar
* p
= query
+ length
;
58 gstring
* yield
= NULL
;
60 do_cache
= do_cache
; /* Placate picky compilers */
62 /* Search backwards for a colon to see if a result field name
65 while (p
> query
&& p
[-1] != ':') p
--;
67 if (p
> query
) /* get the query without the result-field */
69 uint len
= p
-1 - query
;
71 query
= string_copyn(query
, len
);
77 /* Now search backwards to find the comma that starts the
80 while (p
> query
&& p
[-1] != ',') p
--;
83 *errmsg
= US
"NIS+ query malformed";
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. */
92 nrt
= nis_lookup(CS p
, EXPAND_NAME
| NO_CACHE
);
93 if (nrt
->status
!= NIS_SUCCESS
)
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
)
101 tno
= nrt
->objects
.objects_val
;
102 if (tno
->zo_data
.zo_type
!= TABLE_OBJ
)
104 *errmsg
= string_sprintf("NIS+ error: %s is not a table", p
);
107 ta
= &tno
->zo_data
.objdata_u
.ta_data
;
109 /* Now look up the entry in the table, check that we got precisely one
110 object and that it is a table entry. */
112 nre
= nis_list(CS query
, EXPAND_NAME
, NULL
, NULL
);
113 if (nre
->status
!= NIS_SUCCESS
)
115 *errmsg
= string_sprintf("NIS+ error accessing entry %s: %s",
116 query
, nis_sperrno(nre
->status
));
119 if (nre
->objects
.objects_len
> 1)
121 *errmsg
= string_sprintf("NIS+ returned more than one object for %s",
125 else if (nre
->objects
.objects_len
< 1)
127 *errmsg
= string_sprintf("NIS+ returned no data for %s", query
);
130 eno
= nre
->objects
.objects_val
;
131 if (eno
->zo_data
.zo_type
!= ENTRY_OBJ
)
133 *errmsg
= string_sprintf("NIS+ error: %s is not an entry", query
);
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
141 eo
= &(eno
->zo_data
.objdata_u
.en_data
);
142 for (int i
= 0; i
< eo
->en_cols
.en_cols_len
; i
++)
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
;
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
153 if (!value
) value
= US
"";
155 while (len
> 0 && (value
[len
-1] == 0 || isspace(value
[len
-1])))
158 /* Concatenate all fields if no specific one selected */
162 yield
= string_cat (yield
, US tc
->tc_name
);
163 yield
= string_catn(yield
, US
"=", 1);
165 /* Quote the value if it contains spaces or is empty */
167 if (value
[0] == 0 || Ustrchr(value
, ' ') != NULL
)
169 yield
= string_catn(yield
, US
"\"", 1);
170 for (int j
= 0; j
< len
; j
++)
172 if (value
[j
] == '\"' || value
[j
] == '\\')
173 yield
= string_catn(yield
, US
"\\", 1);
174 yield
= string_catn(yield
, value
+j
, 1);
176 yield
= string_catn(yield
, US
"\"", 1);
179 yield
= string_catn(yield
, value
, len
);
181 yield
= string_catn(yield
, US
" ", 1);
184 /* When the specified field is found, grab its data and finish */
186 else if (Ustrcmp(field_name
, tc
->tc_name
) == 0)
188 yield
= string_catn(yield
, value
, len
);
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. */
197 *errmsg
= string_sprintf("NIS+ field %s not found for %s", field_name
,
200 gstring_release_unused(yield
);
202 /* Free result store before finishing. */
205 if (nrt
) nis_freeresult(nrt
);
206 if (nre
) nis_freeresult(nre
);
210 *result
= string_from_gstring(yield
);
214 return error_error
; /* FAIL or DEFER */
219 /*************************************************
220 * Quote entry point *
221 *************************************************/
223 /* The only quoting that is necessary for NIS+ is to double any doublequote
224 characters. No options are recognized.
227 s the string to be quoted
228 opt additional option text or NULL if none
230 Returns: the processed string or NULL for a bad option
234 nisplus_quote(uschar
*s
, uschar
*opt
)
240 if (opt
!= NULL
) return NULL
; /* No options recognized */
242 while (*t
!= 0) if (*t
++ == '\"') count
++;
243 if (count
== 0) return s
;
245 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1, is_tainted(s
));
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(FILE *f
)
270 fprintf(f
, "Library version: NIS+: Exim version %s\n", EXIM_VERSION_STR
);
275 static lookup_info _lookup_info
= {
276 .name
= US
"nisplus", /* lookup name */
277 .type
= lookup_querystyle
, /* query-style lookup */
278 .open
= nisplus_open
, /* open function */
279 .check
= NULL
, /* check function */
280 .find
= nisplus_find
, /* find function */
281 .close
= NULL
, /* no close function */
282 .tidy
= NULL
, /* no tidy function */
283 .quote
= nisplus_quote
, /* quoting function */
284 .version_report
= nisplus_version_report
/* version reporting */
288 #define nisplus_lookup_module_info _lookup_module_info
291 static lookup_info
*_lookup_list
[] = { &_lookup_info
};
292 lookup_module_info nisplus_lookup_module_info
= { LOOKUP_MODULE_INFO_MAGIC
, _lookup_list
, 1 };
294 /* End of lookups/nisplus.c */