Prepare for glibc 2.34 release.
[glibc.git] / nss / nss_compat / compat-pwd.c
blob64d708ff63a8b02af6db66471cbd2e848230cbf7
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/>. */
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <netdb.h>
23 #include <nss.h>
24 #include <nsswitch.h>
25 #include <pwd.h>
26 #include <stdio_ext.h>
27 #include <string.h>
28 #include <libc-lock.h>
29 #include <kernel-features.h>
30 #include <nss_files.h>
32 #include "netgroup.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,
44 int *errnop);
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. */
50 #define ENTNAME pwent
51 #define STRUCTURE passwd
52 #define EXTERN_PARSER
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
58 struct blacklist_t
60 char *data;
61 int current;
62 int size;
65 struct ent_t
67 bool netgroup;
68 bool first;
69 bool files;
70 enum nss_status setent_status;
71 FILE *stream;
72 struct blacklist_t blacklist;
73 struct passwd pwd;
74 struct __netgrent netgrdata;
76 typedef struct ent_t ent_t;
78 static ent_t ext_ent = { false, false, true, NSS_STATUS_SUCCESS, NULL,
79 { NULL, 0, 0 },
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
90 hold the lock. */
91 static void
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");
104 static void
105 give_pwd_free (struct passwd *pwd)
107 free (pwd->pw_name);
108 free (pwd->pw_passwd);
109 free (pwd->pw_gecos);
110 free (pwd->pw_dir);
111 free (pwd->pw_shell);
113 memset (pwd, '\0', sizeof (struct passwd));
116 static size_t
117 pwd_need_buflen (struct passwd *pwd)
119 size_t len = 0;
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;
133 return len;
136 static void
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))
142 if (buffer == NULL)
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);
147 else
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))
158 if (buffer == NULL)
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);
163 else
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))
173 if (buffer == NULL)
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);
177 else
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))
188 if (buffer == NULL)
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);
193 else
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;
209 ent->files = true;
210 ent->setent_status = NSS_STATUS_SUCCESS;
212 /* If something was left over free it. */
213 if (ent->netgroup)
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';
222 else
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;
232 else
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);
240 return status;
244 enum nss_status
245 _nss_compat_setpwent (int stayopen)
247 enum nss_status result;
249 __libc_lock_lock (lock);
251 if (ni == NULL)
252 init_nss_interface ();
254 result = internal_setpwent (&ext_ent, stayopen, 1);
256 __libc_lock_unlock (lock);
258 return result;
262 static enum nss_status __attribute_warn_unused_result__
263 internal_endpwent (ent_t *ent)
265 if (ent->stream != NULL)
267 fclose (ent->stream);
268 ent->stream = NULL;
271 if (ent->netgroup)
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';
282 else
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. */
291 static void
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);
299 enum nss_status
300 _nss_compat_endpwent (void)
302 enum nss_status result;
304 __libc_lock_lock (lock);
306 if (endpwent_impl)
307 endpwent_impl ();
309 result = internal_endpwent (&ext_ent);
311 __libc_lock_unlock (lock);
313 return result;
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,
320 int *errnop)
322 char *curdomain = NULL, *host, *user, *domain, *p2;
323 int status;
324 size_t p2len;
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;
331 if (ent->first)
333 memset (&ent->netgrdata, 0, sizeof (struct __netgrent));
334 __internal_setnetgrent (group, &ent->netgrdata);
335 ent->first = false;
338 while (1)
340 status = __internal_getnetgrent_r (&host, &user, &domain,
341 &ent->netgrdata, buffer, buflen,
342 errnop);
343 if (status != 1)
345 __internal_endnetgrent (&ent->netgrdata);
346 ent->netgroup = 0;
347 give_pwd_free (&ent->pwd);
348 return NSS_STATUS_RETURN;
351 if (user == NULL || user[0] == '-')
352 continue;
354 if (domain != NULL)
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)
365 continue;
368 /* If name != NULL, we are called from getpwnam. */
369 if (name != NULL)
370 if (strcmp (user, name) != 0)
371 continue;
373 p2len = pwd_need_buflen (&ent->pwd);
374 if (p2len > buflen)
376 *errnop = ERANGE;
377 return NSS_STATUS_TRYAGAIN;
379 p2 = buffer + (buflen - p2len);
380 buflen -= p2len;
382 if (getpwnam_r_impl (user, result, buffer, buflen, errnop)
383 != NSS_STATUS_SUCCESS)
384 continue;
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);
392 break;
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;
405 char *p2;
406 size_t p2len;
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);
417 if (p2len > buflen)
419 *errnop = ERANGE;
420 return NSS_STATUS_TRYAGAIN;
422 p2 = buffer + (buflen - p2len);
423 buflen -= p2len;
425 if (ent->first)
426 ent->first = false;
430 if ((status = getpwent_r_impl (result, buffer, buflen, errnop))
431 != NSS_STATUS_SUCCESS)
432 return status;
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;
449 struct passwd pwd;
450 memset (&pwd, '\0', sizeof (struct passwd));
452 copy_pwd_changes (&pwd, result, NULL, 0);
454 size_t plen = pwd_need_buflen (&pwd);
455 if (plen > buflen)
457 *errnop = ERANGE;
458 return NSS_STATUS_TRYAGAIN;
460 char *p = buffer + (buflen - plen);
461 buflen -= plen;
463 enum nss_status status = getpwnam_r_impl (name, result, buffer, buflen,
464 errnop);
465 if (status != NSS_STATUS_SUCCESS)
466 return status;
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;
482 while (1)
484 fpos_t pos;
485 char *p;
486 int parse_res;
490 /* We need at least 3 characters for one line. */
491 if (__glibc_unlikely (buflen < 3))
493 erange:
494 *errnop = ERANGE;
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))
506 erange_reset:
507 fsetpos (ent->stream, &pos);
508 goto erange;
511 /* Terminate the line for any case. */
512 buffer[buflen - 1] = '\0';
514 /* Skip leading blanks. */
515 while (isspace (*p))
516 ++p;
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,
522 errnop)));
524 if (__glibc_unlikely (parse_res == -1))
525 /* The parser ran out of space. */
526 goto erange_reset;
528 if (result->pw_name[0] != '+' && result->pw_name[0] != '-')
529 /* This is a real entry. */
530 break;
532 /* -@netgroup */
533 if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
534 && result->pw_name[2] != '\0')
536 /* XXX Do not use fixed length buffer. */
537 char buf2[1024];
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);
550 continue;
553 /* +@netgroup */
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;
560 ent->first = true;
561 copy_pwd_changes (&ent->pwd, result, NULL, 0);
563 status = getpwent_next_nss_netgr (NULL, result, ent,
564 &result->pw_name[2],
565 buffer, buflen, errnop);
566 if (status == NSS_STATUS_RETURN)
567 continue;
568 else
569 return status;
572 /* -user */
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);
577 continue;
580 /* +user */
581 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
582 && result->pw_name[1] != '@')
584 size_t len = strlen (result->pw_name);
585 char buf[len];
586 enum nss_status status;
588 /* Store the User in the blacklist for the "+" at the end of
589 /etc/passwd */
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. */
596 break;
597 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
598 || status == NSS_STATUS_NOTFOUND) /* entry doesn't exist */
599 continue;
600 else
602 if (status == NSS_STATUS_TRYAGAIN)
604 /* The parser ran out of space */
605 fsetpos (ent->stream, &pos);
606 *errnop = ERANGE;
608 return status;
612 /* +:... */
613 if (result->pw_name[0] == '+' && result->pw_name[1] == '\0')
615 ent->files = false;
616 ent->first = true;
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)
631 if (ent->netgroup)
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,
638 errnop);
639 if (status == NSS_STATUS_RETURN)
640 return getpwent_next_file (pw, ent, buffer, buflen, errnop);
641 else
642 return status;
644 else if (ent->files)
645 return getpwent_next_file (pw, ent, buffer, buflen, errnop);
646 else
647 return getpwent_next_nss (pw, ent, buffer, buflen, errnop);
651 enum nss_status
652 _nss_compat_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
653 int *errnop)
655 enum nss_status result = NSS_STATUS_SUCCESS;
657 __libc_lock_lock (lock);
659 /* Be prepared that the setpwent function was not called before. */
660 if (ni == NULL)
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);
671 return result;
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;
681 while (1)
683 fpos_t pos;
684 char *p;
685 int parse_res;
689 /* We need at least 3 characters for one line. */
690 if (__glibc_unlikely (buflen < 3))
692 erange:
693 *errnop = ERANGE;
694 return NSS_STATUS_TRYAGAIN;
697 fgetpos (ent->stream, &pos);
698 buffer[buflen - 1] = '\xff';
699 p = fgets_unlocked (buffer, buflen, ent->stream);
700 if (p == NULL && feof_unlocked (ent->stream))
702 return NSS_STATUS_NOTFOUND;
704 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
706 erange_reset:
707 fsetpos (ent->stream, &pos);
708 goto erange;
711 /* Terminate the line for any case. */
712 buffer[buflen - 1] = '\0';
714 /* Skip leading blanks. */
715 while (isspace (*p))
716 ++p;
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,
722 errnop)));
724 if (__glibc_unlikely (parse_res == -1))
725 /* The parser ran out of space. */
726 goto erange_reset;
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;
733 else
734 continue;
737 /* -@netgroup */
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;
743 continue;
746 /* +@netgroup */
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,
755 buflen, errnop);
757 if (status == NSS_STATUS_RETURN)
758 continue;
760 return status;
762 continue;
765 /* -user */
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;
771 else
772 continue;
775 /* +user */
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,
784 errnop);
785 if (status == NSS_STATUS_RETURN)
786 /* We couldn't parse the entry */
787 return NSS_STATUS_NOTFOUND;
788 else
789 return status;
793 /* +:... */
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. */
801 break;
802 else if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
803 return NSS_STATUS_NOTFOUND;
804 else
805 return status;
808 return NSS_STATUS_SUCCESS;
811 enum nss_status
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);
824 if (ni == NULL)
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);
836 return result;
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)
844 struct passwd pwd;
845 char *p;
846 size_t plen;
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);
856 if (plen > buflen)
858 *errnop = ERANGE;
859 return NSS_STATUS_TRYAGAIN;
861 p = buffer + (buflen - plen);
862 buflen -= plen;
864 if (getpwuid_r_impl (uid, result, buffer, buflen, errnop) ==
865 NSS_STATUS_SUCCESS)
867 copy_pwd_changes (result, &pwd, p, plen);
868 give_pwd_free (&pwd);
869 /* We found the entry. */
870 return NSS_STATUS_SUCCESS;
872 else
874 /* Give buffer the old len back */
875 buflen += plen;
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;
888 while (1)
890 fpos_t pos;
891 char *p;
892 int parse_res;
896 /* We need at least 3 characters for one line. */
897 if (__glibc_unlikely (buflen < 3))
899 erange:
900 *errnop = ERANGE;
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))
912 erange_reset:
913 fsetpos (ent->stream, &pos);
914 goto erange;
917 /* Terminate the line for any case. */
918 buffer[buflen - 1] = '\0';
920 /* Skip leading blanks. */
921 while (isspace (*p))
922 ++p;
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,
928 errnop)));
930 if (__glibc_unlikely (parse_res == -1))
931 /* The parser ran out of space. */
932 goto erange_reset;
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;
939 else
940 continue;
943 /* -@netgroup */
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;
949 char buf[len];
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;
959 continue;
962 /* +@netgroup */
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;
968 char buf[len];
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)
976 continue;
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;
985 else
986 return status;
988 continue;
991 /* -user */
992 if (result->pw_name[0] == '-' && result->pw_name[1] != '\0'
993 && result->pw_name[1] != '@')
995 size_t len = strlen (result->pw_name);
996 char buf[len];
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;
1005 continue;
1008 /* +user */
1009 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
1010 && result->pw_name[1] != '@')
1012 size_t len = strlen (result->pw_name);
1013 char buf[len];
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)
1021 continue;
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;
1030 else
1031 return status;
1033 continue;
1036 /* +:... */
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. */
1043 break;
1044 else if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
1045 return NSS_STATUS_NOTFOUND;
1046 else
1047 return status;
1050 return NSS_STATUS_SUCCESS;
1053 enum nss_status
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);
1063 if (ni == NULL)
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);
1075 return result;
1079 /* Support routines for remembering -@netgroup and -user entries.
1080 The names are stored in a single string with `|' as separator. */
1081 static void
1082 blacklist_store_name (const char *name, ent_t *ent)
1084 int namelen = strlen (name);
1085 char *tmp;
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)
1093 return;
1094 ent->blacklist.data[0] = '|';
1095 ent->blacklist.data[1] = '\0';
1096 ent->blacklist.current = 1;
1098 else
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);
1107 if (tmp == NULL)
1109 free (ent->blacklist.data);
1110 ent->blacklist.size = 0;
1111 return;
1113 ent->blacklist.data = tmp;
1117 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
1118 *tmp++ = '|';
1119 *tmp = '\0';
1120 ent->blacklist.current += namelen + 1;
1122 return;
1125 /* Returns whether ent->blacklist contains name. */
1126 static bool
1127 in_blacklist (const char *name, int namelen, ent_t *ent)
1129 char buf[namelen + 3];
1130 char *cp;
1132 if (ent->blacklist.data == NULL)
1133 return false;
1135 buf[0] = '|';
1136 cp = stpcpy (&buf[1], name);
1137 *cp++ = '|';
1138 *cp = '\0';
1139 return strstr (ent->blacklist.data, buf) != NULL;