Home | History | Annotate | Line # | Download | only in gen
getgrent.c revision 1.13.4.1
      1  1.13.4.1      jtc /*	$NetBSD: getgrent.c,v 1.13.4.1 1996/09/19 20:02:44 jtc Exp $	*/
      2      1.11      cgd 
      3       1.1      cgd /*
      4      1.11      cgd  * Copyright (c) 1989, 1993
      5      1.11      cgd  *	The Regents of the University of California.  All rights reserved.
      6      1.13     phil  * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
      7       1.1      cgd  *
      8       1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9       1.1      cgd  * modification, are permitted provided that the following conditions
     10       1.1      cgd  * are met:
     11       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12       1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13       1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15       1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16       1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17       1.1      cgd  *    must display the following acknowledgement:
     18       1.1      cgd  *	This product includes software developed by the University of
     19       1.1      cgd  *	California, Berkeley and its contributors.
     20       1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21       1.1      cgd  *    may be used to endorse or promote products derived from this software
     22       1.1      cgd  *    without specific prior written permission.
     23       1.1      cgd  *
     24       1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.1      cgd  * SUCH DAMAGE.
     35       1.1      cgd  */
     36       1.1      cgd 
     37       1.1      cgd #if defined(LIBC_SCCS) && !defined(lint)
     38      1.11      cgd #if 0
     39      1.11      cgd static char sccsid[] = "@(#)getgrent.c	8.2 (Berkeley) 3/21/94";
     40      1.11      cgd #else
     41  1.13.4.1      jtc static char rcsid[] = "$NetBSD: getgrent.c,v 1.13.4.1 1996/09/19 20:02:44 jtc Exp $";
     42      1.11      cgd #endif
     43       1.1      cgd #endif /* LIBC_SCCS and not lint */
     44       1.1      cgd 
     45  1.13.4.1      jtc #include "namespace.h"
     46       1.1      cgd #include <sys/types.h>
     47       1.1      cgd #include <stdio.h>
     48       1.1      cgd #include <stdlib.h>
     49       1.2  deraadt #include <string.h>
     50       1.1      cgd #include <grp.h>
     51       1.2  deraadt #ifdef YP
     52       1.2  deraadt #include <rpc/rpc.h>
     53       1.2  deraadt #include <rpcsvc/yp_prot.h>
     54       1.2  deraadt #include <rpcsvc/ypclnt.h>
     55  1.13.4.1      jtc #endif
     56  1.13.4.1      jtc 
     57  1.13.4.1      jtc #ifdef __weak_alias
     58  1.13.4.1      jtc __weak_alias(endgrent,_endgrent);
     59  1.13.4.1      jtc __weak_alias(getgrent,_getgrent);
     60  1.13.4.1      jtc __weak_alias(getgrgid,_getgrgid);
     61  1.13.4.1      jtc __weak_alias(getgrnam,_getgrnam);
     62  1.13.4.1      jtc __weak_alias(setgrent,_setgrent);
     63  1.13.4.1      jtc __weak_alias(setgroupent,_setgroupent);
     64       1.2  deraadt #endif
     65       1.1      cgd 
     66       1.1      cgd static FILE *_gr_fp;
     67       1.1      cgd static struct group _gr_group;
     68       1.1      cgd static int _gr_stayopen;
     69       1.1      cgd static int grscan(), start_gr();
     70       1.1      cgd 
     71       1.1      cgd #define	MAXGRP		200
     72       1.1      cgd static char *members[MAXGRP];
     73       1.1      cgd #define	MAXLINELENGTH	1024
     74       1.1      cgd static char line[MAXLINELENGTH];
     75       1.1      cgd 
     76       1.2  deraadt #ifdef YP
     77      1.13     phil enum _ypmode { YPMODE_NONE, YPMODE_FULL, YPMODE_NAME };
     78      1.13     phil static enum _ypmode __ypmode;
     79       1.2  deraadt static char	*__ypcurrent, *__ypdomain;
     80      1.13     phil static int	__ypcurrentlen;
     81       1.2  deraadt #endif
     82       1.2  deraadt 
     83       1.1      cgd struct group *
     84       1.1      cgd getgrent()
     85       1.1      cgd {
     86       1.1      cgd 	if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
     87       1.1      cgd 		return(NULL);
     88       1.1      cgd 	return(&_gr_group);
     89       1.1      cgd }
     90       1.1      cgd 
     91       1.1      cgd struct group *
     92       1.1      cgd getgrnam(name)
     93       1.1      cgd 	const char *name;
     94       1.1      cgd {
     95       1.1      cgd 	int rval;
     96       1.1      cgd 
     97       1.1      cgd 	if (!start_gr())
     98       1.1      cgd 		return(NULL);
     99       1.1      cgd 	rval = grscan(1, 0, name);
    100       1.1      cgd 	if (!_gr_stayopen)
    101       1.1      cgd 		endgrent();
    102       1.1      cgd 	return(rval ? &_gr_group : NULL);
    103       1.1      cgd }
    104       1.1      cgd 
    105       1.1      cgd struct group *
    106       1.1      cgd #ifdef __STDC__
    107       1.1      cgd getgrgid(gid_t gid)
    108       1.1      cgd #else
    109       1.1      cgd getgrgid(gid)
    110       1.1      cgd 	gid_t gid;
    111       1.1      cgd #endif
    112       1.1      cgd {
    113       1.1      cgd 	int rval;
    114       1.1      cgd 
    115       1.1      cgd 	if (!start_gr())
    116       1.1      cgd 		return(NULL);
    117       1.1      cgd 	rval = grscan(1, gid, NULL);
    118       1.1      cgd 	if (!_gr_stayopen)
    119       1.1      cgd 		endgrent();
    120       1.1      cgd 	return(rval ? &_gr_group : NULL);
    121       1.1      cgd }
    122       1.1      cgd 
    123       1.2  deraadt static int
    124       1.1      cgd start_gr()
    125       1.1      cgd {
    126       1.1      cgd 	if (_gr_fp) {
    127       1.1      cgd 		rewind(_gr_fp);
    128       1.7  deraadt #ifdef YP
    129      1.13     phil 		__ypmode = YPMODE_NONE;
    130       1.7  deraadt 		if(__ypcurrent)
    131       1.7  deraadt 			free(__ypcurrent);
    132       1.7  deraadt 		__ypcurrent = NULL;
    133       1.7  deraadt #endif
    134       1.1      cgd 		return(1);
    135       1.1      cgd 	}
    136       1.1      cgd 	return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
    137       1.1      cgd }
    138       1.1      cgd 
    139       1.5      jtc void
    140       1.1      cgd setgrent()
    141       1.1      cgd {
    142       1.5      jtc 	(void) setgroupent(0);
    143       1.1      cgd }
    144       1.1      cgd 
    145       1.1      cgd int
    146       1.1      cgd setgroupent(stayopen)
    147       1.1      cgd 	int stayopen;
    148       1.1      cgd {
    149       1.1      cgd 	if (!start_gr())
    150       1.1      cgd 		return(0);
    151       1.1      cgd 	_gr_stayopen = stayopen;
    152       1.1      cgd 	return(1);
    153       1.1      cgd }
    154       1.1      cgd 
    155       1.1      cgd void
    156       1.1      cgd endgrent()
    157       1.1      cgd {
    158       1.1      cgd 	if (_gr_fp) {
    159       1.1      cgd 		(void)fclose(_gr_fp);
    160       1.1      cgd 		_gr_fp = NULL;
    161       1.7  deraadt #ifdef YP
    162      1.13     phil 		__ypmode = YPMODE_NONE;
    163       1.7  deraadt 		if(__ypcurrent)
    164       1.7  deraadt 			free(__ypcurrent);
    165       1.7  deraadt 		__ypcurrent = NULL;
    166       1.7  deraadt #endif
    167       1.1      cgd 	}
    168       1.1      cgd }
    169       1.1      cgd 
    170       1.2  deraadt static int
    171       1.1      cgd grscan(search, gid, name)
    172       1.1      cgd 	register int search, gid;
    173       1.1      cgd 	register char *name;
    174       1.1      cgd {
    175       1.1      cgd 	register char *cp, **m;
    176       1.1      cgd 	char *bp;
    177      1.13     phil #ifdef YP
    178      1.13     phil 	char *grname = (char *)NULL;
    179      1.13     phil #endif
    180       1.1      cgd 
    181       1.1      cgd 	for (;;) {
    182       1.2  deraadt #ifdef YP
    183      1.13     phil 		if(__ypmode != YPMODE_NONE) {
    184       1.2  deraadt 			char *key, *data;
    185       1.2  deraadt 			int keylen, datalen;
    186       1.2  deraadt 			int r;
    187       1.2  deraadt 
    188       1.2  deraadt 			if(!__ypdomain) {
    189       1.2  deraadt 				if(yp_get_default_domain(&__ypdomain)) {
    190      1.13     phil 					__ypmode = YPMODE_NONE;
    191      1.13     phil 					if(grname != (char *)NULL) {
    192      1.13     phil 						free(grname);
    193      1.13     phil 						grname = (char *)NULL;
    194      1.13     phil 					}
    195       1.2  deraadt 					continue;
    196       1.2  deraadt 				}
    197       1.2  deraadt 			}
    198      1.13     phil 			switch(__ypmode) {
    199      1.13     phil 			case YPMODE_FULL:
    200      1.13     phil 				if(__ypcurrent) {
    201      1.13     phil 					r = yp_next(__ypdomain, "group.byname",
    202      1.13     phil 						__ypcurrent, __ypcurrentlen,
    203      1.13     phil 						&key, &keylen, &data, &datalen);
    204      1.13     phil 					free(__ypcurrent);
    205      1.13     phil 					if(r != 0) {
    206      1.13     phil 						__ypcurrent = NULL;
    207      1.13     phil 						__ypmode = YPMODE_NONE;
    208      1.13     phil 						free(data);
    209      1.13     phil 						continue;
    210      1.13     phil 					}
    211      1.13     phil 					__ypcurrent = key;
    212      1.13     phil 					__ypcurrentlen = keylen;
    213      1.13     phil 					bcopy(data, line, datalen);
    214      1.13     phil 					free(data);
    215      1.13     phil 				} else {
    216      1.13     phil 					r = yp_first(__ypdomain, "group.byname",
    217      1.13     phil 						&__ypcurrent, &__ypcurrentlen,
    218      1.13     phil 						&data, &datalen);
    219      1.13     phil 					if(r != 0) {
    220      1.13     phil 						__ypmode = YPMODE_NONE;
    221      1.13     phil 						free(data);
    222      1.13     phil 						continue;
    223      1.13     phil 					}
    224      1.13     phil 					bcopy(data, line, datalen);
    225       1.2  deraadt 					free(data);
    226       1.2  deraadt 				}
    227      1.13     phil 				break;
    228      1.13     phil 			case YPMODE_NAME:
    229      1.13     phil 				if(grname != (char *)NULL) {
    230      1.13     phil 					r = yp_match(__ypdomain, "group.byname",
    231      1.13     phil 						grname, strlen(grname),
    232      1.13     phil 						&data, &datalen);
    233      1.13     phil 					__ypmode = YPMODE_NONE;
    234      1.13     phil 					free(grname);
    235      1.13     phil 					grname = (char *)NULL;
    236      1.13     phil 					if(r != 0) {
    237      1.13     phil 						free(data);
    238      1.13     phil 						continue;
    239      1.13     phil 					}
    240      1.13     phil 					bcopy(data, line, datalen);
    241       1.2  deraadt 					free(data);
    242      1.13     phil 				} else {
    243      1.13     phil 					__ypmode = YPMODE_NONE;	/* ??? */
    244       1.2  deraadt 					continue;
    245       1.2  deraadt 				}
    246      1.13     phil 				break;
    247       1.2  deraadt 			}
    248       1.2  deraadt 			line[datalen] = '\0';
    249       1.2  deraadt 			bp = line;
    250       1.2  deraadt 			goto parse;
    251       1.2  deraadt 		}
    252       1.2  deraadt #endif
    253       1.1      cgd 		if (!fgets(line, sizeof(line), _gr_fp))
    254       1.1      cgd 			return(0);
    255       1.1      cgd 		bp = line;
    256       1.1      cgd 		/* skip lines that are too big */
    257       1.6      jtc 		if (!strchr(line, '\n')) {
    258       1.1      cgd 			int ch;
    259       1.1      cgd 
    260       1.1      cgd 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    261       1.1      cgd 				;
    262       1.1      cgd 			continue;
    263       1.1      cgd 		}
    264       1.2  deraadt #ifdef YP
    265      1.13     phil 		if (line[0] == '+') {
    266      1.13     phil 			switch(line[1]) {
    267      1.13     phil 			case ':':
    268      1.13     phil 			case '\0':
    269      1.13     phil 			case '\n':
    270      1.13     phil 				if(_yp_check(NULL)) {
    271      1.13     phil 					__ypmode = YPMODE_FULL;
    272      1.12       pk 					continue;
    273      1.12       pk 				}
    274      1.13     phil 				break;
    275      1.13     phil 			default:
    276      1.13     phil 				if(_yp_check(NULL)) {
    277      1.13     phil 					register char *tptr;
    278      1.13     phil 
    279      1.13     phil 					__ypmode = YPMODE_NAME;
    280      1.13     phil 					tptr = strsep(&bp, ":\n");
    281      1.13     phil 					grname = strdup(tptr + 1);
    282      1.12       pk 					continue;
    283      1.12       pk 				}
    284      1.13     phil 				break;
    285       1.2  deraadt 			}
    286       1.2  deraadt 		}
    287       1.2  deraadt parse:
    288       1.2  deraadt #endif
    289       1.1      cgd 		_gr_group.gr_name = strsep(&bp, ":\n");
    290       1.1      cgd 		if (search && name && strcmp(_gr_group.gr_name, name))
    291       1.1      cgd 			continue;
    292       1.1      cgd 		_gr_group.gr_passwd = strsep(&bp, ":\n");
    293       1.1      cgd 		if (!(cp = strsep(&bp, ":\n")))
    294       1.1      cgd 			continue;
    295       1.1      cgd 		_gr_group.gr_gid = atoi(cp);
    296       1.1      cgd 		if (search && name == NULL && _gr_group.gr_gid != gid)
    297       1.1      cgd 			continue;
    298       1.8  deraadt 		cp = NULL;
    299       1.9  deraadt 		if (bp == NULL)
    300       1.9  deraadt 			continue;
    301       1.8  deraadt 		for (m = _gr_group.gr_mem = members;; bp++) {
    302       1.8  deraadt 			if (m == &members[MAXGRP - 1])
    303       1.1      cgd 				break;
    304       1.8  deraadt 			if (*bp == ',') {
    305       1.8  deraadt 				if (cp) {
    306       1.8  deraadt 					*bp = '\0';
    307       1.8  deraadt 					*m++ = cp;
    308       1.8  deraadt 					cp = NULL;
    309       1.8  deraadt 				}
    310       1.8  deraadt 			} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    311       1.8  deraadt 				if (cp) {
    312       1.8  deraadt 					*bp = '\0';
    313       1.8  deraadt 					*m++ = cp;
    314       1.8  deraadt 				}
    315       1.1      cgd 				break;
    316       1.8  deraadt 			} else if (cp == NULL)
    317       1.8  deraadt 				cp = bp;
    318       1.1      cgd 		}
    319       1.8  deraadt 		*m = NULL;
    320       1.1      cgd 		return(1);
    321       1.1      cgd 	}
    322       1.1      cgd 	/* NOTREACHED */
    323       1.1      cgd }
    324