Home | History | Annotate | Line # | Download | only in gen
getgrent.c revision 1.19
      1  1.19    lukem /*	$NetBSD: getgrent.c,v 1.19 1997/05/22 10:38:07 lukem 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.19    lukem static char rcsid[] = "$NetBSD: getgrent.c,v 1.19 1997/05/22 10:38:07 lukem Exp $";
     42  1.11      cgd #endif
     43   1.1      cgd #endif /* LIBC_SCCS and not lint */
     44   1.1      cgd 
     45   1.1      cgd #include <sys/types.h>
     46  1.18    lukem #include <limits.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.2  deraadt #endif
     56   1.1      cgd 
     57   1.1      cgd static FILE *_gr_fp;
     58   1.1      cgd static struct group _gr_group;
     59   1.1      cgd static int _gr_stayopen;
     60   1.1      cgd static int grscan(), start_gr();
     61   1.1      cgd 
     62   1.1      cgd #define	MAXGRP		200
     63   1.1      cgd static char *members[MAXGRP];
     64   1.1      cgd #define	MAXLINELENGTH	1024
     65   1.1      cgd static char line[MAXLINELENGTH];
     66   1.1      cgd 
     67   1.2  deraadt #ifdef YP
     68  1.13     phil enum _ypmode { YPMODE_NONE, YPMODE_FULL, YPMODE_NAME };
     69  1.13     phil static enum _ypmode __ypmode;
     70   1.2  deraadt static char	*__ypcurrent, *__ypdomain;
     71  1.13     phil static int	__ypcurrentlen;
     72   1.2  deraadt #endif
     73   1.2  deraadt 
     74   1.1      cgd struct group *
     75   1.1      cgd getgrent()
     76   1.1      cgd {
     77   1.1      cgd 	if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
     78   1.1      cgd 		return(NULL);
     79   1.1      cgd 	return(&_gr_group);
     80   1.1      cgd }
     81   1.1      cgd 
     82   1.1      cgd struct group *
     83   1.1      cgd getgrnam(name)
     84   1.1      cgd 	const char *name;
     85   1.1      cgd {
     86   1.1      cgd 	int rval;
     87   1.1      cgd 
     88   1.1      cgd 	if (!start_gr())
     89   1.1      cgd 		return(NULL);
     90   1.1      cgd 	rval = grscan(1, 0, name);
     91   1.1      cgd 	if (!_gr_stayopen)
     92   1.1      cgd 		endgrent();
     93   1.1      cgd 	return(rval ? &_gr_group : NULL);
     94   1.1      cgd }
     95   1.1      cgd 
     96   1.1      cgd struct group *
     97   1.1      cgd #ifdef __STDC__
     98   1.1      cgd getgrgid(gid_t gid)
     99   1.1      cgd #else
    100   1.1      cgd getgrgid(gid)
    101   1.1      cgd 	gid_t gid;
    102   1.1      cgd #endif
    103   1.1      cgd {
    104   1.1      cgd 	int rval;
    105   1.1      cgd 
    106   1.1      cgd 	if (!start_gr())
    107   1.1      cgd 		return(NULL);
    108   1.1      cgd 	rval = grscan(1, gid, NULL);
    109   1.1      cgd 	if (!_gr_stayopen)
    110   1.1      cgd 		endgrent();
    111   1.1      cgd 	return(rval ? &_gr_group : NULL);
    112   1.1      cgd }
    113   1.1      cgd 
    114   1.2  deraadt static int
    115   1.1      cgd start_gr()
    116   1.1      cgd {
    117   1.1      cgd 	if (_gr_fp) {
    118   1.1      cgd 		rewind(_gr_fp);
    119   1.7  deraadt #ifdef YP
    120  1.13     phil 		__ypmode = YPMODE_NONE;
    121  1.16    lukem 		if (__ypcurrent)
    122   1.7  deraadt 			free(__ypcurrent);
    123   1.7  deraadt 		__ypcurrent = NULL;
    124   1.7  deraadt #endif
    125   1.1      cgd 		return(1);
    126   1.1      cgd 	}
    127   1.1      cgd 	return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
    128   1.1      cgd }
    129   1.1      cgd 
    130   1.5      jtc void
    131   1.1      cgd setgrent()
    132   1.1      cgd {
    133   1.5      jtc 	(void) setgroupent(0);
    134   1.1      cgd }
    135   1.1      cgd 
    136   1.1      cgd int
    137   1.1      cgd setgroupent(stayopen)
    138   1.1      cgd 	int stayopen;
    139   1.1      cgd {
    140   1.1      cgd 	if (!start_gr())
    141   1.1      cgd 		return(0);
    142   1.1      cgd 	_gr_stayopen = stayopen;
    143   1.1      cgd 	return(1);
    144   1.1      cgd }
    145   1.1      cgd 
    146   1.1      cgd void
    147   1.1      cgd endgrent()
    148   1.1      cgd {
    149   1.1      cgd 	if (_gr_fp) {
    150   1.1      cgd 		(void)fclose(_gr_fp);
    151   1.1      cgd 		_gr_fp = NULL;
    152   1.7  deraadt #ifdef YP
    153  1.13     phil 		__ypmode = YPMODE_NONE;
    154  1.16    lukem 		if (__ypcurrent)
    155   1.7  deraadt 			free(__ypcurrent);
    156   1.7  deraadt 		__ypcurrent = NULL;
    157   1.7  deraadt #endif
    158   1.1      cgd 	}
    159   1.1      cgd }
    160   1.1      cgd 
    161   1.2  deraadt static int
    162   1.1      cgd grscan(search, gid, name)
    163  1.18    lukem 	int search;
    164  1.18    lukem 	gid_t gid;
    165  1.16    lukem 	const char *name;
    166   1.1      cgd {
    167  1.16    lukem 	char *cp, **m;
    168  1.18    lukem 	char *bp, *ep;
    169  1.18    lukem 	unsigned long id;
    170  1.13     phil #ifdef YP
    171  1.15  thorpej 	char *key, *data;
    172  1.15  thorpej 	int keylen, datalen;
    173  1.15  thorpej 	int r;
    174  1.13     phil 	char *grname = (char *)NULL;
    175  1.13     phil #endif
    176   1.1      cgd 
    177   1.1      cgd 	for (;;) {
    178   1.2  deraadt #ifdef YP
    179  1.16    lukem 		if (__ypmode != YPMODE_NONE) {
    180   1.2  deraadt 
    181  1.16    lukem 			if (!__ypdomain) {
    182  1.16    lukem 				if (yp_get_default_domain(&__ypdomain)) {
    183  1.13     phil 					__ypmode = YPMODE_NONE;
    184  1.16    lukem 					if (grname != (char *)NULL) {
    185  1.13     phil 						free(grname);
    186  1.13     phil 						grname = (char *)NULL;
    187  1.13     phil 					}
    188   1.2  deraadt 					continue;
    189   1.2  deraadt 				}
    190   1.2  deraadt 			}
    191  1.13     phil 			switch(__ypmode) {
    192  1.13     phil 			case YPMODE_FULL:
    193  1.16    lukem 				data = NULL;
    194  1.16    lukem 				if (__ypcurrent) {
    195  1.17    lukem 					key = NULL;
    196  1.13     phil 					r = yp_next(__ypdomain, "group.byname",
    197  1.13     phil 						__ypcurrent, __ypcurrentlen,
    198  1.13     phil 						&key, &keylen, &data, &datalen);
    199  1.13     phil 					free(__ypcurrent);
    200  1.17    lukem 					if (r != 0) {
    201  1.13     phil 						__ypcurrent = NULL;
    202  1.17    lukem 						if (key)
    203  1.17    lukem 							free(key);
    204  1.17    lukem 					}
    205  1.16    lukem 					else {
    206  1.16    lukem 						__ypcurrent = key;
    207  1.16    lukem 						__ypcurrentlen = keylen;
    208  1.13     phil 					}
    209  1.13     phil 				} else {
    210  1.13     phil 					r = yp_first(__ypdomain, "group.byname",
    211  1.13     phil 						&__ypcurrent, &__ypcurrentlen,
    212  1.13     phil 						&data, &datalen);
    213  1.16    lukem 				}
    214  1.16    lukem 				if (r != 0) {
    215  1.16    lukem 					__ypmode = YPMODE_NONE;
    216  1.16    lukem 					if (data)
    217  1.13     phil 						free(data);
    218  1.16    lukem 					continue;
    219   1.2  deraadt 				}
    220  1.16    lukem 				bcopy(data, line, datalen);
    221  1.16    lukem 				free(data);
    222  1.13     phil 				break;
    223  1.13     phil 			case YPMODE_NAME:
    224  1.16    lukem 				if (grname != (char *)NULL) {
    225  1.16    lukem 					data = NULL;
    226  1.13     phil 					r = yp_match(__ypdomain, "group.byname",
    227  1.13     phil 						grname, strlen(grname),
    228  1.13     phil 						&data, &datalen);
    229  1.13     phil 					__ypmode = YPMODE_NONE;
    230  1.13     phil 					free(grname);
    231  1.13     phil 					grname = (char *)NULL;
    232  1.16    lukem 					if (r != 0) {
    233  1.16    lukem 						if (data)
    234  1.16    lukem 							free(data);
    235  1.13     phil 						continue;
    236  1.13     phil 					}
    237  1.13     phil 					bcopy(data, line, datalen);
    238   1.2  deraadt 					free(data);
    239  1.13     phil 				} else {
    240  1.16    lukem 						/* YP not available? */
    241  1.16    lukem 					__ypmode = YPMODE_NONE;
    242   1.2  deraadt 					continue;
    243   1.2  deraadt 				}
    244  1.13     phil 				break;
    245   1.2  deraadt 			}
    246   1.2  deraadt 			line[datalen] = '\0';
    247   1.2  deraadt 			bp = line;
    248   1.2  deraadt 			goto parse;
    249   1.2  deraadt 		}
    250  1.16    lukem #endif /* YP */
    251   1.1      cgd 		if (!fgets(line, sizeof(line), _gr_fp))
    252   1.1      cgd 			return(0);
    253   1.1      cgd 		bp = line;
    254   1.1      cgd 		/* skip lines that are too big */
    255   1.6      jtc 		if (!strchr(line, '\n')) {
    256   1.1      cgd 			int ch;
    257   1.1      cgd 
    258   1.1      cgd 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    259   1.1      cgd 				;
    260   1.1      cgd 			continue;
    261   1.1      cgd 		}
    262   1.2  deraadt #ifdef YP
    263  1.13     phil 		if (line[0] == '+') {
    264  1.13     phil 			switch(line[1]) {
    265  1.13     phil 			case ':':
    266  1.13     phil 			case '\0':
    267  1.13     phil 			case '\n':
    268  1.16    lukem 				if (_yp_check(NULL)) {
    269  1.15  thorpej 					if (!search) {
    270  1.15  thorpej 						__ypmode = YPMODE_FULL;
    271  1.15  thorpej 						continue;
    272  1.15  thorpej 					}
    273  1.16    lukem 					if (!__ypdomain &&
    274  1.15  thorpej 					   yp_get_default_domain(&__ypdomain))
    275  1.15  thorpej 						continue;
    276  1.16    lukem 					data = NULL;
    277  1.15  thorpej 					if (name) {
    278  1.15  thorpej 						r = yp_match(__ypdomain,
    279  1.15  thorpej 							     "group.byname",
    280  1.15  thorpej 							     name, strlen(name),
    281  1.15  thorpej 							     &data, &datalen);
    282  1.15  thorpej 					} else {
    283  1.15  thorpej 						char buf[20];
    284  1.18    lukem 						snprintf(buf, sizeof(buf),
    285  1.18    lukem 						    "%u", gid);
    286  1.15  thorpej 						r = yp_match(__ypdomain,
    287  1.15  thorpej 							     "group.bygid",
    288  1.15  thorpej 							     buf, strlen(buf),
    289  1.15  thorpej 							     &data, &datalen);
    290  1.15  thorpej 					}
    291  1.16    lukem 					if (r != 0) {
    292  1.16    lukem 						if (data)
    293  1.16    lukem 							free(data);
    294  1.15  thorpej 						continue;
    295  1.16    lukem 					}
    296  1.15  thorpej 					bcopy(data, line, datalen);
    297  1.15  thorpej 					free(data);
    298  1.15  thorpej 					line[datalen] = '\0';
    299  1.15  thorpej 					bp = line;
    300  1.15  thorpej 					_gr_group.gr_name = strsep(&bp, ":\n");
    301  1.15  thorpej 					_gr_group.gr_passwd =
    302  1.16    lukem 					    strsep(&bp, ":\n");
    303  1.15  thorpej 					if (!(cp = strsep(&bp, ":\n")))
    304  1.15  thorpej 						continue;
    305  1.18    lukem 					if (name) {
    306  1.18    lukem 						id = strtoul(cp, &ep, 10);
    307  1.19    lukem 						if (id > GID_MAX || *ep != '\0')
    308  1.18    lukem 							continue;
    309  1.18    lukem 						_gr_group.gr_gid = (gid_t)id;
    310  1.18    lukem 					} else
    311  1.18    lukem 						_gr_group.gr_gid = gid;
    312  1.15  thorpej 					goto found_it;
    313  1.12       pk 				}
    314  1.13     phil 				break;
    315  1.13     phil 			default:
    316  1.16    lukem 				if (_yp_check(NULL)) {
    317  1.16    lukem 					char *tptr;
    318  1.13     phil 
    319  1.15  thorpej 					tptr = strsep(&bp, ":\n");
    320  1.16    lukem 					if (search && name &&
    321  1.16    lukem 					    strcmp(tptr, name))
    322  1.15  thorpej 						continue;
    323  1.13     phil 					__ypmode = YPMODE_NAME;
    324  1.13     phil 					grname = strdup(tptr + 1);
    325  1.12       pk 					continue;
    326  1.12       pk 				}
    327  1.13     phil 				break;
    328   1.2  deraadt 			}
    329   1.2  deraadt 		}
    330   1.2  deraadt parse:
    331  1.16    lukem #endif /* YP */
    332   1.1      cgd 		_gr_group.gr_name = strsep(&bp, ":\n");
    333   1.1      cgd 		if (search && name && strcmp(_gr_group.gr_name, name))
    334   1.1      cgd 			continue;
    335   1.1      cgd 		_gr_group.gr_passwd = strsep(&bp, ":\n");
    336   1.1      cgd 		if (!(cp = strsep(&bp, ":\n")))
    337   1.1      cgd 			continue;
    338  1.18    lukem 		id = strtoul(cp, &ep, 10);
    339  1.19    lukem 		if (id > GID_MAX || *ep != '\0')
    340  1.18    lukem 			continue;
    341  1.18    lukem 		_gr_group.gr_gid = (gid_t)id;
    342   1.1      cgd 		if (search && name == NULL && _gr_group.gr_gid != gid)
    343   1.1      cgd 			continue;
    344  1.15  thorpej 	found_it:
    345   1.8  deraadt 		cp = NULL;
    346   1.9  deraadt 		if (bp == NULL)
    347   1.9  deraadt 			continue;
    348   1.8  deraadt 		for (m = _gr_group.gr_mem = members;; bp++) {
    349   1.8  deraadt 			if (m == &members[MAXGRP - 1])
    350   1.1      cgd 				break;
    351   1.8  deraadt 			if (*bp == ',') {
    352   1.8  deraadt 				if (cp) {
    353   1.8  deraadt 					*bp = '\0';
    354   1.8  deraadt 					*m++ = cp;
    355   1.8  deraadt 					cp = NULL;
    356   1.8  deraadt 				}
    357   1.8  deraadt 			} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    358   1.8  deraadt 				if (cp) {
    359   1.8  deraadt 					*bp = '\0';
    360   1.8  deraadt 					*m++ = cp;
    361   1.8  deraadt 				}
    362   1.1      cgd 				break;
    363   1.8  deraadt 			} else if (cp == NULL)
    364   1.8  deraadt 				cp = bp;
    365   1.1      cgd 		}
    366   1.8  deraadt 		*m = NULL;
    367   1.1      cgd 		return(1);
    368   1.1      cgd 	}
    369   1.1      cgd 	/* NOTREACHED */
    370   1.1      cgd }
    371