getnetgrent.c revision 90041
11573Srgrimes/* 21573Srgrimes * Copyright (c) 1992, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software contributed to Berkeley by 61573Srgrimes * Rick Macklem at The University of Guelph. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 3. All advertising materials mentioning features or use of this software 171573Srgrimes * must display the following acknowledgement: 181573Srgrimes * This product includes software developed by the University of 191573Srgrimes * California, Berkeley and its contributors. 201573Srgrimes * 4. Neither the name of the University nor the names of its contributors 211573Srgrimes * may be used to endorse or promote products derived from this software 221573Srgrimes * without specific prior written permission. 231573Srgrimes * 241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341573Srgrimes * SUCH DAMAGE. 351573Srgrimes */ 361573Srgrimes 371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 3823668Speterstatic char sccsid[] = "@(#)getnetgrent.c 8.2 (Berkeley) 4/27/95"; 391573Srgrimes#endif /* LIBC_SCCS and not lint */ 4090041Sobrien#include <sys/cdefs.h> 4190041Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/getnetgrent.c 90041 2002-02-01 01:08:48Z obrien $"); 421573Srgrimes 431573Srgrimes#include <stdio.h> 441573Srgrimes#include <strings.h> 457615Swpaul#include <stdlib.h> 467615Swpaul#include <unistd.h> 471573Srgrimes 487615Swpaul#ifdef YP 499978Swpaul/* 509978Swpaul * Notes: 519978Swpaul * We want to be able to use NIS netgroups properly while retaining 529978Swpaul * the ability to use a local /etc/netgroup file. Unfortunately, you 539978Swpaul * can't really do both at the same time - at least, not efficiently. 549978Swpaul * NetBSD deals with this problem by creating a netgroup database 559978Swpaul * using Berkeley DB (just like the password database) that allows 569978Swpaul * for lookups using netgroup, netgroup.byuser or netgroup.byhost 579978Swpaul * searches. This is a neat idea, but I don't have time to implement 589978Swpaul * something like that now. (I think ultimately it would be nice 599978Swpaul * if we DB-fied the group and netgroup stuff all in one shot, but 609978Swpaul * for now I'm satisfied just to have something that works well 619978Swpaul * without requiring massive code changes.) 629978Swpaul * 639978Swpaul * Therefore, to still permit the use of the local file and maintain 649978Swpaul * optimum NIS performance, we allow for the following conditions: 659978Swpaul * 669978Swpaul * - If /etc/netgroup does not exist and NIS is turned on, we use 679978Swpaul * NIS netgroups only. 689978Swpaul * 699978Swpaul * - If /etc/netgroup exists but is empty, we use NIS netgroups 709978Swpaul * only. 719978Swpaul * 729978Swpaul * - If /etc/netgroup exists and contains _only_ a '+', we use 739978Swpaul * NIS netgroups only. 749978Swpaul * 759978Swpaul * - If /etc/netgroup exists, contains locally defined netgroups 769978Swpaul * and a '+', we use a mixture of NIS and the local entries. 779978Swpaul * This method should return the same NIS data as just using 789978Swpaul * NIS alone, but it will be slower if the NIS netgroup database 799978Swpaul * is large (innetgr() in particular will suffer since extra 809978Swpaul * processing has to be done in order to determine memberships 819978Swpaul * using just the raw netgroup data). 829978Swpaul * 839978Swpaul * - If /etc/netgroup exists and contains only locally defined 849978Swpaul * netgroup entries, we use just those local entries and ignore 859978Swpaul * NIS (this is the original, pre-NIS behavior). 869978Swpaul */ 8730288Swpaul 887615Swpaul#include <rpc/rpc.h> 897615Swpaul#include <rpcsvc/yp_prot.h> 907615Swpaul#include <rpcsvc/ypclnt.h> 919978Swpaul#include <sys/types.h> 929978Swpaul#include <sys/stat.h> 939978Swpaul#include <sys/param.h> 949978Swpaul#include <sys/errno.h> 959978Swpaulstatic char *_netgr_yp_domain; 9610521Swpaulint _use_only_yp; 977615Swpaulstatic int _netgr_yp_enabled; 989978Swpaulstatic int _yp_innetgr; 997615Swpaul#endif 1007615Swpaul 1019978Swpaul#ifndef _PATH_NETGROUP 1021573Srgrimes#define _PATH_NETGROUP "/etc/netgroup" 1039978Swpaul#endif 1041573Srgrimes 1051573Srgrimes/* 1061573Srgrimes * Static Variables and functions used by setnetgrent(), getnetgrent() and 1071573Srgrimes * endnetgrent(). 1081573Srgrimes * There are two linked lists: 1091573Srgrimes * - linelist is just used by setnetgrent() to parse the net group file via. 1101573Srgrimes * parse_netgrp() 1111573Srgrimes * - netgrp is the list of entries for the current netgroup 1121573Srgrimes */ 1131573Srgrimesstruct linelist { 1141573Srgrimes struct linelist *l_next; /* Chain ptr. */ 1151573Srgrimes int l_parsed; /* Flag for cycles */ 1161573Srgrimes char *l_groupname; /* Name of netgroup */ 1171573Srgrimes char *l_line; /* Netgroup entrie(s) to be parsed */ 1181573Srgrimes}; 1191573Srgrimes 1201573Srgrimesstruct netgrp { 1211573Srgrimes struct netgrp *ng_next; /* Chain ptr */ 1221573Srgrimes char *ng_str[3]; /* Field pointers, see below */ 1231573Srgrimes}; 1241573Srgrimes#define NG_HOST 0 /* Host name */ 1251573Srgrimes#define NG_USER 1 /* User name */ 1261573Srgrimes#define NG_DOM 2 /* and Domain name */ 1271573Srgrimes 1281573Srgrimesstatic struct linelist *linehead = (struct linelist *)0; 1291573Srgrimesstatic struct netgrp *nextgrp = (struct netgrp *)0; 1301573Srgrimesstatic struct { 1311573Srgrimes struct netgrp *gr; 1321573Srgrimes char *grname; 1331573Srgrimes} grouphead = { 1341573Srgrimes (struct netgrp *)0, 1351573Srgrimes (char *)0, 1361573Srgrimes}; 1371573Srgrimesstatic FILE *netf = (FILE *)0; 1381573Srgrimesstatic int parse_netgrp(); 1391573Srgrimesstatic struct linelist *read_for_group(); 1401573Srgrimesvoid setnetgrent(), endnetgrent(); 1411573Srgrimesint getnetgrent(), innetgr(); 1421573Srgrimes 1431573Srgrimes#define LINSIZ 1024 /* Length of netgroup file line */ 1441573Srgrimes 1451573Srgrimes/* 1461573Srgrimes * setnetgrent() 1471573Srgrimes * Parse the netgroup file looking for the netgroup and build the list 1481573Srgrimes * of netgrp structures. Let parse_netgrp() and read_for_group() do 1491573Srgrimes * most of the work. 1501573Srgrimes */ 1511573Srgrimesvoid 1521573Srgrimessetnetgrent(group) 1531573Srgrimes char *group; 1541573Srgrimes{ 1559978Swpaul#ifdef YP 1569978Swpaul struct stat _yp_statp; 1579978Swpaul char _yp_plus; 1589978Swpaul#endif 1599978Swpaul 1607336Swpaul /* Sanity check */ 1617289Swpaul 1627336Swpaul if (group == NULL || !strlen(group)) 1637289Swpaul return; 1647336Swpaul 1651573Srgrimes if (grouphead.gr == (struct netgrp *)0 || 1661573Srgrimes strcmp(group, grouphead.grname)) { 1671573Srgrimes endnetgrent(); 1689978Swpaul#ifdef YP 16910521Swpaul /* Presumed guilty until proven innocent. */ 17010521Swpaul _use_only_yp = 0; 1719978Swpaul /* 17215264Swpaul * If /etc/netgroup doesn't exist or is empty, 1739978Swpaul * use NIS exclusively. 1749978Swpaul */ 1759978Swpaul if (((stat(_PATH_NETGROUP, &_yp_statp) < 0) && 1769978Swpaul errno == ENOENT) || _yp_statp.st_size == 0) 1779978Swpaul _use_only_yp = _netgr_yp_enabled = 1; 1789978Swpaul if ((netf = fopen(_PATH_NETGROUP,"r")) != NULL ||_use_only_yp){ 1799978Swpaul /* 1809978Swpaul * Icky: grab the first character of the netgroup file 1819978Swpaul * and turn on NIS if it's a '+'. rewind the stream 1829978Swpaul * afterwards so we don't goof up read_for_group() later. 1839978Swpaul */ 1849978Swpaul if (netf) { 1859978Swpaul fscanf(netf, "%c", &_yp_plus); 1869978Swpaul rewind(netf); 1879978Swpaul if (_yp_plus == '+') 1889978Swpaul _use_only_yp = _netgr_yp_enabled = 1; 1899978Swpaul } 1909978Swpaul /* 1919978Swpaul * If we were called specifically for an innetgr() 1929978Swpaul * lookup and we're in NIS-only mode, short-circuit 1939978Swpaul * parse_netgroup() and cut directly to the chase. 1949978Swpaul */ 19510521Swpaul if (_use_only_yp && _yp_innetgr) { 19610521Swpaul /* dohw! */ 19712585Swpaul if (netf != NULL) 19812585Swpaul fclose(netf); 1999978Swpaul return; 20010521Swpaul } 2019978Swpaul#else 2021573Srgrimes if (netf = fopen(_PATH_NETGROUP, "r")) { 2039978Swpaul#endif 2041573Srgrimes if (parse_netgrp(group)) 2051573Srgrimes endnetgrent(); 2061573Srgrimes else { 2071573Srgrimes grouphead.grname = (char *) 2081573Srgrimes malloc(strlen(group) + 1); 2091573Srgrimes strcpy(grouphead.grname, group); 2101573Srgrimes } 2119978Swpaul if (netf) 2129978Swpaul fclose(netf); 2131573Srgrimes } 2141573Srgrimes } 2151573Srgrimes nextgrp = grouphead.gr; 2161573Srgrimes} 2171573Srgrimes 2181573Srgrimes/* 2191573Srgrimes * Get the next netgroup off the list. 2201573Srgrimes */ 2211573Srgrimesint 2221573Srgrimesgetnetgrent(hostp, userp, domp) 2231573Srgrimes char **hostp, **userp, **domp; 2241573Srgrimes{ 2259978Swpaul#ifdef YP 2269978Swpaul _yp_innetgr = 0; 2279978Swpaul#endif 2281573Srgrimes 2291573Srgrimes if (nextgrp) { 2301573Srgrimes *hostp = nextgrp->ng_str[NG_HOST]; 2311573Srgrimes *userp = nextgrp->ng_str[NG_USER]; 2321573Srgrimes *domp = nextgrp->ng_str[NG_DOM]; 2331573Srgrimes nextgrp = nextgrp->ng_next; 2341573Srgrimes return (1); 2351573Srgrimes } 2361573Srgrimes return (0); 2371573Srgrimes} 2381573Srgrimes 2391573Srgrimes/* 2401573Srgrimes * endnetgrent() - cleanup 2411573Srgrimes */ 2421573Srgrimesvoid 2431573Srgrimesendnetgrent() 2441573Srgrimes{ 24590041Sobrien struct linelist *lp, *olp; 24690041Sobrien struct netgrp *gp, *ogp; 2471573Srgrimes 2481573Srgrimes lp = linehead; 2491573Srgrimes while (lp) { 2501573Srgrimes olp = lp; 2511573Srgrimes lp = lp->l_next; 2521573Srgrimes free(olp->l_groupname); 2531573Srgrimes free(olp->l_line); 2541573Srgrimes free((char *)olp); 2551573Srgrimes } 2561573Srgrimes linehead = (struct linelist *)0; 2571573Srgrimes if (grouphead.grname) { 2581573Srgrimes free(grouphead.grname); 2591573Srgrimes grouphead.grname = (char *)0; 2601573Srgrimes } 2611573Srgrimes gp = grouphead.gr; 2621573Srgrimes while (gp) { 2631573Srgrimes ogp = gp; 2641573Srgrimes gp = gp->ng_next; 2651573Srgrimes if (ogp->ng_str[NG_HOST]) 2661573Srgrimes free(ogp->ng_str[NG_HOST]); 2671573Srgrimes if (ogp->ng_str[NG_USER]) 2681573Srgrimes free(ogp->ng_str[NG_USER]); 2691573Srgrimes if (ogp->ng_str[NG_DOM]) 2701573Srgrimes free(ogp->ng_str[NG_DOM]); 2711573Srgrimes free((char *)ogp); 2721573Srgrimes } 2731573Srgrimes grouphead.gr = (struct netgrp *)0; 2747149Swpaul#ifdef YP 2757223Swpaul _netgr_yp_enabled = 0; 2767149Swpaul#endif 2771573Srgrimes} 2781573Srgrimes 2799978Swpaul#ifdef YP 2809998Swpaulstatic int _listmatch(list, group, len) 28131180Swpaul char *list, *group; 28231180Swpaul int len; 2839978Swpaul{ 28431180Swpaul char *ptr = list, *cptr; 28531180Swpaul int glen = strlen(group); 2869978Swpaul 28731180Swpaul /* skip possible leading whitespace */ 28852856Sache while(isspace((unsigned char)*ptr)) 28931180Swpaul ptr++; 29015264Swpaul 29133950Ssteve while (ptr < list + len) { 29233950Ssteve cptr = ptr; 29352856Sache while(*ptr != ',' && *ptr != '\0' && !isspace((unsigned char)*ptr)) 29433950Ssteve ptr++; 29533950Ssteve if (strncmp(cptr, group, glen) == 0 && glen == (ptr - cptr)) 29615839Swpaul return(1); 29752856Sache while(*ptr == ',' || isspace((unsigned char)*ptr)) 29833950Ssteve ptr++; 29915839Swpaul } 30015839Swpaul 3019978Swpaul return(0); 3029978Swpaul} 3039978Swpaul 3049978Swpaulstatic int _buildkey(key, str, dom, rotation) 3059978Swpaulchar *key, *str, *dom; 3069978Swpaulint *rotation; 3079978Swpaul{ 3089978Swpaul (*rotation)++; 3099978Swpaul if (*rotation > 4) 3109978Swpaul return(0); 3119978Swpaul switch(*rotation) { 3129978Swpaul case(1): sprintf((char *)key, "%s.%s", str, dom ? dom : "*"); 3139978Swpaul break; 3149978Swpaul case(2): sprintf((char *)key, "%s.*", str); 3159978Swpaul break; 3169978Swpaul case(3): sprintf((char *)key, "*.%s", dom ? dom : "*"); 3179978Swpaul break; 3189978Swpaul case(4): sprintf((char *)key, "*.*"); 3199978Swpaul break; 3209978Swpaul } 3219978Swpaul return(1); 3229978Swpaul} 3239978Swpaul#endif 3249978Swpaul 3251573Srgrimes/* 3261573Srgrimes * Search for a match in a netgroup. 3271573Srgrimes */ 3281573Srgrimesint 3291573Srgrimesinnetgr(group, host, user, dom) 33017141Sjkh const char *group, *host, *user, *dom; 3311573Srgrimes{ 3321573Srgrimes char *hst, *usr, *dm; 3339978Swpaul#ifdef YP 3349978Swpaul char *result; 3359978Swpaul int resultlen; 33615264Swpaul int rv; 3379978Swpaul#endif 3387336Swpaul /* Sanity check */ 3399287Swpaul 3407336Swpaul if (group == NULL || !strlen(group)) 3417336Swpaul return (0); 3427336Swpaul 3439978Swpaul#ifdef YP 3449978Swpaul _yp_innetgr = 1; 3459978Swpaul#endif 3461573Srgrimes setnetgrent(group); 3479978Swpaul#ifdef YP 34815264Swpaul _yp_innetgr = 0; 3499978Swpaul /* 3509978Swpaul * If we're in NIS-only mode, do the search using 3519978Swpaul * NIS 'reverse netgroup' lookups. 3529978Swpaul */ 3539978Swpaul if (_use_only_yp) { 3549978Swpaul char _key[MAXHOSTNAMELEN]; 35530390Swpaul int rot = 0, y = 0; 3569978Swpaul 3579978Swpaul if(yp_get_default_domain(&_netgr_yp_domain)) 3589978Swpaul return(0); 35920161Sjkh while(_buildkey(_key, user ? user : host, dom, &rot)) { 36030390Swpaul y = yp_match(_netgr_yp_domain, user? "netgroup.byuser": 3619978Swpaul "netgroup.byhost", _key, strlen(_key), &result, 36230390Swpaul &resultlen); 36330390Swpaul if (y) { 36430390Swpaul /* 36530390Swpaul * If we get an error other than 'no 36630390Swpaul * such key in map' then something is 36730390Swpaul * wrong and we should stop the search. 36830390Swpaul */ 36930390Swpaul if (y != YPERR_KEY) 37030390Swpaul break; 37130390Swpaul } else { 37215264Swpaul rv = _listmatch(result, group, resultlen); 37315264Swpaul free(result); 37415264Swpaul if (rv) 3759978Swpaul return(1); 37615264Swpaul else 37715264Swpaul return(0); 3789978Swpaul } 3799978Swpaul } 38030390Swpaul /* 38130390Swpaul * Couldn't match using NIS-exclusive mode. If the error 38230390Swpaul * was YPERR_MAP, then the failure happened because there 38330390Swpaul * was no netgroup.byhost or netgroup.byuser map. The odds 38430390Swpaul * are we are talking to an Sun NIS+ server in YP emulation 38530390Swpaul * mode; if this is the case, then we have to do the check 38630390Swpaul * the 'old-fashioned' way by grovelling through the netgroup 38730390Swpaul * map and resolving memberships on the fly. 38830390Swpaul */ 38930390Swpaul if (y != YPERR_MAP) 39030390Swpaul return(0); 3919978Swpaul } 39230390Swpaul 3939978Swpaul setnetgrent(group); 3949978Swpaul#endif /* YP */ 39530390Swpaul 3961573Srgrimes while (getnetgrent(&hst, &usr, &dm)) 3979978Swpaul if ((host == NULL || hst == NULL || !strcmp(host, hst)) && 3989978Swpaul (user == NULL || usr == NULL || !strcmp(user, usr)) && 3999978Swpaul ( dom == NULL || dm == NULL || !strcmp(dom, dm))) { 4001573Srgrimes endnetgrent(); 4011573Srgrimes return (1); 4021573Srgrimes } 4031573Srgrimes endnetgrent(); 4041573Srgrimes return (0); 4051573Srgrimes} 4061573Srgrimes 4071573Srgrimes/* 4081573Srgrimes * Parse the netgroup file setting up the linked lists. 4091573Srgrimes */ 4101573Srgrimesstatic int 4111573Srgrimesparse_netgrp(group) 4121573Srgrimes char *group; 4131573Srgrimes{ 41490041Sobrien char *spos, *epos; 41590041Sobrien int len, strpos; 4169287Swpaul#ifdef DEBUG 41790041Sobrien int fields; 4189287Swpaul#endif 4191573Srgrimes char *pos, *gpos; 4201573Srgrimes struct netgrp *grp; 4211573Srgrimes struct linelist *lp = linehead; 4221573Srgrimes 4231573Srgrimes /* 4241573Srgrimes * First, see if the line has already been read in. 4251573Srgrimes */ 4261573Srgrimes while (lp) { 4271573Srgrimes if (!strcmp(group, lp->l_groupname)) 4281573Srgrimes break; 4291573Srgrimes lp = lp->l_next; 4301573Srgrimes } 4311573Srgrimes if (lp == (struct linelist *)0 && 4321573Srgrimes (lp = read_for_group(group)) == (struct linelist *)0) 4331573Srgrimes return (1); 4341573Srgrimes if (lp->l_parsed) { 4359287Swpaul#ifdef DEBUG 4369287Swpaul /* 4379287Swpaul * This error message is largely superflous since the 4389287Swpaul * code handles the error condition sucessfully, and 4399287Swpaul * spewing it out from inside libc can actually hose 4409287Swpaul * certain programs. 4419287Swpaul */ 4421573Srgrimes fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname); 4439287Swpaul#endif 4441573Srgrimes return (1); 4451573Srgrimes } else 4461573Srgrimes lp->l_parsed = 1; 4471573Srgrimes pos = lp->l_line; 4487149Swpaul /* Watch for null pointer dereferences, dammit! */ 4497149Swpaul while (pos != NULL && *pos != '\0') { 4501573Srgrimes if (*pos == '(') { 4511573Srgrimes grp = (struct netgrp *)malloc(sizeof (struct netgrp)); 4521573Srgrimes bzero((char *)grp, sizeof (struct netgrp)); 4531573Srgrimes grp->ng_next = grouphead.gr; 4541573Srgrimes grouphead.gr = grp; 4551573Srgrimes pos++; 4561573Srgrimes gpos = strsep(&pos, ")"); 4579287Swpaul#ifdef DEBUG 4589287Swpaul fields = 0; 4599287Swpaul#endif 4601573Srgrimes for (strpos = 0; strpos < 3; strpos++) { 4619978Swpaul if ((spos = strsep(&gpos, ","))) { 4629287Swpaul#ifdef DEBUG 4639287Swpaul fields++; 4649287Swpaul#endif 4651573Srgrimes while (*spos == ' ' || *spos == '\t') 4661573Srgrimes spos++; 4679978Swpaul if ((epos = strpbrk(spos, " \t"))) { 4681573Srgrimes *epos = '\0'; 4691573Srgrimes len = epos - spos; 4701573Srgrimes } else 4711573Srgrimes len = strlen(spos); 4721573Srgrimes if (len > 0) { 4731573Srgrimes grp->ng_str[strpos] = (char *) 4741573Srgrimes malloc(len + 1); 4751573Srgrimes bcopy(spos, grp->ng_str[strpos], 4761573Srgrimes len + 1); 4771573Srgrimes } 4789287Swpaul } else { 4799287Swpaul /* 4809287Swpaul * All other systems I've tested 4819287Swpaul * return NULL for empty netgroup 4829287Swpaul * fields. It's up to user programs 4839287Swpaul * to handle the NULLs appropriately. 4849287Swpaul */ 4859287Swpaul grp->ng_str[strpos] = NULL; 4869287Swpaul } 4871573Srgrimes } 4889287Swpaul#ifdef DEBUG 4899287Swpaul /* 4909287Swpaul * Note: on other platforms, malformed netgroup 4919287Swpaul * entries are not normally flagged. While we 4929287Swpaul * can catch bad entries and report them, we should 4939287Swpaul * stay silent by default for compatibility's sake. 4949287Swpaul */ 4959287Swpaul if (fields < 3) 4969287Swpaul fprintf(stderr, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n", 4979287Swpaul grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST], 4989287Swpaul grp->ng_str[NG_USER] == NULL ? "" : ",", 4999287Swpaul grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER], 5009287Swpaul grp->ng_str[NG_DOM] == NULL ? "" : ",", 5019287Swpaul grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM], 5029287Swpaul lp->l_groupname); 5039287Swpaul#endif 5041573Srgrimes } else { 5051573Srgrimes spos = strsep(&pos, ", \t"); 5061573Srgrimes if (parse_netgrp(spos)) 5077175Swpaul continue; 5081573Srgrimes } 50923668Speter if (pos == NULL) 51023668Speter break; 51123668Speter while (*pos == ' ' || *pos == ',' || *pos == '\t') 51223668Speter pos++; 5131573Srgrimes } 5141573Srgrimes return (0); 5151573Srgrimes} 5161573Srgrimes 5171573Srgrimes/* 5181573Srgrimes * Read the netgroup file and save lines until the line for the netgroup 5191573Srgrimes * is found. Return 1 if eof is encountered. 5201573Srgrimes */ 5211573Srgrimesstatic struct linelist * 5221573Srgrimesread_for_group(group) 5231573Srgrimes char *group; 5241573Srgrimes{ 52590041Sobrien char *pos, *spos, *linep, *olinep; 52690041Sobrien int len, olen; 5271573Srgrimes int cont; 5281573Srgrimes struct linelist *lp; 52920957Swpaul char line[LINSIZ + 2]; 5307149Swpaul#ifdef YP 5317149Swpaul char *result; 5327149Swpaul int resultlen; 5331573Srgrimes 5347149Swpaul while (_netgr_yp_enabled || fgets(line, LINSIZ, netf) != NULL) { 5357149Swpaul if (_netgr_yp_enabled) { 5367149Swpaul if(!_netgr_yp_domain) 5377223Swpaul if(yp_get_default_domain(&_netgr_yp_domain)) 5387149Swpaul continue; 5397223Swpaul if (yp_match(_netgr_yp_domain, "netgroup", group, 5407223Swpaul strlen(group), &result, &resultlen)) { 5417223Swpaul free(result); 5429978Swpaul if (_use_only_yp) 5439978Swpaul return ((struct linelist *)0); 5449978Swpaul else { 5459978Swpaul _netgr_yp_enabled = 0; 5469978Swpaul continue; 5479978Swpaul } 5487149Swpaul } 54920957Swpaul snprintf(line, LINSIZ, "%s %s", group, result); 5507149Swpaul free(result); 5517149Swpaul } 5527149Swpaul#else 5531573Srgrimes while (fgets(line, LINSIZ, netf) != NULL) { 5547149Swpaul#endif 5557149Swpaul pos = (char *)&line; 5567149Swpaul#ifdef YP 5577149Swpaul if (*pos == '+') { 5587149Swpaul _netgr_yp_enabled = 1; 5597149Swpaul continue; 5607149Swpaul } 5617149Swpaul#endif 5621573Srgrimes if (*pos == '#') 5631573Srgrimes continue; 5641573Srgrimes while (*pos == ' ' || *pos == '\t') 5651573Srgrimes pos++; 5661573Srgrimes spos = pos; 5671573Srgrimes while (*pos != ' ' && *pos != '\t' && *pos != '\n' && 5681573Srgrimes *pos != '\0') 5691573Srgrimes pos++; 5701573Srgrimes len = pos - spos; 5711573Srgrimes while (*pos == ' ' || *pos == '\t') 5721573Srgrimes pos++; 5731573Srgrimes if (*pos != '\n' && *pos != '\0') { 5741573Srgrimes lp = (struct linelist *)malloc(sizeof (*lp)); 5751573Srgrimes lp->l_parsed = 0; 5761573Srgrimes lp->l_groupname = (char *)malloc(len + 1); 5771573Srgrimes bcopy(spos, lp->l_groupname, len); 5781573Srgrimes *(lp->l_groupname + len) = '\0'; 5791573Srgrimes len = strlen(pos); 5801573Srgrimes olen = 0; 5811573Srgrimes 5821573Srgrimes /* 5831573Srgrimes * Loop around handling line continuations. 5841573Srgrimes */ 5851573Srgrimes do { 5861573Srgrimes if (*(pos + len - 1) == '\n') 5871573Srgrimes len--; 5881573Srgrimes if (*(pos + len - 1) == '\\') { 5891573Srgrimes len--; 5901573Srgrimes cont = 1; 5911573Srgrimes } else 5921573Srgrimes cont = 0; 5931573Srgrimes if (len > 0) { 5941573Srgrimes linep = (char *)malloc(olen + len + 1); 5951573Srgrimes if (olen > 0) { 5961573Srgrimes bcopy(olinep, linep, olen); 5971573Srgrimes free(olinep); 5981573Srgrimes } 5991573Srgrimes bcopy(pos, linep + olen, len); 6001573Srgrimes olen += len; 6011573Srgrimes *(linep + olen) = '\0'; 6021573Srgrimes olinep = linep; 6031573Srgrimes } 6041573Srgrimes if (cont) { 6051573Srgrimes if (fgets(line, LINSIZ, netf)) { 6061573Srgrimes pos = line; 6071573Srgrimes len = strlen(pos); 6081573Srgrimes } else 6091573Srgrimes cont = 0; 6101573Srgrimes } 6111573Srgrimes } while (cont); 6121573Srgrimes lp->l_line = linep; 6131573Srgrimes lp->l_next = linehead; 6141573Srgrimes linehead = lp; 6151573Srgrimes 6161573Srgrimes /* 6171573Srgrimes * If this is the one we wanted, we are done. 6181573Srgrimes */ 6191573Srgrimes if (!strcmp(lp->l_groupname, group)) 6201573Srgrimes return (lp); 6211573Srgrimes } 6221573Srgrimes } 6239978Swpaul#ifdef YP 6249978Swpaul /* 6259978Swpaul * Yucky. The recursive nature of this whole mess might require 6269978Swpaul * us to make more than one pass through the netgroup file. 6279978Swpaul * This might be best left outside the #ifdef YP, but YP is 6289978Swpaul * defined by default anyway, so I'll leave it like this 6299978Swpaul * until I know better. 6309978Swpaul */ 6319978Swpaul rewind(netf); 6329978Swpaul#endif 6331573Srgrimes return ((struct linelist *)0); 6341573Srgrimes} 635