]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 1997-2020 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 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
e61abf83 | 18 | |
636e689e | 19 | #include <atomic.h> |
a334319f | 20 | #include <ctype.h> |
636e689e UD |
21 | #include <errno.h> |
22 | #include <netdb.h> | |
23 | #include <nss.h> | |
9b48fa9b | 24 | #include <stdint.h> |
e61abf83 UD |
25 | #include <string.h> |
26 | #include <arpa/inet.h> | |
a334319f | 27 | #include <rpcsvc/nis.h> |
ec999b8e | 28 | #include <libc-lock.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 | 37 | |
f514f945 UD |
38 | #define NISENTRYVAL(idx, col, res) \ |
39 | (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val) | |
e61abf83 | 40 | |
f514f945 UD |
41 | #define NISENTRYLEN(idx, col, res) \ |
42 | (NIS_RES_OBJECT (res)[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, | |
636e689e | 47 | char *buffer, size_t buflen, int *errnop) |
e61abf83 | 48 | { |
2d7da676 | 49 | char *first_unused = buffer; |
e61abf83 | 50 | size_t room_left = buflen; |
e61abf83 UD |
51 | |
52 | if (result == NULL) | |
26dee9c4 | 53 | return 0; |
e61abf83 | 54 | |
d71b808a | 55 | if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) |
f514f945 UD |
56 | || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ |
57 | || strcmp (NIS_RES_OBJECT (result)[0].EN_data.en_type, | |
d71b808a | 58 | "networks_tbl") != 0 |
f514f945 | 59 | || NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 3) |
26dee9c4 | 60 | return 0; |
e61abf83 | 61 | |
636e689e | 62 | if (NISENTRYLEN (0, 0, result) >= room_left) |
e61abf83 | 63 | { |
2d7da676 UD |
64 | /* The line is too long for our buffer. */ |
65 | no_more_room: | |
d71b808a | 66 | *errnop = ERANGE; |
60c96635 | 67 | return -1; |
e61abf83 UD |
68 | } |
69 | ||
636e689e | 70 | strncpy (first_unused, NISENTRYVAL (0, 0, result), |
2d7da676 | 71 | NISENTRYLEN (0, 0, result)); |
d71b808a | 72 | first_unused[NISENTRYLEN (0, 0, result)] = '\0'; |
2d7da676 | 73 | network->n_name = first_unused; |
f514f945 UD |
74 | size_t len = strlen (first_unused) + 1; |
75 | room_left -= len; | |
76 | first_unused += len; | |
77 | ||
2d7da676 UD |
78 | network->n_addrtype = 0; |
79 | network->n_net = inet_network (NISENTRYVAL (0, 2, result)); | |
2d7da676 | 80 | |
f514f945 UD |
81 | /* XXX Rewrite at some point to allocate the array first and then |
82 | copy the strings. It wasteful to first concatenate the strings | |
83 | to just split them again later. */ | |
84 | char *line = first_unused; | |
85 | for (unsigned int i = 0; i < NIS_RES_NUMOBJ (result); ++i) | |
2d7da676 UD |
86 | { |
87 | if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0) | |
88 | { | |
89 | if (NISENTRYLEN (i, 1, result) + 2 > room_left) | |
60c96635 | 90 | goto no_more_room; |
8f2ece69 | 91 | |
f514f945 UD |
92 | *first_unused++ = ' '; |
93 | first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result), | |
94 | NISENTRYLEN (i, 1, result)); | |
2d7da676 UD |
95 | room_left -= (NISENTRYLEN (i, 1, result) + 1); |
96 | } | |
97 | } | |
f514f945 | 98 | *first_unused++ = '\0'; |
2d7da676 UD |
99 | |
100 | /* Adjust the pointer so it is aligned for | |
101 | storing pointers. */ | |
f514f945 UD |
102 | size_t adjust = ((__alignof__ (char *) |
103 | - (first_unused - (char *) 0) % __alignof__ (char *)) | |
104 | % __alignof__ (char *)); | |
105 | if (room_left < adjust + sizeof (char *)) | |
a334319f | 106 | goto no_more_room; |
f514f945 UD |
107 | first_unused += adjust; |
108 | room_left -= adjust; | |
109 | network->n_aliases = (char **) first_unused; | |
110 | ||
111 | /* For the terminating NULL pointer. */ | |
112 | room_left -= sizeof (char *); | |
0ecb606c | 113 | |
636e689e | 114 | unsigned int i = 0; |
2d7da676 UD |
115 | while (*line != '\0') |
116 | { | |
117 | /* Skip leading blanks. */ | |
118 | while (isspace (*line)) | |
636e689e | 119 | ++line; |
e61abf83 | 120 | |
2d7da676 UD |
121 | if (*line == '\0') |
122 | break; | |
e61abf83 | 123 | |
2d7da676 | 124 | if (room_left < sizeof (char *)) |
60c96635 | 125 | goto no_more_room; |
2d7da676 UD |
126 | |
127 | room_left -= sizeof (char *); | |
f514f945 | 128 | network->n_aliases[i++] = line; |
2d7da676 UD |
129 | |
130 | while (*line != '\0' && *line != ' ') | |
131 | ++line; | |
132 | ||
880f421f | 133 | if (*line == ' ') |
f514f945 | 134 | *line++ = '\0'; |
e61abf83 | 135 | } |
f514f945 | 136 | network->n_aliases[i] = NULL; |
e61abf83 | 137 | |
2d7da676 UD |
138 | return 1; |
139 | } | |
140 | ||
f514f945 | 141 | |
2d7da676 | 142 | static enum nss_status |
d71b808a | 143 | _nss_create_tablename (int *errnop) |
2d7da676 UD |
144 | { |
145 | if (tablename_val == NULL) | |
e61abf83 | 146 | { |
636e689e UD |
147 | const char *local_dir = nis_local_directory (); |
148 | size_t local_dir_len = strlen (local_dir); | |
149 | static const char prefix[] = "networks.org_dir."; | |
2d7da676 | 150 | |
636e689e | 151 | char *p = malloc (sizeof (prefix) + local_dir_len); |
c8e82b4a | 152 | if (p == NULL) |
d71b808a UD |
153 | { |
154 | *errnop = errno; | |
155 | return NSS_STATUS_TRYAGAIN; | |
156 | } | |
636e689e UD |
157 | |
158 | memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1); | |
159 | ||
160 | tablename_len = sizeof (prefix) - 1 + local_dir_len; | |
161 | ||
162 | atomic_write_barrier (); | |
163 | ||
164 | tablename_val = p; | |
e61abf83 | 165 | } |
636e689e | 166 | |
2d7da676 | 167 | return NSS_STATUS_SUCCESS; |
e61abf83 UD |
168 | } |
169 | ||
170 | enum nss_status | |
51eecc4a | 171 | _nss_nisplus_setnetent (int stayopen) |
e61abf83 | 172 | { |
2d7da676 UD |
173 | enum nss_status status = NSS_STATUS_SUCCESS; |
174 | ||
e61abf83 UD |
175 | __libc_lock_lock (lock); |
176 | ||
636e689e UD |
177 | if (result != NULL) |
178 | { | |
179 | nis_freeresult (result); | |
180 | result = NULL; | |
181 | } | |
2d7da676 UD |
182 | |
183 | if (tablename_val == NULL) | |
636e689e UD |
184 | { |
185 | int err; | |
186 | status = _nss_create_tablename (&err); | |
187 | } | |
e61abf83 UD |
188 | |
189 | __libc_lock_unlock (lock); | |
190 | ||
2d7da676 | 191 | return status; |
e61abf83 UD |
192 | } |
193 | ||
194 | enum nss_status | |
195 | _nss_nisplus_endnetent (void) | |
196 | { | |
197 | __libc_lock_lock (lock); | |
198 | ||
636e689e UD |
199 | if (result != NULL) |
200 | { | |
201 | nis_freeresult (result); | |
202 | result = NULL; | |
203 | } | |
e61abf83 UD |
204 | |
205 | __libc_lock_unlock (lock); | |
206 | ||
207 | return NSS_STATUS_SUCCESS; | |
208 | } | |
209 | ||
210 | static enum nss_status | |
211 | internal_nisplus_getnetent_r (struct netent *network, char *buffer, | |
d71b808a | 212 | size_t buflen, int *errnop, int *herrnop) |
e61abf83 UD |
213 | { |
214 | int parse_res; | |
215 | ||
216 | /* Get the next entry until we found a correct one. */ | |
217 | do | |
218 | { | |
60c96635 UD |
219 | nis_result *saved_res; |
220 | ||
e61abf83 UD |
221 | if (result == NULL) |
222 | { | |
60c96635 UD |
223 | saved_res = NULL; |
224 | ||
2d7da676 | 225 | if (tablename_val == NULL) |
d71b808a UD |
226 | { |
227 | enum nss_status status = _nss_create_tablename (errnop); | |
228 | ||
229 | if (status != NSS_STATUS_SUCCESS) | |
230 | return status; | |
231 | } | |
e61abf83 | 232 | |
d71b808a | 233 | result = nis_first_entry (tablename_val); |
487609e3 UD |
234 | if (result == NULL) |
235 | { | |
236 | *errnop = errno; | |
237 | return NSS_STATUS_TRYAGAIN; | |
238 | } | |
e61abf83 UD |
239 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
240 | { | |
636e689e | 241 | int retval = niserr2nss (result->status); |
60c96635 UD |
242 | nis_freeresult (result); |
243 | result = NULL; | |
e61abf83 UD |
244 | if (retval == NSS_STATUS_TRYAGAIN) |
245 | { | |
246 | *herrnop = NETDB_INTERNAL; | |
d71b808a | 247 | *errnop = errno; |
2d7da676 | 248 | return retval; |
e61abf83 | 249 | } |
2d7da676 UD |
250 | else |
251 | return retval; | |
e61abf83 UD |
252 | } |
253 | } | |
254 | else | |
255 | { | |
60c96635 | 256 | saved_res = result; |
487609e3 UD |
257 | result = nis_next_entry (tablename_val, &result->cookie); |
258 | if (result == NULL) | |
259 | { | |
260 | *errnop = errno; | |
261 | return NSS_STATUS_TRYAGAIN; | |
262 | } | |
e61abf83 UD |
263 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
264 | { | |
636e689e | 265 | int retval = niserr2nss (result->status); |
60c96635 UD |
266 | nis_freeresult (result); |
267 | result = saved_res; | |
e61abf83 UD |
268 | if (retval == NSS_STATUS_TRYAGAIN) |
269 | { | |
270 | *herrnop = NETDB_INTERNAL; | |
d71b808a | 271 | *errnop = errno; |
e61abf83 UD |
272 | } |
273 | return retval; | |
274 | } | |
275 | } | |
276 | ||
d71b808a UD |
277 | parse_res = _nss_nisplus_parse_netent (result, network, buffer, |
278 | buflen, errnop); | |
279 | if (parse_res == -1) | |
e61abf83 UD |
280 | { |
281 | *herrnop = NETDB_INTERNAL; | |
282 | return NSS_STATUS_TRYAGAIN; | |
283 | } | |
8f2ece69 | 284 | |
636e689e UD |
285 | } |
286 | while (!parse_res); | |
8f2ece69 | 287 | |
e61abf83 UD |
288 | return NSS_STATUS_SUCCESS; |
289 | } | |
290 | ||
291 | enum nss_status | |
292 | _nss_nisplus_getnetent_r (struct netent *result, char *buffer, | |
d71b808a | 293 | size_t buflen, int *errnop, int *herrnop) |
e61abf83 UD |
294 | { |
295 | int status; | |
296 | ||
297 | __libc_lock_lock (lock); | |
298 | ||
d71b808a UD |
299 | status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop, |
300 | herrnop); | |
e61abf83 UD |
301 | |
302 | __libc_lock_unlock (lock); | |
303 | ||
304 | return status; | |
305 | } | |
306 | ||
307 | enum nss_status | |
308 | _nss_nisplus_getnetbyname_r (const char *name, struct netent *network, | |
d71b808a UD |
309 | char *buffer, size_t buflen, int *errnop, |
310 | int *herrnop) | |
e61abf83 UD |
311 | { |
312 | int parse_res, retval; | |
313 | ||
2d7da676 | 314 | if (tablename_val == NULL) |
d71b808a | 315 | { |
636e689e UD |
316 | __libc_lock_lock (lock); |
317 | ||
d71b808a UD |
318 | enum nss_status status = _nss_create_tablename (errnop); |
319 | ||
636e689e UD |
320 | __libc_lock_unlock (lock); |
321 | ||
d71b808a UD |
322 | if (status != NSS_STATUS_SUCCESS) |
323 | return status; | |
324 | } | |
2d7da676 | 325 | |
e61abf83 UD |
326 | if (name == NULL) |
327 | { | |
d71b808a | 328 | *errnop = EINVAL; |
e61abf83 UD |
329 | *herrnop = NETDB_INTERNAL; |
330 | return NSS_STATUS_UNAVAIL; | |
331 | } | |
8f2ece69 | 332 | |
636e689e UD |
333 | nis_result *result; |
334 | char buf[strlen (name) + 10 + tablename_len]; | |
335 | int olderr = errno; | |
e61abf83 | 336 | |
636e689e UD |
337 | /* Search at first in the alias list, and use the correct name |
338 | for the next search */ | |
339 | snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val); | |
697d37b1 | 340 | result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM, NULL, NULL); |
e61abf83 | 341 | |
636e689e UD |
342 | if (result != NULL) |
343 | { | |
344 | char *bufptr = buf; | |
345 | ||
346 | /* If we do not find it, try it as original name. But if the | |
347 | database is correct, we should find it in the first case, too */ | |
348 | if ((result->status != NIS_SUCCESS | |
349 | && result->status != NIS_S_SUCCESS) | |
350 | || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ | |
351 | || strcmp (result->objects.objects_val[0].EN_data.en_type, | |
352 | "networks_tbl") != 0 | |
353 | || (result->objects.objects_val[0].EN_data.en_cols.en_cols_len | |
354 | < 3)) | |
355 | snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val); | |
356 | else | |
e61abf83 | 357 | { |
636e689e UD |
358 | /* We need to allocate a new buffer since there is no |
359 | guarantee the returned name has a length limit. */ | |
360 | const char *entryval = NISENTRYVAL (0, 0, result); | |
361 | size_t buflen = strlen (entryval) + 10 + tablename_len; | |
362 | bufptr = alloca (buflen); | |
363 | snprintf (bufptr, buflen, "[cname=%s],%s", | |
364 | entryval, tablename_val); | |
e61abf83 UD |
365 | } |
366 | ||
e61abf83 | 367 | nis_freeresult (result); |
697d37b1 JJ |
368 | result = nis_list (bufptr, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM, |
369 | NULL, NULL); | |
636e689e | 370 | } |
e61abf83 | 371 | |
636e689e UD |
372 | if (result == NULL) |
373 | { | |
374 | __set_errno (ENOMEM); | |
375 | return NSS_STATUS_TRYAGAIN; | |
376 | } | |
e61abf83 | 377 | |
636e689e | 378 | retval = niserr2nss (result->status); |
a1ffb40e | 379 | if (__glibc_unlikely (retval != NSS_STATUS_SUCCESS)) |
636e689e UD |
380 | { |
381 | if (retval == NSS_STATUS_TRYAGAIN) | |
d71b808a | 382 | { |
636e689e UD |
383 | *errnop = errno; |
384 | *herrnop = NETDB_INTERNAL; | |
d71b808a | 385 | } |
e61abf83 | 386 | else |
636e689e UD |
387 | __set_errno (olderr); |
388 | nis_freeresult (result); | |
389 | return retval; | |
0ecb606c | 390 | } |
636e689e UD |
391 | |
392 | parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen, | |
393 | errnop); | |
394 | ||
395 | nis_freeresult (result); | |
396 | ||
397 | if (parse_res > 0) | |
398 | return NSS_STATUS_SUCCESS; | |
399 | ||
400 | *herrnop = NETDB_INTERNAL; | |
401 | if (parse_res == -1) | |
402 | { | |
403 | *errnop = ERANGE; | |
404 | return NSS_STATUS_TRYAGAIN; | |
405 | } | |
406 | ||
407 | __set_errno (olderr); | |
408 | return NSS_STATUS_NOTFOUND; | |
e61abf83 UD |
409 | } |
410 | ||
411 | /* XXX type is ignored, SUN's NIS+ table doesn't support it */ | |
412 | enum nss_status | |
9b48fa9b | 413 | _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type, |
d71b808a UD |
414 | struct netent *network, char *buffer, |
415 | size_t buflen, int *errnop, int *herrnop) | |
e61abf83 | 416 | { |
2d7da676 | 417 | if (tablename_val == NULL) |
d71b808a | 418 | { |
636e689e UD |
419 | __libc_lock_lock (lock); |
420 | ||
d71b808a UD |
421 | enum nss_status status = _nss_create_tablename (errnop); |
422 | ||
636e689e UD |
423 | __libc_lock_unlock (lock); |
424 | ||
d71b808a UD |
425 | if (status != NSS_STATUS_SUCCESS) |
426 | return status; | |
427 | } | |
e61abf83 | 428 | |
2d7da676 | 429 | { |
9069c5e9 UD |
430 | char buf[27 + tablename_len]; |
431 | char buf2[18]; | |
34816665 | 432 | int olderr = errno; |
e61abf83 | 433 | |
2fd0cd8b | 434 | struct in_addr in = { .s_addr = htonl (addr) }; |
880f421f | 435 | strcpy (buf2, inet_ntoa (in)); |
9069c5e9 | 436 | size_t b2len = strlen (buf2); |
e61abf83 | 437 | |
880f421f | 438 | while (1) |
2d7da676 | 439 | { |
9069c5e9 | 440 | snprintf (buf, sizeof (buf), "[addr=%s],%s", buf2, tablename_val); |
697d37b1 JJ |
441 | nis_result *result = nis_list (buf, EXPAND_NAME | USE_DGRAM, |
442 | NULL, NULL); | |
880f421f | 443 | |
901956a5 UD |
444 | if (result == NULL) |
445 | { | |
446 | __set_errno (ENOMEM); | |
447 | return NSS_STATUS_TRYAGAIN; | |
448 | } | |
9069c5e9 | 449 | enum nss_status retval = niserr2nss (result->status); |
a1ffb40e | 450 | if (__glibc_unlikely (retval != NSS_STATUS_SUCCESS)) |
2d7da676 | 451 | { |
9069c5e9 | 452 | if (b2len > 2 && buf2[b2len - 2] == '.' && buf2[b2len - 1] == '0') |
880f421f UD |
453 | { |
454 | /* Try again, but with trailing dot(s) | |
455 | removed (one by one) */ | |
456 | buf2[b2len - 2] = '\0'; | |
457 | b2len -= 2; | |
0292b0dd | 458 | nis_freeresult (result); |
880f421f UD |
459 | continue; |
460 | } | |
880f421f UD |
461 | |
462 | if (retval == NSS_STATUS_TRYAGAIN) | |
463 | { | |
464 | *errnop = errno; | |
465 | *herrnop = NETDB_INTERNAL; | |
466 | } | |
34816665 UD |
467 | else |
468 | __set_errno (olderr); | |
880f421f UD |
469 | nis_freeresult (result); |
470 | return retval; | |
2d7da676 | 471 | } |
e61abf83 | 472 | |
9069c5e9 UD |
473 | int parse_res = _nss_nisplus_parse_netent (result, network, buffer, |
474 | buflen, errnop); | |
e61abf83 | 475 | |
880f421f | 476 | nis_freeresult (result); |
e61abf83 | 477 | |
880f421f UD |
478 | if (parse_res > 0) |
479 | return NSS_STATUS_SUCCESS; | |
8f2ece69 | 480 | |
880f421f UD |
481 | *herrnop = NETDB_INTERNAL; |
482 | if (parse_res == -1) | |
483 | { | |
484 | *errnop = ERANGE; | |
485 | return NSS_STATUS_TRYAGAIN; | |
486 | } | |
487 | else | |
34816665 UD |
488 | { |
489 | __set_errno (olderr); | |
490 | return NSS_STATUS_NOTFOUND; | |
491 | } | |
d71b808a | 492 | } |
2d7da676 | 493 | } |
e61abf83 | 494 | } |