]>
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>
34 #include "nss-nisplus.h"
35 #include "nisplus-parser.h"
37 static service_user
*ni
= NULL
;
38 static bool_t use_nisplus
= FALSE
; /* default: passwd_compat: nis */
39 static nis_name pwdtable
= NULL
; /* Name of the pwd table */
40 static size_t pwdtablelen
= 0;
42 /* Get the declaration of the parser function. */
44 #define STRUCTURE passwd
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, NULL
, NULL
, NULL
}};
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_pwd_free (struct passwd
*pwd
)
86 if (pwd
->pw_name
!= NULL
)
88 if (pwd
->pw_passwd
!= NULL
)
89 free (pwd
->pw_passwd
);
90 if (pwd
->pw_gecos
!= NULL
)
92 if (pwd
->pw_dir
!= NULL
)
94 if (pwd
->pw_shell
!= NULL
)
97 memset (pwd
, '\0', sizeof (struct passwd
));
101 pwd_need_buflen (struct passwd
*pwd
)
105 if (pwd
->pw_passwd
!= NULL
)
106 len
+= strlen (pwd
->pw_passwd
) + 1;
108 if (pwd
->pw_gecos
!= NULL
)
109 len
+= strlen (pwd
->pw_gecos
) + 1;
111 if (pwd
->pw_dir
!= NULL
)
112 len
+= strlen (pwd
->pw_dir
) + 1;
114 if (pwd
->pw_shell
!= NULL
)
115 len
+= strlen (pwd
->pw_shell
) + 1;
121 copy_pwd_changes (struct passwd
*dest
, struct passwd
*src
,
122 char *buffer
, size_t buflen
)
124 if (src
->pw_passwd
!= NULL
&& strlen (src
->pw_passwd
))
127 dest
->pw_passwd
= strdup (src
->pw_passwd
);
128 else if (dest
->pw_passwd
&&
129 strlen (dest
->pw_passwd
) >= strlen (src
->pw_passwd
))
130 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
133 dest
->pw_passwd
= buffer
;
134 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
135 buffer
+= strlen (dest
->pw_passwd
) + 1;
136 buflen
= buflen
- (strlen (dest
->pw_passwd
) + 1);
140 if (src
->pw_gecos
!= NULL
&& strlen (src
->pw_gecos
))
143 dest
->pw_gecos
= strdup (src
->pw_gecos
);
144 else if (dest
->pw_gecos
&&
145 strlen (dest
->pw_gecos
) >= strlen (src
->pw_gecos
))
146 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
149 dest
->pw_gecos
= buffer
;
150 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
151 buffer
+= strlen (dest
->pw_gecos
) + 1;
152 buflen
= buflen
- (strlen (dest
->pw_gecos
) + 1);
155 if (src
->pw_dir
!= NULL
&& strlen (src
->pw_dir
))
158 dest
->pw_dir
= strdup (src
->pw_dir
);
159 else if (dest
->pw_dir
&&
160 strlen (dest
->pw_dir
) >= strlen (src
->pw_dir
))
161 strcpy (dest
->pw_dir
, src
->pw_dir
);
164 dest
->pw_dir
= buffer
;
165 strcpy (dest
->pw_dir
, src
->pw_dir
);
166 buffer
+= strlen (dest
->pw_dir
) + 1;
167 buflen
= buflen
- (strlen (dest
->pw_dir
) + 1);
171 if (src
->pw_shell
!= NULL
&& strlen (src
->pw_shell
))
174 dest
->pw_shell
= strdup (src
->pw_shell
);
175 else if (dest
->pw_shell
&&
176 strlen (dest
->pw_shell
) >= strlen (src
->pw_shell
))
177 strcpy (dest
->pw_shell
, src
->pw_shell
);
180 dest
->pw_shell
= buffer
;
181 strcpy (dest
->pw_shell
, src
->pw_shell
);
182 buffer
+= strlen (dest
->pw_shell
) + 1;
183 buflen
= buflen
- (strlen (dest
->pw_shell
) + 1);
188 static enum nss_status
189 internal_setpwent (ent_t
*ent
)
191 enum nss_status status
= NSS_STATUS_SUCCESS
;
193 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
195 /* If something was left over free it. */
197 __internal_endnetgrent (&ent
->netgrdata
);
199 if (ent
->oldkey
!= NULL
)
206 if (ent
->result
!= NULL
)
208 nis_freeresult (ent
->result
);
212 if (pwdtable
== NULL
)
214 static const char key
[] = "passwd.org_dir.";
215 const char *local_dir
= nis_local_directory ();
216 size_t len_local_dir
= strlen (local_dir
);
218 pwdtable
= malloc (sizeof (key
) + len_local_dir
);
219 if (pwdtable
== NULL
)
220 return NSS_STATUS_TRYAGAIN
;
222 pwdtablelen
= ((char *) mempcpy (mempcpy (pwdtable
,
223 key
, sizeof (key
) - 1),
224 local_dir
, len_local_dir
+ 1)
228 ent
->blacklist
.current
= 0;
229 if (ent
->blacklist
.data
!= NULL
)
230 ent
->blacklist
.data
[0] = '\0';
232 if (ent
->stream
== NULL
)
234 ent
->stream
= fopen ("/etc/passwd", "r");
236 if (ent
->stream
== NULL
)
237 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
240 /* We have to make sure the file is `closed on exec'. */
243 result
= flags
= fcntl (fileno (ent
->stream
), F_GETFD
, 0);
247 result
= fcntl (fileno (ent
->stream
), F_SETFD
, flags
);
251 /* Something went wrong. Close the stream and return a
253 fclose (ent
->stream
);
255 status
= NSS_STATUS_UNAVAIL
;
260 rewind (ent
->stream
);
262 give_pwd_free (&ent
->pwd
);
269 _nss_compat_setpwent (void)
271 enum nss_status result
;
273 __libc_lock_lock (lock
);
277 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
278 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
281 result
= internal_setpwent (&ext_ent
);
283 __libc_lock_unlock (lock
);
289 static enum nss_status
290 internal_endpwent (ent_t
*ent
)
292 if (ent
->stream
!= NULL
)
294 fclose (ent
->stream
);
299 __internal_endnetgrent (&ent
->netgrdata
);
301 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
303 if (ent
->oldkey
!= NULL
)
310 if (ent
->result
!= NULL
)
312 nis_freeresult (ent
->result
);
316 ent
->blacklist
.current
= 0;
317 if (ent
->blacklist
.data
!= NULL
)
318 ent
->blacklist
.data
[0] = '\0';
320 give_pwd_free (&ent
->pwd
);
322 return NSS_STATUS_SUCCESS
;
326 _nss_compat_endpwent (void)
328 enum nss_status result
;
330 __libc_lock_lock (lock
);
332 result
= internal_endpwent (&ext_ent
);
334 __libc_lock_unlock (lock
);
339 static enum nss_status
340 getpwent_next_nis_netgr (const char *name
, struct passwd
*result
, ent_t
*ent
,
341 char *group
, char *buffer
, size_t buflen
, int *errnop
)
343 struct parser_data
*data
= (void *) buffer
;
344 char *ypdomain
, *host
, *user
, *domain
, *outval
, *p
, *p2
;
345 int status
, outvallen
;
348 if (yp_get_default_domain (&ypdomain
) != YPERR_SUCCESS
)
352 give_pwd_free (&ent
->pwd
);
353 return NSS_STATUS_UNAVAIL
;
356 if (ent
->first
== TRUE
)
358 memset (&ent
->netgrdata
, 0, sizeof (struct __netgrent
));
359 __internal_setnetgrent (group
, &ent
->netgrdata
);
368 saved_cursor
= ent
->netgrdata
.cursor
;
369 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
370 &ent
->netgrdata
, buffer
, buflen
,
374 __internal_endnetgrent (&ent
->netgrdata
);
376 give_pwd_free (&ent
->pwd
);
377 return NSS_STATUS_RETURN
;
380 if (user
== NULL
|| user
[0] == '-')
383 if (domain
!= NULL
&& strcmp (ypdomain
, domain
) != 0)
386 /* If name != NULL, we are called from getpwnam */
388 if (strcmp (user
, name
) != 0)
391 if (yp_match (ypdomain
, "passwd.byname", user
,
392 strlen (user
), &outval
, &outvallen
)
396 p2len
= pwd_need_buflen (&ent
->pwd
);
400 return NSS_STATUS_TRYAGAIN
;
402 p2
= buffer
+ (buflen
- p2len
);
404 p
= strncpy (buffer
, outval
, buflen
);
408 parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
, errnop
);
411 ent
->netgrdata
.cursor
= saved_cursor
;
412 return NSS_STATUS_TRYAGAIN
;
417 /* Store the User in the blacklist for the "+" at the end of
419 blacklist_store_name (result
->pw_name
, ent
);
420 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
425 return NSS_STATUS_SUCCESS
;
428 static enum nss_status
429 getpwent_next_nisplus_netgr (const char *name
, struct passwd
*result
,
430 ent_t
*ent
, char *group
, char *buffer
,
431 size_t buflen
, int *errnop
)
433 char *ypdomain
, *host
, *user
, *domain
, *p2
;
434 int status
, parse_res
;
438 /* Maybe we should use domainname here ? We need the current
439 domainname for the domain field in netgroups */
440 if (yp_get_default_domain (&ypdomain
) != YPERR_SUCCESS
)
444 give_pwd_free (&ent
->pwd
);
445 return NSS_STATUS_UNAVAIL
;
448 if (ent
->first
== TRUE
)
450 bzero (&ent
->netgrdata
, sizeof (struct __netgrent
));
451 __internal_setnetgrent (group
, &ent
->netgrdata
);
459 saved_cursor
= ent
->netgrdata
.cursor
;
460 status
= __internal_getnetgrent_r (&host
, &user
, &domain
,
461 &ent
->netgrdata
, buffer
, buflen
,
465 __internal_endnetgrent (&ent
->netgrdata
);
467 give_pwd_free (&ent
->pwd
);
468 return NSS_STATUS_RETURN
;
471 if (user
== NULL
|| user
[0] == '-')
474 if (domain
!= NULL
&& strcmp (ypdomain
, domain
) != 0)
477 /* If name != NULL, we are called from getpwnam */
479 if (strcmp (user
, name
) != 0)
482 p2len
= pwd_need_buflen (&ent
->pwd
);
486 return NSS_STATUS_TRYAGAIN
;
488 p2
= buffer
+ (buflen
- p2len
);
491 char buf
[strlen (user
) + 30 + pwdtablelen
];
492 sprintf(buf
, "[name=%s],%s", user
, pwdtable
);
493 nisres
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
495 if (niserr2nss (nisres
->status
) != NSS_STATUS_SUCCESS
)
497 nis_freeresult (nisres
);
500 parse_res
= _nss_nisplus_parse_pwent (nisres
, result
, buffer
,
504 nis_freeresult (nisres
);
505 ent
->netgrdata
.cursor
= saved_cursor
;
507 return NSS_STATUS_TRYAGAIN
;
509 nis_freeresult (nisres
);
513 /* Store the User in the blacklist for the "+" at the end of
515 blacklist_store_name (result
->pw_name
, ent
);
516 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
521 return NSS_STATUS_SUCCESS
;
524 static enum nss_status
525 getpwent_next_nisplus (struct passwd
*result
, ent_t
*ent
, char *buffer
,
526 size_t buflen
, int *errnop
)
532 p2len
= pwd_need_buflen (&ent
->pwd
);
536 return NSS_STATUS_TRYAGAIN
;
538 p2
= buffer
+ (buflen
- p2len
);
543 nis_result
*saved_res
;
548 saved_res
= ent
->result
;
550 ent
->result
= nis_first_entry (pwdtable
);
551 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
554 give_pwd_free (&ent
->pwd
);
555 return niserr2nss (ent
->result
->status
);
563 res
= nis_next_entry (pwdtable
, &ent
->result
->cookie
);
564 saved_res
= ent
->result
;
567 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
570 nis_freeresult (saved_res
);
571 give_pwd_free (&ent
->pwd
);
572 return niserr2nss (ent
->result
->status
);
575 parse_res
= _nss_nisplus_parse_pwent (ent
->result
, result
, buffer
,
579 nis_freeresult (ent
->result
);
580 ent
->result
= saved_res
;
581 ent
->first
= saved_first
;
583 return NSS_STATUS_TRYAGAIN
;
588 nis_freeresult (saved_res
);
592 in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
593 parse_res
= 0; /* if result->pw_name in blacklist,search next entry */
597 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
599 return NSS_STATUS_SUCCESS
;
602 static enum nss_status
603 getpwent_next_nis (struct passwd
*result
, ent_t
*ent
, char *buffer
,
604 size_t buflen
, int *errnop
)
606 struct parser_data
*data
= (void *) buffer
;
607 char *domain
, *outkey
, *outval
, *p
, *p2
;
608 int outkeylen
, outvallen
, parse_res
;
611 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
614 give_pwd_free (&ent
->pwd
);
615 return NSS_STATUS_UNAVAIL
;
618 p2len
= pwd_need_buflen (&ent
->pwd
);
622 return NSS_STATUS_TRYAGAIN
;
624 p2
= buffer
+ (buflen
- p2len
);
634 if (yp_first (domain
, "passwd.byname", &outkey
, &outkeylen
,
635 &outval
, &outvallen
) != YPERR_SUCCESS
)
638 give_pwd_free (&ent
->pwd
);
639 return NSS_STATUS_UNAVAIL
;
643 saved_oldkey
= ent
->oldkey
;
644 saved_oldlen
= ent
->oldkeylen
;
645 ent
->oldkey
= outkey
;
646 ent
->oldkeylen
= outkeylen
;
651 if (yp_next (domain
, "passwd.byname", ent
->oldkey
, ent
->oldkeylen
,
652 &outkey
, &outkeylen
, &outval
, &outvallen
)
656 give_pwd_free (&ent
->pwd
);
657 return NSS_STATUS_NOTFOUND
;
661 saved_oldkey
= ent
->oldkey
;
662 saved_oldlen
= ent
->oldkeylen
;
663 ent
->oldkey
= outkey
;
664 ent
->oldkeylen
= outkeylen
;
667 /* Copy the found data to our buffer */
668 p
= strncpy (buffer
, outval
, buflen
);
670 /* ...and free the data. */
675 parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
, errnop
);
679 ent
->oldkey
= saved_oldkey
;
680 ent
->oldkeylen
= saved_oldlen
;
681 ent
->first
= saved_first
;
683 return NSS_STATUS_TRYAGAIN
;
691 && in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
696 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
698 return NSS_STATUS_SUCCESS
;
701 /* This function handle the +user entrys in /etc/passwd */
702 static enum nss_status
703 getpwnam_plususer (const char *name
, struct passwd
*result
, char *buffer
,
704 size_t buflen
, int *errnop
)
706 struct parser_data
*data
= (void *) buffer
;
712 memset (&pwd
, '\0', sizeof (struct passwd
));
714 copy_pwd_changes (&pwd
, result
, NULL
, 0);
716 plen
= pwd_need_buflen (&pwd
);
720 return NSS_STATUS_TRYAGAIN
;
722 p
= buffer
+ (buflen
- plen
);
725 if (use_nisplus
) /* Do the NIS+ query here */
728 char buf
[strlen (name
) + 24 + pwdtablelen
];
730 sprintf(buf
, "[name=%s],%s", name
, pwdtable
);
731 res
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
732 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
734 enum nss_status status
= niserr2nss (res
->status
);
736 nis_freeresult (res
);
739 parse_res
= _nss_nisplus_parse_pwent (res
, result
, buffer
,
743 nis_freeresult (res
);
745 return NSS_STATUS_TRYAGAIN
;
747 nis_freeresult (res
);
751 char *domain
, *outval
, *ptr
;
754 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
757 return NSS_STATUS_TRYAGAIN
;
760 if (yp_match (domain
, "passwd.byname", name
, strlen (name
),
765 return NSS_STATUS_TRYAGAIN
;
767 ptr
= strncpy (buffer
, outval
, buflen
< (size_t) outvallen
?
768 buflen
: (size_t) outvallen
);
769 buffer
[buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
] = '\0';
771 while (isspace (*ptr
))
773 parse_res
= _nss_files_parse_pwent (ptr
, result
, data
, buflen
, errnop
);
775 return NSS_STATUS_TRYAGAIN
;
780 copy_pwd_changes (result
, &pwd
, p
, plen
);
781 give_pwd_free (&pwd
);
782 /* We found the entry. */
783 return NSS_STATUS_SUCCESS
;
787 /* Give buffer the old len back */
789 give_pwd_free (&pwd
);
791 return NSS_STATUS_RETURN
;
794 static enum nss_status
795 getpwent_next_file (struct passwd
*result
, ent_t
*ent
,
796 char *buffer
, size_t buflen
, int *errnop
)
798 struct parser_data
*data
= (void *) buffer
;
807 fgetpos (ent
->stream
, &pos
);
808 p
= fgets (buffer
, buflen
, ent
->stream
);
811 if (feof (ent
->stream
))
812 return NSS_STATUS_NOTFOUND
;
815 fsetpos (ent
->stream
, &pos
);
817 return NSS_STATUS_TRYAGAIN
;
821 /* Terminate the line for any case. */
822 buffer
[buflen
- 1] = '\0';
824 /* Skip leading blanks. */
828 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
829 /* Parse the line. If it is invalid, loop to
830 get the next line of the file to parse. */
831 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
836 /* The parser ran out of space. */
837 fsetpos (ent
->stream
, &pos
);
839 return NSS_STATUS_TRYAGAIN
;
842 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
843 /* This is a real entry. */
847 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
848 && result
->pw_name
[2] != '\0')
850 /* XXX Do not use fixed length buffer. */
852 char *user
, *host
, *domain
;
853 struct __netgrent netgrdata
;
855 bzero (&netgrdata
, sizeof (struct __netgrent
));
856 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
857 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
858 buf2
, sizeof (buf2
), errnop
))
860 if (user
!= NULL
&& user
[0] != '-')
861 blacklist_store_name (user
, ent
);
863 __internal_endnetgrent (&netgrdata
);
868 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
869 && result
->pw_name
[2] != '\0')
873 ent
->netgroup
= TRUE
;
875 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
878 status
= getpwent_next_nisplus_netgr (NULL
, result
, ent
,
880 buffer
, buflen
, errnop
);
882 status
= getpwent_next_nis_netgr (NULL
, result
, ent
,
884 buffer
, buflen
, errnop
);
885 if (status
== NSS_STATUS_RETURN
)
892 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
893 && result
->pw_name
[1] != '@')
895 blacklist_store_name (&result
->pw_name
[1], ent
);
900 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
901 && result
->pw_name
[1] != '@')
903 enum nss_status status
;
905 /* Store the User in the blacklist for the "+" at the end of
907 blacklist_store_name (&result
->pw_name
[1], ent
);
908 status
= getpwnam_plususer (&result
->pw_name
[1], result
, buffer
,
910 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
913 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
917 if (status
== NSS_STATUS_TRYAGAIN
)
919 /* The parser ran out of space */
920 fsetpos (ent
->stream
, &pos
);
928 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
932 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
935 return getpwent_next_nisplus (result
, ent
, buffer
, buflen
, errnop
);
937 return getpwent_next_nis (result
, ent
, buffer
, buflen
, errnop
);
941 return NSS_STATUS_SUCCESS
;
945 static enum nss_status
946 internal_getpwent_r (struct passwd
*pw
, ent_t
*ent
, char *buffer
,
947 size_t buflen
, int *errnop
)
953 /* We are searching members in a netgroup */
954 /* Since this is not the first call, we don't need the group name */
956 status
= getpwent_next_nisplus_netgr (NULL
, pw
, ent
, NULL
, buffer
,
959 status
= getpwent_next_nis_netgr (NULL
, pw
, ent
, NULL
, buffer
, buflen
,
961 if (status
== NSS_STATUS_RETURN
)
962 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
970 return getpwent_next_nisplus (pw
, ent
, buffer
, buflen
, errnop
);
972 return getpwent_next_nis (pw
, ent
, buffer
, buflen
, errnop
);
975 return getpwent_next_file (pw
, ent
, buffer
, buflen
, errnop
);
979 _nss_compat_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
982 enum nss_status status
= NSS_STATUS_SUCCESS
;
984 __libc_lock_lock (lock
);
988 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
989 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
992 /* Be prepared that the setpwent function was not called before. */
993 if (ext_ent
.stream
== NULL
)
994 status
= internal_setpwent (&ext_ent
);
996 if (status
== NSS_STATUS_SUCCESS
)
997 status
= internal_getpwent_r (pwd
, &ext_ent
, buffer
, buflen
, errnop
);
999 __libc_lock_unlock (lock
);
1004 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
1005 static enum nss_status
1006 internal_getpwnam_r (const char *name
, struct passwd
*result
, ent_t
*ent
,
1007 char *buffer
, size_t buflen
, int *errnop
)
1009 struct parser_data
*data
= (void *) buffer
;
1019 fgetpos (ent
->stream
, &pos
);
1020 p
= fgets (buffer
, buflen
, ent
->stream
);
1023 if (feof (ent
->stream
))
1024 return NSS_STATUS_NOTFOUND
;
1027 fsetpos (ent
->stream
, &pos
);
1029 return NSS_STATUS_TRYAGAIN
;
1033 /* Terminate the line for any case. */
1034 buffer
[buflen
- 1] = '\0';
1036 /* Skip leading blanks. */
1037 while (isspace (*p
))
1040 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
1041 /* Parse the line. If it is invalid, loop to
1042 get the next line of the file to parse. */
1043 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
1046 if (parse_res
== -1)
1048 /* The parser ran out of space. */
1049 fsetpos (ent
->stream
, &pos
);
1051 return NSS_STATUS_TRYAGAIN
;
1054 /* This is a real entry. */
1055 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
1057 if (strcmp (result
->pw_name
, name
) == 0)
1058 return NSS_STATUS_SUCCESS
;
1064 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
1065 && result
->pw_name
[2] != '\0')
1067 /* XXX Do not use fixed length buffers. */
1069 char *user
, *host
, *domain
;
1070 struct __netgrent netgrdata
;
1072 bzero (&netgrdata
, sizeof (struct __netgrent
));
1073 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
1074 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
1075 buf2
, sizeof (buf2
), errnop
))
1077 if (user
!= NULL
&& user
[0] != '-')
1078 if (strcmp (user
, name
) == 0)
1079 return NSS_STATUS_NOTFOUND
;
1081 __internal_endnetgrent (&netgrdata
);
1086 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
1087 && result
->pw_name
[2] != '\0')
1089 char buf
[strlen (result
->pw_name
)];
1092 strcpy (buf
, &result
->pw_name
[2]);
1093 ent
->netgroup
= TRUE
;
1095 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
1100 status
= getpwent_next_nisplus_netgr (name
, result
, ent
, buf
,
1101 buffer
, buflen
, errnop
);
1103 status
= getpwent_next_nis_netgr (name
, result
, ent
, buf
,
1104 buffer
, buflen
, errnop
);
1105 if (status
== NSS_STATUS_RETURN
)
1108 if (status
== NSS_STATUS_SUCCESS
&&
1109 strcmp (result
->pw_name
, name
) == 0)
1110 return NSS_STATUS_SUCCESS
;
1111 } while (status
== NSS_STATUS_SUCCESS
);
1116 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
1117 && result
->pw_name
[1] != '@')
1119 if (strcmp (&result
->pw_name
[1], name
) == 0)
1120 return NSS_STATUS_NOTFOUND
;
1126 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
1127 && result
->pw_name
[1] != '@')
1129 if (strcmp (name
, &result
->pw_name
[1]) == 0)
1131 enum nss_status status
;
1133 status
= getpwnam_plususer (name
, result
, buffer
, buflen
,
1135 if (status
== NSS_STATUS_RETURN
)
1136 /* We couldn't parse the entry */
1137 return NSS_STATUS_NOTFOUND
;
1144 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
1146 enum nss_status status
;
1148 status
= getpwnam_plususer (name
, result
, buffer
, buflen
, errnop
);
1149 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
1152 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1153 return NSS_STATUS_NOTFOUND
;
1158 return NSS_STATUS_SUCCESS
;
1162 _nss_compat_getpwnam_r (const char *name
, struct passwd
*pwd
,
1163 char *buffer
, size_t buflen
, int *errnop
)
1165 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
1166 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
1167 enum nss_status status
;
1169 if (name
[0] == '-' || name
[0] == '+')
1170 return NSS_STATUS_NOTFOUND
;
1172 __libc_lock_lock (lock
);
1176 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
1177 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
1180 __libc_lock_unlock (lock
);
1182 status
= internal_setpwent (&ent
);
1183 if (status
!= NSS_STATUS_SUCCESS
)
1186 status
= internal_getpwnam_r (name
, pwd
, &ent
, buffer
, buflen
, errnop
);
1188 internal_endpwent (&ent
);
1193 /* This function handle the + entry in /etc/passwd for getpwuid */
1194 static enum nss_status
1195 getpwuid_plususer (uid_t uid
, struct passwd
*result
, char *buffer
,
1196 size_t buflen
, int *errnop
)
1198 struct parser_data
*data
= (void *) buffer
;
1204 memset (&pwd
, '\0', sizeof (struct passwd
));
1206 copy_pwd_changes (&pwd
, result
, NULL
, 0);
1208 plen
= pwd_need_buflen (&pwd
);
1212 return NSS_STATUS_TRYAGAIN
;
1214 p
= buffer
+ (buflen
- plen
);
1217 if (use_nisplus
) /* Do the NIS+ query here */
1220 char buf
[1024 + pwdtablelen
];
1222 snprintf(buf
, sizeof (buf
), "[uid=%d],%s", uid
, pwdtable
);
1223 res
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
1224 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
1226 enum nss_status status
= niserr2nss (res
->status
);
1228 nis_freeresult (res
);
1231 if ((parse_res
= _nss_nisplus_parse_pwent (res
, result
, buffer
,
1232 buflen
, errnop
)) == -1)
1234 nis_freeresult (res
);
1236 return NSS_STATUS_TRYAGAIN
;
1238 nis_freeresult (res
);
1243 char *domain
, *outval
, *ptr
;
1246 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
1249 return NSS_STATUS_TRYAGAIN
;
1252 sprintf (buf
, "%d", uid
);
1253 if (yp_match (domain
, "passwd.byuid", buf
, strlen (buf
),
1254 &outval
, &outvallen
)
1258 return NSS_STATUS_TRYAGAIN
;
1260 ptr
= strncpy (buffer
, outval
, buflen
< (size_t) outvallen
?
1261 buflen
: (size_t) outvallen
);
1262 buffer
[buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
] = '\0';
1264 while (isspace (*ptr
))
1266 parse_res
= _nss_files_parse_pwent (ptr
, result
, data
, buflen
, errnop
);
1267 if (parse_res
== -1)
1268 return NSS_STATUS_TRYAGAIN
;
1273 copy_pwd_changes (result
, &pwd
, p
, plen
);
1274 give_pwd_free (&pwd
);
1275 /* We found the entry. */
1276 return NSS_STATUS_SUCCESS
;
1280 /* Give buffer the old len back */
1282 give_pwd_free (&pwd
);
1284 return NSS_STATUS_RETURN
;
1287 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user id */
1288 static enum nss_status
1289 internal_getpwuid_r (uid_t uid
, struct passwd
*result
, ent_t
*ent
,
1290 char *buffer
, size_t buflen
, int *errnop
)
1292 struct parser_data
*data
= (void *) buffer
;
1302 fgetpos (ent
->stream
, &pos
);
1303 p
= fgets (buffer
, buflen
, ent
->stream
);
1306 if (feof (ent
->stream
))
1307 return NSS_STATUS_NOTFOUND
;
1310 fsetpos (ent
->stream
, &pos
);
1312 return NSS_STATUS_TRYAGAIN
;
1316 /* Terminate the line for any case. */
1317 buffer
[buflen
- 1] = '\0';
1319 /* Skip leading blanks. */
1320 while (isspace (*p
))
1323 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
1324 /* Parse the line. If it is invalid, loop to
1325 get the next line of the file to parse. */
1326 !(parse_res
= _nss_files_parse_pwent (p
, result
, data
, buflen
,
1329 if (parse_res
== -1)
1331 /* The parser ran out of space. */
1332 fsetpos (ent
->stream
, &pos
);
1334 return NSS_STATUS_TRYAGAIN
;
1337 /* This is a real entry. */
1338 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
1340 if (result
->pw_uid
== uid
)
1341 return NSS_STATUS_SUCCESS
;
1347 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
1348 && result
->pw_name
[2] != '\0')
1350 /* XXX Do not use fixed length buffers. */
1352 char *user
, *host
, *domain
;
1353 struct __netgrent netgrdata
;
1355 bzero (&netgrdata
, sizeof (struct __netgrent
));
1356 __internal_setnetgrent (&result
->pw_name
[2], &netgrdata
);
1357 while (__internal_getnetgrent_r (&host
, &user
, &domain
, &netgrdata
,
1358 buf2
, sizeof (buf2
), errnop
))
1360 if (user
!= NULL
&& user
[0] != '-')
1361 blacklist_store_name (user
, ent
);
1363 __internal_endnetgrent (&netgrdata
);
1368 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
1369 && result
->pw_name
[2] != '\0')
1371 char buf
[strlen (result
->pw_name
)];
1374 strcpy (buf
, &result
->pw_name
[2]);
1375 ent
->netgroup
= TRUE
;
1377 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
1382 status
= getpwent_next_nisplus_netgr (NULL
, result
, ent
, buf
,
1383 buffer
, buflen
, errnop
);
1385 status
= getpwent_next_nis_netgr (NULL
, result
, ent
, buf
,
1386 buffer
, buflen
, errnop
);
1387 if (status
== NSS_STATUS_RETURN
)
1390 if (status
== NSS_STATUS_SUCCESS
&& uid
== result
->pw_uid
)
1391 return NSS_STATUS_SUCCESS
;
1392 } while (status
== NSS_STATUS_SUCCESS
);
1397 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
1398 && result
->pw_name
[1] != '@')
1400 blacklist_store_name (&result
->pw_name
[1], ent
);
1405 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
1406 && result
->pw_name
[1] != '@')
1408 enum nss_status status
;
1410 /* Store the User in the blacklist for the "+" at the end of
1412 blacklist_store_name (&result
->pw_name
[1], ent
);
1413 status
= getpwnam_plususer (&result
->pw_name
[1], result
, buffer
,
1415 if (status
== NSS_STATUS_SUCCESS
&& result
->pw_uid
== uid
)
1422 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
1424 enum nss_status status
;
1426 status
= getpwuid_plususer (uid
, result
, buffer
, buflen
, errnop
);
1427 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
1430 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
1431 return NSS_STATUS_NOTFOUND
;
1436 return NSS_STATUS_SUCCESS
;
1440 _nss_compat_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
1441 char *buffer
, size_t buflen
, int *errnop
)
1443 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0},
1444 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
1445 enum nss_status status
;
1447 __libc_lock_lock (lock
);
1451 __nss_database_lookup ("passwd_compat", NULL
, "nis", &ni
);
1452 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
1455 __libc_lock_unlock (lock
);
1457 status
= internal_setpwent (&ent
);
1458 if (status
!= NSS_STATUS_SUCCESS
)
1461 status
= internal_getpwuid_r (uid
, pwd
, &ent
, buffer
, buflen
, errnop
);
1463 internal_endpwent (&ent
);
1469 /* Support routines for remembering -@netgroup and -user entries.
1470 The names are stored in a single string with `|' as separator. */
1472 blacklist_store_name (const char *name
, ent_t
*ent
)
1474 int namelen
= strlen (name
);
1477 /* first call, setup cache */
1478 if (ent
->blacklist
.size
== 0)
1480 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
1481 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
1482 if (ent
->blacklist
.data
== NULL
)
1484 ent
->blacklist
.data
[0] = '|';
1485 ent
->blacklist
.data
[1] = '\0';
1486 ent
->blacklist
.current
= 1;
1490 if (in_blacklist (name
, namelen
, ent
))
1491 return; /* no duplicates */
1493 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
1495 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
1496 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
1499 free (ent
->blacklist
.data
);
1500 ent
->blacklist
.size
= 0;
1503 ent
->blacklist
.data
= tmp
;
1507 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
1510 ent
->blacklist
.current
+= namelen
+ 1;
1515 /* returns TRUE if ent->blacklist contains name, else FALSE */
1517 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
1519 char buf
[namelen
+ 3];
1522 if (ent
->blacklist
.data
== NULL
)
1526 cp
= stpcpy (&buf
[1], name
);
1529 return strstr (ent
->blacklist
.data
, buf
) != NULL
;
This page took 0.149593 seconds and 6 git commands to generate.