* libio/fileops.c (_IO_new_file_fopen): Recognize 'e' flag and set
[glibc.git] / nis / nss_compat / compat-spwd.c
bloba5977681f25b514c21668ac62a14f6a9f8274ecd
1 /* Copyright (C) 1996-1999, 2001-2006, 2007 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.
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.
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.
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. */
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <netdb.h>
24 #include <nss.h>
25 #include <nsswitch.h>
26 #include <shadow.h>
27 #include <stdio_ext.h>
28 #include <string.h>
29 #include <rpc/types.h>
30 #include <rpcsvc/ypclnt.h>
31 #include <bits/libc-lock.h>
32 #include <kernel-features.h>
34 #include "netgroup.h"
36 static service_user *ni;
37 static enum nss_status (*nss_setspent) (int stayopen);
38 static enum nss_status (*nss_getspnam_r) (const char *name, struct spwd * sp,
39 char *buffer, size_t buflen,
40 int *errnop);
41 static enum nss_status (*nss_getspent_r) (struct spwd * sp, char *buffer,
42 size_t buflen, int *errnop);
43 static enum nss_status (*nss_endspent) (void);
45 /* Get the declaration of the parser function. */
46 #define ENTNAME spent
47 #define STRUCTURE spwd
48 #define EXTERN_PARSER
49 #include <nss/nss_files/files-parse.c>
51 /* Structure for remembering -@netgroup and -user members ... */
52 #define BLACKLIST_INITIAL_SIZE 512
53 #define BLACKLIST_INCREMENT 256
54 struct blacklist_t
56 char *data;
57 int current;
58 int size;
61 struct ent_t
63 bool netgroup;
64 bool files;
65 bool first;
66 enum nss_status setent_status;
67 FILE *stream;
68 struct blacklist_t blacklist;
69 struct spwd pwd;
70 struct __netgrent netgrdata;
72 typedef struct ent_t ent_t;
74 static ent_t ext_ent = { false, true, false, NSS_STATUS_SUCCESS, NULL,
75 { NULL, 0, 0},
76 { NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
78 /* Protect global state against multiple changers. */
79 __libc_lock_define_initialized (static, lock)
81 /* Positive if O_CLOEXEC is supported, negative if it is not supported,
82 zero if it is still undecided. This variable is shared with the
83 other compat functions. */
84 #ifdef __ASSUME_O_CLOEXEC
85 # define __compat_have_cloexec 1
86 #else
87 # ifdef O_CLOEXEC
88 extern int __compat_have_cloexec;
89 # else
90 # define __compat_have_cloexec -1
91 # endif
92 #endif
94 /* Prototypes for local functions. */
95 static void blacklist_store_name (const char *, ent_t *);
96 static int in_blacklist (const char *, int, ent_t *);
98 /* Initialize the NSS interface/functions. The calling function must
99 hold the lock. */
100 static void
101 init_nss_interface (void)
103 if (__nss_database_lookup ("shadow_compat", "passwd_compat",
104 "nis", &ni) >= 0)
106 nss_setspent = __nss_lookup_function (ni, "setspent");
107 nss_getspnam_r = __nss_lookup_function (ni, "getspnam_r");
108 nss_getspent_r = __nss_lookup_function (ni, "getspent_r");
109 nss_endspent = __nss_lookup_function (ni, "endspent");
113 static void
114 give_spwd_free (struct spwd *pwd)
116 if (pwd->sp_namp != NULL)
117 free (pwd->sp_namp);
118 if (pwd->sp_pwdp != NULL)
119 free (pwd->sp_pwdp);
121 memset (pwd, '\0', sizeof (struct spwd));
122 pwd->sp_warn = -1;
123 pwd->sp_inact = -1;
124 pwd->sp_expire = -1;
125 pwd->sp_flag = ~0ul;
128 static int
129 spwd_need_buflen (struct spwd *pwd)
131 int len = 0;
133 if (pwd->sp_pwdp != NULL)
134 len += strlen (pwd->sp_pwdp) + 1;
136 return len;
139 static void
140 copy_spwd_changes (struct spwd *dest, struct spwd *src,
141 char *buffer, size_t buflen)
143 if (src->sp_pwdp != NULL && strlen (src->sp_pwdp))
145 if (buffer == NULL)
146 dest->sp_pwdp = strdup (src->sp_pwdp);
147 else if (dest->sp_pwdp &&
148 strlen (dest->sp_pwdp) >= strlen (src->sp_pwdp))
149 strcpy (dest->sp_pwdp, src->sp_pwdp);
150 else
152 dest->sp_pwdp = buffer;
153 strcpy (dest->sp_pwdp, src->sp_pwdp);
154 buffer += strlen (dest->sp_pwdp) + 1;
155 buflen = buflen - (strlen (dest->sp_pwdp) + 1);
158 if (src->sp_lstchg != 0)
159 dest->sp_lstchg = src->sp_lstchg;
160 if (src->sp_min != 0)
161 dest->sp_min = src->sp_min;
162 if (src->sp_max != 0)
163 dest->sp_max = src->sp_max;
164 if (src->sp_warn != -1)
165 dest->sp_warn = src->sp_warn;
166 if (src->sp_inact != -1)
167 dest->sp_inact = src->sp_inact;
168 if (src->sp_expire != -1)
169 dest->sp_expire = src->sp_expire;
170 if (src->sp_flag != ~0ul)
171 dest->sp_flag = src->sp_flag;
174 static enum nss_status
175 internal_setspent (ent_t *ent, int stayopen)
177 enum nss_status status = NSS_STATUS_SUCCESS;
179 ent->first = ent->netgroup = 0;
180 ent->files = true;
182 /* If something was left over free it. */
183 if (ent->netgroup)
184 __internal_endnetgrent (&ent->netgrdata);
186 if (ent->blacklist.data != NULL)
188 ent->blacklist.current = 1;
189 ent->blacklist.data[0] = '|';
190 ent->blacklist.data[1] = '\0';
192 else
193 ent->blacklist.current = 0;
195 if (ent->stream == NULL)
197 ent->stream = fopen ("/etc/shadow", "rme");
199 if (ent->stream == NULL)
200 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
201 else
203 /* We have to make sure the file is `closed on exec'. */
204 int result = 0;
206 if (__compat_have_cloexec <= 0)
208 int flags;
209 result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD,
211 if (result >= 0)
213 #if defined O_CLOEXEC && !defined __ASSUME_O_CLOEXEC
214 if (__compat_have_cloexec == 0)
215 __compat_have_cloexec = (flags & FD_CLOEXEC) ? 1 : -1;
217 if (__compat_have_cloexec < 0)
218 #endif
220 flags |= FD_CLOEXEC;
221 result = fcntl (fileno_unlocked (ent->stream), F_SETFD,
222 flags);
227 if (result < 0)
229 /* Something went wrong. Close the stream and return a
230 failure. */
231 fclose (ent->stream);
232 ent->stream = NULL;
233 status = NSS_STATUS_UNAVAIL;
235 else
236 /* We take care of locking ourself. */
237 __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
240 else
241 rewind (ent->stream);
243 give_spwd_free (&ent->pwd);
245 if (status == NSS_STATUS_SUCCESS && nss_setspent)
246 ent->setent_status = nss_setspent (stayopen);
248 return status;
252 enum nss_status
253 _nss_compat_setspent (int stayopen)
255 enum nss_status result;
257 __libc_lock_lock (lock);
259 if (ni == NULL)
260 init_nss_interface ();
262 result = internal_setspent (&ext_ent, stayopen);
264 __libc_lock_unlock (lock);
266 return result;
270 static enum nss_status
271 internal_endspent (ent_t *ent)
273 if (nss_endspent)
274 nss_endspent ();
276 if (ent->stream != NULL)
278 fclose (ent->stream);
279 ent->stream = NULL;
282 if (ent->netgroup)
283 __internal_endnetgrent (&ent->netgrdata);
285 ent->first = ent->netgroup = false;
286 ent->files = true;
288 if (ent->blacklist.data != NULL)
290 ent->blacklist.current = 1;
291 ent->blacklist.data[0] = '|';
292 ent->blacklist.data[1] = '\0';
294 else
295 ent->blacklist.current = 0;
297 give_spwd_free (&ent->pwd);
299 return NSS_STATUS_SUCCESS;
302 enum nss_status
303 _nss_compat_endspent (void)
305 enum nss_status result;
307 __libc_lock_lock (lock);
309 result = internal_endspent (&ext_ent);
311 __libc_lock_unlock (lock);
313 return result;
317 static enum nss_status
318 getspent_next_nss_netgr (const char *name, struct spwd *result, ent_t *ent,
319 char *group, char *buffer, size_t buflen,
320 int *errnop)
322 char *curdomain, *host, *user, *domain, *p2;
323 size_t p2len;
325 if (!nss_getspnam_r)
326 return NSS_STATUS_UNAVAIL;
328 /* If the setpwent call failed, say so. */
329 if (ent->setent_status != NSS_STATUS_SUCCESS)
330 return ent->setent_status;
332 if (yp_get_default_domain (&curdomain) != YPERR_SUCCESS)
334 ent->netgroup = false;
335 ent->first = false;
336 give_spwd_free (&ent->pwd);
337 return NSS_STATUS_UNAVAIL;
340 if (ent->first == true)
342 memset (&ent->netgrdata, 0, sizeof (struct __netgrent));
343 __internal_setnetgrent (group, &ent->netgrdata);
344 ent->first = false;
347 while (1)
349 char *saved_cursor;
350 enum nss_status status;
352 saved_cursor = ent->netgrdata.cursor;
353 status = __internal_getnetgrent_r (&host, &user, &domain,
354 &ent->netgrdata, buffer, buflen,
355 errnop);
356 if (status != 1)
358 __internal_endnetgrent (&ent->netgrdata);
359 ent->netgroup = false;
360 give_spwd_free (&ent->pwd);
361 return NSS_STATUS_RETURN;
364 if (user == NULL || user[0] == '-')
365 continue;
367 if (domain != NULL && strcmp (curdomain, domain) != 0)
368 continue;
370 /* If name != NULL, we are called from getpwnam */
371 if (name != NULL)
372 if (strcmp (user, name) != 0)
373 continue;
375 p2len = spwd_need_buflen (&ent->pwd);
376 if (p2len > buflen)
378 *errnop = ERANGE;
379 return NSS_STATUS_TRYAGAIN;
381 p2 = buffer + (buflen - p2len);
382 buflen -= p2len;
384 if (nss_getspnam_r (user, result, buffer, buflen, errnop) !=
385 NSS_STATUS_SUCCESS)
386 continue;
388 if (!in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
390 /* Store the User in the blacklist for possible the "+" at the
391 end of /etc/passwd */
392 blacklist_store_name (result->sp_namp, ent);
393 copy_spwd_changes (result, &ent->pwd, p2, p2len);
394 break;
398 return NSS_STATUS_SUCCESS;
402 static enum nss_status
403 getspent_next_nss (struct spwd *result, ent_t *ent,
404 char *buffer, size_t buflen, int *errnop)
406 enum nss_status status;
407 char *p2;
408 size_t p2len;
410 if (!nss_getspent_r)
411 return NSS_STATUS_UNAVAIL;
413 p2len = spwd_need_buflen (&ent->pwd);
414 if (p2len > buflen)
416 *errnop = ERANGE;
417 return NSS_STATUS_TRYAGAIN;
419 p2 = buffer + (buflen - p2len);
420 buflen -= p2len;
423 if ((status = nss_getspent_r (result, buffer, buflen, errnop)) !=
424 NSS_STATUS_SUCCESS)
425 return status;
427 while (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent));
429 copy_spwd_changes (result, &ent->pwd, p2, p2len);
431 return NSS_STATUS_SUCCESS;
435 /* This function handle the +user entrys in /etc/shadow */
436 static enum nss_status
437 getspnam_plususer (const char *name, struct spwd *result, ent_t *ent,
438 char *buffer, size_t buflen, int *errnop)
440 if (!nss_getspnam_r)
441 return NSS_STATUS_UNAVAIL;
443 struct spwd pwd;
444 memset (&pwd, '\0', sizeof (struct spwd));
445 pwd.sp_warn = -1;
446 pwd.sp_inact = -1;
447 pwd.sp_expire = -1;
448 pwd.sp_flag = ~0ul;
450 copy_spwd_changes (&pwd, result, NULL, 0);
452 size_t plen = spwd_need_buflen (&pwd);
453 if (plen > buflen)
455 *errnop = ERANGE;
456 return NSS_STATUS_TRYAGAIN;
458 char *p = buffer + (buflen - plen);
459 buflen -= plen;
461 enum nss_status status = nss_getspnam_r (name, result, buffer, buflen,
462 errnop);
463 if (status != NSS_STATUS_SUCCESS)
464 return status;
466 if (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
467 return NSS_STATUS_NOTFOUND;
469 copy_spwd_changes (result, &pwd, p, plen);
470 give_spwd_free (&pwd);
471 /* We found the entry. */
472 return NSS_STATUS_SUCCESS;
476 static enum nss_status
477 getspent_next_file (struct spwd *result, ent_t *ent,
478 char *buffer, size_t buflen, int *errnop)
480 struct parser_data *data = (void *) buffer;
481 while (1)
483 fpos_t pos;
484 int parse_res = 0;
485 char *p;
489 /* We need at least 3 characters for one line. */
490 if (__builtin_expect (buflen < 3, 0))
492 erange:
493 *errnop = ERANGE;
494 return NSS_STATUS_TRYAGAIN;
497 fgetpos (ent->stream, &pos);
498 buffer[buflen - 1] = '\xff';
499 p = fgets_unlocked (buffer, buflen, ent->stream);
500 if (p == NULL && feof_unlocked (ent->stream))
501 return NSS_STATUS_NOTFOUND;
503 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
505 erange_reset:
506 fsetpos (ent->stream, &pos);
507 goto erange;
510 /* Skip leading blanks. */
511 while (isspace (*p))
512 ++p;
514 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
515 /* Parse the line. If it is invalid, loop to
516 get the next line of the file to parse. */
517 || !(parse_res = _nss_files_parse_spent (p, result, data,
518 buflen, errnop)));
520 if (__builtin_expect (parse_res == -1, 0))
521 /* The parser ran out of space. */
522 goto erange_reset;
524 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
525 /* This is a real entry. */
526 break;
528 /* -@netgroup */
529 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
530 && result->sp_namp[2] != '\0')
532 /* XXX Do not use fixed length buffers. */
533 char buf2[1024];
534 char *user, *host, *domain;
535 struct __netgrent netgrdata;
537 bzero (&netgrdata, sizeof (struct __netgrent));
538 __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
539 while (__internal_getnetgrent_r (&host, &user, &domain,
540 &netgrdata, buf2, sizeof (buf2),
541 errnop))
543 if (user != NULL && user[0] != '-')
544 blacklist_store_name (user, ent);
546 __internal_endnetgrent (&netgrdata);
547 continue;
550 /* +@netgroup */
551 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
552 && result->sp_namp[2] != '\0')
554 int status;
556 ent->netgroup = true;
557 ent->first = true;
558 copy_spwd_changes (&ent->pwd, result, NULL, 0);
560 status = getspent_next_nss_netgr (NULL, result, ent,
561 &result->sp_namp[2],
562 buffer, buflen, errnop);
563 if (status == NSS_STATUS_RETURN)
564 continue;
565 else
566 return status;
569 /* -user */
570 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
571 && result->sp_namp[1] != '@')
573 blacklist_store_name (&result->sp_namp[1], ent);
574 continue;
577 /* +user */
578 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
579 && result->sp_namp[1] != '@')
581 size_t len = strlen (result->sp_namp);
582 char buf[len];
583 enum nss_status status;
585 /* Store the User in the blacklist for the "+" at the end of
586 /etc/passwd */
587 memcpy (buf, &result->sp_namp[1], len);
588 status = getspnam_plususer (&result->sp_namp[1], result, ent,
589 buffer, buflen, errnop);
590 blacklist_store_name (buf, ent);
592 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
593 break;
594 /* We couldn't parse the entry */
595 else if (status == NSS_STATUS_RETURN
596 /* entry doesn't exist */
597 || status == NSS_STATUS_NOTFOUND)
598 continue;
599 else
601 if (status == NSS_STATUS_TRYAGAIN)
603 fsetpos (ent->stream, &pos);
604 *errnop = ERANGE;
606 return status;
610 /* +:... */
611 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
613 ent->files = false;
614 ent->first = true;
615 copy_spwd_changes (&ent->pwd, result, NULL, 0);
617 return getspent_next_nss (result, ent, buffer, buflen, errnop);
621 return NSS_STATUS_SUCCESS;
625 static enum nss_status
626 internal_getspent_r (struct spwd *pw, ent_t *ent,
627 char *buffer, size_t buflen, int *errnop)
629 if (ent->netgroup)
631 enum nss_status status;
633 /* We are searching members in a netgroup */
634 /* Since this is not the first call, we don't need the group name */
635 status = getspent_next_nss_netgr (NULL, pw, ent, NULL, buffer,
636 buflen, errnop);
638 if (status == NSS_STATUS_RETURN)
639 return getspent_next_file (pw, ent, buffer, buflen, errnop);
640 else
641 return status;
643 else if (ent->files)
644 return getspent_next_file (pw, ent, buffer, buflen, errnop);
645 else
646 return getspent_next_nss (pw, ent, buffer, buflen, errnop);
650 enum nss_status
651 _nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen,
652 int *errnop)
654 enum nss_status result = NSS_STATUS_SUCCESS;
656 __libc_lock_lock (lock);
658 /* Be prepared that the setpwent function was not called before. */
659 if (ni == NULL)
660 init_nss_interface ();
662 if (ext_ent.stream == NULL)
663 result = internal_setspent (&ext_ent, 1);
665 if (result == NSS_STATUS_SUCCESS)
666 result = internal_getspent_r (pwd, &ext_ent, buffer, buflen, errnop);
668 __libc_lock_unlock (lock);
670 return result;
674 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
675 static enum nss_status
676 internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
677 char *buffer, size_t buflen, int *errnop)
679 struct parser_data *data = (void *) buffer;
681 while (1)
683 fpos_t pos;
684 char *p;
685 int parse_res;
689 /* We need at least 3 characters for one line. */
690 if (__builtin_expect (buflen < 3, 0))
692 erange:
693 *errnop = ERANGE;
694 return NSS_STATUS_TRYAGAIN;
697 fgetpos (ent->stream, &pos);
698 buffer[buflen - 1] = '\xff';
699 p = fgets_unlocked (buffer, buflen, ent->stream);
700 if (p == NULL && feof_unlocked (ent->stream))
701 return NSS_STATUS_NOTFOUND;
703 if (p == NULL || buffer[buflen - 1] != '\xff')
705 erange_reset:
706 fsetpos (ent->stream, &pos);
707 goto erange;
710 /* Terminate the line for any case. */
711 buffer[buflen - 1] = '\0';
713 /* Skip leading blanks. */
714 while (isspace (*p))
715 ++p;
717 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
718 /* Parse the line. If it is invalid, loop to
719 get the next line of the file to parse. */
720 !(parse_res = _nss_files_parse_spent (p, result, data, buflen,
721 errnop)));
723 if (__builtin_expect (parse_res == -1, 0))
724 /* The parser ran out of space. */
725 goto erange_reset;
727 /* This is a real entry. */
728 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
730 if (strcmp (result->sp_namp, name) == 0)
731 return NSS_STATUS_SUCCESS;
732 else
733 continue;
736 /* -@netgroup */
737 /* If the loaded NSS module does not support this service, add
738 all users from a +@netgroup entry to the blacklist, too. */
739 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
740 && result->sp_namp[2] != '\0')
742 if (innetgr (&result->sp_namp[2], NULL, name, NULL))
743 return NSS_STATUS_NOTFOUND;
744 continue;
747 /* +@netgroup */
748 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
749 && result->sp_namp[2] != '\0')
751 enum nss_status status;
753 if (innetgr (&result->sp_namp[2], NULL, name, NULL))
755 status = getspnam_plususer (name, result, ent, buffer,
756 buflen, errnop);
758 if (status == NSS_STATUS_RETURN)
759 continue;
761 return status;
763 continue;
766 /* -user */
767 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
768 && result->sp_namp[1] != '@')
770 if (strcmp (&result->sp_namp[1], name) == 0)
771 return NSS_STATUS_NOTFOUND;
772 else
773 continue;
776 /* +user */
777 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
778 && result->sp_namp[1] != '@')
780 if (strcmp (name, &result->sp_namp[1]) == 0)
782 enum nss_status status;
784 status = getspnam_plususer (name, result, ent,
785 buffer, buflen, errnop);
787 if (status == NSS_STATUS_RETURN)
788 /* We couldn't parse the entry */
789 return NSS_STATUS_NOTFOUND;
790 else
791 return status;
795 /* +:... */
796 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
798 enum nss_status status;
800 status = getspnam_plususer (name, result, ent,
801 buffer, buflen, errnop);
803 if (status == NSS_STATUS_SUCCESS)
804 /* We found the entry. */
805 break;
806 else if (status == NSS_STATUS_RETURN)
807 /* We couldn't parse the entry */
808 return NSS_STATUS_NOTFOUND;
809 else
810 return status;
813 return NSS_STATUS_SUCCESS;
817 enum nss_status
818 _nss_compat_getspnam_r (const char *name, struct spwd *pwd,
819 char *buffer, size_t buflen, int *errnop)
821 enum nss_status result;
822 ent_t ent = { false, true, false, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0},
823 { NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
825 if (name[0] == '-' || name[0] == '+')
826 return NSS_STATUS_NOTFOUND;
828 __libc_lock_lock (lock);
830 if (ni == NULL)
831 init_nss_interface ();
833 __libc_lock_unlock (lock);
835 result = internal_setspent (&ent, 0);
837 if (result == NSS_STATUS_SUCCESS)
838 result = internal_getspnam_r (name, pwd, &ent, buffer, buflen, errnop);
840 internal_endspent (&ent);
842 return result;
846 /* Support routines for remembering -@netgroup and -user entries.
847 The names are stored in a single string with `|' as separator. */
848 static void
849 blacklist_store_name (const char *name, ent_t *ent)
851 int namelen = strlen (name);
852 char *tmp;
854 /* first call, setup cache */
855 if (ent->blacklist.size == 0)
857 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
858 ent->blacklist.data = malloc (ent->blacklist.size);
859 if (ent->blacklist.data == NULL)
860 return;
861 ent->blacklist.data[0] = '|';
862 ent->blacklist.data[1] = '\0';
863 ent->blacklist.current = 1;
865 else
867 if (in_blacklist (name, namelen, ent))
868 return; /* no duplicates */
870 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
872 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
873 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
874 if (tmp == NULL)
876 free (ent->blacklist.data);
877 ent->blacklist.size = 0;
878 return;
880 ent->blacklist.data = tmp;
884 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
885 *tmp++ = '|';
886 *tmp = '\0';
887 ent->blacklist.current += namelen + 1;
889 return;
893 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
894 static bool_t
895 in_blacklist (const char *name, int namelen, ent_t *ent)
897 char buf[namelen + 3];
898 char *cp;
900 if (ent->blacklist.data == NULL)
901 return false;
903 buf[0] = '|';
904 cp = stpcpy (&buf[1], name);
905 *cp++ = '|';
906 *cp = '\0';
907 return strstr (ent->blacklist.data, buf) != NULL;