1 /* Copyright (C) 1996-1999,2001-2006,2007,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <stdio_ext.h>
30 #include <rpc/types.h>
31 #include <rpcsvc/ypclnt.h>
32 #include <bits/libc-lock.h>
33 #include <kernel-features.h>
37 static service_user
*ni
;
38 static enum nss_status (*nss_setpwent
) (int stayopen
);
39 static enum nss_status (*nss_getpwnam_r
) (const char *name
,
40 struct passwd
* pwd
, char *buffer
,
41 size_t buflen
, int *errnop
);
42 static enum nss_status (*nss_getpwuid_r
) (uid_t uid
, struct passwd
* pwd
,
43 char *buffer
, size_t buflen
,
45 static enum nss_status (*nss_getpwent_r
) (struct passwd
* pwd
, char *buffer
,
46 size_t buflen
, int *errnop
);
47 static enum nss_status (*nss_endpwent
) (void);
49 /* Get the declaration of the parser function. */
51 #define STRUCTURE passwd
53 #include <nss/nss_files/files-parse.c>
55 /* Structure for remembering -@netgroup and -user members ... */
56 #define BLACKLIST_INITIAL_SIZE 512
57 #define BLACKLIST_INCREMENT 256
70 enum nss_status setent_status
;
72 struct blacklist_t blacklist
;
74 struct __netgrent netgrdata
;
76 typedef struct ent_t ent_t
;
78 static ent_t ext_ent
= { false, false, true, NSS_STATUS_SUCCESS
, NULL
,
80 { NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
82 /* Protect global state against multiple changers. */
83 __libc_lock_define_initialized (static, lock
)
85 /* Positive if O_CLOEXEC is supported, negative if it is not supported,
86 zero if it is still undecided. This variable is shared with the
87 other compat functions. */
88 #ifdef __ASSUME_O_CLOEXEC
89 # define __compat_have_cloexec 1
92 extern int __compat_have_cloexec
;
94 # define __compat_have_cloexec -1
98 /* Prototypes for local functions. */
99 static void blacklist_store_name (const char *, ent_t
*);
100 static int in_blacklist (const char *, int, ent_t
*);
102 /* Initialize the NSS interface/functions. The calling function must
105 init_nss_interface (void)
107 if (__nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
) >= 0)
109 nss_setpwent
= __nss_lookup_function (ni
, "setpwent");
110 nss_getpwnam_r
= __nss_lookup_function (ni
, "getpwnam_r");
111 nss_getpwuid_r
= __nss_lookup_function (ni
, "getpwuid_r");
112 nss_getpwent_r
= __nss_lookup_function (ni
, "getpwent_r");
113 nss_endpwent
= __nss_lookup_function (ni
, "endpwent");
118 give_pwd_free (struct passwd
*pwd
)
121 free (pwd
->pw_passwd
);
122 free (pwd
->pw_gecos
);
124 free (pwd
->pw_shell
);
126 memset (pwd
, '\0', sizeof (struct passwd
));
130 pwd_need_buflen (struct passwd
*pwd
)
134 if (pwd
->pw_passwd
!= NULL
)
135 len
+= strlen (pwd
->pw_passwd
) + 1;
137 if (pwd
->pw_gecos
!= NULL
)
138 len
+= strlen (pwd
->pw_gecos
) + 1;
140 if (pwd
->pw_dir
!= NULL
)
141 len
+= strlen (pwd
->pw_dir
) + 1;
143 if (pwd
->pw_shell
!= NULL
)
144 len
+= strlen (pwd
->pw_shell
) + 1;
150 copy_pwd_changes (struct passwd
*dest
, struct passwd
*src
,
151 char *buffer
, size_t buflen
)
153 if (src
->pw_passwd
!= NULL
&& strlen (src
->pw_passwd
))
156 dest
->pw_passwd
= strdup (src
->pw_passwd
);
157 else if (dest
->pw_passwd
&&
158 strlen (dest
->pw_passwd
) >= strlen (src
->pw_passwd
))
159 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
162 dest
->pw_passwd
= buffer
;
163 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
164 buffer
+= strlen (dest
->pw_passwd
) + 1;
165 buflen
= buflen
- (strlen (dest
->pw_passwd
) + 1);
169 if (src
->pw_gecos
!= NULL
&& strlen (src
->pw_gecos
))
172 dest
->pw_gecos
= strdup (src
->pw_gecos
);
173 else if (dest
->pw_gecos
&&
174 strlen (dest
->pw_gecos
) >= strlen (src
->pw_gecos
))
175 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
178 dest
->pw_gecos
= buffer
;
179 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
180 buffer
+= strlen (dest
->pw_gecos
) + 1;
181 buflen
= buflen
- (strlen (dest
->pw_gecos
) + 1);
184 if (src
->pw_dir
!= NULL
&& strlen (src
->pw_dir
))
187 dest
->pw_dir
= strdup (src
->pw_dir
);
188 else if (dest
->pw_dir
&& strlen (dest
->pw_dir
) >= strlen (src
->pw_dir
))
189 strcpy (dest
->pw_dir
, src
->pw_dir
);
192 dest
->pw_dir
= buffer
;
193 strcpy (dest
->pw_dir
, src
->pw_dir
);
194 buffer
+= strlen (dest
->pw_dir
) + 1;
195 buflen
= buflen
- (strlen (dest
->pw_dir
) + 1);
199 if (src
->pw_shell
!= NULL
&& strlen (src
->pw_shell
))
202 dest
->pw_shell
= strdup (src
->pw_shell
);
203 else if (dest
->pw_shell
&&
204 strlen (dest
->pw_shell
) >= strlen (src
->pw_shell
))
205 strcpy (dest
->pw_shell
, src
->pw_shell
);
208 dest
->pw_shell
= buffer
;
209 strcpy (dest
->pw_shell
, src
->pw_shell
);
210 buffer
+= strlen (dest
->pw_shell
) + 1;
211 buflen
= buflen
- (strlen (dest
->pw_shell
) + 1);
216 static enum nss_status
217 internal_setpwent (ent_t
*ent
, int stayopen
, int needent
)
219 enum nss_status status
= NSS_STATUS_SUCCESS
;
221 ent
->first
= ent
->netgroup
= false;
223 ent
->setent_status
= NSS_STATUS_SUCCESS
;
225 /* If something was left over free it. */
227 __internal_endnetgrent (&ent
->netgrdata
);
229 if (ent
->blacklist
.data
!= NULL
)
231 ent
->blacklist
.current
= 1;
232 ent
->blacklist
.data
[0] = '|';
233 ent
->blacklist
.data
[1] = '\0';
236 ent
->blacklist
.current
= 0;
238 if (ent
->stream
== NULL
)
240 ent
->stream
= fopen ("/etc/passwd", "rme");
242 if (ent
->stream
== NULL
)
243 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
246 /* We have to make sure the file is `closed on exec'. */
249 if (__compat_have_cloexec
<= 0)
252 result
= flags
= fcntl (fileno_unlocked (ent
->stream
), F_GETFD
,
256 #if defined O_CLOEXEC && !defined __ASSUME_O_CLOEXEC
257 if (__compat_have_cloexec
== 0)
258 __compat_have_cloexec
= (flags
& FD_CLOEXEC
) ? 1 : -1;
260 if (__compat_have_cloexec
< 0)
264 result
= fcntl (fileno_unlocked (ent
->stream
), F_SETFD
,
272 /* Something went wrong. Close the stream and return a
274 fclose (ent
->stream
);
276 status
= NSS_STATUS_UNAVAIL
;
279 /* We take care of locking ourself. */
280 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
284 rewind (ent
->stream
);
286 give_pwd_free (&ent
->pwd
);
288 if (needent
&& status
== NSS_STATUS_SUCCESS
&& nss_setpwent
)
289 ent
->setent_status
= nss_setpwent (stayopen
);
296 _nss_compat_setpwent (int stayopen
)
298 enum nss_status result
;
300 __libc_lock_lock (lock
);
303 init_nss_interface ();
305 result
= internal_setpwent (&ext_ent
, stayopen
, 1);
307 __libc_lock_unlock (lock
);
313 static enum nss_status
314 internal_endpwent (ent_t
*ent
)
319 if (ent
->stream
!= NULL
)
321 fclose (ent
->stream
);
326 __internal_endnetgrent (&ent
->netgrdata
);
328 ent
->first
= ent
->netgroup
= false;
330 if (ent
->blacklist
.data
!= NULL
)
332 ent
->blacklist
.current
= 1;
333 ent
->blacklist
.data
[0] = '|';
334 ent
->blacklist
.data
[1] = '\0';
337 ent
->blacklist
.current
= 0;
339 give_pwd_free (&ent
->pwd
);
341 return NSS_STATUS_SUCCESS
;
345 _nss_compat_endpwent (void)
347 enum nss_status result
;
349 __libc_lock_lock (lock
);
351 result
= internal_endpwent (&ext_ent
);
353 __libc_lock_unlock (lock
);
359 static enum nss_status
360 getpwent_next_nss_netgr (const char *name
, struct passwd
*result
, ent_t
*ent
,
361 char *group
, char *buffer
, size_t buflen
,
364 char *curdomain
= NULL
, *host
, *user
, *domain
, *p2
;
368 /* Leave function if NSS module does not support getpwnam_r,
369 we need this function here. */
371 return NSS_STATUS_UNAVAIL
;
375 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
376 __internal_setnetgrent (group
, &ent
->netgrdata
);
382 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
383 &ent
->netgrdata
, buffer
, buflen
,
387 __internal_endnetgrent (&ent
->netgrdata
);
389 give_pwd_free (&ent
->pwd
);
390 return NSS_STATUS_RETURN
;
393 if (user
== NULL
|| user
[0] == '-')
398 if (curdomain
== NULL
399 && yp_get_default_domain (&curdomain
) != YPERR_SUCCESS
)
401 __internal_endnetgrent (&ent
->netgrdata
);
402 ent
->netgroup
= false;
403 give_pwd_free (&ent
->pwd
);
404 return NSS_STATUS_UNAVAIL
;
406 if (strcmp (curdomain
, domain
) != 0)
410 /* If name != NULL, we are called from getpwnam. */
412 if (strcmp (user
, name
) != 0)
415 p2len
= pwd_need_buflen (&ent
->pwd
);
419 return NSS_STATUS_TRYAGAIN
;
421 p2
= buffer
+ (buflen
- p2len
);
424 if (nss_getpwnam_r (user
, result
, buffer
, buflen
, errnop
) !=
428 if (!in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
430 /* Store the User in the blacklist for possible the "+" at the
431 end of /etc/passwd */
432 blacklist_store_name (result
->pw_name
, ent
);
433 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
438 return NSS_STATUS_SUCCESS
;
441 /* get the next user from NSS (+ entry) */
442 static enum nss_status
443 getpwent_next_nss (struct passwd
*result
, ent_t
*ent
, char *buffer
,
444 size_t buflen
, int *errnop
)
446 enum nss_status status
;
450 /* Return if NSS module does not support getpwent_r. */
452 return NSS_STATUS_UNAVAIL
;
454 /* If the setpwent call failed, say so. */
455 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
456 return ent
->setent_status
;
458 p2len
= pwd_need_buflen (&ent
->pwd
);
462 return NSS_STATUS_TRYAGAIN
;
464 p2
= buffer
+ (buflen
- p2len
);
472 if ((status
= nss_getpwent_r (result
, buffer
, buflen
, errnop
)) !=
476 while (in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
));
478 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
480 return NSS_STATUS_SUCCESS
;
483 /* This function handle the +user entrys in /etc/passwd */
484 static enum nss_status
485 getpwnam_plususer (const char *name
, struct passwd
*result
, ent_t
*ent
,
486 char *buffer
, size_t buflen
, int *errnop
)
489 return NSS_STATUS_UNAVAIL
;
492 memset (&pwd
, '\0', sizeof (struct passwd
));
494 copy_pwd_changes (&pwd
, result
, NULL
, 0);
496 size_t plen
= pwd_need_buflen (&pwd
);
500 return NSS_STATUS_TRYAGAIN
;
502 char *p
= buffer
+ (buflen
- plen
);
505 enum nss_status status
= nss_getpwnam_r (name
, result
, buffer
, buflen
,
507 if (status
!= NSS_STATUS_SUCCESS
)
510 if (in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
511 return NSS_STATUS_NOTFOUND
;
513 copy_pwd_changes (result
, &pwd
, p
, plen
);
514 give_pwd_free (&pwd
);
515 /* We found the entry. */
516 return NSS_STATUS_SUCCESS
;
519 static enum nss_status
520 getpwent_next_file (struct passwd
*result
, ent_t
*ent
,
521 char *buffer
, size_t buflen
, int *errnop
)
523 struct parser_data
*data
= (void *) buffer
;
532 /* We need at least 3 characters for one line. */
533 if (__builtin_expect (buflen
< 3, 0))
537 return NSS_STATUS_TRYAGAIN
;
540 fgetpos (ent
->stream
, &pos
);
541 buffer
[buflen
- 1] = '\xff';
542 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
543 if (p
== NULL
&& feof_unlocked (ent
->stream
))
544 return NSS_STATUS_NOTFOUND
;
546 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
549 fsetpos (ent
->stream
, &pos
);
553 /* Terminate the line for any case. */
554 buffer
[buflen
- 1] = '\0';
556 /* Skip leading blanks. */
560 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
561 /* Parse the line. If it is invalid, loop to
562 get the next line of the file to parse. */
563 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
566 if (__builtin_expect (parse_res
== -1, 0))
567 /* The parser ran out of space. */
570 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
571 /* This is a real entry. */
575 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
576 && result
->pw_name
[2] != '\0')
578 /* XXX Do not use fixed length buffer. */
580 char *user
, *host
, *domain
;
581 struct __netgrent netgrdata
;
583 bzero (&netgrdata
, sizeof (struct __netgrent
));
584 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
585 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
586 buf2
, sizeof (buf2
), errnop
))
588 if (user
!= NULL
&& user
[0] != '-')
589 blacklist_store_name (user
, ent
);
591 __internal_endnetgrent (&netgrdata
);
596 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
597 && result
->pw_name
[2] != '\0')
599 enum nss_status status
;
601 ent
->netgroup
= true;
603 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
605 status
= getpwent_next_nss_netgr (NULL
, result
, ent
,
607 buffer
, buflen
, errnop
);
608 if (status
== NSS_STATUS_RETURN
)
615 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
616 && result
->pw_name
[1] != '@')
618 blacklist_store_name (&result
->pw_name
[1], ent
);
623 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
624 && result
->pw_name
[1] != '@')
626 size_t len
= strlen (result
->pw_name
);
628 enum nss_status status
;
630 /* Store the User in the blacklist for the "+" at the end of
632 memcpy (buf
, &result
->pw_name
[1], len
);
633 status
= getpwnam_plususer (&result
->pw_name
[1], result
, ent
,
634 buffer
, buflen
, errnop
);
635 blacklist_store_name (buf
, ent
);
637 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
639 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry */
640 || status
== NSS_STATUS_NOTFOUND
) /* entry doesn't exist */
644 if (status
== NSS_STATUS_TRYAGAIN
)
646 /* The parser ran out of space */
647 fsetpos (ent
->stream
, &pos
);
655 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
659 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
661 return getpwent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
665 return NSS_STATUS_SUCCESS
;
669 static enum nss_status
670 internal_getpwent_r (struct passwd
*pw
, ent_t
*ent
, char *buffer
,
671 size_t buflen
, int *errnop
)
675 enum nss_status status
;
677 /* We are searching members in a netgroup */
678 /* Since this is not the first call, we don't need the group name */
679 status
= getpwent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
, buflen
,
681 if (status
== NSS_STATUS_RETURN
)
682 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
687 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
689 return getpwent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
694 _nss_compat_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
697 enum nss_status result
= NSS_STATUS_SUCCESS
;
699 __libc_lock_lock (lock
);
701 /* Be prepared that the setpwent function was not called before. */
703 init_nss_interface ();
705 if (ext_ent
.stream
== NULL
)
706 result
= internal_setpwent (&ext_ent
, 1, 1);
708 if (result
== NSS_STATUS_SUCCESS
)
709 result
= internal_getpwent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
711 __libc_lock_unlock (lock
);
716 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
717 static enum nss_status
718 internal_getpwnam_r (const char *name
, struct passwd
*result
, ent_t
*ent
,
719 char *buffer
, size_t buflen
, int *errnop
)
721 struct parser_data
*data
= (void *) buffer
;
731 /* We need at least 3 characters for one line. */
732 if (__builtin_expect (buflen
< 3, 0))
736 return NSS_STATUS_TRYAGAIN
;
739 fgetpos (ent
->stream
, &pos
);
740 buffer
[buflen
- 1] = '\xff';
741 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
742 if (p
== NULL
&& feof_unlocked (ent
->stream
))
744 return NSS_STATUS_NOTFOUND
;
746 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
749 fsetpos (ent
->stream
, &pos
);
753 /* Terminate the line for any case. */
754 buffer
[buflen
- 1] = '\0';
756 /* Skip leading blanks. */
760 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
761 /* Parse the line. If it is invalid, loop to
762 get the next line of the file to parse. */
763 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
766 if (__builtin_expect (parse_res
== -1, 0))
767 /* The parser ran out of space. */
770 /* This is a real entry. */
771 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
773 if (strcmp (result
->pw_name
, name
) == 0)
774 return NSS_STATUS_SUCCESS
;
780 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
781 && result
->pw_name
[2] != '\0')
783 if (innetgr (&result
->pw_name
[2], NULL
, name
, NULL
))
784 return NSS_STATUS_NOTFOUND
;
789 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
790 && result
->pw_name
[2] != '\0')
792 enum nss_status status
;
794 if (innetgr (&result
->pw_name
[2], NULL
, name
, NULL
))
796 status
= getpwnam_plususer (name
, result
, ent
, buffer
,
799 if (status
== NSS_STATUS_RETURN
)
808 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
809 && result
->pw_name
[1] != '@')
811 if (strcmp (&result
->pw_name
[1], name
) == 0)
812 return NSS_STATUS_NOTFOUND
;
818 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
819 && result
->pw_name
[1] != '@')
821 if (strcmp (name
, &result
->pw_name
[1]) == 0)
823 enum nss_status status
;
825 status
= getpwnam_plususer (name
, result
, ent
, buffer
, buflen
,
827 if (status
== NSS_STATUS_RETURN
)
828 /* We couldn't parse the entry */
829 return NSS_STATUS_NOTFOUND
;
836 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
838 enum nss_status status
;
840 status
= getpwnam_plususer (name
, result
, ent
,
841 buffer
, buflen
, errnop
);
842 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
844 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
845 return NSS_STATUS_NOTFOUND
;
850 return NSS_STATUS_SUCCESS
;
854 _nss_compat_getpwnam_r (const char *name
, struct passwd
*pwd
,
855 char *buffer
, size_t buflen
, int *errnop
)
857 enum nss_status result
;
858 ent_t ent
= { false, false, true, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 },
859 { NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
861 if (name
[0] == '-' || name
[0] == '+')
862 return NSS_STATUS_NOTFOUND
;
864 __libc_lock_lock (lock
);
867 init_nss_interface ();
869 __libc_lock_unlock (lock
);
871 result
= internal_setpwent (&ent
, 0, 0);
873 if (result
== NSS_STATUS_SUCCESS
)
874 result
= internal_getpwnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
876 internal_endpwent (&ent
);
881 /* This function handle the + entry in /etc/passwd for getpwuid */
882 static enum nss_status
883 getpwuid_plususer (uid_t uid
, struct passwd
*result
, char *buffer
,
884 size_t buflen
, int *errnop
)
891 return NSS_STATUS_UNAVAIL
;
893 memset (&pwd
, '\0', sizeof (struct passwd
));
895 copy_pwd_changes (&pwd
, result
, NULL
, 0);
897 plen
= pwd_need_buflen (&pwd
);
901 return NSS_STATUS_TRYAGAIN
;
903 p
= buffer
+ (buflen
- plen
);
906 if (nss_getpwuid_r (uid
, result
, buffer
, buflen
, errnop
) ==
909 copy_pwd_changes (result
, &pwd
, p
, plen
);
910 give_pwd_free (&pwd
);
911 /* We found the entry. */
912 return NSS_STATUS_SUCCESS
;
916 /* Give buffer the old len back */
918 give_pwd_free (&pwd
);
920 return NSS_STATUS_RETURN
;
923 /* Searches in /etc/passwd and the NSS subsystem for a special user id */
924 static enum nss_status
925 internal_getpwuid_r (uid_t uid
, struct passwd
*result
, ent_t
*ent
,
926 char *buffer
, size_t buflen
, int *errnop
)
928 struct parser_data
*data
= (void *) buffer
;
938 /* We need at least 3 characters for one line. */
939 if (__builtin_expect (buflen
< 3, 0))
943 return NSS_STATUS_TRYAGAIN
;
946 fgetpos (ent
->stream
, &pos
);
947 buffer
[buflen
- 1] = '\xff';
948 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
949 if (p
== NULL
&& feof_unlocked (ent
->stream
))
950 return NSS_STATUS_NOTFOUND
;
952 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
955 fsetpos (ent
->stream
, &pos
);
959 /* Terminate the line for any case. */
960 buffer
[buflen
- 1] = '\0';
962 /* Skip leading blanks. */
966 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
967 /* Parse the line. If it is invalid, loop to
968 get the next line of the file to parse. */
969 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
972 if (__builtin_expect (parse_res
== -1, 0))
973 /* The parser ran out of space. */
976 /* This is a real entry. */
977 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
979 if (result
->pw_uid
== uid
)
980 return NSS_STATUS_SUCCESS
;
986 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
987 && result
->pw_name
[2] != '\0')
989 /* -1, because we remove first two character of pw_name. */
990 size_t len
= strlen (result
->pw_name
) - 1;
992 enum nss_status status
;
994 memcpy (buf
, &result
->pw_name
[2], len
);
996 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
997 if (status
== NSS_STATUS_SUCCESS
&&
998 innetgr (buf
, NULL
, result
->pw_name
, NULL
))
999 return NSS_STATUS_NOTFOUND
;
1005 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
1006 && result
->pw_name
[2] != '\0')
1008 /* -1, because we remove first two characters of pw_name. */
1009 size_t len
= strlen (result
->pw_name
) - 1;
1011 enum nss_status status
;
1013 memcpy (buf
, &result
->pw_name
[2], len
);
1015 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
1017 if (status
== NSS_STATUS_RETURN
)
1020 if (status
== NSS_STATUS_SUCCESS
)
1022 if (innetgr (buf
, NULL
, result
->pw_name
, NULL
))
1023 return NSS_STATUS_SUCCESS
;
1025 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1026 return NSS_STATUS_NOTFOUND
;
1034 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
1035 && result
->pw_name
[1] != '@')
1037 size_t len
= strlen (result
->pw_name
);
1039 enum nss_status status
;
1041 memcpy (buf
, &result
->pw_name
[1], len
);
1043 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
1044 if (status
== NSS_STATUS_SUCCESS
&&
1045 innetgr (buf
, NULL
, result
->pw_name
, NULL
))
1046 return NSS_STATUS_NOTFOUND
;
1051 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
1052 && result
->pw_name
[1] != '@')
1054 size_t len
= strlen (result
->pw_name
);
1056 enum nss_status status
;
1058 memcpy (buf
, &result
->pw_name
[1], len
);
1060 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
1062 if (status
== NSS_STATUS_RETURN
)
1065 if (status
== NSS_STATUS_SUCCESS
)
1067 if (strcmp (buf
, result
->pw_name
) == 0)
1068 return NSS_STATUS_SUCCESS
;
1070 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1071 return NSS_STATUS_NOTFOUND
;
1079 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
1081 enum nss_status status
;
1083 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
1084 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
1086 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1087 return NSS_STATUS_NOTFOUND
;
1092 return NSS_STATUS_SUCCESS
;
1096 _nss_compat_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
1097 char *buffer
, size_t buflen
, int *errnop
)
1099 enum nss_status result
;
1100 ent_t ent
= { false, false, true, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 },
1101 { NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
1103 __libc_lock_lock (lock
);
1106 init_nss_interface ();
1108 __libc_lock_unlock (lock
);
1110 result
= internal_setpwent (&ent
, 0, 0);
1112 if (result
== NSS_STATUS_SUCCESS
)
1113 result
= internal_getpwuid_r (uid
, pwd
, &ent
, buffer
, buflen
, errnop
);
1115 internal_endpwent (&ent
);
1121 /* Support routines for remembering -@netgroup and -user entries.
1122 The names are stored in a single string with `|' as separator. */
1124 blacklist_store_name (const char *name
, ent_t
*ent
)
1126 int namelen
= strlen (name
);
1129 /* first call, setup cache */
1130 if (ent
->blacklist
.size
== 0)
1132 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
1133 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
1134 if (ent
->blacklist
.data
== NULL
)
1136 ent
->blacklist
.data
[0] = '|';
1137 ent
->blacklist
.data
[1] = '\0';
1138 ent
->blacklist
.current
= 1;
1142 if (in_blacklist (name
, namelen
, ent
))
1143 return; /* no duplicates */
1145 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
1147 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
1148 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
1151 free (ent
->blacklist
.data
);
1152 ent
->blacklist
.size
= 0;
1155 ent
->blacklist
.data
= tmp
;
1159 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
1162 ent
->blacklist
.current
+= namelen
+ 1;
1167 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
1169 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
1171 char buf
[namelen
+ 3];
1174 if (ent
->blacklist
.data
== NULL
)
1178 cp
= stpcpy (&buf
[1], name
);
1181 return strstr (ent
->blacklist
.data
, buf
) != NULL
;