1 /* Copyright (C) 1996-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
25 #include <stdio_ext.h>
27 #include <libc-lock.h>
28 #include <kernel-features.h>
29 #include <nss_files.h>
32 #include "nisdomain.h"
34 NSS_DECLARE_MODULE_FUNCTIONS (compat
)
36 static nss_action_list ni
;
37 static enum nss_status (*setspent_impl
) (int stayopen
);
38 static enum nss_status (*getspnam_r_impl
) (const char *name
, struct spwd
* sp
,
39 char *buffer
, size_t buflen
,
41 static enum nss_status (*getspent_r_impl
) (struct spwd
* sp
, char *buffer
,
42 size_t buflen
, int *errnop
);
43 static enum nss_status (*endspent_impl
) (void);
45 /* Get the declaration of the parser function. */
47 #define STRUCTURE spwd
49 #include <nss/nss_files/files-parse.c>
51 /* Structure for remembering -@netgroup and -user members ... */
52 #define BLACKLIST_INITIAL_SIZE 512
53 #define BLACKLIST_INCREMENT 256
66 enum nss_status setent_status
;
68 struct blacklist_t blacklist
;
70 struct __netgrent netgrdata
;
72 typedef struct ent_t ent_t
;
74 static ent_t ext_ent
= { false, true, false, NSS_STATUS_SUCCESS
, NULL
,
76 { NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
78 /* Protect global state against multiple changers. */
79 __libc_lock_define_initialized (static, lock
)
81 /* Prototypes for local functions. */
82 static void blacklist_store_name (const char *, ent_t
*);
83 static bool in_blacklist (const char *, int, ent_t
*);
85 /* Initialize the NSS interface/functions. The calling function must
88 init_nss_interface (void)
90 if (__nss_database_get (nss_database_shadow_compat
, &ni
))
92 setspent_impl
= __nss_lookup_function (ni
, "setspent");
93 getspnam_r_impl
= __nss_lookup_function (ni
, "getspnam_r");
94 getspent_r_impl
= __nss_lookup_function (ni
, "getspent_r");
95 endspent_impl
= __nss_lookup_function (ni
, "endspent");
100 give_spwd_free (struct spwd
*pwd
)
105 memset (pwd
, '\0', sizeof (struct spwd
));
113 spwd_need_buflen (struct spwd
*pwd
)
117 if (pwd
->sp_pwdp
!= NULL
)
118 len
+= strlen (pwd
->sp_pwdp
) + 1;
124 copy_spwd_changes (struct spwd
*dest
, struct spwd
*src
,
125 char *buffer
, size_t buflen
)
127 if (src
->sp_pwdp
!= NULL
&& strlen (src
->sp_pwdp
))
130 dest
->sp_pwdp
= strdup (src
->sp_pwdp
);
131 else if (dest
->sp_pwdp
132 && strlen (dest
->sp_pwdp
) >= strlen (src
->sp_pwdp
))
133 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
136 dest
->sp_pwdp
= buffer
;
137 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
138 buffer
+= strlen (dest
->sp_pwdp
) + 1;
139 buflen
= buflen
- (strlen (dest
->sp_pwdp
) + 1);
142 if (src
->sp_lstchg
!= 0)
143 dest
->sp_lstchg
= src
->sp_lstchg
;
144 if (src
->sp_min
!= 0)
145 dest
->sp_min
= src
->sp_min
;
146 if (src
->sp_max
!= 0)
147 dest
->sp_max
= src
->sp_max
;
148 if (src
->sp_warn
!= -1)
149 dest
->sp_warn
= src
->sp_warn
;
150 if (src
->sp_inact
!= -1)
151 dest
->sp_inact
= src
->sp_inact
;
152 if (src
->sp_expire
!= -1)
153 dest
->sp_expire
= src
->sp_expire
;
154 if (src
->sp_flag
!= ~0ul)
155 dest
->sp_flag
= src
->sp_flag
;
158 static enum nss_status
159 internal_setspent (ent_t
*ent
, int stayopen
, int needent
)
161 enum nss_status status
= NSS_STATUS_SUCCESS
;
163 ent
->first
= ent
->netgroup
= 0;
166 /* If something was left over free it. */
168 __internal_endnetgrent (&ent
->netgrdata
);
170 if (ent
->blacklist
.data
!= NULL
)
172 ent
->blacklist
.current
= 1;
173 ent
->blacklist
.data
[0] = '|';
174 ent
->blacklist
.data
[1] = '\0';
177 ent
->blacklist
.current
= 0;
179 if (ent
->stream
== NULL
)
181 ent
->stream
= __nss_files_fopen ("/etc/shadow");
183 if (ent
->stream
== NULL
)
184 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
187 rewind (ent
->stream
);
189 give_spwd_free (&ent
->pwd
);
191 if (needent
&& status
== NSS_STATUS_SUCCESS
&& setspent_impl
)
192 ent
->setent_status
= setspent_impl (stayopen
);
199 _nss_compat_setspent (int stayopen
)
201 enum nss_status result
;
203 __libc_lock_lock (lock
);
206 init_nss_interface ();
208 result
= internal_setspent (&ext_ent
, stayopen
, 1);
210 __libc_lock_unlock (lock
);
216 static enum nss_status __attribute_warn_unused_result__
217 internal_endspent (ent_t
*ent
)
219 if (ent
->stream
!= NULL
)
221 fclose (ent
->stream
);
226 __internal_endnetgrent (&ent
->netgrdata
);
228 ent
->first
= ent
->netgroup
= false;
231 if (ent
->blacklist
.data
!= NULL
)
233 ent
->blacklist
.current
= 1;
234 ent
->blacklist
.data
[0] = '|';
235 ent
->blacklist
.data
[1] = '\0';
238 ent
->blacklist
.current
= 0;
240 give_spwd_free (&ent
->pwd
);
242 return NSS_STATUS_SUCCESS
;
245 /* Like internal_endspent, but preserve errno in all cases. */
247 internal_endspent_noerror (ent_t
*ent
)
249 int saved_errno
= errno
;
250 enum nss_status unused
__attribute__ ((unused
)) = internal_endspent (ent
);
251 __set_errno (saved_errno
);
255 _nss_compat_endspent (void)
257 enum nss_status result
;
259 __libc_lock_lock (lock
);
264 result
= internal_endspent (&ext_ent
);
266 __libc_lock_unlock (lock
);
271 static enum nss_status
272 getspent_next_nss_netgr (const char *name
, struct spwd
*result
, ent_t
*ent
,
273 char *group
, char *buffer
, size_t buflen
,
276 char *curdomain
= NULL
, *host
, *user
, *domain
, *p2
;
279 if (!getspnam_r_impl
)
280 return NSS_STATUS_UNAVAIL
;
282 /* If the setpwent call failed, say so. */
283 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
284 return ent
->setent_status
;
288 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
289 __internal_setnetgrent (group
, &ent
->netgrdata
);
295 enum nss_status status
;
297 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
298 &ent
->netgrdata
, buffer
, buflen
,
302 __internal_endnetgrent (&ent
->netgrdata
);
303 ent
->netgroup
= false;
304 give_spwd_free (&ent
->pwd
);
305 return NSS_STATUS_RETURN
;
308 if (user
== NULL
|| user
[0] == '-')
313 if (curdomain
== NULL
314 && __nss_get_default_domain (&curdomain
) != 0)
316 __internal_endnetgrent (&ent
->netgrdata
);
317 ent
->netgroup
= false;
318 give_spwd_free (&ent
->pwd
);
319 return NSS_STATUS_UNAVAIL
;
321 if (strcmp (curdomain
, domain
) != 0)
325 /* If name != NULL, we are called from getpwnam */
327 if (strcmp (user
, name
) != 0)
330 p2len
= spwd_need_buflen (&ent
->pwd
);
334 return NSS_STATUS_TRYAGAIN
;
336 p2
= buffer
+ (buflen
- p2len
);
339 if (getspnam_r_impl (user
, result
, buffer
, buflen
, errnop
)
340 != NSS_STATUS_SUCCESS
)
343 if (!in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
345 /* Store the User in the blacklist for possible the "+" at the
346 end of /etc/passwd */
347 blacklist_store_name (result
->sp_namp
, ent
);
348 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
353 return NSS_STATUS_SUCCESS
;
357 static enum nss_status
358 getspent_next_nss (struct spwd
*result
, ent_t
*ent
,
359 char *buffer
, size_t buflen
, int *errnop
)
361 enum nss_status status
;
365 if (!getspent_r_impl
)
366 return NSS_STATUS_UNAVAIL
;
368 p2len
= spwd_need_buflen (&ent
->pwd
);
372 return NSS_STATUS_TRYAGAIN
;
374 p2
= buffer
+ (buflen
- p2len
);
378 if ((status
= getspent_r_impl (result
, buffer
, buflen
, errnop
))
379 != NSS_STATUS_SUCCESS
)
382 while (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
));
384 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
386 return NSS_STATUS_SUCCESS
;
390 /* This function handle the +user entrys in /etc/shadow */
391 static enum nss_status
392 getspnam_plususer (const char *name
, struct spwd
*result
, ent_t
*ent
,
393 char *buffer
, size_t buflen
, int *errnop
)
395 if (!getspnam_r_impl
)
396 return NSS_STATUS_UNAVAIL
;
399 memset (&pwd
, '\0', sizeof (struct spwd
));
405 copy_spwd_changes (&pwd
, result
, NULL
, 0);
407 size_t plen
= spwd_need_buflen (&pwd
);
411 return NSS_STATUS_TRYAGAIN
;
413 char *p
= buffer
+ (buflen
- plen
);
416 enum nss_status status
= getspnam_r_impl (name
, result
, buffer
, buflen
,
418 if (status
!= NSS_STATUS_SUCCESS
)
421 if (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
422 return NSS_STATUS_NOTFOUND
;
424 copy_spwd_changes (result
, &pwd
, p
, plen
);
425 give_spwd_free (&pwd
);
426 /* We found the entry. */
427 return NSS_STATUS_SUCCESS
;
431 static enum nss_status
432 getspent_next_file (struct spwd
*result
, ent_t
*ent
,
433 char *buffer
, size_t buflen
, int *errnop
)
435 struct parser_data
*data
= (void *) buffer
;
444 /* We need at least 3 characters for one line. */
445 if (__glibc_unlikely (buflen
< 3))
449 return NSS_STATUS_TRYAGAIN
;
452 fgetpos (ent
->stream
, &pos
);
453 buffer
[buflen
- 1] = '\xff';
454 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
455 if (p
== NULL
&& feof_unlocked (ent
->stream
))
456 return NSS_STATUS_NOTFOUND
;
458 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
461 fsetpos (ent
->stream
, &pos
);
465 /* Skip leading blanks. */
469 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
470 /* Parse the line. If it is invalid, loop to
471 get the next line of the file to parse. */
472 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
,
475 if (__glibc_unlikely (parse_res
== -1))
476 /* The parser ran out of space. */
479 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
480 /* This is a real entry. */
484 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
485 && result
->sp_namp
[2] != '\0')
487 /* XXX Do not use fixed length buffers. */
489 char *user
, *host
, *domain
;
490 struct __netgrent netgrdata
;
492 memset (&netgrdata
, 0, sizeof (struct __netgrent
));
493 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
494 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
495 &netgrdata
, buf2
, sizeof (buf2
),
498 if (user
!= NULL
&& user
[0] != '-')
499 blacklist_store_name (user
, ent
);
501 __internal_endnetgrent (&netgrdata
);
506 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
507 && result
->sp_namp
[2] != '\0')
511 ent
->netgroup
= true;
513 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
515 status
= getspent_next_nss_netgr (NULL
, result
, ent
,
517 buffer
, buflen
, errnop
);
518 if (status
== NSS_STATUS_RETURN
)
525 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
526 && result
->sp_namp
[1] != '@')
528 blacklist_store_name (&result
->sp_namp
[1], ent
);
533 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
534 && result
->sp_namp
[1] != '@')
536 size_t len
= strlen (result
->sp_namp
);
538 enum nss_status status
;
540 /* Store the User in the blacklist for the "+" at the end of
542 memcpy (buf
, &result
->sp_namp
[1], len
);
543 status
= getspnam_plususer (&result
->sp_namp
[1], result
, ent
,
544 buffer
, buflen
, errnop
);
545 blacklist_store_name (buf
, ent
);
547 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
549 /* We couldn't parse the entry */
550 else if (status
== NSS_STATUS_RETURN
551 /* entry doesn't exist */
552 || status
== NSS_STATUS_NOTFOUND
)
556 if (status
== NSS_STATUS_TRYAGAIN
)
558 fsetpos (ent
->stream
, &pos
);
566 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
570 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
572 return getspent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
576 return NSS_STATUS_SUCCESS
;
580 static enum nss_status
581 internal_getspent_r (struct spwd
*pw
, ent_t
*ent
,
582 char *buffer
, size_t buflen
, int *errnop
)
586 enum nss_status status
;
588 /* We are searching members in a netgroup */
589 /* Since this is not the first call, we don't need the group name */
590 status
= getspent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
,
593 if (status
== NSS_STATUS_RETURN
)
594 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
599 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
601 return getspent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
606 _nss_compat_getspent_r (struct spwd
*pwd
, char *buffer
, size_t buflen
,
609 enum nss_status result
= NSS_STATUS_SUCCESS
;
611 __libc_lock_lock (lock
);
613 /* Be prepared that the setpwent function was not called before. */
615 init_nss_interface ();
617 if (ext_ent
.stream
== NULL
)
618 result
= internal_setspent (&ext_ent
, 1, 1);
620 if (result
== NSS_STATUS_SUCCESS
)
621 result
= internal_getspent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
623 __libc_lock_unlock (lock
);
629 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
630 static enum nss_status
631 internal_getspnam_r (const char *name
, struct spwd
*result
, ent_t
*ent
,
632 char *buffer
, size_t buflen
, int *errnop
)
634 struct parser_data
*data
= (void *) buffer
;
644 /* We need at least 3 characters for one line. */
645 if (__glibc_unlikely (buflen
< 3))
649 return NSS_STATUS_TRYAGAIN
;
652 fgetpos (ent
->stream
, &pos
);
653 buffer
[buflen
- 1] = '\xff';
654 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
655 if (p
== NULL
&& feof_unlocked (ent
->stream
))
656 return NSS_STATUS_NOTFOUND
;
658 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
661 fsetpos (ent
->stream
, &pos
);
665 /* Terminate the line for any case. */
666 buffer
[buflen
- 1] = '\0';
668 /* Skip leading blanks. */
672 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
673 /* Parse the line. If it is invalid, loop to
674 get the next line of the file to parse. */
675 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
,
678 if (__glibc_unlikely (parse_res
== -1))
679 /* The parser ran out of space. */
682 /* This is a real entry. */
683 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
685 if (strcmp (result
->sp_namp
, name
) == 0)
686 return NSS_STATUS_SUCCESS
;
692 /* If the loaded NSS module does not support this service, add
693 all users from a +@netgroup entry to the blacklist, too. */
694 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
695 && result
->sp_namp
[2] != '\0')
697 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
698 return NSS_STATUS_NOTFOUND
;
703 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
704 && result
->sp_namp
[2] != '\0')
706 enum nss_status status
;
708 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
710 status
= getspnam_plususer (name
, result
, ent
, buffer
,
713 if (status
== NSS_STATUS_RETURN
)
722 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
723 && result
->sp_namp
[1] != '@')
725 if (strcmp (&result
->sp_namp
[1], name
) == 0)
726 return NSS_STATUS_NOTFOUND
;
732 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
733 && result
->sp_namp
[1] != '@')
735 if (strcmp (name
, &result
->sp_namp
[1]) == 0)
737 enum nss_status status
;
739 status
= getspnam_plususer (name
, result
, ent
,
740 buffer
, buflen
, errnop
);
742 if (status
== NSS_STATUS_RETURN
)
743 /* We couldn't parse the entry */
744 return NSS_STATUS_NOTFOUND
;
751 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
753 enum nss_status status
;
755 status
= getspnam_plususer (name
, result
, ent
,
756 buffer
, buflen
, errnop
);
758 if (status
== NSS_STATUS_SUCCESS
)
759 /* We found the entry. */
761 else if (status
== NSS_STATUS_RETURN
)
762 /* We couldn't parse the entry */
763 return NSS_STATUS_NOTFOUND
;
768 return NSS_STATUS_SUCCESS
;
773 _nss_compat_getspnam_r (const char *name
, struct spwd
*pwd
,
774 char *buffer
, size_t buflen
, int *errnop
)
776 enum nss_status result
;
777 ent_t ent
= { false, true, false, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0},
778 { NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
780 if (name
[0] == '-' || name
[0] == '+')
781 return NSS_STATUS_NOTFOUND
;
783 __libc_lock_lock (lock
);
786 init_nss_interface ();
788 __libc_lock_unlock (lock
);
790 result
= internal_setspent (&ent
, 0, 0);
792 if (result
== NSS_STATUS_SUCCESS
)
793 result
= internal_getspnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
795 internal_endspent_noerror (&ent
);
801 /* Support routines for remembering -@netgroup and -user entries.
802 The names are stored in a single string with `|' as separator. */
804 blacklist_store_name (const char *name
, ent_t
*ent
)
806 int namelen
= strlen (name
);
809 /* first call, setup cache */
810 if (ent
->blacklist
.size
== 0)
812 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
813 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
814 if (ent
->blacklist
.data
== NULL
)
816 ent
->blacklist
.data
[0] = '|';
817 ent
->blacklist
.data
[1] = '\0';
818 ent
->blacklist
.current
= 1;
822 if (in_blacklist (name
, namelen
, ent
))
823 return; /* no duplicates */
825 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
827 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
828 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
831 free (ent
->blacklist
.data
);
832 ent
->blacklist
.size
= 0;
835 ent
->blacklist
.data
= tmp
;
839 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
842 ent
->blacklist
.current
+= namelen
+ 1;
848 /* Returns whether ent->blacklist contains name. */
850 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
852 char buf
[namelen
+ 3];
855 if (ent
->blacklist
.data
== NULL
)
859 cp
= stpcpy (&buf
[1], name
);
862 return strstr (ent
->blacklist
.data
, buf
) != NULL
;