Home | History | Annotate | Line # | Download | only in libhack
getgrent.c revision 1.6
      1  1.6    elric /*	$NetBSD: getgrent.c,v 1.6 2003/02/19 08:04:29 elric Exp $	*/
      2  1.1      gwr 
      3  1.1      gwr /*
      4  1.1      gwr  * Copyright (c) 1989, 1993
      5  1.1      gwr  *	The Regents of the University of California.  All rights reserved.
      6  1.1      gwr  * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
      7  1.1      gwr  *
      8  1.1      gwr  * Redistribution and use in source and binary forms, with or without
      9  1.1      gwr  * modification, are permitted provided that the following conditions
     10  1.1      gwr  * are met:
     11  1.1      gwr  * 1. Redistributions of source code must retain the above copyright
     12  1.1      gwr  *    notice, this list of conditions and the following disclaimer.
     13  1.1      gwr  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      gwr  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      gwr  *    documentation and/or other materials provided with the distribution.
     16  1.1      gwr  * 3. All advertising materials mentioning features or use of this software
     17  1.1      gwr  *    must display the following acknowledgement:
     18  1.1      gwr  *	This product includes software developed by the University of
     19  1.1      gwr  *	California, Berkeley and its contributors.
     20  1.1      gwr  * 4. Neither the name of the University nor the names of its contributors
     21  1.1      gwr  *    may be used to endorse or promote products derived from this software
     22  1.1      gwr  *    without specific prior written permission.
     23  1.1      gwr  *
     24  1.1      gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1      gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1      gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1      gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1      gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1      gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1      gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1      gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1      gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1      gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1      gwr  * SUCH DAMAGE.
     35  1.1      gwr  */
     36  1.1      gwr 
     37  1.2      gwr /*
     38  1.2      gwr  * Copied from:  lib/libc/gen/getgrent.c
     39  1.6    elric  *     NetBSD: getgrent.c,v 1.46 2003/02/17 00:11:54 simonb Exp
     40  1.2      gwr  * and then gutted, leaving only /etc/group support.
     41  1.2      gwr  */
     42  1.1      gwr 
     43  1.4  tsutsui #include <sys/cdefs.h>
     44  1.4  tsutsui 
     45  1.4  tsutsui #ifdef __weak_alias
     46  1.4  tsutsui #define endgrent		_endgrent
     47  1.4  tsutsui #define getgrent		_getgrent
     48  1.4  tsutsui #define getgrgid		_getgrgid
     49  1.4  tsutsui #define getgrnam		_getgrnam
     50  1.4  tsutsui #define setgrent		_setgrent
     51  1.4  tsutsui #define setgroupent		_setgroupent
     52  1.4  tsutsui 
     53  1.4  tsutsui __weak_alias(endgrent,_endgrent)
     54  1.4  tsutsui __weak_alias(getgrent,_getgrent)
     55  1.4  tsutsui __weak_alias(getgrgid,_getgrgid)
     56  1.4  tsutsui __weak_alias(getgrnam,_getgrnam)
     57  1.4  tsutsui __weak_alias(setgrent,_setgrent)
     58  1.4  tsutsui __weak_alias(setgroupent,_setgroupent)
     59  1.4  tsutsui #endif
     60  1.1      gwr 
     61  1.5    lukem #include <sys/types.h>
     62  1.5    lukem 
     63  1.5    lukem #include <grp.h>
     64  1.5    lukem #include <limits.h>
     65  1.5    lukem #include <stdio.h>
     66  1.5    lukem #include <stdlib.h>
     67  1.5    lukem #include <string.h>
     68  1.5    lukem 
     69  1.6    elric struct group *_getgrent_user(const char *);
     70  1.6    elric 
     71  1.5    lukem static FILE		*_gr_fp;
     72  1.5    lukem static struct group	_gr_group;
     73  1.6    elric static int		_gr_stayopen;
     74  1.5    lukem static int		_gr_filesdone;
     75  1.5    lukem 
     76  1.6    elric static int grscan(int, gid_t, const char *, const char *);
     77  1.5    lukem static int grstart(void);
     78  1.6    elric static int grmatchline(int, gid_t, const char *, const char *);
     79  1.1      gwr 
     80  1.1      gwr #define	MAXGRP		200
     81  1.1      gwr #define	MAXLINELENGTH	1024
     82  1.6    elric 
     83  1.6    elric static __aconst char	*members[MAXGRP];
     84  1.5    lukem static char		grline[MAXLINELENGTH];
     85  1.1      gwr 
     86  1.1      gwr struct group *
     87  1.5    lukem getgrent(void)
     88  1.1      gwr {
     89  1.6    elric 
     90  1.6    elric 	if ((!_gr_fp && !grstart()) || !grscan(0, 0, NULL, NULL))
     91  1.6    elric  		return (NULL);
     92  1.6    elric 	return &_gr_group;
     93  1.6    elric }
     94  1.6    elric 
     95  1.6    elric /*
     96  1.6    elric  * _getgrent_user() is designed only to be called by getgrouplist(3) and
     97  1.6    elric  * hence makes no guarantees about filling the entire structure that it
     98  1.6    elric  * returns.  It may only fill in the group name and gid fields.
     99  1.6    elric  */
    100  1.6    elric 
    101  1.6    elric struct group *
    102  1.6    elric _getgrent_user(const char *user)
    103  1.6    elric {
    104  1.6    elric 
    105  1.6    elric 	if ((!_gr_fp && !grstart()) || !grscan(0, 0, NULL, user))
    106  1.6    elric  		return (NULL);
    107  1.6    elric 	return &_gr_group;
    108  1.1      gwr }
    109  1.1      gwr 
    110  1.1      gwr struct group *
    111  1.5    lukem getgrnam(const char *name)
    112  1.1      gwr {
    113  1.1      gwr 	int rval;
    114  1.1      gwr 
    115  1.5    lukem 	if (!grstart())
    116  1.6    elric 		return NULL;
    117  1.6    elric 	rval = grscan(1, 0, name, NULL);
    118  1.1      gwr 	if (!_gr_stayopen)
    119  1.1      gwr 		endgrent();
    120  1.6    elric 	return (rval) ? &_gr_group : NULL;
    121  1.1      gwr }
    122  1.1      gwr 
    123  1.1      gwr struct group *
    124  1.1      gwr getgrgid(gid_t gid)
    125  1.1      gwr {
    126  1.1      gwr 	int rval;
    127  1.1      gwr 
    128  1.5    lukem 	if (!grstart())
    129  1.6    elric 		return NULL;
    130  1.6    elric 	rval = grscan(1, gid, NULL, NULL);
    131  1.1      gwr 	if (!_gr_stayopen)
    132  1.1      gwr 		endgrent();
    133  1.6    elric 	return (rval) ? &_gr_group : NULL;
    134  1.1      gwr }
    135  1.1      gwr 
    136  1.1      gwr static int
    137  1.5    lukem grstart(void)
    138  1.1      gwr {
    139  1.6    elric 
    140  1.5    lukem 	_gr_filesdone = 0;
    141  1.1      gwr 	if (_gr_fp) {
    142  1.1      gwr 		rewind(_gr_fp);
    143  1.6    elric 		return 1;
    144  1.1      gwr 	}
    145  1.6    elric 	return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
    146  1.1      gwr }
    147  1.1      gwr 
    148  1.1      gwr void
    149  1.5    lukem setgrent(void)
    150  1.1      gwr {
    151  1.6    elric 
    152  1.1      gwr 	(void) setgroupent(0);
    153  1.1      gwr }
    154  1.1      gwr 
    155  1.1      gwr int
    156  1.5    lukem setgroupent(int stayopen)
    157  1.1      gwr {
    158  1.6    elric 
    159  1.5    lukem 	if (!grstart())
    160  1.6    elric 		return 0;
    161  1.1      gwr 	_gr_stayopen = stayopen;
    162  1.6    elric 	return 1;
    163  1.1      gwr }
    164  1.1      gwr 
    165  1.1      gwr void
    166  1.5    lukem endgrent(void)
    167  1.1      gwr {
    168  1.6    elric 
    169  1.5    lukem 	_gr_filesdone = 0;
    170  1.1      gwr 	if (_gr_fp) {
    171  1.1      gwr 		(void)fclose(_gr_fp);
    172  1.1      gwr 		_gr_fp = NULL;
    173  1.1      gwr 	}
    174  1.1      gwr }
    175  1.1      gwr 
    176  1.1      gwr static int
    177  1.6    elric grscan(int search, gid_t gid, const char *name, const char *user)
    178  1.1      gwr {
    179  1.6    elric 
    180  1.5    lukem 	if (_gr_filesdone)
    181  1.5    lukem 		return 0;
    182  1.1      gwr 	for (;;) {
    183  1.5    lukem 		if (!fgets(grline, sizeof(grline), _gr_fp)) {
    184  1.5    lukem 			if (!search)
    185  1.5    lukem 				_gr_filesdone = 1;
    186  1.5    lukem 			return 0;
    187  1.5    lukem 		}
    188  1.1      gwr 		/* skip lines that are too big */
    189  1.5    lukem 		if (!strchr(grline, '\n')) {
    190  1.1      gwr 			int ch;
    191  1.1      gwr 
    192  1.1      gwr 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    193  1.1      gwr 				;
    194  1.1      gwr 			continue;
    195  1.1      gwr 		}
    196  1.6    elric 		if (grmatchline(search, gid, name, user))
    197  1.5    lukem 			return 1;
    198  1.1      gwr 	}
    199  1.1      gwr 	/* NOTREACHED */
    200  1.5    lukem }
    201  1.5    lukem 
    202  1.5    lukem static int
    203  1.6    elric grmatchline(int search, gid_t gid, const char *name, const char *user)
    204  1.5    lukem {
    205  1.5    lukem 	unsigned long	id;
    206  1.6    elric 	__aconst char	**m;
    207  1.5    lukem 	char		*cp, *bp, *ep;
    208  1.5    lukem 
    209  1.5    lukem 	/* name may be NULL if search is nonzero */
    210  1.5    lukem 
    211  1.5    lukem 	bp = grline;
    212  1.5    lukem 	_gr_group.gr_name = strsep(&bp, ":\n");
    213  1.5    lukem 	if (search && name && strcmp(_gr_group.gr_name, name))
    214  1.5    lukem 		return 0;
    215  1.5    lukem 	_gr_group.gr_passwd = strsep(&bp, ":\n");
    216  1.5    lukem 	if (!(cp = strsep(&bp, ":\n")))
    217  1.5    lukem 		return 0;
    218  1.5    lukem 	id = strtoul(cp, &ep, 10);
    219  1.5    lukem 	if (id > GID_MAX || *ep != '\0')
    220  1.5    lukem 		return 0;
    221  1.5    lukem 	_gr_group.gr_gid = (gid_t)id;
    222  1.5    lukem 	if (search && name == NULL && _gr_group.gr_gid != gid)
    223  1.5    lukem 		return 0;
    224  1.5    lukem 	cp = NULL;
    225  1.5    lukem 	if (bp == NULL)
    226  1.5    lukem 		return 0;
    227  1.5    lukem 	for (_gr_group.gr_mem = m = members;; bp++) {
    228  1.5    lukem 		if (m == &members[MAXGRP - 1])
    229  1.5    lukem 			break;
    230  1.5    lukem 		if (*bp == ',') {
    231  1.5    lukem 			if (cp) {
    232  1.5    lukem 				*bp = '\0';
    233  1.5    lukem 				*m++ = cp;
    234  1.5    lukem 				cp = NULL;
    235  1.5    lukem 			}
    236  1.5    lukem 		} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    237  1.5    lukem 			if (cp) {
    238  1.5    lukem 				*bp = '\0';
    239  1.5    lukem 				*m++ = cp;
    240  1.5    lukem 			}
    241  1.5    lukem 			break;
    242  1.5    lukem 		} else if (cp == NULL)
    243  1.5    lukem 			cp = bp;
    244  1.5    lukem 	}
    245  1.5    lukem 	*m = NULL;
    246  1.6    elric 	if (user) {
    247  1.6    elric 		for (m = members; *m; m++)
    248  1.6    elric 			if (!strcmp(user, *m))
    249  1.6    elric 				return 1;
    250  1.6    elric 		return 0;
    251  1.6    elric 	}
    252  1.5    lukem 	return 1;
    253  1.1      gwr }
    254