1 /* Copyright (C) 1996-1999,2001-2005,2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <stdio_ext.h>
29 #include <rpc/types.h>
30 #include <rpcsvc/ypclnt.h>
31 #include <bits/libc-lock.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
66 struct blacklist_t blacklist
;
68 struct __netgrent netgrdata
;
70 typedef struct ent_t ent_t
;
72 static ent_t ext_ent
= {0, TRUE
, 0, NULL
, {NULL
, 0, 0},
73 {NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
75 /* Protect global state against multiple changers. */
76 __libc_lock_define_initialized (static, lock
)
78 /* Prototypes for local functions. */
79 static void blacklist_store_name (const char *, ent_t
*);
80 static int in_blacklist (const char *, int, ent_t
*);
82 /* Initialize the NSS interface/functions. The calling function must
85 init_nss_interface (void)
87 if (__nss_database_lookup ("shadow_compat", "passwd_compat",
90 nss_setspent
= __nss_lookup_function (ni
, "setspent");
91 nss_getspnam_r
= __nss_lookup_function (ni
, "getspnam_r");
92 nss_getspent_r
= __nss_lookup_function (ni
, "getspent_r");
93 nss_endspent
= __nss_lookup_function (ni
, "endspent");
98 give_spwd_free (struct spwd
*pwd
)
100 if (pwd
->sp_namp
!= NULL
)
102 if (pwd
->sp_pwdp
!= NULL
)
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
)
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", "rm");
183 if (ent
->stream
== NULL
)
184 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
187 /* We have to make sure the file is `closed on exec'. */
190 result
= flags
= fcntl (fileno_unlocked (ent
->stream
), F_GETFD
, 0);
194 result
= fcntl (fileno_unlocked (ent
->stream
), F_SETFD
, flags
);
198 /* Something went wrong. Close the stream and return a
200 fclose (ent
->stream
);
202 status
= NSS_STATUS_UNAVAIL
;
205 /* We take care of locking ourself. */
206 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
210 rewind (ent
->stream
);
212 give_spwd_free (&ent
->pwd
);
214 if (status
== NSS_STATUS_SUCCESS
&& nss_setspent
)
216 status
= nss_setspent (stayopen
);
217 if (status
== NSS_STATUS_UNAVAIL
)
218 status
= NSS_STATUS_SUCCESS
;
226 _nss_compat_setspent (int stayopen
)
228 enum nss_status result
;
230 __libc_lock_lock (lock
);
233 init_nss_interface ();
235 result
= internal_setspent (&ext_ent
, stayopen
);
237 __libc_lock_unlock (lock
);
243 static enum nss_status
244 internal_endspent (ent_t
*ent
)
249 if (ent
->stream
!= NULL
)
251 fclose (ent
->stream
);
256 __internal_endnetgrent (&ent
->netgrdata
);
258 ent
->first
= ent
->netgroup
= FALSE
;
261 if (ent
->blacklist
.data
!= NULL
)
263 ent
->blacklist
.current
= 1;
264 ent
->blacklist
.data
[0] = '|';
265 ent
->blacklist
.data
[1] = '\0';
268 ent
->blacklist
.current
= 0;
270 give_spwd_free (&ent
->pwd
);
272 return NSS_STATUS_SUCCESS
;
276 _nss_compat_endspent (void)
278 enum nss_status result
;
280 __libc_lock_lock (lock
);
282 result
= internal_endspent (&ext_ent
);
284 __libc_lock_unlock (lock
);
290 static enum nss_status
291 getspent_next_nss_netgr (const char *name
, struct spwd
*result
, ent_t
*ent
,
292 char *group
, char *buffer
, size_t buflen
,
295 char *curdomain
, *host
, *user
, *domain
, *p2
;
299 return NSS_STATUS_UNAVAIL
;
301 if (yp_get_default_domain (&curdomain
) != YPERR_SUCCESS
)
303 ent
->netgroup
= FALSE
;
305 give_spwd_free (&ent
->pwd
);
306 return NSS_STATUS_UNAVAIL
;
309 if (ent
->first
== TRUE
)
311 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
312 __internal_setnetgrent (group
, &ent
->netgrdata
);
319 enum nss_status status
;
321 saved_cursor
= ent
->netgrdata
.cursor
;
322 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
323 &ent
->netgrdata
, buffer
, buflen
,
327 __internal_endnetgrent (&ent
->netgrdata
);
328 ent
->netgroup
= FALSE
;
329 give_spwd_free (&ent
->pwd
);
330 return NSS_STATUS_RETURN
;
333 if (user
== NULL
|| user
[0] == '-')
336 if (domain
!= NULL
&& strcmp (curdomain
, domain
) != 0)
339 /* If name != NULL, we are called from getpwnam */
341 if (strcmp (user
, name
) != 0)
344 p2len
= spwd_need_buflen (&ent
->pwd
);
348 return NSS_STATUS_TRYAGAIN
;
350 p2
= buffer
+ (buflen
- p2len
);
353 if (nss_getspnam_r (user
, result
, buffer
, buflen
, errnop
) !=
357 if (!in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
359 /* Store the User in the blacklist for possible the "+" at the
360 end of /etc/passwd */
361 blacklist_store_name (result
->sp_namp
, ent
);
362 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
367 return NSS_STATUS_SUCCESS
;
371 static enum nss_status
372 getspent_next_nss (struct spwd
*result
, ent_t
*ent
,
373 char *buffer
, size_t buflen
, int *errnop
)
375 enum nss_status status
;
380 return NSS_STATUS_UNAVAIL
;
382 p2len
= spwd_need_buflen (&ent
->pwd
);
386 return NSS_STATUS_TRYAGAIN
;
388 p2
= buffer
+ (buflen
- p2len
);
392 if ((status
= nss_getspent_r (result
, buffer
, buflen
, errnop
)) !=
396 while (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
));
398 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
400 return NSS_STATUS_SUCCESS
;
403 /* This function handle the +user entrys in /etc/shadow */
404 static enum nss_status
405 getspnam_plususer (const char *name
, struct spwd
*result
, ent_t
*ent
,
406 char *buffer
, size_t buflen
, int *errnop
)
409 return NSS_STATUS_UNAVAIL
;
412 memset (&pwd
, '\0', sizeof (struct spwd
));
418 copy_spwd_changes (&pwd
, result
, NULL
, 0);
420 size_t plen
= spwd_need_buflen (&pwd
);
424 return NSS_STATUS_TRYAGAIN
;
426 char *p
= buffer
+ (buflen
- plen
);
429 enum nss_status status
= nss_getspnam_r (name
, result
, buffer
, buflen
,
431 if (status
!= NSS_STATUS_SUCCESS
)
434 if (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
435 return NSS_STATUS_NOTFOUND
;
437 copy_spwd_changes (result
, &pwd
, p
, plen
);
438 give_spwd_free (&pwd
);
439 /* We found the entry. */
440 return NSS_STATUS_SUCCESS
;
443 static enum nss_status
444 getspent_next_file (struct spwd
*result
, ent_t
*ent
,
445 char *buffer
, size_t buflen
, int *errnop
)
447 struct parser_data
*data
= (void *) buffer
;
456 /* We need at least 3 characters for one line. */
457 if (__builtin_expect (buflen
< 3, 0))
461 return NSS_STATUS_TRYAGAIN
;
464 fgetpos (ent
->stream
, &pos
);
465 buffer
[buflen
- 1] = '\xff';
466 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
467 if (p
== NULL
&& feof_unlocked (ent
->stream
))
468 return NSS_STATUS_NOTFOUND
;
470 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
473 fsetpos (ent
->stream
, &pos
);
477 /* Skip leading blanks. */
481 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
482 /* Parse the line. If it is invalid, loop to
483 get the next line of the file to parse. */
484 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
,
487 if (__builtin_expect (parse_res
== -1, 0))
488 /* The parser ran out of space. */
491 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
492 /* This is a real entry. */
496 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
497 && result
->sp_namp
[2] != '\0')
499 /* XXX Do not use fixed length buffers. */
501 char *user
, *host
, *domain
;
502 struct __netgrent netgrdata
;
504 bzero (&netgrdata
, sizeof (struct __netgrent
));
505 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
506 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
507 &netgrdata
, buf2
, sizeof (buf2
),
510 if (user
!= NULL
&& user
[0] != '-')
511 blacklist_store_name (user
, ent
);
513 __internal_endnetgrent (&netgrdata
);
518 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
519 && result
->sp_namp
[2] != '\0')
523 ent
->netgroup
= TRUE
;
525 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
527 status
= getspent_next_nss_netgr (NULL
, result
, ent
,
529 buffer
, buflen
, errnop
);
530 if (status
== NSS_STATUS_RETURN
)
537 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
538 && result
->sp_namp
[1] != '@')
540 blacklist_store_name (&result
->sp_namp
[1], ent
);
545 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
546 && result
->sp_namp
[1] != '@')
548 size_t len
= strlen (result
->sp_namp
);
550 enum nss_status status
;
552 /* Store the User in the blacklist for the "+" at the end of
554 memcpy (buf
, &result
->sp_namp
[1], len
);
555 status
= getspnam_plususer (&result
->sp_namp
[1], result
, ent
,
556 buffer
, buflen
, errnop
);
557 blacklist_store_name (buf
, ent
);
559 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
561 /* We couldn't parse the entry */
562 else if (status
== NSS_STATUS_RETURN
563 /* entry doesn't exist */
564 || status
== NSS_STATUS_NOTFOUND
)
568 if (status
== NSS_STATUS_TRYAGAIN
)
570 fsetpos (ent
->stream
, &pos
);
578 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
582 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
584 return getspent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
588 return NSS_STATUS_SUCCESS
;
592 static enum nss_status
593 internal_getspent_r (struct spwd
*pw
, ent_t
*ent
,
594 char *buffer
, size_t buflen
, int *errnop
)
598 enum nss_status status
;
600 /* We are searching members in a netgroup */
601 /* Since this is not the first call, we don't need the group name */
602 status
= getspent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
,
605 if (status
== NSS_STATUS_RETURN
)
606 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
611 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
613 return getspent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
617 _nss_compat_getspent_r (struct spwd
*pwd
, char *buffer
, size_t buflen
,
620 enum nss_status result
= NSS_STATUS_SUCCESS
;
622 __libc_lock_lock (lock
);
624 /* Be prepared that the setpwent function was not called before. */
626 init_nss_interface ();
628 if (ext_ent
.stream
== NULL
)
629 result
= internal_setspent (&ext_ent
, 1);
631 if (result
== NSS_STATUS_SUCCESS
)
632 result
= internal_getspent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
634 __libc_lock_unlock (lock
);
639 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
640 static enum nss_status
641 internal_getspnam_r (const char *name
, struct spwd
*result
, ent_t
*ent
,
642 char *buffer
, size_t buflen
, int *errnop
)
644 struct parser_data
*data
= (void *) buffer
;
654 /* We need at least 3 characters for one line. */
655 if (__builtin_expect (buflen
< 3, 0))
659 return NSS_STATUS_TRYAGAIN
;
662 fgetpos (ent
->stream
, &pos
);
663 buffer
[buflen
- 1] = '\xff';
664 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
665 if (p
== NULL
&& feof_unlocked (ent
->stream
))
666 return NSS_STATUS_NOTFOUND
;
668 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
671 fsetpos (ent
->stream
, &pos
);
675 /* Terminate the line for any case. */
676 buffer
[buflen
- 1] = '\0';
678 /* Skip leading blanks. */
682 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
683 /* Parse the line. If it is invalid, loop to
684 get the next line of the file to parse. */
685 !(parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
,
688 if (__builtin_expect (parse_res
== -1, 0))
689 /* The parser ran out of space. */
692 /* This is a real entry. */
693 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
695 if (strcmp (result
->sp_namp
, name
) == 0)
696 return NSS_STATUS_SUCCESS
;
702 /* If the loaded NSS module does not support this service, add
703 all users from a +@netgroup entry to the blacklist, too. */
704 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
705 && result
->sp_namp
[2] != '\0')
707 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
708 return NSS_STATUS_NOTFOUND
;
713 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
714 && result
->sp_namp
[2] != '\0')
716 enum nss_status status
;
718 if (innetgr (&result
->sp_namp
[2], NULL
, name
, NULL
))
720 status
= getspnam_plususer (name
, result
, ent
, buffer
,
723 if (status
== NSS_STATUS_RETURN
)
732 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
733 && result
->sp_namp
[1] != '@')
735 if (strcmp (&result
->sp_namp
[1], name
) == 0)
736 return NSS_STATUS_NOTFOUND
;
742 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
743 && result
->sp_namp
[1] != '@')
745 if (strcmp (name
, &result
->sp_namp
[1]) == 0)
747 enum nss_status status
;
749 status
= getspnam_plususer (name
, result
, ent
,
750 buffer
, buflen
, errnop
);
752 if (status
== NSS_STATUS_RETURN
)
753 /* We couldn't parse the entry */
754 return NSS_STATUS_NOTFOUND
;
761 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
763 enum nss_status status
;
765 status
= getspnam_plususer (name
, result
, ent
,
766 buffer
, buflen
, errnop
);
768 if (status
== NSS_STATUS_SUCCESS
)
769 /* We found the entry. */
771 else if (status
== NSS_STATUS_RETURN
)
772 /* We couldn't parse the entry */
773 return NSS_STATUS_NOTFOUND
;
778 return NSS_STATUS_SUCCESS
;
782 _nss_compat_getspnam_r (const char *name
, struct spwd
*pwd
,
783 char *buffer
, size_t buflen
, int *errnop
)
785 enum nss_status result
;
786 ent_t ent
= {0, TRUE
, 0, NULL
, {NULL
, 0, 0},
787 {NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
789 if (name
[0] == '-' || name
[0] == '+')
790 return NSS_STATUS_NOTFOUND
;
792 __libc_lock_lock (lock
);
795 init_nss_interface ();
797 __libc_lock_unlock (lock
);
799 result
= internal_setspent (&ent
, 0);
801 if (result
== NSS_STATUS_SUCCESS
)
802 result
= internal_getspnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
804 internal_endspent (&ent
);
809 /* Support routines for remembering -@netgroup and -user entries.
810 The names are stored in a single string with `|' as separator. */
812 blacklist_store_name (const char *name
, ent_t
*ent
)
814 int namelen
= strlen (name
);
817 /* first call, setup cache */
818 if (ent
->blacklist
.size
== 0)
820 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
821 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
822 if (ent
->blacklist
.data
== NULL
)
824 ent
->blacklist
.data
[0] = '|';
825 ent
->blacklist
.data
[1] = '\0';
826 ent
->blacklist
.current
= 1;
830 if (in_blacklist (name
, namelen
, ent
))
831 return; /* no duplicates */
833 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
835 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
836 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
839 free (ent
->blacklist
.data
);
840 ent
->blacklist
.size
= 0;
843 ent
->blacklist
.data
= tmp
;
847 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
850 ent
->blacklist
.current
+= namelen
+ 1;
855 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
857 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
859 char buf
[namelen
+ 3];
862 if (ent
->blacklist
.data
== NULL
)
866 cp
= stpcpy (&buf
[1], name
);
869 return strstr (ent
->blacklist
.data
, buf
) != NULL
;