1 /* Copyright (C) 1996-2017 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 <rpc/types.h>
29 #include <rpcsvc/ypclnt.h>
30 #include <libc-lock.h>
31 #include <kernel-features.h>
35 static service_user
*ni
;
36 static enum nss_status (*nss_setspent
) (int stayopen
);
37 static enum nss_status (*nss_getspnam_r
) (const char *name
, struct spwd
* sp
,
38 char *buffer
, size_t buflen
,
40 static enum nss_status (*nss_getspent_r
) (struct spwd
* sp
, char *buffer
,
41 size_t buflen
, int *errnop
);
42 static enum nss_status (*nss_endspent
) (void);
44 /* Get the declaration of the parser function. */
46 #define STRUCTURE spwd
48 #include <nss/nss_files/files-parse.c>
50 /* Structure for remembering -@netgroup and -user members ... */
51 #define BLACKLIST_INITIAL_SIZE 512
52 #define BLACKLIST_INCREMENT 256
65 enum nss_status setent_status
;
67 struct blacklist_t blacklist
;
69 struct __netgrent netgrdata
;
71 typedef struct ent_t ent_t
;
73 static ent_t ext_ent
= { false, true, false, NSS_STATUS_SUCCESS
, NULL
,
75 { NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
77 /* Protect global state against multiple changers. */
78 __libc_lock_define_initialized (static, lock
)
80 /* Prototypes for local functions. */
81 static void blacklist_store_name (const char *, ent_t
*);
82 static int in_blacklist (const char *, int, ent_t
*);
84 /* Initialize the NSS interface/functions. The calling function must
87 init_nss_interface (void)
89 if (__nss_database_lookup ("shadow_compat", "passwd_compat",
92 nss_setspent
= __nss_lookup_function (ni
, "setspent");
93 nss_getspnam_r
= __nss_lookup_function (ni
, "getspnam_r");
94 nss_getspent_r
= __nss_lookup_function (ni
, "getspent_r");
95 nss_endspent
= __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
= fopen ("/etc/shadow", "rme");
183 if (ent
->stream
== NULL
)
184 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
186 /* We take care of locking ourself. */
187 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
190 rewind (ent
->stream
);
192 give_spwd_free (&ent
->pwd
);
194 if (needent
&& status
== NSS_STATUS_SUCCESS
&& nss_setspent
)
195 ent
->setent_status
= nss_setspent (stayopen
);
202 _nss_compat_setspent (int stayopen
)
204 enum nss_status result
;
206 __libc_lock_lock (lock
);
209 init_nss_interface ();
211 result
= internal_setspent (&ext_ent
, stayopen
, 1);
213 __libc_lock_unlock (lock
);
219 static enum nss_status
220 internal_endspent (ent_t
*ent
)
222 if (ent
->stream
!= NULL
)
224 fclose (ent
->stream
);
229 __internal_endnetgrent (&ent
->netgrdata
);
231 ent
->first
= ent
->netgroup
= false;
234 if (ent
->blacklist
.data
!= NULL
)
236 ent
->blacklist
.current
= 1;
237 ent
->blacklist
.data
[0] = '|';
238 ent
->blacklist
.data
[1] = '\0';
241 ent
->blacklist
.current
= 0;
243 give_spwd_free (&ent
->pwd
);
245 return NSS_STATUS_SUCCESS
;
249 _nss_compat_endspent (void)
251 enum nss_status result
;
253 __libc_lock_lock (lock
);
258 result
= internal_endspent (&ext_ent
);
260 __libc_lock_unlock (lock
);
266 static enum nss_status
267 getspent_next_nss_netgr (const char *name
, struct spwd
*result
, ent_t
*ent
,
268 char *group
, char *buffer
, size_t buflen
,
271 char *curdomain
= NULL
, *host
, *user
, *domain
, *p2
;
275 return NSS_STATUS_UNAVAIL
;
277 /* If the setpwent call failed, say so. */
278 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
279 return ent
->setent_status
;
283 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
284 __internal_setnetgrent (group
, &ent
->netgrdata
);
290 enum nss_status status
;
292 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
293 &ent
->netgrdata
, buffer
, buflen
,
297 __internal_endnetgrent (&ent
->netgrdata
);
298 ent
->netgroup
= false;
299 give_spwd_free (&ent
->pwd
);
300 return NSS_STATUS_RETURN
;
303 if (user
== NULL
|| user
[0] == '-')
308 if (curdomain
== NULL
309 && yp_get_default_domain (&curdomain
) != YPERR_SUCCESS
)
311 __internal_endnetgrent (&ent
->netgrdata
);
312 ent
->netgroup
= false;
313 give_spwd_free (&ent
->pwd
);
314 return NSS_STATUS_UNAVAIL
;
316 if (strcmp (curdomain
, domain
) != 0)
320 /* If name != NULL, we are called from getpwnam */
322 if (strcmp (user
, name
) != 0)
325 p2len
= spwd_need_buflen (&ent
->pwd
);
329 return NSS_STATUS_TRYAGAIN
;
331 p2
= buffer
+ (buflen
- p2len
);
334 if (nss_getspnam_r (user
, result
, buffer
, buflen
, errnop
) !=
338 if (!in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
340 /* Store the User in the blacklist for possible the "+" at the
341 end of /etc/passwd */
342 blacklist_store_name (result
->sp_namp
, ent
);
343 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
348 return NSS_STATUS_SUCCESS
;
352 static enum nss_status
353 getspent_next_nss (struct spwd
*result
, ent_t
*ent
,
354 char *buffer
, size_t buflen
, int *errnop
)
356 enum nss_status status
;
361 return NSS_STATUS_UNAVAIL
;
363 p2len
= spwd_need_buflen (&ent
->pwd
);
367 return NSS_STATUS_TRYAGAIN
;
369 p2
= buffer
+ (buflen
- p2len
);
373 if ((status
= nss_getspent_r (result
, buffer
, buflen
, errnop
)) !=
377 while (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
));
379 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
381 return NSS_STATUS_SUCCESS
;
385 /* This function handle the +user entrys in /etc/shadow */
386 static enum nss_status
387 getspnam_plususer (const char *name
, struct spwd
*result
, ent_t
*ent
,
388 char *buffer
, size_t buflen
, int *errnop
)
391 return NSS_STATUS_UNAVAIL
;
394 memset (&pwd
, '\0', sizeof (struct spwd
));
400 copy_spwd_changes (&pwd
, result
, NULL
, 0);
402 size_t plen
= spwd_need_buflen (&pwd
);
406 return NSS_STATUS_TRYAGAIN
;
408 char *p
= buffer
+ (buflen
- plen
);
411 enum nss_status status
= nss_getspnam_r (name
, result
, buffer
, buflen
,
413 if (status
!= NSS_STATUS_SUCCESS
)
416 if (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
417 return NSS_STATUS_NOTFOUND
;
419 copy_spwd_changes (result
, &pwd
, p
, plen
);
420 give_spwd_free (&pwd
);
421 /* We found the entry. */
422 return NSS_STATUS_SUCCESS
;
426 static enum nss_status
427 getspent_next_file (struct spwd
*result
, ent_t
*ent
,
428 char *buffer
, size_t buflen
, int *errnop
)
430 struct parser_data
*data
= (void *) buffer
;
439 /* We need at least 3 characters for one line. */
440 if (__glibc_unlikely (buflen
< 3))
444 return NSS_STATUS_TRYAGAIN
;
447 fgetpos (ent
->stream
, &pos
);
448 buffer
[buflen
- 1] = '\xff';
449 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
450 if (p
== NULL
&& feof_unlocked (ent
->stream
))
451 return NSS_STATUS_NOTFOUND
;
453 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
456 fsetpos (ent
->stream
, &pos
);
460 /* Skip leading blanks. */
464 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
465 /* Parse the line. If it is invalid, loop to
466 get the next line of the file to parse. */
467 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
,
470 if (__glibc_unlikely (parse_res
== -1))
471 /* The parser ran out of space. */
474 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
475 /* This is a real entry. */
479 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
480 && result
->sp_namp
[2] != '\0')
482 /* XXX Do not use fixed length buffers. */
484 char *user
, *host
, *domain
;
485 struct __netgrent netgrdata
;
487 memset (&netgrdata
, 0, sizeof (struct __netgrent
));
488 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
489 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
490 &netgrdata
, buf2
, sizeof (buf2
),
493 if (user
!= NULL
&& user
[0] != '-')
494 blacklist_store_name (user
, ent
);
496 __internal_endnetgrent (&netgrdata
);
501 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
502 && result
->sp_namp
[2] != '\0')
506 ent
->netgroup
= true;
508 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
510 status
= getspent_next_nss_netgr (NULL
, result
, ent
,
512 buffer
, buflen
, errnop
);
513 if (status
== NSS_STATUS_RETURN
)
520 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
521 && result
->sp_namp
[1] != '@')
523 blacklist_store_name (&result
->sp_namp
[1], ent
);
528 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
529 && result
->sp_namp
[1] != '@')
531 size_t len
= strlen (result
->sp_namp
);
533 enum nss_status status
;
535 /* Store the User in the blacklist for the "+" at the end of
537 memcpy (buf
, &result
->sp_namp
[1], len
);
538 status
= getspnam_plususer (&result
->sp_namp
[1], result
, ent
,
539 buffer
, buflen
, errnop
);
540 blacklist_store_name (buf
, ent
);
542 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
544 /* We couldn't parse the entry */
545 else if (status
== NSS_STATUS_RETURN
546 /* entry doesn't exist */
547 || status
== NSS_STATUS_NOTFOUND
)
551 if (status
== NSS_STATUS_TRYAGAIN
)
553 fsetpos (ent
->stream
, &pos
);
561 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
565 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
567 return getspent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
571 return NSS_STATUS_SUCCESS
;
575 static enum nss_status
576 internal_getspent_r (struct spwd
*pw
, ent_t
*ent
,
577 char *buffer
, size_t buflen
, int *errnop
)
581 enum nss_status status
;
583 /* We are searching members in a netgroup */
584 /* Since this is not the first call, we don't need the group name */
585 status
= getspent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
,
588 if (status
== NSS_STATUS_RETURN
)
589 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
594 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
596 return getspent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
601 _nss_compat_getspent_r (struct spwd
*pwd
, char *buffer
, size_t buflen
,
604 enum nss_status result
= NSS_STATUS_SUCCESS
;
606 __libc_lock_lock (lock
);
608 /* Be prepared that the setpwent function was not called before. */
610 init_nss_interface ();
612 if (ext_ent
.stream
== NULL
)
613 result
= internal_setspent (&ext_ent
, 1, 1);
615 if (result
== NSS_STATUS_SUCCESS
)
616 result
= internal_getspent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
618 __libc_lock_unlock (lock
);
624 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
625 static enum nss_status
626 internal_getspnam_r (const char *name
, struct spwd
*result
, ent_t
*ent
,
627 char *buffer
, size_t buflen
, int *errnop
)
629 struct parser_data
*data
= (void *) buffer
;
639 /* We need at least 3 characters for one line. */
640 if (__glibc_unlikely (buflen
< 3))
644 return NSS_STATUS_TRYAGAIN
;
647 fgetpos (ent
->stream
, &pos
);
648 buffer
[buflen
- 1] = '\xff';
649 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
650 if (p
== NULL
&& feof_unlocked (ent
->stream
))
651 return NSS_STATUS_NOTFOUND
;
653 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
656 fsetpos (ent
->stream
, &pos
);
660 /* Terminate the line for any case. */
661 buffer
[buflen
- 1] = '\0';
663 /* Skip leading blanks. */
667 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
668 /* Parse the line. If it is invalid, loop to
669 get the next line of the file to parse. */
670 !(parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
,
673 if (__glibc_unlikely (parse_res
== -1))
674 /* The parser ran out of space. */
677 /* This is a real entry. */
678 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
680 if (strcmp (result
->sp_namp
, name
) == 0)
681 return NSS_STATUS_SUCCESS
;
687 /* If the loaded NSS module does not support this service, add
688 all users from a +@netgroup entry to the blacklist, too. */
689 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
690 && result
->sp_namp
[2] != '\0')
692 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
693 return NSS_STATUS_NOTFOUND
;
698 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
699 && result
->sp_namp
[2] != '\0')
701 enum nss_status status
;
703 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
705 status
= getspnam_plususer (name
, result
, ent
, buffer
,
708 if (status
== NSS_STATUS_RETURN
)
717 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
718 && result
->sp_namp
[1] != '@')
720 if (strcmp (&result
->sp_namp
[1], name
) == 0)
721 return NSS_STATUS_NOTFOUND
;
727 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
728 && result
->sp_namp
[1] != '@')
730 if (strcmp (name
, &result
->sp_namp
[1]) == 0)
732 enum nss_status status
;
734 status
= getspnam_plususer (name
, result
, ent
,
735 buffer
, buflen
, errnop
);
737 if (status
== NSS_STATUS_RETURN
)
738 /* We couldn't parse the entry */
739 return NSS_STATUS_NOTFOUND
;
746 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
748 enum nss_status status
;
750 status
= getspnam_plususer (name
, result
, ent
,
751 buffer
, buflen
, errnop
);
753 if (status
== NSS_STATUS_SUCCESS
)
754 /* We found the entry. */
756 else if (status
== NSS_STATUS_RETURN
)
757 /* We couldn't parse the entry */
758 return NSS_STATUS_NOTFOUND
;
763 return NSS_STATUS_SUCCESS
;
768 _nss_compat_getspnam_r (const char *name
, struct spwd
*pwd
,
769 char *buffer
, size_t buflen
, int *errnop
)
771 enum nss_status result
;
772 ent_t ent
= { false, true, false, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0},
773 { NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
775 if (name
[0] == '-' || name
[0] == '+')
776 return NSS_STATUS_NOTFOUND
;
778 __libc_lock_lock (lock
);
781 init_nss_interface ();
783 __libc_lock_unlock (lock
);
785 result
= internal_setspent (&ent
, 0, 0);
787 if (result
== NSS_STATUS_SUCCESS
)
788 result
= internal_getspnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
790 internal_endspent (&ent
);
796 /* Support routines for remembering -@netgroup and -user entries.
797 The names are stored in a single string with `|' as separator. */
799 blacklist_store_name (const char *name
, ent_t
*ent
)
801 int namelen
= strlen (name
);
804 /* first call, setup cache */
805 if (ent
->blacklist
.size
== 0)
807 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
808 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
809 if (ent
->blacklist
.data
== NULL
)
811 ent
->blacklist
.data
[0] = '|';
812 ent
->blacklist
.data
[1] = '\0';
813 ent
->blacklist
.current
= 1;
817 if (in_blacklist (name
, namelen
, ent
))
818 return; /* no duplicates */
820 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
822 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
823 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
826 free (ent
->blacklist
.data
);
827 ent
->blacklist
.size
= 0;
830 ent
->blacklist
.data
= tmp
;
834 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
837 ent
->blacklist
.current
+= namelen
+ 1;
843 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
845 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
847 char buf
[namelen
+ 3];
850 if (ent
->blacklist
.data
== NULL
)
854 cp
= stpcpy (&buf
[1], name
);
857 return strstr (ent
->blacklist
.data
, buf
) != NULL
;