17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
99525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /* Imports */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "port_before.h"
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <sys/types.h>
237c478bd9Sstevel@tonic-gate #include <netinet/in.h>
247c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
257c478bd9Sstevel@tonic-gate #include <resolv.h>
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <irs.h>
337c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "port_after.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "irs_p.h"
387c478bd9Sstevel@tonic-gate #include "lcl_p.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* Definitions */
417c478bd9Sstevel@tonic-gate 
429525b14bSRao Shoaib #define NG_HOST         0       /*%< Host name */
439525b14bSRao Shoaib #define NG_USER         1       /*%< User name */
449525b14bSRao Shoaib #define NG_DOM          2       /*%< and Domain name */
459525b14bSRao Shoaib #define LINSIZ		1024    /*%< Length of netgroup file line */
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * XXX Warning XXX
487c478bd9Sstevel@tonic-gate  * This code is a hack-and-slash special.  It realy needs to be
497c478bd9Sstevel@tonic-gate  * rewritten with things like strdup, and realloc in mind.
507c478bd9Sstevel@tonic-gate  * More reasonable data structures would not be a bad thing.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
539525b14bSRao Shoaib /*%
547c478bd9Sstevel@tonic-gate  * Static Variables and functions used by setnetgrent(), getnetgrent() and
557c478bd9Sstevel@tonic-gate  * endnetgrent().
569525b14bSRao Shoaib  *
577c478bd9Sstevel@tonic-gate  * There are two linked lists:
589525b14bSRao Shoaib  * \li linelist is just used by setnetgrent() to parse the net group file via.
597c478bd9Sstevel@tonic-gate  *   parse_netgrp()
609525b14bSRao Shoaib  * \li netgrp is the list of entries for the current netgroup
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate struct linelist {
639525b14bSRao Shoaib 	struct linelist *l_next;	/*%< Chain ptr. */
649525b14bSRao Shoaib 	int		l_parsed;	/*%< Flag for cycles */
659525b14bSRao Shoaib 	char *		l_groupname;	/*%< Name of netgroup */
669525b14bSRao Shoaib 	char *		l_line;		/*%< Netgroup entrie(s) to be parsed */
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate struct ng_old_struct {
709525b14bSRao Shoaib 	struct ng_old_struct *ng_next;	/*%< Chain ptr */
719525b14bSRao Shoaib 	char *		ng_str[3];	/*%< Field pointers, see below */
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate struct pvt {
757c478bd9Sstevel@tonic-gate 	FILE			*fp;
767c478bd9Sstevel@tonic-gate 	struct linelist		*linehead;
777c478bd9Sstevel@tonic-gate 	struct ng_old_struct    *nextgrp;
787c478bd9Sstevel@tonic-gate 	struct {
797c478bd9Sstevel@tonic-gate 		struct ng_old_struct	*gr;
807c478bd9Sstevel@tonic-gate 		char			*grname;
817c478bd9Sstevel@tonic-gate 	} grouphead;
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* Forward */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static void 		ng_rewind(struct irs_ng *, const char*);
877c478bd9Sstevel@tonic-gate static void 		ng_close(struct irs_ng *);
887c478bd9Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **,
897c478bd9Sstevel@tonic-gate 				const char **, const char **);
907c478bd9Sstevel@tonic-gate static int 		ng_test(struct irs_ng *, const char *,
917c478bd9Sstevel@tonic-gate 				const char *, const char *,
927c478bd9Sstevel@tonic-gate 				const char *);
937c478bd9Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static int 		parse_netgrp(struct irs_ng *, const char*);
967c478bd9Sstevel@tonic-gate static struct linelist *read_for_group(struct irs_ng *, const char *);
977c478bd9Sstevel@tonic-gate static void		freelists(struct irs_ng *);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* Public */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate struct irs_ng *
irs_lcl_ng(struct irs_acc * this)1027c478bd9Sstevel@tonic-gate irs_lcl_ng(struct irs_acc *this) {
1037c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
1047c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	UNUSED(this);
107*55fea89dSDan Cross 
1087c478bd9Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
1097c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1107c478bd9Sstevel@tonic-gate 		return (NULL);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
1137c478bd9Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
1147c478bd9Sstevel@tonic-gate 		memput(ng, sizeof *ng);
1157c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1167c478bd9Sstevel@tonic-gate 		return (NULL);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1197c478bd9Sstevel@tonic-gate 	ng->private = pvt;
1207c478bd9Sstevel@tonic-gate 	ng->close = ng_close;
1217c478bd9Sstevel@tonic-gate 	ng->next = ng_next;
1227c478bd9Sstevel@tonic-gate 	ng->test = ng_test;
1237c478bd9Sstevel@tonic-gate 	ng->rewind = ng_rewind;
1247c478bd9Sstevel@tonic-gate 	ng->minimize = ng_minimize;
1257c478bd9Sstevel@tonic-gate 	return (ng);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* Methods */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static void
ng_close(struct irs_ng * this)1317c478bd9Sstevel@tonic-gate ng_close(struct irs_ng *this) {
1327c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
133*55fea89dSDan Cross 
1347c478bd9Sstevel@tonic-gate 	if (pvt->fp != NULL)
1357c478bd9Sstevel@tonic-gate 		fclose(pvt->fp);
1367c478bd9Sstevel@tonic-gate 	freelists(this);
1377c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1387c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1397c478bd9Sstevel@tonic-gate }
140*55fea89dSDan Cross 
1419525b14bSRao Shoaib /*%
1427c478bd9Sstevel@tonic-gate  * Parse the netgroup file looking for the netgroup and build the list
1437c478bd9Sstevel@tonic-gate  * of netgrp structures. Let parse_netgrp() and read_for_group() do
1447c478bd9Sstevel@tonic-gate  * most of the work.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate static void
ng_rewind(struct irs_ng * this,const char * group)1477c478bd9Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
1487c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
149*55fea89dSDan Cross 
1507c478bd9Sstevel@tonic-gate 	if (pvt->fp != NULL && fseek(pvt->fp, SEEK_CUR, 0L) == -1) {
1517c478bd9Sstevel@tonic-gate 		fclose(pvt->fp);
1527c478bd9Sstevel@tonic-gate 		pvt->fp = NULL;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
155*55fea89dSDan Cross 	if (pvt->fp == NULL || pvt->grouphead.gr == NULL ||
1567c478bd9Sstevel@tonic-gate 	    strcmp(group, pvt->grouphead.grname)) {
1577c478bd9Sstevel@tonic-gate 		freelists(this);
1587c478bd9Sstevel@tonic-gate 		if (pvt->fp != NULL)
1597c478bd9Sstevel@tonic-gate 			fclose(pvt->fp);
1607c478bd9Sstevel@tonic-gate 		pvt->fp = fopen(_PATH_NETGROUP, "r");
1617c478bd9Sstevel@tonic-gate 		if (pvt->fp != NULL) {
1627c478bd9Sstevel@tonic-gate 			if (parse_netgrp(this, group))
1637c478bd9Sstevel@tonic-gate 				freelists(this);
1647c478bd9Sstevel@tonic-gate 			if (!(pvt->grouphead.grname = strdup(group)))
1657c478bd9Sstevel@tonic-gate 				freelists(this);
1667c478bd9Sstevel@tonic-gate 			fclose(pvt->fp);
1677c478bd9Sstevel@tonic-gate 			pvt->fp = NULL;
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	pvt->nextgrp = pvt->grouphead.gr;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1739525b14bSRao Shoaib /*%
1747c478bd9Sstevel@tonic-gate  * Get the next netgroup off the list.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)1777c478bd9Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
1787c478bd9Sstevel@tonic-gate 	const char **domain)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
181*55fea89dSDan Cross 
1827c478bd9Sstevel@tonic-gate 	if (pvt->nextgrp) {
1837c478bd9Sstevel@tonic-gate 		*host = pvt->nextgrp->ng_str[NG_HOST];
1847c478bd9Sstevel@tonic-gate 		*user = pvt->nextgrp->ng_str[NG_USER];
1857c478bd9Sstevel@tonic-gate 		*domain = pvt->nextgrp->ng_str[NG_DOM];
1867c478bd9Sstevel@tonic-gate 		pvt->nextgrp = pvt->nextgrp->ng_next;
1877c478bd9Sstevel@tonic-gate 		return (1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	return (0);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1929525b14bSRao Shoaib /*%
1937c478bd9Sstevel@tonic-gate  * Search for a match in a netgroup.
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate static int
ng_test(struct irs_ng * this,const char * name,const char * host,const char * user,const char * domain)1967c478bd9Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
1977c478bd9Sstevel@tonic-gate 	const char *host, const char *user, const char *domain)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	const char *ng_host, *ng_user, *ng_domain;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	ng_rewind(this, name);
2027c478bd9Sstevel@tonic-gate 	while (ng_next(this, &ng_host, &ng_user, &ng_domain))
203*55fea89dSDan Cross 		if ((host == NULL || ng_host == NULL ||
2047c478bd9Sstevel@tonic-gate 		     !strcmp(host, ng_host)) &&
205*55fea89dSDan Cross 		    (user ==  NULL || ng_user == NULL ||
2067c478bd9Sstevel@tonic-gate 		     !strcmp(user, ng_user)) &&
2077c478bd9Sstevel@tonic-gate 		    (domain == NULL || ng_domain == NULL ||
2087c478bd9Sstevel@tonic-gate 		     !strcmp(domain, ng_domain))) {
2097c478bd9Sstevel@tonic-gate 			freelists(this);
2107c478bd9Sstevel@tonic-gate 			return (1);
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 	freelists(this);
2137c478bd9Sstevel@tonic-gate 	return (0);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static void
ng_minimize(struct irs_ng * this)2177c478bd9Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
2187c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (pvt->fp != NULL) {
2217c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->fp);
2227c478bd9Sstevel@tonic-gate 		pvt->fp = NULL;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /* Private */
2277c478bd9Sstevel@tonic-gate 
2289525b14bSRao Shoaib /*%
2297c478bd9Sstevel@tonic-gate  * endnetgrent() - cleanup
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate static void
freelists(struct irs_ng * this)2327c478bd9Sstevel@tonic-gate freelists(struct irs_ng *this) {
2337c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2347c478bd9Sstevel@tonic-gate 	struct linelist *lp, *olp;
2357c478bd9Sstevel@tonic-gate 	struct ng_old_struct *gp, *ogp;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	lp = pvt->linehead;
2387c478bd9Sstevel@tonic-gate 	while (lp) {
2397c478bd9Sstevel@tonic-gate 		olp = lp;
2407c478bd9Sstevel@tonic-gate 		lp = lp->l_next;
2417c478bd9Sstevel@tonic-gate 		free(olp->l_groupname);
2427c478bd9Sstevel@tonic-gate 		free(olp->l_line);
2437c478bd9Sstevel@tonic-gate 		free((char *)olp);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	pvt->linehead = NULL;
2467c478bd9Sstevel@tonic-gate 	if (pvt->grouphead.grname) {
2477c478bd9Sstevel@tonic-gate 		free(pvt->grouphead.grname);
2487c478bd9Sstevel@tonic-gate 		pvt->grouphead.grname = NULL;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	gp = pvt->grouphead.gr;
2517c478bd9Sstevel@tonic-gate 	while (gp) {
2527c478bd9Sstevel@tonic-gate 		ogp = gp;
2537c478bd9Sstevel@tonic-gate 		gp = gp->ng_next;
2547c478bd9Sstevel@tonic-gate 		if (ogp->ng_str[NG_HOST])
2557c478bd9Sstevel@tonic-gate 			free(ogp->ng_str[NG_HOST]);
2567c478bd9Sstevel@tonic-gate 		if (ogp->ng_str[NG_USER])
2577c478bd9Sstevel@tonic-gate 			free(ogp->ng_str[NG_USER]);
2587c478bd9Sstevel@tonic-gate 		if (ogp->ng_str[NG_DOM])
2597c478bd9Sstevel@tonic-gate 			free(ogp->ng_str[NG_DOM]);
2607c478bd9Sstevel@tonic-gate 		free((char *)ogp);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	pvt->grouphead.gr = NULL;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2659525b14bSRao Shoaib /*%
2667c478bd9Sstevel@tonic-gate  * Parse the netgroup file setting up the linked lists.
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate static int
parse_netgrp(struct irs_ng * this,const char * group)2697c478bd9Sstevel@tonic-gate parse_netgrp(struct irs_ng *this, const char *group) {
2707c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2717c478bd9Sstevel@tonic-gate 	char *spos, *epos;
2727c478bd9Sstevel@tonic-gate 	int len, strpos;
2737c478bd9Sstevel@tonic-gate 	char *pos, *gpos;
2747c478bd9Sstevel@tonic-gate 	struct ng_old_struct *grp;
2757c478bd9Sstevel@tonic-gate 	struct linelist *lp = pvt->linehead;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate         /*
2787c478bd9Sstevel@tonic-gate          * First, see if the line has already been read in.
2797c478bd9Sstevel@tonic-gate          */
2807c478bd9Sstevel@tonic-gate 	while (lp) {
2817c478bd9Sstevel@tonic-gate 		if (!strcmp(group, lp->l_groupname))
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 		lp = lp->l_next;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	if (lp == NULL &&
2867c478bd9Sstevel@tonic-gate 	    (lp = read_for_group(this, group)) == NULL)
2877c478bd9Sstevel@tonic-gate 		return (1);
2887c478bd9Sstevel@tonic-gate 	if (lp->l_parsed) {
2897c478bd9Sstevel@tonic-gate 		/*fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);*/
2907c478bd9Sstevel@tonic-gate 		return (1);
2917c478bd9Sstevel@tonic-gate 	} else
2927c478bd9Sstevel@tonic-gate 		lp->l_parsed = 1;
2937c478bd9Sstevel@tonic-gate 	pos = lp->l_line;
2947c478bd9Sstevel@tonic-gate 	while (*pos != '\0') {
2957c478bd9Sstevel@tonic-gate 		if (*pos == '(') {
2967c478bd9Sstevel@tonic-gate 			if (!(grp = malloc(sizeof (struct ng_old_struct)))) {
2977c478bd9Sstevel@tonic-gate 				freelists(this);
2987c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
2997c478bd9Sstevel@tonic-gate 				return (1);
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			memset(grp, 0, sizeof (struct ng_old_struct));
3027c478bd9Sstevel@tonic-gate 			grp->ng_next = pvt->grouphead.gr;
3037c478bd9Sstevel@tonic-gate 			pvt->grouphead.gr = grp;
3047c478bd9Sstevel@tonic-gate 			pos++;
3057c478bd9Sstevel@tonic-gate 			gpos = strsep(&pos, ")");
3067c478bd9Sstevel@tonic-gate 			for (strpos = 0; strpos < 3; strpos++) {
3077c478bd9Sstevel@tonic-gate 				if ((spos = strsep(&gpos, ","))) {
3087c478bd9Sstevel@tonic-gate 					while (*spos == ' ' || *spos == '\t')
3097c478bd9Sstevel@tonic-gate 						spos++;
3107c478bd9Sstevel@tonic-gate 					if ((epos = strpbrk(spos, " \t"))) {
3117c478bd9Sstevel@tonic-gate 						*epos = '\0';
3127c478bd9Sstevel@tonic-gate 						len = epos - spos;
3137c478bd9Sstevel@tonic-gate 					} else
3147c478bd9Sstevel@tonic-gate 						len = strlen(spos);
3157c478bd9Sstevel@tonic-gate 					if (len > 0) {
316*55fea89dSDan Cross 						if(!(grp->ng_str[strpos]
3177c478bd9Sstevel@tonic-gate 						   =  (char *)
3187c478bd9Sstevel@tonic-gate 						   malloc(len + 1))) {
3197c478bd9Sstevel@tonic-gate 							freelists(this);
3207c478bd9Sstevel@tonic-gate 							return (1);
3217c478bd9Sstevel@tonic-gate 						}
3227c478bd9Sstevel@tonic-gate 						memcpy(grp->ng_str[strpos],
3237c478bd9Sstevel@tonic-gate 						       spos,
3247c478bd9Sstevel@tonic-gate 						       len + 1);
3257c478bd9Sstevel@tonic-gate 					}
3267c478bd9Sstevel@tonic-gate 				} else
3277c478bd9Sstevel@tonic-gate 					goto errout;
3287c478bd9Sstevel@tonic-gate 			}
3297c478bd9Sstevel@tonic-gate 		} else {
3307c478bd9Sstevel@tonic-gate 			spos = strsep(&pos, ", \t");
3317c478bd9Sstevel@tonic-gate 			if (spos != NULL && parse_netgrp(this, spos)) {
3327c478bd9Sstevel@tonic-gate 				freelists(this);
3337c478bd9Sstevel@tonic-gate 				return (1);
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		if (pos == NULL)
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 		while (*pos == ' ' || *pos == ',' || *pos == '\t')
3397c478bd9Sstevel@tonic-gate 			pos++;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	return (0);
3427c478bd9Sstevel@tonic-gate  errout:
3437c478bd9Sstevel@tonic-gate 	/*fprintf(stderr, "Bad netgroup %s at ..%s\n", lp->l_groupname,
3447c478bd9Sstevel@tonic-gate 		  spos);*/
3457c478bd9Sstevel@tonic-gate 	return (1);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3489525b14bSRao Shoaib /*%
3497c478bd9Sstevel@tonic-gate  * Read the netgroup file and save lines until the line for the netgroup
3507c478bd9Sstevel@tonic-gate  * is found. Return 1 if eof is encountered.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate static struct linelist *
read_for_group(struct irs_ng * this,const char * group)3537c478bd9Sstevel@tonic-gate read_for_group(struct irs_ng *this, const char *group) {
3547c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3557c478bd9Sstevel@tonic-gate 	char *pos, *spos, *linep = NULL, *olinep;
3567c478bd9Sstevel@tonic-gate 	int len, olen, cont;
3577c478bd9Sstevel@tonic-gate 	struct linelist *lp;
3587c478bd9Sstevel@tonic-gate 	char line[LINSIZ + 1];
359*55fea89dSDan Cross 
3607c478bd9Sstevel@tonic-gate 	while (fgets(line, LINSIZ, pvt->fp) != NULL) {
3617c478bd9Sstevel@tonic-gate 		pos = line;
3627c478bd9Sstevel@tonic-gate 		if (*pos == '#')
3637c478bd9Sstevel@tonic-gate 			continue;
3647c478bd9Sstevel@tonic-gate 		while (*pos == ' ' || *pos == '\t')
3657c478bd9Sstevel@tonic-gate 			pos++;
3667c478bd9Sstevel@tonic-gate 		spos = pos;
3677c478bd9Sstevel@tonic-gate 		while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
3687c478bd9Sstevel@tonic-gate 			*pos != '\0')
3697c478bd9Sstevel@tonic-gate 			pos++;
3707c478bd9Sstevel@tonic-gate 		len = pos - spos;
3717c478bd9Sstevel@tonic-gate 		while (*pos == ' ' || *pos == '\t')
3727c478bd9Sstevel@tonic-gate 			pos++;
3737c478bd9Sstevel@tonic-gate 		if (*pos != '\n' && *pos != '\0') {
3747c478bd9Sstevel@tonic-gate 			if (!(lp = malloc(sizeof (*lp)))) {
3757c478bd9Sstevel@tonic-gate 				freelists(this);
3767c478bd9Sstevel@tonic-gate 				return (NULL);
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 			lp->l_parsed = 0;
3797c478bd9Sstevel@tonic-gate 			if (!(lp->l_groupname = malloc(len + 1))) {
3807c478bd9Sstevel@tonic-gate 				free(lp);
3817c478bd9Sstevel@tonic-gate 				freelists(this);
3827c478bd9Sstevel@tonic-gate 				return (NULL);
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 			memcpy(lp->l_groupname, spos,  len);
3857c478bd9Sstevel@tonic-gate 			*(lp->l_groupname + len) = '\0';
3867c478bd9Sstevel@tonic-gate 			len = strlen(pos);
3877c478bd9Sstevel@tonic-gate 			olen = 0;
3887c478bd9Sstevel@tonic-gate 			olinep = NULL;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 			/*
3917c478bd9Sstevel@tonic-gate 			 * Loop around handling line continuations.
3927c478bd9Sstevel@tonic-gate 			 */
3937c478bd9Sstevel@tonic-gate 			do {
3947c478bd9Sstevel@tonic-gate 				if (*(pos + len - 1) == '\n')
3957c478bd9Sstevel@tonic-gate 					len--;
3967c478bd9Sstevel@tonic-gate 				if (*(pos + len - 1) == '\\') {
3977c478bd9Sstevel@tonic-gate 					len--;
3987c478bd9Sstevel@tonic-gate 					cont = 1;
3997c478bd9Sstevel@tonic-gate 				} else
4007c478bd9Sstevel@tonic-gate 					cont = 0;
4017c478bd9Sstevel@tonic-gate 				if (len > 0) {
4027c478bd9Sstevel@tonic-gate 					if (!(linep = malloc(olen + len + 1))){
4037c478bd9Sstevel@tonic-gate 						if (olen > 0)
4047c478bd9Sstevel@tonic-gate 							free(olinep);
4057c478bd9Sstevel@tonic-gate 						free(lp->l_groupname);
4067c478bd9Sstevel@tonic-gate 						free(lp);
4077c478bd9Sstevel@tonic-gate 						freelists(this);
4087c478bd9Sstevel@tonic-gate 						errno = ENOMEM;
4097c478bd9Sstevel@tonic-gate 						return (NULL);
4107c478bd9Sstevel@tonic-gate 					}
4117c478bd9Sstevel@tonic-gate 					if (olen > 0) {
4127c478bd9Sstevel@tonic-gate 						memcpy(linep, olinep, olen);
4137c478bd9Sstevel@tonic-gate 						free(olinep);
4147c478bd9Sstevel@tonic-gate 					}
4157c478bd9Sstevel@tonic-gate 					memcpy(linep + olen, pos, len);
4167c478bd9Sstevel@tonic-gate 					olen += len;
4177c478bd9Sstevel@tonic-gate 					*(linep + olen) = '\0';
4187c478bd9Sstevel@tonic-gate 					olinep = linep;
4197c478bd9Sstevel@tonic-gate 				}
4207c478bd9Sstevel@tonic-gate 				if (cont) {
4217c478bd9Sstevel@tonic-gate 					if (fgets(line, LINSIZ, pvt->fp)) {
4227c478bd9Sstevel@tonic-gate 						pos = line;
4237c478bd9Sstevel@tonic-gate 						len = strlen(pos);
4247c478bd9Sstevel@tonic-gate 					} else
4257c478bd9Sstevel@tonic-gate 						cont = 0;
4267c478bd9Sstevel@tonic-gate 				}
4277c478bd9Sstevel@tonic-gate 			} while (cont);
4287c478bd9Sstevel@tonic-gate 			lp->l_line = linep;
4297c478bd9Sstevel@tonic-gate 			lp->l_next = pvt->linehead;
4307c478bd9Sstevel@tonic-gate 			pvt->linehead = lp;
431*55fea89dSDan Cross 
4327c478bd9Sstevel@tonic-gate 			/*
4337c478bd9Sstevel@tonic-gate 			 * If this is the one we wanted, we are done.
4347c478bd9Sstevel@tonic-gate 			 */
4357c478bd9Sstevel@tonic-gate 			if (!strcmp(lp->l_groupname, group))
4367c478bd9Sstevel@tonic-gate 				return (lp);
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	return (NULL);
4407c478bd9Sstevel@tonic-gate }
4419525b14bSRao Shoaib 
4429525b14bSRao Shoaib /*! \file */
443