1 /* Copyright (C) 1996-2021 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 <https://www.gnu.org/licenses/>. */
26 #include <stdio_ext.h>
28 #include <libc-lock.h>
29 #include <kernel-features.h>
30 #include <nss_files.h>
33 #include "nisdomain.h"
35 NSS_DECLARE_MODULE_FUNCTIONS (compat
)
37 static nss_action_list ni
;
38 static enum nss_status (*setpwent_impl
) (int stayopen
);
39 static enum nss_status (*getpwnam_r_impl
) (const char *name
,
40 struct passwd
* pwd
, char *buffer
,
41 size_t buflen
, int *errnop
);
42 static enum nss_status (*getpwuid_r_impl
) (uid_t uid
, struct passwd
* pwd
,
43 char *buffer
, size_t buflen
,
45 static enum nss_status (*getpwent_r_impl
) (struct passwd
* pwd
, char *buffer
,
46 size_t buflen
, int *errnop
);
47 static enum nss_status (*endpwent_impl
) (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 /* Prototypes for local functions. */
86 static void blacklist_store_name (const char *, ent_t
*);
87 static bool in_blacklist (const char *, int, ent_t
*);
89 /* Initialize the NSS interface/functions. The calling function must
92 init_nss_interface (void)
94 if (__nss_database_get (nss_database_passwd_compat
, &ni
))
96 setpwent_impl
= __nss_lookup_function (ni
, "setpwent");
97 getpwnam_r_impl
= __nss_lookup_function (ni
, "getpwnam_r");
98 getpwuid_r_impl
= __nss_lookup_function (ni
, "getpwuid_r");
99 getpwent_r_impl
= __nss_lookup_function (ni
, "getpwent_r");
100 endpwent_impl
= __nss_lookup_function (ni
, "endpwent");
105 give_pwd_free (struct passwd
*pwd
)
108 free (pwd
->pw_passwd
);
109 free (pwd
->pw_gecos
);
111 free (pwd
->pw_shell
);
113 memset (pwd
, '\0', sizeof (struct passwd
));
117 pwd_need_buflen (struct passwd
*pwd
)
121 if (pwd
->pw_passwd
!= NULL
)
122 len
+= strlen (pwd
->pw_passwd
) + 1;
124 if (pwd
->pw_gecos
!= NULL
)
125 len
+= strlen (pwd
->pw_gecos
) + 1;
127 if (pwd
->pw_dir
!= NULL
)
128 len
+= strlen (pwd
->pw_dir
) + 1;
130 if (pwd
->pw_shell
!= NULL
)
131 len
+= strlen (pwd
->pw_shell
) + 1;
137 copy_pwd_changes (struct passwd
*dest
, struct passwd
*src
,
138 char *buffer
, size_t buflen
)
140 if (src
->pw_passwd
!= NULL
&& strlen (src
->pw_passwd
))
143 dest
->pw_passwd
= strdup (src
->pw_passwd
);
144 else if (dest
->pw_passwd
145 && strlen (dest
->pw_passwd
) >= strlen (src
->pw_passwd
))
146 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
149 dest
->pw_passwd
= buffer
;
150 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
151 buffer
+= strlen (dest
->pw_passwd
) + 1;
152 buflen
= buflen
- (strlen (dest
->pw_passwd
) + 1);
156 if (src
->pw_gecos
!= NULL
&& strlen (src
->pw_gecos
))
159 dest
->pw_gecos
= strdup (src
->pw_gecos
);
160 else if (dest
->pw_gecos
161 && strlen (dest
->pw_gecos
) >= strlen (src
->pw_gecos
))
162 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
165 dest
->pw_gecos
= buffer
;
166 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
167 buffer
+= strlen (dest
->pw_gecos
) + 1;
168 buflen
= buflen
- (strlen (dest
->pw_gecos
) + 1);
171 if (src
->pw_dir
!= NULL
&& strlen (src
->pw_dir
))
174 dest
->pw_dir
= strdup (src
->pw_dir
);
175 else if (dest
->pw_dir
&& strlen (dest
->pw_dir
) >= strlen (src
->pw_dir
))
176 strcpy (dest
->pw_dir
, src
->pw_dir
);
179 dest
->pw_dir
= buffer
;
180 strcpy (dest
->pw_dir
, src
->pw_dir
);
181 buffer
+= strlen (dest
->pw_dir
) + 1;
182 buflen
= buflen
- (strlen (dest
->pw_dir
) + 1);
186 if (src
->pw_shell
!= NULL
&& strlen (src
->pw_shell
))
189 dest
->pw_shell
= strdup (src
->pw_shell
);
190 else if (dest
->pw_shell
191 && strlen (dest
->pw_shell
) >= strlen (src
->pw_shell
))
192 strcpy (dest
->pw_shell
, src
->pw_shell
);
195 dest
->pw_shell
= buffer
;
196 strcpy (dest
->pw_shell
, src
->pw_shell
);
197 buffer
+= strlen (dest
->pw_shell
) + 1;
198 buflen
= buflen
- (strlen (dest
->pw_shell
) + 1);
203 static enum nss_status
204 internal_setpwent (ent_t
*ent
, int stayopen
, int needent
)
206 enum nss_status status
= NSS_STATUS_SUCCESS
;
208 ent
->first
= ent
->netgroup
= false;
210 ent
->setent_status
= NSS_STATUS_SUCCESS
;
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
= __nss_files_fopen ("/etc/passwd");
229 if (ent
->stream
== NULL
)
230 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
233 rewind (ent
->stream
);
235 give_pwd_free (&ent
->pwd
);
237 if (needent
&& status
== NSS_STATUS_SUCCESS
&& setpwent_impl
)
238 ent
->setent_status
= setpwent_impl (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 __attribute_warn_unused_result__
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
;
290 /* Like internal_endpwent, but preserve errno in all cases. */
292 internal_endpwent_noerror (ent_t
*ent
)
294 int saved_errno
= errno
;
295 enum nss_status unused
__attribute__ ((unused
)) = internal_endpwent (ent
);
296 __set_errno (saved_errno
);
300 _nss_compat_endpwent (void)
302 enum nss_status result
;
304 __libc_lock_lock (lock
);
309 result
= internal_endpwent (&ext_ent
);
311 __libc_lock_unlock (lock
);
317 static enum nss_status
318 getpwent_next_nss_netgr (const char *name
, struct passwd
*result
, ent_t
*ent
,
319 char *group
, char *buffer
, size_t buflen
,
322 char *curdomain
= NULL
, *host
, *user
, *domain
, *p2
;
326 /* Leave function if NSS module does not support getpwnam_r,
327 we need this function here. */
328 if (!getpwnam_r_impl
)
329 return NSS_STATUS_UNAVAIL
;
333 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
334 __internal_setnetgrent (group
, &ent
->netgrdata
);
340 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
341 &ent
->netgrdata
, buffer
, buflen
,
345 __internal_endnetgrent (&ent
->netgrdata
);
347 give_pwd_free (&ent
->pwd
);
348 return NSS_STATUS_RETURN
;
351 if (user
== NULL
|| user
[0] == '-')
356 if (curdomain
== NULL
357 && __nss_get_default_domain (&curdomain
) != 0)
359 __internal_endnetgrent (&ent
->netgrdata
);
360 ent
->netgroup
= false;
361 give_pwd_free (&ent
->pwd
);
362 return NSS_STATUS_UNAVAIL
;
364 if (strcmp (curdomain
, domain
) != 0)
368 /* If name != NULL, we are called from getpwnam. */
370 if (strcmp (user
, name
) != 0)
373 p2len
= pwd_need_buflen (&ent
->pwd
);
377 return NSS_STATUS_TRYAGAIN
;
379 p2
= buffer
+ (buflen
- p2len
);
382 if (getpwnam_r_impl (user
, result
, buffer
, buflen
, errnop
)
383 != NSS_STATUS_SUCCESS
)
386 if (!in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
388 /* Store the User in the blacklist for possible the "+" at the
389 end of /etc/passwd */
390 blacklist_store_name (result
->pw_name
, ent
);
391 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
396 return NSS_STATUS_SUCCESS
;
399 /* get the next user from NSS (+ entry) */
400 static enum nss_status
401 getpwent_next_nss (struct passwd
*result
, ent_t
*ent
, char *buffer
,
402 size_t buflen
, int *errnop
)
404 enum nss_status status
;
408 /* Return if NSS module does not support getpwent_r. */
409 if (!getpwent_r_impl
)
410 return NSS_STATUS_UNAVAIL
;
412 /* If the setpwent call failed, say so. */
413 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
414 return ent
->setent_status
;
416 p2len
= pwd_need_buflen (&ent
->pwd
);
420 return NSS_STATUS_TRYAGAIN
;
422 p2
= buffer
+ (buflen
- p2len
);
430 if ((status
= getpwent_r_impl (result
, buffer
, buflen
, errnop
))
431 != NSS_STATUS_SUCCESS
)
434 while (in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
));
436 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
438 return NSS_STATUS_SUCCESS
;
441 /* This function handle the +user entrys in /etc/passwd */
442 static enum nss_status
443 getpwnam_plususer (const char *name
, struct passwd
*result
, ent_t
*ent
,
444 char *buffer
, size_t buflen
, int *errnop
)
446 if (!getpwnam_r_impl
)
447 return NSS_STATUS_UNAVAIL
;
450 memset (&pwd
, '\0', sizeof (struct passwd
));
452 copy_pwd_changes (&pwd
, result
, NULL
, 0);
454 size_t plen
= pwd_need_buflen (&pwd
);
458 return NSS_STATUS_TRYAGAIN
;
460 char *p
= buffer
+ (buflen
- plen
);
463 enum nss_status status
= getpwnam_r_impl (name
, result
, buffer
, buflen
,
465 if (status
!= NSS_STATUS_SUCCESS
)
468 if (in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
469 return NSS_STATUS_NOTFOUND
;
471 copy_pwd_changes (result
, &pwd
, p
, plen
);
472 give_pwd_free (&pwd
);
473 /* We found the entry. */
474 return NSS_STATUS_SUCCESS
;
477 static enum nss_status
478 getpwent_next_file (struct passwd
*result
, ent_t
*ent
,
479 char *buffer
, size_t buflen
, int *errnop
)
481 struct parser_data
*data
= (void *) buffer
;
490 /* We need at least 3 characters for one line. */
491 if (__glibc_unlikely (buflen
< 3))
495 return NSS_STATUS_TRYAGAIN
;
498 fgetpos (ent
->stream
, &pos
);
499 buffer
[buflen
- 1] = '\xff';
500 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
501 if (p
== NULL
&& feof_unlocked (ent
->stream
))
502 return NSS_STATUS_NOTFOUND
;
504 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
507 fsetpos (ent
->stream
, &pos
);
511 /* Terminate the line for any case. */
512 buffer
[buflen
- 1] = '\0';
514 /* Skip leading blanks. */
518 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
519 /* Parse the line. If it is invalid, loop to
520 get the next line of the file to parse. */
521 || !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
524 if (__glibc_unlikely (parse_res
== -1))
525 /* The parser ran out of space. */
528 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
529 /* This is a real entry. */
533 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
534 && result
->pw_name
[2] != '\0')
536 /* XXX Do not use fixed length buffer. */
538 char *user
, *host
, *domain
;
539 struct __netgrent netgrdata
;
541 memset (&netgrdata
, 0, sizeof (struct __netgrent
));
542 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
543 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
544 buf2
, sizeof (buf2
), errnop
))
546 if (user
!= NULL
&& user
[0] != '-')
547 blacklist_store_name (user
, ent
);
549 __internal_endnetgrent (&netgrdata
);
554 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
555 && result
->pw_name
[2] != '\0')
557 enum nss_status status
;
559 ent
->netgroup
= true;
561 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
563 status
= getpwent_next_nss_netgr (NULL
, result
, ent
,
565 buffer
, buflen
, errnop
);
566 if (status
== NSS_STATUS_RETURN
)
573 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
574 && result
->pw_name
[1] != '@')
576 blacklist_store_name (&result
->pw_name
[1], ent
);
581 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
582 && result
->pw_name
[1] != '@')
584 size_t len
= strlen (result
->pw_name
);
586 enum nss_status status
;
588 /* Store the User in the blacklist for the "+" at the end of
590 memcpy (buf
, &result
->pw_name
[1], len
);
591 status
= getpwnam_plususer (&result
->pw_name
[1], result
, ent
,
592 buffer
, buflen
, errnop
);
593 blacklist_store_name (buf
, ent
);
595 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
597 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry */
598 || status
== NSS_STATUS_NOTFOUND
) /* entry doesn't exist */
602 if (status
== NSS_STATUS_TRYAGAIN
)
604 /* The parser ran out of space */
605 fsetpos (ent
->stream
, &pos
);
613 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
617 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
619 return getpwent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
623 return NSS_STATUS_SUCCESS
;
627 static enum nss_status
628 internal_getpwent_r (struct passwd
*pw
, ent_t
*ent
, char *buffer
,
629 size_t buflen
, int *errnop
)
633 enum nss_status status
;
635 /* We are searching members in a netgroup */
636 /* Since this is not the first call, we don't need the group name */
637 status
= getpwent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
, buflen
,
639 if (status
== NSS_STATUS_RETURN
)
640 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
645 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
647 return getpwent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
652 _nss_compat_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
655 enum nss_status result
= NSS_STATUS_SUCCESS
;
657 __libc_lock_lock (lock
);
659 /* Be prepared that the setpwent function was not called before. */
661 init_nss_interface ();
663 if (ext_ent
.stream
== NULL
)
664 result
= internal_setpwent (&ext_ent
, 1, 1);
666 if (result
== NSS_STATUS_SUCCESS
)
667 result
= internal_getpwent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
669 __libc_lock_unlock (lock
);
674 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
675 static enum nss_status
676 internal_getpwnam_r (const char *name
, struct passwd
*result
, ent_t
*ent
,
677 char *buffer
, size_t buflen
, int *errnop
)
679 struct parser_data
*data
= (void *) buffer
;
689 /* We need at least 3 characters for one line. */
690 if (__glibc_unlikely (buflen
< 3))
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
))
702 return NSS_STATUS_NOTFOUND
;
704 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
707 fsetpos (ent
->stream
, &pos
);
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
,
724 if (__glibc_unlikely (parse_res
== -1))
725 /* The parser ran out of space. */
728 /* This is a real entry. */
729 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
731 if (strcmp (result
->pw_name
, name
) == 0)
732 return NSS_STATUS_SUCCESS
;
738 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
739 && result
->pw_name
[2] != '\0')
741 if (innetgr (&result
->pw_name
[2], NULL
, name
, NULL
))
742 return NSS_STATUS_NOTFOUND
;
747 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
748 && result
->pw_name
[2] != '\0')
750 enum nss_status status
;
752 if (innetgr (&result
->pw_name
[2], NULL
, name
, NULL
))
754 status
= getpwnam_plususer (name
, result
, ent
, buffer
,
757 if (status
== NSS_STATUS_RETURN
)
766 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
767 && result
->pw_name
[1] != '@')
769 if (strcmp (&result
->pw_name
[1], name
) == 0)
770 return NSS_STATUS_NOTFOUND
;
776 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
777 && result
->pw_name
[1] != '@')
779 if (strcmp (name
, &result
->pw_name
[1]) == 0)
781 enum nss_status status
;
783 status
= getpwnam_plususer (name
, result
, ent
, buffer
, buflen
,
785 if (status
== NSS_STATUS_RETURN
)
786 /* We couldn't parse the entry */
787 return NSS_STATUS_NOTFOUND
;
794 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
796 enum nss_status status
;
798 status
= getpwnam_plususer (name
, result
, ent
,
799 buffer
, buflen
, errnop
);
800 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
802 else if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
803 return NSS_STATUS_NOTFOUND
;
808 return NSS_STATUS_SUCCESS
;
812 _nss_compat_getpwnam_r (const char *name
, struct passwd
*pwd
,
813 char *buffer
, size_t buflen
, int *errnop
)
815 enum nss_status result
;
816 ent_t ent
= { false, false, true, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 },
817 { NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
819 if (name
[0] == '-' || name
[0] == '+')
820 return NSS_STATUS_NOTFOUND
;
822 __libc_lock_lock (lock
);
825 init_nss_interface ();
827 __libc_lock_unlock (lock
);
829 result
= internal_setpwent (&ent
, 0, 0);
831 if (result
== NSS_STATUS_SUCCESS
)
832 result
= internal_getpwnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
834 internal_endpwent_noerror (&ent
);
839 /* This function handle the + entry in /etc/passwd for getpwuid */
840 static enum nss_status
841 getpwuid_plususer (uid_t uid
, struct passwd
*result
, char *buffer
,
842 size_t buflen
, int *errnop
)
848 if (!getpwuid_r_impl
)
849 return NSS_STATUS_UNAVAIL
;
851 memset (&pwd
, '\0', sizeof (struct passwd
));
853 copy_pwd_changes (&pwd
, result
, NULL
, 0);
855 plen
= pwd_need_buflen (&pwd
);
859 return NSS_STATUS_TRYAGAIN
;
861 p
= buffer
+ (buflen
- plen
);
864 if (getpwuid_r_impl (uid
, result
, buffer
, buflen
, errnop
) ==
867 copy_pwd_changes (result
, &pwd
, p
, plen
);
868 give_pwd_free (&pwd
);
869 /* We found the entry. */
870 return NSS_STATUS_SUCCESS
;
874 /* Give buffer the old len back */
876 give_pwd_free (&pwd
);
878 return NSS_STATUS_RETURN
;
881 /* Searches in /etc/passwd and the NSS subsystem for a special user id */
882 static enum nss_status
883 internal_getpwuid_r (uid_t uid
, struct passwd
*result
, ent_t
*ent
,
884 char *buffer
, size_t buflen
, int *errnop
)
886 struct parser_data
*data
= (void *) buffer
;
896 /* We need at least 3 characters for one line. */
897 if (__glibc_unlikely (buflen
< 3))
901 return NSS_STATUS_TRYAGAIN
;
904 fgetpos (ent
->stream
, &pos
);
905 buffer
[buflen
- 1] = '\xff';
906 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
907 if (p
== NULL
&& feof_unlocked (ent
->stream
))
908 return NSS_STATUS_NOTFOUND
;
910 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
913 fsetpos (ent
->stream
, &pos
);
917 /* Terminate the line for any case. */
918 buffer
[buflen
- 1] = '\0';
920 /* Skip leading blanks. */
924 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
925 /* Parse the line. If it is invalid, loop to
926 get the next line of the file to parse. */
927 || !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
930 if (__glibc_unlikely (parse_res
== -1))
931 /* The parser ran out of space. */
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
= { false, false, true, NSS_STATUS_SUCCESS
, 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, 0);
1070 if (result
== NSS_STATUS_SUCCESS
)
1071 result
= internal_getpwuid_r (uid
, pwd
, &ent
, buffer
, buflen
, errnop
);
1073 internal_endpwent_noerror (&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 whether ent->blacklist contains name. */
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
;