]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nss_compat/compat-spwd.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nss / nss_compat / compat-spwd.c
CommitLineData
04277e02 1/* Copyright (C) 1996-2019 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 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
6259ec0d 18
6259ec0d 19#include <ctype.h>
1aa43890 20#include <errno.h>
3996f34b 21#include <fcntl.h>
6259ec0d 22#include <netdb.h>
1aa43890
UD
23#include <nss.h>
24#include <nsswitch.h>
6259ec0d 25#include <shadow.h>
1aa43890 26#include <stdio_ext.h>
6259ec0d 27#include <string.h>
ec999b8e 28#include <libc-lock.h>
cc783763 29#include <kernel-features.h>
26dee9c4
UD
30
31#include "netgroup.h"
64d1e08e 32#include "nisdomain.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 60{
cb62745a
UD
61 bool netgroup;
62 bool files;
63 bool first;
64 enum nss_status setent_status;
1e2e27fd
UD
65 FILE *stream;
66 struct blacklist_t blacklist;
67 struct spwd pwd;
68 struct __netgrent netgrdata;
69};
6259ec0d
UD
70typedef struct ent_t ent_t;
71
cb62745a
UD
72static ent_t ext_ent = { false, true, false, NSS_STATUS_SUCCESS, NULL,
73 { NULL, 0, 0},
74 { NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
6259ec0d
UD
75
76/* Protect global state against multiple changers. */
77__libc_lock_define_initialized (static, lock)
78
79/* Prototypes for local functions. */
80static void blacklist_store_name (const char *, ent_t *);
64d1e08e 81static bool in_blacklist (const char *, int, ent_t *);
2d7da676 82
1e2e27fd
UD
83/* Initialize the NSS interface/functions. The calling function must
84 hold the lock. */
85static void
86init_nss_interface (void)
87{
a9368c34
FW
88 if (__nss_database_lookup2 ("shadow_compat", "passwd_compat",
89 "nis", &ni) >= 0)
1e2e27fd
UD
90 {
91 nss_setspent = __nss_lookup_function (ni, "setspent");
92 nss_getspnam_r = __nss_lookup_function (ni, "getspnam_r");
93 nss_getspent_r = __nss_lookup_function (ni, "getspent_r");
94 nss_endspent = __nss_lookup_function (ni, "endspent");
95 }
96}
97
6259ec0d
UD
98static void
99give_spwd_free (struct spwd *pwd)
100{
72e6cdfa
UD
101 free (pwd->sp_namp);
102 free (pwd->sp_pwdp);
6259ec0d
UD
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);
34a5a146
JM
130 else if (dest->sp_pwdp
131 && strlen (dest->sp_pwdp) >= strlen (src->sp_pwdp))
6259ec0d
UD
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
b13b96ca 158internal_setspent (ent_t *ent, int stayopen, int needent)
6259ec0d
UD
159{
160 enum nss_status status = NSS_STATUS_SUCCESS;
161
1e2e27fd 162 ent->first = ent->netgroup = 0;
cb62745a 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 {
cc783763 180 ent->stream = fopen ("/etc/shadow", "rme");
6259ec0d
UD
181
182 if (ent->stream == NULL)
183 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
3996f34b 184 else
cef9b653
FW
185 /* We take care of locking ourself. */
186 __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
6259ec0d
UD
187 }
188 else
189 rewind (ent->stream);
190
191 give_spwd_free (&ent->pwd);
192
b13b96ca 193 if (needent && status == NSS_STATUS_SUCCESS && nss_setspent)
cb62745a 194 ent->setent_status = nss_setspent (stayopen);
1e2e27fd 195
6259ec0d
UD
196 return status;
197}
198
199
200enum nss_status
51eecc4a 201_nss_compat_setspent (int stayopen)
6259ec0d
UD
202{
203 enum nss_status result;
204
205 __libc_lock_lock (lock);
206
26dee9c4 207 if (ni == NULL)
1e2e27fd 208 init_nss_interface ();
c131718c 209
b13b96ca 210 result = internal_setspent (&ext_ent, stayopen, 1);
6259ec0d
UD
211
212 __libc_lock_unlock (lock);
213
214 return result;
215}
216
217
218static enum nss_status
219internal_endspent (ent_t *ent)
220{
221 if (ent->stream != NULL)
222 {
223 fclose (ent->stream);
224 ent->stream = NULL;
225 }
226
26dee9c4
UD
227 if (ent->netgroup)
228 __internal_endnetgrent (&ent->netgrdata);
229
cb62745a
UD
230 ent->first = ent->netgroup = false;
231 ent->files = true;
c131718c 232
6259ec0d 233 if (ent->blacklist.data != NULL)
bd355af0
UD
234 {
235 ent->blacklist.current = 1;
236 ent->blacklist.data[0] = '|';
237 ent->blacklist.data[1] = '\0';
238 }
239 else
240 ent->blacklist.current = 0;
c131718c 241
6259ec0d
UD
242 give_spwd_free (&ent->pwd);
243
244 return NSS_STATUS_SUCCESS;
245}
246
247enum nss_status
248_nss_compat_endspent (void)
249{
250 enum nss_status result;
251
252 __libc_lock_lock (lock);
253
b13b96ca
AS
254 if (nss_endspent)
255 nss_endspent ();
256
6259ec0d
UD
257 result = internal_endspent (&ext_ent);
258
259 __libc_lock_unlock (lock);
260
261 return result;
262}
263
264
265static enum nss_status
1e2e27fd
UD
266getspent_next_nss_netgr (const char *name, struct spwd *result, ent_t *ent,
267 char *group, char *buffer, size_t buflen,
268 int *errnop)
6259ec0d 269{
6d4d8e8e 270 char *curdomain = NULL, *host, *user, *domain, *p2;
6259ec0d
UD
271 size_t p2len;
272
1e2e27fd
UD
273 if (!nss_getspnam_r)
274 return NSS_STATUS_UNAVAIL;
275
cb62745a
UD
276 /* If the setpwent call failed, say so. */
277 if (ent->setent_status != NSS_STATUS_SUCCESS)
278 return ent->setent_status;
279
6d4d8e8e 280 if (ent->first)
6259ec0d 281 {
1e2e27fd 282 memset (&ent->netgrdata, 0, sizeof (struct __netgrent));
26dee9c4 283 __internal_setnetgrent (group, &ent->netgrdata);
cb62745a 284 ent->first = false;
6259ec0d
UD
285 }
286
287 while (1)
288 {
1e2e27fd 289 enum nss_status status;
60c96635 290
26dee9c4 291 status = __internal_getnetgrent_r (&host, &user, &domain,
d71b808a
UD
292 &ent->netgrdata, buffer, buflen,
293 errnop);
26dee9c4 294 if (status != 1)
6259ec0d 295 {
26dee9c4 296 __internal_endnetgrent (&ent->netgrdata);
cb62745a 297 ent->netgroup = false;
6259ec0d
UD
298 give_spwd_free (&ent->pwd);
299 return NSS_STATUS_RETURN;
300 }
3996f34b 301
6259ec0d
UD
302 if (user == NULL || user[0] == '-')
303 continue;
3996f34b 304
6d4d8e8e
AS
305 if (domain != NULL)
306 {
307 if (curdomain == NULL
64d1e08e 308 && __nss_get_default_domain (&curdomain) != 0)
6d4d8e8e
AS
309 {
310 __internal_endnetgrent (&ent->netgrdata);
311 ent->netgroup = false;
312 give_spwd_free (&ent->pwd);
313 return NSS_STATUS_UNAVAIL;
314 }
315 if (strcmp (curdomain, domain) != 0)
316 continue;
317 }
6259ec0d 318
cc3fa755
UD
319 /* If name != NULL, we are called from getpwnam */
320 if (name != NULL)
321 if (strcmp (user, name) != 0)
322 continue;
323
6259ec0d
UD
324 p2len = spwd_need_buflen (&ent->pwd);
325 if (p2len > buflen)
326 {
d71b808a 327 *errnop = ERANGE;
6259ec0d
UD
328 return NSS_STATUS_TRYAGAIN;
329 }
330 p2 = buffer + (buflen - p2len);
331 buflen -= p2len;
26dee9c4 332
a04549c1
JM
333 if (nss_getspnam_r (user, result, buffer, buflen, errnop)
334 != NSS_STATUS_SUCCESS)
1e2e27fd 335 continue;
cc3fa755 336
1e2e27fd 337 if (!in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
60c96635 338 {
1e2e27fd
UD
339 /* Store the User in the blacklist for possible the "+" at the
340 end of /etc/passwd */
cc3fa755
UD
341 blacklist_store_name (result->sp_namp, ent);
342 copy_spwd_changes (result, &ent->pwd, p2, p2len);
1e2e27fd 343 break;
60c96635 344 }
26dee9c4 345 }
c131718c 346
26dee9c4
UD
347 return NSS_STATUS_SUCCESS;
348}
349
350
6259ec0d 351static enum nss_status
1e2e27fd 352getspent_next_nss (struct spwd *result, ent_t *ent,
d71b808a 353 char *buffer, size_t buflen, int *errnop)
6259ec0d 354{
1e2e27fd
UD
355 enum nss_status status;
356 char *p2;
6259ec0d
UD
357 size_t p2len;
358
1e2e27fd
UD
359 if (!nss_getspent_r)
360 return NSS_STATUS_UNAVAIL;
6259ec0d
UD
361
362 p2len = spwd_need_buflen (&ent->pwd);
363 if (p2len > buflen)
364 {
d71b808a 365 *errnop = ERANGE;
6259ec0d
UD
366 return NSS_STATUS_TRYAGAIN;
367 }
368 p2 = buffer + (buflen - p2len);
369 buflen -= p2len;
370 do
371 {
a04549c1
JM
372 if ((status = nss_getspent_r (result, buffer, buflen, errnop))
373 != NSS_STATUS_SUCCESS)
1e2e27fd 374 return status;
6259ec0d 375 }
1e2e27fd 376 while (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent));
6259ec0d
UD
377
378 copy_spwd_changes (result, &ent->pwd, p2, p2len);
379
26dee9c4 380 return NSS_STATUS_SUCCESS;
6259ec0d
UD
381}
382
cb62745a 383
26dee9c4
UD
384/* This function handle the +user entrys in /etc/shadow */
385static enum nss_status
1e2e27fd
UD
386getspnam_plususer (const char *name, struct spwd *result, ent_t *ent,
387 char *buffer, size_t buflen, int *errnop)
26dee9c4 388{
1e2e27fd
UD
389 if (!nss_getspnam_r)
390 return NSS_STATUS_UNAVAIL;
391
016c70ea 392 struct spwd pwd;
26dee9c4 393 memset (&pwd, '\0', sizeof (struct spwd));
b378b9f9
UD
394 pwd.sp_warn = -1;
395 pwd.sp_inact = -1;
396 pwd.sp_expire = -1;
397 pwd.sp_flag = ~0ul;
c131718c 398
26dee9c4 399 copy_spwd_changes (&pwd, result, NULL, 0);
c131718c 400
016c70ea 401 size_t plen = spwd_need_buflen (&pwd);
26dee9c4
UD
402 if (plen > buflen)
403 {
d71b808a 404 *errnop = ERANGE;
26dee9c4
UD
405 return NSS_STATUS_TRYAGAIN;
406 }
016c70ea 407 char *p = buffer + (buflen - plen);
26dee9c4 408 buflen -= plen;
c131718c 409
016c70ea
UD
410 enum nss_status status = nss_getspnam_r (name, result, buffer, buflen,
411 errnop);
412 if (status != NSS_STATUS_SUCCESS)
413 return status;
6591c335 414
1e2e27fd
UD
415 if (in_blacklist (result->sp_namp, strlen (result->sp_namp), ent))
416 return NSS_STATUS_NOTFOUND;
c131718c 417
1e2e27fd
UD
418 copy_spwd_changes (result, &pwd, p, plen);
419 give_spwd_free (&pwd);
420 /* We found the entry. */
c503d3dc 421 return NSS_STATUS_SUCCESS;
26dee9c4 422}
6259ec0d 423
cb62745a 424
6259ec0d
UD
425static enum nss_status
426getspent_next_file (struct spwd *result, ent_t *ent,
d71b808a 427 char *buffer, size_t buflen, int *errnop)
6259ec0d 428{
7e3be507 429 struct parser_data *data = (void *) buffer;
6259ec0d
UD
430 while (1)
431 {
60c96635
UD
432 fpos_t pos;
433 int parse_res = 0;
c131718c 434 char *p;
6259ec0d
UD
435
436 do
437 {
20f8e666 438 /* We need at least 3 characters for one line. */
a1ffb40e 439 if (__glibc_unlikely (buflen < 3))
20f8e666
UD
440 {
441 erange:
442 *errnop = ERANGE;
443 return NSS_STATUS_TRYAGAIN;
444 }
445
60c96635 446 fgetpos (ent->stream, &pos);
af69217f 447 buffer[buflen - 1] = '\xff';
1aa43890
UD
448 p = fgets_unlocked (buffer, buflen, ent->stream);
449 if (p == NULL && feof_unlocked (ent->stream))
34816665
UD
450 return NSS_STATUS_NOTFOUND;
451
20f8e666 452 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
af69217f 453 {
20f8e666 454 erange_reset:
af69217f 455 fsetpos (ent->stream, &pos);
20f8e666 456 goto erange;
af69217f 457 }
6259ec0d
UD
458
459 /* Skip leading blanks. */
460 while (isspace (*p))
461 ++p;
462 }
c131718c 463 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
1e2e27fd
UD
464 /* Parse the line. If it is invalid, loop to
465 get the next line of the file to parse. */
60c96635 466 || !(parse_res = _nss_files_parse_spent (p, result, data,
d71b808a 467 buflen, errnop)));
6259ec0d 468
a1ffb40e 469 if (__glibc_unlikely (parse_res == -1))
20f8e666
UD
470 /* The parser ran out of space. */
471 goto erange_reset;
3996f34b 472
6259ec0d
UD
473 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
474 /* This is a real entry. */
475 break;
476
477 /* -@netgroup */
478 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
479 && result->sp_namp[2] != '\0')
480 {
d71b808a 481 /* XXX Do not use fixed length buffers. */
1e2e27fd 482 char buf2[1024];
51eecc4a 483 char *user, *host, *domain;
1e2e27fd 484 struct __netgrent netgrdata;
c131718c 485
1c6e6f23 486 memset (&netgrdata, 0, sizeof (struct __netgrent));
1e2e27fd 487 __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
c131718c 488 while (__internal_getnetgrent_r (&host, &user, &domain,
d71b808a
UD
489 &netgrdata, buf2, sizeof (buf2),
490 errnop))
6259ec0d
UD
491 {
492 if (user != NULL && user[0] != '-')
493 blacklist_store_name (user, ent);
494 }
c131718c 495 __internal_endnetgrent (&netgrdata);
6259ec0d
UD
496 continue;
497 }
498
499 /* +@netgroup */
500 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
501 && result->sp_namp[2] != '\0')
502 {
503 int status;
504
cb62745a
UD
505 ent->netgroup = true;
506 ent->first = true;
6259ec0d
UD
507 copy_spwd_changes (&ent->pwd, result, NULL, 0);
508
1e2e27fd
UD
509 status = getspent_next_nss_netgr (NULL, result, ent,
510 &result->sp_namp[2],
511 buffer, buflen, errnop);
6259ec0d
UD
512 if (status == NSS_STATUS_RETURN)
513 continue;
514 else
515 return status;
516 }
517
518 /* -user */
519 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
520 && result->sp_namp[1] != '@')
521 {
522 blacklist_store_name (&result->sp_namp[1], ent);
523 continue;
524 }
525
526 /* +user */
527 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
528 && result->sp_namp[1] != '@')
529 {
c503d3dc
UD
530 size_t len = strlen (result->sp_namp);
531 char buf[len];
1e2e27fd 532 enum nss_status status;
c131718c 533
cc3fa755
UD
534 /* Store the User in the blacklist for the "+" at the end of
535 /etc/passwd */
c503d3dc 536 memcpy (buf, &result->sp_namp[1], len);
1e2e27fd
UD
537 status = getspnam_plususer (&result->sp_namp[1], result, ent,
538 buffer, buflen, errnop);
c503d3dc 539 blacklist_store_name (buf, ent);
1e2e27fd
UD
540
541 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
542 break;
c503d3dc
UD
543 /* We couldn't parse the entry */
544 else if (status == NSS_STATUS_RETURN
545 /* entry doesn't exist */
546 || status == NSS_STATUS_NOTFOUND)
1e2e27fd
UD
547 continue;
548 else
549 {
550 if (status == NSS_STATUS_TRYAGAIN)
551 {
552 fsetpos (ent->stream, &pos);
553 *errnop = ERANGE;
554 }
555 return status;
556 }
6259ec0d
UD
557 }
558
559 /* +:... */
560 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
561 {
cb62745a
UD
562 ent->files = false;
563 ent->first = true;
6259ec0d
UD
564 copy_spwd_changes (&ent->pwd, result, NULL, 0);
565
1e2e27fd 566 return getspent_next_nss (result, ent, buffer, buflen, errnop);
6259ec0d
UD
567 }
568 }
569
570 return NSS_STATUS_SUCCESS;
571}
572
573
574static enum nss_status
575internal_getspent_r (struct spwd *pw, ent_t *ent,
d71b808a 576 char *buffer, size_t buflen, int *errnop)
6259ec0d
UD
577{
578 if (ent->netgroup)
579 {
1e2e27fd 580 enum nss_status status;
6259ec0d
UD
581
582 /* We are searching members in a netgroup */
583 /* Since this is not the first call, we don't need the group name */
1e2e27fd
UD
584 status = getspent_next_nss_netgr (NULL, pw, ent, NULL, buffer,
585 buflen, errnop);
586
6259ec0d 587 if (status == NSS_STATUS_RETURN)
d71b808a 588 return getspent_next_file (pw, ent, buffer, buflen, errnop);
6259ec0d
UD
589 else
590 return status;
591 }
1e2e27fd
UD
592 else if (ent->files)
593 return getspent_next_file (pw, ent, buffer, buflen, errnop);
6259ec0d 594 else
1e2e27fd 595 return getspent_next_nss (pw, ent, buffer, buflen, errnop);
6259ec0d
UD
596}
597
cb62745a 598
6259ec0d 599enum nss_status
d71b808a
UD
600_nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen,
601 int *errnop)
6259ec0d 602{
1e2e27fd 603 enum nss_status result = NSS_STATUS_SUCCESS;
6259ec0d
UD
604
605 __libc_lock_lock (lock);
606
1e2e27fd 607 /* Be prepared that the setpwent function was not called before. */
26dee9c4 608 if (ni == NULL)
1e2e27fd 609 init_nss_interface ();
c131718c 610
6259ec0d 611 if (ext_ent.stream == NULL)
b13b96ca 612 result = internal_setspent (&ext_ent, 1, 1);
6259ec0d 613
1e2e27fd
UD
614 if (result == NSS_STATUS_SUCCESS)
615 result = internal_getspent_r (pwd, &ext_ent, buffer, buflen, errnop);
6259ec0d
UD
616
617 __libc_lock_unlock (lock);
618
1e2e27fd 619 return result;
6259ec0d
UD
620}
621
cb62745a 622
cc3fa755
UD
623/* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
624static enum nss_status
625internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
d71b808a 626 char *buffer, size_t buflen, int *errnop)
cc3fa755
UD
627{
628 struct parser_data *data = (void *) buffer;
629
630 while (1)
631 {
632 fpos_t pos;
633 char *p;
634 int parse_res;
635
636 do
637 {
20f8e666 638 /* We need at least 3 characters for one line. */
a1ffb40e 639 if (__glibc_unlikely (buflen < 3))
20f8e666
UD
640 {
641 erange:
642 *errnop = ERANGE;
643 return NSS_STATUS_TRYAGAIN;
644 }
645
cc3fa755 646 fgetpos (ent->stream, &pos);
af69217f 647 buffer[buflen - 1] = '\xff';
1aa43890
UD
648 p = fgets_unlocked (buffer, buflen, ent->stream);
649 if (p == NULL && feof_unlocked (ent->stream))
34816665
UD
650 return NSS_STATUS_NOTFOUND;
651
af69217f
UD
652 if (p == NULL || buffer[buflen - 1] != '\xff')
653 {
20f8e666 654 erange_reset:
af69217f 655 fsetpos (ent->stream, &pos);
20f8e666 656 goto erange;
af69217f 657 }
cc3fa755 658
c503d3dc
UD
659 /* Terminate the line for any case. */
660 buffer[buflen - 1] = '\0';
661
cc3fa755
UD
662 /* Skip leading blanks. */
663 while (isspace (*p))
664 ++p;
665 }
a04549c1 666 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
cc3fa755 667 /* Parse the line. If it is invalid, loop to
1e2e27fd 668 get the next line of the file to parse. */
a04549c1
JM
669 || !(parse_res = _nss_files_parse_spent (p, result, data, buflen,
670 errnop)));
cc3fa755 671
a1ffb40e 672 if (__glibc_unlikely (parse_res == -1))
20f8e666
UD
673 /* The parser ran out of space. */
674 goto erange_reset;
cc3fa755
UD
675
676 /* This is a real entry. */
677 if (result->sp_namp[0] != '+' && result->sp_namp[0] != '-')
678 {
679 if (strcmp (result->sp_namp, name) == 0)
680 return NSS_STATUS_SUCCESS;
681 else
682 continue;
683 }
684
685 /* -@netgroup */
1e2e27fd
UD
686 /* If the loaded NSS module does not support this service, add
687 all users from a +@netgroup entry to the blacklist, too. */
cc3fa755
UD
688 if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
689 && result->sp_namp[2] != '\0')
690 {
c503d3dc
UD
691 if (innetgr (&result->sp_namp[2], NULL, name, NULL))
692 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
693 continue;
694 }
695
696 /* +@netgroup */
697 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
698 && result->sp_namp[2] != '\0')
699 {
c503d3dc 700 enum nss_status status;
cc3fa755 701
c503d3dc 702 if (innetgr (&result->sp_namp[2], NULL, name, NULL))
cc3fa755 703 {
c503d3dc
UD
704 status = getspnam_plususer (name, result, ent, buffer,
705 buflen, errnop);
706
cc3fa755
UD
707 if (status == NSS_STATUS_RETURN)
708 continue;
709
c503d3dc 710 return status;
1e2e27fd 711 }
cc3fa755
UD
712 continue;
713 }
714
715 /* -user */
716 if (result->sp_namp[0] == '-' && result->sp_namp[1] != '\0'
717 && result->sp_namp[1] != '@')
718 {
719 if (strcmp (&result->sp_namp[1], name) == 0)
34816665 720 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
721 else
722 continue;
723 }
724
725 /* +user */
726 if (result->sp_namp[0] == '+' && result->sp_namp[1] != '\0'
727 && result->sp_namp[1] != '@')
728 {
729 if (strcmp (name, &result->sp_namp[1]) == 0)
730 {
731 enum nss_status status;
732
1e2e27fd
UD
733 status = getspnam_plususer (name, result, ent,
734 buffer, buflen, errnop);
735
cc3fa755 736 if (status == NSS_STATUS_RETURN)
34816665
UD
737 /* We couldn't parse the entry */
738 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
739 else
740 return status;
741 }
742 }
743
744 /* +:... */
745 if (result->sp_namp[0] == '+' && result->sp_namp[1] == '\0')
746 {
747 enum nss_status status;
748
1e2e27fd
UD
749 status = getspnam_plususer (name, result, ent,
750 buffer, buflen, errnop);
751
c503d3dc
UD
752 if (status == NSS_STATUS_SUCCESS)
753 /* We found the entry. */
754 break;
755 else if (status == NSS_STATUS_RETURN)
756 /* We couldn't parse the entry */
757 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
758 else
759 return status;
760 }
761 }
762 return NSS_STATUS_SUCCESS;
763}
6259ec0d 764
cb62745a 765
6259ec0d
UD
766enum nss_status
767_nss_compat_getspnam_r (const char *name, struct spwd *pwd,
d71b808a 768 char *buffer, size_t buflen, int *errnop)
6259ec0d 769{
1e2e27fd 770 enum nss_status result;
cb62745a
UD
771 ent_t ent = { false, true, false, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0},
772 { NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
6259ec0d
UD
773
774 if (name[0] == '-' || name[0] == '+')
34816665 775 return NSS_STATUS_NOTFOUND;
6259ec0d 776
1e2e27fd
UD
777 __libc_lock_lock (lock);
778
26dee9c4 779 if (ni == NULL)
1e2e27fd
UD
780 init_nss_interface ();
781
782 __libc_lock_unlock (lock);
c131718c 783
b13b96ca 784 result = internal_setspent (&ent, 0, 0);
6259ec0d 785
c503d3dc
UD
786 if (result == NSS_STATUS_SUCCESS)
787 result = internal_getspnam_r (name, pwd, &ent, buffer, buflen, errnop);
6259ec0d
UD
788
789 internal_endspent (&ent);
cc3fa755 790
1e2e27fd 791 return result;
6259ec0d
UD
792}
793
cb62745a 794
6259ec0d
UD
795/* Support routines for remembering -@netgroup and -user entries.
796 The names are stored in a single string with `|' as separator. */
797static void
798blacklist_store_name (const char *name, ent_t *ent)
799{
800 int namelen = strlen (name);
801 char *tmp;
802
803 /* first call, setup cache */
804 if (ent->blacklist.size == 0)
805 {
806 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
807 ent->blacklist.data = malloc (ent->blacklist.size);
808 if (ent->blacklist.data == NULL)
809 return;
810 ent->blacklist.data[0] = '|';
811 ent->blacklist.data[1] = '\0';
812 ent->blacklist.current = 1;
813 }
814 else
815 {
816 if (in_blacklist (name, namelen, ent))
817 return; /* no duplicates */
818
819 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
820 {
821 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
822 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
823 if (tmp == NULL)
824 {
825 free (ent->blacklist.data);
826 ent->blacklist.size = 0;
827 return;
828 }
829 ent->blacklist.data = tmp;
830 }
831 }
832
833 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
834 *tmp++ = '|';
835 *tmp = '\0';
836 ent->blacklist.current += namelen + 1;
837
838 return;
839}
840
cb62745a 841
64d1e08e
AS
842/* Returns whether ent->blacklist contains name. */
843static bool
6259ec0d
UD
844in_blacklist (const char *name, int namelen, ent_t *ent)
845{
846 char buf[namelen + 3];
c131718c 847 char *cp;
6259ec0d
UD
848
849 if (ent->blacklist.data == NULL)
cb62745a 850 return false;
6259ec0d 851
c131718c
UD
852 buf[0] = '|';
853 cp = stpcpy (&buf[1], name);
1e2e27fd 854 *cp++ = '|';
c131718c 855 *cp = '\0';
6259ec0d
UD
856 return strstr (ent->blacklist.data, buf) != NULL;
857}