xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/lcl_ng.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: lcl_ng.c,v 1.3 2005/04/27 04:56:31 sra Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Imports */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <netinet/in.h>
280Sstevel@tonic-gate #include <arpa/nameser.h>
290Sstevel@tonic-gate #include <resolv.h>
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <irs.h>
370Sstevel@tonic-gate #include <isc/memcluster.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include "port_after.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "irs_p.h"
420Sstevel@tonic-gate #include "lcl_p.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /* Definitions */
450Sstevel@tonic-gate 
46*11038SRao.Shoaib@Sun.COM #define NG_HOST         0       /*%< Host name */
47*11038SRao.Shoaib@Sun.COM #define NG_USER         1       /*%< User name */
48*11038SRao.Shoaib@Sun.COM #define NG_DOM          2       /*%< and Domain name */
49*11038SRao.Shoaib@Sun.COM #define LINSIZ		1024    /*%< Length of netgroup file line */
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * XXX Warning XXX
520Sstevel@tonic-gate  * This code is a hack-and-slash special.  It realy needs to be
530Sstevel@tonic-gate  * rewritten with things like strdup, and realloc in mind.
540Sstevel@tonic-gate  * More reasonable data structures would not be a bad thing.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
57*11038SRao.Shoaib@Sun.COM /*%
580Sstevel@tonic-gate  * Static Variables and functions used by setnetgrent(), getnetgrent() and
590Sstevel@tonic-gate  * endnetgrent().
60*11038SRao.Shoaib@Sun.COM  *
610Sstevel@tonic-gate  * There are two linked lists:
62*11038SRao.Shoaib@Sun.COM  * \li linelist is just used by setnetgrent() to parse the net group file via.
630Sstevel@tonic-gate  *   parse_netgrp()
64*11038SRao.Shoaib@Sun.COM  * \li netgrp is the list of entries for the current netgroup
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate struct linelist {
67*11038SRao.Shoaib@Sun.COM 	struct linelist *l_next;	/*%< Chain ptr. */
68*11038SRao.Shoaib@Sun.COM 	int		l_parsed;	/*%< Flag for cycles */
69*11038SRao.Shoaib@Sun.COM 	char *		l_groupname;	/*%< Name of netgroup */
70*11038SRao.Shoaib@Sun.COM 	char *		l_line;		/*%< Netgroup entrie(s) to be parsed */
710Sstevel@tonic-gate };
720Sstevel@tonic-gate 
730Sstevel@tonic-gate struct ng_old_struct {
74*11038SRao.Shoaib@Sun.COM 	struct ng_old_struct *ng_next;	/*%< Chain ptr */
75*11038SRao.Shoaib@Sun.COM 	char *		ng_str[3];	/*%< Field pointers, see below */
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate struct pvt {
790Sstevel@tonic-gate 	FILE			*fp;
800Sstevel@tonic-gate 	struct linelist		*linehead;
810Sstevel@tonic-gate 	struct ng_old_struct    *nextgrp;
820Sstevel@tonic-gate 	struct {
830Sstevel@tonic-gate 		struct ng_old_struct	*gr;
840Sstevel@tonic-gate 		char			*grname;
850Sstevel@tonic-gate 	} grouphead;
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /* Forward */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static void 		ng_rewind(struct irs_ng *, const char*);
910Sstevel@tonic-gate static void 		ng_close(struct irs_ng *);
920Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **,
930Sstevel@tonic-gate 				const char **, const char **);
940Sstevel@tonic-gate static int 		ng_test(struct irs_ng *, const char *,
950Sstevel@tonic-gate 				const char *, const char *,
960Sstevel@tonic-gate 				const char *);
970Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static int 		parse_netgrp(struct irs_ng *, const char*);
1000Sstevel@tonic-gate static struct linelist *read_for_group(struct irs_ng *, const char *);
1010Sstevel@tonic-gate static void		freelists(struct irs_ng *);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /* Public */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate struct irs_ng *
irs_lcl_ng(struct irs_acc * this)1060Sstevel@tonic-gate irs_lcl_ng(struct irs_acc *this) {
1070Sstevel@tonic-gate 	struct irs_ng *ng;
1080Sstevel@tonic-gate 	struct pvt *pvt;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	UNUSED(this);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
1130Sstevel@tonic-gate 		errno = ENOMEM;
1140Sstevel@tonic-gate 		return (NULL);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
1170Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
1180Sstevel@tonic-gate 		memput(ng, sizeof *ng);
1190Sstevel@tonic-gate 		errno = ENOMEM;
1200Sstevel@tonic-gate 		return (NULL);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1230Sstevel@tonic-gate 	ng->private = pvt;
1240Sstevel@tonic-gate 	ng->close = ng_close;
1250Sstevel@tonic-gate 	ng->next = ng_next;
1260Sstevel@tonic-gate 	ng->test = ng_test;
1270Sstevel@tonic-gate 	ng->rewind = ng_rewind;
1280Sstevel@tonic-gate 	ng->minimize = ng_minimize;
1290Sstevel@tonic-gate 	return (ng);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /* Methods */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static void
ng_close(struct irs_ng * this)1350Sstevel@tonic-gate ng_close(struct irs_ng *this) {
1360Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (pvt->fp != NULL)
1390Sstevel@tonic-gate 		fclose(pvt->fp);
1400Sstevel@tonic-gate 	freelists(this);
1410Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1420Sstevel@tonic-gate 	memput(this, sizeof *this);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
145*11038SRao.Shoaib@Sun.COM /*%
1460Sstevel@tonic-gate  * Parse the netgroup file looking for the netgroup and build the list
1470Sstevel@tonic-gate  * of netgrp structures. Let parse_netgrp() and read_for_group() do
1480Sstevel@tonic-gate  * most of the work.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate static void
ng_rewind(struct irs_ng * this,const char * group)1510Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
1520Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (pvt->fp != NULL && fseek(pvt->fp, SEEK_CUR, 0L) == -1) {
1550Sstevel@tonic-gate 		fclose(pvt->fp);
1560Sstevel@tonic-gate 		pvt->fp = NULL;
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (pvt->fp == NULL || pvt->grouphead.gr == NULL ||
1600Sstevel@tonic-gate 	    strcmp(group, pvt->grouphead.grname)) {
1610Sstevel@tonic-gate 		freelists(this);
1620Sstevel@tonic-gate 		if (pvt->fp != NULL)
1630Sstevel@tonic-gate 			fclose(pvt->fp);
1640Sstevel@tonic-gate 		pvt->fp = fopen(_PATH_NETGROUP, "r");
1650Sstevel@tonic-gate 		if (pvt->fp != NULL) {
1660Sstevel@tonic-gate 			if (parse_netgrp(this, group))
1670Sstevel@tonic-gate 				freelists(this);
1680Sstevel@tonic-gate 			if (!(pvt->grouphead.grname = strdup(group)))
1690Sstevel@tonic-gate 				freelists(this);
1700Sstevel@tonic-gate 			fclose(pvt->fp);
1710Sstevel@tonic-gate 			pvt->fp = NULL;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	pvt->nextgrp = pvt->grouphead.gr;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
177*11038SRao.Shoaib@Sun.COM /*%
1780Sstevel@tonic-gate  * Get the next netgroup off the list.
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)1810Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
1820Sstevel@tonic-gate 	const char **domain)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (pvt->nextgrp) {
1870Sstevel@tonic-gate 		*host = pvt->nextgrp->ng_str[NG_HOST];
1880Sstevel@tonic-gate 		*user = pvt->nextgrp->ng_str[NG_USER];
1890Sstevel@tonic-gate 		*domain = pvt->nextgrp->ng_str[NG_DOM];
1900Sstevel@tonic-gate 		pvt->nextgrp = pvt->nextgrp->ng_next;
1910Sstevel@tonic-gate 		return (1);
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 	return (0);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
196*11038SRao.Shoaib@Sun.COM /*%
1970Sstevel@tonic-gate  * Search for a match in a netgroup.
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate static int
ng_test(struct irs_ng * this,const char * name,const char * host,const char * user,const char * domain)2000Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
2010Sstevel@tonic-gate 	const char *host, const char *user, const char *domain)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	const char *ng_host, *ng_user, *ng_domain;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	ng_rewind(this, name);
2060Sstevel@tonic-gate 	while (ng_next(this, &ng_host, &ng_user, &ng_domain))
2070Sstevel@tonic-gate 		if ((host == NULL || ng_host == NULL ||
2080Sstevel@tonic-gate 		     !strcmp(host, ng_host)) &&
2090Sstevel@tonic-gate 		    (user ==  NULL || ng_user == NULL ||
2100Sstevel@tonic-gate 		     !strcmp(user, ng_user)) &&
2110Sstevel@tonic-gate 		    (domain == NULL || ng_domain == NULL ||
2120Sstevel@tonic-gate 		     !strcmp(domain, ng_domain))) {
2130Sstevel@tonic-gate 			freelists(this);
2140Sstevel@tonic-gate 			return (1);
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 	freelists(this);
2170Sstevel@tonic-gate 	return (0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static void
ng_minimize(struct irs_ng * this)2210Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
2220Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (pvt->fp != NULL) {
2250Sstevel@tonic-gate 		(void)fclose(pvt->fp);
2260Sstevel@tonic-gate 		pvt->fp = NULL;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /* Private */
2310Sstevel@tonic-gate 
232*11038SRao.Shoaib@Sun.COM /*%
2330Sstevel@tonic-gate  * endnetgrent() - cleanup
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate static void
freelists(struct irs_ng * this)2360Sstevel@tonic-gate freelists(struct irs_ng *this) {
2370Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2380Sstevel@tonic-gate 	struct linelist *lp, *olp;
2390Sstevel@tonic-gate 	struct ng_old_struct *gp, *ogp;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	lp = pvt->linehead;
2420Sstevel@tonic-gate 	while (lp) {
2430Sstevel@tonic-gate 		olp = lp;
2440Sstevel@tonic-gate 		lp = lp->l_next;
2450Sstevel@tonic-gate 		free(olp->l_groupname);
2460Sstevel@tonic-gate 		free(olp->l_line);
2470Sstevel@tonic-gate 		free((char *)olp);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	pvt->linehead = NULL;
2500Sstevel@tonic-gate 	if (pvt->grouphead.grname) {
2510Sstevel@tonic-gate 		free(pvt->grouphead.grname);
2520Sstevel@tonic-gate 		pvt->grouphead.grname = NULL;
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 	gp = pvt->grouphead.gr;
2550Sstevel@tonic-gate 	while (gp) {
2560Sstevel@tonic-gate 		ogp = gp;
2570Sstevel@tonic-gate 		gp = gp->ng_next;
2580Sstevel@tonic-gate 		if (ogp->ng_str[NG_HOST])
2590Sstevel@tonic-gate 			free(ogp->ng_str[NG_HOST]);
2600Sstevel@tonic-gate 		if (ogp->ng_str[NG_USER])
2610Sstevel@tonic-gate 			free(ogp->ng_str[NG_USER]);
2620Sstevel@tonic-gate 		if (ogp->ng_str[NG_DOM])
2630Sstevel@tonic-gate 			free(ogp->ng_str[NG_DOM]);
2640Sstevel@tonic-gate 		free((char *)ogp);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	pvt->grouphead.gr = NULL;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
269*11038SRao.Shoaib@Sun.COM /*%
2700Sstevel@tonic-gate  * Parse the netgroup file setting up the linked lists.
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate static int
parse_netgrp(struct irs_ng * this,const char * group)2730Sstevel@tonic-gate parse_netgrp(struct irs_ng *this, const char *group) {
2740Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2750Sstevel@tonic-gate 	char *spos, *epos;
2760Sstevel@tonic-gate 	int len, strpos;
2770Sstevel@tonic-gate 	char *pos, *gpos;
2780Sstevel@tonic-gate 	struct ng_old_struct *grp;
2790Sstevel@tonic-gate 	struct linelist *lp = pvt->linehead;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate         /*
2820Sstevel@tonic-gate          * First, see if the line has already been read in.
2830Sstevel@tonic-gate          */
2840Sstevel@tonic-gate 	while (lp) {
2850Sstevel@tonic-gate 		if (!strcmp(group, lp->l_groupname))
2860Sstevel@tonic-gate 			break;
2870Sstevel@tonic-gate 		lp = lp->l_next;
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	if (lp == NULL &&
2900Sstevel@tonic-gate 	    (lp = read_for_group(this, group)) == NULL)
2910Sstevel@tonic-gate 		return (1);
2920Sstevel@tonic-gate 	if (lp->l_parsed) {
2930Sstevel@tonic-gate 		/*fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);*/
2940Sstevel@tonic-gate 		return (1);
2950Sstevel@tonic-gate 	} else
2960Sstevel@tonic-gate 		lp->l_parsed = 1;
2970Sstevel@tonic-gate 	pos = lp->l_line;
2980Sstevel@tonic-gate 	while (*pos != '\0') {
2990Sstevel@tonic-gate 		if (*pos == '(') {
3000Sstevel@tonic-gate 			if (!(grp = malloc(sizeof (struct ng_old_struct)))) {
3010Sstevel@tonic-gate 				freelists(this);
3020Sstevel@tonic-gate 				errno = ENOMEM;
3030Sstevel@tonic-gate 				return (1);
3040Sstevel@tonic-gate 			}
3050Sstevel@tonic-gate 			memset(grp, 0, sizeof (struct ng_old_struct));
3060Sstevel@tonic-gate 			grp->ng_next = pvt->grouphead.gr;
3070Sstevel@tonic-gate 			pvt->grouphead.gr = grp;
3080Sstevel@tonic-gate 			pos++;
3090Sstevel@tonic-gate 			gpos = strsep(&pos, ")");
3100Sstevel@tonic-gate 			for (strpos = 0; strpos < 3; strpos++) {
3110Sstevel@tonic-gate 				if ((spos = strsep(&gpos, ","))) {
3120Sstevel@tonic-gate 					while (*spos == ' ' || *spos == '\t')
3130Sstevel@tonic-gate 						spos++;
3140Sstevel@tonic-gate 					if ((epos = strpbrk(spos, " \t"))) {
3150Sstevel@tonic-gate 						*epos = '\0';
3160Sstevel@tonic-gate 						len = epos - spos;
3170Sstevel@tonic-gate 					} else
3180Sstevel@tonic-gate 						len = strlen(spos);
3190Sstevel@tonic-gate 					if (len > 0) {
3200Sstevel@tonic-gate 						if(!(grp->ng_str[strpos]
3210Sstevel@tonic-gate 						   =  (char *)
3220Sstevel@tonic-gate 						   malloc(len + 1))) {
3230Sstevel@tonic-gate 							freelists(this);
3240Sstevel@tonic-gate 							return (1);
3250Sstevel@tonic-gate 						}
3260Sstevel@tonic-gate 						memcpy(grp->ng_str[strpos],
3270Sstevel@tonic-gate 						       spos,
3280Sstevel@tonic-gate 						       len + 1);
3290Sstevel@tonic-gate 					}
3300Sstevel@tonic-gate 				} else
3310Sstevel@tonic-gate 					goto errout;
3320Sstevel@tonic-gate 			}
3330Sstevel@tonic-gate 		} else {
3340Sstevel@tonic-gate 			spos = strsep(&pos, ", \t");
3350Sstevel@tonic-gate 			if (spos != NULL && parse_netgrp(this, spos)) {
3360Sstevel@tonic-gate 				freelists(this);
3370Sstevel@tonic-gate 				return (1);
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 		if (pos == NULL)
3410Sstevel@tonic-gate 			break;
3420Sstevel@tonic-gate 		while (*pos == ' ' || *pos == ',' || *pos == '\t')
3430Sstevel@tonic-gate 			pos++;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 	return (0);
3460Sstevel@tonic-gate  errout:
3470Sstevel@tonic-gate 	/*fprintf(stderr, "Bad netgroup %s at ..%s\n", lp->l_groupname,
3480Sstevel@tonic-gate 		  spos);*/
3490Sstevel@tonic-gate 	return (1);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
352*11038SRao.Shoaib@Sun.COM /*%
3530Sstevel@tonic-gate  * Read the netgroup file and save lines until the line for the netgroup
3540Sstevel@tonic-gate  * is found. Return 1 if eof is encountered.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate static struct linelist *
read_for_group(struct irs_ng * this,const char * group)3570Sstevel@tonic-gate read_for_group(struct irs_ng *this, const char *group) {
3580Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3590Sstevel@tonic-gate 	char *pos, *spos, *linep = NULL, *olinep;
3600Sstevel@tonic-gate 	int len, olen, cont;
3610Sstevel@tonic-gate 	struct linelist *lp;
3620Sstevel@tonic-gate 	char line[LINSIZ + 1];
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	while (fgets(line, LINSIZ, pvt->fp) != NULL) {
3650Sstevel@tonic-gate 		pos = line;
3660Sstevel@tonic-gate 		if (*pos == '#')
3670Sstevel@tonic-gate 			continue;
3680Sstevel@tonic-gate 		while (*pos == ' ' || *pos == '\t')
3690Sstevel@tonic-gate 			pos++;
3700Sstevel@tonic-gate 		spos = pos;
3710Sstevel@tonic-gate 		while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
3720Sstevel@tonic-gate 			*pos != '\0')
3730Sstevel@tonic-gate 			pos++;
3740Sstevel@tonic-gate 		len = pos - spos;
3750Sstevel@tonic-gate 		while (*pos == ' ' || *pos == '\t')
3760Sstevel@tonic-gate 			pos++;
3770Sstevel@tonic-gate 		if (*pos != '\n' && *pos != '\0') {
3780Sstevel@tonic-gate 			if (!(lp = malloc(sizeof (*lp)))) {
3790Sstevel@tonic-gate 				freelists(this);
3800Sstevel@tonic-gate 				return (NULL);
3810Sstevel@tonic-gate 			}
3820Sstevel@tonic-gate 			lp->l_parsed = 0;
3830Sstevel@tonic-gate 			if (!(lp->l_groupname = malloc(len + 1))) {
3840Sstevel@tonic-gate 				free(lp);
3850Sstevel@tonic-gate 				freelists(this);
3860Sstevel@tonic-gate 				return (NULL);
3870Sstevel@tonic-gate 			}
3880Sstevel@tonic-gate 			memcpy(lp->l_groupname, spos,  len);
3890Sstevel@tonic-gate 			*(lp->l_groupname + len) = '\0';
3900Sstevel@tonic-gate 			len = strlen(pos);
3910Sstevel@tonic-gate 			olen = 0;
3920Sstevel@tonic-gate 			olinep = NULL;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 			/*
3950Sstevel@tonic-gate 			 * Loop around handling line continuations.
3960Sstevel@tonic-gate 			 */
3970Sstevel@tonic-gate 			do {
3980Sstevel@tonic-gate 				if (*(pos + len - 1) == '\n')
3990Sstevel@tonic-gate 					len--;
4000Sstevel@tonic-gate 				if (*(pos + len - 1) == '\\') {
4010Sstevel@tonic-gate 					len--;
4020Sstevel@tonic-gate 					cont = 1;
4030Sstevel@tonic-gate 				} else
4040Sstevel@tonic-gate 					cont = 0;
4050Sstevel@tonic-gate 				if (len > 0) {
4060Sstevel@tonic-gate 					if (!(linep = malloc(olen + len + 1))){
4070Sstevel@tonic-gate 						if (olen > 0)
4080Sstevel@tonic-gate 							free(olinep);
4090Sstevel@tonic-gate 						free(lp->l_groupname);
4100Sstevel@tonic-gate 						free(lp);
4110Sstevel@tonic-gate 						freelists(this);
4120Sstevel@tonic-gate 						errno = ENOMEM;
4130Sstevel@tonic-gate 						return (NULL);
4140Sstevel@tonic-gate 					}
4150Sstevel@tonic-gate 					if (olen > 0) {
4160Sstevel@tonic-gate 						memcpy(linep, olinep, olen);
4170Sstevel@tonic-gate 						free(olinep);
4180Sstevel@tonic-gate 					}
4190Sstevel@tonic-gate 					memcpy(linep + olen, pos, len);
4200Sstevel@tonic-gate 					olen += len;
4210Sstevel@tonic-gate 					*(linep + olen) = '\0';
4220Sstevel@tonic-gate 					olinep = linep;
4230Sstevel@tonic-gate 				}
4240Sstevel@tonic-gate 				if (cont) {
4250Sstevel@tonic-gate 					if (fgets(line, LINSIZ, pvt->fp)) {
4260Sstevel@tonic-gate 						pos = line;
4270Sstevel@tonic-gate 						len = strlen(pos);
4280Sstevel@tonic-gate 					} else
4290Sstevel@tonic-gate 						cont = 0;
4300Sstevel@tonic-gate 				}
4310Sstevel@tonic-gate 			} while (cont);
4320Sstevel@tonic-gate 			lp->l_line = linep;
4330Sstevel@tonic-gate 			lp->l_next = pvt->linehead;
4340Sstevel@tonic-gate 			pvt->linehead = lp;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 			/*
4370Sstevel@tonic-gate 			 * If this is the one we wanted, we are done.
4380Sstevel@tonic-gate 			 */
4390Sstevel@tonic-gate 			if (!strcmp(lp->l_groupname, group))
4400Sstevel@tonic-gate 				return (lp);
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 	return (NULL);
4440Sstevel@tonic-gate }
445*11038SRao.Shoaib@Sun.COM 
446*11038SRao.Shoaib@Sun.COM /*! \file */
447