]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-spwd.c
Update.
[thirdparty/glibc.git] / nis / nss_compat / compat-spwd.c
1 /* Copyright (C) 1996,1997,1998,1999,2001,2002,2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
4
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
14
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. */
19
20 #include <nss.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include <netdb.h>
25 #include <shadow.h>
26 #include <string.h>
27 #include <bits/libc-lock.h>
28 #include <rpc/types.h>
29 #include <rpcsvc/ypclnt.h>
30 #include <nsswitch.h>
31
32 #include "netgroup.h"
33
34 static service_user *ni;
35 static enum nss_status (*nss_setspent) (int stayopen);
36 static enum nss_status (*nss_getspnam_r) (const char *name, struct spwd * sp,
37 char *buffer, size_t buflen,
38 int *errnop);
39 static enum nss_status (*nss_getspent_r) (struct spwd * sp, char *buffer,
40 size_t buflen, int *errnop);
41 static enum nss_status (*nss_endspent) (void);
42
43 /* Get the declaration of the parser function. */
44 #define ENTNAME spent
45 #define STRUCTURE spwd
46 #define EXTERN_PARSER
47 #include <nss/nss_files/files-parse.c>
48
49 /* Structure for remembering -@netgroup and -user members ... */
50 #define BLACKLIST_INITIAL_SIZE 512
51 #define BLACKLIST_INCREMENT 256
52 struct blacklist_t
53 {
54 char *data;
55 int current;
56 int size;
57 };
58
59 struct ent_t
60 {
61 bool_t netgroup;
62 bool_t files;
63 bool_t first;
64 FILE *stream;
65 struct blacklist_t blacklist;
66 struct spwd pwd;
67 struct __netgrent netgrdata;
68 };
69 typedef struct ent_t ent_t;
70
71 static ent_t ext_ent = {0, TRUE, 0, NULL, {NULL, 0, 0},
72 {NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
73
74 /* Protect global state against multiple changers. */
75 __libc_lock_define_initialized (static, lock)
76
77 /* Prototypes for local functions. */
78 static void blacklist_store_name (const char *, ent_t *);
79 static int in_blacklist (const char *, int, ent_t *);
80
81 /* Initialize the NSS interface/functions. The calling function must
82 hold the lock. */
83 static void
84 init_nss_interface (void)
85 {
86 if (__nss_database_lookup ("shadow_compat", "passwd_compat",
87 "nis", &ni) >= 0)
88 {
89 nss_setspent = __nss_lookup_function (ni, "setspent");
90 nss_getspnam_r = __nss_lookup_function (ni, "getspnam_r");
91 nss_getspent_r = __nss_lookup_function (ni, "getspent_r");
92 nss_endspent = __nss_lookup_function (ni, "endspent");
93 }
94 }
95
96 static void
97 give_spwd_free (struct spwd *pwd)
98 {
99 if (pwd->sp_namp != NULL)
100 free (pwd->sp_namp);
101 if (pwd->sp_pwdp != NULL)
102 free (pwd->sp_pwdp);
103
104 memset (pwd, '\0', sizeof (struct spwd));
105 pwd->sp_warn = -1;
106 pwd->sp_inact = -1;
107 pwd->sp_expire = -1;
108 pwd->sp_flag = ~0ul;
109 }
110
111 static int
112 spwd_need_buflen (struct spwd *pwd)
113 {
114 int len = 0;
115
116 if (pwd->sp_pwdp != NULL)
117 len += strlen (pwd->sp_pwdp) + 1;
118
119 return len;
120 }
121
122 static void
123 copy_spwd_changes (struct spwd *dest, struct spwd *src,
124 char *buffer, size_t buflen)
125 {
126 if (src->sp_pwdp != NULL && strlen (src->sp_pwdp))
127 {
128 if (buffer == NULL)
129 dest->sp_pwdp = strdup (src->sp_pwdp);
130 else if (dest->sp_pwdp &&
131 strlen (dest->sp_pwdp) >= strlen (src->sp_pwdp))
132 strcpy (dest->sp_pwdp, src->sp_pwdp);
133 else
134 {
135 dest->sp_pwdp = buffer;
136 strcpy (dest->sp_pwdp, src->sp_pwdp);
137 buffer += strlen (dest->sp_pwdp) + 1;
138 buflen = buflen - (strlen (dest->sp_pwdp) + 1);
139 }
140 }
141 if (src->sp_lstchg != 0)
142 dest->sp_lstchg = src->sp_lstchg;
143 if (src->sp_min != 0)
144 dest->sp_min = src->sp_min;
145 if (src->sp_max != 0)
146 dest->sp_max = src->sp_max;
147 if (src->sp_warn != -1)
148 dest->sp_warn = src->sp_warn;
149 if (src->sp_inact != -1)
150 dest->sp_inact = src->sp_inact;
151 if (src->sp_expire != -1)
152 dest->sp_expire = src->sp_expire;
153 if (src->sp_flag != ~0ul)
154 dest->sp_flag = src->sp_flag;
155 }
156
157 static enum nss_status
158 internal_setspent (ent_t *ent, int stayopen)
159 {
160 enum nss_status status = NSS_STATUS_SUCCESS;
161
162 ent->first = ent->netgroup = 0;
163 ent->files = TRUE;
164
165 /* If something was left over free it. */
166 if (ent->netgroup)
167 __internal_endnetgrent (&ent->netgrdata);
168
169 if (ent->blacklist.data != NULL)
170 {
171 ent->blacklist.current = 1;
172 ent->blacklist.data[0] = '|';
173 ent->blacklist.data[1] = '\0';
174 }
175 else
176 ent->blacklist.current = 0;
177
178 if (ent->stream == NULL)
179 {
180 ent->stream = fopen ("/etc/shadow", "r");
181
182 if (ent->stream == NULL)
183 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
184 else
185 {
186 /* We have to make sure the file is `closed on exec'. */
187 int result, flags;
188
189 result = flags = fcntl (fileno (ent->stream), F_GETFD, 0);
190 if (result >= 0)
191 {
192 flags |= FD_CLOEXEC;
193 result = fcntl (fileno (ent->stream), F_SETFD, flags);
194 }
195 if (result < 0)
196 {
197 /* Something went wrong. Close the stream and return a
198 failure. */
199 fclose (ent->stream);
200 ent->stream = NULL;
201 status = NSS_STATUS_UNAVAIL;
202 }
203 }
204 }
205 else
206 rewind (ent->stream);
207
208 give_spwd_free (&ent->pwd);
209
210 if (status == NSS_STATUS_SUCCESS && nss_setspent)
211 return nss_setspent (stayopen);
212
213 return status;
214 }
215
216
217 enum nss_status
218 _nss_compat_setspent (int stayopen)
219 {
220 enum nss_status result;
221
222 __libc_lock_lock (lock);
223
224 if (ni == NULL)
225 init_nss_interface ();
226
227 result = internal_setspent (&ext_ent, stayopen);
228
229 __libc_lock_unlock (lock);
230
231 return result;
232 }
233
234
235 static enum nss_status
236 internal_endspent (ent_t *ent)
237 {
238 if (nss_endspent)
239 nss_endspent ();
240
241 if (ent->stream != NULL)
242 {
243 fclose (ent->stream);
244 ent->stream = NULL;
245 }
246
247 if (ent->netgroup)
248 __internal_endnetgrent (&ent->netgrdata);
249
250 ent->first = ent->netgroup = FALSE;
251 ent->files = TRUE;
252
253 if (ent->blacklist.data != NULL)
254 {
255 ent->blacklist.current = 1;
256 ent->blacklist.data[0] = '|';
257 ent->blacklist.data[1] = '\0';
258 }
259 else
260 ent->blacklist.current = 0;
261
262 give_spwd_free (&ent->pwd);
263
264 return NSS_STATUS_SUCCESS;
265 }
266
267 enum nss_status
268 _nss_compat_endspent (void)
269 {
270 enum nss_status result;
271
272 __libc_lock_lock (lock);
273
274 result = internal_endspent (&ext_ent);
275
276 __libc_lock_unlock (lock);
277
278 return result;
279 }
280
281
282 static enum nss_status
283 getspent_next_nss_netgr (const char *name, struct spwd *result, ent_t *ent,
284 char *group, char *buffer, size_t buflen,
285 int *errnop)
286 {
287 char *curdomain, *host, *user, *domain, *p2;
288 size_t p2len;
289
290 if (!nss_getspnam_r)
291 return NSS_STATUS_UNAVAIL;
292
293 if (yp_get_default_domain (&curdomain) != YPERR_SUCCESS)
294 {
295 ent->netgroup = FALSE;
296 ent->first = FALSE;
297 give_spwd_free (&ent->pwd);
298 return NSS_STATUS_UNAVAIL;
299 }
300
301 if (ent->first == TRUE)
302 {
303 memset (&ent->netgrdata, 0, sizeof (struct __netgrent));
304 __internal_setnetgrent (group, &ent->netgrdata);
305 ent->first = FALSE;
306 }
307
308 while (1)
309 {
310 char *saved_cursor;
311 enum nss_status status;
312
313 saved_cursor = ent->netgrdata.cursor;
314 status = __internal_getnetgrent_r (&host, &user, &domain,
315 &ent->netgrdata, buffer, buflen,
316 errnop);
317 if (status != 1)
318 {
319 __internal_endnetgrent (&ent->netgrdata);
320 ent->netgroup = FALSE;
321 give_spwd_free (&ent->pwd);
322 return NSS_STATUS_RETURN;
323 }
324
325 if (user == NULL || user[0] == '-')
326 continue;
327
328 if (domain != NULL && strcmp (curdomain, domain) != 0)
329 continue;
330
331 /* If name != NULL, we are called from getpwnam */
332 if (name != NULL)
333 if (strcmp (user, name) != 0)
334 continue;
335
336 p2len = spwd_need_buflen (&ent->pwd);
337 if (p2len > buflen)
338 {
339 *errnop = ERANGE;
340 return NSS_STATUS_TRYAGAIN;
341 }
342 p2 = buffer + (buflen - p2len);
343 buflen -= p2len;
344
345 if (nss_getspnam_r (user, result, buffer, buflen, errnop) !=
346 NSS_STATUS_SUCCESS)
347 continue;
348
349 if (!in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
350 {
351 /* Store the User in the blacklist for possible the "+" at the
352 end of /etc/passwd */
353 blacklist_store_name (result->sp_namp, ent);
354 copy_spwd_changes (result, &ent->pwd, p2, p2len);
355 break;
356 }
357 }
358
359 return NSS_STATUS_SUCCESS;
360 }
361
362
363 static enum nss_status
364 getspent_next_nss (struct spwd *result, ent_t *ent,
365 char *buffer, size_t buflen, int *errnop)
366 {
367 enum nss_status status;
368 char *p2;
369 size_t p2len;
370
371 if (!nss_getspent_r)
372 return NSS_STATUS_UNAVAIL;
373
374 p2len = spwd_need_buflen (&ent->pwd);
375 if (p2len > buflen)
376 {
377 *errnop = ERANGE;
378 return NSS_STATUS_TRYAGAIN;
379 }
380 p2 = buffer + (buflen - p2len);
381 buflen -= p2len;
382 do
383 {
384 if ((status = nss_getspent_r (result, buffer, buflen, errnop)) !=
385 NSS_STATUS_SUCCESS)
386 return status;
387 }
388 while (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent));
389
390 copy_spwd_changes (result, &ent->pwd, p2, p2len);
391
392 return NSS_STATUS_SUCCESS;
393 }
394
395 /* This function handle the +user entrys in /etc/shadow */
396 static enum nss_status
397 getspnam_plususer (const char *name, struct spwd *result, ent_t *ent,
398 char *buffer, size_t buflen, int *errnop)
399 {
400 struct spwd pwd;
401 char *p;
402 size_t plen;
403
404 if (!nss_getspnam_r)
405 return NSS_STATUS_UNAVAIL;
406
407 memset (&pwd, '\0', sizeof (struct spwd));
408 pwd.sp_warn = -1;
409 pwd.sp_inact = -1;
410 pwd.sp_expire = -1;
411 pwd.sp_flag = ~0ul;
412
413 copy_spwd_changes (&pwd, result, NULL, 0);
414
415 plen = spwd_need_buflen (&pwd);
416 if (plen > buflen)
417 {
418 *errnop = ERANGE;
419 return NSS_STATUS_TRYAGAIN;
420 }
421 p = buffer + (buflen - plen);
422 buflen -= plen;
423
424 if (nss_getspnam_r (name, result, buffer, buflen, errnop) !=
425 NSS_STATUS_SUCCESS)
426 return NSS_STATUS_NOTFOUND;
427
428 if (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
429 return NSS_STATUS_NOTFOUND;
430
431 copy_spwd_changes (result, &pwd, p, plen);
432 give_spwd_free (&pwd);
433 /* We found the entry. */
434 return NSS_STATUS_RETURN;
435 }
436
437 static enum nss_status
438 getspent_next_file (struct spwd *result, ent_t *ent,
439 char *buffer, size_t buflen, int *errnop)
440 {
441 struct parser_data *data = (void *) buffer;
442 while (1)
443 {
444 fpos_t pos;
445 int parse_res = 0;
446 char *p;
447
448 do
449 {
450 fgetpos (ent->stream, &pos);
451 buffer[buflen - 1] = '\xff';
452 p = fgets (buffer, buflen, ent->stream);
453 if (p == NULL && feof (ent->stream))
454 return NSS_STATUS_NOTFOUND;
455
456 if (p == NULL || buffer[buflen - 1] != '\xff')
457 {
458 fsetpos (ent->stream, &pos);
459 *errnop = ERANGE;
460 return NSS_STATUS_TRYAGAIN;
461 }
462
463 /* Skip leading blanks. */
464 while (isspace (*p))
465 ++p;
466 }
467 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
468 /* Parse the line. If it is invalid, loop to
469 get the next line of the file to parse. */
470 || !(parse_res = _nss_files_parse_spent (p, result, data,
471 buflen, errnop)));
472
473 if (parse_res == -1)
474 {
475 /* The parser ran out of space. */
476 fsetpos (ent->stream, &pos);
477 *errnop = ERANGE;
478 return NSS_STATUS_TRYAGAIN;
479 }
480
481 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
482 /* This is a real entry. */
483 break;
484
485 /* -@netgroup */
486 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
487 && result->sp_namp[2] != '\0')
488 {
489 /* XXX Do not use fixed length buffers. */
490 char buf2[1024];
491 char *user, *host, *domain;
492 struct __netgrent netgrdata;
493
494 bzero (&netgrdata, sizeof (struct __netgrent));
495 __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
496 while (__internal_getnetgrent_r (&host, &user, &domain,
497 &netgrdata, buf2, sizeof (buf2),
498 errnop))
499 {
500 if (user != NULL && user[0] != '-')
501 blacklist_store_name (user, ent);
502 }
503 __internal_endnetgrent (&netgrdata);
504 continue;
505 }
506
507 /* +@netgroup */
508 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
509 && result->sp_namp[2] != '\0')
510 {
511 int status;
512
513 ent->netgroup = TRUE;
514 ent->first = TRUE;
515 copy_spwd_changes (&ent->pwd, result, NULL, 0);
516
517 status = getspent_next_nss_netgr (NULL, result, ent,
518 &result->sp_namp[2],
519 buffer, buflen, errnop);
520 if (status == NSS_STATUS_RETURN)
521 continue;
522 else
523 return status;
524 }
525
526 /* -user */
527 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
528 && result->sp_namp[1] != '@')
529 {
530 blacklist_store_name (&result->sp_namp[1], ent);
531 continue;
532 }
533
534 /* +user */
535 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
536 && result->sp_namp[1] != '@')
537 {
538 enum nss_status status;
539
540 /* Store the User in the blacklist for the "+" at the end of
541 /etc/passwd */
542 blacklist_store_name (&result->sp_namp[1], ent);
543 status = getspnam_plususer (&result->sp_namp[1], result, ent,
544 buffer, buflen, errnop);
545
546 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
547 break;
548 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
549 || status == NSS_STATUS_NOTFOUND) /* entry doesn't exist */
550 continue;
551 else
552 {
553 if (status == NSS_STATUS_TRYAGAIN)
554 {
555 fsetpos (ent->stream, &pos);
556 *errnop = ERANGE;
557 }
558 return status;
559 }
560 }
561
562 /* +:... */
563 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
564 {
565 ent->files = FALSE;
566 ent->first = TRUE;
567 copy_spwd_changes (&ent->pwd, result, NULL, 0);
568
569 return getspent_next_nss (result, ent, buffer, buflen, errnop);
570 }
571 }
572
573 return NSS_STATUS_SUCCESS;
574 }
575
576
577 static enum nss_status
578 internal_getspent_r (struct spwd *pw, ent_t *ent,
579 char *buffer, size_t buflen, int *errnop)
580 {
581 if (ent->netgroup)
582 {
583 enum nss_status status;
584
585 /* We are searching members in a netgroup */
586 /* Since this is not the first call, we don't need the group name */
587 status = getspent_next_nss_netgr (NULL, pw, ent, NULL, buffer,
588 buflen, errnop);
589
590 if (status == NSS_STATUS_RETURN)
591 return getspent_next_file (pw, ent, buffer, buflen, errnop);
592 else
593 return status;
594 }
595 else if (ent->files)
596 return getspent_next_file (pw, ent, buffer, buflen, errnop);
597 else
598 return getspent_next_nss (pw, ent, buffer, buflen, errnop);
599 }
600
601 enum nss_status
602 _nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen,
603 int *errnop)
604 {
605 enum nss_status result = NSS_STATUS_SUCCESS;
606
607 __libc_lock_lock (lock);
608
609 /* Be prepared that the setpwent function was not called before. */
610 if (ni == NULL)
611 init_nss_interface ();
612
613 if (ext_ent.stream == NULL)
614 result = internal_setspent (&ext_ent, 1);
615
616 if (result == NSS_STATUS_SUCCESS)
617 result = internal_getspent_r (pwd, &ext_ent, buffer, buflen, errnop);
618
619 __libc_lock_unlock (lock);
620
621 return result;
622 }
623
624 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
625 static enum nss_status
626 internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
627 char *buffer, size_t buflen, int *errnop)
628 {
629 struct parser_data *data = (void *) buffer;
630
631 while (1)
632 {
633 fpos_t pos;
634 char *p;
635 int parse_res;
636
637 do
638 {
639 fgetpos (ent->stream, &pos);
640 buffer[buflen - 1] = '\xff';
641 p = fgets (buffer, buflen, ent->stream);
642 if (p == NULL && feof (ent->stream))
643 return NSS_STATUS_NOTFOUND;
644
645 if (p == NULL || buffer[buflen - 1] != '\xff')
646 {
647 fsetpos (ent->stream, &pos);
648 *errnop = ERANGE;
649 return NSS_STATUS_TRYAGAIN;
650 }
651
652 /* Skip leading blanks. */
653 while (isspace (*p))
654 ++p;
655 }
656 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
657 /* Parse the line. If it is invalid, loop to
658 get the next line of the file to parse. */
659 !(parse_res = _nss_files_parse_spent (p, result, data, buflen,
660 errnop)));
661
662 if (parse_res == -1)
663 {
664 /* The parser ran out of space. */
665 fsetpos (ent->stream, &pos);
666 *errnop = ERANGE;
667 return NSS_STATUS_TRYAGAIN;
668 }
669
670 /* This is a real entry. */
671 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
672 {
673 if (strcmp (result->sp_namp, name) == 0)
674 return NSS_STATUS_SUCCESS;
675 else
676 continue;
677 }
678
679 /* -@netgroup */
680 /* If the loaded NSS module does not support this service, add
681 all users from a +@netgroup entry to the blacklist, too. */
682 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
683 && result->sp_namp[2] != '\0')
684 {
685 /* XXX Do not use fixed length buffers. */
686 char buf2[1024];
687 char *user, *host, *domain;
688 struct __netgrent netgrdata;
689
690 bzero (&netgrdata, sizeof (struct __netgrent));
691 __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
692 while (__internal_getnetgrent_r (&host, &user, &domain, &netgrdata,
693 buf2, sizeof (buf2), errnop))
694 {
695 if (user != NULL && user[0] != '-')
696 if (strcmp (user, name) == 0)
697 return NSS_STATUS_NOTFOUND;
698 }
699 __internal_endnetgrent (&netgrdata);
700 continue;
701 }
702
703 /* +@netgroup */
704 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
705 && result->sp_namp[2] != '\0')
706 {
707 char *buf = strdupa (&result->sp_namp[2]);
708 int status;
709
710 ent->netgroup = TRUE;
711 ent->first = TRUE;
712 copy_spwd_changes (&ent->pwd, result, NULL, 0);
713
714 do
715 {
716 status = getspent_next_nss_netgr (name, result, ent, buf,
717 buffer, buflen, errnop);
718 if (status == NSS_STATUS_RETURN)
719 continue;
720
721 if (status == NSS_STATUS_SUCCESS
722 && strcmp (result->sp_namp, name) == 0)
723 return NSS_STATUS_SUCCESS;
724 }
725 while (status == NSS_STATUS_SUCCESS);
726 continue;
727 }
728
729 /* -user */
730 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
731 && result->sp_namp[1] != '@')
732 {
733 if (strcmp (&result->sp_namp[1], name) == 0)
734 return NSS_STATUS_NOTFOUND;
735 else
736 continue;
737 }
738
739 /* +user */
740 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
741 && result->sp_namp[1] != '@')
742 {
743 if (strcmp (name, &result->sp_namp[1]) == 0)
744 {
745 enum nss_status status;
746
747 status = getspnam_plususer (name, result, ent,
748 buffer, buflen, errnop);
749
750 if (status == NSS_STATUS_RETURN)
751 /* We couldn't parse the entry */
752 return NSS_STATUS_NOTFOUND;
753 else
754 return status;
755 }
756 }
757
758 /* +:... */
759 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
760 {
761 enum nss_status status;
762
763 status = getspnam_plususer (name, result, ent,
764 buffer, buflen, errnop);
765
766 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
767 return NSS_STATUS_NOTFOUND;
768 else
769 return status;
770 }
771 }
772 return NSS_STATUS_SUCCESS;
773 }
774
775 enum nss_status
776 _nss_compat_getspnam_r (const char *name, struct spwd *pwd,
777 char *buffer, size_t buflen, int *errnop)
778 {
779 enum nss_status result;
780 ent_t ent = {0, TRUE, 0, NULL, {NULL, 0, 0},
781 {NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
782
783 if (name[0] == '-' || name[0] == '+')
784 return NSS_STATUS_NOTFOUND;
785
786 __libc_lock_lock (lock);
787
788 if (ni == NULL)
789 init_nss_interface ();
790
791 __libc_lock_unlock (lock);
792
793 result = internal_setspent (&ent, 0);
794
795 if (result != NSS_STATUS_SUCCESS)
796 return result;
797
798 result = internal_getspnam_r (name, pwd, &ent, buffer, buflen, errnop);
799
800 internal_endspent (&ent);
801
802 return result;
803 }
804
805 /* Support routines for remembering -@netgroup and -user entries.
806 The names are stored in a single string with `|' as separator. */
807 static void
808 blacklist_store_name (const char *name, ent_t *ent)
809 {
810 int namelen = strlen (name);
811 char *tmp;
812
813 /* first call, setup cache */
814 if (ent->blacklist.size == 0)
815 {
816 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
817 ent->blacklist.data = malloc (ent->blacklist.size);
818 if (ent->blacklist.data == NULL)
819 return;
820 ent->blacklist.data[0] = '|';
821 ent->blacklist.data[1] = '\0';
822 ent->blacklist.current = 1;
823 }
824 else
825 {
826 if (in_blacklist (name, namelen, ent))
827 return; /* no duplicates */
828
829 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
830 {
831 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
832 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
833 if (tmp == NULL)
834 {
835 free (ent->blacklist.data);
836 ent->blacklist.size = 0;
837 return;
838 }
839 ent->blacklist.data = tmp;
840 }
841 }
842
843 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
844 *tmp++ = '|';
845 *tmp = '\0';
846 ent->blacklist.current += namelen + 1;
847
848 return;
849 }
850
851 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
852 static bool_t
853 in_blacklist (const char *name, int namelen, ent_t *ent)
854 {
855 char buf[namelen + 3];
856 char *cp;
857
858 if (ent->blacklist.data == NULL)
859 return FALSE;
860
861 buf[0] = '|';
862 cp = stpcpy (&buf[1], name);
863 *cp++ = '|';
864 *cp = '\0';
865 return strstr (ent->blacklist.data, buf) != NULL;
866 }