1 /* Copyright (C) 1996,1997,1998,1999,2001,2002 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 <rpcsvc/yp.h>
29 #include <rpcsvc/ypclnt.h>
30 #include <rpcsvc/nis.h>
34 #include "nss-nisplus.h"
35 #include "nisplus-parser.h"
37 static service_user
*ni
;
38 static bool_t use_nisplus
; /* default: passwd_compat: nis */
39 static nis_name pwdtable
; /* Name of the password table */
40 static size_t pwdtablelen
;
42 /* Get the declaration of the parser function. */
44 #define STRUCTURE spwd
46 #include <nss/nss_files/files-parse.c>
48 /* Structure for remembering -@netgroup and -user members ... */
49 #define BLACKLIST_INITIAL_SIZE 512
50 #define BLACKLIST_INCREMENT 256
67 struct blacklist_t blacklist
;
69 struct __netgrent netgrdata
;
71 typedef struct ent_t ent_t
;
73 static ent_t ext_ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
74 {NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
76 /* Protect global state against multiple changers. */
77 __libc_lock_define_initialized (static, lock
)
79 /* Prototypes for local functions. */
80 static void blacklist_store_name (const char *, ent_t
*);
81 static int in_blacklist (const char *, int, ent_t
*);
84 give_spwd_free (struct spwd
*pwd
)
86 if (pwd
->sp_namp
!= NULL
)
88 if (pwd
->sp_pwdp
!= NULL
)
91 memset (pwd
, '\0', sizeof (struct spwd
));
99 spwd_need_buflen (struct spwd
*pwd
)
103 if (pwd
->sp_pwdp
!= NULL
)
104 len
+= strlen (pwd
->sp_pwdp
) + 1;
110 copy_spwd_changes (struct spwd
*dest
, struct spwd
*src
,
111 char *buffer
, size_t buflen
)
113 if (src
->sp_pwdp
!= NULL
&& strlen (src
->sp_pwdp
))
116 dest
->sp_pwdp
= strdup (src
->sp_pwdp
);
117 else if (dest
->sp_pwdp
&&
118 strlen (dest
->sp_pwdp
) >= strlen (src
->sp_pwdp
))
119 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
122 dest
->sp_pwdp
= buffer
;
123 strcpy (dest
->sp_pwdp
, src
->sp_pwdp
);
124 buffer
+= strlen (dest
->sp_pwdp
) + 1;
125 buflen
= buflen
- (strlen (dest
->sp_pwdp
) + 1);
128 if (src
->sp_lstchg
!= 0)
129 dest
->sp_lstchg
= src
->sp_lstchg
;
130 if (src
->sp_min
!= 0)
131 dest
->sp_min
= src
->sp_min
;
132 if (src
->sp_max
!= 0)
133 dest
->sp_max
= src
->sp_max
;
134 if (src
->sp_warn
!= -1)
135 dest
->sp_warn
= src
->sp_warn
;
136 if (src
->sp_inact
!= -1)
137 dest
->sp_inact
= src
->sp_inact
;
138 if (src
->sp_expire
!= -1)
139 dest
->sp_expire
= src
->sp_expire
;
140 if (src
->sp_flag
!= ~0ul)
141 dest
->sp_flag
= src
->sp_flag
;
144 static enum nss_status
145 internal_setspent (ent_t
*ent
)
147 enum nss_status status
= NSS_STATUS_SUCCESS
;
149 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
151 /* If something was left over free it. */
153 __internal_endnetgrent (&ent
->netgrdata
);
155 if (ent
->oldkey
!= NULL
)
162 if (ent
->result
!= NULL
)
164 nis_freeresult (ent
->result
);
168 if (pwdtable
== NULL
)
170 static const char key
[] = "passwd.org_dir.";
171 const char *local_dir
= nis_local_directory ();
172 size_t len_local_dir
= strlen (local_dir
);
174 pwdtable
= malloc (sizeof (key
) + len_local_dir
);
175 if (pwdtable
== NULL
)
176 return NSS_STATUS_TRYAGAIN
;
178 pwdtablelen
= ((char *) mempcpy (mempcpy (pwdtable
,
179 key
, sizeof (key
) - 1),
180 local_dir
, len_local_dir
+ 1)
184 if (ent
->blacklist
.data
!= NULL
)
186 ent
->blacklist
.current
= 1;
187 ent
->blacklist
.data
[0] = '|';
188 ent
->blacklist
.data
[1] = '\0';
191 ent
->blacklist
.current
= 0;
193 if (ent
->stream
== NULL
)
195 ent
->stream
= fopen ("/etc/shadow", "r");
197 if (ent
->stream
== NULL
)
198 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
201 /* We have to make sure the file is `closed on exec'. */
204 result
= flags
= fcntl (fileno (ent
->stream
), F_GETFD
, 0);
208 result
= fcntl (fileno (ent
->stream
), F_SETFD
, flags
);
212 /* Something went wrong. Close the stream and return a
214 fclose (ent
->stream
);
216 status
= NSS_STATUS_UNAVAIL
;
221 rewind (ent
->stream
);
223 give_spwd_free (&ent
->pwd
);
230 _nss_compat_setspent (int stayopen
)
232 enum nss_status result
;
234 __libc_lock_lock (lock
);
238 __nss_database_lookup ("shadow_compat", "passwd_compat", "nis", &ni
);
239 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
242 result
= internal_setspent (&ext_ent
);
244 __libc_lock_unlock (lock
);
250 static enum nss_status
251 internal_endspent (ent_t
*ent
)
253 if (ent
->stream
!= NULL
)
255 fclose (ent
->stream
);
260 __internal_endnetgrent (&ent
->netgrdata
);
262 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
264 if (ent
->oldkey
!= NULL
)
271 if (ent
->result
!= NULL
)
273 nis_freeresult (ent
->result
);
277 if (ent
->blacklist
.data
!= NULL
)
279 ent
->blacklist
.current
= 1;
280 ent
->blacklist
.data
[0] = '|';
281 ent
->blacklist
.data
[1] = '\0';
284 ent
->blacklist
.current
= 0;
286 give_spwd_free (&ent
->pwd
);
288 return NSS_STATUS_SUCCESS
;
292 _nss_compat_endspent (void)
294 enum nss_status result
;
296 __libc_lock_lock (lock
);
298 result
= internal_endspent (&ext_ent
);
300 __libc_lock_unlock (lock
);
306 static enum nss_status
307 getspent_next_nis_netgr (const char *name
, struct spwd
*result
, ent_t
*ent
,
308 char *group
, char *buffer
, size_t buflen
, int *errnop
)
310 struct parser_data
*data
= (void *) buffer
;
311 char *ypdomain
, *host
, *user
, *domain
, *outval
, *p
, *p2
;
312 int status
, outvallen
;
315 if (yp_get_default_domain (&ypdomain
) != YPERR_SUCCESS
)
319 give_spwd_free (&ent
->pwd
);
320 return NSS_STATUS_UNAVAIL
;
323 if (ent
->first
== TRUE
)
325 bzero (&ent
->netgrdata
, sizeof (struct __netgrent
));
326 __internal_setnetgrent (group
, &ent
->netgrdata
);
335 saved_cursor
= ent
->netgrdata
.cursor
;
336 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
337 &ent
->netgrdata
, buffer
, buflen
,
341 __internal_endnetgrent (&ent
->netgrdata
);
343 give_spwd_free (&ent
->pwd
);
344 return NSS_STATUS_RETURN
;
347 if (user
== NULL
|| user
[0] == '-')
350 if (domain
!= NULL
&& strcmp (ypdomain
, domain
) != 0)
353 /* If name != NULL, we are called from getpwnam */
355 if (strcmp (user
, name
) != 0)
358 if (yp_match (ypdomain
, "shadow.byname", user
,
359 strlen (user
), &outval
, &outvallen
)
363 p2len
= spwd_need_buflen (&ent
->pwd
);
368 return NSS_STATUS_TRYAGAIN
;
370 p2
= buffer
+ (buflen
- p2len
);
372 if (buflen
< ((size_t) outval
+ 1))
376 return NSS_STATUS_TRYAGAIN
;
378 p
= strncpy (buffer
, outval
, buflen
);
382 parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
, errnop
);
385 ent
->netgrdata
.cursor
= saved_cursor
;
387 return NSS_STATUS_TRYAGAIN
;
392 /* Store the User in the blacklist for the "+" at the end of
394 blacklist_store_name (result
->sp_namp
, ent
);
395 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
400 return NSS_STATUS_SUCCESS
;
403 static enum nss_status
404 getspent_next_nisplus_netgr (const char *name
, struct spwd
*result
,
405 ent_t
*ent
, char *group
, char *buffer
,
406 size_t buflen
, int *errnop
)
408 char *ypdomain
, *host
, *user
, *domain
, *p2
;
409 int status
, parse_res
;
413 /* Maybe we should use domainname here ? We need the current
414 domainname for the domain field in netgroups */
415 if (yp_get_default_domain (&ypdomain
) != YPERR_SUCCESS
)
419 give_spwd_free (&ent
->pwd
);
420 return NSS_STATUS_UNAVAIL
;
423 if (ent
->first
== TRUE
)
425 bzero (&ent
->netgrdata
, sizeof (struct __netgrent
));
426 __internal_setnetgrent (group
, &ent
->netgrdata
);
434 saved_cursor
= ent
->netgrdata
.cursor
;
435 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
436 &ent
->netgrdata
, buffer
, buflen
,
440 __internal_endnetgrent (&ent
->netgrdata
);
442 give_spwd_free (&ent
->pwd
);
443 return NSS_STATUS_RETURN
;
446 if (user
== NULL
|| user
[0] == '-')
449 if (domain
!= NULL
&& strcmp (ypdomain
, domain
) != 0)
452 /* If name != NULL, we are called from getpwnam */
454 if (strcmp (user
, name
) != 0)
457 p2len
= spwd_need_buflen (&ent
->pwd
);
461 return NSS_STATUS_TRYAGAIN
;
463 p2
= buffer
+ (buflen
- p2len
);
466 char buf
[strlen (user
) + 30 + pwdtablelen
];
467 sprintf (buf
, "[name=%s],%s", user
, pwdtable
);
468 nisres
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
470 if (niserr2nss (nisres
->status
) != NSS_STATUS_SUCCESS
)
472 nis_freeresult (nisres
);
475 parse_res
= _nss_nisplus_parse_spent (nisres
, result
, buffer
,
479 nis_freeresult (nisres
);
481 return NSS_STATUS_TRYAGAIN
;
483 nis_freeresult (nisres
);
487 /* Store the User in the blacklist for the "+" at the end of
489 blacklist_store_name (result
->sp_namp
, ent
);
490 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
495 return NSS_STATUS_SUCCESS
;
498 static enum nss_status
499 getspent_next_nisplus (struct spwd
*result
, ent_t
*ent
, char *buffer
,
500 size_t buflen
, int *errnop
)
506 p2len
= spwd_need_buflen (&ent
->pwd
);
510 return NSS_STATUS_TRYAGAIN
;
512 p2
= buffer
+ (buflen
- p2len
);
517 nis_result
*saved_res
;
522 saved_res
= ent
->result
;
524 ent
->result
= nis_first_entry (pwdtable
);
525 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
528 give_spwd_free (&ent
->pwd
);
529 return niserr2nss (ent
->result
->status
);
538 saved_res
= ent
->result
;
540 res
= nis_next_entry (pwdtable
, &ent
->result
->cookie
);
542 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
544 nis_freeresult (saved_res
);
546 give_spwd_free (&ent
->pwd
);
547 return niserr2nss (ent
->result
->status
);
550 parse_res
= _nss_nisplus_parse_spent (ent
->result
, result
, buffer
,
554 ent
->first
= saved_first
;
555 nis_freeresult (ent
->result
);
556 ent
->result
= saved_res
;
558 return NSS_STATUS_TRYAGAIN
;
563 nis_freeresult (saved_res
);
566 in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
567 parse_res
= 0; /* if result->pw_name in blacklist,search next entry */
571 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
573 return NSS_STATUS_SUCCESS
;
577 static enum nss_status
578 getspent_next_nis (struct spwd
*result
, ent_t
*ent
,
579 char *buffer
, size_t buflen
, int *errnop
)
581 struct parser_data
*data
= (void *) buffer
;
582 char *domain
, *outkey
, *outval
, *p
, *p2
;
583 int outkeylen
, outvallen
, parse_res
;
586 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
589 give_spwd_free (&ent
->pwd
);
590 return NSS_STATUS_UNAVAIL
;
593 p2len
= spwd_need_buflen (&ent
->pwd
);
597 return NSS_STATUS_TRYAGAIN
;
599 p2
= buffer
+ (buflen
- p2len
);
609 if (yp_first (domain
, "shadow.byname", &outkey
, &outkeylen
,
610 &outval
, &outvallen
) != YPERR_SUCCESS
)
613 give_spwd_free (&ent
->pwd
);
614 return NSS_STATUS_UNAVAIL
;
617 if (buflen
< ((size_t) outvallen
+ 1))
621 return NSS_STATUS_TRYAGAIN
;
625 saved_oldkey
= ent
->oldkey
;
626 saved_oldlen
= ent
->oldkeylen
;
627 ent
->oldkey
= outkey
;
628 ent
->oldkeylen
= outkeylen
;
633 if (yp_next (domain
, "shadow.byname", ent
->oldkey
, ent
->oldkeylen
,
634 &outkey
, &outkeylen
, &outval
, &outvallen
)
638 give_spwd_free (&ent
->pwd
);
639 return NSS_STATUS_NOTFOUND
;
642 if (buflen
< ((size_t) outvallen
+ 1))
646 return NSS_STATUS_TRYAGAIN
;
650 saved_oldkey
= ent
->oldkey
;
651 saved_oldlen
= ent
->oldkeylen
;
652 ent
->oldkey
= outkey
;
653 ent
->oldkeylen
= outkeylen
;
656 /* Copy the found data to our buffer */
657 p
= strncpy (buffer
, outval
, buflen
);
659 /* ...and free the data. */
664 parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
, errnop
);
668 ent
->oldkey
= saved_oldkey
;
669 ent
->oldkeylen
= saved_oldlen
;
670 ent
->first
= saved_first
;
672 return NSS_STATUS_TRYAGAIN
;
680 in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
685 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
687 return NSS_STATUS_SUCCESS
;
690 /* This function handle the +user entrys in /etc/shadow */
691 static enum nss_status
692 getspnam_plususer (const char *name
, struct spwd
*result
, char *buffer
,
693 size_t buflen
, int *errnop
)
695 struct parser_data
*data
= (void *) buffer
;
701 memset (&pwd
, '\0', sizeof (struct spwd
));
707 copy_spwd_changes (&pwd
, result
, NULL
, 0);
709 plen
= spwd_need_buflen (&pwd
);
713 return NSS_STATUS_TRYAGAIN
;
715 p
= buffer
+ (buflen
- plen
);
718 if (use_nisplus
) /* Do the NIS+ query here */
721 char buf
[strlen (name
) + 24 + pwdtablelen
];
723 sprintf(buf
, "[name=%s],%s", name
, pwdtable
);
724 res
= nis_list(buf
, 0, NULL
, NULL
);
725 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
727 enum nss_status status
= niserr2nss (res
->status
);
729 nis_freeresult (res
);
732 parse_res
= _nss_nisplus_parse_spent (res
, result
, buffer
,
736 nis_freeresult (res
);
738 return NSS_STATUS_TRYAGAIN
;
740 nis_freeresult (res
);
744 char *domain
, *outval
, *ptr
;
747 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
748 return NSS_STATUS_NOTFOUND
;
750 if (yp_match (domain
, "shadow.byname", name
, strlen (name
),
751 &outval
, &outvallen
) != YPERR_SUCCESS
)
752 return NSS_STATUS_NOTFOUND
;
754 if (buflen
< ((size_t) outvallen
+ 1))
758 return NSS_STATUS_TRYAGAIN
;
761 ptr
= strncpy (buffer
, outval
, buflen
);
763 while (isspace (*ptr
))
765 parse_res
= _nss_files_parse_spent (ptr
, result
, data
, buflen
, errnop
);
767 return NSS_STATUS_TRYAGAIN
;
772 copy_spwd_changes (result
, &pwd
, p
, plen
);
773 give_spwd_free (&pwd
);
774 /* We found the entry. */
775 return NSS_STATUS_SUCCESS
;
779 /* Give buffer the old len back */
781 give_spwd_free (&pwd
);
783 return NSS_STATUS_RETURN
;
786 static enum nss_status
787 getspent_next_file (struct spwd
*result
, ent_t
*ent
,
788 char *buffer
, size_t buflen
, int *errnop
)
790 struct parser_data
*data
= (void *) buffer
;
799 fgetpos (ent
->stream
, &pos
);
800 buffer
[buflen
- 1] = '\xff';
801 p
= fgets (buffer
, buflen
, ent
->stream
);
802 if (p
== NULL
&& feof (ent
->stream
))
803 return NSS_STATUS_NOTFOUND
;
805 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
807 fsetpos (ent
->stream
, &pos
);
809 return NSS_STATUS_TRYAGAIN
;
812 /* Skip leading blanks. */
816 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
817 /* Parse the line. If it is invalid, loop to
818 get the next line of the file to parse. */
819 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
,
824 /* The parser ran out of space. */
825 fsetpos (ent
->stream
, &pos
);
827 return NSS_STATUS_TRYAGAIN
;
830 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
831 /* This is a real entry. */
835 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
836 && result
->sp_namp
[2] != '\0')
838 /* XXX Do not use fixed length buffers. */
840 char *user
, *host
, *domain
;
841 struct __netgrent netgrdata
;
843 bzero (&netgrdata
, sizeof (struct __netgrent
));
844 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
845 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
846 &netgrdata
, buf2
, sizeof (buf2
),
849 if (user
!= NULL
&& user
[0] != '-')
850 blacklist_store_name (user
, ent
);
852 __internal_endnetgrent (&netgrdata
);
857 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
858 && result
->sp_namp
[2] != '\0')
862 ent
->netgroup
= TRUE
;
864 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
867 status
= getspent_next_nisplus_netgr (NULL
, result
, ent
,
869 buffer
, buflen
, errnop
);
871 status
= getspent_next_nis_netgr (NULL
, result
, ent
,
873 buffer
, buflen
, errnop
);
874 if (status
== NSS_STATUS_RETURN
)
881 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
882 && result
->sp_namp
[1] != '@')
884 blacklist_store_name (&result
->sp_namp
[1], ent
);
889 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
890 && result
->sp_namp
[1] != '@')
892 enum nss_status status
;
894 /* Store the User in the blacklist for the "+" at the end of
896 blacklist_store_name (&result
->sp_namp
[1], ent
);
897 status
= getspnam_plususer (&result
->sp_namp
[1], result
, buffer
,
899 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
902 if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry */
903 || status
== NSS_STATUS_NOTFOUND
) /* entry doesn't exist */
907 if (status
== NSS_STATUS_TRYAGAIN
)
909 fsetpos (ent
->stream
, &pos
);
917 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
921 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
924 return getspent_next_nisplus (result
, ent
, buffer
, buflen
, errnop
);
926 return getspent_next_nis (result
, ent
, buffer
, buflen
, errnop
);
930 return NSS_STATUS_SUCCESS
;
934 static enum nss_status
935 internal_getspent_r (struct spwd
*pw
, ent_t
*ent
,
936 char *buffer
, size_t buflen
, int *errnop
)
942 /* We are searching members in a netgroup */
943 /* Since this is not the first call, we don't need the group name */
945 status
= getspent_next_nisplus_netgr (NULL
, pw
, ent
, NULL
, buffer
,
948 status
= getspent_next_nis_netgr (NULL
, pw
, ent
, NULL
, buffer
, buflen
,
950 if (status
== NSS_STATUS_RETURN
)
951 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
959 return getspent_next_nisplus (pw
, ent
, buffer
, buflen
, errnop
);
961 return getspent_next_nis (pw
, ent
, buffer
, buflen
, errnop
);
964 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
968 _nss_compat_getspent_r (struct spwd
*pwd
, char *buffer
, size_t buflen
,
971 enum nss_status status
= NSS_STATUS_SUCCESS
;
973 __libc_lock_lock (lock
);
977 __nss_database_lookup ("shadow_compat", "passwd_compat", "nis", &ni
);
978 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
981 /* Be prepared that the setspent function was not called before. */
982 if (ext_ent
.stream
== NULL
)
983 status
= internal_setspent (&ext_ent
);
985 if (status
== NSS_STATUS_SUCCESS
)
986 status
= internal_getspent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
988 __libc_lock_unlock (lock
);
993 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
994 static enum nss_status
995 internal_getspnam_r (const char *name
, struct spwd
*result
, ent_t
*ent
,
996 char *buffer
, size_t buflen
, int *errnop
)
998 struct parser_data
*data
= (void *) buffer
;
1008 fgetpos (ent
->stream
, &pos
);
1009 buffer
[buflen
- 1] = '\xff';
1010 p
= fgets (buffer
, buflen
, ent
->stream
);
1011 if (p
== NULL
&& feof (ent
->stream
))
1012 return NSS_STATUS_NOTFOUND
;
1014 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
1016 fsetpos (ent
->stream
, &pos
);
1018 return NSS_STATUS_TRYAGAIN
;
1021 /* Skip leading blanks. */
1022 while (isspace (*p
))
1025 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
1026 /* Parse the line. If it is invalid, loop to
1027 get the next line of the file to parse. */
1028 !(parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
,
1031 if (parse_res
== -1)
1033 /* The parser ran out of space. */
1034 fsetpos (ent
->stream
, &pos
);
1036 return NSS_STATUS_TRYAGAIN
;
1039 /* This is a real entry. */
1040 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
1042 if (strcmp (result
->sp_namp
, name
) == 0)
1043 return NSS_STATUS_SUCCESS
;
1049 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
1050 && result
->sp_namp
[2] != '\0')
1052 /* XXX Do not use fixed length buffers. */
1054 char *user
, *host
, *domain
;
1055 struct __netgrent netgrdata
;
1057 bzero (&netgrdata
, sizeof (struct __netgrent
));
1058 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
1059 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
1060 buf2
, sizeof (buf2
), errnop
))
1062 if (user
!= NULL
&& user
[0] != '-')
1063 if (strcmp (user
, name
) == 0)
1064 return NSS_STATUS_NOTFOUND
;
1066 __internal_endnetgrent (&netgrdata
);
1071 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
1072 && result
->sp_namp
[2] != '\0')
1074 char *buf
= strdupa (&result
->sp_namp
[2]);
1077 ent
->netgroup
= TRUE
;
1079 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
1084 status
= getspent_next_nisplus_netgr (name
, result
, ent
, buf
,
1085 buffer
, buflen
, errnop
);
1087 status
= getspent_next_nis_netgr (name
, result
, ent
, buf
,
1088 buffer
, buflen
, errnop
);
1089 if (status
== NSS_STATUS_RETURN
)
1092 if (status
== NSS_STATUS_SUCCESS
1093 && strcmp (result
->sp_namp
, name
) == 0)
1094 return NSS_STATUS_SUCCESS
;
1095 } while (status
== NSS_STATUS_SUCCESS
);
1100 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
1101 && result
->sp_namp
[1] != '@')
1103 if (strcmp (&result
->sp_namp
[1], name
) == 0)
1104 return NSS_STATUS_NOTFOUND
;
1110 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
1111 && result
->sp_namp
[1] != '@')
1113 if (strcmp (name
, &result
->sp_namp
[1]) == 0)
1115 enum nss_status status
;
1117 status
= getspnam_plususer (name
, result
, buffer
, buflen
,
1119 if (status
== NSS_STATUS_RETURN
)
1120 /* We couldn't parse the entry */
1121 return NSS_STATUS_NOTFOUND
;
1128 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
1130 enum nss_status status
;
1132 status
= getspnam_plususer (name
, result
, buffer
, buflen
, errnop
);
1133 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1134 return NSS_STATUS_NOTFOUND
;
1139 return NSS_STATUS_SUCCESS
;
1143 _nss_compat_getspnam_r (const char *name
, struct spwd
*pwd
,
1144 char *buffer
, size_t buflen
, int *errnop
)
1146 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
1147 {NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
1148 enum nss_status status
;
1150 if (name
[0] == '-' || name
[0] == '+')
1151 return NSS_STATUS_NOTFOUND
;
1155 __nss_database_lookup ("shadow_compat", "passwd_compat", "nis", &ni
);
1156 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
1159 status
= internal_setspent (&ent
);
1160 if (status
!= NSS_STATUS_SUCCESS
)
1163 status
= internal_getspnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
1165 internal_endspent (&ent
);
1170 /* Support routines for remembering -@netgroup and -user entries.
1171 The names are stored in a single string with `|' as separator. */
1173 blacklist_store_name (const char *name
, ent_t
*ent
)
1175 int namelen
= strlen (name
);
1178 /* first call, setup cache */
1179 if (ent
->blacklist
.size
== 0)
1181 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
1182 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
1183 if (ent
->blacklist
.data
== NULL
)
1185 ent
->blacklist
.data
[0] = '|';
1186 ent
->blacklist
.data
[1] = '\0';
1187 ent
->blacklist
.current
= 1;
1191 if (in_blacklist (name
, namelen
, ent
))
1192 return; /* no duplicates */
1194 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
1196 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
1197 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
1200 free (ent
->blacklist
.data
);
1201 ent
->blacklist
.size
= 0;
1204 ent
->blacklist
.data
= tmp
;
1208 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
1211 ent
->blacklist
.current
+= namelen
+ 1;
1216 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
1218 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
1220 char buf
[namelen
+ 3];
1223 if (ent
->blacklist
.data
== NULL
)
1227 cp
= stpcpy (&buf
[1], name
);
1230 return strstr (ent
->blacklist
.data
, buf
) != NULL
;