Commit | Line | Data |
---|---|---|
c988f1f4 | 1 | /* $Cambridge: exim/src/src/lookups/nisplus.c,v 1.2 2005/01/04 10:00:44 ph10 Exp $ */ |
0756eb3c PH |
2 | |
3 | /************************************************* | |
4 | * Exim - an Internet mail transport agent * | |
5 | *************************************************/ | |
6 | ||
c988f1f4 | 7 | /* Copyright (c) University of Cambridge 1995 - 2005 */ |
0756eb3c PH |
8 | /* See the file NOTICE for conditions of use and distribution. */ |
9 | ||
10 | #include "../exim.h" | |
11 | #include "lf_functions.h" | |
12 | #include "nisplus.h" | |
13 | ||
14 | /* We can't just compile this code and allow the library mechanism to omit the | |
15 | functions if they are not wanted, because we need to have the NIS+ header | |
16 | available for compiling. Therefore, compile these functions only if | |
17 | LOOKUP_NISPLUS is defined. However, some compilers don't like compiling empty | |
18 | modules, so keep them happy with a dummy when skipping the rest. Make it | |
19 | reference itself to stop picky compilers complaining that it is unused, and put | |
20 | in a dummy argument to stop even pickier compilers complaining about infinite | |
21 | loops. */ | |
22 | ||
23 | #ifndef LOOKUP_NISPLUS | |
24 | static void dummy(int x) { dummy(x-1); } | |
25 | #else | |
26 | ||
27 | ||
28 | #include <rpcsvc/nis.h> | |
29 | ||
30 | ||
31 | /************************************************* | |
32 | * Open entry point * | |
33 | *************************************************/ | |
34 | ||
35 | /* See local README for interface description. */ | |
36 | ||
37 | void * | |
38 | nisplus_open(uschar *filename, uschar **errmsg) | |
39 | { | |
40 | return (void *)(1); /* Just return something non-null */ | |
41 | } | |
42 | ||
43 | ||
44 | ||
45 | /************************************************* | |
46 | * Find entry point * | |
47 | *************************************************/ | |
48 | ||
49 | /* See local README for interface description. The format of queries for a | |
50 | NIS+ search is | |
51 | ||
52 | [field=value,...],table-name | |
53 | or | |
54 | [field=value,...],table-name:result-field-name | |
55 | ||
56 | in other words, a normal NIS+ "indexed name", with an optional result field | |
57 | name tagged on the end after a colon. If there is no result-field name, the | |
58 | yield is the concatenation of all the fields, preceded by their names and an | |
59 | equals sign. */ | |
60 | ||
61 | int | |
62 | nisplus_find(void *handle, uschar *filename, uschar *query, int length, | |
63 | uschar **result, uschar **errmsg, BOOL *do_cache) | |
64 | { | |
65 | int i; | |
66 | int ssize = 0; | |
67 | int offset = 0; | |
68 | int error_error = FAIL; | |
69 | uschar *field_name = NULL; | |
70 | nis_result *nrt = NULL; | |
71 | nis_result *nre = NULL; | |
72 | nis_object *tno, *eno; | |
73 | struct entry_obj *eo; | |
74 | struct table_obj *ta; | |
75 | uschar *p = query + length; | |
76 | uschar *yield = NULL; | |
77 | ||
78 | do_cache = do_cache; /* Placate picky compilers */ | |
79 | ||
80 | /* Search backwards for a colon to see if a result field name | |
81 | has been given. */ | |
82 | ||
83 | while (p > query && p[-1] != ':') p--; | |
84 | ||
85 | if (p > query) | |
86 | { | |
87 | field_name = p; | |
88 | p[-1] = 0; | |
89 | } | |
90 | else p = query + length; | |
91 | ||
92 | /* Now search backwards to find the comma that starts the | |
93 | table name. */ | |
94 | ||
95 | while (p > query && p[-1] != ',') p--; | |
96 | if (p <= query) | |
97 | { | |
98 | *errmsg = US"NIS+ query malformed"; | |
99 | error_error = DEFER; | |
100 | goto NISPLUS_EXIT; | |
101 | } | |
102 | ||
103 | /* Look up the data for the table, in order to get the field names, | |
104 | check that we got back a table, and set up pointers so the field | |
105 | names can be scanned. */ | |
106 | ||
107 | nrt = nis_lookup(CS p, EXPAND_NAME | NO_CACHE); | |
108 | if (nrt->status != NIS_SUCCESS) | |
109 | { | |
110 | *errmsg = string_sprintf("NIS+ error accessing %s table: %s", p, | |
111 | nis_sperrno(nrt->status)); | |
112 | if (nrt->status != NIS_NOTFOUND && nrt->status != NIS_NOSUCHTABLE) | |
113 | error_error = DEFER; | |
114 | goto NISPLUS_EXIT; | |
115 | } | |
116 | tno = nrt->objects.objects_val; | |
117 | if (tno->zo_data.zo_type != TABLE_OBJ) | |
118 | { | |
119 | *errmsg = string_sprintf("NIS+ error: %s is not a table", p); | |
120 | goto NISPLUS_EXIT; | |
121 | } | |
122 | ta = &(tno->zo_data.objdata_u.ta_data); | |
123 | ||
124 | /* Now look up the entry in the table, check that we got precisely one | |
125 | object and that it is a table entry. */ | |
126 | ||
127 | nre = nis_list(CS query, EXPAND_NAME, NULL, NULL); | |
128 | if (nre->status != NIS_SUCCESS) | |
129 | { | |
130 | *errmsg = string_sprintf("NIS+ error accessing entry %s: %s", | |
131 | query, nis_sperrno(nre->status)); | |
132 | goto NISPLUS_EXIT; | |
133 | } | |
134 | if (nre->objects.objects_len > 1) | |
135 | { | |
136 | *errmsg = string_sprintf("NIS+ returned more than one object for %s", | |
137 | query); | |
138 | goto NISPLUS_EXIT; | |
139 | } | |
140 | else if (nre->objects.objects_len < 1) | |
141 | { | |
142 | *errmsg = string_sprintf("NIS+ returned no data for %s", query); | |
143 | goto NISPLUS_EXIT; | |
144 | } | |
145 | eno = nre->objects.objects_val; | |
146 | if (eno->zo_data.zo_type != ENTRY_OBJ) | |
147 | { | |
148 | *errmsg = string_sprintf("NIS+ error: %s is not an entry", query); | |
149 | goto NISPLUS_EXIT; | |
150 | } | |
151 | ||
152 | /* Scan the columns in the entry and in the table. If a result field | |
153 | was given, look for that field; otherwise concatenate all the fields | |
154 | with their names. */ | |
155 | ||
156 | eo = &(eno->zo_data.objdata_u.en_data); | |
157 | for (i = 0; i < eo->en_cols.en_cols_len; i++) | |
158 | { | |
159 | table_col *tc = ta->ta_cols.ta_cols_val + i; | |
160 | entry_col *ec = eo->en_cols.en_cols_val + i; | |
161 | int len = ec->ec_value.ec_value_len; | |
162 | uschar *value = US ec->ec_value.ec_value_val; | |
163 | ||
164 | /* The value may be NULL for a zero-length field. Turn this into an | |
165 | empty string for consistency. Remove trailing whitespace and zero | |
166 | bytes. */ | |
167 | ||
168 | if (value == NULL) value = US""; else | |
169 | while (len > 0 && (value[len-1] == 0 || isspace(value[len-1]))) | |
170 | len--; | |
171 | ||
172 | /* Concatenate all fields if no specific one selected */ | |
173 | ||
174 | if (field_name == NULL) | |
175 | { | |
176 | yield = string_cat(yield, &ssize, &offset,US tc->tc_name, | |
177 | Ustrlen(tc->tc_name)); | |
178 | yield = string_cat(yield, &ssize, &offset, US"=", 1); | |
179 | ||
180 | /* Quote the value if it contains spaces or is empty */ | |
181 | ||
182 | if (value[0] == 0 || Ustrchr(value, ' ') != NULL) | |
183 | { | |
184 | int j; | |
185 | yield = string_cat(yield, &ssize, &offset, US"\"", 1); | |
186 | for (j = 0; j < len; j++) | |
187 | { | |
188 | if (value[j] == '\"' || value[j] == '\\') | |
189 | yield = string_cat(yield, &ssize, &offset, US"\\", 1); | |
190 | yield = string_cat(yield, &ssize, &offset, value+j, 1); | |
191 | } | |
192 | yield = string_cat(yield, &ssize, &offset, US"\"", 1); | |
193 | } | |
194 | else yield = string_cat(yield, &ssize, &offset, value, len); | |
195 | ||
196 | yield = string_cat(yield, &ssize, &offset, US" ", 1); | |
197 | } | |
198 | ||
199 | /* When the specified field is found, grab its data and finish */ | |
200 | ||
201 | else if (Ustrcmp(field_name, tc->tc_name) == 0) | |
202 | { | |
203 | yield = string_copyn(value, len); | |
204 | goto NISPLUS_EXIT; | |
205 | } | |
206 | } | |
207 | ||
208 | /* Error if a field name was specified and we didn't find it; if no | |
209 | field name, ensure the concatenated data is zero-terminated. */ | |
210 | ||
211 | if (field_name != NULL) | |
212 | *errmsg = string_sprintf("NIS+ field %s not found for %s", field_name, | |
213 | query); | |
214 | else | |
215 | { | |
216 | yield[offset] = 0; | |
217 | store_reset(yield + offset + 1); | |
218 | } | |
219 | ||
220 | /* Restore the colon in the query, and free result store before | |
221 | finishing. */ | |
222 | ||
223 | NISPLUS_EXIT: | |
224 | if (field_name != NULL) field_name[-1] = ':'; | |
225 | if (nrt != NULL) nis_freeresult(nrt); | |
226 | if (nre != NULL) nis_freeresult(nre); | |
227 | ||
228 | if (yield != NULL) | |
229 | { | |
230 | *result = yield; | |
231 | return OK; | |
232 | } | |
233 | ||
234 | return error_error; /* FAIL or DEFER */ | |
235 | } | |
236 | ||
237 | ||
238 | ||
239 | /************************************************* | |
240 | * Quote entry point * | |
241 | *************************************************/ | |
242 | ||
243 | /* The only quoting that is necessary for NIS+ is to double any doublequote | |
244 | characters. No options are recognized. | |
245 | ||
246 | Arguments: | |
247 | s the string to be quoted | |
248 | opt additional option text or NULL if none | |
249 | ||
250 | Returns: the processed string or NULL for a bad option | |
251 | */ | |
252 | ||
253 | uschar * | |
254 | nisplus_quote(uschar *s, uschar *opt) | |
255 | { | |
256 | int count = 0; | |
257 | uschar *quoted; | |
258 | uschar *t = s; | |
259 | ||
260 | if (opt != NULL) return NULL; /* No options recognized */ | |
261 | ||
262 | while (*t != 0) if (*t++ == '\"') count++; | |
263 | if (count == 0) return s; | |
264 | ||
265 | t = quoted = store_get(Ustrlen(s) + count + 1); | |
266 | ||
267 | while (*s != 0) | |
268 | { | |
269 | *t++ = *s; | |
270 | if (*s++ == '\"') *t++ = '\"'; | |
271 | } | |
272 | ||
273 | *t = 0; | |
274 | return quoted; | |
275 | } | |
276 | ||
277 | #endif /* LOOKUP_NISPLUS */ | |
278 | ||
279 | /* End of lookups/nisplus.c */ |