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"
13 #include <rpcsvc/nis.h>
16 /*************************************************
18 *************************************************/
20 /* See local README for interface description. */
23 nisplus_open(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
, uschar
*filename
, uschar
*query
, int length
,
48 uschar
**result
, uschar
**errmsg
, BOOL
*do_cache
)
53 int error_error
= FAIL
;
54 uschar
*field_name
= NULL
;
55 nis_result
*nrt
= NULL
;
56 nis_result
*nre
= NULL
;
57 nis_object
*tno
, *eno
;
60 uschar
*p
= query
+ length
;
63 do_cache
= do_cache
; /* Placate picky compilers */
65 /* Search backwards for a colon to see if a result field name
68 while (p
> query
&& p
[-1] != ':') p
--;
75 else p
= query
+ length
;
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 (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
== NULL
) value
= US
""; else
154 while (len
> 0 && (value
[len
-1] == 0 || isspace(value
[len
-1])))
157 /* Concatenate all fields if no specific one selected */
159 if (field_name
== NULL
)
161 yield
= string_cat(yield
, &ssize
, &offset
,US tc
->tc_name
,
162 Ustrlen(tc
->tc_name
));
163 yield
= string_cat(yield
, &ssize
, &offset
, US
"=", 1);
165 /* Quote the value if it contains spaces or is empty */
167 if (value
[0] == 0 || Ustrchr(value
, ' ') != NULL
)
170 yield
= string_cat(yield
, &ssize
, &offset
, US
"\"", 1);
171 for (j
= 0; j
< len
; j
++)
173 if (value
[j
] == '\"' || value
[j
] == '\\')
174 yield
= string_cat(yield
, &ssize
, &offset
, US
"\\", 1);
175 yield
= string_cat(yield
, &ssize
, &offset
, value
+j
, 1);
177 yield
= string_cat(yield
, &ssize
, &offset
, US
"\"", 1);
179 else yield
= string_cat(yield
, &ssize
, &offset
, value
, len
);
181 yield
= string_cat(yield
, &ssize
, &offset
, 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_copyn(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. */
196 if (field_name
!= NULL
)
197 *errmsg
= string_sprintf("NIS+ field %s not found for %s", field_name
,
202 store_reset(yield
+ offset
+ 1);
205 /* Restore the colon in the query, and free result store before
209 if (field_name
!= NULL
) field_name
[-1] = ':';
210 if (nrt
!= NULL
) nis_freeresult(nrt
);
211 if (nre
!= NULL
) nis_freeresult(nre
);
219 return error_error
; /* FAIL or DEFER */
224 /*************************************************
225 * Quote entry point *
226 *************************************************/
228 /* The only quoting that is necessary for NIS+ is to double any doublequote
229 characters. No options are recognized.
232 s the string to be quoted
233 opt additional option text or NULL if none
235 Returns: the processed string or NULL for a bad option
239 nisplus_quote(uschar
*s
, uschar
*opt
)
245 if (opt
!= NULL
) return NULL
; /* No options recognized */
247 while (*t
!= 0) if (*t
++ == '\"') count
++;
248 if (count
== 0) return s
;
250 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1);
255 if (*s
++ == '\"') *t
++ = '\"';
262 static lookup_info _lookup_info
= {
263 US
"nisplus", /* lookup name */
264 lookup_querystyle
, /* query-style lookup */
265 nisplus_open
, /* open function */
266 NULL
, /* check function */
267 nisplus_find
, /* find function */
268 NULL
, /* no close function */
269 NULL
, /* no tidy function */
270 nisplus_quote
/* quoting function */
274 #define nisplus_lookup_module_info _lookup_module_info
277 static lookup_info
*_lookup_list
[] = { &_lookup_info
};
278 lookup_module_info nisplus_lookup_module_info
= { LOOKUP_MODULE_INFO_MAGIC
, _lookup_list
, 1 };
280 /* End of lookups/nisplus.c */