]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-spwd.c
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2001 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
);
640 return NSS_STATUS_NOTFOUND
;
643 if (buflen
< ((size_t) outvallen
+ 1))
647 return NSS_STATUS_TRYAGAIN
;
651 saved_oldkey
= ent
->oldkey
;
652 saved_oldlen
= ent
->oldkeylen
;
653 ent
->oldkey
= outkey
;
654 ent
->oldkeylen
= outkeylen
;
657 /* Copy the found data to our buffer */
658 p
= strncpy (buffer
, outval
, buflen
);
660 /* ...and free the data. */
665 parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
, errnop
);
669 ent
->oldkey
= saved_oldkey
;
670 ent
->oldkeylen
= saved_oldlen
;
671 ent
->first
= saved_first
;
673 return NSS_STATUS_TRYAGAIN
;
681 in_blacklist (result
->sp_namp
, strlen (result
->sp_namp
), ent
))
686 copy_spwd_changes (result
, &ent
->pwd
, p2
, p2len
);
688 return NSS_STATUS_SUCCESS
;
691 /* This function handle the +user entrys in /etc/shadow */
692 static enum nss_status
693 getspnam_plususer (const char *name
, struct spwd
*result
, char *buffer
,
694 size_t buflen
, int *errnop
)
696 struct parser_data
*data
= (void *) buffer
;
702 memset (&pwd
, '\0', sizeof (struct spwd
));
708 copy_spwd_changes (&pwd
, result
, NULL
, 0);
710 plen
= spwd_need_buflen (&pwd
);
714 return NSS_STATUS_TRYAGAIN
;
716 p
= buffer
+ (buflen
- plen
);
719 if (use_nisplus
) /* Do the NIS+ query here */
722 char buf
[strlen (name
) + 24 + pwdtablelen
];
724 sprintf(buf
, "[name=%s],%s", name
, pwdtable
);
725 res
= nis_list(buf
, 0, NULL
, NULL
);
726 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
728 enum nss_status status
= niserr2nss (res
->status
);
730 nis_freeresult (res
);
733 parse_res
= _nss_nisplus_parse_spent (res
, result
, buffer
,
737 nis_freeresult (res
);
739 return NSS_STATUS_TRYAGAIN
;
741 nis_freeresult (res
);
745 char *domain
, *outval
, *ptr
;
748 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
751 return NSS_STATUS_NOTFOUND
;
753 if (yp_match (domain
, "shadow.byname", name
, strlen (name
),
754 &outval
, &outvallen
) != YPERR_SUCCESS
)
757 return NSS_STATUS_NOTFOUND
;
759 if (buflen
< ((size_t) outvallen
+ 1))
763 return NSS_STATUS_TRYAGAIN
;
766 ptr
= strncpy (buffer
, outval
, buflen
);
768 while (isspace (*ptr
))
770 parse_res
= _nss_files_parse_spent (ptr
, result
, data
, buflen
, errnop
);
772 return NSS_STATUS_TRYAGAIN
;
777 copy_spwd_changes (result
, &pwd
, p
, plen
);
778 give_spwd_free (&pwd
);
779 /* We found the entry. */
780 return NSS_STATUS_SUCCESS
;
784 /* Give buffer the old len back */
786 give_spwd_free (&pwd
);
788 return NSS_STATUS_RETURN
;
791 static enum nss_status
792 getspent_next_file (struct spwd
*result
, ent_t
*ent
,
793 char *buffer
, size_t buflen
, int *errnop
)
795 struct parser_data
*data
= (void *) buffer
;
804 fgetpos (ent
->stream
, &pos
);
805 buffer
[buflen
- 1] = '\xff';
806 p
= fgets (buffer
, buflen
, ent
->stream
);
807 if (p
== NULL
&& feof (ent
->stream
))
810 return NSS_STATUS_NOTFOUND
;
812 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
814 fsetpos (ent
->stream
, &pos
);
816 return NSS_STATUS_TRYAGAIN
;
819 /* Skip leading blanks. */
823 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
824 /* Parse the line. If it is invalid, loop to
825 get the next line of the file to parse. */
826 || !(parse_res
= _nss_files_parse_spent (p
, result
, data
,
831 /* The parser ran out of space. */
832 fsetpos (ent
->stream
, &pos
);
834 return NSS_STATUS_TRYAGAIN
;
837 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
838 /* This is a real entry. */
842 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
843 && result
->sp_namp
[2] != '\0')
845 /* XXX Do not use fixed length buffers. */
847 char *user
, *host
, *domain
;
848 struct __netgrent netgrdata
;
850 bzero (&netgrdata
, sizeof (struct __netgrent
));
851 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
852 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
853 &netgrdata
, buf2
, sizeof (buf2
),
856 if (user
!= NULL
&& user
[0] != '-')
857 blacklist_store_name (user
, ent
);
859 __internal_endnetgrent (&netgrdata
);
864 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
865 && result
->sp_namp
[2] != '\0')
869 ent
->netgroup
= TRUE
;
871 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
874 status
= getspent_next_nisplus_netgr (NULL
, result
, ent
,
876 buffer
, buflen
, errnop
);
878 status
= getspent_next_nis_netgr (NULL
, result
, ent
,
880 buffer
, buflen
, errnop
);
881 if (status
== NSS_STATUS_RETURN
)
888 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
889 && result
->sp_namp
[1] != '@')
891 blacklist_store_name (&result
->sp_namp
[1], ent
);
896 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
897 && result
->sp_namp
[1] != '@')
899 enum nss_status status
;
901 /* Store the User in the blacklist for the "+" at the end of
903 blacklist_store_name (&result
->sp_namp
[1], ent
);
904 status
= getspnam_plususer (&result
->sp_namp
[1], result
, buffer
,
906 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
909 if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry */
910 || status
== NSS_STATUS_NOTFOUND
) /* entry doesn't exist */
914 if (status
== NSS_STATUS_TRYAGAIN
)
916 fsetpos (ent
->stream
, &pos
);
924 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
928 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
931 return getspent_next_nisplus (result
, ent
, buffer
, buflen
, errnop
);
933 return getspent_next_nis (result
, ent
, buffer
, buflen
, errnop
);
937 return NSS_STATUS_SUCCESS
;
941 static enum nss_status
942 internal_getspent_r (struct spwd
*pw
, ent_t
*ent
,
943 char *buffer
, size_t buflen
, int *errnop
)
949 /* We are searching members in a netgroup */
950 /* Since this is not the first call, we don't need the group name */
952 status
= getspent_next_nisplus_netgr (NULL
, pw
, ent
, NULL
, buffer
,
955 status
= getspent_next_nis_netgr (NULL
, pw
, ent
, NULL
, buffer
, buflen
,
957 if (status
== NSS_STATUS_RETURN
)
958 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
966 return getspent_next_nisplus (pw
, ent
, buffer
, buflen
, errnop
);
968 return getspent_next_nis (pw
, ent
, buffer
, buflen
, errnop
);
971 return getspent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
975 _nss_compat_getspent_r (struct spwd
*pwd
, char *buffer
, size_t buflen
,
978 enum nss_status status
= NSS_STATUS_SUCCESS
;
980 __libc_lock_lock (lock
);
984 __nss_database_lookup ("shadow_compat", "passwd_compat", "nis", &ni
);
985 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
988 /* Be prepared that the setspent function was not called before. */
989 if (ext_ent
.stream
== NULL
)
990 status
= internal_setspent (&ext_ent
);
992 if (status
== NSS_STATUS_SUCCESS
)
993 status
= internal_getspent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
995 __libc_lock_unlock (lock
);
1000 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
1001 static enum nss_status
1002 internal_getspnam_r (const char *name
, struct spwd
*result
, ent_t
*ent
,
1003 char *buffer
, size_t buflen
, int *errnop
)
1005 struct parser_data
*data
= (void *) buffer
;
1015 fgetpos (ent
->stream
, &pos
);
1016 buffer
[buflen
- 1] = '\xff';
1017 p
= fgets (buffer
, buflen
, ent
->stream
);
1018 if (p
== NULL
&& feof (ent
->stream
))
1021 return NSS_STATUS_NOTFOUND
;
1023 if (p
== NULL
|| buffer
[buflen
- 1] != '\xff')
1025 fsetpos (ent
->stream
, &pos
);
1027 return NSS_STATUS_TRYAGAIN
;
1030 /* Skip leading blanks. */
1031 while (isspace (*p
))
1034 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
1035 /* Parse the line. If it is invalid, loop to
1036 get the next line of the file to parse. */
1037 !(parse_res
= _nss_files_parse_spent (p
, result
, data
, buflen
,
1040 if (parse_res
== -1)
1042 /* The parser ran out of space. */
1043 fsetpos (ent
->stream
, &pos
);
1045 return NSS_STATUS_TRYAGAIN
;
1048 /* This is a real entry. */
1049 if (result
->sp_namp
[0] != '+' && result
->sp_namp
[0] != '-')
1051 if (strcmp (result
->sp_namp
, name
) == 0)
1052 return NSS_STATUS_SUCCESS
;
1058 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] == '@'
1059 && result
->sp_namp
[2] != '\0')
1061 /* XXX Do not use fixed length buffers. */
1063 char *user
, *host
, *domain
;
1064 struct __netgrent netgrdata
;
1066 bzero (&netgrdata
, sizeof (struct __netgrent
));
1067 __internal_setnetgrent (&result
->sp_namp
[2], &netgrdata
);
1068 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
1069 buf2
, sizeof (buf2
), errnop
))
1071 if (user
!= NULL
&& user
[0] != '-')
1072 if (strcmp (user
, name
) == 0)
1075 return NSS_STATUS_NOTFOUND
;
1078 __internal_endnetgrent (&netgrdata
);
1083 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '@'
1084 && result
->sp_namp
[2] != '\0')
1086 char *buf
= strdupa (&result
->sp_namp
[2]);
1089 ent
->netgroup
= TRUE
;
1091 copy_spwd_changes (&ent
->pwd
, result
, NULL
, 0);
1096 status
= getspent_next_nisplus_netgr (name
, result
, ent
, buf
,
1097 buffer
, buflen
, errnop
);
1099 status
= getspent_next_nis_netgr (name
, result
, ent
, buf
,
1100 buffer
, buflen
, errnop
);
1101 if (status
== NSS_STATUS_RETURN
)
1104 if (status
== NSS_STATUS_SUCCESS
1105 && strcmp (result
->sp_namp
, name
) == 0)
1106 return NSS_STATUS_SUCCESS
;
1107 } while (status
== NSS_STATUS_SUCCESS
);
1112 if (result
->sp_namp
[0] == '-' && result
->sp_namp
[1] != '\0'
1113 && result
->sp_namp
[1] != '@')
1115 if (strcmp (&result
->sp_namp
[1], name
) == 0)
1118 return NSS_STATUS_NOTFOUND
;
1125 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] != '\0'
1126 && result
->sp_namp
[1] != '@')
1128 if (strcmp (name
, &result
->sp_namp
[1]) == 0)
1130 enum nss_status status
;
1132 status
= getspnam_plususer (name
, result
, buffer
, buflen
,
1134 if (status
== NSS_STATUS_RETURN
)
1136 /* We couldn't parse the entry */
1138 return NSS_STATUS_NOTFOUND
;
1146 if (result
->sp_namp
[0] == '+' && result
->sp_namp
[1] == '\0')
1148 enum nss_status status
;
1150 status
= getspnam_plususer (name
, result
, buffer
, buflen
, errnop
);
1151 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1154 return NSS_STATUS_NOTFOUND
;
1160 return NSS_STATUS_SUCCESS
;
1164 _nss_compat_getspnam_r (const char *name
, struct spwd
*pwd
,
1165 char *buffer
, size_t buflen
, int *errnop
)
1167 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
1168 {NULL
, NULL
, 0, 0, 0, 0, 0, 0, 0}};
1169 enum nss_status status
;
1171 if (name
[0] == '-' || name
[0] == '+')
1174 return NSS_STATUS_NOTFOUND
;
1179 __nss_database_lookup ("shadow_compat", "passwd_compat", "nis", &ni
);
1180 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
1183 status
= internal_setspent (&ent
);
1184 if (status
!= NSS_STATUS_SUCCESS
)
1187 status
= internal_getspnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
1189 internal_endspent (&ent
);
1194 /* Support routines for remembering -@netgroup and -user entries.
1195 The names are stored in a single string with `|' as separator. */
1197 blacklist_store_name (const char *name
, ent_t
*ent
)
1199 int namelen
= strlen (name
);
1202 /* first call, setup cache */
1203 if (ent
->blacklist
.size
== 0)
1205 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
1206 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
1207 if (ent
->blacklist
.data
== NULL
)
1209 ent
->blacklist
.data
[0] = '|';
1210 ent
->blacklist
.data
[1] = '\0';
1211 ent
->blacklist
.current
= 1;
1215 if (in_blacklist (name
, namelen
, ent
))
1216 return; /* no duplicates */
1218 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
1220 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
1221 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
1224 free (ent
->blacklist
.data
);
1225 ent
->blacklist
.size
= 0;
1228 ent
->blacklist
.data
= tmp
;
1232 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
1235 ent
->blacklist
.current
+= namelen
+ 1;
1240 /* Returns TRUE if ent->blacklist contains name, else FALSE. */
1242 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
1244 char buf
[namelen
+ 3];
1247 if (ent
->blacklist
.data
== NULL
)
1251 cp
= stpcpy (&buf
[1], name
);
1254 return strstr (ent
->blacklist
.data
, buf
) != NULL
;