1 /* Copyright (C) 1996-2018 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, see
17 <http://www.gnu.org/licenses/>. */
26 #include <stdio_ext.h>
28 #include <libc-lock.h>
29 #include <kernel-features.h>
32 #include "nisdomain.h"
34 static service_user
*ni
;
35 static enum nss_status (*nss_setpwent
) (int stayopen
);
36 static enum nss_status (*nss_getpwnam_r
) (const char *name
,
37 struct passwd
* pwd
, char *buffer
,
38 size_t buflen
, int *errnop
);
39 static enum nss_status (*nss_getpwuid_r
) (uid_t uid
, struct passwd
* pwd
,
40 char *buffer
, size_t buflen
,
42 static enum nss_status (*nss_getpwent_r
) (struct passwd
* pwd
, char *buffer
,
43 size_t buflen
, int *errnop
);
44 static enum nss_status (*nss_endpwent
) (void);
46 /* Get the declaration of the parser function. */
48 #define STRUCTURE passwd
50 #include <nss/nss_files/files-parse.c>
52 /* Structure for remembering -@netgroup and -user members ... */
53 #define BLACKLIST_INITIAL_SIZE 512
54 #define BLACKLIST_INCREMENT 256
67 enum nss_status setent_status
;
69 struct blacklist_t blacklist
;
71 struct __netgrent netgrdata
;
73 typedef struct ent_t ent_t
;
75 static ent_t ext_ent
= { false, false, true, NSS_STATUS_SUCCESS
, NULL
,
77 { NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
79 /* Protect global state against multiple changers. */
80 __libc_lock_define_initialized (static, lock
)
82 /* Prototypes for local functions. */
83 static void blacklist_store_name (const char *, ent_t
*);
84 static bool in_blacklist (const char *, int, ent_t
*);
86 /* Initialize the NSS interface/functions. The calling function must
89 init_nss_interface (void)
91 if (__nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
) >= 0)
93 nss_setpwent
= __nss_lookup_function (ni
, "setpwent");
94 nss_getpwnam_r
= __nss_lookup_function (ni
, "getpwnam_r");
95 nss_getpwuid_r
= __nss_lookup_function (ni
, "getpwuid_r");
96 nss_getpwent_r
= __nss_lookup_function (ni
, "getpwent_r");
97 nss_endpwent
= __nss_lookup_function (ni
, "endpwent");
102 give_pwd_free (struct passwd
*pwd
)
105 free (pwd
->pw_passwd
);
106 free (pwd
->pw_gecos
);
108 free (pwd
->pw_shell
);
110 memset (pwd
, '\0', sizeof (struct passwd
));
114 pwd_need_buflen (struct passwd
*pwd
)
118 if (pwd
->pw_passwd
!= NULL
)
119 len
+= strlen (pwd
->pw_passwd
) + 1;
121 if (pwd
->pw_gecos
!= NULL
)
122 len
+= strlen (pwd
->pw_gecos
) + 1;
124 if (pwd
->pw_dir
!= NULL
)
125 len
+= strlen (pwd
->pw_dir
) + 1;
127 if (pwd
->pw_shell
!= NULL
)
128 len
+= strlen (pwd
->pw_shell
) + 1;
134 copy_pwd_changes (struct passwd
*dest
, struct passwd
*src
,
135 char *buffer
, size_t buflen
)
137 if (src
->pw_passwd
!= NULL
&& strlen (src
->pw_passwd
))
140 dest
->pw_passwd
= strdup (src
->pw_passwd
);
141 else if (dest
->pw_passwd
&&
142 strlen (dest
->pw_passwd
) >= strlen (src
->pw_passwd
))
143 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
146 dest
->pw_passwd
= buffer
;
147 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
148 buffer
+= strlen (dest
->pw_passwd
) + 1;
149 buflen
= buflen
- (strlen (dest
->pw_passwd
) + 1);
153 if (src
->pw_gecos
!= NULL
&& strlen (src
->pw_gecos
))
156 dest
->pw_gecos
= strdup (src
->pw_gecos
);
157 else if (dest
->pw_gecos
&&
158 strlen (dest
->pw_gecos
) >= strlen (src
->pw_gecos
))
159 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
162 dest
->pw_gecos
= buffer
;
163 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
164 buffer
+= strlen (dest
->pw_gecos
) + 1;
165 buflen
= buflen
- (strlen (dest
->pw_gecos
) + 1);
168 if (src
->pw_dir
!= NULL
&& strlen (src
->pw_dir
))
171 dest
->pw_dir
= strdup (src
->pw_dir
);
172 else if (dest
->pw_dir
&& strlen (dest
->pw_dir
) >= strlen (src
->pw_dir
))
173 strcpy (dest
->pw_dir
, src
->pw_dir
);
176 dest
->pw_dir
= buffer
;
177 strcpy (dest
->pw_dir
, src
->pw_dir
);
178 buffer
+= strlen (dest
->pw_dir
) + 1;
179 buflen
= buflen
- (strlen (dest
->pw_dir
) + 1);
183 if (src
->pw_shell
!= NULL
&& strlen (src
->pw_shell
))
186 dest
->pw_shell
= strdup (src
->pw_shell
);
187 else if (dest
->pw_shell
&&
188 strlen (dest
->pw_shell
) >= strlen (src
->pw_shell
))
189 strcpy (dest
->pw_shell
, src
->pw_shell
);
192 dest
->pw_shell
= buffer
;
193 strcpy (dest
->pw_shell
, src
->pw_shell
);
194 buffer
+= strlen (dest
->pw_shell
) + 1;
195 buflen
= buflen
- (strlen (dest
->pw_shell
) + 1);
200 static enum nss_status
201 internal_setpwent (ent_t
*ent
, int stayopen
, int needent
)
203 enum nss_status status
= NSS_STATUS_SUCCESS
;
205 ent
->first
= ent
->netgroup
= false;
207 ent
->setent_status
= NSS_STATUS_SUCCESS
;
209 /* If something was left over free it. */
211 __internal_endnetgrent (&ent
->netgrdata
);
213 if (ent
->blacklist
.data
!= NULL
)
215 ent
->blacklist
.current
= 1;
216 ent
->blacklist
.data
[0] = '|';
217 ent
->blacklist
.data
[1] = '\0';
220 ent
->blacklist
.current
= 0;
222 if (ent
->stream
== NULL
)
224 ent
->stream
= fopen ("/etc/passwd", "rme");
226 if (ent
->stream
== NULL
)
227 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
229 /* We take care of locking ourself. */
230 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
233 rewind (ent
->stream
);
235 give_pwd_free (&ent
->pwd
);
237 if (needent
&& status
== NSS_STATUS_SUCCESS
&& nss_setpwent
)
238 ent
->setent_status
= nss_setpwent (stayopen
);
245 _nss_compat_setpwent (int stayopen
)
247 enum nss_status result
;
249 __libc_lock_lock (lock
);
252 init_nss_interface ();
254 result
= internal_setpwent (&ext_ent
, stayopen
, 1);
256 __libc_lock_unlock (lock
);
262 static enum nss_status
263 internal_endpwent (ent_t
*ent
)
265 if (ent
->stream
!= NULL
)
267 fclose (ent
->stream
);
272 __internal_endnetgrent (&ent
->netgrdata
);
274 ent
->first
= ent
->netgroup
= false;
276 if (ent
->blacklist
.data
!= NULL
)
278 ent
->blacklist
.current
= 1;
279 ent
->blacklist
.data
[0] = '|';
280 ent
->blacklist
.data
[1] = '\0';
283 ent
->blacklist
.current
= 0;
285 give_pwd_free (&ent
->pwd
);
287 return NSS_STATUS_SUCCESS
;
291 _nss_compat_endpwent (void)
293 enum nss_status result
;
295 __libc_lock_lock (lock
);
300 result
= internal_endpwent (&ext_ent
);
302 __libc_lock_unlock (lock
);
308 static enum nss_status
309 getpwent_next_nss_netgr (const char *name
, struct passwd
*result
, ent_t
*ent
,
310 char *group
, char *buffer
, size_t buflen
,
313 char *curdomain
= NULL
, *host
, *user
, *domain
, *p2
;
317 /* Leave function if NSS module does not support getpwnam_r,
318 we need this function here. */
320 return NSS_STATUS_UNAVAIL
;
324 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
325 __internal_setnetgrent (group
, &ent
->netgrdata
);
331 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
332 &ent
->netgrdata
, buffer
, buflen
,
336 __internal_endnetgrent (&ent
->netgrdata
);
338 give_pwd_free (&ent
->pwd
);
339 return NSS_STATUS_RETURN
;
342 if (user
== NULL
|| user
[0] == '-')
347 if (curdomain
== NULL
348 && __nss_get_default_domain (&curdomain
) != 0)
350 __internal_endnetgrent (&ent
->netgrdata
);
351 ent
->netgroup
= false;
352 give_pwd_free (&ent
->pwd
);
353 return NSS_STATUS_UNAVAIL
;
355 if (strcmp (curdomain
, domain
) != 0)
359 /* If name != NULL, we are called from getpwnam. */
361 if (strcmp (user
, name
) != 0)
364 p2len
= pwd_need_buflen (&ent
->pwd
);
368 return NSS_STATUS_TRYAGAIN
;
370 p2
= buffer
+ (buflen
- p2len
);
373 if (nss_getpwnam_r (user
, result
, buffer
, buflen
, errnop
) !=
377 if (!in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
379 /* Store the User in the blacklist for possible the "+" at the
380 end of /etc/passwd */
381 blacklist_store_name (result
->pw_name
, ent
);
382 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
387 return NSS_STATUS_SUCCESS
;
390 /* get the next user from NSS (+ entry) */
391 static enum nss_status
392 getpwent_next_nss (struct passwd
*result
, ent_t
*ent
, char *buffer
,
393 size_t buflen
, int *errnop
)
395 enum nss_status status
;
399 /* Return if NSS module does not support getpwent_r. */
401 return NSS_STATUS_UNAVAIL
;
403 /* If the setpwent call failed, say so. */
404 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
405 return ent
->setent_status
;
407 p2len
= pwd_need_buflen (&ent
->pwd
);
411 return NSS_STATUS_TRYAGAIN
;
413 p2
= buffer
+ (buflen
- p2len
);
421 if ((status
= nss_getpwent_r (result
, buffer
, buflen
, errnop
)) !=
425 while (in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
));
427 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
429 return NSS_STATUS_SUCCESS
;
432 /* This function handle the +user entrys in /etc/passwd */
433 static enum nss_status
434 getpwnam_plususer (const char *name
, struct passwd
*result
, ent_t
*ent
,
435 char *buffer
, size_t buflen
, int *errnop
)
438 return NSS_STATUS_UNAVAIL
;
441 memset (&pwd
, '\0', sizeof (struct passwd
));
443 copy_pwd_changes (&pwd
, result
, NULL
, 0);
445 size_t plen
= pwd_need_buflen (&pwd
);
449 return NSS_STATUS_TRYAGAIN
;
451 char *p
= buffer
+ (buflen
- plen
);
454 enum nss_status status
= nss_getpwnam_r (name
, result
, buffer
, buflen
,
456 if (status
!= NSS_STATUS_SUCCESS
)
459 if (in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
460 return NSS_STATUS_NOTFOUND
;
462 copy_pwd_changes (result
, &pwd
, p
, plen
);
463 give_pwd_free (&pwd
);
464 /* We found the entry. */
465 return NSS_STATUS_SUCCESS
;
468 static enum nss_status
469 getpwent_next_file (struct passwd
*result
, ent_t
*ent
,
470 char *buffer
, size_t buflen
, int *errnop
)
472 struct parser_data
*data
= (void *) buffer
;
481 /* We need at least 3 characters for one line. */
482 if (__glibc_unlikely (buflen
< 3))
486 return NSS_STATUS_TRYAGAIN
;
489 fgetpos (ent
->stream
, &pos
);
490 buffer
[buflen
- 1] = '\xff';
491 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
492 if (p
== NULL
&& feof_unlocked (ent
->stream
))
493 return NSS_STATUS_NOTFOUND
;
495 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
498 fsetpos (ent
->stream
, &pos
);
502 /* Terminate the line for any case. */
503 buffer
[buflen
- 1] = '\0';
505 /* Skip leading blanks. */
509 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
510 /* Parse the line. If it is invalid, loop to
511 get the next line of the file to parse. */
512 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
515 if (__glibc_unlikely (parse_res
== -1))
516 /* The parser ran out of space. */
519 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
520 /* This is a real entry. */
524 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
525 && result
->pw_name
[2] != '\0')
527 /* XXX Do not use fixed length buffer. */
529 char *user
, *host
, *domain
;
530 struct __netgrent netgrdata
;
532 memset (&netgrdata
, 0, sizeof (struct __netgrent
));
533 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
534 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
535 buf2
, sizeof (buf2
), errnop
))
537 if (user
!= NULL
&& user
[0] != '-')
538 blacklist_store_name (user
, ent
);
540 __internal_endnetgrent (&netgrdata
);
545 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
546 && result
->pw_name
[2] != '\0')
548 enum nss_status status
;
550 ent
->netgroup
= true;
552 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
554 status
= getpwent_next_nss_netgr (NULL
, result
, ent
,
556 buffer
, buflen
, errnop
);
557 if (status
== NSS_STATUS_RETURN
)
564 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
565 && result
->pw_name
[1] != '@')
567 blacklist_store_name (&result
->pw_name
[1], ent
);
572 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
573 && result
->pw_name
[1] != '@')
575 size_t len
= strlen (result
->pw_name
);
577 enum nss_status status
;
579 /* Store the User in the blacklist for the "+" at the end of
581 memcpy (buf
, &result
->pw_name
[1], len
);
582 status
= getpwnam_plususer (&result
->pw_name
[1], result
, ent
,
583 buffer
, buflen
, errnop
);
584 blacklist_store_name (buf
, ent
);
586 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
588 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry */
589 || status
== NSS_STATUS_NOTFOUND
) /* entry doesn't exist */
593 if (status
== NSS_STATUS_TRYAGAIN
)
595 /* The parser ran out of space */
596 fsetpos (ent
->stream
, &pos
);
604 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
608 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
610 return getpwent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
614 return NSS_STATUS_SUCCESS
;
618 static enum nss_status
619 internal_getpwent_r (struct passwd
*pw
, ent_t
*ent
, char *buffer
,
620 size_t buflen
, int *errnop
)
624 enum nss_status status
;
626 /* We are searching members in a netgroup */
627 /* Since this is not the first call, we don't need the group name */
628 status
= getpwent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
, buflen
,
630 if (status
== NSS_STATUS_RETURN
)
631 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
636 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
638 return getpwent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
643 _nss_compat_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
646 enum nss_status result
= NSS_STATUS_SUCCESS
;
648 __libc_lock_lock (lock
);
650 /* Be prepared that the setpwent function was not called before. */
652 init_nss_interface ();
654 if (ext_ent
.stream
== NULL
)
655 result
= internal_setpwent (&ext_ent
, 1, 1);
657 if (result
== NSS_STATUS_SUCCESS
)
658 result
= internal_getpwent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
660 __libc_lock_unlock (lock
);
665 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
666 static enum nss_status
667 internal_getpwnam_r (const char *name
, struct passwd
*result
, ent_t
*ent
,
668 char *buffer
, size_t buflen
, int *errnop
)
670 struct parser_data
*data
= (void *) buffer
;
680 /* We need at least 3 characters for one line. */
681 if (__glibc_unlikely (buflen
< 3))
685 return NSS_STATUS_TRYAGAIN
;
688 fgetpos (ent
->stream
, &pos
);
689 buffer
[buflen
- 1] = '\xff';
690 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
691 if (p
== NULL
&& feof_unlocked (ent
->stream
))
693 return NSS_STATUS_NOTFOUND
;
695 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
698 fsetpos (ent
->stream
, &pos
);
702 /* Terminate the line for any case. */
703 buffer
[buflen
- 1] = '\0';
705 /* Skip leading blanks. */
709 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
710 /* Parse the line. If it is invalid, loop to
711 get the next line of the file to parse. */
712 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
715 if (__glibc_unlikely (parse_res
== -1))
716 /* The parser ran out of space. */
719 /* This is a real entry. */
720 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
722 if (strcmp (result
->pw_name
, name
) == 0)
723 return NSS_STATUS_SUCCESS
;
729 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
730 && result
->pw_name
[2] != '\0')
732 if (innetgr (&result
->pw_name
[2], NULL
, name
, NULL
))
733 return NSS_STATUS_NOTFOUND
;
738 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
739 && result
->pw_name
[2] != '\0')
741 enum nss_status status
;
743 if (innetgr (&result
->pw_name
[2], NULL
, name
, NULL
))
745 status
= getpwnam_plususer (name
, result
, ent
, buffer
,
748 if (status
== NSS_STATUS_RETURN
)
757 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
758 && result
->pw_name
[1] != '@')
760 if (strcmp (&result
->pw_name
[1], name
) == 0)
761 return NSS_STATUS_NOTFOUND
;
767 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
768 && result
->pw_name
[1] != '@')
770 if (strcmp (name
, &result
->pw_name
[1]) == 0)
772 enum nss_status status
;
774 status
= getpwnam_plususer (name
, result
, ent
, buffer
, buflen
,
776 if (status
== NSS_STATUS_RETURN
)
777 /* We couldn't parse the entry */
778 return NSS_STATUS_NOTFOUND
;
785 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
787 enum nss_status status
;
789 status
= getpwnam_plususer (name
, result
, ent
,
790 buffer
, buflen
, errnop
);
791 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
793 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
794 return NSS_STATUS_NOTFOUND
;
799 return NSS_STATUS_SUCCESS
;
803 _nss_compat_getpwnam_r (const char *name
, struct passwd
*pwd
,
804 char *buffer
, size_t buflen
, int *errnop
)
806 enum nss_status result
;
807 ent_t ent
= { false, false, true, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 },
808 { NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
810 if (name
[0] == '-' || name
[0] == '+')
811 return NSS_STATUS_NOTFOUND
;
813 __libc_lock_lock (lock
);
816 init_nss_interface ();
818 __libc_lock_unlock (lock
);
820 result
= internal_setpwent (&ent
, 0, 0);
822 if (result
== NSS_STATUS_SUCCESS
)
823 result
= internal_getpwnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
825 internal_endpwent (&ent
);
830 /* This function handle the + entry in /etc/passwd for getpwuid */
831 static enum nss_status
832 getpwuid_plususer (uid_t uid
, struct passwd
*result
, char *buffer
,
833 size_t buflen
, int *errnop
)
840 return NSS_STATUS_UNAVAIL
;
842 memset (&pwd
, '\0', sizeof (struct passwd
));
844 copy_pwd_changes (&pwd
, result
, NULL
, 0);
846 plen
= pwd_need_buflen (&pwd
);
850 return NSS_STATUS_TRYAGAIN
;
852 p
= buffer
+ (buflen
- plen
);
855 if (nss_getpwuid_r (uid
, result
, buffer
, buflen
, errnop
) ==
858 copy_pwd_changes (result
, &pwd
, p
, plen
);
859 give_pwd_free (&pwd
);
860 /* We found the entry. */
861 return NSS_STATUS_SUCCESS
;
865 /* Give buffer the old len back */
867 give_pwd_free (&pwd
);
869 return NSS_STATUS_RETURN
;
872 /* Searches in /etc/passwd and the NSS subsystem for a special user id */
873 static enum nss_status
874 internal_getpwuid_r (uid_t uid
, struct passwd
*result
, ent_t
*ent
,
875 char *buffer
, size_t buflen
, int *errnop
)
877 struct parser_data
*data
= (void *) buffer
;
887 /* We need at least 3 characters for one line. */
888 if (__glibc_unlikely (buflen
< 3))
892 return NSS_STATUS_TRYAGAIN
;
895 fgetpos (ent
->stream
, &pos
);
896 buffer
[buflen
- 1] = '\xff';
897 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
898 if (p
== NULL
&& feof_unlocked (ent
->stream
))
899 return NSS_STATUS_NOTFOUND
;
901 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
904 fsetpos (ent
->stream
, &pos
);
908 /* Terminate the line for any case. */
909 buffer
[buflen
- 1] = '\0';
911 /* Skip leading blanks. */
915 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
916 /* Parse the line. If it is invalid, loop to
917 get the next line of the file to parse. */
918 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
921 if (__glibc_unlikely (parse_res
== -1))
922 /* The parser ran out of space. */
925 /* This is a real entry. */
926 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
928 if (result
->pw_uid
== uid
)
929 return NSS_STATUS_SUCCESS
;
935 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
936 && result
->pw_name
[2] != '\0')
938 /* -1, because we remove first two character of pw_name. */
939 size_t len
= strlen (result
->pw_name
) - 1;
941 enum nss_status status
;
943 memcpy (buf
, &result
->pw_name
[2], len
);
945 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
946 if (status
== NSS_STATUS_SUCCESS
&&
947 innetgr (buf
, NULL
, result
->pw_name
, NULL
))
948 return NSS_STATUS_NOTFOUND
;
954 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
955 && result
->pw_name
[2] != '\0')
957 /* -1, because we remove first two characters of pw_name. */
958 size_t len
= strlen (result
->pw_name
) - 1;
960 enum nss_status status
;
962 memcpy (buf
, &result
->pw_name
[2], len
);
964 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
966 if (status
== NSS_STATUS_RETURN
)
969 if (status
== NSS_STATUS_SUCCESS
)
971 if (innetgr (buf
, NULL
, result
->pw_name
, NULL
))
972 return NSS_STATUS_SUCCESS
;
974 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
975 return NSS_STATUS_NOTFOUND
;
983 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
984 && result
->pw_name
[1] != '@')
986 size_t len
= strlen (result
->pw_name
);
988 enum nss_status status
;
990 memcpy (buf
, &result
->pw_name
[1], len
);
992 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
993 if (status
== NSS_STATUS_SUCCESS
&&
994 innetgr (buf
, NULL
, result
->pw_name
, NULL
))
995 return NSS_STATUS_NOTFOUND
;
1000 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
1001 && result
->pw_name
[1] != '@')
1003 size_t len
= strlen (result
->pw_name
);
1005 enum nss_status status
;
1007 memcpy (buf
, &result
->pw_name
[1], len
);
1009 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
1011 if (status
== NSS_STATUS_RETURN
)
1014 if (status
== NSS_STATUS_SUCCESS
)
1016 if (strcmp (buf
, result
->pw_name
) == 0)
1017 return NSS_STATUS_SUCCESS
;
1019 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1020 return NSS_STATUS_NOTFOUND
;
1028 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
1030 enum nss_status status
;
1032 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
1033 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
1035 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1036 return NSS_STATUS_NOTFOUND
;
1041 return NSS_STATUS_SUCCESS
;
1045 _nss_compat_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
1046 char *buffer
, size_t buflen
, int *errnop
)
1048 enum nss_status result
;
1049 ent_t ent
= { false, false, true, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 },
1050 { NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
1052 __libc_lock_lock (lock
);
1055 init_nss_interface ();
1057 __libc_lock_unlock (lock
);
1059 result
= internal_setpwent (&ent
, 0, 0);
1061 if (result
== NSS_STATUS_SUCCESS
)
1062 result
= internal_getpwuid_r (uid
, pwd
, &ent
, buffer
, buflen
, errnop
);
1064 internal_endpwent (&ent
);
1070 /* Support routines for remembering -@netgroup and -user entries.
1071 The names are stored in a single string with `|' as separator. */
1073 blacklist_store_name (const char *name
, ent_t
*ent
)
1075 int namelen
= strlen (name
);
1078 /* first call, setup cache */
1079 if (ent
->blacklist
.size
== 0)
1081 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
1082 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
1083 if (ent
->blacklist
.data
== NULL
)
1085 ent
->blacklist
.data
[0] = '|';
1086 ent
->blacklist
.data
[1] = '\0';
1087 ent
->blacklist
.current
= 1;
1091 if (in_blacklist (name
, namelen
, ent
))
1092 return; /* no duplicates */
1094 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
1096 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
1097 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
1100 free (ent
->blacklist
.data
);
1101 ent
->blacklist
.size
= 0;
1104 ent
->blacklist
.data
= tmp
;
1108 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
1111 ent
->blacklist
.current
+= namelen
+ 1;
1116 /* Returns whether ent->blacklist contains name. */
1118 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
1120 char buf
[namelen
+ 3];
1123 if (ent
->blacklist
.data
== NULL
)
1127 cp
= stpcpy (&buf
[1], name
);
1130 return strstr (ent
->blacklist
.data
, buf
) != NULL
;