1 /* Copyright (C) 1996-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.
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
27 #include <stdio_ext.h>
29 #include <rpc/types.h>
30 #include <rpcsvc/ypclnt.h>
31 #include <bits/libc-lock.h>
35 static service_user *ni;
36 static enum nss_status (*nss_setpwent) (int stayopen);
37 static enum nss_status (*nss_getpwnam_r) (const char *name,
38 struct passwd * pwd, char *buffer,
39 size_t buflen, int *errnop);
40 static enum nss_status (*nss_getpwuid_r) (uid_t uid, struct passwd * pwd,
41 char *buffer, size_t buflen,
43 static enum nss_status (*nss_getpwent_r) (struct passwd * pwd, char *buffer,
44 size_t buflen, int *errnop);
45 static enum nss_status (*nss_endpwent) (void);
47 /* Get the declaration of the parser function. */
49 #define STRUCTURE passwd
51 #include <nss/nss_files/files-parse.c>
53 /* Structure for remembering -@netgroup and -user members ... */
54 #define BLACKLIST_INITIAL_SIZE 512
55 #define BLACKLIST_INCREMENT 256
69 struct blacklist_t blacklist;
71 struct __netgrent netgrdata;
73 typedef struct ent_t ent_t;
75 static ent_t ext_ent = {0, 0, TRUE, NULL, {NULL, 0, 0},
76 {NULL, NULL, 0, 0, NULL, NULL, NULL}};
78 /* Protect global state against multiple changers. */
79 __libc_lock_define_initialized (static, lock)
81 /* Prototypes for local functions. */
82 static void blacklist_store_name (const char *, ent_t *);
83 static int in_blacklist (const char *, int, ent_t *);
85 /* Initialize the NSS interface/functions. The calling function must
88 init_nss_interface (void)
90 if (__nss_database_lookup ("passwd_compat", NULL, "nis", &ni) >= 0)
92 nss_setpwent = __nss_lookup_function (ni, "setpwent");
93 nss_getpwnam_r = __nss_lookup_function (ni, "getpwnam_r");
94 nss_getpwuid_r = __nss_lookup_function (ni, "getpwuid_r");
95 nss_getpwent_r = __nss_lookup_function (ni, "getpwent_r");
96 nss_endpwent = __nss_lookup_function (ni, "endpwent");
101 give_pwd_free (struct passwd *pwd)
103 if (pwd->pw_name != NULL)
105 if (pwd->pw_passwd != NULL)
106 free (pwd->pw_passwd);
107 if (pwd->pw_gecos != NULL)
108 free (pwd->pw_gecos);
109 if (pwd->pw_dir != NULL)
111 if (pwd->pw_shell != NULL)
112 free (pwd->pw_shell);
114 memset (pwd, '\0', sizeof (struct passwd));
118 pwd_need_buflen (struct passwd *pwd)
122 if (pwd->pw_passwd != NULL)
123 len += strlen (pwd->pw_passwd) + 1;
125 if (pwd->pw_gecos != NULL)
126 len += strlen (pwd->pw_gecos) + 1;
128 if (pwd->pw_dir != NULL)
129 len += strlen (pwd->pw_dir) + 1;
131 if (pwd->pw_shell != NULL)
132 len += strlen (pwd->pw_shell) + 1;
138 copy_pwd_changes (struct passwd *dest, struct passwd *src,
139 char *buffer, size_t buflen)
141 if (src->pw_passwd != NULL && strlen (src->pw_passwd))
144 dest->pw_passwd = strdup (src->pw_passwd);
145 else if (dest->pw_passwd &&
146 strlen (dest->pw_passwd) >= strlen (src->pw_passwd))
147 strcpy (dest->pw_passwd, src->pw_passwd);
150 dest->pw_passwd = buffer;
151 strcpy (dest->pw_passwd, src->pw_passwd);
152 buffer += strlen (dest->pw_passwd) + 1;
153 buflen = buflen - (strlen (dest->pw_passwd) + 1);
157 if (src->pw_gecos != NULL && strlen (src->pw_gecos))
160 dest->pw_gecos = strdup (src->pw_gecos);
161 else if (dest->pw_gecos &&
162 strlen (dest->pw_gecos) >= strlen (src->pw_gecos))
163 strcpy (dest->pw_gecos, src->pw_gecos);
166 dest->pw_gecos = buffer;
167 strcpy (dest->pw_gecos, src->pw_gecos);
168 buffer += strlen (dest->pw_gecos) + 1;
169 buflen = buflen - (strlen (dest->pw_gecos) + 1);
172 if (src->pw_dir != NULL && strlen (src->pw_dir))
175 dest->pw_dir = strdup (src->pw_dir);
176 else if (dest->pw_dir && strlen (dest->pw_dir) >= strlen (src->pw_dir))
177 strcpy (dest->pw_dir, src->pw_dir);
180 dest->pw_dir = buffer;
181 strcpy (dest->pw_dir, src->pw_dir);
182 buffer += strlen (dest->pw_dir) + 1;
183 buflen = buflen - (strlen (dest->pw_dir) + 1);
187 if (src->pw_shell != NULL && strlen (src->pw_shell))
190 dest->pw_shell = strdup (src->pw_shell);
191 else if (dest->pw_shell &&
192 strlen (dest->pw_shell) >= strlen (src->pw_shell))
193 strcpy (dest->pw_shell, src->pw_shell);
196 dest->pw_shell = buffer;
197 strcpy (dest->pw_shell, src->pw_shell);
198 buffer += strlen (dest->pw_shell) + 1;
199 buflen = buflen - (strlen (dest->pw_shell) + 1);
204 static enum nss_status
205 internal_setpwent (ent_t *ent, int stayopen)
207 enum nss_status status = NSS_STATUS_SUCCESS;
209 ent->first = ent->netgroup = FALSE;
212 /* If something was left over free it. */
214 __internal_endnetgrent (&ent->netgrdata);
216 if (ent->blacklist.data != NULL)
218 ent->blacklist.current = 1;
219 ent->blacklist.data[0] = '|';
220 ent->blacklist.data[1] = '\0';
223 ent->blacklist.current = 0;
225 if (ent->stream == NULL)
227 ent->stream = fopen ("/etc/passwd", "rm");
229 if (ent->stream == NULL)
230 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
233 /* We have to make sure the file is `closed on exec'. */
236 result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD, 0);
240 result = fcntl (fileno_unlocked (ent->stream), F_SETFD, flags);
244 /* Something went wrong. Close the stream and return a
246 fclose (ent->stream);
248 status = NSS_STATUS_UNAVAIL;
251 /* We take care of locking ourself. */
252 __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
256 rewind (ent->stream);
258 give_pwd_free (&ent->pwd);
260 if (status == NSS_STATUS_SUCCESS && nss_setpwent)
261 return nss_setpwent (stayopen);
268 _nss_compat_setpwent (int stayopen)
270 enum nss_status result;
272 __libc_lock_lock (lock);
275 init_nss_interface ();
277 result = internal_setpwent (&ext_ent, stayopen);
279 __libc_lock_unlock (lock);
285 static enum nss_status
286 internal_endpwent (ent_t *ent)
291 if (ent->stream != NULL)
293 fclose (ent->stream);
298 __internal_endnetgrent (&ent->netgrdata);
300 ent->first = ent->netgroup = FALSE;
302 if (ent->blacklist.data != NULL)
304 ent->blacklist.current = 1;
305 ent->blacklist.data[0] = '|';
306 ent->blacklist.data[1] = '\0';
309 ent->blacklist.current = 0;
311 give_pwd_free (&ent->pwd);
313 return NSS_STATUS_SUCCESS;
317 _nss_compat_endpwent (void)
319 enum nss_status result;
321 __libc_lock_lock (lock);
323 result = internal_endpwent (&ext_ent);
325 __libc_lock_unlock (lock);
331 static enum nss_status
332 getpwent_next_nss_netgr (const char *name, struct passwd *result, ent_t *ent,
333 char *group, char *buffer, size_t buflen,
336 char *curdomain, *host, *user, *domain, *p2;
340 /* Leave function if NSS module does not support getpwnam_r,
341 we need this function here. */
343 return NSS_STATUS_UNAVAIL;
345 if (yp_get_default_domain (&curdomain) != YPERR_SUCCESS)
347 ent->netgroup = FALSE;
349 give_pwd_free (&ent->pwd);
350 return NSS_STATUS_UNAVAIL;
353 if (ent->first == TRUE)
355 memset (&ent->netgrdata, 0, sizeof (struct __netgrent));
356 __internal_setnetgrent (group, &ent->netgrdata);
364 saved_cursor = ent->netgrdata.cursor;
365 status = __internal_getnetgrent_r (&host, &user, &domain,
366 &ent->netgrdata, buffer, buflen,
370 __internal_endnetgrent (&ent->netgrdata);
372 give_pwd_free (&ent->pwd);
373 return NSS_STATUS_RETURN;
376 if (user == NULL || user[0] == '-')
379 if (domain != NULL && strcmp (curdomain, domain) != 0)
382 /* If name != NULL, we are called from getpwnam. */
384 if (strcmp (user, name) != 0)
387 p2len = pwd_need_buflen (&ent->pwd);
391 return NSS_STATUS_TRYAGAIN;
393 p2 = buffer + (buflen - p2len);
396 if (nss_getpwnam_r (user, result, buffer, buflen, errnop) !=
400 if (!in_blacklist (result->pw_name, strlen (result->pw_name), ent))
402 /* Store the User in the blacklist for possible the "+" at the
403 end of /etc/passwd */
404 blacklist_store_name (result->pw_name, ent);
405 copy_pwd_changes (result, &ent->pwd, p2, p2len);
410 return NSS_STATUS_SUCCESS;
413 /* get the next user from NSS (+ entry) */
414 static enum nss_status
415 getpwent_next_nss (struct passwd *result, ent_t *ent, char *buffer,
416 size_t buflen, int *errnop)
418 enum nss_status status;
422 /* Return if NSS module does not support getpwent_r. */
424 return NSS_STATUS_UNAVAIL;
426 p2len = pwd_need_buflen (&ent->pwd);
430 return NSS_STATUS_TRYAGAIN;
432 p2 = buffer + (buflen - p2len);
440 if ((status = nss_getpwent_r (result, buffer, buflen, errnop)) !=
444 while (in_blacklist (result->pw_name, strlen (result->pw_name), ent));
446 copy_pwd_changes (result, &ent->pwd, p2, p2len);
448 return NSS_STATUS_SUCCESS;
451 /* This function handle the +user entrys in /etc/passwd */
452 static enum nss_status
453 getpwnam_plususer (const char *name, struct passwd *result, ent_t *ent,
454 char *buffer, size_t buflen, int *errnop)
461 return NSS_STATUS_UNAVAIL;
463 memset (&pwd, '\0', sizeof (struct passwd));
465 copy_pwd_changes (&pwd, result, NULL, 0);
467 plen = pwd_need_buflen (&pwd);
471 return NSS_STATUS_TRYAGAIN;
473 p = buffer + (buflen - plen);
476 if (nss_getpwnam_r (name, result, buffer, buflen, errnop) !=
478 return NSS_STATUS_NOTFOUND;
480 if (in_blacklist (result->pw_name, strlen (result->pw_name), ent))
481 return NSS_STATUS_NOTFOUND;
483 copy_pwd_changes (result, &pwd, p, plen);
484 give_pwd_free (&pwd);
485 /* We found the entry. */
486 return NSS_STATUS_SUCCESS;
489 static enum nss_status
490 getpwent_next_file (struct passwd *result, ent_t *ent,
491 char *buffer, size_t buflen, int *errnop)
493 struct parser_data *data = (void *) buffer;
502 fgetpos (ent->stream, &pos);
503 buffer[buflen - 1] = '\xff';
504 p = fgets_unlocked (buffer, buflen, ent->stream);
505 if (p == NULL && feof_unlocked (ent->stream))
506 return NSS_STATUS_NOTFOUND;
508 if (p == NULL || buffer[buflen - 1] != '\xff')
510 fsetpos (ent->stream, &pos);
512 return NSS_STATUS_TRYAGAIN;
515 /* Terminate the line for any case. */
516 buffer[buflen - 1] = '\0';
518 /* Skip leading blanks. */
522 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
523 /* Parse the line. If it is invalid, loop to
524 get the next line of the file to parse. */
525 !(parse_res = _nss_files_parse_pwent (p, result, data, buflen,
530 /* The parser ran out of space. */
531 fsetpos (ent->stream, &pos);
533 return NSS_STATUS_TRYAGAIN;
536 if (result->pw_name[0] != '+' && result->pw_name[0] != '-')
537 /* This is a real entry. */
541 if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
542 && result->pw_name[2] != '\0')
544 /* XXX Do not use fixed length buffer. */
546 char *user, *host, *domain;
547 struct __netgrent netgrdata;
549 bzero (&netgrdata, sizeof (struct __netgrent));
550 __internal_setnetgrent (&result->pw_name[2], &netgrdata);
551 while (__internal_getnetgrent_r (&host, &user, &domain, &netgrdata,
552 buf2, sizeof (buf2), errnop))
554 if (user != NULL && user[0] != '-')
555 blacklist_store_name (user, ent);
557 __internal_endnetgrent (&netgrdata);
562 if (result->pw_name[0] == '+' && result->pw_name[1] == '@'
563 && result->pw_name[2] != '\0')
565 enum nss_status status;
567 ent->netgroup = TRUE;
569 copy_pwd_changes (&ent->pwd, result, NULL, 0);
571 status = getpwent_next_nss_netgr (NULL, result, ent,
573 buffer, buflen, errnop);
574 if (status == NSS_STATUS_RETURN)
581 if (result->pw_name[0] == '-' && result->pw_name[1] != '\0'
582 && result->pw_name[1] != '@')
584 blacklist_store_name (&result->pw_name[1], ent);
589 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
590 && result->pw_name[1] != '@')
592 size_t len = strlen (result->pw_name);
594 enum nss_status status;
596 /* Store the User in the blacklist for the "+" at the end of
598 memcpy (buf, &result->pw_name[1], len);
599 status = getpwnam_plususer (&result->pw_name[1], result, ent,
600 buffer, buflen, errnop);
601 blacklist_store_name (buf, ent);
603 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
605 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
606 || status == NSS_STATUS_NOTFOUND) /* entry doesn't exist */
610 if (status == NSS_STATUS_TRYAGAIN)
612 /* The parser ran out of space */
613 fsetpos (ent->stream, &pos);
621 if (result->pw_name[0] == '+' && result->pw_name[1] == '\0')
625 copy_pwd_changes (&ent->pwd, result, NULL, 0);
627 return getpwent_next_nss (result, ent, buffer, buflen, errnop);
631 return NSS_STATUS_SUCCESS;
635 static enum nss_status
636 internal_getpwent_r (struct passwd *pw, ent_t *ent, char *buffer,
637 size_t buflen, int *errnop)
641 enum nss_status status;
643 /* We are searching members in a netgroup */
644 /* Since this is not the first call, we don't need the group name */
645 status = getpwent_next_nss_netgr (NULL, pw, ent, NULL, buffer, buflen,
647 if (status == NSS_STATUS_RETURN)
648 return getpwent_next_file (pw, ent, buffer, buflen, errnop);
653 return getpwent_next_file (pw, ent, buffer, buflen, errnop);
655 return getpwent_next_nss (pw, ent, buffer, buflen, errnop);
660 _nss_compat_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
663 enum nss_status result = NSS_STATUS_SUCCESS;
665 __libc_lock_lock (lock);
667 /* Be prepared that the setpwent function was not called before. */
669 init_nss_interface ();
671 if (ext_ent.stream == NULL)
672 result = internal_setpwent (&ext_ent, 1);
674 if (result == NSS_STATUS_SUCCESS)
675 result = internal_getpwent_r (pwd, &ext_ent, buffer, buflen, errnop);
677 __libc_lock_unlock (lock);
682 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
683 static enum nss_status
684 internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
685 char *buffer, size_t buflen, int *errnop)
687 struct parser_data *data = (void *) buffer;
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))
702 return NSS_STATUS_NOTFOUND;
704 if (p == NULL || buffer[buflen - 1] != '\xff')
706 fsetpos (ent->stream, &pos);
708 return NSS_STATUS_TRYAGAIN;
711 /* Terminate the line for any case. */
712 buffer[buflen - 1] = '\0';
714 /* Skip leading blanks. */
718 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
719 /* Parse the line. If it is invalid, loop to
720 get the next line of the file to parse. */
721 !(parse_res = _nss_files_parse_pwent (p, result, data, buflen,
726 /* The parser ran out of space. */
727 fsetpos (ent->stream, &pos);
729 return NSS_STATUS_TRYAGAIN;
732 /* This is a real entry. */
733 if (result->pw_name[0] != '+' && result->pw_name[0] != '-')
735 if (strcmp (result->pw_name, name) == 0)
736 return NSS_STATUS_SUCCESS;
742 if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
743 && result->pw_name[2] != '\0')
745 if (innetgr (&result->pw_name[2], NULL, name, NULL))
746 return NSS_STATUS_NOTFOUND;
751 if (result->pw_name[0] == '+' && result->pw_name[1] == '@'
752 && result->pw_name[2] != '\0')
754 enum nss_status status;
756 if (innetgr (&result->pw_name[2], NULL, name, NULL))
758 status = getpwnam_plususer (name, result, ent, buffer,
761 if (status == NSS_STATUS_RETURN)
770 if (result->pw_name[0] == '-' && result->pw_name[1] != '\0'
771 && result->pw_name[1] != '@')
773 if (strcmp (&result->pw_name[1], name) == 0)
774 return NSS_STATUS_NOTFOUND;
780 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
781 && result->pw_name[1] != '@')
783 if (strcmp (name, &result->pw_name[1]) == 0)
785 enum nss_status status;
787 status = getpwnam_plususer (name, result, ent, buffer, buflen,
789 if (status == NSS_STATUS_RETURN)
790 /* We couldn't parse the entry */
791 return NSS_STATUS_NOTFOUND;
798 if (result->pw_name[0] == '+' && result->pw_name[1] == '\0')
800 enum nss_status status;
802 status = getpwnam_plususer (name, result, ent,
803 buffer, buflen, errnop);
804 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
806 else if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
807 return NSS_STATUS_NOTFOUND;
812 return NSS_STATUS_SUCCESS;
816 _nss_compat_getpwnam_r (const char *name, struct passwd *pwd,
817 char *buffer, size_t buflen, int *errnop)
819 enum nss_status result;
820 ent_t ent = {0, 0, TRUE, NULL, {NULL, 0, 0},
821 {NULL, NULL, 0, 0, NULL, NULL, NULL}};
823 if (name[0] == '-' || name[0] == '+')
824 return NSS_STATUS_NOTFOUND;
826 __libc_lock_lock (lock);
829 init_nss_interface ();
831 __libc_lock_unlock (lock);
833 result = internal_setpwent (&ent, 0);
835 if (result == NSS_STATUS_SUCCESS)
836 result = internal_getpwnam_r (name, pwd, &ent, buffer, buflen, errnop);
838 internal_endpwent (&ent);
843 /* This function handle the + entry in /etc/passwd for getpwuid */
844 static enum nss_status
845 getpwuid_plususer (uid_t uid, struct passwd *result, char *buffer,
846 size_t buflen, int *errnop)
853 return NSS_STATUS_UNAVAIL;
855 memset (&pwd, '\0', sizeof (struct passwd));
857 copy_pwd_changes (&pwd, result, NULL, 0);
859 plen = pwd_need_buflen (&pwd);
863 return NSS_STATUS_TRYAGAIN;
865 p = buffer + (buflen - plen);
868 if (nss_getpwuid_r (uid, result, buffer, buflen, errnop) ==
871 copy_pwd_changes (result, &pwd, p, plen);
872 give_pwd_free (&pwd);
873 /* We found the entry. */
874 return NSS_STATUS_SUCCESS;
878 /* Give buffer the old len back */
880 give_pwd_free (&pwd);
882 return NSS_STATUS_RETURN;
885 /* Searches in /etc/passwd and the NSS subsystem for a special user id */
886 static enum nss_status
887 internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
888 char *buffer, size_t buflen, int *errnop)
890 struct parser_data *data = (void *) buffer;
900 fgetpos (ent->stream, &pos);
901 buffer[buflen - 1] = '\xff';
902 p = fgets_unlocked (buffer, buflen, ent->stream);
903 if (p == NULL && feof_unlocked (ent->stream))
904 return NSS_STATUS_NOTFOUND;
906 if (p == NULL || buffer[buflen - 1] != '\xff')
908 fsetpos (ent->stream, &pos);
910 return NSS_STATUS_TRYAGAIN;
913 /* Terminate the line for any case. */
914 buffer[buflen - 1] = '\0';
916 /* Skip leading blanks. */
920 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
921 /* Parse the line. If it is invalid, loop to
922 get the next line of the file to parse. */
923 !(parse_res = _nss_files_parse_pwent (p, result, data, buflen,
928 /* The parser ran out of space. */
929 fsetpos (ent->stream, &pos);
931 return NSS_STATUS_TRYAGAIN;
934 /* This is a real entry. */
935 if (result->pw_name[0] != '+' && result->pw_name[0] != '-')
937 if (result->pw_uid == uid)
938 return NSS_STATUS_SUCCESS;
944 if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
945 && result->pw_name[2] != '\0')
947 /* -1, because we remove first two character of pw_name. */
948 size_t len = strlen (result->pw_name) - 1;
950 enum nss_status status;
952 memcpy (buf, &result->pw_name[2], len);
954 status = getpwuid_plususer (uid, result, buffer, buflen, errnop);
955 if (status == NSS_STATUS_SUCCESS &&
956 innetgr (buf, NULL, result->pw_name, NULL))
957 return NSS_STATUS_NOTFOUND;
963 if (result->pw_name[0] == '+' && result->pw_name[1] == '@'
964 && result->pw_name[2] != '\0')
966 /* -1, because we remove first two characters of pw_name. */
967 size_t len = strlen (result->pw_name) - 1;
969 enum nss_status status;
971 memcpy (buf, &result->pw_name[2], len);
973 status = getpwuid_plususer (uid, result, buffer, buflen, errnop);
975 if (status == NSS_STATUS_RETURN)
978 if (status == NSS_STATUS_SUCCESS)
980 if (innetgr (buf, NULL, result->pw_name, NULL))
981 return NSS_STATUS_SUCCESS;
983 else if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
984 return NSS_STATUS_NOTFOUND;
992 if (result->pw_name[0] == '-' && result->pw_name[1] != '\0'
993 && result->pw_name[1] != '@')
995 size_t len = strlen (result->pw_name);
997 enum nss_status status;
999 memcpy (buf, &result->pw_name[1], len);
1001 status = getpwuid_plususer (uid, result, buffer, buflen, errnop);
1002 if (status == NSS_STATUS_SUCCESS &&
1003 innetgr (buf, NULL, result->pw_name, NULL))
1004 return NSS_STATUS_NOTFOUND;
1009 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
1010 && result->pw_name[1] != '@')
1012 size_t len = strlen (result->pw_name);
1014 enum nss_status status;
1016 memcpy (buf, &result->pw_name[1], len);
1018 status = getpwuid_plususer (uid, result, buffer, buflen, errnop);
1020 if (status == NSS_STATUS_RETURN)
1023 if (status == NSS_STATUS_SUCCESS)
1025 if (strcmp (buf, result->pw_name) == 0)
1026 return NSS_STATUS_SUCCESS;
1028 else if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
1029 return NSS_STATUS_NOTFOUND;
1037 if (result->pw_name[0] == '+' && result->pw_name[1] == '\0')
1039 enum nss_status status;
1041 status = getpwuid_plususer (uid, result, buffer, buflen, errnop);
1042 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
1044 else if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
1045 return NSS_STATUS_NOTFOUND;
1050 return NSS_STATUS_SUCCESS;
1054 _nss_compat_getpwuid_r (uid_t uid, struct passwd *pwd,
1055 char *buffer, size_t buflen, int *errnop)
1057 enum nss_status result;
1058 ent_t ent = {0, 0, TRUE, NULL, {NULL, 0, 0},
1059 {NULL, NULL, 0, 0, NULL, NULL, NULL}};
1061 __libc_lock_lock (lock);
1064 init_nss_interface ();
1066 __libc_lock_unlock (lock);
1068 result = internal_setpwent (&ent, 0);
1070 if (result == NSS_STATUS_SUCCESS)
1071 result = internal_getpwuid_r (uid, pwd, &ent, buffer, buflen, errnop);
1073 internal_endpwent (&ent);
1079 /* Support routines for remembering -@netgroup and -user entries.
1080 The names are stored in a single string with `|' as separator. */
1082 blacklist_store_name (const char *name, ent_t *ent)
1084 int namelen = strlen (name);
1087 /* first call, setup cache */
1088 if (ent->blacklist.size == 0)
1090 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
1091 ent->blacklist.data = malloc (ent->blacklist.size);
1092 if (ent->blacklist.data == NULL)
1094 ent->blacklist.data[0] = '|';
1095 ent->blacklist.data[1] = '\0';
1096 ent->blacklist.current = 1;
1100 if (in_blacklist (name, namelen, ent))
1101 return; /* no duplicates */
1103 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
1105 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
1106 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
1109 free (ent->blacklist.data);
1110 ent->blacklist.size = 0;
1113 ent->blacklist.data = tmp;
1117 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
1120 ent->blacklist.current += namelen + 1;
1125 /* returns TRUE if ent->blacklist contains name, else FALSE */
1127 in_blacklist (const char *name, int namelen, ent_t *ent)
1129 char buf[namelen + 3];
1132 if (ent->blacklist.data == NULL)
1136 cp = stpcpy (&buf[1], name);
1139 return strstr (ent->blacklist.data, buf) != NULL;