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 st->stayopen = stayopen; 828 break; 829 case ENDGRENT: 830 if (st->fp != NULL) { 831 fclose(st->fp); 832 st->fp = NULL; 833 } 834 break; 835 default: 836 break; 837 } 838 return (NS_UNAVAIL); 839 } 840 841 842 static int 843 files_group(void *retval, void *mdata, va_list ap) 844 { 845 struct files_state *st; 846 enum nss_lookup_type how; 847 const char *name, *line; 848 struct group *grp; 849 gid_t gid; 850 char *buffer; 851 size_t bufsize, linesize; 852 off_t pos; 853 int fresh, rv, stayopen, *errnop; 854 855 fresh = 0; 856 name = NULL; 857 gid = (gid_t)-1; 858 how = (enum nss_lookup_type)(uintptr_t)mdata; 859 switch (how) { 860 case nss_lt_name: 861 name = va_arg(ap, const char *); 862 break; 863 case nss_lt_id: 864 gid = va_arg(ap, gid_t); 865 break; 866 case nss_lt_all: 867 break; 868 default: 869 return (NS_NOTFOUND); 870 } 871 grp = va_arg(ap, struct group *); 872 buffer = va_arg(ap, char *); 873 bufsize = va_arg(ap, size_t); 874 errnop = va_arg(ap, int *); 875 *errnop = files_getstate(&st); 876 if (*errnop != 0) 877 return (NS_UNAVAIL); 878 if (st->fp == NULL) { 879 st->fp = fopen(_PATH_GROUP, "re"); 880 if (st->fp == NULL) { 881 *errnop = errno; 882 return (NS_UNAVAIL); 883 } 884 fresh = 1; 885 } 886 stayopen = (how == nss_lt_all || !fresh) ? 1 : st->stayopen; 887 if (stayopen) 888 pos = ftello(st->fp); 889 if (how != nss_lt_all && !fresh) 890 rewind(st->fp); 891 rv = NS_NOTFOUND; 892 while ((line = fgetln(st->fp, &linesize)) != NULL) { 893 if (line[linesize-1] == '\n') 894 linesize--; 895 rv = __gr_match_entry(line, linesize, how, name, gid); 896 if (rv != NS_SUCCESS) 897 continue; 898 /* We need room at least for the line, a string NUL 899 * terminator, alignment padding, and one (char *) 900 * pointer for the member list terminator. 901 */ 902 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { 903 *errnop = ERANGE; 904 rv = NS_RETURN; 905 break; 906 } 907 memcpy(buffer, line, linesize); 908 buffer[linesize] = '\0'; 909 rv = __gr_parse_entry(buffer, linesize, grp, 910 &buffer[linesize + 1], bufsize - linesize - 1, errnop); 911 if (rv & NS_TERMINATE) 912 break; 913 if (how == nss_lt_all) 914 pos = ftello(st->fp); 915 } 916 if (st->fp != NULL && !stayopen) { 917 fclose(st->fp); 918 st->fp = NULL; 919 } 920 if (st->fp != NULL && how != nss_lt_all) 921 fseeko(st->fp, pos, SEEK_SET); 922 if (rv == NS_SUCCESS && retval != NULL) 923 *(struct group **)retval = grp; 924 else if (rv == NS_RETURN && *errnop == ERANGE && st->fp != NULL) 925 fseeko(st->fp, pos, SEEK_SET); 926 return (rv); 927 } 928 929 930 #ifdef HESIOD 931 /* 932 * dns backend 933 */ 934 static void 935 dns_endstate(void *p) 936 { 937 938 free(p); 939 } 940 941 942 static int 943 dns_setgrent(void *retval, void *cb_data, va_list ap) 944 { 945 struct dns_state *st; 946 int rv; 947 948 rv = dns_getstate(&st); 949 if (rv != 0) 950 return (NS_UNAVAIL); 951 st->counter = 0; 952 return (NS_UNAVAIL); 953 } 954 955 956 static int 957 dns_group(void *retval, void *mdata, va_list ap) 958 { 959 char buf[HESIOD_NAME_MAX]; 960 struct dns_state *st; 961 struct group *grp; 962 const char *name, *label; 963 void *ctx; 964 char *buffer, **hes; 965 size_t bufsize, adjsize, linesize; 966 gid_t gid; 967 enum nss_lookup_type how; 968 int rv, *errnop; 969 970 ctx = NULL; 971 hes = NULL; 972 name = NULL; 973 gid = (gid_t)-1; 974 how = (enum nss_lookup_type)mdata; 975 switch (how) { 976 case nss_lt_name: 977 name = va_arg(ap, const char *); 978 break; 979 case nss_lt_id: 980 gid = va_arg(ap, gid_t); 981 break; 982 case nss_lt_all: 983 break; 984 } 985 grp = va_arg(ap, struct group *); 986 buffer = va_arg(ap, char *); 987 bufsize = va_arg(ap, size_t); 988 errnop = va_arg(ap, int *); 989 *errnop = dns_getstate(&st); 990 if (*errnop != 0) 991 return (NS_UNAVAIL); 992 if (hesiod_init(&ctx) != 0) { 993 *errnop = errno; 994 rv = NS_UNAVAIL; 995 goto fin; 996 } 997 do { 998 rv = NS_NOTFOUND; 999 switch (how) { 1000 case nss_lt_name: 1001 label = name; 1002 break; 1003 case nss_lt_id: 1004 if (snprintf(buf, sizeof(buf), "%lu", 1005 (unsigned long)gid) >= sizeof(buf)) 1006 goto fin; 1007 label = buf; 1008 break; 1009 case nss_lt_all: 1010 if (st->counter < 0) 1011 goto fin; 1012 if (snprintf(buf, sizeof(buf), "group-%ld", 1013 st->counter++) >= sizeof(buf)) 1014 goto fin; 1015 label = buf; 1016 break; 1017 } 1018 hes = hesiod_resolve(ctx, label, 1019 how == nss_lt_id ? "gid" : "group"); 1020 if ((how == nss_lt_id && hes == NULL && 1021 (hes = hesiod_resolve(ctx, buf, "group")) == NULL) || 1022 hes == NULL) { 1023 if (how == nss_lt_all) 1024 st->counter = -1; 1025 if (errno != ENOENT) 1026 *errnop = errno; 1027 goto fin; 1028 } 1029 rv = __gr_match_entry(hes[0], strlen(hes[0]), how, name, gid); 1030 if (rv != NS_SUCCESS) { 1031 hesiod_free_list(ctx, hes); 1032 hes = NULL; 1033 continue; 1034 } 1035 /* We need room at least for the line, a string NUL 1036 * terminator, alignment padding, and one (char *) 1037 * pointer for the member list terminator. 1038 */ 1039 adjsize = bufsize - _ALIGNBYTES - sizeof(char *); 1040 linesize = strlcpy(buffer, hes[0], adjsize); 1041 if (linesize >= adjsize) { 1042 *errnop = ERANGE; 1043 rv = NS_RETURN; 1044 goto fin; 1045 } 1046 hesiod_free_list(ctx, hes); 1047 hes = NULL; 1048 rv = __gr_parse_entry(buffer, linesize, grp, 1049 &buffer[linesize + 1], bufsize - linesize - 1, errnop); 1050 } while (how == nss_lt_all && !(rv & NS_TERMINATE)); 1051 fin: 1052 if (hes != NULL) 1053 hesiod_free_list(ctx, hes); 1054 if (ctx != NULL) 1055 hesiod_end(ctx); 1056 if (rv == NS_SUCCESS && retval != NULL) 1057 *(struct group **)retval = grp; 1058 return (rv); 1059 } 1060 #endif /* HESIOD */ 1061 1062 1063 #ifdef YP 1064 /* 1065 * nis backend 1066 */ 1067 static void 1068 nis_endstate(void *p) 1069 { 1070 1071 if (p == NULL) 1072 return; 1073 free(((struct nis_state *)p)->key); 1074 free(p); 1075 } 1076 1077 1078 static int 1079 nis_setgrent(void *retval, void *cb_data, va_list ap) 1080 { 1081 struct nis_state *st; 1082 int rv; 1083 1084 rv = nis_getstate(&st); 1085 if (rv != 0) 1086 return (NS_UNAVAIL); 1087 st->done = 0; 1088 free(st->key); 1089 st->key = NULL; 1090 return (NS_UNAVAIL); 1091 } 1092 1093 1094 static int 1095 nis_group(void *retval, void *mdata, va_list ap) 1096 { 1097 char *map; 1098 struct nis_state *st; 1099 struct group *grp; 1100 const char *name; 1101 char *buffer, *key, *result; 1102 size_t bufsize; 1103 gid_t gid; 1104 enum nss_lookup_type how; 1105 int *errnop, keylen, resultlen, rv; 1106 1107 name = NULL; 1108 gid = (gid_t)-1; 1109 how = (enum nss_lookup_type)(uintptr_t)mdata; 1110 switch (how) { 1111 case nss_lt_name: 1112 name = va_arg(ap, const char *); 1113 map = "group.byname"; 1114 break; 1115 case nss_lt_id: 1116 gid = va_arg(ap, gid_t); 1117 map = "group.bygid"; 1118 break; 1119 case nss_lt_all: 1120 map = "group.byname"; 1121 break; 1122 } 1123 grp = va_arg(ap, struct group *); 1124 buffer = va_arg(ap, char *); 1125 bufsize = va_arg(ap, size_t); 1126 errnop = va_arg(ap, int *); 1127 *errnop = nis_getstate(&st); 1128 if (*errnop != 0) 1129 return (NS_UNAVAIL); 1130 if (st->domain[0] == '\0') { 1131 if (getdomainname(st->domain, sizeof(st->domain)) != 0) { 1132 *errnop = errno; 1133 return (NS_UNAVAIL); 1134 } 1135 } 1136 result = NULL; 1137 do { 1138 rv = NS_NOTFOUND; 1139 switch (how) { 1140 case nss_lt_name: 1141 if (strlcpy(buffer, name, bufsize) >= bufsize) 1142 goto erange; 1143 break; 1144 case nss_lt_id: 1145 if (snprintf(buffer, bufsize, "%lu", 1146 (unsigned long)gid) >= bufsize) 1147 goto erange; 1148 break; 1149 case nss_lt_all: 1150 if (st->done) 1151 goto fin; 1152 break; 1153 } 1154 result = NULL; 1155 if (how == nss_lt_all) { 1156 if (st->key == NULL) 1157 rv = yp_first(st->domain, map, &st->key, 1158 &st->keylen, &result, &resultlen); 1159 else { 1160 key = st->key; 1161 keylen = st->keylen; 1162 st->key = NULL; 1163 rv = yp_next(st->domain, map, key, keylen, 1164 &st->key, &st->keylen, &result, 1165 &resultlen); 1166 free(key); 1167 } 1168 if (rv != 0) { 1169 free(result); 1170 free(st->key); 1171 st->key = NULL; 1172 if (rv == YPERR_NOMORE) { 1173 st->done = 1; 1174 rv = NS_NOTFOUND; 1175 } else 1176 rv = NS_UNAVAIL; 1177 goto fin; 1178 } 1179 } else { 1180 rv = yp_match(st->domain, map, buffer, strlen(buffer), 1181 &result, &resultlen); 1182 if (rv == YPERR_KEY) { 1183 rv = NS_NOTFOUND; 1184 continue; 1185 } else if (rv != 0) { 1186 free(result); 1187 rv = NS_UNAVAIL; 1188 continue; 1189 } 1190 } 1191 /* We need room at least for the line, a string NUL 1192 * terminator, alignment padding, and one (char *) 1193 * pointer for the member list terminator. 1194 */ 1195 if (resultlen >= bufsize - _ALIGNBYTES - sizeof(char *)) { 1196 free(result); 1197 goto erange; 1198 } 1199 memcpy(buffer, result, resultlen); 1200 buffer[resultlen] = '\0'; 1201 free(result); 1202 rv = __gr_match_entry(buffer, resultlen, how, name, gid); 1203 if (rv == NS_SUCCESS) 1204 rv = __gr_parse_entry(buffer, resultlen, grp, 1205 &buffer[resultlen+1], bufsize - resultlen - 1, 1206 errnop); 1207 } while (how == nss_lt_all && !(rv & NS_TERMINATE)); 1208 fin: 1209 if (rv == NS_SUCCESS && retval != NULL) 1210 *(struct group **)retval = grp; 1211 return (rv); 1212 erange: 1213 *errnop = ERANGE; 1214 return (NS_RETURN); 1215 } 1216 #endif /* YP */ 1217 1218 1219 1220 /* 1221 * compat backend 1222 */ 1223 static void 1224 compat_endstate(void *p) 1225 { 1226 struct compat_state *st; 1227 1228 if (p == NULL) 1229 return; 1230 st = (struct compat_state *)p; 1231 free(st->name); 1232 if (st->fp != NULL) 1233 fclose(st->fp); 1234 free(p); 1235 } 1236 1237 1238 static int 1239 compat_setgrent(void *retval, void *mdata, va_list ap) 1240 { 1241 static const ns_src compatsrc[] = { 1242 #ifdef YP 1243 { NSSRC_NIS, NS_SUCCESS }, 1244 #endif 1245 { NULL, 0 } 1246 }; 1247 ns_dtab dtab[] = { 1248 #ifdef HESIOD 1249 { NSSRC_DNS, dns_setgrent, NULL }, 1250 #endif 1251 #ifdef YP 1252 { NSSRC_NIS, nis_setgrent, NULL }, 1253 #endif 1254 { NULL, NULL, NULL } 1255 }; 1256 struct compat_state *st; 1257 int rv, stayopen; 1258 1259 #define set_setent(x, y) do { \ 1260 int i; \ 1261 for (i = 0; i < (int)(nitems(x) - 1); i++) \ 1262 x[i].mdata = (void *)y; \ 1263 } while (0) 1264 1265 rv = compat_getstate(&st); 1266 if (rv != 0) 1267 return (NS_UNAVAIL); 1268 switch ((enum constants)(uintptr_t)mdata) { 1269 case SETGRENT: 1270 stayopen = va_arg(ap, int); 1271 if (st->fp != NULL) 1272 rewind(st->fp); 1273 else if (stayopen) 1274 st->fp = fopen(_PATH_GROUP, "re"); 1275 st->stayopen = stayopen; 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 stayopen = (how == nss_lt_all || !fresh) ? 1 : st->stayopen; 1368 if (stayopen) 1369 pos = ftello(st->fp); 1370 if (how != nss_lt_all && !fresh) 1371 rewind(st->fp); 1372 docompat: 1373 switch (st->compat) { 1374 case COMPAT_MODE_ALL: 1375 set_lookup_type(dtab, how); 1376 switch (how) { 1377 case nss_lt_all: 1378 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1379 "getgrent_r", compatsrc, grp, buffer, bufsize, 1380 errnop); 1381 break; 1382 case nss_lt_id: 1383 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1384 "getgrgid_r", compatsrc, gid, grp, buffer, bufsize, 1385 errnop); 1386 break; 1387 case nss_lt_name: 1388 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1389 "getgrnam_r", compatsrc, name, grp, buffer, 1390 bufsize, errnop); 1391 break; 1392 } 1393 if (rv & NS_TERMINATE) 1394 goto fin; 1395 st->compat = COMPAT_MODE_OFF; 1396 break; 1397 case COMPAT_MODE_NAME: 1398 set_lookup_type(dtab, nss_lt_name); 1399 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, 1400 "getgrnam_r", compatsrc, st->name, grp, buffer, bufsize, 1401 errnop); 1402 switch (rv) { 1403 case NS_SUCCESS: 1404 switch (how) { 1405 case nss_lt_name: 1406 if (strcmp(name, grp->gr_name) != 0) 1407 rv = NS_NOTFOUND; 1408 break; 1409 case nss_lt_id: 1410 if (gid != grp->gr_gid) 1411 rv = NS_NOTFOUND; 1412 break; 1413 default: 1414 break; 1415 } 1416 break; 1417 case NS_RETURN: 1418 goto fin; 1419 default: 1420 break; 1421 } 1422 free(st->name); 1423 st->name = NULL; 1424 st->compat = COMPAT_MODE_OFF; 1425 if (rv == NS_SUCCESS) 1426 goto fin; 1427 break; 1428 default: 1429 break; 1430 } 1431 rv = NS_NOTFOUND; 1432 while ((line = fgetln(st->fp, &linesize)) != NULL) { 1433 if (line[linesize-1] == '\n') 1434 linesize--; 1435 if (linesize > 2 && line[0] == '+') { 1436 p = memchr(&line[1], ':', linesize); 1437 if (p == NULL || p == &line[1]) 1438 st->compat = COMPAT_MODE_ALL; 1439 else { 1440 st->name = malloc(p - line); 1441 if (st->name == NULL) { 1442 syslog(LOG_ERR, 1443 "getgrent memory allocation failure"); 1444 *errnop = ENOMEM; 1445 rv = NS_UNAVAIL; 1446 break; 1447 } 1448 memcpy(st->name, &line[1], p - line - 1); 1449 st->name[p - line - 1] = '\0'; 1450 st->compat = COMPAT_MODE_NAME; 1451 } 1452 goto docompat; 1453 } 1454 rv = __gr_match_entry(line, linesize, how, name, gid); 1455 if (rv != NS_SUCCESS) 1456 continue; 1457 /* We need room at least for the line, a string NUL 1458 * terminator, alignment padding, and one (char *) 1459 * pointer for the member list terminator. 1460 */ 1461 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { 1462 *errnop = ERANGE; 1463 rv = NS_RETURN; 1464 break; 1465 } 1466 memcpy(buffer, line, linesize); 1467 buffer[linesize] = '\0'; 1468 rv = __gr_parse_entry(buffer, linesize, grp, 1469 &buffer[linesize + 1], bufsize - linesize - 1, errnop); 1470 if (rv & NS_TERMINATE) 1471 break; 1472 if (how == nss_lt_all) 1473 pos = ftello(st->fp); 1474 } 1475 fin: 1476 if (st->fp != NULL && !stayopen) { 1477 fclose(st->fp); 1478 st->fp = NULL; 1479 } 1480 if (st->fp != NULL && how != nss_lt_all) 1481 fseeko(st->fp, pos, SEEK_SET); 1482 if (rv == NS_SUCCESS && retval != NULL) 1483 *(struct group **)retval = grp; 1484 else if (rv == NS_RETURN && *errnop == ERANGE && st->fp != NULL) 1485 fseeko(st->fp, pos, SEEK_SET); 1486 return (rv); 1487 #undef set_lookup_type 1488 } 1489 1490 1491 /* 1492 * common group line matching and parsing 1493 */ 1494 int 1495 __gr_match_entry(const char *line, size_t linesize, enum nss_lookup_type how, 1496 const char *name, gid_t gid) 1497 { 1498 size_t namesize; 1499 const char *p, *eol; 1500 char *q; 1501 unsigned long n; 1502 int i, needed; 1503 1504 if (linesize == 0 || is_comment_line(line, linesize)) 1505 return (NS_NOTFOUND); 1506 switch (how) { 1507 case nss_lt_name: needed = 1; break; 1508 case nss_lt_id: needed = 2; break; 1509 default: needed = 2; break; 1510 } 1511 eol = &line[linesize]; 1512 for (p = line, i = 0; i < needed && p < eol; p++) 1513 if (*p == ':') 1514 i++; 1515 if (i < needed) 1516 return (NS_NOTFOUND); 1517 switch (how) { 1518 case nss_lt_name: 1519 namesize = strlen(name); 1520 if (namesize + 1 == (size_t)(p - line) && 1521 memcmp(line, name, namesize) == 0) 1522 return (NS_SUCCESS); 1523 break; 1524 case nss_lt_id: 1525 n = strtoul(p, &q, 10); 1526 if (q < eol && *q == ':' && gid == (gid_t)n) 1527 return (NS_SUCCESS); 1528 break; 1529 case nss_lt_all: 1530 return (NS_SUCCESS); 1531 default: 1532 break; 1533 } 1534 return (NS_NOTFOUND); 1535 } 1536 1537 1538 int 1539 __gr_parse_entry(char *line, size_t linesize, struct group *grp, char *membuf, 1540 size_t membufsize, int *errnop) 1541 { 1542 char *s_gid, *s_mem, *p, **members; 1543 unsigned long n; 1544 int maxmembers; 1545 1546 memset(grp, 0, sizeof(*grp)); 1547 members = (char **)_ALIGN(membuf); 1548 membufsize -= (char *)members - membuf; 1549 maxmembers = membufsize / sizeof(*members); 1550 if (maxmembers <= 0 || 1551 (grp->gr_name = strsep(&line, ":")) == NULL || 1552 grp->gr_name[0] == '\0' || 1553 (grp->gr_passwd = strsep(&line, ":")) == NULL || 1554 (s_gid = strsep(&line, ":")) == NULL || 1555 s_gid[0] == '\0') 1556 return (NS_NOTFOUND); 1557 s_mem = line; 1558 n = strtoul(s_gid, &s_gid, 10); 1559 if (s_gid[0] != '\0') 1560 return (NS_NOTFOUND); 1561 grp->gr_gid = (gid_t)n; 1562 grp->gr_mem = members; 1563 while (maxmembers > 1 && s_mem != NULL) { 1564 p = strsep(&s_mem, ","); 1565 if (p != NULL && *p != '\0') { 1566 *members++ = p; 1567 maxmembers--; 1568 } 1569 } 1570 *members = NULL; 1571 if (s_mem == NULL) 1572 return (NS_SUCCESS); 1573 else { 1574 *errnop = ERANGE; 1575 return (NS_RETURN); 1576 } 1577 } 1578 1579 1580