]> sourceware.org Git - glibc.git/blob - nis/nss_compat/compat-pwd.c
update from main arcive 961210
[glibc.git] / nis / nss_compat / compat-pwd.c
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.
4
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.
9
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.
14
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. */
19
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>
29
30 #include "netgroup.h"
31
32 /* Structure for remembering -@netgroup and -user members ... */
33 #define BLACKLIST_INITIAL_SIZE 512
34 #define BLACKLIST_INCREMENT 256
35 struct blacklist_t
36 {
37 char *data;
38 int current;
39 int size;
40 };
41
42 struct ent_t
43 {
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;
53 };
54 typedef struct ent_t ent_t;
55
56 static ent_t ext_ent = {0, 0, 0, NULL, 0, NULL, {NULL, 0, 0},
57 {NULL, NULL, 0, 0, NULL, NULL, NULL}};
58
59 /* Protect global state against multiple changers. */
60 __libc_lock_define_initialized (static, lock)
61
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 *);
65
66 static void
67 give_pwd_free (struct passwd *pwd)
68 {
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);
79
80 memset (pwd, '\0', sizeof (struct passwd));
81 }
82
83 static size_t
84 pwd_need_buflen (struct passwd *pwd)
85 {
86 size_t len = 0;
87
88 if (pwd->pw_passwd != NULL)
89 len += strlen (pwd->pw_passwd) + 1;
90
91 if (pwd->pw_gecos != NULL)
92 len += strlen (pwd->pw_gecos) + 1;
93
94 if (pwd->pw_dir != NULL)
95 len += strlen (pwd->pw_dir) + 1;
96
97 if (pwd->pw_shell != NULL)
98 len += strlen (pwd->pw_shell) + 1;
99
100 return len;
101 }
102
103 static void
104 copy_pwd_changes (struct passwd *dest, struct passwd *src,
105 char *buffer, size_t buflen)
106 {
107 if (src->pw_passwd != NULL && strlen (src->pw_passwd))
108 {
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
115 {
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);
120 }
121 }
122
123 if (src->pw_gecos != NULL && strlen (src->pw_gecos))
124 {
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
131 {
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);
136 }
137 }
138 if (src->pw_dir != NULL && strlen (src->pw_dir))
139 {
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
146 {
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);
151 }
152 }
153
154 if (src->pw_shell != NULL && strlen (src->pw_shell))
155 {
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
162 {
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);
167 }
168 }
169 }
170
171 static enum nss_status
172 internal_setpwent (ent_t *ent)
173 {
174 enum nss_status status = NSS_STATUS_SUCCESS;
175
176 ent->nis = ent->first = ent->netgroup = 0;
177
178 /* If something was left over free it. */
179 if (ent->netgroup)
180 __internal_endnetgrent (&ent->netgrdata);
181
182 if (ent->oldkey != NULL)
183 {
184 free (ent->oldkey);
185 ent->oldkey = NULL;
186 ent->oldkeylen = 0;
187 }
188
189 ent->blacklist.current = 0;
190 if (ent->blacklist.data != NULL)
191 ent->blacklist.data[0] = '\0';
192
193 if (ent->stream == NULL)
194 {
195 ent->stream = fopen ("/etc/passwd", "r");
196
197 if (ent->stream == NULL)
198 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
199 }
200 else
201 rewind (ent->stream);
202
203 give_pwd_free (&ent->pwd);
204
205 return status;
206 }
207
208
209 enum nss_status
210 _nss_compat_setpwent (void)
211 {
212 enum nss_status result;
213
214 __libc_lock_lock (lock);
215
216 result = internal_setpwent (&ext_ent);
217
218 __libc_lock_unlock (lock);
219
220 return result;
221 }
222
223
224 static enum nss_status
225 internal_endpwent (ent_t *ent)
226 {
227 if (ent->stream != NULL)
228 {
229 fclose (ent->stream);
230 ent->stream = NULL;
231 }
232
233 ent->nis = ent->first = ent->netgroup = 0;
234
235 if (ent->oldkey != NULL)
236 {
237 free (ent->oldkey);
238 ent->oldkey = NULL;
239 ent->oldkeylen = 0;
240 }
241
242 ent->blacklist.current = 0;
243 if (ent->blacklist.data != NULL)
244 ent->blacklist.data[0] = '\0';
245
246 give_pwd_free (&ent->pwd);
247
248 return NSS_STATUS_SUCCESS;
249 }
250
251 enum nss_status
252 _nss_compat_endpwent (void)
253 {
254 enum nss_status result;
255
256 __libc_lock_lock (lock);
257
258 if (ext_ent.netgroup)
259 __internal_endnetgrent (&ext_ent.netgrdata);
260
261 result = internal_endpwent (&ext_ent);
262
263 __libc_lock_unlock (lock);
264
265 return result;
266 }
267
268 static enum nss_status
269 getpwent_next_netgr (struct passwd *result, ent_t *ent, char *group,
270 char *buffer, size_t buflen)
271 {
272 char *ypdomain, *host, *user, *domain, *outval, *p, *p2;
273 int status, outvallen, p2len;
274
275 if (yp_get_default_domain (&ypdomain) != YPERR_SUCCESS)
276 {
277 ent->netgroup = 0;
278 ent->first = 0;
279 give_pwd_free (&ent->pwd);
280 return NSS_STATUS_UNAVAIL;
281 }
282
283 if (ent->first == TRUE)
284 {
285 bzero (&ent->netgrdata, sizeof (struct __netgrent));
286 __internal_setnetgrent (group, &ent->netgrdata);
287 ent->first = FALSE;
288 }
289
290 while (1)
291 {
292 status = __internal_getnetgrent (&host, &user, &domain, &ent->netgrdata,
293 buffer, buflen);
294 if (status != 1)
295 {
296 __internal_endnetgrent (&ent->netgrdata);
297 ent->netgroup = 0;
298 give_pwd_free (&ent->pwd);
299 return NSS_STATUS_RETURN;
300 }
301
302 if (user == NULL || user[0] == '-')
303 continue;
304
305 if (domain != NULL && strcmp (ypdomain, domain) != 0)
306 continue;
307
308 if (yp_match (ypdomain, "passwd.byname", user,
309 strlen (user), &outval, &outvallen)
310 != YPERR_SUCCESS)
311 continue;
312
313 p2len = pwd_need_buflen (&ent->pwd);
314 if (p2len > buflen)
315 {
316 __set_errno (ERANGE);
317 return NSS_STATUS_TRYAGAIN;
318 }
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))
326 {
327 copy_pwd_changes (result, &ent->pwd, p2, p2len);
328 break;
329 }
330 }
331
332 return NSS_STATUS_SUCCESS;
333 }
334
335 static enum nss_status
336 getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
337 size_t buflen)
338 {
339 char *domain, *outkey, *outval, *p, *p2;
340 int outkeylen, outvallen, p2len;
341
342 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
343 {
344 ent->nis = 0;
345 give_pwd_free (&ent->pwd);
346 return NSS_STATUS_UNAVAIL;
347 }
348
349 p2len = pwd_need_buflen (&ent->pwd);
350 if (p2len > buflen)
351 {
352 __set_errno (ERANGE);
353 return NSS_STATUS_TRYAGAIN;
354 }
355 p2 = buffer + (buflen - p2len);
356 buflen -= p2len;
357 do
358 {
359 if (ent->first)
360 {
361 if (yp_first (domain, "passwd.byname", &outkey, &outkeylen,
362 &outval, &outvallen) != YPERR_SUCCESS)
363 {
364 ent->nis = 0;
365 give_pwd_free (&ent->pwd);
366 return NSS_STATUS_UNAVAIL;
367 }
368
369 ent->oldkey = outkey;
370 ent->oldkeylen = outkeylen;
371 ent->first = FALSE;
372 }
373 else
374 {
375 if (yp_next (domain, "passwd.byname", ent->oldkey, ent->oldkeylen,
376 &outkey, &outkeylen, &outval, &outvallen)
377 != YPERR_SUCCESS)
378 {
379 ent->nis = 0;
380 give_pwd_free (&ent->pwd);
381 return NSS_STATUS_NOTFOUND;
382 }
383
384 free (ent->oldkey);
385 ent->oldkey = outkey;
386 ent->oldkeylen = outkeylen;
387 }
388
389 /* Copy the found data to our buffer */
390 p = strncpy (buffer, outval, buflen);
391
392 /* ...and free the data. */
393 free (outval);
394
395 while (isspace (*p))
396 ++p;
397 }
398 while (!_nss_files_parse_pwent (p, result, buffer, buflen));
399
400 copy_pwd_changes (result, &ent->pwd, p2, p2len);
401
402 if (!in_blacklist (result->pw_name, strlen (result->pw_name), ent))
403 return NSS_STATUS_SUCCESS;
404 else
405 return NSS_STATUS_NOTFOUND;
406 }
407
408
409 static enum nss_status
410 getpwent_next_file (struct passwd *result, ent_t *ent,
411 char *buffer, size_t buflen)
412 {
413 while (1)
414 {
415 char *p, *p2;
416 int p2len;
417
418 do
419 {
420 p = fgets (buffer, buflen, ent->stream);
421 if (p == NULL)
422 return NSS_STATUS_NOTFOUND;
423
424 /* Terminate the line for any case. */
425 buffer[buflen - 1] = '\0';
426
427 /* Skip leading blanks. */
428 while (isspace (*p))
429 ++p;
430 }
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));
435
436 if (result->pw_name[0] != '+' && result->pw_name[0] != '-')
437 /* This is a real entry. */
438 break;
439
440 /* -@netgroup */
441 if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
442 && result->pw_name[2] != '\0')
443 {
444 char *user, *host, *domain;
445
446 setnetgrent (&result->pw_name[2]);
447 while (getnetgrent (&host, &user, &domain))
448 {
449 if (user != NULL && user[0] != '-')
450 blacklist_store_name (user, ent);
451 }
452 endnetgrent ();
453 continue;
454 }
455
456 /* +@netgroup */
457 if (result->pw_name[0] == '+' && result->pw_name[1] == '@'
458 && result->pw_name[2] != '\0')
459 {
460 int status;
461
462 ent->netgroup = TRUE;
463 ent->first = TRUE;
464 copy_pwd_changes (&ent->pwd, result, NULL, 0);
465
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;
472 }
473
474 /* -user */
475 if (result->pw_name[0] == '-' && result->pw_name[1] != '\0'
476 && result->pw_name[1] != '@')
477 {
478 blacklist_store_name (&result->pw_name[1], ent);
479 continue;
480 }
481
482 /* +user */
483 if (result->pw_name[0] == '+' && result->pw_name[1] != '\0'
484 && result->pw_name[1] != '@')
485 {
486 char *domain;
487 char *outval;
488 int outvallen;
489 struct passwd pwd;
490
491 memset (&pwd, '\0', sizeof (struct passwd));
492
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;
497
498 if (yp_match (domain, "passwd.byname", &result->pw_name[1],
499 strlen (result->pw_name) - 1, &outval, &outvallen)
500 != YPERR_SUCCESS)
501 continue;
502
503 copy_pwd_changes (&pwd, result, NULL, 0);
504
505 p2len = pwd_need_buflen (&pwd);
506 if (p2len > buflen)
507 {
508 __set_errno (ERANGE);
509 return NSS_STATUS_TRYAGAIN;
510 }
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))
518 {
519 copy_pwd_changes (result, &pwd, p2, p2len);
520 give_pwd_free (&pwd);
521 /* We found the entry. */
522 break;
523 }
524 else
525 {
526 /* Give buffer the old len back */
527 buflen += p2len;
528 give_pwd_free (&pwd);
529 }
530 }
531
532 /* +:... */
533 if (result->pw_name[0] == '+' && result->pw_name[1] == '\0')
534 {
535 ent->nis = TRUE;
536 ent->first = TRUE;
537 copy_pwd_changes (&ent->pwd, result, NULL, 0);
538
539 return getpwent_next_nis (result, ent, buffer, buflen);
540 }
541 }
542
543 return NSS_STATUS_SUCCESS;
544 }
545
546
547 static enum nss_status
548 internal_getpwent_r (struct passwd *pw, ent_t *ent, char *buffer,
549 size_t buflen)
550 {
551 if (ent->netgroup)
552 {
553 int status;
554
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;
562 }
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);
567 }
568
569 enum nss_status
570 _nss_compat_getpwent_r (struct passwd *pwd, char *buffer,
571 size_t buflen)
572 {
573 enum nss_status status = NSS_STATUS_SUCCESS;
574
575 __libc_lock_lock (lock);
576
577 /* Be prepared that the setpwent function was not called before. */
578 if (ext_ent.stream == NULL)
579 status = internal_setpwent (&ext_ent);
580
581 if (status == NSS_STATUS_SUCCESS)
582 status = internal_getpwent_r (pwd, &ext_ent, buffer, buflen);
583
584 __libc_lock_unlock (lock);
585
586 return status;
587 }
588
589
590 enum nss_status
591 _nss_compat_getpwnam_r (const char *name, struct passwd *pwd,
592 char *buffer, size_t buflen)
593 {
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;
597
598 if (name[0] == '-' || name[0] == '+')
599 return NSS_STATUS_NOTFOUND;
600
601
602 status = internal_setpwent (&ent);
603 if (status != NSS_STATUS_SUCCESS)
604 return status;
605
606 while ((status = internal_getpwent_r (pwd, &ent, buffer, buflen))
607 == NSS_STATUS_SUCCESS)
608 if (strcmp (pwd->pw_name, name) == 0)
609 break;
610
611 internal_endpwent (&ent);
612 return status;
613 }
614
615
616 enum nss_status
617 _nss_compat_getpwuid_r (uid_t uid, struct passwd *pwd,
618 char *buffer, size_t buflen)
619 {
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;
623
624 status = internal_setpwent (&ent);
625 if (status != NSS_STATUS_SUCCESS)
626 return status;
627
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;
632
633 internal_endpwent (&ent);
634 return status;
635 }
636
637
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)
642 {
643 int namelen = strlen (name);
644 char *tmp;
645
646 /* first call, setup cache */
647 if (ent->blacklist.size == 0)
648 {
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;
656 }
657 else
658 {
659 if (in_blacklist (name, namelen, ent))
660 return; /* no duplicates */
661
662 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
663 {
664 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
665 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
666 if (tmp == NULL)
667 {
668 free (ent->blacklist.data);
669 ent->blacklist.size = 0;
670 return;
671 }
672 ent->blacklist.data = tmp;
673 }
674 }
675
676 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
677 *tmp++ = '|';
678 *tmp = '\0';
679 ent->blacklist.current += namelen + 1;
680
681 return;
682 }
683
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)
687 {
688 char buf[namelen + 3];
689
690 if (ent->blacklist.data == NULL)
691 return FALSE;
692
693 stpcpy (stpcpy (stpcpy (buf, "|"), name), "|");
694 return strstr (ent->blacklist.data, buf) != NULL;
695 }
This page took 0.068902 seconds and 6 git commands to generate.