]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nss_compat/compat-grp.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nss / nss_compat / compat-grp.c
CommitLineData
04277e02 1/* Copyright (C) 1996-2019 Free Software Foundation, Inc.
6259ec0d 2 This file is part of the GNU C Library.
b85697f6 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
6259ec0d
UD
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
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
6259ec0d 18
1aa43890 19#include <ctype.h>
6259ec0d 20#include <errno.h>
3996f34b 21#include <fcntl.h>
6259ec0d 22#include <grp.h>
1aa43890
UD
23#include <nss.h>
24#include <nsswitch.h>
b4431a72 25#include <stdio_ext.h>
6259ec0d 26#include <string.h>
ec999b8e 27#include <libc-lock.h>
cc783763 28#include <kernel-features.h>
26dee9c4 29
fc9f33e3 30static service_user *ni;
1e2e27fd
UD
31static enum nss_status (*nss_setgrent) (int stayopen);
32static enum nss_status (*nss_getgrnam_r) (const char *name,
33 struct group * grp, char *buffer,
34 size_t buflen, int *errnop);
35static enum nss_status (*nss_getgrgid_r) (gid_t gid, struct group * grp,
36 char *buffer, size_t buflen,
37 int *errnop);
38static enum nss_status (*nss_getgrent_r) (struct group * grp, char *buffer,
39 size_t buflen, int *errnop);
40static enum nss_status (*nss_endgrent) (void);
6259ec0d 41
7e3be507
UD
42/* Get the declaration of the parser function. */
43#define ENTNAME grent
44#define STRUCTURE group
45#define EXTERN_PARSER
cc3fa755 46#include <nss/nss_files/files-parse.c>
7e3be507 47
26dee9c4 48/* Structure for remembering -group members ... */
6259ec0d
UD
49#define BLACKLIST_INITIAL_SIZE 512
50#define BLACKLIST_INCREMENT 256
51struct blacklist_t
1e2e27fd
UD
52{
53 char *data;
54 int current;
55 int size;
56};
6259ec0d
UD
57
58struct ent_t
1e2e27fd 59{
64d1e08e 60 bool files;
cb62745a 61 enum nss_status setent_status;
1e2e27fd
UD
62 FILE *stream;
63 struct blacklist_t blacklist;
26dee9c4 64};
6259ec0d
UD
65typedef struct ent_t ent_t;
66
64d1e08e 67static ent_t ext_ent = { true, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
6259ec0d
UD
68
69/* Protect global state against multiple changers. */
70__libc_lock_define_initialized (static, lock)
71
72/* Prototypes for local functions. */
73static void blacklist_store_name (const char *, ent_t *);
64d1e08e 74static bool in_blacklist (const char *, int, ent_t *);
2d7da676 75
1e2e27fd
UD
76/* Initialize the NSS interface/functions. The calling function must
77 hold the lock. */
78static void
79init_nss_interface (void)
2d7da676 80{
1e2e27fd 81 if (__nss_database_lookup ("group_compat", NULL, "nis", &ni) >= 0)
2d7da676 82 {
1e2e27fd
UD
83 nss_setgrent = __nss_lookup_function (ni, "setgrent");
84 nss_getgrnam_r = __nss_lookup_function (ni, "getgrnam_r");
85 nss_getgrgid_r = __nss_lookup_function (ni, "getgrgid_r");
86 nss_getgrent_r = __nss_lookup_function (ni, "getgrent_r");
87 nss_endgrent = __nss_lookup_function (ni, "endgrent");
2d7da676 88 }
2d7da676 89}
6259ec0d
UD
90
91static enum nss_status
cb62745a 92internal_setgrent (ent_t *ent, int stayopen, int needent)
6259ec0d
UD
93{
94 enum nss_status status = NSS_STATUS_SUCCESS;
95
64d1e08e 96 ent->files = true;
c131718c 97
6259ec0d 98 if (ent->blacklist.data != NULL)
bd355af0
UD
99 {
100 ent->blacklist.current = 1;
101 ent->blacklist.data[0] = '|';
102 ent->blacklist.data[1] = '\0';
103 }
104 else
105 ent->blacklist.current = 0;
c131718c 106
6259ec0d
UD
107 if (ent->stream == NULL)
108 {
cc783763 109 ent->stream = fopen ("/etc/group", "rme");
c131718c 110
6259ec0d
UD
111 if (ent->stream == NULL)
112 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
3996f34b 113 else
cef9b653
FW
114 /* We take care of locking ourself. */
115 __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
6259ec0d
UD
116 }
117 else
118 rewind (ent->stream);
119
cb62745a
UD
120 if (needent && status == NSS_STATUS_SUCCESS && nss_setgrent)
121 ent->setent_status = nss_setgrent (stayopen);
1e2e27fd 122
6259ec0d
UD
123 return status;
124}
125
126
127enum nss_status
51eecc4a 128_nss_compat_setgrent (int stayopen)
6259ec0d
UD
129{
130 enum nss_status result;
131
132 __libc_lock_lock (lock);
133
1e2e27fd
UD
134 if (ni == NULL)
135 init_nss_interface ();
136
cb62745a 137 result = internal_setgrent (&ext_ent, stayopen, 1);
6259ec0d
UD
138
139 __libc_lock_unlock (lock);
140
141 return result;
142}
143
144
145static enum nss_status
146internal_endgrent (ent_t *ent)
147{
148 if (ent->stream != NULL)
149 {
150 fclose (ent->stream);
151 ent->stream = NULL;
152 }
153
6259ec0d 154 if (ent->blacklist.data != NULL)
bd355af0
UD
155 {
156 ent->blacklist.current = 1;
157 ent->blacklist.data[0] = '|';
158 ent->blacklist.data[1] = '\0';
159 }
160 else
161 ent->blacklist.current = 0;
6259ec0d
UD
162
163 return NSS_STATUS_SUCCESS;
164}
165
166enum nss_status
167_nss_compat_endgrent (void)
168{
169 enum nss_status result;
170
171 __libc_lock_lock (lock);
172
b13b96ca
AS
173 if (nss_endgrent)
174 nss_endgrent ();
175
6259ec0d
UD
176 result = internal_endgrent (&ext_ent);
177
178 __libc_lock_unlock (lock);
179
180 return result;
181}
182
1e2e27fd 183/* get the next group from NSS (+ entry) */
6259ec0d 184static enum nss_status
1e2e27fd 185getgrent_next_nss (struct group *result, ent_t *ent, char *buffer,
d71b808a 186 size_t buflen, int *errnop)
6259ec0d 187{
1e2e27fd
UD
188 if (!nss_getgrent_r)
189 return NSS_STATUS_UNAVAIL;
c131718c 190
cb62745a
UD
191 /* If the setgrent call failed, say so. */
192 if (ent->setent_status != NSS_STATUS_SUCCESS)
193 return ent->setent_status;
194
26dee9c4
UD
195 do
196 {
1e2e27fd 197 enum nss_status status;
60c96635 198
1e2e27fd
UD
199 if ((status = nss_getgrent_r (result, buffer, buflen, errnop)) !=
200 NSS_STATUS_SUCCESS)
201 return status;
26dee9c4 202 }
1e2e27fd 203 while (in_blacklist (result->gr_name, strlen (result->gr_name), ent));
c131718c 204
26dee9c4
UD
205 return NSS_STATUS_SUCCESS;
206}
6259ec0d 207
26dee9c4
UD
208/* This function handle the +group entrys in /etc/group */
209static enum nss_status
1e2e27fd
UD
210getgrnam_plusgroup (const char *name, struct group *result, ent_t *ent,
211 char *buffer, size_t buflen, int *errnop)
26dee9c4 212{
1e2e27fd
UD
213 if (!nss_getgrnam_r)
214 return NSS_STATUS_UNAVAIL;
6591c335 215
016c70ea
UD
216 enum nss_status status = nss_getgrnam_r (name, result, buffer, buflen,
217 errnop);
218 if (status != NSS_STATUS_SUCCESS)
219 return status;
6591c335 220
1e2e27fd
UD
221 if (in_blacklist (result->gr_name, strlen (result->gr_name), ent))
222 return NSS_STATUS_NOTFOUND;
26dee9c4 223
1e2e27fd
UD
224 /* We found the entry. */
225 return NSS_STATUS_SUCCESS;
6259ec0d
UD
226}
227
6259ec0d
UD
228static enum nss_status
229getgrent_next_file (struct group *result, ent_t *ent,
d71b808a 230 char *buffer, size_t buflen, int *errnop)
6259ec0d 231{
7e3be507 232 struct parser_data *data = (void *) buffer;
6259ec0d
UD
233 while (1)
234 {
60c96635
UD
235 fpos_t pos;
236 int parse_res = 0;
6259ec0d
UD
237 char *p;
238
239 do
240 {
20f8e666 241 /* We need at least 3 characters for one line. */
a1ffb40e 242 if (__glibc_unlikely (buflen < 3))
20f8e666
UD
243 {
244 erange:
245 *errnop = ERANGE;
246 return NSS_STATUS_TRYAGAIN;
247 }
248
60c96635 249 fgetpos (ent->stream, &pos);
af69217f 250 buffer[buflen - 1] = '\xff';
1aa43890
UD
251 p = fgets_unlocked (buffer, buflen, ent->stream);
252 if (p == NULL && feof_unlocked (ent->stream))
34816665
UD
253 return NSS_STATUS_NOTFOUND;
254
20f8e666 255 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
60c96635 256 {
20f8e666 257 erange_reset:
af69217f 258 fsetpos (ent->stream, &pos);
20f8e666 259 goto erange;
60c96635 260 }
6259ec0d
UD
261
262 /* Terminate the line for any case. */
263 buffer[buflen - 1] = '\0';
264
265 /* Skip leading blanks. */
266 while (isspace (*p))
267 ++p;
268 }
1e2e27fd
UD
269 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
270 /* Parse the line. If it is invalid, loop to
271 get the next line of the file to parse. */
d71b808a
UD
272 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
273 errnop)));
60c96635 274
a1ffb40e 275 if (__glibc_unlikely (parse_res == -1))
20f8e666
UD
276 /* The parser ran out of space. */
277 goto erange_reset;
6259ec0d
UD
278
279 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
280 /* This is a real entry. */
281 break;
282
283 /* -group */
284 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
285 && result->gr_name[1] != '@')
286 {
287 blacklist_store_name (&result->gr_name[1], ent);
288 continue;
289 }
290
291 /* +group */
292 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
293 && result->gr_name[1] != '@')
294 {
bbca27a4
UD
295 size_t len = strlen (result->gr_name);
296 char buf[len];
1e2e27fd 297 enum nss_status status;
c131718c 298
51eecc4a 299 /* Store the group in the blacklist for the "+" at the end of
cc3fa755 300 /etc/group */
bbca27a4 301 memcpy (buf, &result->gr_name[1], len);
1e2e27fd
UD
302 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
303 buffer, buflen, errnop);
bbca27a4 304 blacklist_store_name (buf, ent);
1e2e27fd
UD
305 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
306 break;
20f8e666 307 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry*/
1e2e27fd
UD
308 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
309 continue;
310 else
311 {
312 if (status == NSS_STATUS_TRYAGAIN)
20f8e666
UD
313 /* The parser ran out of space. */
314 goto erange_reset;
315
1e2e27fd
UD
316 return status;
317 }
6259ec0d
UD
318 }
319
320 /* +:... */
321 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
322 {
64d1e08e 323 ent->files = false;
6259ec0d 324
1e2e27fd 325 return getgrent_next_nss (result, ent, buffer, buflen, errnop);
6259ec0d
UD
326 }
327 }
328
329 return NSS_STATUS_SUCCESS;
330}
331
332
6259ec0d 333enum nss_status
d71b808a
UD
334_nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
335 int *errnop)
6259ec0d 336{
1e2e27fd 337 enum nss_status result = NSS_STATUS_SUCCESS;
6259ec0d
UD
338
339 __libc_lock_lock (lock);
340
341 /* Be prepared that the setgrent function was not called before. */
1e2e27fd
UD
342 if (ni == NULL)
343 init_nss_interface ();
6259ec0d 344
1e2e27fd 345 if (ext_ent.stream == NULL)
cb62745a 346 result = internal_setgrent (&ext_ent, 1, 1);
6259ec0d 347
1e2e27fd
UD
348 if (result == NSS_STATUS_SUCCESS)
349 {
350 if (ext_ent.files)
351 result = getgrent_next_file (grp, &ext_ent, buffer, buflen, errnop);
352 else
353 result = getgrent_next_nss (grp, &ext_ent, buffer, buflen, errnop);
354 }
6259ec0d
UD
355 __libc_lock_unlock (lock);
356
1e2e27fd 357 return result;
6259ec0d
UD
358}
359
cc3fa755
UD
360/* Searches in /etc/group and the NIS/NIS+ map for a special group */
361static enum nss_status
362internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
d71b808a 363 char *buffer, size_t buflen, int *errnop)
cc3fa755
UD
364{
365 struct parser_data *data = (void *) buffer;
366 while (1)
367 {
368 fpos_t pos;
369 int parse_res = 0;
370 char *p;
371
372 do
373 {
20f8e666 374 /* We need at least 3 characters for one line. */
a1ffb40e 375 if (__glibc_unlikely (buflen < 3))
20f8e666
UD
376 {
377 erange:
378 *errnop = ERANGE;
379 return NSS_STATUS_TRYAGAIN;
380 }
381
cc3fa755 382 fgetpos (ent->stream, &pos);
af69217f 383 buffer[buflen - 1] = '\xff';
1aa43890
UD
384 p = fgets_unlocked (buffer, buflen, ent->stream);
385 if (p == NULL && feof_unlocked (ent->stream))
1e2e27fd 386 return NSS_STATUS_NOTFOUND;
34816665 387
20f8e666 388 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
cc3fa755 389 {
20f8e666 390 erange_reset:
af69217f 391 fsetpos (ent->stream, &pos);
20f8e666 392 goto erange;
cc3fa755
UD
393 }
394
395 /* Terminate the line for any case. */
396 buffer[buflen - 1] = '\0';
397
398 /* Skip leading blanks. */
399 while (isspace (*p))
400 ++p;
401 }
1e2e27fd
UD
402 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
403 /* Parse the line. If it is invalid, loop to
404 get the next line of the file to parse. */
d71b808a
UD
405 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
406 errnop)));
cc3fa755 407
a1ffb40e 408 if (__glibc_unlikely (parse_res == -1))
20f8e666
UD
409 /* The parser ran out of space. */
410 goto erange_reset;
cc3fa755
UD
411
412 /* This is a real entry. */
413 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
414 {
415 if (strcmp (result->gr_name, name) == 0)
416 return NSS_STATUS_SUCCESS;
417 else
418 continue;
419 }
420
421 /* -group */
d71b808a 422 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
cc3fa755
UD
423 {
424 if (strcmp (&result->gr_name[1], name) == 0)
34816665 425 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
426 else
427 continue;
428 }
429
430 /* +group */
d71b808a 431 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
cc3fa755
UD
432 {
433 if (strcmp (name, &result->gr_name[1]) == 0)
434 {
435 enum nss_status status;
436
1e2e27fd
UD
437 status = getgrnam_plusgroup (name, result, ent,
438 buffer, buflen, errnop);
cc3fa755
UD
439 if (status == NSS_STATUS_RETURN)
440 /* We couldn't parse the entry */
441 continue;
442 else
443 return status;
444 }
445 }
446 /* +:... */
447 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
448 {
449 enum nss_status status;
450
1e2e27fd
UD
451 status = getgrnam_plusgroup (name, result, ent,
452 buffer, buflen, errnop);
cc3fa755
UD
453 if (status == NSS_STATUS_RETURN)
454 /* We couldn't parse the entry */
455 continue;
456 else
457 return status;
458 }
459 }
460
461 return NSS_STATUS_SUCCESS;
462}
6259ec0d
UD
463
464enum nss_status
465_nss_compat_getgrnam_r (const char *name, struct group *grp,
d71b808a 466 char *buffer, size_t buflen, int *errnop)
6259ec0d 467{
64d1e08e 468 ent_t ent = { true, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
1e2e27fd 469 enum nss_status result;
6259ec0d
UD
470
471 if (name[0] == '-' || name[0] == '+')
34816665 472 return NSS_STATUS_NOTFOUND;
6259ec0d 473
26dee9c4
UD
474 __libc_lock_lock (lock);
475
1e2e27fd
UD
476 if (ni == NULL)
477 init_nss_interface ();
c131718c 478
26dee9c4 479 __libc_lock_unlock (lock);
6259ec0d 480
cb62745a 481 result = internal_setgrent (&ent, 0, 0);
6259ec0d 482
1e2e27fd
UD
483 if (result == NSS_STATUS_SUCCESS)
484 result = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
6259ec0d
UD
485
486 internal_endgrent (&ent);
cc3fa755 487
1e2e27fd 488 return result;
cc3fa755
UD
489}
490
491/* Searches in /etc/group and the NIS/NIS+ map for a special group id */
492static enum nss_status
493internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
d71b808a 494 char *buffer, size_t buflen, int *errnop)
cc3fa755
UD
495{
496 struct parser_data *data = (void *) buffer;
497 while (1)
498 {
499 fpos_t pos;
500 int parse_res = 0;
501 char *p;
502
503 do
504 {
20f8e666 505 /* We need at least 3 characters for one line. */
a1ffb40e 506 if (__glibc_unlikely (buflen < 3))
20f8e666
UD
507 {
508 erange:
509 *errnop = ERANGE;
510 return NSS_STATUS_TRYAGAIN;
511 }
512
cc3fa755 513 fgetpos (ent->stream, &pos);
af69217f 514 buffer[buflen - 1] = '\xff';
1aa43890
UD
515 p = fgets_unlocked (buffer, buflen, ent->stream);
516 if (p == NULL && feof_unlocked (ent->stream))
34816665
UD
517 return NSS_STATUS_NOTFOUND;
518
20f8e666 519 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
cc3fa755 520 {
20f8e666 521 erange_reset:
af69217f 522 fsetpos (ent->stream, &pos);
20f8e666 523 goto erange;
cc3fa755
UD
524 }
525
526 /* Terminate the line for any case. */
527 buffer[buflen - 1] = '\0';
528
529 /* Skip leading blanks. */
530 while (isspace (*p))
531 ++p;
532 }
1e2e27fd
UD
533 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
534 /* Parse the line. If it is invalid, loop to
535 get the next line of the file to parse. */
d71b808a
UD
536 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
537 errnop)));
cc3fa755 538
a1ffb40e 539 if (__glibc_unlikely (parse_res == -1))
20f8e666
UD
540 /* The parser ran out of space. */
541 goto erange_reset;
cc3fa755
UD
542
543 /* This is a real entry. */
544 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
545 {
546 if (result->gr_gid == gid)
547 return NSS_STATUS_SUCCESS;
548 else
549 continue;
550 }
551
552 /* -group */
d71b808a 553 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
cc3fa755 554 {
1e2e27fd
UD
555 blacklist_store_name (&result->gr_name[1], ent);
556 continue;
cc3fa755
UD
557 }
558
559 /* +group */
d71b808a 560 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
cc3fa755 561 {
33c6de58
UD
562 /* Yes, no +1, see the memcpy call below. */
563 size_t len = strlen (result->gr_name);
564 char buf[len];
cc3fa755
UD
565 enum nss_status status;
566
567 /* Store the group in the blacklist for the "+" at the end of
1e2e27fd 568 /etc/group */
33c6de58 569 memcpy (buf, &result->gr_name[1], len);
1e2e27fd
UD
570 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
571 buffer, buflen, errnop);
33c6de58 572 blacklist_store_name (buf, ent);
cc3fa755
UD
573 if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
574 break;
575 else
576 continue;
577 }
578 /* +:... */
579 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
580 {
016c70ea
UD
581 if (!nss_getgrgid_r)
582 return NSS_STATUS_UNAVAIL;
cc3fa755 583
016c70ea
UD
584 enum nss_status status = nss_getgrgid_r (gid, result, buffer, buflen,
585 errnop);
cc3fa755 586 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
34816665 587 return NSS_STATUS_NOTFOUND;
cc3fa755
UD
588 else
589 return status;
590 }
591 }
592
593 return NSS_STATUS_SUCCESS;
594}
6259ec0d
UD
595
596enum nss_status
597_nss_compat_getgrgid_r (gid_t gid, struct group *grp,
d71b808a 598 char *buffer, size_t buflen, int *errnop)
6259ec0d 599{
64d1e08e 600 ent_t ent = { true, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
1e2e27fd 601 enum nss_status result;
6259ec0d 602
26dee9c4 603 __libc_lock_lock (lock);
c131718c 604
1e2e27fd
UD
605 if (ni == NULL)
606 init_nss_interface ();
c131718c 607
26dee9c4
UD
608 __libc_lock_unlock (lock);
609
cb62745a 610 result = internal_setgrent (&ent, 0, 0);
6259ec0d 611
1e2e27fd
UD
612 if (result == NSS_STATUS_SUCCESS)
613 result = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
6259ec0d
UD
614
615 internal_endgrent (&ent);
cc3fa755 616
1e2e27fd 617 return result;
6259ec0d
UD
618}
619
620
621/* Support routines for remembering -@netgroup and -user entries.
622 The names are stored in a single string with `|' as separator. */
623static void
624blacklist_store_name (const char *name, ent_t *ent)
625{
626 int namelen = strlen (name);
627 char *tmp;
628
629 /* first call, setup cache */
630 if (ent->blacklist.size == 0)
631 {
632 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
633 ent->blacklist.data = malloc (ent->blacklist.size);
634 if (ent->blacklist.data == NULL)
635 return;
636 ent->blacklist.data[0] = '|';
637 ent->blacklist.data[1] = '\0';
638 ent->blacklist.current = 1;
639 }
640 else
641 {
642 if (in_blacklist (name, namelen, ent))
643 return; /* no duplicates */
644
645 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
646 {
647 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
648 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
649 if (tmp == NULL)
650 {
651 free (ent->blacklist.data);
652 ent->blacklist.size = 0;
653 return;
654 }
655 ent->blacklist.data = tmp;
656 }
657 }
658
659 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
660 *tmp++ = '|';
661 *tmp = '\0';
662 ent->blacklist.current += namelen + 1;
663
664 return;
665}
666
64d1e08e
AS
667/* Return whether ent->blacklist contains name. */
668static bool
6259ec0d
UD
669in_blacklist (const char *name, int namelen, ent_t *ent)
670{
671 char buf[namelen + 3];
c131718c 672 char *cp;
6259ec0d
UD
673
674 if (ent->blacklist.data == NULL)
64d1e08e 675 return false;
6259ec0d 676
c131718c
UD
677 buf[0] = '|';
678 cp = stpcpy (&buf[1], name);
1e2e27fd 679 *cp++ = '|';
c131718c 680 *cp = '\0';
6259ec0d
UD
681 return strstr (ent->blacklist.data, buf) != NULL;
682}