]>
Commit | Line | Data |
---|---|---|
34816665 | 1 | /* Copyright (C) 1997, 1998, 2000, 2001, 2002 Free Software Foundation, Inc. |
e61abf83 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
e61abf83 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
e61abf83 | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
e61abf83 UD |
19 | |
20 | #include <nss.h> | |
21 | #include <netdb.h> | |
22 | #include <errno.h> | |
23 | #include <ctype.h> | |
9b48fa9b | 24 | #include <stdint.h> |
e61abf83 UD |
25 | #include <string.h> |
26 | #include <arpa/inet.h> | |
5107cf1d | 27 | #include <bits/libc-lock.h> |
e61abf83 | 28 | #include <rpcsvc/nis.h> |
e61abf83 UD |
29 | |
30 | #include "nss-nisplus.h" | |
31 | ||
e61abf83 UD |
32 | __libc_lock_define_initialized (static, lock) |
33 | ||
fc9f33e3 UD |
34 | static nis_result *result; |
35 | static nis_name tablename_val; | |
36 | static u_long tablename_len; | |
e61abf83 UD |
37 | |
38 | #define NISENTRYVAL(idx,col,res) \ | |
2d7da676 | 39 | ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) |
e61abf83 UD |
40 | |
41 | #define NISENTRYLEN(idx,col,res) \ | |
2d7da676 | 42 | ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) |
e61abf83 UD |
43 | |
44 | ||
45 | static int | |
46 | _nss_nisplus_parse_netent (nis_result *result, struct netent *network, | |
d71b808a | 47 | char *buffer, size_t buflen, int *errnop) |
e61abf83 | 48 | { |
2d7da676 | 49 | char *first_unused = buffer; |
e61abf83 | 50 | size_t room_left = buflen; |
7799b7b3 | 51 | unsigned int i; |
2d7da676 | 52 | char *p, *line; |
e61abf83 UD |
53 | |
54 | if (result == NULL) | |
26dee9c4 | 55 | return 0; |
e61abf83 | 56 | |
d71b808a UD |
57 | if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) |
58 | || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ | |
59 | || strcmp (result->objects.objects_val[0].EN_data.en_type, | |
60 | "networks_tbl") != 0 | |
61 | || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3) | |
26dee9c4 | 62 | return 0; |
e61abf83 | 63 | |
2d7da676 | 64 | if (NISENTRYLEN(0, 0, result) >= room_left) |
e61abf83 | 65 | { |
2d7da676 UD |
66 | /* The line is too long for our buffer. */ |
67 | no_more_room: | |
d71b808a | 68 | *errnop = ERANGE; |
60c96635 | 69 | return -1; |
e61abf83 UD |
70 | } |
71 | ||
2d7da676 UD |
72 | strncpy (first_unused, NISENTRYVAL(0, 0, result), |
73 | NISENTRYLEN (0, 0, result)); | |
d71b808a | 74 | first_unused[NISENTRYLEN (0, 0, result)] = '\0'; |
2d7da676 UD |
75 | network->n_name = first_unused; |
76 | room_left -= (strlen (first_unused) +1); | |
77 | first_unused += strlen (first_unused) +1; | |
78 | network->n_addrtype = 0; | |
79 | network->n_net = inet_network (NISENTRYVAL (0, 2, result)); | |
80 | p = first_unused; | |
81 | ||
82 | line = p; | |
880f421f | 83 | for (i = 0; i < result->objects.objects_len; ++i) |
2d7da676 UD |
84 | { |
85 | if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0) | |
86 | { | |
87 | if (NISENTRYLEN (i, 1, result) + 2 > room_left) | |
60c96635 | 88 | goto no_more_room; |
8f2ece69 | 89 | |
cd6ede75 | 90 | *p++ = ' '; |
9a0a462c UD |
91 | p = __stpncpy (p, NISENTRYVAL (i, 1, result), |
92 | NISENTRYLEN (i, 1, result)); | |
2d7da676 UD |
93 | *p = '\0'; |
94 | room_left -= (NISENTRYLEN (i, 1, result) + 1); | |
95 | } | |
96 | } | |
880f421f | 97 | *p++ = '\0'; |
2d7da676 UD |
98 | first_unused = p; |
99 | ||
100 | /* Adjust the pointer so it is aligned for | |
101 | storing pointers. */ | |
102 | first_unused += __alignof__ (char *) - 1; | |
103 | first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *)); | |
104 | network->n_aliases = (char **) first_unused; | |
105 | if (room_left < 2 * sizeof (char *)) | |
106 | goto no_more_room; | |
107 | room_left -= (2 * sizeof (char *)); | |
108 | network->n_aliases[0] = NULL; | |
109 | ||
880f421f | 110 | i = 0; |
2d7da676 UD |
111 | while (*line != '\0') |
112 | { | |
113 | /* Skip leading blanks. */ | |
114 | while (isspace (*line)) | |
115 | line++; | |
e61abf83 | 116 | |
2d7da676 UD |
117 | if (*line == '\0') |
118 | break; | |
e61abf83 | 119 | |
2d7da676 | 120 | if (room_left < sizeof (char *)) |
60c96635 | 121 | goto no_more_room; |
2d7da676 UD |
122 | |
123 | room_left -= sizeof (char *); | |
124 | network->n_aliases[i] = line; | |
125 | ||
126 | while (*line != '\0' && *line != ' ') | |
127 | ++line; | |
128 | ||
880f421f UD |
129 | if (*line == ' ') |
130 | { | |
131 | *line = '\0'; | |
132 | ++line; | |
2d7da676 UD |
133 | ++i; |
134 | } | |
135 | else | |
880f421f | 136 | network->n_aliases[i+1] = NULL; |
e61abf83 | 137 | } |
e61abf83 | 138 | |
2d7da676 UD |
139 | return 1; |
140 | } | |
141 | ||
142 | static enum nss_status | |
d71b808a | 143 | _nss_create_tablename (int *errnop) |
2d7da676 UD |
144 | { |
145 | if (tablename_val == NULL) | |
e61abf83 | 146 | { |
2d7da676 UD |
147 | char buf [40 + strlen (nis_local_directory ())]; |
148 | char *p; | |
149 | ||
9a0a462c UD |
150 | p = __stpcpy (buf, "networks.org_dir."); |
151 | p = __stpcpy (p, nis_local_directory ()); | |
152 | tablename_val = __strdup (buf); | |
2d7da676 | 153 | if (tablename_val == NULL) |
d71b808a UD |
154 | { |
155 | *errnop = errno; | |
156 | return NSS_STATUS_TRYAGAIN; | |
157 | } | |
2d7da676 | 158 | tablename_len = strlen (tablename_val); |
e61abf83 | 159 | } |
2d7da676 | 160 | return NSS_STATUS_SUCCESS; |
e61abf83 UD |
161 | } |
162 | ||
163 | enum nss_status | |
51eecc4a | 164 | _nss_nisplus_setnetent (int stayopen) |
e61abf83 | 165 | { |
2d7da676 | 166 | enum nss_status status = NSS_STATUS_SUCCESS; |
d71b808a | 167 | int err; |
2d7da676 | 168 | |
e61abf83 UD |
169 | __libc_lock_lock (lock); |
170 | ||
171 | if (result) | |
172 | nis_freeresult (result); | |
173 | result = NULL; | |
2d7da676 UD |
174 | |
175 | if (tablename_val == NULL) | |
d71b808a | 176 | status = _nss_create_tablename (&err); |
e61abf83 UD |
177 | |
178 | __libc_lock_unlock (lock); | |
179 | ||
2d7da676 | 180 | return status; |
e61abf83 UD |
181 | } |
182 | ||
183 | enum nss_status | |
184 | _nss_nisplus_endnetent (void) | |
185 | { | |
186 | __libc_lock_lock (lock); | |
187 | ||
188 | if (result) | |
189 | nis_freeresult (result); | |
190 | result = NULL; | |
e61abf83 UD |
191 | |
192 | __libc_lock_unlock (lock); | |
193 | ||
194 | return NSS_STATUS_SUCCESS; | |
195 | } | |
196 | ||
197 | static enum nss_status | |
198 | internal_nisplus_getnetent_r (struct netent *network, char *buffer, | |
d71b808a | 199 | size_t buflen, int *errnop, int *herrnop) |
e61abf83 UD |
200 | { |
201 | int parse_res; | |
202 | ||
203 | /* Get the next entry until we found a correct one. */ | |
204 | do | |
205 | { | |
60c96635 UD |
206 | nis_result *saved_res; |
207 | ||
e61abf83 UD |
208 | if (result == NULL) |
209 | { | |
60c96635 UD |
210 | saved_res = NULL; |
211 | ||
2d7da676 | 212 | if (tablename_val == NULL) |
d71b808a UD |
213 | { |
214 | enum nss_status status = _nss_create_tablename (errnop); | |
215 | ||
216 | if (status != NSS_STATUS_SUCCESS) | |
217 | return status; | |
218 | } | |
e61abf83 | 219 | |
d71b808a | 220 | result = nis_first_entry (tablename_val); |
e61abf83 UD |
221 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
222 | { | |
223 | int retval; | |
8f2ece69 | 224 | |
e61abf83 | 225 | retval = niserr2nss (result->status); |
60c96635 UD |
226 | nis_freeresult (result); |
227 | result = NULL; | |
e61abf83 UD |
228 | if (retval == NSS_STATUS_TRYAGAIN) |
229 | { | |
230 | *herrnop = NETDB_INTERNAL; | |
d71b808a | 231 | *errnop = errno; |
2d7da676 | 232 | return retval; |
e61abf83 | 233 | } |
2d7da676 UD |
234 | else |
235 | return retval; | |
e61abf83 UD |
236 | } |
237 | } | |
238 | else | |
239 | { | |
240 | nis_result *res; | |
241 | ||
d71b808a | 242 | res = nis_next_entry (tablename_val, &result->cookie); |
60c96635 | 243 | saved_res = result; |
e61abf83 UD |
244 | result = res; |
245 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
246 | { | |
247 | int retval; | |
248 | ||
249 | retval = niserr2nss (result->status); | |
60c96635 UD |
250 | nis_freeresult (result); |
251 | result = saved_res; | |
e61abf83 UD |
252 | if (retval == NSS_STATUS_TRYAGAIN) |
253 | { | |
254 | *herrnop = NETDB_INTERNAL; | |
d71b808a | 255 | *errnop = errno; |
e61abf83 UD |
256 | } |
257 | return retval; | |
258 | } | |
259 | } | |
260 | ||
d71b808a UD |
261 | parse_res = _nss_nisplus_parse_netent (result, network, buffer, |
262 | buflen, errnop); | |
263 | if (parse_res == -1) | |
e61abf83 UD |
264 | { |
265 | *herrnop = NETDB_INTERNAL; | |
266 | return NSS_STATUS_TRYAGAIN; | |
267 | } | |
8f2ece69 | 268 | |
e61abf83 | 269 | } while (!parse_res); |
8f2ece69 | 270 | |
e61abf83 UD |
271 | return NSS_STATUS_SUCCESS; |
272 | } | |
273 | ||
274 | enum nss_status | |
275 | _nss_nisplus_getnetent_r (struct netent *result, char *buffer, | |
d71b808a | 276 | size_t buflen, int *errnop, int *herrnop) |
e61abf83 UD |
277 | { |
278 | int status; | |
279 | ||
280 | __libc_lock_lock (lock); | |
281 | ||
d71b808a UD |
282 | status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop, |
283 | herrnop); | |
e61abf83 UD |
284 | |
285 | __libc_lock_unlock (lock); | |
286 | ||
287 | return status; | |
288 | } | |
289 | ||
290 | enum nss_status | |
291 | _nss_nisplus_getnetbyname_r (const char *name, struct netent *network, | |
d71b808a UD |
292 | char *buffer, size_t buflen, int *errnop, |
293 | int *herrnop) | |
e61abf83 UD |
294 | { |
295 | int parse_res, retval; | |
296 | ||
2d7da676 | 297 | if (tablename_val == NULL) |
d71b808a UD |
298 | { |
299 | enum nss_status status = _nss_create_tablename (errnop); | |
300 | ||
301 | if (status != NSS_STATUS_SUCCESS) | |
302 | return status; | |
303 | } | |
2d7da676 | 304 | |
e61abf83 UD |
305 | if (name == NULL) |
306 | { | |
d71b808a | 307 | *errnop = EINVAL; |
e61abf83 UD |
308 | *herrnop = NETDB_INTERNAL; |
309 | return NSS_STATUS_UNAVAIL; | |
310 | } | |
311 | else | |
312 | { | |
313 | nis_result *result; | |
2d7da676 | 314 | char buf[strlen (name) + 255 + tablename_len]; |
34816665 | 315 | int olderr = errno; |
8f2ece69 | 316 | |
e61abf83 UD |
317 | /* Search at first in the alias list, and use the correct name |
318 | for the next search */ | |
d71b808a UD |
319 | sprintf (buf, "[name=%s],%s", name, tablename_val); |
320 | result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL); | |
e61abf83 UD |
321 | |
322 | /* If we do not find it, try it as original name. But if the | |
323 | database is correct, we should find it in the first case, too */ | |
d71b808a UD |
324 | if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) |
325 | || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ | |
326 | || strcmp (result->objects.objects_val[0].EN_data.en_type, | |
327 | "networks_tbl") != 0 | |
328 | || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3) | |
329 | sprintf (buf, "[cname=%s],%s", name, tablename_val); | |
e61abf83 | 330 | else |
d71b808a UD |
331 | sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result), |
332 | tablename_val); | |
e61abf83 UD |
333 | |
334 | nis_freeresult (result); | |
d71b808a | 335 | result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL); |
e61abf83 UD |
336 | |
337 | retval = niserr2nss (result->status); | |
338 | if (retval != NSS_STATUS_SUCCESS) | |
339 | { | |
340 | if (retval == NSS_STATUS_TRYAGAIN) | |
341 | { | |
d71b808a | 342 | *errnop = errno; |
e61abf83 UD |
343 | *herrnop = NETDB_INTERNAL; |
344 | } | |
34816665 UD |
345 | else |
346 | __set_errno (olderr); | |
e61abf83 UD |
347 | nis_freeresult (result); |
348 | return retval; | |
349 | } | |
350 | ||
d71b808a UD |
351 | parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen, |
352 | errnop); | |
e61abf83 UD |
353 | |
354 | nis_freeresult (result); | |
355 | ||
60c96635 | 356 | if (parse_res > 0) |
e61abf83 UD |
357 | return NSS_STATUS_SUCCESS; |
358 | ||
359 | *herrnop = NETDB_INTERNAL; | |
60c96635 | 360 | if (parse_res == -1) |
d71b808a UD |
361 | { |
362 | *errnop = ERANGE; | |
363 | return NSS_STATUS_TRYAGAIN; | |
364 | } | |
e61abf83 | 365 | else |
34816665 UD |
366 | { |
367 | __set_errno (olderr); | |
368 | return NSS_STATUS_NOTFOUND; | |
369 | } | |
e61abf83 UD |
370 | } |
371 | } | |
372 | ||
373 | /* XXX type is ignored, SUN's NIS+ table doesn't support it */ | |
374 | enum nss_status | |
9b48fa9b | 375 | _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type, |
d71b808a UD |
376 | struct netent *network, char *buffer, |
377 | size_t buflen, int *errnop, int *herrnop) | |
e61abf83 | 378 | { |
2d7da676 | 379 | if (tablename_val == NULL) |
d71b808a UD |
380 | { |
381 | enum nss_status status = _nss_create_tablename (errnop); | |
382 | ||
383 | if (status != NSS_STATUS_SUCCESS) | |
384 | return status; | |
385 | } | |
e61abf83 | 386 | |
2d7da676 UD |
387 | { |
388 | int parse_res, retval; | |
389 | nis_result *result; | |
390 | char buf[1024 + tablename_len]; | |
391 | struct in_addr in; | |
880f421f UD |
392 | char buf2[256]; |
393 | int b2len; | |
34816665 | 394 | int olderr = errno; |
e61abf83 | 395 | |
2d7da676 | 396 | in = inet_makeaddr (addr, 0); |
880f421f UD |
397 | strcpy (buf2, inet_ntoa (in)); |
398 | b2len = strlen (buf2); | |
e61abf83 | 399 | |
880f421f | 400 | while (1) |
2d7da676 | 401 | { |
880f421f UD |
402 | sprintf (buf, "[addr=%s],%s", buf2, tablename_val); |
403 | result = nis_list (buf, EXPAND_NAME, NULL, NULL); | |
404 | ||
405 | retval = niserr2nss (result->status); | |
406 | if (retval != NSS_STATUS_SUCCESS) | |
2d7da676 | 407 | { |
880f421f UD |
408 | if (buf2[b2len -2] == '.' && buf2[b2len -1] == '0') |
409 | { | |
410 | /* Try again, but with trailing dot(s) | |
411 | removed (one by one) */ | |
412 | buf2[b2len - 2] = '\0'; | |
413 | b2len -= 2; | |
414 | continue; | |
415 | } | |
416 | else | |
417 | return NSS_STATUS_NOTFOUND; | |
418 | ||
419 | if (retval == NSS_STATUS_TRYAGAIN) | |
420 | { | |
421 | *errnop = errno; | |
422 | *herrnop = NETDB_INTERNAL; | |
423 | } | |
34816665 UD |
424 | else |
425 | __set_errno (olderr); | |
880f421f UD |
426 | nis_freeresult (result); |
427 | return retval; | |
2d7da676 | 428 | } |
e61abf83 | 429 | |
880f421f UD |
430 | parse_res = _nss_nisplus_parse_netent (result, network, buffer, |
431 | buflen, errnop); | |
e61abf83 | 432 | |
880f421f | 433 | nis_freeresult (result); |
e61abf83 | 434 | |
880f421f UD |
435 | if (parse_res > 0) |
436 | return NSS_STATUS_SUCCESS; | |
8f2ece69 | 437 | |
880f421f UD |
438 | *herrnop = NETDB_INTERNAL; |
439 | if (parse_res == -1) | |
440 | { | |
441 | *errnop = ERANGE; | |
442 | return NSS_STATUS_TRYAGAIN; | |
443 | } | |
444 | else | |
34816665 UD |
445 | { |
446 | __set_errno (olderr); | |
447 | return NSS_STATUS_NOTFOUND; | |
448 | } | |
d71b808a | 449 | } |
2d7da676 | 450 | } |
e61abf83 | 451 | } |