Home | History | Annotate | Line # | Download | only in revnetgroup
parse_netgroup.c revision 1.1
      1 /* $OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $ */
      2 /*
      3  * Copyright (c) 1992, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Rick Macklem at The University of Guelph.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	$FreeBSD: parse_netgroup.c,v 1.5 1997/02/22 14:22:02 peter Exp $
     38  */
     39 
     40 /*
     41  * This is a specially hacked-up version of getnetgrent.c used to parse
     42  * data from the stored hash table of netgroup info rather than from a
     43  * file. It's used mainly for the parse_netgroup() function. All the YP
     44  * stuff and file support has been stripped out since it isn't needed.
     45  */
     46 
     47 #include <stdio.h>
     48 #include <string.h>
     49 #include <stdlib.h>
     50 #include <unistd.h>
     51 #include "hash.h"
     52 
     53 #ifndef lint
     54 static const char rcsid[] = "$OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $";
     55 #endif
     56 
     57 /*
     58  * Static Variables and functions used by setnetgrent(), getnetgrent() and
     59  * __endnetgrent().
     60  * There are two linked lists:
     61  * - linelist is just used by setnetgrent() to parse the net group file via.
     62  *   parse_netgrp()
     63  * - netgrp is the list of entries for the current netgroup
     64  */
     65 struct linelist {
     66 	struct linelist	*l_next;	/* Chain ptr. */
     67 	int		l_parsed;	/* Flag for cycles */
     68 	char		*l_groupname;	/* Name of netgroup */
     69 	char		*l_line;	/* Netgroup entrie(s) to be parsed */
     70 };
     71 
     72 struct netgrp {
     73 	struct netgrp	*ng_next;	/* Chain ptr */
     74 	char		*ng_str[3];	/* Field pointers, see below */
     75 };
     76 #define NG_HOST		0	/* Host name */
     77 #define NG_USER		1	/* User name */
     78 #define NG_DOM		2	/* and Domain name */
     79 
     80 static struct linelist	*linehead = (struct linelist *)0;
     81 static struct netgrp	*nextgrp = (struct netgrp *)0;
     82 static struct {
     83 	struct netgrp	*gr;
     84 	char		*grname;
     85 } grouphead = {
     86 	(struct netgrp *)0,
     87 	(char *)0,
     88 };
     89 static int parse_netgrp();
     90 static struct linelist *read_for_group();
     91 void __setnetgrent(), __endnetgrent();
     92 int __getnetgrent();
     93 extern struct group_entry *gtable[];
     94 
     95 /*
     96  * setnetgrent()
     97  * Parse the netgroup file looking for the netgroup and build the list
     98  * of netgrp structures. Let parse_netgrp() and read_for_group() do
     99  * most of the work.
    100  */
    101 void
    102 __setnetgrent(group)
    103 	char *group;
    104 {
    105 	/* Sanity check */
    106 
    107 	if (group == NULL || !strlen(group))
    108 		return;
    109 
    110 	if (grouphead.gr == (struct netgrp *)0 ||
    111 		strcmp(group, grouphead.grname)) {
    112 		__endnetgrent();
    113 		if (parse_netgrp(group))
    114 			__endnetgrent();
    115 		else {
    116 			grouphead.grname = (char *)
    117 				malloc(strlen(group) + 1);
    118 			strcpy(grouphead.grname, group);
    119 		}
    120 	}
    121 	nextgrp = grouphead.gr;
    122 }
    123 
    124 /*
    125  * Get the next netgroup off the list.
    126  */
    127 int
    128 __getnetgrent(hostp, userp, domp)
    129 	char **hostp, **userp, **domp;
    130 {
    131 	if (nextgrp) {
    132 		*hostp = nextgrp->ng_str[NG_HOST];
    133 		*userp = nextgrp->ng_str[NG_USER];
    134 		*domp = nextgrp->ng_str[NG_DOM];
    135 		nextgrp = nextgrp->ng_next;
    136 		return (1);
    137 	}
    138 	return (0);
    139 }
    140 
    141 /*
    142  * __endnetgrent() - cleanup
    143  */
    144 void
    145 __endnetgrent()
    146 {
    147 	register struct linelist *lp, *olp;
    148 	register struct netgrp *gp, *ogp;
    149 
    150 	lp = linehead;
    151 	while (lp) {
    152 		olp = lp;
    153 		lp = lp->l_next;
    154 		free(olp->l_groupname);
    155 		free(olp->l_line);
    156 		free((char *)olp);
    157 	}
    158 	linehead = (struct linelist *)0;
    159 	if (grouphead.grname) {
    160 		free(grouphead.grname);
    161 		grouphead.grname = (char *)0;
    162 	}
    163 	gp = grouphead.gr;
    164 	while (gp) {
    165 		ogp = gp;
    166 		gp = gp->ng_next;
    167 		if (ogp->ng_str[NG_HOST])
    168 			free(ogp->ng_str[NG_HOST]);
    169 		if (ogp->ng_str[NG_USER])
    170 			free(ogp->ng_str[NG_USER]);
    171 		if (ogp->ng_str[NG_DOM])
    172 			free(ogp->ng_str[NG_DOM]);
    173 		free((char *)ogp);
    174 	}
    175 	grouphead.gr = (struct netgrp *)0;
    176 }
    177 
    178 /*
    179  * Parse the netgroup file setting up the linked lists.
    180  */
    181 static int
    182 parse_netgrp(group)
    183 	char *group;
    184 {
    185 	register char *spos, *epos;
    186 	register int len, strpos;
    187 #ifdef DEBUG
    188 	register int fields;
    189 #endif
    190 	char *pos, *gpos;
    191 	struct netgrp *grp;
    192 	struct linelist *lp = linehead;
    193 
    194 	/*
    195 	 * First, see if the line has already been read in.
    196 	 */
    197 	while (lp) {
    198 		if (!strcmp(group, lp->l_groupname))
    199 			break;
    200 		lp = lp->l_next;
    201 	}
    202 	if (lp == (struct linelist *)0 &&
    203 	    (lp = read_for_group(group)) == (struct linelist *)0)
    204 		return (1);
    205 	if (lp->l_parsed) {
    206 #ifdef DEBUG
    207 		/*
    208 		 * This error message is largely superflous since the
    209 		 * code handles the error condition sucessfully, and
    210 		 * spewing it out from inside libc can actually hose
    211 		 * certain programs.
    212 		 */
    213 		fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);
    214 #endif
    215 		return (1);
    216 	} else
    217 		lp->l_parsed = 1;
    218 	pos = lp->l_line;
    219 	/* Watch for null pointer dereferences, dammit! */
    220 	while (pos != NULL && *pos != '\0') {
    221 		if (*pos == '(') {
    222 			grp = (struct netgrp *)malloc(sizeof (struct netgrp));
    223 			bzero((char *)grp, sizeof (struct netgrp));
    224 			grp->ng_next = grouphead.gr;
    225 			grouphead.gr = grp;
    226 			pos++;
    227 			gpos = strsep(&pos, ")");
    228 #ifdef DEBUG
    229 			fields = 0;
    230 #endif
    231 			for (strpos = 0; strpos < 3; strpos++) {
    232 				if ((spos = strsep(&gpos, ","))) {
    233 #ifdef DEBUG
    234 					fields++;
    235 #endif
    236 					while (*spos == ' ' || *spos == '\t')
    237 						spos++;
    238 					if ((epos = strpbrk(spos, " \t"))) {
    239 						*epos = '\0';
    240 						len = epos - spos;
    241 					} else
    242 						len = strlen(spos);
    243 					if (len > 0) {
    244 						grp->ng_str[strpos] =  (char *)
    245 							malloc(len + 1);
    246 						bcopy(spos, grp->ng_str[strpos],
    247 							len + 1);
    248 					}
    249 				} else {
    250 					/*
    251 					 * All other systems I've tested
    252 					 * return NULL for empty netgroup
    253 					 * fields. It's up to user programs
    254 					 * to handle the NULLs appropriately.
    255 					 */
    256 					grp->ng_str[strpos] = NULL;
    257 				}
    258 			}
    259 #ifdef DEBUG
    260 			/*
    261 			 * Note: on other platforms, malformed netgroup
    262 			 * entries are not normally flagged. While we
    263 			 * can catch bad entries and report them, we should
    264 			 * stay silent by default for compatibility's sake.
    265 			 */
    266 			if (fields < 3)
    267 					fprintf(stderr, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n",
    268 						grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST],
    269 						grp->ng_str[NG_USER] == NULL ? "" : ",",
    270 						grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER],
    271 						grp->ng_str[NG_DOM] == NULL ? "" : ",",
    272 						grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM],
    273 						lp->l_groupname);
    274 #endif
    275 		} else {
    276 			spos = strsep(&pos, ", \t");
    277 			if (parse_netgrp(spos))
    278 				continue;
    279 		}
    280 		/* Watch for null pointer dereferences, dammit! */
    281 		if (pos != NULL)
    282 			while (*pos == ' ' || *pos == ',' || *pos == '\t')
    283 				pos++;
    284 	}
    285 	return (0);
    286 }
    287 
    288 /*
    289  * Read the netgroup file and save lines until the line for the netgroup
    290  * is found. Return 1 if eof is encountered.
    291  */
    292 static struct linelist *
    293 read_for_group(group)
    294 	char *group;
    295 {
    296 	register char *pos, *spos, *linep = NULL, *olinep = NULL;
    297 	register int len, olen;
    298 	int cont;
    299 	struct linelist *lp;
    300 	char line[LINSIZ + 1];
    301 	char *data = NULL;
    302 
    303 	data = lookup (gtable, group);
    304 	sprintf(line, "%s %s", group, data);
    305 	pos = (char *)&line;
    306 #ifdef CANT_HAPPEN
    307 	if (*pos == '#')
    308 		continue;
    309 #endif
    310 	while (*pos == ' ' || *pos == '\t')
    311 		pos++;
    312 	spos = pos;
    313 	while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
    314 		*pos != '\0')
    315 		pos++;
    316 	len = pos - spos;
    317 	while (*pos == ' ' || *pos == '\t')
    318 		pos++;
    319 	if (*pos != '\n' && *pos != '\0') {
    320 		lp = (struct linelist *)malloc(sizeof (*lp));
    321 		lp->l_parsed = 0;
    322 		lp->l_groupname = (char *)malloc(len + 1);
    323 		bcopy(spos, lp->l_groupname, len);
    324 		*(lp->l_groupname + len) = '\0';
    325 		len = strlen(pos);
    326 		olen = 0;
    327 			/*
    328 			 * Loop around handling line continuations.
    329 			 */
    330 			do {
    331 				if (*(pos + len - 1) == '\n')
    332 					len--;
    333 				if (*(pos + len - 1) == '\\') {
    334 					len--;
    335 					cont = 1;
    336 				} else
    337 					cont = 0;
    338 				if (len > 0) {
    339 					linep = (char *)malloc(olen + len + 1);
    340 					if (olen > 0) {
    341 						bcopy(olinep, linep, olen);
    342 						free(olinep);
    343 					}
    344 					bcopy(pos, linep + olen, len);
    345 					olen += len;
    346 					*(linep + olen) = '\0';
    347 					olinep = linep;
    348 				}
    349 #ifdef CANT_HAPPEN
    350 				if (cont) {
    351 					if (fgets(line, LINSIZ, netf)) {
    352 						pos = line;
    353 						len = strlen(pos);
    354 					} else
    355 						cont = 0;
    356 				}
    357 #endif
    358 			} while (cont);
    359 		lp->l_line = linep;
    360 		lp->l_next = linehead;
    361 		linehead = lp;
    362 #ifdef CANT_HAPPEN
    363 		/*
    364 		 * If this is the one we wanted, we are done.
    365 		 */
    366 		if (!strcmp(lp->l_groupname, group))
    367 #endif
    368 			return (lp);
    369 	}
    370 	return ((struct linelist *)0);
    371 }
    372