]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-spwd.c
1 /* Copyright (C) 1996,1997,1998,1999,2001,2002,2003 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 <bits/libc-lock.h>
28 #include <rpc/types.h>
29 #include <rpcsvc/ypclnt.h>
34 static service_user
*ni
;
35 static enum nss_status (*nss_setspent
) (int stayopen
);
36 static enum nss_status (*nss_getspnam_r
) (const char *name
, struct spwd
* sp
,
37 char *buffer
, size_t buflen
,
39 static enum nss_status (*nss_getspent_r
) (struct spwd
* sp
, char *buffer
,
40 size_t buflen
, int *errnop
);
41 static enum nss_status (*nss_endspent
) (void);
43 /* Get the declaration of the parser function. */
45 #define STRUCTURE spwd
47 #include <nss/nss_files/files-parse.c>
49 /* Structure for remembering -@netgroup and -user members ... */
50 #define BLACKLIST_INITIAL_SIZE 512
51 #define BLACKLIST_INCREMENT 256
65 struct blacklist_t blacklist
;
67 struct __netgrent netgrdata
;
69 typedef struct ent_t ent_t
;
71 static ent_t ext_ent
= {0, TRUE
, 0, NULL
, {NULL
, 0, 0},
72 {NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
74 /* Protect global state against multiple changers. */
75 __libc_lock_define_initialized (static, lock
)
77 /* Prototypes for local functions. */
78 static void blacklist_store_name (const char *, ent_t
*);
79 static int in_blacklist (const char *, int, ent_t
*);
81 /* Initialize the NSS interface/functions. The calling function must
84 init_nss_interface (void)
86 if (__nss_database_lookup ("shadow_compat", "passwd_compat",
89 nss_setspent
= __nss_lookup_function (ni
, "setspent");
90 nss_getspnam_r
= __nss_lookup_function (ni
, "getspnam_r");
91 nss_getspent_r
= __nss_lookup_function (ni
, "getspent_r");
92 nss_endspent
= __nss_lookup_function (ni
, "endspent");
97 give_spwd_free (struct spwd
*pwd
)
99 if (pwd
->sp_namp
!= NULL
)
101 if (pwd
->sp_pwdp
!= NULL
)
104 memset (pwd
, '\0', sizeof (struct spwd
));
112 spwd_need_buflen (struct spwd
*pwd
)
116 if (pwd
->sp_pwdp
!= NULL
)
117 len
+= strlen (pwd
->sp_pwdp
) + 1;
123 copy_spwd_changes (struct spwd
*dest
, struct spwd
*src
,
124 char *buffer
, size_t buflen
)
126 if (src
->sp_pwdp
!= NULL
&& strlen (src
->sp_pwdp
))
129 dest
->sp_pwdp
= strdup (src
->sp_pwdp
);
130 else if (dest
->sp_pwdp
&&
131 strlen (dest
->sp_pwdp
) >= strlen (src
->sp_pwdp
))
132 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
135 dest
->sp_pwdp
= buffer
;
136 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
137 buffer
+= strlen (dest
->sp_pwdp
) + 1;
138 buflen
= buflen
- (strlen (dest
->sp_pwdp
) + 1);
141 if (src
->sp_lstchg
!= 0)
142 dest
->sp_lstchg
= src
->sp_lstchg
;
143 if (src
->sp_min
!= 0)
144 dest
->sp_min
= src
->sp_min
;
145 if (src
->sp_max
!= 0)
146 dest
->sp_max
= src
->sp_max
;
147 if (src
->sp_warn
!= -1)
148 dest
->sp_warn
= src
->sp_warn
;
149 if (src
->sp_inact
!= -1)
150 dest
->sp_inact
= src
->sp_inact
;
151 if (src
->sp_expire
!= -1)
152 dest
->sp_expire
= src
->sp_expire
;
153 if (src
->sp_flag
!= ~0ul)
154 dest
->sp_flag
= src
->sp_flag
;
157 static enum nss_status
158 internal_setspent (ent_t
*ent
, int stayopen
)
160 enum nss_status status
= NSS_STATUS_SUCCESS
;
162 ent
->first
= ent
->netgroup
= 0;
165 /* If something was left over free it. */
167 __internal_endnetgrent (&ent
->netgrdata
);
169 if (ent
->blacklist
.data
!= NULL
)
171 ent
->blacklist
.current
= 1;
172 ent
->blacklist
.data
[0] = '|';
173 ent
->blacklist
.data
[1] = '\0';
176 ent
->blacklist
.current
= 0;
178 if (ent
->stream
== NULL
)
180 ent
->stream
= fopen ("/etc/shadow", "r");
182 if (ent
->stream
== NULL
)
183 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
186 /* We have to make sure the file is `closed on exec'. */
189 result
= flags
= fcntl (fileno (ent
->stream
), F_GETFD
, 0);
193 result
= fcntl (fileno (ent
->stream
), F_SETFD
, flags
);
197 /* Something went wrong. Close the stream and return a
199 fclose (ent
->stream
);
201 status
= NSS_STATUS_UNAVAIL
;
206 rewind (ent
->stream
);
208 give_spwd_free (&ent
->pwd
);
210 if (status
== NSS_STATUS_SUCCESS
&& nss_setspent
)
211 return nss_setspent (stayopen
);
218 _nss_compat_setspent (int stayopen
)
220 enum nss_status result
;
222 __libc_lock_lock (lock
);
225 init_nss_interface ();
227 result
= internal_setspent (&ext_ent
, stayopen
);
229 __libc_lock_unlock (lock
);
235 static enum nss_status
236 internal_endspent (ent_t
*ent
)
241 if (ent
->stream
!= NULL
)
243 fclose (ent
->stream
);
248 __internal_endnetgrent (&ent
->netgrdata
);
250 ent
->first
= ent
->netgroup
= FALSE
;
253 if (ent
->blacklist
.data
!= NULL
)
255 ent
->blacklist
.current
= 1;
256 ent
->blacklist
.data
[0] = '|';
257 ent
->blacklist
.data
[1] = '\0';
260 ent
->blacklist
.current
= 0;
262 give_spwd_free (&ent
->pwd
);
264 return NSS_STATUS_SUCCESS
;
268 _nss_compat_endspent (void)
270 enum nss_status result
;
272 __libc_lock_lock (lock
);
274 result
= internal_endspent (&ext_ent
);
276 __libc_lock_unlock (lock
);
282 static enum nss_status
283 getspent_next_nss_netgr (const char *name
, struct spwd
*result
, ent_t
*ent
,
284 char *group
, char *buffer
, size_t buflen
,
287 char *curdomain
, *host
, *user
, *domain
, *p2
;
291 return NSS_STATUS_UNAVAIL
;
293 if (yp_get_default_domain (&curdomain
) != YPERR_SUCCESS
)
295 ent
->netgroup
= FALSE
;
297 give_spwd_free (&ent
->pwd
);
298 return NSS_STATUS_UNAVAIL
;
301 if (ent
->first
== TRUE
)
303 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
304 __internal_setnetgrent (group
, &ent
->netgrdata
);
311 enum nss_status status
;
313 saved_cursor
= ent
->netgrdata
.cursor
;
314 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
315 &ent
->netgrdata
, buffer
, buflen
,
319 __internal_endnetgrent (&ent
->netgrdata
);
320 ent
->netgroup
= FALSE
;
321 give_spwd_free (&ent
->pwd
);
322 return NSS_STATUS_RETURN
;
325 if (user
== NULL
|| user
[0] == '-')
328 if (domain
!= NULL
&& strcmp (curdomain
, domain
) != 0)
331 /* If name != NULL, we are called from getpwnam */
333 if (strcmp (user
, name
) != 0)
336 p2len
= spwd_need_buflen (&ent
->pwd
);
340 return NSS_STATUS_TRYAGAIN
;
342 p2
= buffer
+ (buflen
- p2len
);
345 if (nss_getspnam_r (user
, result
, buffer
, buflen
, errnop
) !=
349 if (!in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
351 /* Store the User in the blacklist for possible the "+" at the
352 end of /etc/passwd */
353 blacklist_store_name (result
->sp_namp
, ent
);
354 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
359 return NSS_STATUS_SUCCESS
;
363 static enum nss_status
364 getspent_next_nss (struct spwd
*result
, ent_t
*ent
,
365 char *buffer
, size_t buflen
, int *errnop
)
367 enum nss_status status
;
372 return NSS_STATUS_UNAVAIL
;
374 p2len
= spwd_need_buflen (&ent
->pwd
);
378 return NSS_STATUS_TRYAGAIN
;
380 p2
= buffer
+ (buflen
- p2len
);
384 if ((status
= nss_getspent_r (result
, buffer
, buflen
, errnop
)) !=
388 while (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
));
390 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
392 return NSS_STATUS_SUCCESS
;
395 /* This function handle the +user entrys in /etc/shadow */
396 static enum nss_status
397 getspnam_plususer (const char *name
, struct spwd
*result
, ent_t
*ent
,
398 char *buffer
, size_t buflen
, int *errnop
)
405 return NSS_STATUS_UNAVAIL
;
407 memset (&pwd
, '\0', sizeof (struct spwd
));
413 copy_spwd_changes (&pwd
, result
, NULL
, 0);
415 plen
= spwd_need_buflen (&pwd
);
419 return NSS_STATUS_TRYAGAIN
;
421 p
= buffer
+ (buflen
- plen
);
424 if (nss_getspnam_r (name
, result
, buffer
, buflen
, errnop
) !=
426 return NSS_STATUS_NOTFOUND
;
428 if (in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
429 return NSS_STATUS_NOTFOUND
;
431 copy_spwd_changes (result
, &pwd
, p
, plen
);
432 give_spwd_free (&pwd
);
433 /* We found the entry. */
434 return NSS_STATUS_RETURN
;
437 static enum nss_status
438 getspent_next_file (struct spwd
*result
, ent_t
*ent
,
439 char *buffer
, size_t buflen
, int *errnop
)
441 struct parser_data
*data
= (void *) buffer
;
450 fgetpos (ent
->stream
, &pos
);
451 buffer
[buflen
- 1] = '\xff';
452 p
= fgets (buffer
, buflen
, ent
->stream
);
453 if (p
== NULL
&& feof (ent
->stream
))
454 return NSS_STATUS_NOTFOUND
;
456 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
458 fsetpos (ent
->stream
, &pos
);
460 return NSS_STATUS_TRYAGAIN
;
463 /* Skip leading blanks. */
467 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
468 /* Parse the line. If it is invalid, loop to
469 get the next line of the file to parse. */
470 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
,
475 /* The parser ran out of space. */
476 fsetpos (ent
->stream
, &pos
);
478 return NSS_STATUS_TRYAGAIN
;
481 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
482 /* This is a real entry. */
486 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
487 && result
->sp_namp
[2] != '\0')
489 /* XXX Do not use fixed length buffers. */
491 char *user
, *host
, *domain
;
492 struct __netgrent netgrdata
;
494 bzero (&netgrdata
, sizeof (struct __netgrent
));
495 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
496 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
497 &netgrdata
, buf2
, sizeof (buf2
),
500 if (user
!= NULL
&& user
[0] != '-')
501 blacklist_store_name (user
, ent
);
503 __internal_endnetgrent (&netgrdata
);
508 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
509 && result
->sp_namp
[2] != '\0')
513 ent
->netgroup
= TRUE
;
515 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
517 status
= getspent_next_nss_netgr (NULL
, result
, ent
,
519 buffer
, buflen
, errnop
);
520 if (status
== NSS_STATUS_RETURN
)
527 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
528 && result
->sp_namp
[1] != '@')
530 blacklist_store_name (&result
->sp_namp
[1], ent
);
535 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
536 && result
->sp_namp
[1] != '@')
538 enum nss_status status
;
540 /* Store the User in the blacklist for the "+" at the end of
542 blacklist_store_name (&result
->sp_namp
[1], ent
);
543 status
= getspnam_plususer (&result
->sp_namp
[1], result
, ent
,
544 buffer
, buflen
, errnop
);
546 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
548 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry */
549 || status
== NSS_STATUS_NOTFOUND
) /* entry doesn't exist */
553 if (status
== NSS_STATUS_TRYAGAIN
)
555 fsetpos (ent
->stream
, &pos
);
563 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
567 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
569 return getspent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
573 return NSS_STATUS_SUCCESS
;
577 static enum nss_status
578 internal_getspent_r (struct spwd
*pw
, ent_t
*ent
,
579 char *buffer
, size_t buflen
, int *errnop
)
583 enum nss_status status
;
585 /* We are searching members in a netgroup */
586 /* Since this is not the first call, we don't need the group name */
587 status
= getspent_next_nss_netgr (NULL
, pw
, ent
, NULL
, buffer
,
590 if (status
== NSS_STATUS_RETURN
)
591 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
596 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
598 return getspent_next_nss (pw
, ent
, buffer
, buflen
, errnop
);
602 _nss_compat_getspent_r (struct spwd
*pwd
, char *buffer
, size_t buflen
,
605 enum nss_status result
= NSS_STATUS_SUCCESS
;
607 __libc_lock_lock (lock
);
609 /* Be prepared that the setpwent function was not called before. */
611 init_nss_interface ();
613 if (ext_ent
.stream
== NULL
)
614 result
= internal_setspent (&ext_ent
, 1);
616 if (result
== NSS_STATUS_SUCCESS
)
617 result
= internal_getspent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
619 __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 fgetpos (ent
->stream
, &pos
);
640 buffer
[buflen
- 1] = '\xff';
641 p
= fgets (buffer
, buflen
, ent
->stream
);
642 if (p
== NULL
&& feof (ent
->stream
))
643 return NSS_STATUS_NOTFOUND
;
645 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
647 fsetpos (ent
->stream
, &pos
);
649 return NSS_STATUS_TRYAGAIN
;
652 /* Skip leading blanks. */
656 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
657 /* Parse the line. If it is invalid, loop to
658 get the next line of the file to parse. */
659 !(parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
,
664 /* The parser ran out of space. */
665 fsetpos (ent
->stream
, &pos
);
667 return NSS_STATUS_TRYAGAIN
;
670 /* This is a real entry. */
671 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
673 if (strcmp (result
->sp_namp
, name
) == 0)
674 return NSS_STATUS_SUCCESS
;
680 /* If the loaded NSS module does not support this service, add
681 all users from a +@netgroup entry to the blacklist, too. */
682 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
683 && result
->sp_namp
[2] != '\0')
685 /* XXX Do not use fixed length buffers. */
687 char *user
, *host
, *domain
;
688 struct __netgrent netgrdata
;
690 bzero (&netgrdata
, sizeof (struct __netgrent
));
691 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
692 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
693 buf2
, sizeof (buf2
), errnop
))
695 if (user
!= NULL
&& user
[0] != '-')
696 if (strcmp (user
, name
) == 0)
697 return NSS_STATUS_NOTFOUND
;
699 __internal_endnetgrent (&netgrdata
);
704 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
705 && result
->sp_namp
[2] != '\0')
707 char *buf
= strdupa (&result
->sp_namp
[2]);
710 ent
->netgroup
= TRUE
;
712 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
716 status
= getspent_next_nss_netgr (name
, result
, ent
, buf
,
717 buffer
, buflen
, errnop
);
718 if (status
== NSS_STATUS_RETURN
)
721 if (status
== NSS_STATUS_SUCCESS
722 && strcmp (result
->sp_namp
, name
) == 0)
723 return NSS_STATUS_SUCCESS
;
725 while (status
== NSS_STATUS_SUCCESS
);
730 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
731 && result
->sp_namp
[1] != '@')
733 if (strcmp (&result
->sp_namp
[1], name
) == 0)
734 return NSS_STATUS_NOTFOUND
;
740 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
741 && result
->sp_namp
[1] != '@')
743 if (strcmp (name
, &result
->sp_namp
[1]) == 0)
745 enum nss_status status
;
747 status
= getspnam_plususer (name
, result
, ent
,
748 buffer
, buflen
, errnop
);
750 if (status
== NSS_STATUS_RETURN
)
751 /* We couldn't parse the entry */
752 return NSS_STATUS_NOTFOUND
;
759 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
761 enum nss_status status
;
763 status
= getspnam_plususer (name
, result
, ent
,
764 buffer
, buflen
, errnop
);
766 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
767 return NSS_STATUS_NOTFOUND
;
772 return NSS_STATUS_SUCCESS
;
776 _nss_compat_getspnam_r (const char *name
, struct spwd
*pwd
,
777 char *buffer
, size_t buflen
, int *errnop
)
779 enum nss_status result
;
780 ent_t ent
= {0, TRUE
, 0, NULL
, {NULL
, 0, 0},
781 {NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
783 if (name
[0] == '-' || name
[0] == '+')
784 return NSS_STATUS_NOTFOUND
;
786 __libc_lock_lock (lock
);
789 init_nss_interface ();
791 __libc_lock_unlock (lock
);
793 result
= internal_setspent (&ent
, 0);
795 if (result
!= NSS_STATUS_SUCCESS
)
798 result
= internal_getspnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
800 internal_endspent (&ent
);
805 /* Support routines for remembering -@netgroup and -user entries.
806 The names are stored in a single string with `|' as separator. */
808 blacklist_store_name (const char *name
, ent_t
*ent
)
810 int namelen
= strlen (name
);
813 /* first call, setup cache */
814 if (ent
->blacklist
.size
== 0)
816 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
817 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
818 if (ent
->blacklist
.data
== NULL
)
820 ent
->blacklist
.data
[0] = '|';
821 ent
->blacklist
.data
[1] = '\0';
822 ent
->blacklist
.current
= 1;
826 if (in_blacklist (name
, namelen
, ent
))
827 return; /* no duplicates */
829 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
831 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
832 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
835 free (ent
->blacklist
.data
);
836 ent
->blacklist
.size
= 0;
839 ent
->blacklist
.data
= tmp
;
843 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
846 ent
->blacklist
.current
+= namelen
+ 1;
851 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
853 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
855 char buf
[namelen
+ 3];
858 if (ent
->blacklist
.data
== NULL
)
862 cp
= stpcpy (&buf
[1], name
);
865 return strstr (ent
->blacklist
.data
, buf
) != NULL
;