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 (*setspent_impl
) (int stayopen
);
39 static enum nss_status (*getspnam_r_impl
) (const char *name
, struct spwd
* sp
,
40 char *buffer
, size_t buflen
,
42 static enum nss_status (*getspent_r_impl
) (struct spwd
* sp
, char *buffer
,
43 size_t buflen
, int *errnop
);
44 static enum nss_status (*endspent_impl
) (void);
46 /* Get the declaration of the parser function. */
48 #define STRUCTURE spwd
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, true, false, NSS_STATUS_SUCCESS
, NULL
,
77 { NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
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_get (nss_database_shadow_compat
, &ni
))
93 setspent_impl
= __nss_lookup_function (ni
, "setspent");
94 getspnam_r_impl
= __nss_lookup_function (ni
, "getspnam_r");
95 getspent_r_impl
= __nss_lookup_function (ni
, "getspent_r");
96 endspent_impl
= __nss_lookup_function (ni
, "endspent");
101 give_spwd_free (struct spwd
*pwd
)
106 memset (pwd
, '\0', sizeof (struct spwd
));
114 spwd_need_buflen (struct spwd
*pwd
)
118 if (pwd
->sp_pwdp
!= NULL
)
119 len
+= strlen (pwd
->sp_pwdp
) + 1;
125 copy_spwd_changes (struct spwd
*dest
, struct spwd
*src
,
126 char *buffer
, size_t buflen
)
128 if (src
->sp_pwdp
!= NULL
&& strlen (src
->sp_pwdp
))
131 dest
->sp_pwdp
= strdup (src
->sp_pwdp
);
132 else if (dest
->sp_pwdp
133 && strlen (dest
->sp_pwdp
) >= strlen (src
->sp_pwdp
))
134 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
137 dest
->sp_pwdp
= buffer
;
138 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
139 buffer
+= strlen (dest
->sp_pwdp
) + 1;
140 buflen
= buflen
- (strlen (dest
->sp_pwdp
) + 1);
143 if (src
->sp_lstchg
!= 0)
144 dest
->sp_lstchg
= src
->sp_lstchg
;
145 if (src
->sp_min
!= 0)
146 dest
->sp_min
= src
->sp_min
;
147 if (src
->sp_max
!= 0)
148 dest
->sp_max
= src
->sp_max
;
149 if (src
->sp_warn
!= -1)
150 dest
->sp_warn
= src
->sp_warn
;
151 if (src
->sp_inact
!= -1)
152 dest
->sp_inact
= src
->sp_inact
;
153 if (src
->sp_expire
!= -1)
154 dest
->sp_expire
= src
->sp_expire
;
155 if (src
->sp_flag
!= ~0ul)
156 dest
->sp_flag
= src
->sp_flag
;
159 static enum nss_status
160 internal_setspent (ent_t
*ent
, int stayopen
, int needent
)
162 enum nss_status status
= NSS_STATUS_SUCCESS
;
164 ent
->first
= ent
->netgroup
= 0;
167 /* If something was left over free it. */
169 __internal_endnetgrent (&ent
->netgrdata
);
171 if (ent
->blacklist
.data
!= NULL
)
173 ent
->blacklist
.current
= 1;
174 ent
->blacklist
.data
[0] = '|';
175 ent
->blacklist
.data
[1] = '\0';
178 ent
->blacklist
.current
= 0;
180 if (ent
->stream
== NULL
)
182 ent
->stream
= __nss_files_fopen ("/etc/shadow");
184 if (ent
->stream
== NULL
)
185 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
188 rewind (ent
->stream
);
190 give_spwd_free (&ent
->pwd
);
192 if (needent
&& status
== NSS_STATUS_SUCCESS
&& setspent_impl
)
193 ent
->setent_status
= setspent_impl (stayopen
);
200 _nss_compat_setspent (int stayopen
)
202 enum nss_status result
;
204 __libc_lock_lock (lock
);
207 init_nss_interface ();
209 result
= internal_setspent (&ext_ent
, stayopen
, 1);
211 __libc_lock_unlock (lock
);
217 static enum nss_status __attribute_warn_unused_result__
218 internal_endspent (ent_t
*ent
)
220 if (ent
->stream
!= NULL
)
222 fclose (ent
->stream
);
227 __internal_endnetgrent (&ent
->netgrdata
);
229 ent
->first
= ent
->netgroup
= false;
232 if (ent
->blacklist
.data
!= NULL
)
234 ent
->blacklist
.current
= 1;
235 ent
->blacklist
.data
[0] = '|';
236 ent
->blacklist
.data
[1] = '\0';
239 ent
->blacklist
.current
= 0;
241 give_spwd_free (&ent
->pwd
);
243 return NSS_STATUS_SUCCESS
;
246 /* Like internal_endspent, but preserve errno in all cases. */
248 internal_endspent_noerror (ent_t
*ent
)
250 int saved_errno
= errno
;
251 enum nss_status unused
__attribute__ ((unused
)) = internal_endspent (ent
);
252 __set_errno (saved_errno
);
256 _nss_compat_endspent (void)
258 enum nss_status result
;
260 __libc_lock_lock (lock
);
265 result
= internal_endspent (&ext_ent
);
267 __libc_lock_unlock (lock
);
272 static enum nss_status
273 getspent_next_nss_netgr (const char *name
, struct spwd
*result
, ent_t
*ent
,
274 char *group
, char *buffer
, size_t buflen
,
277 char *curdomain
= NULL
, *host
, *user
, *domain
, *p2
;
280 if (!getspnam_r_impl
)
281 return NSS_STATUS_UNAVAIL
;
283 /* If the setpwent call failed, say so. */
284 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
285 return ent
->setent_status
;
289 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
290 __internal_setnetgrent (group
, &ent
->netgrdata
);
296 enum nss_status status
;
298 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
299 &ent
->netgrdata
, buffer
, buflen
,
303 __internal_endnetgrent (&ent
->netgrdata
);
304 ent
->netgroup
= false;
305 give_spwd_free (&ent
->pwd
);
306 return NSS_STATUS_RETURN
;
309 if (user
== NULL
|| user
[0] == '-')
314 if (curdomain
== NULL
315 && __nss_get_default_domain (&curdomain
) != 0)
317 __internal_endnetgrent (&ent
->netgrdata
);
318 ent
->netgroup
= false;
319 give_spwd_free (&ent
->pwd
);
320 return NSS_STATUS_UNAVAIL
;
322 if (strcmp (curdomain
, domain
) != 0)
326 /* If name != NULL, we are called from getpwnam */
328 if (strcmp (user
, name
) != 0)
331 p2len
= spwd_need_buflen (&ent
->pwd
);
335 return NSS_STATUS_TRYAGAIN
;
337 p2
= buffer
+ (buflen
- p2len
);
340 if (getspnam_r_impl (user
, result
, buffer
, buflen
, errnop
)
341 != NSS_STATUS_SUCCESS
)
344 if (!in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
346 /* Store the User in the blacklist for possible the "+" at the
347 end of /etc/passwd */
348 blacklist_store_name (result
->sp_namp
, ent
);
349 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
354 return NSS_STATUS_SUCCESS
;
358 static enum nss_status
359 getspent_next_nss (struct spwd
*result
, ent_t
*ent
,
360 char *buffer
, size_t buflen
, int *errnop
)
362 enum nss_status status
;
366 if (!getspent_r_impl
)
367 return NSS_STATUS_UNAVAIL
;
369 p2len
= spwd_need_buflen (&ent
->pwd
);
373 return NSS_STATUS_TRYAGAIN
;
375 p2
= buffer
+ (buflen
- p2len
);
379 if ((status
= getspent_r_impl (result
, buffer
, buflen
, errnop
))
380 != NSS_STATUS_SUCCESS
)
383 while (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
));
385 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
387 return NSS_STATUS_SUCCESS
;
391 /* This function handle the +user entrys in /etc/shadow */
392 static enum nss_status
393 getspnam_plususer (const char *name
, struct spwd
*result
, ent_t
*ent
,
394 char *buffer
, size_t buflen
, int *errnop
)
396 if (!getspnam_r_impl
)
397 return NSS_STATUS_UNAVAIL
;
400 memset (&pwd
, '\0', sizeof (struct spwd
));
406 copy_spwd_changes (&pwd
, result
, NULL
, 0);
408 size_t plen
= spwd_need_buflen (&pwd
);
412 return NSS_STATUS_TRYAGAIN
;
414 char *p
= buffer
+ (buflen
- plen
);
417 enum nss_status status
= getspnam_r_impl (name
, result
, buffer
, buflen
,
419 if (status
!= NSS_STATUS_SUCCESS
)
422 if (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
423 return NSS_STATUS_NOTFOUND
;
425 copy_spwd_changes (result
, &pwd
, p
, plen
);
426 give_spwd_free (&pwd
);
427 /* We found the entry. */
428 return NSS_STATUS_SUCCESS
;
432 static enum nss_status
433 getspent_next_file (struct spwd
*result
, ent_t
*ent
,
434 char *buffer
, size_t buflen
, int *errnop
)
436 struct parser_data
*data
= (void *) buffer
;
445 /* We need at least 3 characters for one line. */
446 if (__glibc_unlikely (buflen
< 3))
450 return NSS_STATUS_TRYAGAIN
;
453 fgetpos (ent
->stream
, &pos
);
454 buffer
[buflen
- 1] = '\xff';
455 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
456 if (p
== NULL
&& feof_unlocked (ent
->stream
))
457 return NSS_STATUS_NOTFOUND
;
459 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
462 fsetpos (ent
->stream
, &pos
);
466 /* Skip leading blanks. */
470 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
471 /* Parse the line. If it is invalid, loop to
472 get the next line of the file to parse. */
473 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
,
476 if (__glibc_unlikely (parse_res
== -1))
477 /* The parser ran out of space. */
480 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
481 /* This is a real entry. */
485 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
486 && result
->sp_namp
[2] != '\0')
488 /* XXX Do not use fixed length buffers. */
490 char *user
, *host
, *domain
;
491 struct __netgrent netgrdata
;
493 memset (&netgrdata
, 0, sizeof (struct __netgrent
));
494 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
495 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
496 &netgrdata
, buf2
, sizeof (buf2
),
499 if (user
!= NULL
&& user
[0] != '-')
500 blacklist_store_name (user
, ent
);
502 __internal_endnetgrent (&netgrdata
);
507 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
508 && result
->sp_namp
[2] != '\0')
512 ent
->netgroup
= true;
514 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
516 status
= getspent_next_nss_netgr (NULL
, result
, ent
,
518 buffer
, buflen
, errnop
);
519 if (status
== NSS_STATUS_RETURN
)
526 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
527 && result
->sp_namp
[1] != '@')
529 blacklist_store_name (&result
->sp_namp
[1], ent
);
534 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
535 && result
->sp_namp
[1] != '@')
537 size_t len
= strlen (result
->sp_namp
);
539 enum nss_status status
;
541 /* Store the User in the blacklist for the "+" at the end of
543 memcpy (buf
, &result
->sp_namp
[1], len
);
544 status
= getspnam_plususer (&result
->sp_namp
[1], result
, ent
,
545 buffer
, buflen
, errnop
);
546 blacklist_store_name (buf
, ent
);
548 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
550 /* We couldn't parse the entry */
551 else if (status
== NSS_STATUS_RETURN
552 /* entry doesn't exist */
553 || status
== NSS_STATUS_NOTFOUND
)
557 if (status
== NSS_STATUS_TRYAGAIN
)
559 fsetpos (ent
->stream
, &pos
);
567 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
571 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
573 return getspent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
577 return NSS_STATUS_SUCCESS
;
581 static enum nss_status
582 internal_getspent_r (struct spwd
*pw
, ent_t
*ent
,
583 char *buffer
, size_t buflen
, int *errnop
)
587 enum nss_status status
;
589 /* We are searching members in a netgroup */
590 /* Since this is not the first call, we don't need the group name */
591 status
= getspent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
,
594 if (status
== NSS_STATUS_RETURN
)
595 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
600 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
602 return getspent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
607 _nss_compat_getspent_r (struct spwd
*pwd
, char *buffer
, size_t buflen
,
610 enum nss_status result
= NSS_STATUS_SUCCESS
;
612 __libc_lock_lock (lock
);
614 /* Be prepared that the setpwent function was not called before. */
616 init_nss_interface ();
618 if (ext_ent
.stream
== NULL
)
619 result
= internal_setspent (&ext_ent
, 1, 1);
621 if (result
== NSS_STATUS_SUCCESS
)
622 result
= internal_getspent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
624 __libc_lock_unlock (lock
);
630 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
631 static enum nss_status
632 internal_getspnam_r (const char *name
, struct spwd
*result
, ent_t
*ent
,
633 char *buffer
, size_t buflen
, int *errnop
)
635 struct parser_data
*data
= (void *) buffer
;
645 /* We need at least 3 characters for one line. */
646 if (__glibc_unlikely (buflen
< 3))
650 return NSS_STATUS_TRYAGAIN
;
653 fgetpos (ent
->stream
, &pos
);
654 buffer
[buflen
- 1] = '\xff';
655 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
656 if (p
== NULL
&& feof_unlocked (ent
->stream
))
657 return NSS_STATUS_NOTFOUND
;
659 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
662 fsetpos (ent
->stream
, &pos
);
666 /* Terminate the line for any case. */
667 buffer
[buflen
- 1] = '\0';
669 /* Skip leading blanks. */
673 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
674 /* Parse the line. If it is invalid, loop to
675 get the next line of the file to parse. */
676 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
,
679 if (__glibc_unlikely (parse_res
== -1))
680 /* The parser ran out of space. */
683 /* This is a real entry. */
684 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
686 if (strcmp (result
->sp_namp
, name
) == 0)
687 return NSS_STATUS_SUCCESS
;
693 /* If the loaded NSS module does not support this service, add
694 all users from a +@netgroup entry to the blacklist, too. */
695 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
696 && result
->sp_namp
[2] != '\0')
698 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
699 return NSS_STATUS_NOTFOUND
;
704 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
705 && result
->sp_namp
[2] != '\0')
707 enum nss_status status
;
709 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
711 status
= getspnam_plususer (name
, result
, ent
, buffer
,
714 if (status
== NSS_STATUS_RETURN
)
723 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
724 && result
->sp_namp
[1] != '@')
726 if (strcmp (&result
->sp_namp
[1], name
) == 0)
727 return NSS_STATUS_NOTFOUND
;
733 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
734 && result
->sp_namp
[1] != '@')
736 if (strcmp (name
, &result
->sp_namp
[1]) == 0)
738 enum nss_status status
;
740 status
= getspnam_plususer (name
, result
, ent
,
741 buffer
, buflen
, errnop
);
743 if (status
== NSS_STATUS_RETURN
)
744 /* We couldn't parse the entry */
745 return NSS_STATUS_NOTFOUND
;
752 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
754 enum nss_status status
;
756 status
= getspnam_plususer (name
, result
, ent
,
757 buffer
, buflen
, errnop
);
759 if (status
== NSS_STATUS_SUCCESS
)
760 /* We found the entry. */
762 else if (status
== NSS_STATUS_RETURN
)
763 /* We couldn't parse the entry */
764 return NSS_STATUS_NOTFOUND
;
769 return NSS_STATUS_SUCCESS
;
774 _nss_compat_getspnam_r (const char *name
, struct spwd
*pwd
,
775 char *buffer
, size_t buflen
, int *errnop
)
777 enum nss_status result
;
778 ent_t ent
= { false, true, false, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0},
779 { NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
781 if (name
[0] == '-' || name
[0] == '+')
782 return NSS_STATUS_NOTFOUND
;
784 __libc_lock_lock (lock
);
787 init_nss_interface ();
789 __libc_lock_unlock (lock
);
791 result
= internal_setspent (&ent
, 0, 0);
793 if (result
== NSS_STATUS_SUCCESS
)
794 result
= internal_getspnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
796 internal_endspent_noerror (&ent
);
802 /* Support routines for remembering -@netgroup and -user entries.
803 The names are stored in a single string with `|' as separator. */
805 blacklist_store_name (const char *name
, ent_t
*ent
)
807 int namelen
= strlen (name
);
810 /* first call, setup cache */
811 if (ent
->blacklist
.size
== 0)
813 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
814 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
815 if (ent
->blacklist
.data
== NULL
)
817 ent
->blacklist
.data
[0] = '|';
818 ent
->blacklist
.data
[1] = '\0';
819 ent
->blacklist
.current
= 1;
823 if (in_blacklist (name
, namelen
, ent
))
824 return; /* no duplicates */
826 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
828 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
829 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
832 free (ent
->blacklist
.data
);
833 ent
->blacklist
.size
= 0;
836 ent
->blacklist
.data
= tmp
;
840 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
843 ent
->blacklist
.current
+= namelen
+ 1;
849 /* Returns whether ent->blacklist contains name. */
851 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
853 char buf
[namelen
+ 3];
856 if (ent
->blacklist
.data
== NULL
)
860 cp
= stpcpy (&buf
[1], name
);
863 return strstr (ent
->blacklist
.data
, buf
) != NULL
;