1 /* 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #if defined(LIBC_SCCS) && !defined(lint) 38 static char sccsid[] = "@(#)getnetgrent.c 8.2 (Berkeley) 4/27/95"; 39 #endif /* LIBC_SCCS and not lint */ 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include <ctype.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #ifdef YP 50 /* 51 * Notes: 52 * We want to be able to use NIS netgroups properly while retaining 53 * the ability to use a local /etc/netgroup file. Unfortunately, you 54 * can't really do both at the same time - at least, not efficiently. 55 * NetBSD deals with this problem by creating a netgroup database 56 * using Berkeley DB (just like the password database) that allows 57 * for lookups using netgroup, netgroup.byuser or netgroup.byhost 58 * searches. This is a neat idea, but I don't have time to implement 59 * something like that now. (I think ultimately it would be nice 60 * if we DB-fied the group and netgroup stuff all in one shot, but 61 * for now I'm satisfied just to have something that works well 62 * without requiring massive code changes.) 63 * 64 * Therefore, to still permit the use of the local file and maintain 65 * optimum NIS performance, we allow for the following conditions: 66 * 67 * - If /etc/netgroup does not exist and NIS is turned on, we use 68 * NIS netgroups only. 69 * 70 * - If /etc/netgroup exists but is empty, we use NIS netgroups 71 * only. 72 * 73 * - If /etc/netgroup exists and contains _only_ a '+', we use 74 * NIS netgroups only. 75 * 76 * - If /etc/netgroup exists, contains locally defined netgroups 77 * and a '+', we use a mixture of NIS and the local entries. 78 * This method should return the same NIS data as just using 79 * NIS alone, but it will be slower if the NIS netgroup database 80 * is large (innetgr() in particular will suffer since extra 81 * processing has to be done in order to determine memberships 82 * using just the raw netgroup data). 83 * 84 * - If /etc/netgroup exists and contains only locally defined 85 * netgroup entries, we use just those local entries and ignore 86 * NIS (this is the original, pre-NIS behavior). 87 */ 88 89 #include <rpc/rpc.h> 90 #include <rpcsvc/yp_prot.h> 91 #include <rpcsvc/ypclnt.h> 92 #include <sys/types.h> 93 #include <sys/stat.h> 94 #include <sys/param.h> 95 #include <sys/errno.h> 96 static char *_netgr_yp_domain; 97 int _use_only_yp; 98 static int _netgr_yp_enabled; 99 static int _yp_innetgr; 100 #endif 101 102 #ifndef _PATH_NETGROUP 103 #define _PATH_NETGROUP "/etc/netgroup" 104 #endif 105 106 /* 107 * Static Variables and functions used by setnetgrent(), getnetgrent() and 108 * endnetgrent(). 109 * There are two linked lists: 110 * - linelist is just used by setnetgrent() to parse the net group file via. 111 * parse_netgrp() 112 * - netgrp is the list of entries for the current netgroup 113 */ 114 struct linelist { 115 struct linelist *l_next; /* Chain ptr. */ 116 int l_parsed; /* Flag for cycles */ 117 char *l_groupname; /* Name of netgroup */ 118 char *l_line; /* Netgroup entrie(s) to be parsed */ 119 }; 120 121 struct netgrp { 122 struct netgrp *ng_next; /* Chain ptr */ 123 char *ng_str[3]; /* Field pointers, see below */ 124 }; 125 #define NG_HOST 0 /* Host name */ 126 #define NG_USER 1 /* User name */ 127 #define NG_DOM 2 /* and Domain name */ 128 129 static struct linelist *linehead = (struct linelist *)0; 130 static struct netgrp *nextgrp = (struct netgrp *)0; 131 static struct { 132 struct netgrp *gr; 133 char *grname; 134 } grouphead = { 135 (struct netgrp *)0, 136 (char *)0, 137 }; 138 static FILE *netf = (FILE *)0; 139 static int parse_netgrp(); 140 static struct linelist *read_for_group(); 141 void setnetgrent(), endnetgrent(); 142 int getnetgrent(), innetgr(); 143 144 #define LINSIZ 1024 /* Length of netgroup file line */ 145 146 /* 147 * setnetgrent() 148 * Parse the netgroup file looking for the netgroup and build the list 149 * of netgrp structures. Let parse_netgrp() and read_for_group() do 150 * most of the work. 151 */ 152 void 153 setnetgrent(group) 154 char *group; 155 { 156 #ifdef YP 157 struct stat _yp_statp; 158 char _yp_plus; 159 #endif 160 161 /* Sanity check */ 162 163 if (group == NULL || !strlen(group)) 164 return; 165 166 if (grouphead.gr == (struct netgrp *)0 || 167 strcmp(group, grouphead.grname)) { 168 endnetgrent(); 169 #ifdef YP 170 /* Presumed guilty until proven innocent. */ 171 _use_only_yp = 0; 172 /* 173 * If /etc/netgroup doesn't exist or is empty, 174 * use NIS exclusively. 175 */ 176 if (((stat(_PATH_NETGROUP, &_yp_statp) < 0) && 177 errno == ENOENT) || _yp_statp.st_size == 0) 178 _use_only_yp = _netgr_yp_enabled = 1; 179 if ((netf = fopen(_PATH_NETGROUP,"r")) != NULL ||_use_only_yp){ 180 /* 181 * Icky: grab the first character of the netgroup file 182 * and turn on NIS if it's a '+'. rewind the stream 183 * afterwards so we don't goof up read_for_group() later. 184 */ 185 if (netf) { 186 fscanf(netf, "%c", &_yp_plus); 187 rewind(netf); 188 if (_yp_plus == '+') 189 _use_only_yp = _netgr_yp_enabled = 1; 190 } 191 /* 192 * If we were called specifically for an innetgr() 193 * lookup and we're in NIS-only mode, short-circuit 194 * parse_netgroup() and cut directly to the chase. 195 */ 196 if (_use_only_yp && _yp_innetgr) { 197 /* dohw! */ 198 if (netf != NULL) 199 fclose(netf); 200 return; 201 } 202 #else 203 if (netf = fopen(_PATH_NETGROUP, "r")) { 204 #endif 205 if (parse_netgrp(group)) 206 endnetgrent(); 207 else { 208 grouphead.grname = (char *) 209 malloc(strlen(group) + 1); 210 strcpy(grouphead.grname, group); 211 } 212 if (netf) 213 fclose(netf); 214 } 215 } 216 nextgrp = grouphead.gr; 217 } 218 219 /* 220 * Get the next netgroup off the list. 221 */ 222 int 223 getnetgrent(hostp, userp, domp) 224 char **hostp, **userp, **domp; 225 { 226 #ifdef YP 227 _yp_innetgr = 0; 228 #endif 229 230 if (nextgrp) { 231 *hostp = nextgrp->ng_str[NG_HOST]; 232 *userp = nextgrp->ng_str[NG_USER]; 233 *domp = nextgrp->ng_str[NG_DOM]; 234 nextgrp = nextgrp->ng_next; 235 return (1); 236 } 237 return (0); 238 } 239 240 /* 241 * endnetgrent() - cleanup 242 */ 243 void 244 endnetgrent() 245 { 246 struct linelist *lp, *olp; 247 struct netgrp *gp, *ogp; 248 249 lp = linehead; 250 while (lp) { 251 olp = lp; 252 lp = lp->l_next; 253 free(olp->l_groupname); 254 free(olp->l_line); 255 free((char *)olp); 256 } 257 linehead = (struct linelist *)0; 258 if (grouphead.grname) { 259 free(grouphead.grname); 260 grouphead.grname = (char *)0; 261 } 262 gp = grouphead.gr; 263 while (gp) { 264 ogp = gp; 265 gp = gp->ng_next; 266 if (ogp->ng_str[NG_HOST]) 267 free(ogp->ng_str[NG_HOST]); 268 if (ogp->ng_str[NG_USER]) 269 free(ogp->ng_str[NG_USER]); 270 if (ogp->ng_str[NG_DOM]) 271 free(ogp->ng_str[NG_DOM]); 272 free((char *)ogp); 273 } 274 grouphead.gr = (struct netgrp *)0; 275 #ifdef YP 276 _netgr_yp_enabled = 0; 277 #endif 278 } 279 280 #ifdef YP 281 static int _listmatch(list, group, len) 282 char *list, *group; 283 int len; 284 { 285 char *ptr = list, *cptr; 286 int glen = strlen(group); 287 288 /* skip possible leading whitespace */ 289 while(isspace((unsigned char)*ptr)) 290 ptr++; 291 292 while (ptr < list + len) { 293 cptr = ptr; 294 while(*ptr != ',' && *ptr != '\0' && !isspace((unsigned char)*ptr)) 295 ptr++; 296 if (strncmp(cptr, group, glen) == 0 && glen == (ptr - cptr)) 297 return(1); 298 while(*ptr == ',' || isspace((unsigned char)*ptr)) 299 ptr++; 300 } 301 302 return(0); 303 } 304 305 static int _buildkey(key, str, dom, rotation) 306 char *key, *str, *dom; 307 int *rotation; 308 { 309 (*rotation)++; 310 if (*rotation > 4) 311 return(0); 312 switch(*rotation) { 313 case(1): sprintf((char *)key, "%s.%s", str, dom ? dom : "*"); 314 break; 315 case(2): sprintf((char *)key, "%s.*", str); 316 break; 317 case(3): sprintf((char *)key, "*.%s", dom ? dom : "*"); 318 break; 319 case(4): sprintf((char *)key, "*.*"); 320 break; 321 } 322 return(1); 323 } 324 #endif 325 326 /* 327 * Search for a match in a netgroup. 328 */ 329 int 330 innetgr(group, host, user, dom) 331 const char *group, *host, *user, *dom; 332 { 333 char *hst, *usr, *dm; 334 #ifdef YP 335 char *result; 336 int resultlen; 337 int rv; 338 #endif 339 /* Sanity check */ 340 341 if (group == NULL || !strlen(group)) 342 return (0); 343 344 #ifdef YP 345 _yp_innetgr = 1; 346 #endif 347 setnetgrent(group); 348 #ifdef YP 349 _yp_innetgr = 0; 350 /* 351 * If we're in NIS-only mode, do the search using 352 * NIS 'reverse netgroup' lookups. 353 */ 354 if (_use_only_yp) { 355 char _key[MAXHOSTNAMELEN]; 356 int rot = 0, y = 0; 357 358 if(yp_get_default_domain(&_netgr_yp_domain)) 359 return(0); 360 while(_buildkey(_key, user ? user : host, dom, &rot)) { 361 y = yp_match(_netgr_yp_domain, user? "netgroup.byuser": 362 "netgroup.byhost", _key, strlen(_key), &result, 363 &resultlen); 364 if (y) { 365 /* 366 * If we get an error other than 'no 367 * such key in map' then something is 368 * wrong and we should stop the search. 369 */ 370 if (y != YPERR_KEY) 371 break; 372 } else { 373 rv = _listmatch(result, group, resultlen); 374 free(result); 375 if (rv) 376 return(1); 377 else 378 return(0); 379 } 380 } 381 /* 382 * Couldn't match using NIS-exclusive mode. If the error 383 * was YPERR_MAP, then the failure happened because there 384 * was no netgroup.byhost or netgroup.byuser map. The odds 385 * are we are talking to a Sun NIS+ server in YP emulation 386 * mode; if this is the case, then we have to do the check 387 * the 'old-fashioned' way by grovelling through the netgroup 388 * map and resolving memberships on the fly. 389 */ 390 if (y != YPERR_MAP) 391 return(0); 392 } 393 394 setnetgrent(group); 395 #endif /* YP */ 396 397 while (getnetgrent(&hst, &usr, &dm)) 398 if ((host == NULL || hst == NULL || !strcmp(host, hst)) && 399 (user == NULL || usr == NULL || !strcmp(user, usr)) && 400 ( dom == NULL || dm == NULL || !strcmp(dom, dm))) { 401 endnetgrent(); 402 return (1); 403 } 404 endnetgrent(); 405 return (0); 406 } 407 408 /* 409 * Parse the netgroup file setting up the linked lists. 410 */ 411 static int 412 parse_netgrp(group) 413 char *group; 414 { 415 char *spos, *epos; 416 int len, strpos; 417 #ifdef DEBUG 418 int fields; 419 #endif 420 char *pos, *gpos; 421 struct netgrp *grp; 422 struct linelist *lp = linehead; 423 424 /* 425 * First, see if the line has already been read in. 426 */ 427 while (lp) { 428 if (!strcmp(group, lp->l_groupname)) 429 break; 430 lp = lp->l_next; 431 } 432 if (lp == (struct linelist *)0 && 433 (lp = read_for_group(group)) == (struct linelist *)0) 434 return (1); 435 if (lp->l_parsed) { 436 #ifdef DEBUG 437 /* 438 * This error message is largely superflous since the 439 * code handles the error condition sucessfully, and 440 * spewing it out from inside libc can actually hose 441 * certain programs. 442 */ 443 fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname); 444 #endif 445 return (1); 446 } else 447 lp->l_parsed = 1; 448 pos = lp->l_line; 449 /* Watch for null pointer dereferences, dammit! */ 450 while (pos != NULL && *pos != '\0') { 451 if (*pos == '(') { 452 grp = (struct netgrp *)malloc(sizeof (struct netgrp)); 453 bzero((char *)grp, sizeof (struct netgrp)); 454 grp->ng_next = grouphead.gr; 455 grouphead.gr = grp; 456 pos++; 457 gpos = strsep(&pos, ")"); 458 #ifdef DEBUG 459 fields = 0; 460 #endif 461 for (strpos = 0; strpos < 3; strpos++) { 462 if ((spos = strsep(&gpos, ","))) { 463 #ifdef DEBUG 464 fields++; 465 #endif 466 while (*spos == ' ' || *spos == '\t') 467 spos++; 468 if ((epos = strpbrk(spos, " \t"))) { 469 *epos = '\0'; 470 len = epos - spos; 471 } else 472 len = strlen(spos); 473 if (len > 0) { 474 grp->ng_str[strpos] = (char *) 475 malloc(len + 1); 476 bcopy(spos, grp->ng_str[strpos], 477 len + 1); 478 } 479 } else { 480 /* 481 * All other systems I've tested 482 * return NULL for empty netgroup 483 * fields. It's up to user programs 484 * to handle the NULLs appropriately. 485 */ 486 grp->ng_str[strpos] = NULL; 487 } 488 } 489 #ifdef DEBUG 490 /* 491 * Note: on other platforms, malformed netgroup 492 * entries are not normally flagged. While we 493 * can catch bad entries and report them, we should 494 * stay silent by default for compatibility's sake. 495 */ 496 if (fields < 3) 497 fprintf(stderr, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n", 498 grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST], 499 grp->ng_str[NG_USER] == NULL ? "" : ",", 500 grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER], 501 grp->ng_str[NG_DOM] == NULL ? "" : ",", 502 grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM], 503 lp->l_groupname); 504 #endif 505 } else { 506 spos = strsep(&pos, ", \t"); 507 if (parse_netgrp(spos)) 508 continue; 509 } 510 if (pos == NULL) 511 break; 512 while (*pos == ' ' || *pos == ',' || *pos == '\t') 513 pos++; 514 } 515 return (0); 516 } 517 518 /* 519 * Read the netgroup file and save lines until the line for the netgroup 520 * is found. Return 1 if eof is encountered. 521 */ 522 static struct linelist * 523 read_for_group(group) 524 char *group; 525 { 526 char *pos, *spos, *linep, *olinep; 527 int len, olen; 528 int cont; 529 struct linelist *lp; 530 char line[LINSIZ + 2]; 531 #ifdef YP 532 char *result; 533 int resultlen; 534 535 while (_netgr_yp_enabled || fgets(line, LINSIZ, netf) != NULL) { 536 if (_netgr_yp_enabled) { 537 if(!_netgr_yp_domain) 538 if(yp_get_default_domain(&_netgr_yp_domain)) 539 continue; 540 if (yp_match(_netgr_yp_domain, "netgroup", group, 541 strlen(group), &result, &resultlen)) { 542 free(result); 543 if (_use_only_yp) 544 return ((struct linelist *)0); 545 else { 546 _netgr_yp_enabled = 0; 547 continue; 548 } 549 } 550 snprintf(line, LINSIZ, "%s %s", group, result); 551 free(result); 552 } 553 #else 554 while (fgets(line, LINSIZ, netf) != NULL) { 555 #endif 556 pos = (char *)&line; 557 #ifdef YP 558 if (*pos == '+') { 559 _netgr_yp_enabled = 1; 560 continue; 561 } 562 #endif 563 if (*pos == '#') 564 continue; 565 while (*pos == ' ' || *pos == '\t') 566 pos++; 567 spos = pos; 568 while (*pos != ' ' && *pos != '\t' && *pos != '\n' && 569 *pos != '\0') 570 pos++; 571 len = pos - spos; 572 while (*pos == ' ' || *pos == '\t') 573 pos++; 574 if (*pos != '\n' && *pos != '\0') { 575 lp = (struct linelist *)malloc(sizeof (*lp)); 576 lp->l_parsed = 0; 577 lp->l_groupname = (char *)malloc(len + 1); 578 bcopy(spos, lp->l_groupname, len); 579 *(lp->l_groupname + len) = '\0'; 580 len = strlen(pos); 581 olen = 0; 582 583 /* 584 * Loop around handling line continuations. 585 */ 586 do { 587 if (*(pos + len - 1) == '\n') 588 len--; 589 if (*(pos + len - 1) == '\\') { 590 len--; 591 cont = 1; 592 } else 593 cont = 0; 594 if (len > 0) { 595 linep = (char *)malloc(olen + len + 1); 596 if (olen > 0) { 597 bcopy(olinep, linep, olen); 598 free(olinep); 599 } 600 bcopy(pos, linep + olen, len); 601 olen += len; 602 *(linep + olen) = '\0'; 603 olinep = linep; 604 } 605 if (cont) { 606 if (fgets(line, LINSIZ, netf)) { 607 pos = line; 608 len = strlen(pos); 609 } else 610 cont = 0; 611 } 612 } while (cont); 613 lp->l_line = linep; 614 lp->l_next = linehead; 615 linehead = lp; 616 617 /* 618 * If this is the one we wanted, we are done. 619 */ 620 if (!strcmp(lp->l_groupname, group)) 621 return (lp); 622 } 623 } 624 #ifdef YP 625 /* 626 * Yucky. The recursive nature of this whole mess might require 627 * us to make more than one pass through the netgroup file. 628 * This might be best left outside the #ifdef YP, but YP is 629 * defined by default anyway, so I'll leave it like this 630 * until I know better. 631 */ 632 rewind(netf); 633 #endif 634 return ((struct linelist *)0); 635 } 636