]> sourceware.org Git - glibc.git/blame - nis/nss_compat/compat-spwd.c
Update.
[glibc.git] / nis / nss_compat / compat-spwd.c
CommitLineData
1e2e27fd 1/* Copyright (C) 1996,1997,1998,1999,2001,2002,2003 Free Software Foundation, Inc.
6259ec0d
UD
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
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
6259ec0d
UD
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
41bdb6e2 13 Lesser General Public License for more details.
6259ec0d 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
6259ec0d
UD
19
20#include <nss.h>
21#include <errno.h>
22#include <ctype.h>
3996f34b 23#include <fcntl.h>
6259ec0d
UD
24#include <netdb.h>
25#include <shadow.h>
26#include <string.h>
5107cf1d 27#include <bits/libc-lock.h>
1e2e27fd 28#include <rpc/types.h>
6259ec0d 29#include <rpcsvc/ypclnt.h>
26dee9c4
UD
30#include <nsswitch.h>
31
32#include "netgroup.h"
26dee9c4 33
fc9f33e3 34static service_user *ni;
1e2e27fd
UD
35static enum nss_status (*nss_setspent) (int stayopen);
36static enum nss_status (*nss_getspnam_r) (const char *name, struct spwd * sp,
37 char *buffer, size_t buflen,
38 int *errnop);
39static enum nss_status (*nss_getspent_r) (struct spwd * sp, char *buffer,
40 size_t buflen, int *errnop);
41static enum nss_status (*nss_endspent) (void);
6259ec0d 42
7e3be507
UD
43/* Get the declaration of the parser function. */
44#define ENTNAME spent
45#define STRUCTURE spwd
46#define EXTERN_PARSER
cc3fa755 47#include <nss/nss_files/files-parse.c>
7e3be507 48
6259ec0d
UD
49/* Structure for remembering -@netgroup and -user members ... */
50#define BLACKLIST_INITIAL_SIZE 512
51#define BLACKLIST_INCREMENT 256
52struct blacklist_t
1e2e27fd
UD
53{
54 char *data;
55 int current;
56 int size;
57};
6259ec0d
UD
58
59struct ent_t
1e2e27fd
UD
60{
61 bool_t netgroup;
62 bool_t files;
63 bool_t first;
64 FILE *stream;
65 struct blacklist_t blacklist;
66 struct spwd pwd;
67 struct __netgrent netgrdata;
68};
6259ec0d
UD
69typedef struct ent_t ent_t;
70
1e2e27fd 71static ent_t ext_ent = {0, TRUE, 0, NULL, {NULL, 0, 0},
6259ec0d
UD
72 {NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
73
74/* Protect global state against multiple changers. */
75__libc_lock_define_initialized (static, lock)
76
77/* Prototypes for local functions. */
78static void blacklist_store_name (const char *, ent_t *);
79static int in_blacklist (const char *, int, ent_t *);
2d7da676 80
1e2e27fd
UD
81/* Initialize the NSS interface/functions. The calling function must
82 hold the lock. */
83static void
84init_nss_interface (void)
85{
86 if (__nss_database_lookup ("shadow_compat", "passwd_compat",
87 "nis", &ni) >= 0)
88 {
89 nss_setspent = __nss_lookup_function (ni, "setspent");
90 nss_getspnam_r = __nss_lookup_function (ni, "getspnam_r");
91 nss_getspent_r = __nss_lookup_function (ni, "getspent_r");
92 nss_endspent = __nss_lookup_function (ni, "endspent");
93 }
94}
95
6259ec0d
UD
96static void
97give_spwd_free (struct spwd *pwd)
98{
99 if (pwd->sp_namp != NULL)
100 free (pwd->sp_namp);
101 if (pwd->sp_pwdp != NULL)
102 free (pwd->sp_pwdp);
103
104 memset (pwd, '\0', sizeof (struct spwd));
b378b9f9
UD
105 pwd->sp_warn = -1;
106 pwd->sp_inact = -1;
107 pwd->sp_expire = -1;
108 pwd->sp_flag = ~0ul;
6259ec0d
UD
109}
110
111static int
112spwd_need_buflen (struct spwd *pwd)
113{
114 int len = 0;
115
116 if (pwd->sp_pwdp != NULL)
117 len += strlen (pwd->sp_pwdp) + 1;
118
119 return len;
120}
121
122static void
123copy_spwd_changes (struct spwd *dest, struct spwd *src,
124 char *buffer, size_t buflen)
125{
126 if (src->sp_pwdp != NULL && strlen (src->sp_pwdp))
127 {
128 if (buffer == NULL)
129 dest->sp_pwdp = strdup (src->sp_pwdp);
130 else if (dest->sp_pwdp &&
131 strlen (dest->sp_pwdp) >= strlen (src->sp_pwdp))
132 strcpy (dest->sp_pwdp, src->sp_pwdp);
133 else
134 {
135 dest->sp_pwdp = buffer;
136 strcpy (dest->sp_pwdp, src->sp_pwdp);
137 buffer += strlen (dest->sp_pwdp) + 1;
138 buflen = buflen - (strlen (dest->sp_pwdp) + 1);
139 }
140 }
141 if (src->sp_lstchg != 0)
142 dest->sp_lstchg = src->sp_lstchg;
143 if (src->sp_min != 0)
144 dest->sp_min = src->sp_min;
145 if (src->sp_max != 0)
146 dest->sp_max = src->sp_max;
b378b9f9 147 if (src->sp_warn != -1)
6259ec0d 148 dest->sp_warn = src->sp_warn;
b378b9f9 149 if (src->sp_inact != -1)
6259ec0d 150 dest->sp_inact = src->sp_inact;
b378b9f9 151 if (src->sp_expire != -1)
6259ec0d 152 dest->sp_expire = src->sp_expire;
b378b9f9 153 if (src->sp_flag != ~0ul)
6259ec0d
UD
154 dest->sp_flag = src->sp_flag;
155}
156
157static enum nss_status
1e2e27fd 158internal_setspent (ent_t *ent, int stayopen)
6259ec0d
UD
159{
160 enum nss_status status = NSS_STATUS_SUCCESS;
161
1e2e27fd
UD
162 ent->first = ent->netgroup = 0;
163 ent->files = TRUE;
6259ec0d 164
26dee9c4
UD
165 /* If something was left over free it. */
166 if (ent->netgroup)
167 __internal_endnetgrent (&ent->netgrdata);
c131718c 168
6259ec0d 169 if (ent->blacklist.data != NULL)
bd355af0
UD
170 {
171 ent->blacklist.current = 1;
172 ent->blacklist.data[0] = '|';
173 ent->blacklist.data[1] = '\0';
174 }
175 else
176 ent->blacklist.current = 0;
6259ec0d
UD
177
178 if (ent->stream == NULL)
179 {
180 ent->stream = fopen ("/etc/shadow", "r");
181
182 if (ent->stream == NULL)
183 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
3996f34b
UD
184 else
185 {
186 /* We have to make sure the file is `closed on exec'. */
187 int result, flags;
188
189 result = flags = fcntl (fileno (ent->stream), F_GETFD, 0);
190 if (result >= 0)
191 {
192 flags |= FD_CLOEXEC;
193 result = fcntl (fileno (ent->stream), F_SETFD, flags);
194 }
195 if (result < 0)
196 {
197 /* Something went wrong. Close the stream and return a
1e2e27fd 198 failure. */
3996f34b
UD
199 fclose (ent->stream);
200 ent->stream = NULL;
201 status = NSS_STATUS_UNAVAIL;
202 }
203 }
6259ec0d
UD
204 }
205 else
206 rewind (ent->stream);
207
208 give_spwd_free (&ent->pwd);
209
1e2e27fd
UD
210 if (status == NSS_STATUS_SUCCESS && nss_setspent)
211 return nss_setspent (stayopen);
212
6259ec0d
UD
213 return status;
214}
215
216
217enum nss_status
51eecc4a 218_nss_compat_setspent (int stayopen)
6259ec0d
UD
219{
220 enum nss_status result;
221
222 __libc_lock_lock (lock);
223
26dee9c4 224 if (ni == NULL)
1e2e27fd 225 init_nss_interface ();
c131718c 226
1e2e27fd 227 result = internal_setspent (&ext_ent, stayopen);
6259ec0d
UD
228
229 __libc_lock_unlock (lock);
230
231 return result;
232}
233
234
235static enum nss_status
236internal_endspent (ent_t *ent)
237{
1e2e27fd
UD
238 if (nss_endspent)
239 nss_endspent ();
240
6259ec0d
UD
241 if (ent->stream != NULL)
242 {
243 fclose (ent->stream);
244 ent->stream = NULL;
245 }
246
26dee9c4
UD
247 if (ent->netgroup)
248 __internal_endnetgrent (&ent->netgrdata);
249
1e2e27fd
UD
250 ent->first = ent->netgroup = FALSE;
251 ent->files = TRUE;
c131718c 252
6259ec0d 253 if (ent->blacklist.data != NULL)
bd355af0
UD
254 {
255 ent->blacklist.current = 1;
256 ent->blacklist.data[0] = '|';
257 ent->blacklist.data[1] = '\0';
258 }
259 else
260 ent->blacklist.current = 0;
c131718c 261
6259ec0d
UD
262 give_spwd_free (&ent->pwd);
263
264 return NSS_STATUS_SUCCESS;
265}
266
267enum nss_status
268_nss_compat_endspent (void)
269{
270 enum nss_status result;
271
272 __libc_lock_lock (lock);
273
274 result = internal_endspent (&ext_ent);
275
276 __libc_lock_unlock (lock);
277
278 return result;
279}
280
281
282static enum nss_status
1e2e27fd
UD
283getspent_next_nss_netgr (const char *name, struct spwd *result, ent_t *ent,
284 char *group, char *buffer, size_t buflen,
285 int *errnop)
6259ec0d 286{
1e2e27fd 287 char *curdomain, *host, *user, *domain, *p2;
6259ec0d
UD
288 size_t p2len;
289
1e2e27fd
UD
290 if (!nss_getspnam_r)
291 return NSS_STATUS_UNAVAIL;
292
293 if (yp_get_default_domain (&curdomain) != YPERR_SUCCESS)
6259ec0d 294 {
1e2e27fd
UD
295 ent->netgroup = FALSE;
296 ent->first = FALSE;
6259ec0d
UD
297 give_spwd_free (&ent->pwd);
298 return NSS_STATUS_UNAVAIL;
299 }
300
301 if (ent->first == TRUE)
302 {
1e2e27fd 303 memset (&ent->netgrdata, 0, sizeof (struct __netgrent));
26dee9c4 304 __internal_setnetgrent (group, &ent->netgrdata);
6259ec0d
UD
305 ent->first = FALSE;
306 }
307
308 while (1)
309 {
60c96635 310 char *saved_cursor;
1e2e27fd 311 enum nss_status status;
60c96635
UD
312
313 saved_cursor = ent->netgrdata.cursor;
26dee9c4 314 status = __internal_getnetgrent_r (&host, &user, &domain,
d71b808a
UD
315 &ent->netgrdata, buffer, buflen,
316 errnop);
26dee9c4 317 if (status != 1)
6259ec0d 318 {
26dee9c4 319 __internal_endnetgrent (&ent->netgrdata);
1e2e27fd 320 ent->netgroup = FALSE;
6259ec0d
UD
321 give_spwd_free (&ent->pwd);
322 return NSS_STATUS_RETURN;
323 }
3996f34b 324
6259ec0d
UD
325 if (user == NULL || user[0] == '-')
326 continue;
3996f34b 327
1e2e27fd 328 if (domain != NULL && strcmp (curdomain, domain) != 0)
6259ec0d
UD
329 continue;
330
cc3fa755
UD
331 /* If name != NULL, we are called from getpwnam */
332 if (name != NULL)
333 if (strcmp (user, name) != 0)
334 continue;
335
6259ec0d
UD
336 p2len = spwd_need_buflen (&ent->pwd);
337 if (p2len > buflen)
338 {
d71b808a 339 *errnop = ERANGE;
6259ec0d
UD
340 return NSS_STATUS_TRYAGAIN;
341 }
342 p2 = buffer + (buflen - p2len);
343 buflen -= p2len;
26dee9c4 344
1e2e27fd
UD
345 if (nss_getspnam_r (user, result, buffer, buflen, errnop) !=
346 NSS_STATUS_SUCCESS)
347 continue;
cc3fa755 348
1e2e27fd 349 if (!in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
60c96635 350 {
1e2e27fd
UD
351 /* Store the User in the blacklist for possible the "+" at the
352 end of /etc/passwd */
cc3fa755
UD
353 blacklist_store_name (result->sp_namp, ent);
354 copy_spwd_changes (result, &ent->pwd, p2, p2len);
1e2e27fd 355 break;
60c96635 356 }
26dee9c4 357 }
c131718c 358
26dee9c4
UD
359 return NSS_STATUS_SUCCESS;
360}
361
362
6259ec0d 363static enum nss_status
1e2e27fd 364getspent_next_nss (struct spwd *result, ent_t *ent,
d71b808a 365 char *buffer, size_t buflen, int *errnop)
6259ec0d 366{
1e2e27fd
UD
367 enum nss_status status;
368 char *p2;
6259ec0d
UD
369 size_t p2len;
370
1e2e27fd
UD
371 if (!nss_getspent_r)
372 return NSS_STATUS_UNAVAIL;
6259ec0d
UD
373
374 p2len = spwd_need_buflen (&ent->pwd);
375 if (p2len > buflen)
376 {
d71b808a 377 *errnop = ERANGE;
6259ec0d
UD
378 return NSS_STATUS_TRYAGAIN;
379 }
380 p2 = buffer + (buflen - p2len);
381 buflen -= p2len;
382 do
383 {
1e2e27fd
UD
384 if ((status = nss_getspent_r (result, buffer, buflen, errnop)) !=
385 NSS_STATUS_SUCCESS)
386 return status;
6259ec0d 387 }
1e2e27fd 388 while (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent));
6259ec0d
UD
389
390 copy_spwd_changes (result, &ent->pwd, p2, p2len);
391
26dee9c4 392 return NSS_STATUS_SUCCESS;
6259ec0d
UD
393}
394
26dee9c4
UD
395/* This function handle the +user entrys in /etc/shadow */
396static enum nss_status
1e2e27fd
UD
397getspnam_plususer (const char *name, struct spwd *result, ent_t *ent,
398 char *buffer, size_t buflen, int *errnop)
26dee9c4 399{
26dee9c4 400 struct spwd pwd;
26dee9c4
UD
401 char *p;
402 size_t plen;
c131718c 403
1e2e27fd
UD
404 if (!nss_getspnam_r)
405 return NSS_STATUS_UNAVAIL;
406
26dee9c4 407 memset (&pwd, '\0', sizeof (struct spwd));
b378b9f9
UD
408 pwd.sp_warn = -1;
409 pwd.sp_inact = -1;
410 pwd.sp_expire = -1;
411 pwd.sp_flag = ~0ul;
c131718c 412
26dee9c4 413 copy_spwd_changes (&pwd, result, NULL, 0);
c131718c 414
26dee9c4
UD
415 plen = spwd_need_buflen (&pwd);
416 if (plen > buflen)
417 {
d71b808a 418 *errnop = ERANGE;
26dee9c4
UD
419 return NSS_STATUS_TRYAGAIN;
420 }
421 p = buffer + (buflen - plen);
422 buflen -= plen;
c131718c 423
1e2e27fd
UD
424 if (nss_getspnam_r (name, result, buffer, buflen, errnop) !=
425 NSS_STATUS_SUCCESS)
426 return NSS_STATUS_NOTFOUND;
6591c335 427
1e2e27fd
UD
428 if (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
429 return NSS_STATUS_NOTFOUND;
c131718c 430
1e2e27fd
UD
431 copy_spwd_changes (result, &pwd, p, plen);
432 give_spwd_free (&pwd);
433 /* We found the entry. */
26dee9c4
UD
434 return NSS_STATUS_RETURN;
435}
6259ec0d
UD
436
437static enum nss_status
438getspent_next_file (struct spwd *result, ent_t *ent,
d71b808a 439 char *buffer, size_t buflen, int *errnop)
6259ec0d 440{
7e3be507 441 struct parser_data *data = (void *) buffer;
6259ec0d
UD
442 while (1)
443 {
60c96635
UD
444 fpos_t pos;
445 int parse_res = 0;
c131718c 446 char *p;
6259ec0d
UD
447
448 do
449 {
60c96635 450 fgetpos (ent->stream, &pos);
af69217f 451 buffer[buflen - 1] = '\xff';
6259ec0d 452 p = fgets (buffer, buflen, ent->stream);
af69217f 453 if (p == NULL && feof (ent->stream))
34816665
UD
454 return NSS_STATUS_NOTFOUND;
455
af69217f
UD
456 if (p == NULL || buffer[buflen - 1] != '\xff')
457 {
458 fsetpos (ent->stream, &pos);
459 *errnop = ERANGE;
460 return NSS_STATUS_TRYAGAIN;
461 }
6259ec0d
UD
462
463 /* Skip leading blanks. */
464 while (isspace (*p))
465 ++p;
466 }
c131718c 467 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
1e2e27fd
UD
468 /* Parse the line. If it is invalid, loop to
469 get the next line of the file to parse. */
60c96635 470 || !(parse_res = _nss_files_parse_spent (p, result, data,
d71b808a 471 buflen, errnop)));
6259ec0d 472
60c96635 473 if (parse_res == -1)
1e2e27fd
UD
474 {
475 /* The parser ran out of space. */
476 fsetpos (ent->stream, &pos);
477 *errnop = ERANGE;
478 return NSS_STATUS_TRYAGAIN;
479 }
3996f34b 480
6259ec0d
UD
481 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
482 /* This is a real entry. */
483 break;
484
485 /* -@netgroup */
486 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
487 && result->sp_namp[2] != '\0')
488 {
d71b808a 489 /* XXX Do not use fixed length buffers. */
1e2e27fd 490 char buf2[1024];
51eecc4a 491 char *user, *host, *domain;
1e2e27fd 492 struct __netgrent netgrdata;
c131718c 493
1e2e27fd
UD
494 bzero (&netgrdata, sizeof (struct __netgrent));
495 __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
c131718c 496 while (__internal_getnetgrent_r (&host, &user, &domain,
d71b808a
UD
497 &netgrdata, buf2, sizeof (buf2),
498 errnop))
6259ec0d
UD
499 {
500 if (user != NULL && user[0] != '-')
501 blacklist_store_name (user, ent);
502 }
c131718c 503 __internal_endnetgrent (&netgrdata);
6259ec0d
UD
504 continue;
505 }
506
507 /* +@netgroup */
508 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
509 && result->sp_namp[2] != '\0')
510 {
511 int status;
512
513 ent->netgroup = TRUE;
514 ent->first = TRUE;
515 copy_spwd_changes (&ent->pwd, result, NULL, 0);
516
1e2e27fd
UD
517 status = getspent_next_nss_netgr (NULL, result, ent,
518 &result->sp_namp[2],
519 buffer, buflen, errnop);
6259ec0d
UD
520 if (status == NSS_STATUS_RETURN)
521 continue;
522 else
523 return status;
524 }
525
526 /* -user */
527 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
528 && result->sp_namp[1] != '@')
529 {
530 blacklist_store_name (&result->sp_namp[1], ent);
531 continue;
532 }
533
534 /* +user */
535 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
536 && result->sp_namp[1] != '@')
537 {
1e2e27fd 538 enum nss_status status;
c131718c 539
cc3fa755
UD
540 /* Store the User in the blacklist for the "+" at the end of
541 /etc/passwd */
542 blacklist_store_name (&result->sp_namp[1], ent);
1e2e27fd
UD
543 status = getspnam_plususer (&result->sp_namp[1], result, ent,
544 buffer, buflen, errnop);
545
546 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
547 break;
548 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
549 || status == NSS_STATUS_NOTFOUND) /* entry doesn't exist */
550 continue;
551 else
552 {
553 if (status == NSS_STATUS_TRYAGAIN)
554 {
555 fsetpos (ent->stream, &pos);
556 *errnop = ERANGE;
557 }
558 return status;
559 }
6259ec0d
UD
560 }
561
562 /* +:... */
563 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
564 {
1e2e27fd 565 ent->files = FALSE;
6259ec0d
UD
566 ent->first = TRUE;
567 copy_spwd_changes (&ent->pwd, result, NULL, 0);
568
1e2e27fd 569 return getspent_next_nss (result, ent, buffer, buflen, errnop);
6259ec0d
UD
570 }
571 }
572
573 return NSS_STATUS_SUCCESS;
574}
575
576
577static enum nss_status
578internal_getspent_r (struct spwd *pw, ent_t *ent,
d71b808a 579 char *buffer, size_t buflen, int *errnop)
6259ec0d
UD
580{
581 if (ent->netgroup)
582 {
1e2e27fd 583 enum nss_status status;
6259ec0d
UD
584
585 /* We are searching members in a netgroup */
586 /* Since this is not the first call, we don't need the group name */
1e2e27fd
UD
587 status = getspent_next_nss_netgr (NULL, pw, ent, NULL, buffer,
588 buflen, errnop);
589
6259ec0d 590 if (status == NSS_STATUS_RETURN)
d71b808a 591 return getspent_next_file (pw, ent, buffer, buflen, errnop);
6259ec0d
UD
592 else
593 return status;
594 }
1e2e27fd
UD
595 else if (ent->files)
596 return getspent_next_file (pw, ent, buffer, buflen, errnop);
6259ec0d 597 else
1e2e27fd 598 return getspent_next_nss (pw, ent, buffer, buflen, errnop);
6259ec0d
UD
599}
600
601enum nss_status
d71b808a
UD
602_nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen,
603 int *errnop)
6259ec0d 604{
1e2e27fd 605 enum nss_status result = NSS_STATUS_SUCCESS;
6259ec0d
UD
606
607 __libc_lock_lock (lock);
608
1e2e27fd 609 /* Be prepared that the setpwent function was not called before. */
26dee9c4 610 if (ni == NULL)
1e2e27fd 611 init_nss_interface ();
c131718c 612
6259ec0d 613 if (ext_ent.stream == NULL)
1e2e27fd 614 result = internal_setspent (&ext_ent, 1);
6259ec0d 615
1e2e27fd
UD
616 if (result == NSS_STATUS_SUCCESS)
617 result = internal_getspent_r (pwd, &ext_ent, buffer, buflen, errnop);
6259ec0d
UD
618
619 __libc_lock_unlock (lock);
620
1e2e27fd 621 return result;
6259ec0d
UD
622}
623
cc3fa755
UD
624/* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
625static enum nss_status
626internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
d71b808a 627 char *buffer, size_t buflen, int *errnop)
cc3fa755
UD
628{
629 struct parser_data *data = (void *) buffer;
630
631 while (1)
632 {
633 fpos_t pos;
634 char *p;
635 int parse_res;
636
637 do
638 {
639 fgetpos (ent->stream, &pos);
af69217f 640 buffer[buflen - 1] = '\xff';
cc3fa755 641 p = fgets (buffer, buflen, ent->stream);
af69217f 642 if (p == NULL && feof (ent->stream))
34816665
UD
643 return NSS_STATUS_NOTFOUND;
644
af69217f
UD
645 if (p == NULL || buffer[buflen - 1] != '\xff')
646 {
647 fsetpos (ent->stream, &pos);
648 *errnop = ERANGE;
649 return NSS_STATUS_TRYAGAIN;
650 }
cc3fa755
UD
651
652 /* Skip leading blanks. */
653 while (isspace (*p))
654 ++p;
655 }
1e2e27fd 656 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
cc3fa755 657 /* Parse the line. If it is invalid, loop to
1e2e27fd 658 get the next line of the file to parse. */
d71b808a
UD
659 !(parse_res = _nss_files_parse_spent (p, result, data, buflen,
660 errnop)));
cc3fa755
UD
661
662 if (parse_res == -1)
663 {
664 /* The parser ran out of space. */
665 fsetpos (ent->stream, &pos);
d71b808a 666 *errnop = ERANGE;
cc3fa755
UD
667 return NSS_STATUS_TRYAGAIN;
668 }
669
670 /* This is a real entry. */
671 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
672 {
673 if (strcmp (result->sp_namp, name) == 0)
674 return NSS_STATUS_SUCCESS;
675 else
676 continue;
677 }
678
679 /* -@netgroup */
1e2e27fd
UD
680 /* If the loaded NSS module does not support this service, add
681 all users from a +@netgroup entry to the blacklist, too. */
cc3fa755
UD
682 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
683 && result->sp_namp[2] != '\0')
684 {
d71b808a 685 /* XXX Do not use fixed length buffers. */
cc3fa755
UD
686 char buf2[1024];
687 char *user, *host, *domain;
688 struct __netgrent netgrdata;
689
690 bzero (&netgrdata, sizeof (struct __netgrent));
691 __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
d71b808a
UD
692 while (__internal_getnetgrent_r (&host, &user, &domain, &netgrdata,
693 buf2, sizeof (buf2), errnop))
cc3fa755
UD
694 {
695 if (user != NULL && user[0] != '-')
696 if (strcmp (user, name) == 0)
34816665 697 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
698 }
699 __internal_endnetgrent (&netgrdata);
700 continue;
701 }
702
703 /* +@netgroup */
704 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
705 && result->sp_namp[2] != '\0')
706 {
d71b808a 707 char *buf = strdupa (&result->sp_namp[2]);
cc3fa755
UD
708 int status;
709
cc3fa755
UD
710 ent->netgroup = TRUE;
711 ent->first = TRUE;
712 copy_spwd_changes (&ent->pwd, result, NULL, 0);
713
714 do
715 {
1e2e27fd
UD
716 status = getspent_next_nss_netgr (name, result, ent, buf,
717 buffer, buflen, errnop);
cc3fa755
UD
718 if (status == NSS_STATUS_RETURN)
719 continue;
720
d71b808a
UD
721 if (status == NSS_STATUS_SUCCESS
722 && strcmp (result->sp_namp, name) == 0)
cc3fa755 723 return NSS_STATUS_SUCCESS;
1e2e27fd
UD
724 }
725 while (status == NSS_STATUS_SUCCESS);
cc3fa755
UD
726 continue;
727 }
728
729 /* -user */
730 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
731 && result->sp_namp[1] != '@')
732 {
733 if (strcmp (&result->sp_namp[1], name) == 0)
34816665 734 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
735 else
736 continue;
737 }
738
739 /* +user */
740 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
741 && result->sp_namp[1] != '@')
742 {
743 if (strcmp (name, &result->sp_namp[1]) == 0)
744 {
745 enum nss_status status;
746
1e2e27fd
UD
747 status = getspnam_plususer (name, result, ent,
748 buffer, buflen, errnop);
749
cc3fa755 750 if (status == NSS_STATUS_RETURN)
34816665
UD
751 /* We couldn't parse the entry */
752 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
753 else
754 return status;
755 }
756 }
757
758 /* +:... */
759 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
760 {
761 enum nss_status status;
762
1e2e27fd
UD
763 status = getspnam_plususer (name, result, ent,
764 buffer, buflen, errnop);
765
766 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
34816665 767 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
768 else
769 return status;
770 }
771 }
772 return NSS_STATUS_SUCCESS;
773}
6259ec0d
UD
774
775enum nss_status
776_nss_compat_getspnam_r (const char *name, struct spwd *pwd,
d71b808a 777 char *buffer, size_t buflen, int *errnop)
6259ec0d 778{
1e2e27fd
UD
779 enum nss_status result;
780 ent_t ent = {0, TRUE, 0, NULL, {NULL, 0, 0},
6259ec0d 781 {NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
6259ec0d
UD
782
783 if (name[0] == '-' || name[0] == '+')
34816665 784 return NSS_STATUS_NOTFOUND;
6259ec0d 785
1e2e27fd
UD
786 __libc_lock_lock (lock);
787
26dee9c4 788 if (ni == NULL)
1e2e27fd
UD
789 init_nss_interface ();
790
791 __libc_lock_unlock (lock);
c131718c 792
1e2e27fd 793 result = internal_setspent (&ent, 0);
6259ec0d 794
1e2e27fd
UD
795 if (result != NSS_STATUS_SUCCESS)
796 return result;
797
798 result = internal_getspnam_r (name, pwd, &ent, buffer, buflen, errnop);
6259ec0d
UD
799
800 internal_endspent (&ent);
cc3fa755 801
1e2e27fd 802 return result;
6259ec0d
UD
803}
804
805/* Support routines for remembering -@netgroup and -user entries.
806 The names are stored in a single string with `|' as separator. */
807static void
808blacklist_store_name (const char *name, ent_t *ent)
809{
810 int namelen = strlen (name);
811 char *tmp;
812
813 /* first call, setup cache */
814 if (ent->blacklist.size == 0)
815 {
816 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
817 ent->blacklist.data = malloc (ent->blacklist.size);
818 if (ent->blacklist.data == NULL)
819 return;
820 ent->blacklist.data[0] = '|';
821 ent->blacklist.data[1] = '\0';
822 ent->blacklist.current = 1;
823 }
824 else
825 {
826 if (in_blacklist (name, namelen, ent))
827 return; /* no duplicates */
828
829 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
830 {
831 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
832 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
833 if (tmp == NULL)
834 {
835 free (ent->blacklist.data);
836 ent->blacklist.size = 0;
837 return;
838 }
839 ent->blacklist.data = tmp;
840 }
841 }
842
843 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
844 *tmp++ = '|';
845 *tmp = '\0';
846 ent->blacklist.current += namelen + 1;
847
848 return;
849}
850
c131718c 851/* Returns TRUE if ent->blacklist contains name, else FALSE. */
6259ec0d
UD
852static bool_t
853in_blacklist (const char *name, int namelen, ent_t *ent)
854{
855 char buf[namelen + 3];
c131718c 856 char *cp;
6259ec0d
UD
857
858 if (ent->blacklist.data == NULL)
859 return FALSE;
860
c131718c
UD
861 buf[0] = '|';
862 cp = stpcpy (&buf[1], name);
1e2e27fd 863 *cp++ = '|';
c131718c 864 *cp = '\0';
6259ec0d
UD
865 return strstr (ent->blacklist.data, buf) != NULL;
866}
This page took 0.30094 seconds and 5 git commands to generate.