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 /* Get the declaration of the parser function. */
34 #define STRUCTURE passwd
36 #include "../../nss/nss_files/files-parse.c"
38 /* Structure for remembering -@netgroup and -user members ... */
39 #define BLACKLIST_INITIAL_SIZE 512
40 #define BLACKLIST_INCREMENT 256
56 struct blacklist_t blacklist;
58 struct __netgrent netgrdata;
60 typedef struct ent_t ent_t;
62 static ent_t ext_ent = {0, 0, 0, NULL, 0, NULL, {NULL, 0, 0},
63 {NULL, NULL, 0, 0, NULL, NULL, NULL}};
65 /* Protect global state against multiple changers. */
66 __libc_lock_define_initialized (static, lock)
68 /* Prototypes for local functions. */
69 static void blacklist_store_name (const char *, ent_t *);
70 static int in_blacklist (const char *, int, ent_t *);
73 give_pwd_free (struct passwd *pwd)
75 if (pwd->pw_name != NULL)
77 if (pwd->pw_passwd != NULL)
78 free (pwd->pw_passwd);
79 if (pwd->pw_gecos != NULL)
81 if (pwd->pw_dir != NULL)
83 if (pwd->pw_shell != NULL)
86 memset (pwd, '\0', sizeof (struct passwd));
90 pwd_need_buflen (struct passwd *pwd)
94 if (pwd->pw_passwd != NULL)
95 len += strlen (pwd->pw_passwd) + 1;
97 if (pwd->pw_gecos != NULL)
98 len += strlen (pwd->pw_gecos) + 1;
100 if (pwd->pw_dir != NULL)
101 len += strlen (pwd->pw_dir) + 1;
103 if (pwd->pw_shell != NULL)
104 len += strlen (pwd->pw_shell) + 1;
110 copy_pwd_changes (struct passwd *dest, struct passwd *src,
111 char *buffer, size_t buflen)
113 if (src->pw_passwd != NULL && strlen (src->pw_passwd))
116 dest->pw_passwd = strdup (src->pw_passwd);
117 else if (dest->pw_passwd &&
118 strlen (dest->pw_passwd) >= strlen (src->pw_passwd))
119 strcpy (dest->pw_passwd, src->pw_passwd);
122 dest->pw_passwd = buffer;
123 strcpy (dest->pw_passwd, src->pw_passwd);
124 buffer += strlen (dest->pw_passwd) + 1;
125 buflen = buflen - (strlen (dest->pw_passwd) + 1);
129 if (src->pw_gecos != NULL && strlen (src->pw_gecos))
132 dest->pw_gecos = strdup (src->pw_gecos);
133 else if (dest->pw_gecos &&
134 strlen (dest->pw_gecos) >= strlen (src->pw_gecos))
135 strcpy (dest->pw_gecos, src->pw_gecos);
138 dest->pw_gecos = buffer;
139 strcpy (dest->pw_gecos, src->pw_gecos);
140 buffer += strlen (dest->pw_gecos) + 1;
141 buflen = buflen - (strlen (dest->pw_gecos) + 1);
144 if (src->pw_dir != NULL && strlen (src->pw_dir))
147 dest->pw_dir = strdup (src->pw_dir);
148 else if (dest->pw_dir &&
149 strlen (dest->pw_dir) >= strlen (src->pw_dir))
150 strcpy (dest->pw_dir, src->pw_dir);
153 dest->pw_dir = buffer;
154 strcpy (dest->pw_dir, src->pw_dir);
155 buffer += strlen (dest->pw_dir) + 1;
156 buflen = buflen - (strlen (dest->pw_dir) + 1);
160 if (src->pw_shell != NULL && strlen (src->pw_shell))
163 dest->pw_shell = strdup (src->pw_shell);
164 else if (dest->pw_shell &&
165 strlen (dest->pw_shell) >= strlen (src->pw_shell))
166 strcpy (dest->pw_shell, src->pw_shell);
169 dest->pw_shell = buffer;
170 strcpy (dest->pw_shell, src->pw_shell);
171 buffer += strlen (dest->pw_shell) + 1;
172 buflen = buflen - (strlen (dest->pw_shell) + 1);
177 static enum nss_status
178 internal_setpwent (ent_t *ent)
180 enum nss_status status = NSS_STATUS_SUCCESS;
182 ent->nis = ent->first = ent->netgroup = 0;
184 /* If something was left over free it. */
186 __internal_endnetgrent (&ent->netgrdata);
188 if (ent->oldkey != NULL)
195 ent->blacklist.current = 0;
196 if (ent->blacklist.data != NULL)
197 ent->blacklist.data[0] = '\0';
199 if (ent->stream == NULL)
201 ent->stream = fopen ("/etc/passwd", "r");
203 if (ent->stream == NULL)
204 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
207 rewind (ent->stream);
209 give_pwd_free (&ent->pwd);
216 _nss_compat_setpwent (void)
218 enum nss_status result;
220 __libc_lock_lock (lock);
222 result = internal_setpwent (&ext_ent);
224 __libc_lock_unlock (lock);
230 static enum nss_status
231 internal_endpwent (ent_t *ent)
233 if (ent->stream != NULL)
235 fclose (ent->stream);
239 ent->nis = ent->first = ent->netgroup = 0;
241 if (ent->oldkey != NULL)
248 ent->blacklist.current = 0;
249 if (ent->blacklist.data != NULL)
250 ent->blacklist.data[0] = '\0';
252 give_pwd_free (&ent->pwd);
254 return NSS_STATUS_SUCCESS;
258 _nss_compat_endpwent (void)
260 enum nss_status result;
262 __libc_lock_lock (lock);
264 if (ext_ent.netgroup)
265 __internal_endnetgrent (&ext_ent.netgrdata);
267 result = internal_endpwent (&ext_ent);
269 __libc_lock_unlock (lock);
274 static enum nss_status
275 getpwent_next_netgr (struct passwd *result, ent_t *ent, char *group,
276 char *buffer, size_t buflen)
278 struct parser_data *data = (void *) buffer;
279 char *ypdomain, *host, *user, *domain, *outval, *p, *p2;
280 int status, outvallen;
283 if (yp_get_default_domain (&ypdomain) != YPERR_SUCCESS)
287 give_pwd_free (&ent->pwd);
288 return NSS_STATUS_UNAVAIL;
291 if (ent->first == TRUE)
293 bzero (&ent->netgrdata, sizeof (struct __netgrent));
294 __internal_setnetgrent (group, &ent->netgrdata);
300 status = __internal_getnetgrent_r (&host, &user, &domain,
301 &ent->netgrdata, buffer, buflen);
304 __internal_endnetgrent (&ent->netgrdata);
306 give_pwd_free (&ent->pwd);
307 return NSS_STATUS_RETURN;
310 if (user == NULL || user[0] == '-')
313 if (domain != NULL && strcmp (ypdomain, domain) != 0)
316 if (yp_match (ypdomain, "passwd.byname", user,
317 strlen (user), &outval, &outvallen)
321 p2len = pwd_need_buflen (&ent->pwd);
324 __set_errno (ERANGE);
325 return NSS_STATUS_TRYAGAIN;
327 p2 = buffer + (buflen - p2len);
329 p = strncpy (buffer, outval, buflen);
333 if (_nss_files_parse_pwent (p, result, data, buflen))
335 copy_pwd_changes (result, &ent->pwd, p2, p2len);
340 return NSS_STATUS_SUCCESS;
343 static enum nss_status
344 getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
347 struct parser_data *data = (void *) buffer;
348 char *domain, *outkey, *outval, *p, *p2;
349 int outkeylen, outvallen;
352 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
355 give_pwd_free (&ent->pwd);
356 return NSS_STATUS_UNAVAIL;
359 p2len = pwd_need_buflen (&ent->pwd);
362 __set_errno (ERANGE);
363 return NSS_STATUS_TRYAGAIN;
365 p2 = buffer + (buflen - p2len);
371 if (yp_first (domain, "passwd.byname", &outkey, &outkeylen,
372 &outval, &outvallen) != YPERR_SUCCESS)
375 give_pwd_free (&ent->pwd);
376 return NSS_STATUS_UNAVAIL;
379 ent->oldkey = outkey;
380 ent->oldkeylen = outkeylen;
385 if (yp_next (domain, "passwd.byname", ent->oldkey, ent->oldkeylen,
386 &outkey, &outkeylen, &outval, &outvallen)
390 give_pwd_free (&ent->pwd);
391 return NSS_STATUS_NOTFOUND;
395 ent->oldkey = outkey;
396 ent->oldkeylen = outkeylen;
399 /* Copy the found data to our buffer */
400 p = strncpy (buffer, outval, buflen);
402 /* ...and free the data. */
408 while (!_nss_files_parse_pwent (p, result, data, buflen));
410 copy_pwd_changes (result, &ent->pwd, p2, p2len);
412 if (!in_blacklist (result->pw_name, strlen (result->pw_name), ent))
413 return NSS_STATUS_SUCCESS;
415 return NSS_STATUS_NOTFOUND;
419 static enum nss_status
420 getpwent_next_file (struct passwd *result, ent_t *ent,
421 char *buffer, size_t buflen)
423 struct parser_data *data = (void *) buffer;
431 p = fgets (buffer, buflen, ent->stream);
433 return NSS_STATUS_NOTFOUND;
435 /* Terminate the line for any case. */
436 buffer[buflen - 1] = '\0';
438 /* Skip leading blanks. */
442 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
443 /* Parse the line. If it is invalid, loop to
444 get the next line of the file to parse. */
445 !_nss_files_parse_pwent (p, result, data, buflen));
447 if (result->pw_name[0] != '+' && result->pw_name[0] != '-')
448 /* This is a real entry. */
452 if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
453 && result->pw_name[2] != '\0')
455 char *user, *host, *domain;
457 setnetgrent (&result->pw_name[2]);
458 while (getnetgrent (&host, &user, &domain))
460 if (user != NULL && user[0] != '-')
461 blacklist_store_name (user, ent);
468 if (result->pw_name[0] == '+' && result->pw_name[1] == '@'
469 && result->pw_name[2] != '\0')
473 ent->netgroup = TRUE;
475 copy_pwd_changes (&ent->pwd, result, NULL, 0);
477 status = getpwent_next_netgr (result, ent, &result->pw_name[2],
479 if (status == NSS_STATUS_RETURN)
486 if (result->pw_name[0] == '-' && result->pw_name[1] != '\0'
487 && result->pw_name[1] != '@')
489 blacklist_store_name (&result->pw_name[1], ent);
494 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
495 && result->pw_name[1] != '@')
502 memset (&pwd, '\0', sizeof (struct passwd));
504 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
505 /* XXX Should we regard this as an fatal error? I don't
506 think so. Just continue working. --drepper@gnu */
509 if (yp_match (domain, "passwd.byname", &result->pw_name[1],
510 strlen (result->pw_name) - 1, &outval, &outvallen)
514 copy_pwd_changes (&pwd, result, NULL, 0);
516 p2len = pwd_need_buflen (&pwd);
519 __set_errno (ERANGE);
520 return NSS_STATUS_TRYAGAIN;
522 p2 = buffer + (buflen - p2len);
524 p = strncpy (buffer, outval, buflen);
528 if (_nss_files_parse_pwent (p, result, data, buflen))
530 copy_pwd_changes (result, &pwd, p2, p2len);
531 give_pwd_free (&pwd);
532 /* We found the entry. */
537 /* Give buffer the old len back */
539 give_pwd_free (&pwd);
544 if (result->pw_name[0] == '+' && result->pw_name[1] == '\0')
548 copy_pwd_changes (&ent->pwd, result, NULL, 0);
550 return getpwent_next_nis (result, ent, buffer, buflen);
554 return NSS_STATUS_SUCCESS;
558 static enum nss_status
559 internal_getpwent_r (struct passwd *pw, ent_t *ent, char *buffer,
566 /* We are searching members in a netgroup */
567 /* Since this is not the first call, we don't need the group name */
568 status = getpwent_next_netgr (pw, ent, NULL, buffer, buflen);
569 if (status == NSS_STATUS_RETURN)
570 return getpwent_next_file (pw, ent, buffer, buflen);
575 return getpwent_next_nis (pw, ent, buffer, buflen);
577 return getpwent_next_file (pw, ent, buffer, buflen);
581 _nss_compat_getpwent_r (struct passwd *pwd, char *buffer,
584 enum nss_status status = NSS_STATUS_SUCCESS;
586 __libc_lock_lock (lock);
588 /* Be prepared that the setpwent function was not called before. */
589 if (ext_ent.stream == NULL)
590 status = internal_setpwent (&ext_ent);
592 if (status == NSS_STATUS_SUCCESS)
593 status = internal_getpwent_r (pwd, &ext_ent, buffer, buflen);
595 __libc_lock_unlock (lock);
602 _nss_compat_getpwnam_r (const char *name, struct passwd *pwd,
603 char *buffer, size_t buflen)
605 ent_t ent = {0, 0, 0, NULL, 0, NULL, {NULL, 0, 0},
606 {NULL, NULL, 0, 0, NULL, NULL, NULL}};
607 enum nss_status status;
609 if (name[0] == '-' || name[0] == '+')
610 return NSS_STATUS_NOTFOUND;
613 status = internal_setpwent (&ent);
614 if (status != NSS_STATUS_SUCCESS)
617 while ((status = internal_getpwent_r (pwd, &ent, buffer, buflen))
618 == NSS_STATUS_SUCCESS)
619 if (strcmp (pwd->pw_name, name) == 0)
622 internal_endpwent (&ent);
628 _nss_compat_getpwuid_r (uid_t uid, struct passwd *pwd,
629 char *buffer, size_t buflen)
631 ent_t ent = {0, 0, 0, NULL, 0, NULL, {NULL, 0, 0},
632 {NULL, NULL, 0, 0, NULL, NULL, NULL}};
633 enum nss_status status;
635 status = internal_setpwent (&ent);
636 if (status != NSS_STATUS_SUCCESS)
639 while ((status = internal_getpwent_r (pwd, &ent, buffer, buflen))
640 == NSS_STATUS_SUCCESS)
641 if (pwd->pw_uid == uid && pwd->pw_name[0] != '+' && pwd->pw_name[0] != '-')
644 internal_endpwent (&ent);
649 /* Support routines for remembering -@netgroup and -user entries.
650 The names are stored in a single string with `|' as separator. */
652 blacklist_store_name (const char *name, ent_t *ent)
654 int namelen = strlen (name);
657 /* first call, setup cache */
658 if (ent->blacklist.size == 0)
660 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
661 ent->blacklist.data = malloc (ent->blacklist.size);
662 if (ent->blacklist.data == NULL)
664 ent->blacklist.data[0] = '|';
665 ent->blacklist.data[1] = '\0';
666 ent->blacklist.current = 1;
670 if (in_blacklist (name, namelen, ent))
671 return; /* no duplicates */
673 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
675 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
676 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
679 free (ent->blacklist.data);
680 ent->blacklist.size = 0;
683 ent->blacklist.data = tmp;
687 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
690 ent->blacklist.current += namelen + 1;
695 /* returns TRUE if ent->blacklist contains name, else FALSE */
697 in_blacklist (const char *name, int namelen, ent_t *ent)
699 char buf[namelen + 3];
701 if (ent->blacklist.data == NULL)
704 stpcpy (stpcpy (stpcpy (buf, "|"), name), "|");
705 return strstr (ent->blacklist.data, buf) != NULL;