1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * This software was developed for the FreeBSD Project by 8 * Jacques A. Vidrine, Safeport Network Services, and Network 9 * Associates Laboratories, the Security Research Division of Network 10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 11 * ("CBOSS"), as part of the DARPA CHATS research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "namespace.h" 39 #include <sys/param.h> 40 #ifdef YP 41 #include <rpc/rpc.h> 42 #include <rpcsvc/yp_prot.h> 43 #include <rpcsvc/ypclnt.h> 44 #endif 45 #include <assert.h> 46 #include <ctype.h> 47 #include <errno.h> 48 #ifdef HESIOD 49 #include <hesiod.h> 50 #endif 51 #include <grp.h> 52 #include <nsswitch.h> 53 #include <pthread.h> 54 #include <pthread_np.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <syslog.h> 59 #include <unistd.h> 60 #include "un-namespace.h" 61 #include "libc_private.h" 62 #include "nss_tls.h" 63 #ifdef NS_CACHING 64 #include "nscache.h" 65 #endif 66 67 enum constants { 68 GRP_STORAGE_INITIAL = 1 << 10, /* 1 KByte */ 69 GRP_STORAGE_MAX = 1 << 20, /* 1 MByte */ 70 SETGRENT = 1, 71 ENDGRENT = 2, 72 HESIOD_NAME_MAX = 256, 73 }; 74 75 static const ns_src defaultsrc[] = { 76 { NSSRC_COMPAT, NS_SUCCESS }, 77 { NULL, 0 } 78 }; 79 80 int __getgroupmembership(const char *, gid_t, gid_t *, int, int *); 81 int __gr_match_entry(const char *, size_t, enum nss_lookup_type, 82 const char *, gid_t); 83 int __gr_parse_entry(char *, size_t, struct group *, char *, size_t, 84 int *); 85 86 static int is_comment_line(const char *, size_t); 87 88 union key { 89 const char *name; 90 gid_t gid; 91 }; 92 static struct group *getgr(int (*)(union key, struct group *, char *, size_t, 93 struct group **), union key); 94 static int wrap_getgrnam_r(union key, struct group *, char *, size_t, 95 struct group **); 96 static int wrap_getgrgid_r(union key, struct group *, char *, size_t, 97 struct group **); 98 static int wrap_getgrent_r(union key, struct group *, char *, size_t, 99 struct group **); 100 101 struct files_state { 102 FILE *fp; 103 int stayopen; 104 }; 105 static void files_endstate(void *); 106 NSS_TLS_HANDLING(files); 107 static int files_setgrent(void *, void *, va_list); 108 static int files_group(void *, void *, va_list); 109 110 111 #ifdef HESIOD 112 struct dns_state { 113 long counter; 114 }; 115 static void dns_endstate(void *); 116 NSS_TLS_HANDLING(dns); 117 static int dns_setgrent(void *, void *, va_list); 118 static int dns_group(void *, void *, va_list); 119 #endif 120 121 122 #ifdef YP 123 struct nis_state { 124 char domain[MAXHOSTNAMELEN]; 125 int done; 126 char *key; 127 int keylen; 128 }; 129 static void nis_endstate(void *); 130 NSS_TLS_HANDLING(nis); 131 static int nis_setgrent(void *, void *, va_list); 132 static int nis_group(void *, void *, va_list); 133 #endif 134 135 struct compat_state { 136 FILE *fp; 137 int stayopen; 138 char *name; 139 enum _compat { 140 COMPAT_MODE_OFF = 0, 141 COMPAT_MODE_ALL, 142 COMPAT_MODE_NAME 143 } compat; 144 }; 145 static void compat_endstate(void *); 146 NSS_TLS_HANDLING(compat); 147 static int compat_setgrent(void *, void *, va_list); 148 static int compat_group(void *, void *, va_list); 149 150 static int gr_addgid(gid_t, gid_t *, int, int *); 151 static int getgroupmembership_fallback(void *, void *, va_list); 152 153 #ifdef NS_CACHING 154 static int grp_id_func(char *, size_t *, va_list, void *); 155 static int grp_marshal_func(char *, size_t *, void *, va_list, void *); 156 static int grp_unmarshal_func(char *, size_t, void *, va_list, void *); 157 158 static int 159 grp_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata) 160 { 161 char *name; 162 gid_t gid; 163 164 size_t desired_size, size; 165 int res = NS_UNAVAIL; 166 enum nss_lookup_type lookup_type; 167 168 169 lookup_type = (enum nss_lookup_type)(uintptr_t)cache_mdata; 170 switch (lookup_type) { 171 case nss_lt_name: 172 name = va_arg(ap, char *); 173 size = strlen(name); 174 desired_size = sizeof(enum nss_lookup_type) + size + 1; 175 if (desired_size > *buffer_size) { 176 res = NS_RETURN; 177 goto fin; 178 } 179 180 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 181 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1); 182 183 res = NS_SUCCESS; 184 break; 185 case nss_lt_id: 186 gid = va_arg(ap, gid_t); 187 desired_size = sizeof(enum nss_lookup_type) + sizeof(gid_t); 188 if (desired_size > *buffer_size) { 189 res = NS_RETURN; 190 goto fin; 191 } 192 193 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type)); 194 memcpy(buffer + sizeof(enum nss_lookup_type), &gid, 195 sizeof(gid_t)); 196 197 res = NS_SUCCESS; 198 break; 199 default: 200 /* should be unreachable */ 201 return (NS_UNAVAIL); 202 } 203 204 fin: 205 *buffer_size = desired_size; 206 return (res); 207 } 208 209 static int 210 grp_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap, 211 void *cache_mdata) 212 { 213 char *name; 214 gid_t gid; 215 struct group *grp; 216 char *orig_buf; 217 size_t orig_buf_size; 218 219 struct group new_grp; 220 size_t desired_size, size, mem_size; 221 char *p, **mem; 222 223 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) { 224 case nss_lt_name: 225 name = va_arg(ap, char *); 226 break; 227 case nss_lt_id: 228 gid = va_arg(ap, gid_t); 229 break; 230 case nss_lt_all: 231 break; 232 default: 233 /* should be unreachable */ 234 return (NS_UNAVAIL); 235 } 236 237 grp = va_arg(ap, struct group *); 238 orig_buf = va_arg(ap, char *); 239 orig_buf_size = va_arg(ap, size_t); 240 241 desired_size = _ALIGNBYTES + sizeof(struct group) + sizeof(char *); 242 243 if (grp->gr_name != NULL) 244 desired_size += strlen(grp->gr_name) + 1; 245 if (grp->gr_passwd != NULL) 246 desired_size += strlen(grp->gr_passwd) + 1; 247 248 if (grp->gr_mem != NULL) { 249 mem_size = 0; 250 for (mem = grp->gr_mem; *mem; ++mem) { 251 desired_size += strlen(*mem) + 1; 252 ++mem_size; 253 } 254 255 desired_size += _ALIGNBYTES + (mem_size + 1) * sizeof(char *); 256 } 257 258 if (desired_size > *buffer_size) { 259 /* this assignment is here for future use */ 260 *buffer_size = desired_size; 261 return (NS_RETURN); 262 } 263 264 memcpy(&new_grp, grp, sizeof(struct group)); 265 memset(buffer, 0, desired_size); 266 267 *buffer_size = desired_size; 268 p = buffer + sizeof(struct group) + sizeof(char *); 269 memcpy(buffer + sizeof(struct group), &p, sizeof(char *)); 270 p = (char *)_ALIGN(p); 271 272 if (new_grp.gr_name != NULL) { 273 size = strlen(new_grp.gr_name); 274 memcpy(p, new_grp.gr_name, size); 275 new_grp.gr_name = p; 276 p += size + 1; 277 } 278 279 if (new_grp.gr_passwd != NULL) { 280 size = strlen(new_grp.gr_passwd); 281 memcpy(p, new_grp.gr_passwd, size); 282 new_grp.gr_passwd = p; 283 p += size + 1; 284 } 285 286 if (new_grp.gr_mem != NULL) { 287 p = (char *)_ALIGN(p); 288 memcpy(p, new_grp.gr_mem, sizeof(char *) * mem_size); 289 new_grp.gr_mem = (char **)p; 290 p += sizeof(char *) * (mem_size + 1); 291 292 for (mem = new_grp.gr_mem; *mem; ++mem) { 293 size = strlen(*mem); 294 memcpy(p, *mem, size); 295 *mem = p; 296 p += size + 1; 297 } 298 } 299 300 memcpy(buffer, &new_grp, sizeof(struct group)); 301 return (NS_SUCCESS); 302 } 303 304 static int 305 grp_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap, 306 void *cache_mdata) 307 { 308 char *name; 309 gid_t gid; 310 struct group *grp; 311 char *orig_buf; 312 size_t orig_buf_size; 313 int *ret_errno; 314 315 char *p; 316 char **mem; 317 318 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) { 319 case nss_lt_name: 320 name = va_arg(ap, char *); 321 break; 322 case nss_lt_id: 323 gid = va_arg(ap, gid_t); 324 break; 325 case nss_lt_all: 326 break; 327 default: 328 /* should be unreachable */ 329 return (NS_UNAVAIL); 330 } 331 332 grp = va_arg(ap, struct group *); 333 orig_buf = va_arg(ap, char *); 334 orig_buf_size = va_arg(ap, size_t); 335 ret_errno = va_arg(ap, int *); 336 337 if (orig_buf_size + sizeof(struct group) + sizeof(char *) < buffer_size) 338 { 339 *ret_errno = ERANGE; 340 return (NS_RETURN); 341 } else if (buffer_size < sizeof(struct group) + sizeof(char *)) { 342 /* 343 * nscd(8) sometimes returns buffer_size=1 for nonexistent 344 * entries. 345 */ 346 *ret_errno = 0; 347 return (NS_NOTFOUND); 348 } 349 350 memcpy(grp, buffer, sizeof(struct group)); 351 memcpy(&p, buffer + sizeof(struct group), sizeof(char *)); 352 353 if (orig_buf_size + sizeof(struct group) + sizeof(char *) + 354 _ALIGN(p) - (size_t)p < buffer_size) { 355 *ret_errno = ERANGE; 356 return (NS_RETURN); 357 } 358 359 orig_buf = (char *)_ALIGN(orig_buf); 360 memcpy(orig_buf, buffer + sizeof(struct group) + sizeof(char *) + 361 _ALIGN(p) - (size_t)p, 362 buffer_size - sizeof(struct group) - sizeof(char *) - 363 _ALIGN(p) + (size_t)p); 364 p = (char *)_ALIGN(p); 365 366 NS_APPLY_OFFSET(grp->gr_name, orig_buf, p, char *); 367 NS_APPLY_OFFSET(grp->gr_passwd, orig_buf, p, char *); 368 if (grp->gr_mem != NULL) { 369 NS_APPLY_OFFSET(grp->gr_mem, orig_buf, p, char **); 370 371 for (mem = grp->gr_mem; *mem; ++mem) 372 NS_APPLY_OFFSET(*mem, orig_buf, p, char *); 373 } 374 375 if (retval != NULL) 376 *((struct group **)retval) = grp; 377 378 return (NS_SUCCESS); 379 } 380 381 NSS_MP_CACHE_HANDLING(group); 382 #endif /* NS_CACHING */ 383 384 #ifdef NS_CACHING 385 static const nss_cache_info setgrent_cache_info = NS_MP_CACHE_INFO_INITIALIZER( 386 group, (void *)nss_lt_all, 387 NULL, NULL); 388 #endif 389 390 static const ns_dtab setgrent_dtab[] = { 391 { NSSRC_FILES, files_setgrent, (void *)SETGRENT }, 392 #ifdef HESIOD 393 { NSSRC_DNS, dns_setgrent, (void *)SETGRENT }, 394 #endif 395 #ifdef YP 396 { NSSRC_NIS, nis_setgrent, (void *)SETGRENT }, 397 #endif 398 { NSSRC_COMPAT, compat_setgrent, (void *)SETGRENT }, 399 #ifdef NS_CACHING 400 NS_CACHE_CB(&setgrent_cache_info) 401 #endif 402 { NULL, NULL, NULL } 403 }; 404 405 #ifdef NS_CACHING 406 static const nss_cache_info endgrent_cache_info = NS_MP_CACHE_INFO_INITIALIZER( 407 group, (void *)nss_lt_all, 408 NULL, NULL); 409 #endif 410 411 static const ns_dtab endgrent_dtab[] = { 412 { NSSRC_FILES, files_setgrent, (void *)ENDGRENT }, 413 #ifdef HESIOD 414 { NSSRC_DNS, dns_setgrent, (void *)ENDGRENT }, 415 #endif 416 #ifdef YP 417 { NSSRC_NIS, nis_setgrent, (void *)ENDGRENT }, 418 #endif 419 { NSSRC_COMPAT, compat_setgrent, (void *)ENDGRENT }, 420 #ifdef NS_CACHING 421 NS_CACHE_CB(&endgrent_cache_info) 422 #endif 423 { NULL, NULL, NULL } 424 }; 425 426 #ifdef NS_CACHING 427 static const nss_cache_info getgrent_r_cache_info = NS_MP_CACHE_INFO_INITIALIZER( 428 group, (void *)nss_lt_all, 429 grp_marshal_func, grp_unmarshal_func); 430 #endif 431 432 static const ns_dtab getgrent_r_dtab[] = { 433 { NSSRC_FILES, files_group, (void *)nss_lt_all }, 434 #ifdef HESIOD 435 { NSSRC_DNS, dns_group, (void *)nss_lt_all }, 436 #endif 437 #ifdef YP 438 { NSSRC_NIS, nis_group, (void *)nss_lt_all }, 439 #endif 440 { NSSRC_COMPAT, compat_group, (void *)nss_lt_all }, 441 #ifdef NS_CACHING 442 NS_CACHE_CB(&getgrent_r_cache_info) 443 #endif 444 { NULL, NULL, NULL } 445 }; 446 447 static int 448 gr_addgid(gid_t gid, gid_t *groups, int maxgrp, int *grpcnt) 449 { 450 int ret, dupc; 451 452 for (dupc = 1; dupc < MIN(maxgrp, *grpcnt); dupc++) { 453 if (groups[dupc] == gid) 454 return 1; 455 } 456 457 ret = 1; 458 if (*grpcnt < maxgrp) 459 groups[*grpcnt] = gid; 460 else 461 ret = 0; 462 463 (*grpcnt)++; 464 465 return ret; 466 } 467 468 static int 469 getgroupmembership_fallback(void *retval, void *mdata, va_list ap) 470 { 471 const ns_src src[] = { 472 { mdata, NS_SUCCESS }, 473 { NULL, 0} 474 }; 475 struct group grp; 476 struct group *grp_p; 477 char *buf; 478 size_t bufsize; 479 const char *uname; 480 gid_t *groups; 481 gid_t agroup; 482 int maxgrp, *grpcnt; 483 int i, rv, ret_errno; 484 485 /* 486 * As this is a fallback method, only provided src 487 * list will be respected during methods search. 488 */ 489 assert(src[0].name != NULL); 490 491 uname = va_arg(ap, const char *); 492 agroup = va_arg(ap, gid_t); 493 groups = va_arg(ap, gid_t *); 494 maxgrp = va_arg(ap, int); 495 grpcnt = va_arg(ap, int *); 496 497 rv = NS_UNAVAIL; 498 499 buf = malloc(GRP_STORAGE_INITIAL); 500 if (buf == NULL) 501 goto out; 502 503 bufsize = GRP_STORAGE_INITIAL; 504 505 gr_addgid(agroup, groups, maxgrp, grpcnt); 506 507 _nsdispatch(NULL, setgrent_dtab, NSDB_GROUP, "setgrent", src, 0); 508 for (;;) { 509 do { 510 ret_errno = 0; 511 grp_p = NULL; 512 rv = _nsdispatch(&grp_p, getgrent_r_dtab, NSDB_GROUP, 513 "getgrent_r", src, &grp, buf, bufsize, &ret_errno); 514 515 if (grp_p == NULL && ret_errno == ERANGE) { 516 free(buf); 517 if ((bufsize << 1) > GRP_STORAGE_MAX) { 518 buf = NULL; 519 errno = ERANGE; 520 goto out; 521 } 522 523 bufsize <<= 1; 524 buf = malloc(bufsize); 525 if (buf == NULL) { 526 goto out; 527 } 528 } 529 } while (grp_p == NULL && ret_errno == ERANGE); 530 531 if (ret_errno != 0) { 532 errno = ret_errno; 533 goto out; 534 } 535 536 if (grp_p == NULL) 537 break; 538 539 for (i = 0; grp.gr_mem[i]; i++) { 540 if (strcmp(grp.gr_mem[i], uname) == 0) 541 gr_addgid(grp.gr_gid, groups, maxgrp, grpcnt); 542 } 543 } 544 545 _nsdispatch(NULL, endgrent_dtab, NSDB_GROUP, "endgrent", src); 546 out: 547 free(buf); 548 return (rv); 549 } 550 551 void 552 setgrent(void) 553 { 554 (void)_nsdispatch(NULL, setgrent_dtab, NSDB_GROUP, "setgrent", defaultsrc, 0); 555 } 556 557 558 int 559 setgroupent(int stayopen) 560 { 561 (void)_nsdispatch(NULL, setgrent_dtab, NSDB_GROUP, "setgrent", defaultsrc, 562 stayopen); 563 return (1); 564 } 565 566 567 void 568 endgrent(void) 569 { 570 (void)_nsdispatch(NULL, endgrent_dtab, NSDB_GROUP, "endgrent", defaultsrc); 571 } 572 573 574 int 575 getgrent_r(struct group *grp, char *buffer, size_t bufsize, 576 struct group **result) 577 { 578 int rv, ret_errno; 579 580 ret_errno = 0; 581 *result = NULL; 582 rv = _nsdispatch(result, getgrent_r_dtab, NSDB_GROUP, "getgrent_r", defaultsrc, 583 grp, buffer, bufsize, &ret_errno); 584 if (rv == NS_SUCCESS) 585 return (0); 586 else 587 return (ret_errno); 588 } 589 590 591 int 592 getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize, 593 struct group **result) 594 { 595 #ifdef NS_CACHING 596 static const nss_cache_info cache_info = 597 NS_COMMON_CACHE_INFO_INITIALIZER( 598 group, (void *)nss_lt_name, 599 grp_id_func, grp_marshal_func, grp_unmarshal_func); 600 #endif 601 602 static const ns_dtab dtab[] = { 603 { NSSRC_FILES, files_group, (void *)nss_lt_name }, 604 #ifdef HESIOD 605 { NSSRC_DNS, dns_group, (void *)nss_lt_name }, 606 #endif 607 #ifdef YP 608 { NSSRC_NIS, nis_group, (void *)nss_lt_name }, 609 #endif 610 { NSSRC_COMPAT, compat_group, (void *)nss_lt_name }, 611 #ifdef NS_CACHING 612 NS_CACHE_CB(&cache_info) 613 #endif 614 { NULL, NULL, NULL } 615 }; 616 int rv, ret_errno; 617 618 ret_errno = 0; 619 *result = NULL; 620 rv = _nsdispatch(result, dtab, NSDB_GROUP, "getgrnam_r", defaultsrc, 621 name, grp, buffer, bufsize, &ret_errno); 622 if (rv == NS_SUCCESS) 623 return (0); 624 else 625 return (ret_errno); 626 } 627 628 629 int 630 getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, 631 struct group **result) 632 { 633 #ifdef NS_CACHING 634 static const nss_cache_info cache_info = 635 NS_COMMON_CACHE_INFO_INITIALIZER( 636 group, (void *)nss_lt_id, 637 grp_id_func, grp_marshal_func, grp_unmarshal_func); 638 #endif 639 640 static const ns_dtab dtab[] = { 641 { NSSRC_FILES, files_group, (void *)nss_lt_id }, 642 #ifdef HESIOD 643 { NSSRC_DNS, dns_group, (void *)nss_lt_id }, 644 #endif 645 #ifdef YP 646 { NSSRC_NIS, nis_group, (void *)nss_lt_id }, 647 #endif 648 { NSSRC_COMPAT, compat_group, (void *)nss_lt_id }, 649 #ifdef NS_CACHING 650 NS_CACHE_CB(&cache_info) 651 #endif 652 { NULL, NULL, NULL } 653 }; 654 int rv, ret_errno; 655 656 ret_errno = 0; 657 *result = NULL; 658 rv = _nsdispatch(result, dtab, NSDB_GROUP, "getgrgid_r", defaultsrc, 659 gid, grp, buffer, bufsize, &ret_errno); 660 if (rv == NS_SUCCESS) 661 return (0); 662 else 663 return (ret_errno); 664 } 665 666 667 668 int 669 __getgroupmembership(const char *uname, gid_t agroup, gid_t *groups, 670 int maxgrp, int *grpcnt) 671 { 672 static const ns_dtab dtab[] = { 673 NS_FALLBACK_CB(getgroupmembership_fallback) 674 { NULL, NULL, NULL } 675 }; 676 677 assert(uname != NULL); 678 /* groups may be NULL if just sizing when invoked with maxgrp = 0 */ 679 assert(grpcnt != NULL); 680 681 *grpcnt = 0; 682 (void)_nsdispatch(NULL, dtab, NSDB_GROUP, "getgroupmembership", 683 defaultsrc, uname, agroup, groups, maxgrp, grpcnt); 684 685 /* too many groups found? */ 686 return (*grpcnt > maxgrp ? -1 : 0); 687 } 688 689 690 static struct group grp; 691 static char *grp_storage; 692 static size_t grp_storage_size; 693 694 static struct group * 695 getgr(int (*fn)(union key, struct group *, char *, size_t, struct group **), 696 union key key) 697 { 698 int rv; 699 struct group *res; 700 701 if (grp_storage == NULL) { 702 grp_storage = malloc(GRP_STORAGE_INITIAL); 703 if (grp_storage == NULL) 704 return (NULL); 705 grp_storage_size = GRP_STORAGE_INITIAL; 706 } 707 do { 708 rv = fn(key, &grp, grp_storage, grp_storage_size, &res); 709 if (res == NULL && rv == ERANGE) { 710 free(grp_storage); 711 if ((grp_storage_size << 1) > GRP_STORAGE_MAX) { 712 grp_storage = NULL; 713 errno = ERANGE; 714 return (NULL); 715 } 716 grp_storage_size <<= 1; 717 grp_storage = malloc(grp_storage_size); 718 if (grp_storage == NULL) 719 return (NULL); 720 } 721 } while (res == NULL && rv == ERANGE); 722 if (rv != 0) 723 errno = rv; 724 return (res); 725 } 726 727 728 static int 729 wrap_getgrnam_r(union key key, struct group *grp, char *buffer, size_t bufsize, 730 struct group **res) 731 { 732 return (getgrnam_r(key.name, grp, buffer, bufsize, res)); 733 } 734 735 736 static int 737 wrap_getgrgid_r(union key key, struct group *grp, char *buffer, size_t bufsize, 738 struct group **res) 739 { 740 return (getgrgid_r(key.gid, grp, buffer, bufsize, res)); 741 } 742 743 744 static int 745 wrap_getgrent_r(union key key __unused, struct group *grp, char *buffer, 746 size_t bufsize, struct group **res) 747 { 748 return (getgrent_r(grp, buffer, bufsize, res)); 749 } 750 751 752 struct group * 753 getgrnam(const char *name) 754 { 755 union key key; 756 757 key.name = name; 758 return (getgr(wrap_getgrnam_r, key)); 759 } 760 761 762 struct group * 763 getgrgid(gid_t gid) 764 { 765 union key key; 766 767 key.gid = gid; 768 return (getgr(wrap_getgrgid_r, key)); 769 } 770 771 772 struct group * 773 getgrent(void) 774 { 775 union key key; 776 777 key.gid = 0; /* not used */ 778 return (getgr(wrap_getgrent_r, key)); 779 } 780 781 782 static int 783 is_comment_line(const char *s, size_t n) 784 { 785 const char *eom; 786 787 eom = &s[n]; 788 789 for (; s < eom; s++) 790 if (*s == '#' || !isspace((unsigned char)*s)) 791 break; 792 return (*s == '#' || s == eom); 793 } 794 795 796 /* 797 * files backend 798 */ 799 static void 800 files_endstate(void *p) 801 { 802 803 if (p == NULL) 804 return; 805 if (((struct files_state *)p)->fp != NULL) 806 fclose(((struct files_state *)p)->fp); 807 free(p); 808 } 809 810 811 static int 812 files_setgrent(void *retval, void *mdata, va_list ap) 813 { 814 struct files_state *st; 815 int rv, stayopen; 816 817 rv = files_getstate(&st); 818 if (rv != 0) 819 return (NS_UNAVAIL); 820 switch ((enum constants)(uintptr_t)mdata) { 821 case SETGRENT: 822 stayopen = va_arg(ap, int); 823 if (st->fp != NULL) 824 rewind(st->fp); 825 else if (stayopen) 826 st->fp = fopen(_PATH_GROUP, "re"); 827 break; 828 case ENDGRENT: 829 if (st->fp != NULL) { 830 fclose(st->fp); 831 st->fp = NULL; 832 } 833 break; 834 default: 835 break; 836 } 837 return (NS_UNAVAIL); 838 } 839 840 841 static int 842 files_group(void *retval, void *mdata, va_list ap) 843 { 844 struct files_state *st; 845 enum nss_lookup_type how; 846 const char *name, *line; 847 struct group *grp; 848 gid_t gid; 849 char *buffer; 850 size_t bufsize, linesize; 851 off_t pos; 852 int fresh, rv, stayopen, *errnop; 853 854 fresh = 0; 855 name = NULL; 856 gid = (gid_t)-1; 857 how = (enum nss_lookup_type)(uintptr_t)mdata; 858 switch (how) { 859 case nss_lt_name: 860 name = va_arg(ap, const char *); 861 break; 862 case nss_lt_id: 863 gid = va_arg(ap, gid_t); 864 break; 865 case nss_lt_all: 866 break; 867 default: 868 return (NS_NOTFOUND); 869 } 870 grp = va_arg(ap, struct group *); 871 buffer = va_arg(ap, char *); 872 bufsize = va_arg(ap, size_t); 873 errnop = va_arg(ap, int *); 874 *errnop = files_getstate(&st); 875 if (*errnop != 0) 876 return (NS_UNAVAIL); 877 if (st->fp == NULL) { 878 st->fp = fopen(_PATH_GROUP, "re"); 879 if (st->fp == NULL) { 880 *errnop = errno; 881 return (NS_UNAVAIL); 882 } 883 fresh = 1; 884 } 885 if (how == nss_lt_all) 886 stayopen = 1; 887 else { 888 if (!fresh) 889 rewind(st->fp); 890 stayopen = st->stayopen; 891 } 892 rv = NS_NOTFOUND; 893 if (stayopen) 894 pos = ftello(st->fp); 895 while ((line = fgetln(st->fp, &linesize)) != NULL) { 896 if (line[linesize-1] == '\n') 897 linesize--; 898 rv = __gr_match_entry(line, linesize, how, name, gid); 899 if (rv != NS_SUCCESS) 900 continue; 901 /* We need room at least for the line, a string NUL 902 * terminator, alignment padding, and one (char *) 903 * pointer for the member list terminator. 904 */ 905 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { 906 *errnop = ERANGE; 907 rv = NS_RETURN; 908 break; 909 } 910 memcpy(buffer, line, linesize); 911 buffer[linesize] = '\0'; 912 rv = __gr_parse_entry(buffer, linesize, grp, 913 &buffer[linesize + 1], bufsize - linesize - 1, errnop); 914 if (rv & NS_TERMINATE) 915 break; 916 if (stayopen) 917 pos = ftello(st->fp); 918 } 919 if (st->fp != NULL && !stayopen) { 920 fclose(st->fp); 921 st->fp = NULL; 922 } 923 if (rv == NS_SUCCESS && retval != NULL) 924 *(struct group **)retval = grp; 925 else if (rv == NS_RETURN && *errnop == ERANGE && st->fp != NULL) 926 fseeko(st->fp, pos, SEEK_SET); 927 return (rv); 928 } 929 930 931 #ifdef HESIOD 932 /* 933 * dns backend 934 */ 935 static void 936 dns_endstate(void *p) 937 { 938 939 free(p); 940 } 941 942 943 static int 944 dns_setgrent(void *retval, void *cb_data, va_list ap) 945 { 946 struct dns_state *st; 947 int rv; 948 949 rv = dns_getstate(&st); 950 if (rv != 0) 951 return (NS_UNAVAIL); 952 st->counter = 0; 953 return (NS_UNAVAIL); 954 } 955 956 957 static int 958 dns_group(void *retval, void *mdata, va_list ap) 959 { 960 char buf[HESIOD_NAME_MAX]; 961 struct dns_state *st; 962 struct group *grp; 963 const char *name, *label; 964 void *ctx; 965 char *buffer, **hes; 966 size_t bufsize, adjsize, linesize; 967 gid_t gid; 968 enum nss_lookup_type how; 969 int rv, *errnop; 970 971 ctx = NULL; 972 hes = NULL; 973 name = NULL; 974 gid = (gid_t)-1; 975 how = (enum nss_lookup_type)mdata; 976 switch (how) { 977 case nss_lt_name: 978 name = va_arg(ap, const char *); 979 break; 980 case nss_lt_id: 981 gid = va_arg(ap, gid_t); 982 break; 983 case nss_lt_all: 984 break; 985 } 986 grp = va_arg(ap, struct group *); 987 buffer = va_arg(ap, char *); 988 bufsize = va_arg(ap, size_t); 989 errnop = va_arg(ap, int *); 990 *errnop = dns_getstate(&st); 991 if (*errnop != 0) 992 return (NS_UNAVAIL); 993 if (hesiod_init(&ctx) != 0) { 994 *errnop = errno; 995 rv = NS_UNAVAIL; 996 goto fin; 997 } 998 do { 999 rv = NS_NOTFOUND; 1000 switch (how) { 1001 case nss_lt_name: 1002 label = name; 1003 break; 1004 case nss_lt_id: 1005 if (snprintf(buf, sizeof(buf), "%lu", 1006 (unsigned long)gid) >= sizeof(buf)) 1007 goto fin; 1008 label = buf; 1009 break; 1010 case nss_lt_all: 1011 if (st->counter < 0) 1012 goto fin; 1013 if (snprintf(buf, sizeof(buf), "group-%ld", 1014 st->counter++) >= sizeof(buf)) 1015 goto fin; 1016 label = buf; 1017 break; 1018 } 1019 hes = hesiod_resolve(ctx, label, 1020 how == nss_lt_id ? "gid" : "group"); 1021 if ((how == nss_lt_id && hes == NULL && 1022 (hes = hesiod_resolve(ctx, buf, "group")) == NULL) || 1023 hes == NULL) { 1024 if (how == nss_lt_all) 1025 st->counter = -1; 1026 if (errno != ENOENT) 1027 *errnop = errno; 1028 goto fin; 1029 } 1030 rv = __gr_match_entry(hes[0], strlen(hes[0]), how, name, gid); 1031 if (rv != NS_SUCCESS) { 1032 hesiod_free_list(ctx, hes); 1033 hes = NULL; 1034 continue; 1035 } 1036 /* We need room at least for the line, a string NUL 1037 * terminator, alignment padding, and one (char *) 1038 * pointer for the member list terminator. 1039 */ 1040 adjsize = bufsize - _ALIGNBYTES - sizeof(char *); 1041 linesize = strlcpy(buffer, hes[0], adjsize); 1042 if (linesize >= adjsize) { 1043 *errnop = ERANGE; 1044 rv = NS_RETURN; 1045 goto fin; 1046 } 1047 hesiod_free_list(ctx, hes); 1048 hes = NULL; 1049 rv = __gr_parse_entry(buffer, linesize, grp, 1050 &buffer[linesize + 1], bufsize - linesize - 1, errnop); 1051 } while (how == nss_lt_all && !(rv & NS_TERMINATE)); 1052 fin: 1053 if (hes != NULL) 1054 hesiod_free_list(ctx, hes); 1055 if (ctx != NULL) 1056 hesiod_end(ctx); 1057 if (rv == NS_SUCCESS && retval != NULL) 1058 *(struct group **)retval = grp; 1059 return (rv); 1060 } 1061 #endif /* HESIOD */ 1062 1063 1064 #ifdef YP 1065 /* 1066 * nis backend 1067 */ 1068 static void 1069 nis_endstate(void *p) 1070 { 1071 1072 if (p == NULL) 1073 return; 1074 free(((struct nis_state *)p)->key); 1075 free(p); 1076 } 1077 1078 1079 static int 1080 nis_setgrent(void *retval, void *cb_data, va_list ap) 1081 { 1082 struct nis_state *st; 1083 int rv; 1084 1085 rv = nis_getstate(&st); 1086 if (rv != 0) 1087 return (NS_UNAVAIL); 1088 st->done = 0; 1089 free(st->key); 1090 st->key = NULL; 1091 return (NS_UNAVAIL); 1092 } 1093 1094 1095 static int 1096 nis_group(void *retval, void *mdata, va_list ap) 1097 { 1098 char *map; 1099 struct nis_state *st; 1100 struct group *grp; 1101 const char *name; 1102 char *buffer, *key, *result; 1103 size_t bufsize; 1104 gid_t gid; 1105 enum nss_lookup_type how; 1106 int *errnop, keylen, resultlen, rv; 1107 1108 name = NULL; 1109 gid = (gid_t)-1; 1110 how = (enum nss_lookup_type)(uintptr_t)mdata; 1111 switch (how) { 1112 case nss_lt_name: 1113 name = va_arg(ap, const char *); 1114 map = "group.byname"; 1115 break; 1116 case nss_lt_id: 1117 gid = va_arg(ap, gid_t); 1118 map = "group.bygid"; 1119 break; 1120 case nss_lt_all: 1121 map = "group.byname"; 1122 break; 1123 } 1124 grp = va_arg(ap, struct group *); 1125 buffer = va_arg(ap, char *); 1126 bufsize = va_arg(ap, size_t); 1127 errnop = va_arg(ap, int *); 1128 *errnop = nis_getstate(&st); 1129 if (*errnop != 0) 1130 return (NS_UNAVAIL); 1131 if (st->domain[0] == '\0') { 1132 if (getdomainname(st->domain, sizeof(st->domain)) != 0) { 1133 *errnop = errno; 1134 return (NS_UNAVAIL); 1135 } 1136 } 1137 result = NULL; 1138 do { 1139 rv = NS_NOTFOUND; 1140 switch (how) { 1141 case nss_lt_name: 1142 if (strlcpy(buffer, name, bufsize) >= bufsize) 1143 goto erange; 1144 break; 1145 case nss_lt_id: 1146 if (snprintf(buffer, bufsize, "%lu", 1147 (unsigned long)gid) >= bufsize) 1148 goto erange; 1149 break; 1150 case nss_lt_all: 1151 if (st->done) 1152 goto fin; 1153 break; 1154 } 1155 result = NULL; 1156 if (how == nss_lt_all) { 1157 if (st->key == NULL) 1158 rv = yp_first(st->domain, map, &st->key, 1159 &st->keylen, &result, &resultlen); 1160 else { 1161 key = st->key; 1162 keylen = st->keylen; 1163 st->key = NULL; 1164 rv = yp_next(st->domain, map, key, keylen, 1165 &st->key, &st->keylen, &result, 1166 &resultlen); 1167 free(key); 1168 } 1169 if (rv != 0) { 1170 free(result); 1171 free(st->key); 1172 st->key = NULL; 1173 if (rv == YPERR_NOMORE) { 1174 st->done = 1; 1175 rv = NS_NOTFOUND; 1176 } else 1177 rv = NS_UNAVAIL; 1178 goto fin; 1179 } 1180 } else { 1181 rv = yp_match(st->domain, map, buffer, strlen(buffer), 1182 &result, &resultlen); 1183 if (rv == YPERR_KEY) { 1184 rv = NS_NOTFOUND; 1185 continue; 1186 } else if (rv != 0) { 1187 free(result); 1188 rv = NS_UNAVAIL; 1189 continue; 1190 } 1191 } 1192 /* We need room at least for the line, a string NUL 1193 * terminator, alignment padding, and one (char *) 1194 * pointer for the member list terminator. 1195 */ 1196 if (resultlen >= bufsize - _ALIGNBYTES - sizeof(char *)) { 1197 free(result); 1198 goto erange; 1199 } 1200 memcpy(buffer, result, resultlen); 1201 buffer[resultlen] = '\0'; 1202 free(result); 1203 rv = __gr_match_entry(buffer, resultlen, how, name, gid); 1204 if (rv == NS_SUCCESS) 1205 rv = __gr_parse_entry(buffer, resultlen, grp, 1206 &buffer[resultlen+1], bufsize - resultlen - 1, 1207 errnop); 1208 } while (how == nss_lt_all && !(rv & NS_TERMINATE)); 1209 fin: 1210 if (rv == NS_SUCCESS && retval != NULL) 1211 *(struct group **)retval = grp; 1212 return (rv); 1213 erange: 1214 *errnop = ERANGE; 1215 return (NS_RETURN); 1216 } 1217 #endif /* YP */ 1218 1219 1220 1221 /* 1222 * compat backend 1223 */ 1224 static void 1225 compat_endstate(void *p) 1226 { 1227 struct compat_state *st; 1228 1229 if (p == NULL) 1230 return; 1231 st = (struct compat_state *)p; 1232 free(st->name); 1233 if (st->fp != NULL) 1234 fclose(st->fp); 1235 free(p); 1236 } 1237 1238 1239 static int 1240 compat_setgrent(void *retval, void *mdata, va_list ap) 1241 { 1242 static const ns_src compatsrc[] = { 1243 #ifdef YP 1244 { NSSRC_NIS, NS_SUCCESS }, 1245 #endif 1246 { NULL, 0 } 1247 }; 1248 ns_dtab dtab[] = { 1249 #ifdef HESIOD 1250 { NSSRC_DNS, dns_setgrent, NULL }, 1251 #endif 1252 #ifdef YP 1253 { NSSRC_NIS, nis_setgrent, NULL }, 1254 #endif 1255 { NULL, NULL, NULL } 1256 }; 1257 struct compat_state *st; 1258 int rv, stayopen; 1259 1260 #define set_setent(x, y) do { \ 1261 int i; \ 1262 for (i = 0; i < (int)(nitems(x) - 1); i++) \ 1263 x[i].mdata = (void *)y; \ 1264 } while (0) 1265 1266 rv = compat_getstate(&st); 1267 if (rv != 0) 1268 return (NS_UNAVAIL); 1269 switch ((enum constants)(uintptr_t)mdata) { 1270 case SETGRENT: 1271 stayopen = va_arg(ap, int); 1272 if (st->fp != NULL) 1273 rewind(st->fp); 1274 else if (stayopen) 1275 st->fp = fopen(_PATH_GROUP, "re"); 1276 set_setent(dtab, mdata); 1277 (void)_nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "setgrent", 1278 compatsrc, 0); 1279 break; 1280 case ENDGRENT: 1281 if (st->fp != NULL) { 1282 fclose(st->fp); 1283 st->fp = NULL; 1284 } 1285 set_setent(dtab, mdata); 1286 (void)_nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "endgrent", 1287 compatsrc, 0); 1288 break; 1289 default: 1290 break; 1291 } 1292 st->compat = COMPAT_MODE_OFF; 1293 free(st->name); 1294 st->name = NULL; 1295 return (NS_UNAVAIL); 1296 #undef set_setent 1297 } 1298 1299 1300 static int 1301 compat_group(void *retval, void *mdata, va_list ap) 1302 { 1303 static const ns_src compatsrc[] = { 1304 #ifdef YP 1305 { NSSRC_NIS, NS_SUCCESS }, 1306 #endif 1307 { NULL, 0 } 1308 }; 1309 ns_dtab dtab[] = { 1310 #ifdef YP 1311 { NSSRC_NIS, nis_group, NULL }, 1312 #endif 1313 #ifdef HESIOD 1314 { NSSRC_DNS, dns_group, NULL }, 1315 #endif 1316 { NULL, NULL, NULL } 1317 }; 1318 struct compat_state *st; 1319 enum nss_lookup_type how; 1320 const char *name, *line; 1321 struct group *grp; 1322 gid_t gid; 1323 char *buffer, *p; 1324 void *discard; 1325 size_t bufsize, linesize; 1326 off_t pos; 1327 int fresh, rv, stayopen, *errnop; 1328 1329 #define set_lookup_type(x, y) do { \ 1330 int i; \ 1331 for (i = 0; i < (int)(nitems(x) - 1); i++) \ 1332 x[i].mdata = (void *)y; \ 1333 } while (0) 1334 1335 fresh = 0; 1336 name = NULL; 1337 gid = (gid_t)-1; 1338 how = (enum nss_lookup_type)(uintptr_t)mdata; 1339 switch (how) { 1340 case nss_lt_name: 1341 name = va_arg(ap, const char *); 1342 break; 1343 case nss_lt_id: 1344 gid = va_arg(ap, gid_t); 1345 break; 1346 case nss_lt_all: 1347 break; 1348 default: 1349 return (NS_NOTFOUND); 1350 } 1351 grp = va_arg(ap, struct group *); 1352 buffer = va_arg(ap, char *); 1353 bufsize = va_arg(ap, size_t); 1354 errnop = va_arg(ap, int *); 1355 *errnop = compat_getstate(&st); 1356 if (*errnop != 0) 1357 return (NS_UNAVAIL); 1358 if (st->fp == NULL) { 1359 st->fp = fopen(_PATH_GROUP, "re"); 1360 if (st->fp == NULL) { 1361 *errnop = errno; 1362 rv = NS_UNAVAIL; 1363 goto fin; 1364 } 1365 fresh = 1; 1366 } 1367 if (how == nss_lt_all) 1368 stayopen = 1; 1369 else { 1370 if (!fresh) 1371 rewind(st->fp); 1372 stayopen = st->stayopen; 1373 } 1374 docompat: 1375 switch (st->compat) { 1376 case COMPAT_MODE_ALL: 1377 set_lookup_type(dtab, how); 1378 switch (how) { 1379 case nss_lt_all: 1380 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1381 "getgrent_r", compatsrc, grp, buffer, bufsize, 1382 errnop); 1383 break; 1384 case nss_lt_id: 1385 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1386 "getgrgid_r", compatsrc, gid, grp, buffer, bufsize, 1387 errnop); 1388 break; 1389 case nss_lt_name: 1390 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1391 "getgrnam_r", compatsrc, name, grp, buffer, 1392 bufsize, errnop); 1393 break; 1394 } 1395 if (rv & NS_TERMINATE) 1396 goto fin; 1397 st->compat = COMPAT_MODE_OFF; 1398 break; 1399 case COMPAT_MODE_NAME: 1400 set_lookup_type(dtab, nss_lt_name); 1401 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1402 "getgrnam_r", compatsrc, st->name, grp, buffer, bufsize, 1403 errnop); 1404 switch (rv) { 1405 case NS_SUCCESS: 1406 switch (how) { 1407 case nss_lt_name: 1408 if (strcmp(name, grp->gr_name) != 0) 1409 rv = NS_NOTFOUND; 1410 break; 1411 case nss_lt_id: 1412 if (gid != grp->gr_gid) 1413 rv = NS_NOTFOUND; 1414 break; 1415 default: 1416 break; 1417 } 1418 break; 1419 case NS_RETURN: 1420 goto fin; 1421 default: 1422 break; 1423 } 1424 free(st->name); 1425 st->name = NULL; 1426 st->compat = COMPAT_MODE_OFF; 1427 if (rv == NS_SUCCESS) 1428 goto fin; 1429 break; 1430 default: 1431 break; 1432 } 1433 rv = NS_NOTFOUND; 1434 if (stayopen) 1435 pos = ftello(st->fp); 1436 while ((line = fgetln(st->fp, &linesize)) != NULL) { 1437 if (line[linesize-1] == '\n') 1438 linesize--; 1439 if (linesize > 2 && line[0] == '+') { 1440 p = memchr(&line[1], ':', linesize); 1441 if (p == NULL || p == &line[1]) 1442 st->compat = COMPAT_MODE_ALL; 1443 else { 1444 st->name = malloc(p - line); 1445 if (st->name == NULL) { 1446 syslog(LOG_ERR, 1447 "getgrent memory allocation failure"); 1448 *errnop = ENOMEM; 1449 rv = NS_UNAVAIL; 1450 break; 1451 } 1452 memcpy(st->name, &line[1], p - line - 1); 1453 st->name[p - line - 1] = '\0'; 1454 st->compat = COMPAT_MODE_NAME; 1455 } 1456 goto docompat; 1457 } 1458 rv = __gr_match_entry(line, linesize, how, name, gid); 1459 if (rv != NS_SUCCESS) 1460 continue; 1461 /* We need room at least for the line, a string NUL 1462 * terminator, alignment padding, and one (char *) 1463 * pointer for the member list terminator. 1464 */ 1465 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { 1466 *errnop = ERANGE; 1467 rv = NS_RETURN; 1468 break; 1469 } 1470 memcpy(buffer, line, linesize); 1471 buffer[linesize] = '\0'; 1472 rv = __gr_parse_entry(buffer, linesize, grp, 1473 &buffer[linesize + 1], bufsize - linesize - 1, errnop); 1474 if (rv & NS_TERMINATE) 1475 break; 1476 if (stayopen) 1477 pos = ftello(st->fp); 1478 } 1479 fin: 1480 if (st->fp != NULL && !stayopen) { 1481 fclose(st->fp); 1482 st->fp = NULL; 1483 } 1484 if (rv == NS_SUCCESS && retval != NULL) 1485 *(struct group **)retval = grp; 1486 else if (rv == NS_RETURN && *errnop == ERANGE && st->fp != NULL) 1487 fseeko(st->fp, pos, SEEK_SET); 1488 return (rv); 1489 #undef set_lookup_type 1490 } 1491 1492 1493 /* 1494 * common group line matching and parsing 1495 */ 1496 int 1497 __gr_match_entry(const char *line, size_t linesize, enum nss_lookup_type how, 1498 const char *name, gid_t gid) 1499 { 1500 size_t namesize; 1501 const char *p, *eol; 1502 char *q; 1503 unsigned long n; 1504 int i, needed; 1505 1506 if (linesize == 0 || is_comment_line(line, linesize)) 1507 return (NS_NOTFOUND); 1508 switch (how) { 1509 case nss_lt_name: needed = 1; break; 1510 case nss_lt_id: needed = 2; break; 1511 default: needed = 2; break; 1512 } 1513 eol = &line[linesize]; 1514 for (p = line, i = 0; i < needed && p < eol; p++) 1515 if (*p == ':') 1516 i++; 1517 if (i < needed) 1518 return (NS_NOTFOUND); 1519 switch (how) { 1520 case nss_lt_name: 1521 namesize = strlen(name); 1522 if (namesize + 1 == (size_t)(p - line) && 1523 memcmp(line, name, namesize) == 0) 1524 return (NS_SUCCESS); 1525 break; 1526 case nss_lt_id: 1527 n = strtoul(p, &q, 10); 1528 if (q < eol && *q == ':' && gid == (gid_t)n) 1529 return (NS_SUCCESS); 1530 break; 1531 case nss_lt_all: 1532 return (NS_SUCCESS); 1533 default: 1534 break; 1535 } 1536 return (NS_NOTFOUND); 1537 } 1538 1539 1540 int 1541 __gr_parse_entry(char *line, size_t linesize, struct group *grp, char *membuf, 1542 size_t membufsize, int *errnop) 1543 { 1544 char *s_gid, *s_mem, *p, **members; 1545 unsigned long n; 1546 int maxmembers; 1547 1548 memset(grp, 0, sizeof(*grp)); 1549 members = (char **)_ALIGN(membuf); 1550 membufsize -= (char *)members - membuf; 1551 maxmembers = membufsize / sizeof(*members); 1552 if (maxmembers <= 0 || 1553 (grp->gr_name = strsep(&line, ":")) == NULL || 1554 grp->gr_name[0] == '\0' || 1555 (grp->gr_passwd = strsep(&line, ":")) == NULL || 1556 (s_gid = strsep(&line, ":")) == NULL || 1557 s_gid[0] == '\0') 1558 return (NS_NOTFOUND); 1559 s_mem = line; 1560 n = strtoul(s_gid, &s_gid, 10); 1561 if (s_gid[0] != '\0') 1562 return (NS_NOTFOUND); 1563 grp->gr_gid = (gid_t)n; 1564 grp->gr_mem = members; 1565 while (maxmembers > 1 && s_mem != NULL) { 1566 p = strsep(&s_mem, ","); 1567 if (p != NULL && *p != '\0') { 1568 *members++ = p; 1569 maxmembers--; 1570 } 1571 } 1572 *members = NULL; 1573 if (s_mem == NULL) 1574 return (NS_SUCCESS); 1575 else { 1576 *errnop = ERANGE; 1577 return (NS_RETURN); 1578 } 1579 } 1580 1581 1582