xref: /titanic_44/usr/src/lib/libresolv2/common/irs/lcl_ng.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao 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  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: lcl_ng.c,v 1.3 2005/04/27 04:56:31 sra Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /* Imports */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include "port_before.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <netinet/in.h>
287c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
297c478bd9Sstevel@tonic-gate #include <resolv.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <irs.h>
377c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "port_after.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "irs_p.h"
427c478bd9Sstevel@tonic-gate #include "lcl_p.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* Definitions */
457c478bd9Sstevel@tonic-gate 
46*9525b14bSRao Shoaib #define NG_HOST         0       /*%< Host name */
47*9525b14bSRao Shoaib #define NG_USER         1       /*%< User name */
48*9525b14bSRao Shoaib #define NG_DOM          2       /*%< and Domain name */
49*9525b14bSRao Shoaib #define LINSIZ		1024    /*%< Length of netgroup file line */
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * XXX Warning XXX
527c478bd9Sstevel@tonic-gate  * This code is a hack-and-slash special.  It realy needs to be
537c478bd9Sstevel@tonic-gate  * rewritten with things like strdup, and realloc in mind.
547c478bd9Sstevel@tonic-gate  * More reasonable data structures would not be a bad thing.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
57*9525b14bSRao Shoaib /*%
587c478bd9Sstevel@tonic-gate  * Static Variables and functions used by setnetgrent(), getnetgrent() and
597c478bd9Sstevel@tonic-gate  * endnetgrent().
60*9525b14bSRao Shoaib  *
617c478bd9Sstevel@tonic-gate  * There are two linked lists:
62*9525b14bSRao Shoaib  * \li linelist is just used by setnetgrent() to parse the net group file via.
637c478bd9Sstevel@tonic-gate  *   parse_netgrp()
64*9525b14bSRao Shoaib  * \li netgrp is the list of entries for the current netgroup
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate struct linelist {
67*9525b14bSRao Shoaib 	struct linelist *l_next;	/*%< Chain ptr. */
68*9525b14bSRao Shoaib 	int		l_parsed;	/*%< Flag for cycles */
69*9525b14bSRao Shoaib 	char *		l_groupname;	/*%< Name of netgroup */
70*9525b14bSRao Shoaib 	char *		l_line;		/*%< Netgroup entrie(s) to be parsed */
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate struct ng_old_struct {
74*9525b14bSRao Shoaib 	struct ng_old_struct *ng_next;	/*%< Chain ptr */
75*9525b14bSRao Shoaib 	char *		ng_str[3];	/*%< Field pointers, see below */
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate struct pvt {
797c478bd9Sstevel@tonic-gate 	FILE			*fp;
807c478bd9Sstevel@tonic-gate 	struct linelist		*linehead;
817c478bd9Sstevel@tonic-gate 	struct ng_old_struct    *nextgrp;
827c478bd9Sstevel@tonic-gate 	struct {
837c478bd9Sstevel@tonic-gate 		struct ng_old_struct	*gr;
847c478bd9Sstevel@tonic-gate 		char			*grname;
857c478bd9Sstevel@tonic-gate 	} grouphead;
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* Forward */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static void 		ng_rewind(struct irs_ng *, const char*);
917c478bd9Sstevel@tonic-gate static void 		ng_close(struct irs_ng *);
927c478bd9Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **,
937c478bd9Sstevel@tonic-gate 				const char **, const char **);
947c478bd9Sstevel@tonic-gate static int 		ng_test(struct irs_ng *, const char *,
957c478bd9Sstevel@tonic-gate 				const char *, const char *,
967c478bd9Sstevel@tonic-gate 				const char *);
977c478bd9Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static int 		parse_netgrp(struct irs_ng *, const char*);
1007c478bd9Sstevel@tonic-gate static struct linelist *read_for_group(struct irs_ng *, const char *);
1017c478bd9Sstevel@tonic-gate static void		freelists(struct irs_ng *);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* Public */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate struct irs_ng *
irs_lcl_ng(struct irs_acc * this)1067c478bd9Sstevel@tonic-gate irs_lcl_ng(struct irs_acc *this) {
1077c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
1087c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	UNUSED(this);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
1137c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1147c478bd9Sstevel@tonic-gate 		return (NULL);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
1177c478bd9Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
1187c478bd9Sstevel@tonic-gate 		memput(ng, sizeof *ng);
1197c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1207c478bd9Sstevel@tonic-gate 		return (NULL);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1237c478bd9Sstevel@tonic-gate 	ng->private = pvt;
1247c478bd9Sstevel@tonic-gate 	ng->close = ng_close;
1257c478bd9Sstevel@tonic-gate 	ng->next = ng_next;
1267c478bd9Sstevel@tonic-gate 	ng->test = ng_test;
1277c478bd9Sstevel@tonic-gate 	ng->rewind = ng_rewind;
1287c478bd9Sstevel@tonic-gate 	ng->minimize = ng_minimize;
1297c478bd9Sstevel@tonic-gate 	return (ng);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* Methods */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static void
ng_close(struct irs_ng * this)1357c478bd9Sstevel@tonic-gate ng_close(struct irs_ng *this) {
1367c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (pvt->fp != NULL)
1397c478bd9Sstevel@tonic-gate 		fclose(pvt->fp);
1407c478bd9Sstevel@tonic-gate 	freelists(this);
1417c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1427c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
145*9525b14bSRao Shoaib /*%
1467c478bd9Sstevel@tonic-gate  * Parse the netgroup file looking for the netgroup and build the list
1477c478bd9Sstevel@tonic-gate  * of netgrp structures. Let parse_netgrp() and read_for_group() do
1487c478bd9Sstevel@tonic-gate  * most of the work.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate static void
ng_rewind(struct irs_ng * this,const char * group)1517c478bd9Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
1527c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (pvt->fp != NULL && fseek(pvt->fp, SEEK_CUR, 0L) == -1) {
1557c478bd9Sstevel@tonic-gate 		fclose(pvt->fp);
1567c478bd9Sstevel@tonic-gate 		pvt->fp = NULL;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (pvt->fp == NULL || pvt->grouphead.gr == NULL ||
1607c478bd9Sstevel@tonic-gate 	    strcmp(group, pvt->grouphead.grname)) {
1617c478bd9Sstevel@tonic-gate 		freelists(this);
1627c478bd9Sstevel@tonic-gate 		if (pvt->fp != NULL)
1637c478bd9Sstevel@tonic-gate 			fclose(pvt->fp);
1647c478bd9Sstevel@tonic-gate 		pvt->fp = fopen(_PATH_NETGROUP, "r");
1657c478bd9Sstevel@tonic-gate 		if (pvt->fp != NULL) {
1667c478bd9Sstevel@tonic-gate 			if (parse_netgrp(this, group))
1677c478bd9Sstevel@tonic-gate 				freelists(this);
1687c478bd9Sstevel@tonic-gate 			if (!(pvt->grouphead.grname = strdup(group)))
1697c478bd9Sstevel@tonic-gate 				freelists(this);
1707c478bd9Sstevel@tonic-gate 			fclose(pvt->fp);
1717c478bd9Sstevel@tonic-gate 			pvt->fp = NULL;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	pvt->nextgrp = pvt->grouphead.gr;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
177*9525b14bSRao Shoaib /*%
1787c478bd9Sstevel@tonic-gate  * Get the next netgroup off the list.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)1817c478bd9Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
1827c478bd9Sstevel@tonic-gate 	const char **domain)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (pvt->nextgrp) {
1877c478bd9Sstevel@tonic-gate 		*host = pvt->nextgrp->ng_str[NG_HOST];
1887c478bd9Sstevel@tonic-gate 		*user = pvt->nextgrp->ng_str[NG_USER];
1897c478bd9Sstevel@tonic-gate 		*domain = pvt->nextgrp->ng_str[NG_DOM];
1907c478bd9Sstevel@tonic-gate 		pvt->nextgrp = pvt->nextgrp->ng_next;
1917c478bd9Sstevel@tonic-gate 		return (1);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	return (0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
196*9525b14bSRao Shoaib /*%
1977c478bd9Sstevel@tonic-gate  * Search for a match in a netgroup.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate static int
ng_test(struct irs_ng * this,const char * name,const char * host,const char * user,const char * domain)2007c478bd9Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
2017c478bd9Sstevel@tonic-gate 	const char *host, const char *user, const char *domain)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	const char *ng_host, *ng_user, *ng_domain;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	ng_rewind(this, name);
2067c478bd9Sstevel@tonic-gate 	while (ng_next(this, &ng_host, &ng_user, &ng_domain))
2077c478bd9Sstevel@tonic-gate 		if ((host == NULL || ng_host == NULL ||
2087c478bd9Sstevel@tonic-gate 		     !strcmp(host, ng_host)) &&
2097c478bd9Sstevel@tonic-gate 		    (user ==  NULL || ng_user == NULL ||
2107c478bd9Sstevel@tonic-gate 		     !strcmp(user, ng_user)) &&
2117c478bd9Sstevel@tonic-gate 		    (domain == NULL || ng_domain == NULL ||
2127c478bd9Sstevel@tonic-gate 		     !strcmp(domain, ng_domain))) {
2137c478bd9Sstevel@tonic-gate 			freelists(this);
2147c478bd9Sstevel@tonic-gate 			return (1);
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 	freelists(this);
2177c478bd9Sstevel@tonic-gate 	return (0);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static void
ng_minimize(struct irs_ng * this)2217c478bd9Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
2227c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (pvt->fp != NULL) {
2257c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->fp);
2267c478bd9Sstevel@tonic-gate 		pvt->fp = NULL;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /* Private */
2317c478bd9Sstevel@tonic-gate 
232*9525b14bSRao Shoaib /*%
2337c478bd9Sstevel@tonic-gate  * endnetgrent() - cleanup
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate static void
freelists(struct irs_ng * this)2367c478bd9Sstevel@tonic-gate freelists(struct irs_ng *this) {
2377c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2387c478bd9Sstevel@tonic-gate 	struct linelist *lp, *olp;
2397c478bd9Sstevel@tonic-gate 	struct ng_old_struct *gp, *ogp;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	lp = pvt->linehead;
2427c478bd9Sstevel@tonic-gate 	while (lp) {
2437c478bd9Sstevel@tonic-gate 		olp = lp;
2447c478bd9Sstevel@tonic-gate 		lp = lp->l_next;
2457c478bd9Sstevel@tonic-gate 		free(olp->l_groupname);
2467c478bd9Sstevel@tonic-gate 		free(olp->l_line);
2477c478bd9Sstevel@tonic-gate 		free((char *)olp);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	pvt->linehead = NULL;
2507c478bd9Sstevel@tonic-gate 	if (pvt->grouphead.grname) {
2517c478bd9Sstevel@tonic-gate 		free(pvt->grouphead.grname);
2527c478bd9Sstevel@tonic-gate 		pvt->grouphead.grname = NULL;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	gp = pvt->grouphead.gr;
2557c478bd9Sstevel@tonic-gate 	while (gp) {
2567c478bd9Sstevel@tonic-gate 		ogp = gp;
2577c478bd9Sstevel@tonic-gate 		gp = gp->ng_next;
2587c478bd9Sstevel@tonic-gate 		if (ogp->ng_str[NG_HOST])
2597c478bd9Sstevel@tonic-gate 			free(ogp->ng_str[NG_HOST]);
2607c478bd9Sstevel@tonic-gate 		if (ogp->ng_str[NG_USER])
2617c478bd9Sstevel@tonic-gate 			free(ogp->ng_str[NG_USER]);
2627c478bd9Sstevel@tonic-gate 		if (ogp->ng_str[NG_DOM])
2637c478bd9Sstevel@tonic-gate 			free(ogp->ng_str[NG_DOM]);
2647c478bd9Sstevel@tonic-gate 		free((char *)ogp);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	pvt->grouphead.gr = NULL;
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
269*9525b14bSRao Shoaib /*%
2707c478bd9Sstevel@tonic-gate  * Parse the netgroup file setting up the linked lists.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate static int
parse_netgrp(struct irs_ng * this,const char * group)2737c478bd9Sstevel@tonic-gate parse_netgrp(struct irs_ng *this, const char *group) {
2747c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2757c478bd9Sstevel@tonic-gate 	char *spos, *epos;
2767c478bd9Sstevel@tonic-gate 	int len, strpos;
2777c478bd9Sstevel@tonic-gate 	char *pos, *gpos;
2787c478bd9Sstevel@tonic-gate 	struct ng_old_struct *grp;
2797c478bd9Sstevel@tonic-gate 	struct linelist *lp = pvt->linehead;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate         /*
2827c478bd9Sstevel@tonic-gate          * First, see if the line has already been read in.
2837c478bd9Sstevel@tonic-gate          */
2847c478bd9Sstevel@tonic-gate 	while (lp) {
2857c478bd9Sstevel@tonic-gate 		if (!strcmp(group, lp->l_groupname))
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		lp = lp->l_next;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	if (lp == NULL &&
2907c478bd9Sstevel@tonic-gate 	    (lp = read_for_group(this, group)) == NULL)
2917c478bd9Sstevel@tonic-gate 		return (1);
2927c478bd9Sstevel@tonic-gate 	if (lp->l_parsed) {
2937c478bd9Sstevel@tonic-gate 		/*fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);*/
2947c478bd9Sstevel@tonic-gate 		return (1);
2957c478bd9Sstevel@tonic-gate 	} else
2967c478bd9Sstevel@tonic-gate 		lp->l_parsed = 1;
2977c478bd9Sstevel@tonic-gate 	pos = lp->l_line;
2987c478bd9Sstevel@tonic-gate 	while (*pos != '\0') {
2997c478bd9Sstevel@tonic-gate 		if (*pos == '(') {
3007c478bd9Sstevel@tonic-gate 			if (!(grp = malloc(sizeof (struct ng_old_struct)))) {
3017c478bd9Sstevel@tonic-gate 				freelists(this);
3027c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
3037c478bd9Sstevel@tonic-gate 				return (1);
3047c478bd9Sstevel@tonic-gate 			}
3057c478bd9Sstevel@tonic-gate 			memset(grp, 0, sizeof (struct ng_old_struct));
3067c478bd9Sstevel@tonic-gate 			grp->ng_next = pvt->grouphead.gr;
3077c478bd9Sstevel@tonic-gate 			pvt->grouphead.gr = grp;
3087c478bd9Sstevel@tonic-gate 			pos++;
3097c478bd9Sstevel@tonic-gate 			gpos = strsep(&pos, ")");
3107c478bd9Sstevel@tonic-gate 			for (strpos = 0; strpos < 3; strpos++) {
3117c478bd9Sstevel@tonic-gate 				if ((spos = strsep(&gpos, ","))) {
3127c478bd9Sstevel@tonic-gate 					while (*spos == ' ' || *spos == '\t')
3137c478bd9Sstevel@tonic-gate 						spos++;
3147c478bd9Sstevel@tonic-gate 					if ((epos = strpbrk(spos, " \t"))) {
3157c478bd9Sstevel@tonic-gate 						*epos = '\0';
3167c478bd9Sstevel@tonic-gate 						len = epos - spos;
3177c478bd9Sstevel@tonic-gate 					} else
3187c478bd9Sstevel@tonic-gate 						len = strlen(spos);
3197c478bd9Sstevel@tonic-gate 					if (len > 0) {
3207c478bd9Sstevel@tonic-gate 						if(!(grp->ng_str[strpos]
3217c478bd9Sstevel@tonic-gate 						   =  (char *)
3227c478bd9Sstevel@tonic-gate 						   malloc(len + 1))) {
3237c478bd9Sstevel@tonic-gate 							freelists(this);
3247c478bd9Sstevel@tonic-gate 							return (1);
3257c478bd9Sstevel@tonic-gate 						}
3267c478bd9Sstevel@tonic-gate 						memcpy(grp->ng_str[strpos],
3277c478bd9Sstevel@tonic-gate 						       spos,
3287c478bd9Sstevel@tonic-gate 						       len + 1);
3297c478bd9Sstevel@tonic-gate 					}
3307c478bd9Sstevel@tonic-gate 				} else
3317c478bd9Sstevel@tonic-gate 					goto errout;
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 		} else {
3347c478bd9Sstevel@tonic-gate 			spos = strsep(&pos, ", \t");
3357c478bd9Sstevel@tonic-gate 			if (spos != NULL && parse_netgrp(this, spos)) {
3367c478bd9Sstevel@tonic-gate 				freelists(this);
3377c478bd9Sstevel@tonic-gate 				return (1);
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		if (pos == NULL)
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 		while (*pos == ' ' || *pos == ',' || *pos == '\t')
3437c478bd9Sstevel@tonic-gate 			pos++;
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 	return (0);
3467c478bd9Sstevel@tonic-gate  errout:
3477c478bd9Sstevel@tonic-gate 	/*fprintf(stderr, "Bad netgroup %s at ..%s\n", lp->l_groupname,
3487c478bd9Sstevel@tonic-gate 		  spos);*/
3497c478bd9Sstevel@tonic-gate 	return (1);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
352*9525b14bSRao Shoaib /*%
3537c478bd9Sstevel@tonic-gate  * Read the netgroup file and save lines until the line for the netgroup
3547c478bd9Sstevel@tonic-gate  * is found. Return 1 if eof is encountered.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate static struct linelist *
read_for_group(struct irs_ng * this,const char * group)3577c478bd9Sstevel@tonic-gate read_for_group(struct irs_ng *this, const char *group) {
3587c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3597c478bd9Sstevel@tonic-gate 	char *pos, *spos, *linep = NULL, *olinep;
3607c478bd9Sstevel@tonic-gate 	int len, olen, cont;
3617c478bd9Sstevel@tonic-gate 	struct linelist *lp;
3627c478bd9Sstevel@tonic-gate 	char line[LINSIZ + 1];
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	while (fgets(line, LINSIZ, pvt->fp) != NULL) {
3657c478bd9Sstevel@tonic-gate 		pos = line;
3667c478bd9Sstevel@tonic-gate 		if (*pos == '#')
3677c478bd9Sstevel@tonic-gate 			continue;
3687c478bd9Sstevel@tonic-gate 		while (*pos == ' ' || *pos == '\t')
3697c478bd9Sstevel@tonic-gate 			pos++;
3707c478bd9Sstevel@tonic-gate 		spos = pos;
3717c478bd9Sstevel@tonic-gate 		while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
3727c478bd9Sstevel@tonic-gate 			*pos != '\0')
3737c478bd9Sstevel@tonic-gate 			pos++;
3747c478bd9Sstevel@tonic-gate 		len = pos - spos;
3757c478bd9Sstevel@tonic-gate 		while (*pos == ' ' || *pos == '\t')
3767c478bd9Sstevel@tonic-gate 			pos++;
3777c478bd9Sstevel@tonic-gate 		if (*pos != '\n' && *pos != '\0') {
3787c478bd9Sstevel@tonic-gate 			if (!(lp = malloc(sizeof (*lp)))) {
3797c478bd9Sstevel@tonic-gate 				freelists(this);
3807c478bd9Sstevel@tonic-gate 				return (NULL);
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			lp->l_parsed = 0;
3837c478bd9Sstevel@tonic-gate 			if (!(lp->l_groupname = malloc(len + 1))) {
3847c478bd9Sstevel@tonic-gate 				free(lp);
3857c478bd9Sstevel@tonic-gate 				freelists(this);
3867c478bd9Sstevel@tonic-gate 				return (NULL);
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 			memcpy(lp->l_groupname, spos,  len);
3897c478bd9Sstevel@tonic-gate 			*(lp->l_groupname + len) = '\0';
3907c478bd9Sstevel@tonic-gate 			len = strlen(pos);
3917c478bd9Sstevel@tonic-gate 			olen = 0;
3927c478bd9Sstevel@tonic-gate 			olinep = NULL;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 			/*
3957c478bd9Sstevel@tonic-gate 			 * Loop around handling line continuations.
3967c478bd9Sstevel@tonic-gate 			 */
3977c478bd9Sstevel@tonic-gate 			do {
3987c478bd9Sstevel@tonic-gate 				if (*(pos + len - 1) == '\n')
3997c478bd9Sstevel@tonic-gate 					len--;
4007c478bd9Sstevel@tonic-gate 				if (*(pos + len - 1) == '\\') {
4017c478bd9Sstevel@tonic-gate 					len--;
4027c478bd9Sstevel@tonic-gate 					cont = 1;
4037c478bd9Sstevel@tonic-gate 				} else
4047c478bd9Sstevel@tonic-gate 					cont = 0;
4057c478bd9Sstevel@tonic-gate 				if (len > 0) {
4067c478bd9Sstevel@tonic-gate 					if (!(linep = malloc(olen + len + 1))){
4077c478bd9Sstevel@tonic-gate 						if (olen > 0)
4087c478bd9Sstevel@tonic-gate 							free(olinep);
4097c478bd9Sstevel@tonic-gate 						free(lp->l_groupname);
4107c478bd9Sstevel@tonic-gate 						free(lp);
4117c478bd9Sstevel@tonic-gate 						freelists(this);
4127c478bd9Sstevel@tonic-gate 						errno = ENOMEM;
4137c478bd9Sstevel@tonic-gate 						return (NULL);
4147c478bd9Sstevel@tonic-gate 					}
4157c478bd9Sstevel@tonic-gate 					if (olen > 0) {
4167c478bd9Sstevel@tonic-gate 						memcpy(linep, olinep, olen);
4177c478bd9Sstevel@tonic-gate 						free(olinep);
4187c478bd9Sstevel@tonic-gate 					}
4197c478bd9Sstevel@tonic-gate 					memcpy(linep + olen, pos, len);
4207c478bd9Sstevel@tonic-gate 					olen += len;
4217c478bd9Sstevel@tonic-gate 					*(linep + olen) = '\0';
4227c478bd9Sstevel@tonic-gate 					olinep = linep;
4237c478bd9Sstevel@tonic-gate 				}
4247c478bd9Sstevel@tonic-gate 				if (cont) {
4257c478bd9Sstevel@tonic-gate 					if (fgets(line, LINSIZ, pvt->fp)) {
4267c478bd9Sstevel@tonic-gate 						pos = line;
4277c478bd9Sstevel@tonic-gate 						len = strlen(pos);
4287c478bd9Sstevel@tonic-gate 					} else
4297c478bd9Sstevel@tonic-gate 						cont = 0;
4307c478bd9Sstevel@tonic-gate 				}
4317c478bd9Sstevel@tonic-gate 			} while (cont);
4327c478bd9Sstevel@tonic-gate 			lp->l_line = linep;
4337c478bd9Sstevel@tonic-gate 			lp->l_next = pvt->linehead;
4347c478bd9Sstevel@tonic-gate 			pvt->linehead = lp;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 			/*
4377c478bd9Sstevel@tonic-gate 			 * If this is the one we wanted, we are done.
4387c478bd9Sstevel@tonic-gate 			 */
4397c478bd9Sstevel@tonic-gate 			if (!strcmp(lp->l_groupname, group))
4407c478bd9Sstevel@tonic-gate 				return (lp);
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	return (NULL);
4447c478bd9Sstevel@tonic-gate }
445*9525b14bSRao Shoaib 
446*9525b14bSRao Shoaib /*! \file */
447