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