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