1 /* Copyright (C) 1996 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. */
26 #include <libc-lock.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
32 /* Structure for remembering -@netgroup and -user members ... */
33 #define BLACKLIST_INITIAL_SIZE 512
34 #define BLACKLIST_INCREMENT 256
50 struct blacklist_t blacklist
;
52 struct __netgrent netgrdata
;
54 typedef struct ent_t ent_t
;
56 static ent_t ext_ent
= {0, 0, 0, NULL
, 0, NULL
, {NULL
, 0, 0},
57 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
59 /* Protect global state against multiple changers. */
60 __libc_lock_define_initialized (static, lock
)
62 /* Prototypes for local functions. */
63 static void blacklist_store_name (const char *, ent_t
*);
64 static int in_blacklist (const char *, int, ent_t
*);
67 give_pwd_free (struct passwd
*pwd
)
69 if (pwd
->pw_name
!= NULL
)
71 if (pwd
->pw_passwd
!= NULL
)
72 free (pwd
->pw_passwd
);
73 if (pwd
->pw_gecos
!= NULL
)
75 if (pwd
->pw_dir
!= NULL
)
77 if (pwd
->pw_shell
!= NULL
)
80 memset (pwd
, '\0', sizeof (struct passwd
));
84 pwd_need_buflen (struct passwd
*pwd
)
88 if (pwd
->pw_passwd
!= NULL
)
89 len
+= strlen (pwd
->pw_passwd
) + 1;
91 if (pwd
->pw_gecos
!= NULL
)
92 len
+= strlen (pwd
->pw_gecos
) + 1;
94 if (pwd
->pw_dir
!= NULL
)
95 len
+= strlen (pwd
->pw_dir
) + 1;
97 if (pwd
->pw_shell
!= NULL
)
98 len
+= strlen (pwd
->pw_shell
) + 1;
104 copy_pwd_changes (struct passwd
*dest
, struct passwd
*src
,
105 char *buffer
, size_t buflen
)
107 if (src
->pw_passwd
!= NULL
&& strlen (src
->pw_passwd
))
110 dest
->pw_passwd
= strdup (src
->pw_passwd
);
111 else if (dest
->pw_passwd
&&
112 strlen (dest
->pw_passwd
) >= strlen (src
->pw_passwd
))
113 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
116 dest
->pw_passwd
= buffer
;
117 strcpy (dest
->pw_passwd
, src
->pw_passwd
);
118 buffer
+= strlen (dest
->pw_passwd
) + 1;
119 buflen
= buflen
- (strlen (dest
->pw_passwd
) + 1);
123 if (src
->pw_gecos
!= NULL
&& strlen (src
->pw_gecos
))
126 dest
->pw_gecos
= strdup (src
->pw_gecos
);
127 else if (dest
->pw_gecos
&&
128 strlen (dest
->pw_gecos
) >= strlen (src
->pw_gecos
))
129 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
132 dest
->pw_gecos
= buffer
;
133 strcpy (dest
->pw_gecos
, src
->pw_gecos
);
134 buffer
+= strlen (dest
->pw_gecos
) + 1;
135 buflen
= buflen
- (strlen (dest
->pw_gecos
) + 1);
138 if (src
->pw_dir
!= NULL
&& strlen (src
->pw_dir
))
141 dest
->pw_dir
= strdup (src
->pw_dir
);
142 else if (dest
->pw_dir
&&
143 strlen (dest
->pw_dir
) >= strlen (src
->pw_dir
))
144 strcpy (dest
->pw_dir
, src
->pw_dir
);
147 dest
->pw_dir
= buffer
;
148 strcpy (dest
->pw_dir
, src
->pw_dir
);
149 buffer
+= strlen (dest
->pw_dir
) + 1;
150 buflen
= buflen
- (strlen (dest
->pw_dir
) + 1);
154 if (src
->pw_shell
!= NULL
&& strlen (src
->pw_shell
))
157 dest
->pw_shell
= strdup (src
->pw_shell
);
158 else if (dest
->pw_shell
&&
159 strlen (dest
->pw_shell
) >= strlen (src
->pw_shell
))
160 strcpy (dest
->pw_shell
, src
->pw_shell
);
163 dest
->pw_shell
= buffer
;
164 strcpy (dest
->pw_shell
, src
->pw_shell
);
165 buffer
+= strlen (dest
->pw_shell
) + 1;
166 buflen
= buflen
- (strlen (dest
->pw_shell
) + 1);
171 static enum nss_status
172 internal_setpwent (ent_t
*ent
)
174 enum nss_status status
= NSS_STATUS_SUCCESS
;
176 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
178 /* If something was left over free it. */
180 __internal_endnetgrent (&ent
->netgrdata
);
182 if (ent
->oldkey
!= NULL
)
189 ent
->blacklist
.current
= 0;
190 if (ent
->blacklist
.data
!= NULL
)
191 ent
->blacklist
.data
[0] = '\0';
193 if (ent
->stream
== NULL
)
195 ent
->stream
= fopen ("/etc/passwd", "r");
197 if (ent
->stream
== NULL
)
198 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
201 rewind (ent
->stream
);
203 give_pwd_free (&ent
->pwd
);
210 _nss_compat_setpwent (void)
212 enum nss_status result
;
214 __libc_lock_lock (lock
);
216 result
= internal_setpwent (&ext_ent
);
218 __libc_lock_unlock (lock
);
224 static enum nss_status
225 internal_endpwent (ent_t
*ent
)
227 if (ent
->stream
!= NULL
)
229 fclose (ent
->stream
);
233 ent
->nis
= ent
->first
= ent
->netgroup
= 0;
235 if (ent
->oldkey
!= NULL
)
242 ent
->blacklist
.current
= 0;
243 if (ent
->blacklist
.data
!= NULL
)
244 ent
->blacklist
.data
[0] = '\0';
246 give_pwd_free (&ent
->pwd
);
248 return NSS_STATUS_SUCCESS
;
252 _nss_compat_endpwent (void)
254 enum nss_status result
;
256 __libc_lock_lock (lock
);
258 if (ext_ent
.netgroup
)
259 __internal_endnetgrent (&ext_ent
.netgrdata
);
261 result
= internal_endpwent (&ext_ent
);
263 __libc_lock_unlock (lock
);
268 static enum nss_status
269 getpwent_next_netgr (struct passwd
*result
, ent_t
*ent
, char *group
,
270 char *buffer
, size_t buflen
)
272 char *ypdomain
, *host
, *user
, *domain
, *outval
, *p
, *p2
;
273 int status
, outvallen
, p2len
;
275 if (yp_get_default_domain (&ypdomain
) != YPERR_SUCCESS
)
279 give_pwd_free (&ent
->pwd
);
280 return NSS_STATUS_UNAVAIL
;
283 if (ent
->first
== TRUE
)
285 bzero (&ent
->netgrdata
, sizeof (struct __netgrent
));
286 __internal_setnetgrent (group
, &ent
->netgrdata
);
292 status
= __internal_getnetgrent (&host
, &user
, &domain
, &ent
->netgrdata
,
296 __internal_endnetgrent (&ent
->netgrdata
);
298 give_pwd_free (&ent
->pwd
);
299 return NSS_STATUS_RETURN
;
302 if (user
== NULL
|| user
[0] == '-')
305 if (domain
!= NULL
&& strcmp (ypdomain
, domain
) != 0)
308 if (yp_match (ypdomain
, "passwd.byname", user
,
309 strlen (user
), &outval
, &outvallen
)
313 p2len
= pwd_need_buflen (&ent
->pwd
);
316 __set_errno (ERANGE
);
317 return NSS_STATUS_TRYAGAIN
;
319 p2
= buffer
+ (buflen
- p2len
);
321 p
= strncpy (buffer
, outval
, buflen
);
325 if (_nss_files_parse_pwent (p
, result
, buffer
, buflen
))
327 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
332 return NSS_STATUS_SUCCESS
;
335 static enum nss_status
336 getpwent_next_nis (struct passwd
*result
, ent_t
*ent
, char *buffer
,
339 char *domain
, *outkey
, *outval
, *p
, *p2
;
340 int outkeylen
, outvallen
, p2len
;
342 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
345 give_pwd_free (&ent
->pwd
);
346 return NSS_STATUS_UNAVAIL
;
349 p2len
= pwd_need_buflen (&ent
->pwd
);
352 __set_errno (ERANGE
);
353 return NSS_STATUS_TRYAGAIN
;
355 p2
= buffer
+ (buflen
- p2len
);
361 if (yp_first (domain
, "passwd.byname", &outkey
, &outkeylen
,
362 &outval
, &outvallen
) != YPERR_SUCCESS
)
365 give_pwd_free (&ent
->pwd
);
366 return NSS_STATUS_UNAVAIL
;
369 ent
->oldkey
= outkey
;
370 ent
->oldkeylen
= outkeylen
;
375 if (yp_next (domain
, "passwd.byname", ent
->oldkey
, ent
->oldkeylen
,
376 &outkey
, &outkeylen
, &outval
, &outvallen
)
380 give_pwd_free (&ent
->pwd
);
381 return NSS_STATUS_NOTFOUND
;
385 ent
->oldkey
= outkey
;
386 ent
->oldkeylen
= outkeylen
;
389 /* Copy the found data to our buffer */
390 p
= strncpy (buffer
, outval
, buflen
);
392 /* ...and free the data. */
398 while (!_nss_files_parse_pwent (p
, result
, buffer
, buflen
));
400 copy_pwd_changes (result
, &ent
->pwd
, p2
, p2len
);
402 if (!in_blacklist (result
->pw_name
, strlen (result
->pw_name
), ent
))
403 return NSS_STATUS_SUCCESS
;
405 return NSS_STATUS_NOTFOUND
;
409 static enum nss_status
410 getpwent_next_file (struct passwd
*result
, ent_t
*ent
,
411 char *buffer
, size_t buflen
)
420 p
= fgets (buffer
, buflen
, ent
->stream
);
422 return NSS_STATUS_NOTFOUND
;
424 /* Terminate the line for any case. */
425 buffer
[buflen
- 1] = '\0';
427 /* Skip leading blanks. */
431 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
432 /* Parse the line. If it is invalid, loop to
433 get the next line of the file to parse. */
434 !_nss_files_parse_pwent (p
, result
, buffer
, buflen
));
436 if (result
->pw_name
[0] != '+' && result
->pw_name
[0] != '-')
437 /* This is a real entry. */
441 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] == '@'
442 && result
->pw_name
[2] != '\0')
444 char *user
, *host
, *domain
;
446 setnetgrent (&result
->pw_name
[2]);
447 while (getnetgrent (&host
, &user
, &domain
))
449 if (user
!= NULL
&& user
[0] != '-')
450 blacklist_store_name (user
, ent
);
457 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '@'
458 && result
->pw_name
[2] != '\0')
462 ent
->netgroup
= TRUE
;
464 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
466 status
= getpwent_next_netgr (result
, ent
, &result
->pw_name
[2],
468 if (status
== NSS_STATUS_RETURN
)
475 if (result
->pw_name
[0] == '-' && result
->pw_name
[1] != '\0'
476 && result
->pw_name
[1] != '@')
478 blacklist_store_name (&result
->pw_name
[1], ent
);
483 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] != '\0'
484 && result
->pw_name
[1] != '@')
491 memset (&pwd
, '\0', sizeof (struct passwd
));
493 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
494 /* XXX Should we regard this as an fatal error? I don't
495 think so. Just continue working. --drepper@gnu */
498 if (yp_match (domain
, "passwd.byname", &result
->pw_name
[1],
499 strlen (result
->pw_name
) - 1, &outval
, &outvallen
)
503 copy_pwd_changes (&pwd
, result
, NULL
, 0);
505 p2len
= pwd_need_buflen (&pwd
);
508 __set_errno (ERANGE
);
509 return NSS_STATUS_TRYAGAIN
;
511 p2
= buffer
+ (buflen
- p2len
);
513 p
= strncpy (buffer
, outval
, buflen
);
517 if (_nss_files_parse_pwent (p
, result
, buffer
, buflen
))
519 copy_pwd_changes (result
, &pwd
, p2
, p2len
);
520 give_pwd_free (&pwd
);
521 /* We found the entry. */
526 /* Give buffer the old len back */
528 give_pwd_free (&pwd
);
533 if (result
->pw_name
[0] == '+' && result
->pw_name
[1] == '\0')
537 copy_pwd_changes (&ent
->pwd
, result
, NULL
, 0);
539 return getpwent_next_nis (result
, ent
, buffer
, buflen
);
543 return NSS_STATUS_SUCCESS
;
547 static enum nss_status
548 internal_getpwent_r (struct passwd
*pw
, ent_t
*ent
, char *buffer
,
555 /* We are searching members in a netgroup */
556 /* Since this is not the first call, we don't need the group name */
557 status
= getpwent_next_netgr (pw
, ent
, NULL
, buffer
, buflen
);
558 if (status
== NSS_STATUS_RETURN
)
559 return getpwent_next_file (pw
, ent
, buffer
, buflen
);
564 return getpwent_next_nis (pw
, ent
, buffer
, buflen
);
566 return getpwent_next_file (pw
, ent
, buffer
, buflen
);
570 _nss_compat_getpwent_r (struct passwd
*pwd
, char *buffer
,
573 enum nss_status status
= NSS_STATUS_SUCCESS
;
575 __libc_lock_lock (lock
);
577 /* Be prepared that the setpwent function was not called before. */
578 if (ext_ent
.stream
== NULL
)
579 status
= internal_setpwent (&ext_ent
);
581 if (status
== NSS_STATUS_SUCCESS
)
582 status
= internal_getpwent_r (pwd
, &ext_ent
, buffer
, buflen
);
584 __libc_lock_unlock (lock
);
591 _nss_compat_getpwnam_r (const char *name
, struct passwd
*pwd
,
592 char *buffer
, size_t buflen
)
594 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, {NULL
, 0, 0},
595 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
596 enum nss_status status
;
598 if (name
[0] == '-' || name
[0] == '+')
599 return NSS_STATUS_NOTFOUND
;
602 status
= internal_setpwent (&ent
);
603 if (status
!= NSS_STATUS_SUCCESS
)
606 while ((status
= internal_getpwent_r (pwd
, &ent
, buffer
, buflen
))
607 == NSS_STATUS_SUCCESS
)
608 if (strcmp (pwd
->pw_name
, name
) == 0)
611 internal_endpwent (&ent
);
617 _nss_compat_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
618 char *buffer
, size_t buflen
)
620 ent_t ent
= {0, 0, 0, NULL
, 0, NULL
, {NULL
, 0, 0},
621 {NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
}};
622 enum nss_status status
;
624 status
= internal_setpwent (&ent
);
625 if (status
!= NSS_STATUS_SUCCESS
)
628 while ((status
= internal_getpwent_r (pwd
, &ent
, buffer
, buflen
))
629 == NSS_STATUS_SUCCESS
)
630 if (pwd
->pw_uid
== uid
&& pwd
->pw_name
[0] != '+' && pwd
->pw_name
[0] != '-')
633 internal_endpwent (&ent
);
638 /* Support routines for remembering -@netgroup and -user entries.
639 The names are stored in a single string with `|' as separator. */
641 blacklist_store_name (const char *name
, ent_t
*ent
)
643 int namelen
= strlen (name
);
646 /* first call, setup cache */
647 if (ent
->blacklist
.size
== 0)
649 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
650 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
651 if (ent
->blacklist
.data
== NULL
)
653 ent
->blacklist
.data
[0] = '|';
654 ent
->blacklist
.data
[1] = '\0';
655 ent
->blacklist
.current
= 1;
659 if (in_blacklist (name
, namelen
, ent
))
660 return; /* no duplicates */
662 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
664 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
665 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
668 free (ent
->blacklist
.data
);
669 ent
->blacklist
.size
= 0;
672 ent
->blacklist
.data
= tmp
;
676 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
679 ent
->blacklist
.current
+= namelen
+ 1;
684 /* returns TRUE if ent->blacklist contains name, else FALSE */
686 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
688 char buf
[namelen
+ 3];
690 if (ent
->blacklist
.data
== NULL
)
693 stpcpy (stpcpy (stpcpy (buf
, "|"), name
), "|");
694 return strstr (ent
->blacklist
.data
, buf
) != NULL
;