update from main arcive 961210
[glibc.git] / nis / nss_compat / compat-pwd.c
blob503aafb51eb2b179bc872f9b3b07c3f748a5afa7
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. */
20 #include <nss.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <netdb.h>
25 #include <string.h>
26 #include <libc-lock.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
30 #include "netgroup.h"
32 /* Structure for remembering -@netgroup and -user members ... */
33 #define BLACKLIST_INITIAL_SIZE 512
34 #define BLACKLIST_INCREMENT 256
35 struct blacklist_t
37 char *data;
38 int current;
39 int size;
42 struct ent_t
44 bool_t netgroup;
45 bool_t nis;
46 bool_t first;
47 char *oldkey;
48 int oldkeylen;
49 FILE *stream;
50 struct blacklist_t blacklist;
51 struct passwd pwd;
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 *);
66 static void
67 give_pwd_free (struct passwd *pwd)
69 if (pwd->pw_name != NULL)
70 free (pwd->pw_name);
71 if (pwd->pw_passwd != NULL)
72 free (pwd->pw_passwd);
73 if (pwd->pw_gecos != NULL)
74 free (pwd->pw_gecos);
75 if (pwd->pw_dir != NULL)
76 free (pwd->pw_dir);
77 if (pwd->pw_shell != NULL)
78 free (pwd->pw_shell);
80 memset (pwd, '\0', sizeof (struct passwd));
83 static size_t
84 pwd_need_buflen (struct passwd *pwd)
86 size_t len = 0;
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;
100 return len;
103 static void
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))
109 if (buffer == NULL)
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);
114 else
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))
125 if (buffer == NULL)
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);
130 else
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))
140 if (buffer == NULL)
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);
145 else
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))
156 if (buffer == NULL)
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);
161 else
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. */
179 if (ent->netgroup)
180 __internal_endnetgrent (&ent->netgrdata);
182 if (ent->oldkey != NULL)
184 free (ent->oldkey);
185 ent->oldkey = NULL;
186 ent->oldkeylen = 0;
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;
200 else
201 rewind (ent->stream);
203 give_pwd_free (&ent->pwd);
205 return status;
209 enum nss_status
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);
220 return result;
224 static enum nss_status
225 internal_endpwent (ent_t *ent)
227 if (ent->stream != NULL)
229 fclose (ent->stream);
230 ent->stream = NULL;
233 ent->nis = ent->first = ent->netgroup = 0;
235 if (ent->oldkey != NULL)
237 free (ent->oldkey);
238 ent->oldkey = NULL;
239 ent->oldkeylen = 0;
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;
251 enum nss_status
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);
265 return result;
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)
277 ent->netgroup = 0;
278 ent->first = 0;
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);
287 ent->first = FALSE;
290 while (1)
292 status = __internal_getnetgrent (&host, &user, &domain, &ent->netgrdata,
293 buffer, buflen);
294 if (status != 1)
296 __internal_endnetgrent (&ent->netgrdata);
297 ent->netgroup = 0;
298 give_pwd_free (&ent->pwd);
299 return NSS_STATUS_RETURN;
302 if (user == NULL || user[0] == '-')
303 continue;
305 if (domain != NULL && strcmp (ypdomain, domain) != 0)
306 continue;
308 if (yp_match (ypdomain, "passwd.byname", user,
309 strlen (user), &outval, &outvallen)
310 != YPERR_SUCCESS)
311 continue;
313 p2len = pwd_need_buflen (&ent->pwd);
314 if (p2len > buflen)
316 __set_errno (ERANGE);
317 return NSS_STATUS_TRYAGAIN;
319 p2 = buffer + (buflen - p2len);
320 buflen -= p2len;
321 p = strncpy (buffer, outval, buflen);
322 while (isspace (*p))
323 p++;
324 free (outval);
325 if (_nss_files_parse_pwent (p, result, buffer, buflen))
327 copy_pwd_changes (result, &ent->pwd, p2, p2len);
328 break;
332 return NSS_STATUS_SUCCESS;
335 static enum nss_status
336 getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
337 size_t buflen)
339 char *domain, *outkey, *outval, *p, *p2;
340 int outkeylen, outvallen, p2len;
342 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
344 ent->nis = 0;
345 give_pwd_free (&ent->pwd);
346 return NSS_STATUS_UNAVAIL;
349 p2len = pwd_need_buflen (&ent->pwd);
350 if (p2len > buflen)
352 __set_errno (ERANGE);
353 return NSS_STATUS_TRYAGAIN;
355 p2 = buffer + (buflen - p2len);
356 buflen -= p2len;
359 if (ent->first)
361 if (yp_first (domain, "passwd.byname", &outkey, &outkeylen,
362 &outval, &outvallen) != YPERR_SUCCESS)
364 ent->nis = 0;
365 give_pwd_free (&ent->pwd);
366 return NSS_STATUS_UNAVAIL;
369 ent->oldkey = outkey;
370 ent->oldkeylen = outkeylen;
371 ent->first = FALSE;
373 else
375 if (yp_next (domain, "passwd.byname", ent->oldkey, ent->oldkeylen,
376 &outkey, &outkeylen, &outval, &outvallen)
377 != YPERR_SUCCESS)
379 ent->nis = 0;
380 give_pwd_free (&ent->pwd);
381 return NSS_STATUS_NOTFOUND;
384 free (ent->oldkey);
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. */
393 free (outval);
395 while (isspace (*p))
396 ++p;
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;
404 else
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)
413 while (1)
415 char *p, *p2;
416 int p2len;
420 p = fgets (buffer, buflen, ent->stream);
421 if (p == NULL)
422 return NSS_STATUS_NOTFOUND;
424 /* Terminate the line for any case. */
425 buffer[buflen - 1] = '\0';
427 /* Skip leading blanks. */
428 while (isspace (*p))
429 ++p;
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. */
438 break;
440 /* -@netgroup */
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);
452 endnetgrent ();
453 continue;
456 /* +@netgroup */
457 if (result->pw_name[0] == '+' && result->pw_name[1] == '@'
458 && result->pw_name[2] != '\0')
460 int status;
462 ent->netgroup = TRUE;
463 ent->first = TRUE;
464 copy_pwd_changes (&ent->pwd, result, NULL, 0);
466 status = getpwent_next_netgr (result, ent, &result->pw_name[2],
467 buffer, buflen);
468 if (status == NSS_STATUS_RETURN)
469 continue;
470 else
471 return status;
474 /* -user */
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);
479 continue;
482 /* +user */
483 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
484 && result->pw_name[1] != '@')
486 char *domain;
487 char *outval;
488 int outvallen;
489 struct passwd pwd;
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 */
496 continue;
498 if (yp_match (domain, "passwd.byname", &result->pw_name[1],
499 strlen (result->pw_name) - 1, &outval, &outvallen)
500 != YPERR_SUCCESS)
501 continue;
503 copy_pwd_changes (&pwd, result, NULL, 0);
505 p2len = pwd_need_buflen (&pwd);
506 if (p2len > buflen)
508 __set_errno (ERANGE);
509 return NSS_STATUS_TRYAGAIN;
511 p2 = buffer + (buflen - p2len);
512 buflen -= p2len;
513 p = strncpy (buffer, outval, buflen);
514 while (isspace (*p))
515 p++;
516 free (outval);
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. */
522 break;
524 else
526 /* Give buffer the old len back */
527 buflen += p2len;
528 give_pwd_free (&pwd);
532 /* +:... */
533 if (result->pw_name[0] == '+' && result->pw_name[1] == '\0')
535 ent->nis = TRUE;
536 ent->first = TRUE;
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,
549 size_t buflen)
551 if (ent->netgroup)
553 int status;
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);
560 else
561 return status;
563 else if (ent->nis)
564 return getpwent_next_nis (pw, ent, buffer, buflen);
565 else
566 return getpwent_next_file (pw, ent, buffer, buflen);
569 enum nss_status
570 _nss_compat_getpwent_r (struct passwd *pwd, char *buffer,
571 size_t buflen)
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);
586 return status;
590 enum nss_status
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)
604 return status;
606 while ((status = internal_getpwent_r (pwd, &ent, buffer, buflen))
607 == NSS_STATUS_SUCCESS)
608 if (strcmp (pwd->pw_name, name) == 0)
609 break;
611 internal_endpwent (&ent);
612 return status;
616 enum nss_status
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)
626 return status;
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] != '-')
631 break;
633 internal_endpwent (&ent);
634 return status;
638 /* Support routines for remembering -@netgroup and -user entries.
639 The names are stored in a single string with `|' as separator. */
640 static void
641 blacklist_store_name (const char *name, ent_t *ent)
643 int namelen = strlen (name);
644 char *tmp;
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)
652 return;
653 ent->blacklist.data[0] = '|';
654 ent->blacklist.data[1] = '\0';
655 ent->blacklist.current = 1;
657 else
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);
666 if (tmp == NULL)
668 free (ent->blacklist.data);
669 ent->blacklist.size = 0;
670 return;
672 ent->blacklist.data = tmp;
676 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
677 *tmp++ = '|';
678 *tmp = '\0';
679 ent->blacklist.current += namelen + 1;
681 return;
684 /* returns TRUE if ent->blacklist contains name, else FALSE */
685 static bool_t
686 in_blacklist (const char *name, int namelen, ent_t *ent)
688 char buf[namelen + 3];
690 if (ent->blacklist.data == NULL)
691 return FALSE;
693 stpcpy (stpcpy (stpcpy (buf, "|"), name), "|");
694 return strstr (ent->blacklist.data, buf) != NULL;