]>
sourceware.org Git - glibc.git/blob - nis/nss_compat/compat-pwd.c
1 /* Copyright (C) 1996, 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/yp.h>
29 #include <rpcsvc/ypclnt.h>
30 #include <rpcsvc/nis.h>
33 /* Comment out the following line for the production version. */
34 /* #define NDEBUG 1 */
38 #include "nss-nisplus.h"
39 #include "nisplus-parser.h"
41 static service_user
*ni
= NULL
;
42 static bool_t use_nisplus
= FALSE
; /* default: passwd_compat: nis */
43 static nis_name pwdtable
= NULL
; /* Name of the pwd table */
44 static size_t pwdtablelen
= 0;
46 /* Get the declaration of the parser function. */
48 #define STRUCTURE passwd
50 #include <nss/nss_files/files-parse.c>
52 /* Structure for remembering -@netgroup and -user members ... */
53 #define BLACKLIST_INITIAL_SIZE 512
54 #define BLACKLIST_INCREMENT 256
71 struct blacklist_t blacklist
;
73 struct __netgrent netgrdata
;
75 typedef struct ent_t ent_t
;
77 static ent_t ext_ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
78 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
80 /* Protect global state against multiple changers. */
81 __libc_lock_define_initialized (static, lock
)
83 /* Prototypes for local functions. */
84 static void blacklist_store_name (const char *, ent_t
*);
85 static int in_blacklist (const char *, int, ent_t
*);
88 give_pwd_free (struct passwd
*pwd
)
90 if (pwd
->pw_name
!= NULL
)
92 if (pwd
->pw_passwd
!= NULL
)
93 free (pwd
->pw_passwd
);
94 if (pwd
->pw_gecos
!= NULL
)
96 if (pwd
->pw_dir
!= NULL
)
98 if (pwd
->pw_shell
!= NULL
)
101 memset (pwd
, '\0', sizeof (struct passwd
));
105 pwd_need_buflen (struct passwd
*pwd
)
109 if (pwd
->pw_passwd
!= NULL
)
110 len
+= strlen (pwd
->pw_passwd
) + 1;
112 if (pwd
->pw_gecos
!= NULL
)
113 len
+= strlen (pwd
->pw_gecos
) + 1;
115 if (pwd
->pw_dir
!= NULL
)
116 len
+= strlen (pwd
->pw_dir
) + 1;
118 if (pwd
->pw_shell
!= NULL
)
119 len
+= strlen (pwd
->pw_shell
) + 1;
125 copy_pwd_changes (struct passwd
*dest
, struct passwd
*src
,
126 char *buffer
, size_t buflen
)
128 if (src
->pw_passwd
!= NULL
&& strlen (src
->pw_passwd
))
131 dest
->pw_passwd
= strdup (src
->pw_passwd
);
132 else if (dest
->pw_passwd
&&
133 strlen (dest
->pw_passwd
) >= strlen (src
->pw_passwd
))
134 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
137 dest
->pw_passwd
= buffer
;
138 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
139 buffer
+= strlen (dest
->pw_passwd
) + 1;
140 buflen
= buflen
- (strlen (dest
->pw_passwd
) + 1);
144 if (src
->pw_gecos
!= NULL
&& strlen (src
->pw_gecos
))
147 dest
->pw_gecos
= strdup (src
->pw_gecos
);
148 else if (dest
->pw_gecos
&&
149 strlen (dest
->pw_gecos
) >= strlen (src
->pw_gecos
))
150 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
153 dest
->pw_gecos
= buffer
;
154 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
155 buffer
+= strlen (dest
->pw_gecos
) + 1;
156 buflen
= buflen
- (strlen (dest
->pw_gecos
) + 1);
159 if (src
->pw_dir
!= NULL
&& strlen (src
->pw_dir
))
162 dest
->pw_dir
= strdup (src
->pw_dir
);
163 else if (dest
->pw_dir
&&
164 strlen (dest
->pw_dir
) >= strlen (src
->pw_dir
))
165 strcpy (dest
->pw_dir
, src
->pw_dir
);
168 dest
->pw_dir
= buffer
;
169 strcpy (dest
->pw_dir
, src
->pw_dir
);
170 buffer
+= strlen (dest
->pw_dir
) + 1;
171 buflen
= buflen
- (strlen (dest
->pw_dir
) + 1);
175 if (src
->pw_shell
!= NULL
&& strlen (src
->pw_shell
))
178 dest
->pw_shell
= strdup (src
->pw_shell
);
179 else if (dest
->pw_shell
&&
180 strlen (dest
->pw_shell
) >= strlen (src
->pw_shell
))
181 strcpy (dest
->pw_shell
, src
->pw_shell
);
184 dest
->pw_shell
= buffer
;
185 strcpy (dest
->pw_shell
, src
->pw_shell
);
186 buffer
+= strlen (dest
->pw_shell
) + 1;
187 buflen
= buflen
- (strlen (dest
->pw_shell
) + 1);
192 static enum nss_status
193 internal_setpwent (ent_t
*ent
)
195 enum nss_status status
= NSS_STATUS_SUCCESS
;
197 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
199 /* If something was left over free it. */
201 __internal_endnetgrent (&ent
->netgrdata
);
203 if (ent
->oldkey
!= NULL
)
210 if (ent
->result
!= NULL
)
212 nis_freeresult (ent
->result
);
216 if (pwdtable
== NULL
)
218 static const char key
[] = "passwd.org_dir.";
219 const char *local_dir
= nis_local_directory ();
220 size_t len_local_dir
= strlen (local_dir
);
222 pwdtable
= malloc (sizeof (key
) + len_local_dir
);
223 if (pwdtable
== NULL
)
224 return NSS_STATUS_TRYAGAIN
;
226 pwdtablelen
= ((char *) mempcpy (mempcpy (pwdtable
,
227 key
, sizeof (key
) - 1),
228 local_dir
, len_local_dir
+ 1)
231 /* *Maybe* (I'm no NIS expert) we have to duplicate the `local_dir'
232 value since it might change during our work. So add a test here. */
233 assert (pwdtablelen
== sizeof (key
) + len_local_dir
);
236 ent
->blacklist
.current
= 0;
237 if (ent
->blacklist
.data
!= NULL
)
238 ent
->blacklist
.data
[0] = '\0';
240 if (ent
->stream
== NULL
)
242 ent
->stream
= fopen ("/etc/passwd", "r");
244 if (ent
->stream
== NULL
)
245 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
248 /* We have to make sure the file is `closed on exec'. */
251 result
= flags
= fcntl (fileno (ent
->stream
), F_GETFD
, 0);
255 result
= fcntl (fileno (ent
->stream
), F_SETFD
, flags
);
259 /* Something went wrong. Close the stream and return a
261 fclose (ent
->stream
);
263 status
= NSS_STATUS_UNAVAIL
;
268 rewind (ent
->stream
);
270 give_pwd_free (&ent
->pwd
);
277 _nss_compat_setpwent (void)
279 enum nss_status result
;
281 __libc_lock_lock (lock
);
285 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
286 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
289 result
= internal_setpwent (&ext_ent
);
291 __libc_lock_unlock (lock
);
297 static enum nss_status
298 internal_endpwent (ent_t
*ent
)
300 if (ent
->stream
!= NULL
)
302 fclose (ent
->stream
);
307 __internal_endnetgrent (&ent
->netgrdata
);
309 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
311 if (ent
->oldkey
!= NULL
)
318 if (ent
->result
!= NULL
)
320 nis_freeresult (ent
->result
);
324 ent
->blacklist
.current
= 0;
325 if (ent
->blacklist
.data
!= NULL
)
326 ent
->blacklist
.data
[0] = '\0';
328 give_pwd_free (&ent
->pwd
);
330 return NSS_STATUS_SUCCESS
;
334 _nss_compat_endpwent (void)
336 enum nss_status result
;
338 __libc_lock_lock (lock
);
340 result
= internal_endpwent (&ext_ent
);
342 __libc_lock_unlock (lock
);
347 static enum nss_status
348 getpwent_next_nis_netgr (const char *name
, struct passwd
*result
, ent_t
*ent
,
349 char *group
, char *buffer
, size_t buflen
)
351 struct parser_data
*data
= (void *) buffer
;
352 char *ypdomain
, *host
, *user
, *domain
, *outval
, *p
, *p2
;
353 int status
, outvallen
;
356 if (yp_get_default_domain (&ypdomain
) != YPERR_SUCCESS
)
360 give_pwd_free (&ent
->pwd
);
361 return NSS_STATUS_UNAVAIL
;
364 if (ent
->first
== TRUE
)
366 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
367 __internal_setnetgrent (group
, &ent
->netgrdata
);
376 saved_cursor
= ent
->netgrdata
.cursor
;
377 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
378 &ent
->netgrdata
, buffer
, buflen
);
381 __internal_endnetgrent (&ent
->netgrdata
);
383 give_pwd_free (&ent
->pwd
);
384 return NSS_STATUS_RETURN
;
387 if (user
== NULL
|| user
[0] == '-')
390 if (domain
!= NULL
&& strcmp (ypdomain
, domain
) != 0)
393 /* If name != NULL, we are called from getpwnam */
395 if (strcmp (user
, name
) != 0)
398 if (yp_match (ypdomain
, "passwd.byname", user
,
399 strlen (user
), &outval
, &outvallen
)
403 p2len
= pwd_need_buflen (&ent
->pwd
);
406 __set_errno (ERANGE
);
407 return NSS_STATUS_TRYAGAIN
;
409 p2
= buffer
+ (buflen
- p2len
);
411 p
= strncpy (buffer
, outval
, buflen
);
415 if ((parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
)) == -1)
417 ent
->netgrdata
.cursor
= saved_cursor
;
418 return NSS_STATUS_TRYAGAIN
;
423 /* Store the User in the blacklist for the "+" at the end of
425 blacklist_store_name (result
->pw_name
, ent
);
426 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
431 return NSS_STATUS_SUCCESS
;
434 static enum nss_status
435 getpwent_next_nisplus_netgr (const char *name
, struct passwd
*result
,
436 ent_t
*ent
, char *group
, char *buffer
,
439 char *ypdomain
, *host
, *user
, *domain
, *p2
;
440 int status
, parse_res
;
444 /* Maybe we should use domainname here ? We need the current
445 domainname for the domain field in netgroups */
446 if (yp_get_default_domain (&ypdomain
) != YPERR_SUCCESS
)
450 give_pwd_free (&ent
->pwd
);
451 return NSS_STATUS_UNAVAIL
;
454 if (ent
->first
== TRUE
)
456 bzero (&ent
->netgrdata
, sizeof (struct __netgrent
));
457 __internal_setnetgrent (group
, &ent
->netgrdata
);
465 saved_cursor
= ent
->netgrdata
.cursor
;
466 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
467 &ent
->netgrdata
, buffer
, buflen
);
470 __internal_endnetgrent (&ent
->netgrdata
);
472 give_pwd_free (&ent
->pwd
);
473 return NSS_STATUS_RETURN
;
476 if (user
== NULL
|| user
[0] == '-')
479 if (domain
!= NULL
&& strcmp (ypdomain
, domain
) != 0)
482 /* If name != NULL, we are called from getpwnam */
484 if (strcmp (user
, name
) != 0)
487 p2len
= pwd_need_buflen (&ent
->pwd
);
490 __set_errno (ERANGE
);
491 return NSS_STATUS_TRYAGAIN
;
493 p2
= buffer
+ (buflen
- p2len
);
496 char buf
[strlen (user
) + 30 + pwdtablelen
];
497 sprintf(buf
, "[name=%s],%s", user
, pwdtable
);
498 nisres
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
500 if (niserr2nss (nisres
->status
) != NSS_STATUS_SUCCESS
)
502 nis_freeresult (nisres
);
505 if ((parse_res
= _nss_nisplus_parse_pwent (nisres
, result
, buffer
,
508 nis_freeresult (nisres
);
509 ent
->netgrdata
.cursor
= saved_cursor
;
510 return NSS_STATUS_TRYAGAIN
;
512 nis_freeresult (nisres
);
516 /* Store the User in the blacklist for the "+" at the end of
518 blacklist_store_name (result
->pw_name
, ent
);
519 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
524 return NSS_STATUS_SUCCESS
;
527 static enum nss_status
528 getpwent_next_nisplus (struct passwd
*result
, ent_t
*ent
, char *buffer
,
535 p2len
= pwd_need_buflen (&ent
->pwd
);
538 __set_errno (ERANGE
);
539 return NSS_STATUS_TRYAGAIN
;
541 p2
= buffer
+ (buflen
- p2len
);
546 nis_result
*saved_res
;
551 saved_res
= ent
->result
;
553 ent
->result
= nis_first_entry(pwdtable
);
554 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
557 give_pwd_free (&ent
->pwd
);
558 return niserr2nss (ent
->result
->status
);
566 res
= nis_next_entry(pwdtable
, &ent
->result
->cookie
);
567 saved_res
= ent
->result
;
570 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
573 nis_freeresult (saved_res
);
574 give_pwd_free (&ent
->pwd
);
575 return niserr2nss (ent
->result
->status
);
578 if ((parse_res
= _nss_nisplus_parse_pwent (ent
->result
, result
, buffer
,
581 nis_freeresult (ent
->result
);
582 ent
->result
= saved_res
;
583 ent
->first
= saved_first
;
584 __set_errno (ERANGE
);
585 return NSS_STATUS_TRYAGAIN
;
590 nis_freeresult (saved_res
);
594 in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
595 parse_res
= 0; /* if result->pw_name in blacklist,search next entry */
599 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
601 return NSS_STATUS_SUCCESS
;
604 static enum nss_status
605 getpwent_next_nis (struct passwd
*result
, ent_t
*ent
, char *buffer
,
608 struct parser_data
*data
= (void *) buffer
;
609 char *domain
, *outkey
, *outval
, *p
, *p2
;
610 int outkeylen
, outvallen
, parse_res
;
613 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
616 give_pwd_free (&ent
->pwd
);
617 return NSS_STATUS_UNAVAIL
;
620 p2len
= pwd_need_buflen (&ent
->pwd
);
623 __set_errno (ERANGE
);
624 return NSS_STATUS_TRYAGAIN
;
626 p2
= buffer
+ (buflen
- p2len
);
636 if (yp_first (domain
, "passwd.byname", &outkey
, &outkeylen
,
637 &outval
, &outvallen
) != YPERR_SUCCESS
)
640 give_pwd_free (&ent
->pwd
);
641 return NSS_STATUS_UNAVAIL
;
645 saved_oldkey
= ent
->oldkey
;
646 saved_oldlen
= ent
->oldkeylen
;
647 ent
->oldkey
= outkey
;
648 ent
->oldkeylen
= outkeylen
;
653 if (yp_next (domain
, "passwd.byname", ent
->oldkey
, ent
->oldkeylen
,
654 &outkey
, &outkeylen
, &outval
, &outvallen
)
658 give_pwd_free (&ent
->pwd
);
659 return NSS_STATUS_NOTFOUND
;
663 saved_oldkey
= ent
->oldkey
;
664 saved_oldlen
= ent
->oldkeylen
;
665 ent
->oldkey
= outkey
;
666 ent
->oldkeylen
= outkeylen
;
669 /* Copy the found data to our buffer */
670 p
= strncpy (buffer
, outval
, buflen
);
672 /* ...and free the data. */
677 if ((parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
)) == -1)
680 ent
->oldkey
= saved_oldkey
;
681 ent
->oldkeylen
= saved_oldlen
;
682 ent
->first
= saved_first
;
683 __set_errno (ERANGE
);
684 return NSS_STATUS_TRYAGAIN
;
692 in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
697 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
699 return NSS_STATUS_SUCCESS
;
702 /* This function handle the +user entrys in /etc/passwd */
703 static enum nss_status
704 getpwnam_plususer (const char *name
, struct passwd
*result
, char *buffer
,
707 struct parser_data
*data
= (void *) buffer
;
713 memset (&pwd
, '\0', sizeof (struct passwd
));
715 copy_pwd_changes (&pwd
, result
, NULL
, 0);
717 plen
= pwd_need_buflen (&pwd
);
720 __set_errno (ERANGE
);
721 return NSS_STATUS_TRYAGAIN
;
723 p
= buffer
+ (buflen
- plen
);
726 if (use_nisplus
) /* Do the NIS+ query here */
729 char buf
[strlen (name
) + 24 + pwdtablelen
];
731 sprintf(buf
, "[name=%s],%s", name
, pwdtable
);
732 res
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
733 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
735 enum nss_status status
= niserr2nss (res
->status
);
737 nis_freeresult (res
);
740 if ((parse_res
= _nss_nisplus_parse_pwent (res
, result
, buffer
,
743 nis_freeresult (res
);
744 __set_errno (ERANGE
);
745 return NSS_STATUS_TRYAGAIN
;
747 nis_freeresult (res
);
751 char *domain
, *outval
, *ptr
;
754 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
755 return NSS_STATUS_TRYAGAIN
;
757 if (yp_match (domain
, "passwd.byname", name
, strlen (name
),
760 return NSS_STATUS_TRYAGAIN
;
761 ptr
= strncpy (buffer
, outval
, buflen
< (size_t) outvallen
?
762 buflen
: (size_t) outvallen
);
763 buffer
[buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
] = '\0';
765 while (isspace (*ptr
))
767 if ((parse_res
= _nss_files_parse_pwent (ptr
, result
, data
, buflen
))
770 __set_errno (ERANGE
);
771 return NSS_STATUS_TRYAGAIN
;
777 copy_pwd_changes (result
, &pwd
, p
, plen
);
778 give_pwd_free (&pwd
);
779 /* We found the entry. */
780 return NSS_STATUS_SUCCESS
;
784 /* Give buffer the old len back */
786 give_pwd_free (&pwd
);
788 return NSS_STATUS_RETURN
;
791 static enum nss_status
792 getpwent_next_file (struct passwd
*result
, ent_t
*ent
,
793 char *buffer
, size_t buflen
)
795 struct parser_data
*data
= (void *) buffer
;
804 fgetpos (ent
->stream
, &pos
);
805 p
= fgets (buffer
, buflen
, ent
->stream
);
807 return NSS_STATUS_NOTFOUND
;
809 /* Terminate the line for any case. */
810 buffer
[buflen
- 1] = '\0';
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_pwent (p
, result
, data
, buflen
)));
823 /* The parser ran out of space. */
824 fsetpos (ent
->stream
, &pos
);
825 __set_errno (ERANGE
);
826 return NSS_STATUS_TRYAGAIN
;
829 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
830 /* This is a real entry. */
834 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
835 && result
->pw_name
[2] != '\0')
838 char *user
, *host
, *domain
;
839 struct __netgrent netgrdata
;
841 bzero (&netgrdata
, sizeof (struct __netgrent
));
842 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
843 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
844 &netgrdata
, buf2
, sizeof (buf2
)))
846 if (user
!= NULL
&& user
[0] != '-')
847 blacklist_store_name (user
, ent
);
849 __internal_endnetgrent (&netgrdata
);
854 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
855 && result
->pw_name
[2] != '\0')
859 ent
->netgroup
= TRUE
;
861 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
864 status
= getpwent_next_nisplus_netgr (NULL
, result
, ent
,
868 status
= getpwent_next_nis_netgr (NULL
, result
, ent
,
871 if (status
== NSS_STATUS_RETURN
)
878 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
879 && result
->pw_name
[1] != '@')
881 blacklist_store_name (&result
->pw_name
[1], ent
);
886 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
887 && result
->pw_name
[1] != '@')
889 enum nss_status status
;
891 /* Store the User in the blacklist for the "+" at the end of
893 blacklist_store_name (&result
->pw_name
[1], ent
);
894 status
= getpwnam_plususer (&result
->pw_name
[1], result
, buffer
,
896 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
899 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
906 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
910 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
913 return getpwent_next_nisplus (result
, ent
, buffer
, buflen
);
915 return getpwent_next_nis (result
, ent
, buffer
, buflen
);
919 return NSS_STATUS_SUCCESS
;
923 static enum nss_status
924 internal_getpwent_r (struct passwd
*pw
, ent_t
*ent
, char *buffer
,
931 /* We are searching members in a netgroup */
932 /* Since this is not the first call, we don't need the group name */
934 status
= getpwent_next_nisplus_netgr (NULL
, pw
, ent
, NULL
, buffer
,
937 status
= getpwent_next_nis_netgr (NULL
, pw
, ent
, NULL
, buffer
, buflen
);
938 if (status
== NSS_STATUS_RETURN
)
939 return getpwent_next_file (pw
, ent
, buffer
, buflen
);
947 return getpwent_next_nisplus (pw
, ent
, buffer
, buflen
);
949 return getpwent_next_nis (pw
, ent
, buffer
, buflen
);
952 return getpwent_next_file (pw
, ent
, buffer
, buflen
);
956 _nss_compat_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
)
958 enum nss_status status
= NSS_STATUS_SUCCESS
;
960 __libc_lock_lock (lock
);
964 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
965 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
968 /* Be prepared that the setpwent function was not called before. */
969 if (ext_ent
.stream
== NULL
)
970 status
= internal_setpwent (&ext_ent
);
972 if (status
== NSS_STATUS_SUCCESS
)
973 status
= internal_getpwent_r (pwd
, &ext_ent
, buffer
, buflen
);
975 __libc_lock_unlock (lock
);
980 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
981 static enum nss_status
982 internal_getpwnam_r (const char *name
, struct passwd
*result
, ent_t
*ent
,
983 char *buffer
, size_t buflen
)
985 struct parser_data
*data
= (void *) buffer
;
995 fgetpos (ent
->stream
, &pos
);
996 p
= fgets (buffer
, buflen
, ent
->stream
);
999 if (feof (ent
->stream
))
1000 return NSS_STATUS_NOTFOUND
;
1003 __set_errno (ERANGE
);
1004 return NSS_STATUS_TRYAGAIN
;
1008 /* Terminate the line for any case. */
1009 buffer
[buflen
- 1] = '\0';
1011 /* Skip leading blanks. */
1012 while (isspace (*p
))
1015 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
1016 /* Parse the line. If it is invalid, loop to
1017 get the next line of the file to parse. */
1018 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
)));
1020 if (parse_res
== -1)
1022 /* The parser ran out of space. */
1023 fsetpos (ent
->stream
, &pos
);
1024 __set_errno (ERANGE
);
1025 return NSS_STATUS_TRYAGAIN
;
1028 /* This is a real entry. */
1029 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
1031 if (strcmp (result
->pw_name
, name
) == 0)
1032 return NSS_STATUS_SUCCESS
;
1038 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
1039 && result
->pw_name
[2] != '\0')
1042 char *user
, *host
, *domain
;
1043 struct __netgrent netgrdata
;
1045 bzero (&netgrdata
, sizeof (struct __netgrent
));
1046 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
1047 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
1048 &netgrdata
, buf2
, sizeof (buf2
)))
1050 if (user
!= NULL
&& user
[0] != '-')
1051 if (strcmp (user
, name
) == 0)
1052 return NSS_STATUS_NOTFOUND
;
1054 __internal_endnetgrent (&netgrdata
);
1059 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
1060 && result
->pw_name
[2] != '\0')
1062 char buf
[strlen (result
->pw_name
)];
1065 strcpy (buf
, &result
->pw_name
[2]);
1066 ent
->netgroup
= TRUE
;
1068 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
1073 status
= getpwent_next_nisplus_netgr (name
, result
, ent
, buf
,
1076 status
= getpwent_next_nis_netgr (name
, result
, ent
, buf
,
1078 if (status
== NSS_STATUS_RETURN
)
1081 if (status
== NSS_STATUS_SUCCESS
&&
1082 strcmp (result
->pw_name
, name
) == 0)
1083 return NSS_STATUS_SUCCESS
;
1084 } while (status
== NSS_STATUS_SUCCESS
);
1089 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
1090 && result
->pw_name
[1] != '@')
1092 if (strcmp (&result
->pw_name
[1], name
) == 0)
1093 return NSS_STATUS_NOTFOUND
;
1099 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
1100 && result
->pw_name
[1] != '@')
1102 if (strcmp (name
, &result
->pw_name
[1]) == 0)
1104 enum nss_status status
;
1106 status
= getpwnam_plususer (name
, result
, buffer
, buflen
);
1107 if (status
== NSS_STATUS_RETURN
)
1108 /* We couldn't parse the entry */
1109 return NSS_STATUS_NOTFOUND
;
1116 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
1118 enum nss_status status
;
1120 status
= getpwnam_plususer (name
, result
, buffer
, buflen
);
1121 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
1124 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1125 return NSS_STATUS_NOTFOUND
;
1130 return NSS_STATUS_SUCCESS
;
1134 _nss_compat_getpwnam_r (const char *name
, struct passwd
*pwd
,
1135 char *buffer
, size_t buflen
)
1137 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
1138 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
1139 enum nss_status status
;
1141 if (name
[0] == '-' || name
[0] == '+')
1142 return NSS_STATUS_NOTFOUND
;
1144 __libc_lock_lock (lock
);
1148 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
1149 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
1152 __libc_lock_unlock (lock
);
1154 status
= internal_setpwent (&ent
);
1155 if (status
!= NSS_STATUS_SUCCESS
)
1158 status
= internal_getpwnam_r (name
, pwd
, &ent
, buffer
, buflen
);
1160 internal_endpwent (&ent
);
1165 /* This function handle the + entry in /etc/passwd for getpwuid */
1166 static enum nss_status
1167 getpwuid_plususer (uid_t uid
, struct passwd
*result
, char *buffer
,
1170 struct parser_data
*data
= (void *) buffer
;
1176 memset (&pwd
, '\0', sizeof (struct passwd
));
1178 copy_pwd_changes (&pwd
, result
, NULL
, 0);
1180 plen
= pwd_need_buflen (&pwd
);
1183 __set_errno (ERANGE
);
1184 return NSS_STATUS_TRYAGAIN
;
1186 p
= buffer
+ (buflen
- plen
);
1189 if (use_nisplus
) /* Do the NIS+ query here */
1192 char buf
[1024 + pwdtablelen
];
1194 sprintf(buf
, "[uid=%d],%s", uid
, pwdtable
);
1195 res
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
1196 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
1198 enum nss_status status
= niserr2nss (res
->status
);
1200 nis_freeresult (res
);
1203 if ((parse_res
= _nss_nisplus_parse_pwent (res
, result
, buffer
,
1206 nis_freeresult (res
);
1207 __set_errno (ERANGE
);
1208 return NSS_STATUS_TRYAGAIN
;
1210 nis_freeresult (res
);
1215 char *domain
, *outval
, *ptr
;
1218 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
1219 return NSS_STATUS_TRYAGAIN
;
1221 sprintf (buf
, "%d", uid
);
1222 if (yp_match (domain
, "passwd.byuid", buf
, strlen (buf
),
1223 &outval
, &outvallen
)
1225 return NSS_STATUS_TRYAGAIN
;
1226 ptr
= strncpy (buffer
, outval
, buflen
< (size_t) outvallen
?
1227 buflen
: (size_t) outvallen
);
1228 buffer
[buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
] = '\0';
1230 while (isspace (*ptr
))
1232 if ((parse_res
= _nss_files_parse_pwent (ptr
, result
, data
, buflen
))
1235 __set_errno (ERANGE
);
1236 return NSS_STATUS_TRYAGAIN
;
1242 copy_pwd_changes (result
, &pwd
, p
, plen
);
1243 give_pwd_free (&pwd
);
1244 /* We found the entry. */
1245 return NSS_STATUS_SUCCESS
;
1249 /* Give buffer the old len back */
1251 give_pwd_free (&pwd
);
1253 return NSS_STATUS_RETURN
;
1256 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user id */
1257 static enum nss_status
1258 internal_getpwuid_r (uid_t uid
, struct passwd
*result
, ent_t
*ent
,
1259 char *buffer
, size_t buflen
)
1261 struct parser_data
*data
= (void *) buffer
;
1271 fgetpos (ent
->stream
, &pos
);
1272 p
= fgets (buffer
, buflen
, ent
->stream
);
1274 return NSS_STATUS_NOTFOUND
;
1276 /* Terminate the line for any case. */
1277 buffer
[buflen
- 1] = '\0';
1279 /* Skip leading blanks. */
1280 while (isspace (*p
))
1283 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
1284 /* Parse the line. If it is invalid, loop to
1285 get the next line of the file to parse. */
1286 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
)));
1288 if (parse_res
== -1)
1290 /* The parser ran out of space. */
1291 fsetpos (ent
->stream
, &pos
);
1292 __set_errno (ERANGE
);
1293 return NSS_STATUS_TRYAGAIN
;
1296 /* This is a real entry. */
1297 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
1299 if (result
->pw_uid
== uid
)
1300 return NSS_STATUS_SUCCESS
;
1306 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
1307 && result
->pw_name
[2] != '\0')
1310 char *user
, *host
, *domain
;
1311 struct __netgrent netgrdata
;
1313 bzero (&netgrdata
, sizeof (struct __netgrent
));
1314 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
1315 while (__internal_getnetgrent_r (&host
, &user
, &domain
,
1316 &netgrdata
, buf2
, sizeof (buf2
)))
1318 if (user
!= NULL
&& user
[0] != '-')
1319 blacklist_store_name (user
, ent
);
1321 __internal_endnetgrent (&netgrdata
);
1326 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
1327 && result
->pw_name
[2] != '\0')
1329 char buf
[strlen (result
->pw_name
)];
1332 strcpy (buf
, &result
->pw_name
[2]);
1333 ent
->netgroup
= TRUE
;
1335 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
1340 status
= getpwent_next_nisplus_netgr (NULL
, result
, ent
, buf
,
1343 status
= getpwent_next_nis_netgr (NULL
, result
, ent
, buf
,
1345 if (status
== NSS_STATUS_RETURN
)
1348 if (status
== NSS_STATUS_SUCCESS
&& uid
== result
->pw_uid
)
1349 return NSS_STATUS_SUCCESS
;
1350 } while (status
== NSS_STATUS_SUCCESS
);
1355 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
1356 && result
->pw_name
[1] != '@')
1358 blacklist_store_name (&result
->pw_name
[1], ent
);
1363 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
1364 && result
->pw_name
[1] != '@')
1366 enum nss_status status
;
1368 /* Store the User in the blacklist for the "+" at the end of
1370 blacklist_store_name (&result
->pw_name
[1], ent
);
1371 status
= getpwnam_plususer (&result
->pw_name
[1], result
, buffer
,
1373 if (status
== NSS_STATUS_SUCCESS
&& result
->pw_uid
== uid
)
1380 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
1382 enum nss_status status
;
1384 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
);
1385 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
1388 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1389 return NSS_STATUS_NOTFOUND
;
1394 return NSS_STATUS_SUCCESS
;
1398 _nss_compat_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
1399 char *buffer
, size_t buflen
)
1401 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
1402 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
1403 enum nss_status status
;
1405 __libc_lock_lock (lock
);
1409 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
1410 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
1413 __libc_lock_unlock (lock
);
1415 status
= internal_setpwent (&ent
);
1416 if (status
!= NSS_STATUS_SUCCESS
)
1419 status
= internal_getpwuid_r (uid
, pwd
, &ent
, buffer
, buflen
);
1421 internal_endpwent (&ent
);
1427 /* Support routines for remembering -@netgroup and -user entries.
1428 The names are stored in a single string with `|' as separator. */
1430 blacklist_store_name (const char *name
, ent_t
*ent
)
1432 int namelen
= strlen (name
);
1435 /* first call, setup cache */
1436 if (ent
->blacklist
.size
== 0)
1438 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
1439 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
1440 if (ent
->blacklist
.data
== NULL
)
1442 ent
->blacklist
.data
[0] = '|';
1443 ent
->blacklist
.data
[1] = '\0';
1444 ent
->blacklist
.current
= 1;
1448 if (in_blacklist (name
, namelen
, ent
))
1449 return; /* no duplicates */
1451 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
1453 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
1454 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
1457 free (ent
->blacklist
.data
);
1458 ent
->blacklist
.size
= 0;
1461 ent
->blacklist
.data
= tmp
;
1465 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
1468 ent
->blacklist
.current
+= namelen
+ 1;
1473 /* returns TRUE if ent->blacklist contains name, else FALSE */
1475 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
1477 char buf
[namelen
+ 3];
1480 if (ent
->blacklist
.data
== NULL
)
1484 cp
= stpcpy (&buf
[1], name
);
1487 return strstr (ent
->blacklist
.data
, buf
) != NULL
;
This page took 0.103354 seconds and 6 git commands to generate.