From ca09eb424cb0b1b019b46a928a5f9398dc578268 Mon Sep 17 00:00:00 2001 From: Bill Paul Date: Tue, 25 Jun 1996 20:26:38 +0000 Subject: [PATCH] (This import had better work correctly or so help me I'll move to the Himalayas and become a hermit.) Import new mknetid program. This replaces the crufty, soon to be defunct mknetid script packaged with ypserv. This program parses the group, passwd, hosts and netid databases into the netid.byname map. Duplicate checking is performed using hash tables. Testing on my 486DX2/66 with FreeBSD 2.1.0 showed that this program can process a 30,000-entry passwd database into a netid map (along with assorted group and hosts information) in about 22 seconds. On my SPARC IPX with SunOS 4.1.3, it takes about 15 seconds. This compares favorably with the SunOS mknetid program, which parses the same database(s) in 13 seconds. (With smaller databases, my program is actually slightly faster. Go figure.) --- libexec/mknetid/Makefile | 8 + libexec/mknetid/hash.c | 179 +++++++++++++++++++++ libexec/mknetid/hash.h | 54 +++++++ libexec/mknetid/mknetid.8 | 141 +++++++++++++++++ libexec/mknetid/mknetid.c | 289 ++++++++++++++++++++++++++++++++++ libexec/mknetid/parse_group.c | 167 ++++++++++++++++++++ 6 files changed, 838 insertions(+) create mode 100644 libexec/mknetid/Makefile create mode 100644 libexec/mknetid/hash.c create mode 100644 libexec/mknetid/hash.h create mode 100644 libexec/mknetid/mknetid.8 create mode 100644 libexec/mknetid/mknetid.c create mode 100644 libexec/mknetid/parse_group.c diff --git a/libexec/mknetid/Makefile b/libexec/mknetid/Makefile new file mode 100644 index 000000000000..9a3e82bf61bd --- /dev/null +++ b/libexec/mknetid/Makefile @@ -0,0 +1,8 @@ +# $Id: Makefile,v 1.1 1996/06/24 00:05:28 wpaul Exp $ + +PROG= mknetid +SRCS= mknetid.c hash.c parse_group.c + +MAN8= mknetid.8 + +.include diff --git a/libexec/mknetid/hash.c b/libexec/mknetid/hash.c new file mode 100644 index 000000000000..ec4167279ee3 --- /dev/null +++ b/libexec/mknetid/hash.c @@ -0,0 +1,179 @@ +/* + * Copyright (c) 1995 + * Bill Paul . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Bill Paul. + * 4. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: hash.c,v 1.3 1996/06/24 22:48:50 wpaul Exp $ + */ + +#include +#include +#include +#include +#include "hash.h" + +#ifndef lint +static const char rcsid[] = "$Id: hash.c,v 1.3 1996/06/24 22:48:50 wpaul Exp $"; +#endif + + +/* + * This hash function is stolen directly from the + * Berkeley DB package. It already exists inside libc, but + * it's declared static which prevents us from calling it + * from here. + */ +/* + * OZ's original sdbm hash + */ +u_int32_t +hash(keyarg, len) + const void *keyarg; + register size_t len; +{ + register const u_char *key; + register size_t loop; + register u_int32_t h; + +#define HASHC h = *key++ + 65599 * h + + h = 0; + key = keyarg; + if (len > 0) { + loop = (len + 8 - 1) >> 3; + + switch (len & (8 - 1)) { + case 0: + do { + HASHC; + /* FALLTHROUGH */ + case 7: + HASHC; + /* FALLTHROUGH */ + case 6: + HASHC; + /* FALLTHROUGH */ + case 5: + HASHC; + /* FALLTHROUGH */ + case 4: + HASHC; + /* FALLTHROUGH */ + case 3: + HASHC; + /* FALLTHROUGH */ + case 2: + HASHC; + /* FALLTHROUGH */ + case 1: + HASHC; + } while (--loop); + } + } + return (h); +} + +/* + * Generate a hash value for a given key (character string). + * We mask off all but the lower 8 bits since our table array + * can only hole 256 elements. + */ +u_int32_t hashkey(key) + char *key; +{ + + if (key == NULL) + return (-1); + return(hash((void *)key, strlen(key)) & HASH_MASK); +} + +/* Find an entry in the hash table (may be hanging off a linked list). */ +struct grouplist *lookup(table, key) + struct member_entry *table[]; + char *key; +{ + struct member_entry *cur; + + cur = table[hashkey(key)]; + + while (cur) { + if (!strcmp(cur->key, key)) + return(cur->groups); + cur = cur->next; + } + + return(NULL); +} + +struct grouplist dummy = { 99999, NULL }; + +/* + * Store an group member entry and/or update its grouplist. + */ +void mstore (table, key, gid, dup) + struct member_entry *table[]; + char *key; + int gid; + int dup; +{ + struct member_entry *cur, *new; + struct grouplist *tmp; + u_int32_t i; + + i = hashkey(key); + cur = table[i]; + + if (!dup) { + tmp = (struct grouplist *)malloc(sizeof(struct grouplist)); + tmp->groupid = gid; + tmp->next = NULL; + } + + /* Check if all we have to do is insert a new groupname. */ + while (cur) { + if (!dup && !strcmp(cur->key, key)) { + tmp->next = cur->groups; + cur->groups = tmp; + return; + } + cur = cur->next; + } + + /* Didn't find a match -- add the whole mess to the table. */ + new = (struct member_entry *)malloc(sizeof(struct member_entry)); + new->key = strdup(key); + if (!dup) + new->groups = tmp; + else + new->groups = (struct grouplist *)&dummy; + new->next = table[i]; + table[i] = new; + + return; +} diff --git a/libexec/mknetid/hash.h b/libexec/mknetid/hash.h new file mode 100644 index 000000000000..1023e3b32515 --- /dev/null +++ b/libexec/mknetid/hash.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 1995 + * Bill Paul . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Bill Paul. + * 4. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: hash.h,v 1.3 1996/06/24 22:48:32 wpaul Exp $ + */ + +/* Groupit entry hung off a member_entry node. */ +struct grouplist { + gid_t groupid; + struct grouplist *next; +}; + +/* Entry in the cooked member list hash table. */ +struct member_entry { + char *key; /* username */ + struct grouplist *groups; + struct member_entry *next; +}; + +/* Table size (chosen arbitrarily). Not too big, not too small. */ +#define TABLESIZE 256 +#define HASH_MASK 0x000000FF + +extern void mstore __P(( struct member_entry ** , char *, int, int )); +extern struct grouplist *lookup __P(( struct member_entry **, char * )); + diff --git a/libexec/mknetid/mknetid.8 b/libexec/mknetid/mknetid.8 new file mode 100644 index 000000000000..f246418ab95d --- /dev/null +++ b/libexec/mknetid/mknetid.8 @@ -0,0 +1,141 @@ +.\" Copyright (c) 1995, 1996 +.\" Bill Paul . All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by Bill Paul. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $Id: mknetid.8,v 1.2 1996/06/25 02:53:44 wpaul Exp $ +.\" +.Dd June 23, 1996 +.Dt MKNETID 8 +.Os +.Sh NAME +.Nm mknetid +.Nd "generate netid map data" +.Sh SYNOPSIS +.Nm mknetid +.Op Fl q +.Op Fl g Ar group_file +.Op Fl p Ar passwd_file +.Op Fl h Ar hosts_file +.Op Fl n Ar netid_file +.Op Fl d Ar domain +.Sh DESCRIPTION +.Nm Mknetid +processes the contents of the +.Xr group 5 , +.Xr passwd 5 , +.Xr hosts 5 and +.Xr netid 5 +files into the format used to generate the +.Pa netid.byname +NIS map. This map is used to hold credential information for both users +and hosts in a operating system independent format. +.Pp +The +.Nm mknetid +command checks for duplicate occurances of netids and filters +them out. +.Pp +The +.Nm mknetid +command prints its results on the standard output. It is usually called +only by +.Nm /var/yp/Makefile +when rebuilding the NIS maps. +.Pp +.Sh OPTIONS +The +.Nm mknetid +command supports the following options: +.Bl -tag -width flag +.It Fl q +Normally, +.Nm mknetid +prints a warning message when it encounters a duplicate netid. +This flag turns on 'quiet' mode, allowing the warnings to be +surpressed. Other error messages may still be generated. +.It Fl g Ar group-file +The +.Fl g +flag can be used to specify the location of the group information +file. The compiled-in default is +.Pa /etc/group . +.It Fl p Ar passwd-file +The +.Fl p +flag can be used to specify the location of the passwd information +file. The compiled-in default is +.Pa /etc/passwd . +.It Fl h Ar group-file +The +.Fl h +flag can be used to specify the location of the hosts database +file. The compiled-in default is +.Pa /etc/hosts . +.It Fl n Ar netid-file +The +.Fl n +flag can be used to specify the location of the netid information +file. The compiled-in default is +.Pa /etc/netid . +Note that no error is generated if the netid database can't be +found. The netid database is not likely to be present on most systems +until Secure RPC support is added to FreeBSD. +.It Fl d Ar domain +By default, the +.Nm mknetid +command uses the system domainname when generating netid records. If +the system domainnameis not set, the domain must be specified on the +command line with the +.Fl d +flag. If the domainname is set, the +.Fl d +flag may be used to override it. +.El +.Sh FILES +.Bl -tag -width Pa -compact +.It Pa /var/yp/Makefile +The Makefile that calls +.Nm yp_mkdb +and +.Nm mknetid +to build the NIS databases. +.It Pa /etc/group +The default group database file. +.It Pa /etc/passwd +The default passwd database file. +.It Pa /etc/hosts +The default hosts database file. +.It Pa /etc/netid +The default netid database file. +.El +.Sh SEE ALSO +.Xr yp 4 , +.Xr yp_mkdb 8 +.Sh AUTHOR +Bill Paul diff --git a/libexec/mknetid/mknetid.c b/libexec/mknetid/mknetid.c new file mode 100644 index 000000000000..ee306cd55492 --- /dev/null +++ b/libexec/mknetid/mknetid.c @@ -0,0 +1,289 @@ +/* + * Copyright (c) 1995, 1996 + * Bill Paul . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Bill Paul. + * 4. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * netid map generator program + * + * Written by Bill Paul + * Center for Telecommunications Research + * Columbia University, New York City + * + * $Id: mknetid.c,v 1.5 1996/06/24 22:48:15 wpaul Exp $ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "hash.h" + +#ifndef lint +static const char rcsid[] = "$Id: mknetid.c,v 1.5 1996/06/24 22:48:15 wpaul Exp $"; +#endif + +#define LINSIZ 1024 +#define OPSYS "unix" + +/* Default location of group file. */ +char *groupfile = _PATH_GROUP; +/* Default location of master.passwd file. */ +char *passfile = _PATH_PASSWD; +/* Default location of hosts file. */ +char *hostsfile = _PATH_HOSTS; +/* Default location of netid file */ +char *netidfile = "/etc/netid"; + +/* + * Stored hash table of 'reverse' group member database + * which we will construct. + */ +struct member_entry *mtable[TABLESIZE]; + +/* + * Dupe table: used to keep track of entries so we don't + * print the same thing twice. + */ +struct member_entry *dtable[TABLESIZE]; + +extern struct group *_getgrent __P(( void )); +extern int _setgrent __P(( void )); +extern void _endgrent __P(( void )); +void usage(prog) +char *prog; +{ + fprintf (stderr,"usage: %s [-q] [-g group file] [-p passwd file] \ +[-h hosts file]\n\t\t\t[-d netid file] [-d domain]\n",prog); + exit(1); +} + +extern char *optarg; +extern FILE *_gr_fp; + +main(argc, argv) + int argc; + char *argv[]; +{ + FILE *gfp, *pfp, *hfp, *nfp; + char readbuf[LINSIZ]; + char writebuf[LINSIZ]; + struct group *gr; + struct grouplist *glist; + char *domain; + int ch; + gid_t i; + char *ptr, *pidptr, *gidptr, *hptr; + int quiet = 0; + + while ((ch = getopt(argc, argv, "g:p:h:n:d:q")) != EOF) { + switch(ch) { + case 'g': + groupfile = optarg; + break; + case 'p': + passfile = optarg; + break; + case 'h': + hostsfile = optarg; + break; + case 'n': + netidfile = optarg; + break; + case 'd': + domain = optarg; + break; + case 'q': + quiet++; + break; + default: + usage(argv[0]); + break; + } + } + + if (domain == NULL) { + if (yp_get_default_domain(&domain)) + errx(1, "no domain name specified and default \ +domain not set"); + } + + if ((gfp = fopen(groupfile, "r")) == NULL) { + err(1, "%s", groupfile); + } + + if ((pfp = fopen(passfile, "r")) == NULL) { + err(1, "%s", passfile); + } + + if ((hfp = fopen(hostsfile, "r")) == NULL) { + err(1, "%s", hostsfile); + } + + if ((nfp = fopen(netidfile, "r")) == NULL) { + /* netid is optional -- just continue */ + nfp = NULL; + } + + _gr_fp = gfp; + + /* Load all the group membership info into a hash table. */ + + _setgrent(); + while((gr = _getgrent()) != NULL) { + while(*gr->gr_mem) { + mstore(mtable, *gr->gr_mem, gr->gr_gid, 0); + gr->gr_mem++; + } + } + + fclose(gfp); + _endgrent(); + + /* + * Now parse the passwd database, spewing out the extra + * group information we just stored if necessary. + */ + while(fgets(readbuf, LINSIZ, pfp)) { + if ((ptr = strchr(readbuf, ':')) == NULL) + warnx("bad passwd file entry: %s", readbuf); + *ptr = '\0'; + ptr++; + if ((ptr = strchr(ptr, ':')) == NULL) + warnx("bad passwd file entry: %s", readbuf); + *ptr = '\0'; + ptr++; + pidptr = ptr; + if ((ptr = strchr(ptr, ':')) == NULL) + warnx("bad passwd file entry: %s", readbuf); + *ptr = '\0'; + ptr++; + gidptr = ptr; + if ((ptr = strchr(ptr, ':')) == NULL) + warnx("bad passwd file entry: %s", readbuf); + *ptr = '\0'; + i = atol(gidptr); + + snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS, + pidptr, domain); + + if (lookup(dtable, writebuf)) { + if (!quiet) + warnx("duplicate netid '%s.%s@%s' -- skipping", + OPSYS, pidptr, domain); + continue; + } else { + mstore(dtable, writebuf, 0, 1); + } + printf("%s.%s@%s\t\t%s:%s", OPSYS, pidptr, domain, pidptr, gidptr); + if ((glist = lookup(mtable, (char *)&readbuf)) != NULL) { + while(glist) { + if (glist->groupid != i) + printf(",%lu", glist->groupid); + glist = glist->next; + } + } + printf ("\n"); + } + + fclose(pfp); + + /* + * Now parse the hosts database (this part sucks). + */ + + while ((ptr = fgets(readbuf, LINSIZ, hfp))) { + if (*ptr == '#') + continue; + if (!(hptr = strpbrk(ptr, "#\n"))) + continue; + *hptr = '\0'; + if (!(hptr = strpbrk(ptr, " \t"))) + continue; + *hptr++ = '\0'; + ptr = hptr; + while (*ptr == ' ' || *ptr == '\t') + ptr++; + if (!(hptr = strpbrk(ptr, " \t"))) + continue; + *hptr++ = '\0'; + snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS, + ptr, domain); + if (lookup(dtable, (char *)&writebuf)) { + if (!quiet) + warnx("duplicate netid '%s' -- skipping", + writebuf); + continue; + } else { + mstore(dtable, (char *)&writebuf, 0, 1); + } + printf ("%s.%s@%s\t\t0:%s\n", OPSYS, ptr, domain, ptr); + } + + fclose(hfp); + + /* + * Lastly, copy out any extra information in the netid + * file. If it's not open, just ignore it: it's optional anyway. + */ + + if (nfp != NULL) { + while(fgets(readbuf, LINSIZ, nfp)) { + if (readbuf[0] == '#') + continue; + if ((ptr = strpbrk((char*)&readbuf, " \t")) == NULL) { + warnx("bad netid entry: '%s'", readbuf); + continue; + } + + writebuf[0] = *ptr; + *ptr = '\0'; + snprintf(writebuf, sizeof(readbuf), "%s.%s@%s", OPSYS, + ptr, domain); + if (lookup(dtable, (char *)&readbuf)) { + if (!quiet) + warnx("duplicate netid '%s' -- skipping", + readbuf); + continue; + } else { + mstore(dtable, (char *)&readbuf, 0, 1); + } + *ptr = writebuf[0]; + printf("%s",readbuf); + } + fclose(nfp); + } + + exit(0); +} diff --git a/libexec/mknetid/parse_group.c b/libexec/mknetid/parse_group.c new file mode 100644 index 000000000000..9f6709c44329 --- /dev/null +++ b/libexec/mknetid/parse_group.c @@ -0,0 +1,167 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static const char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94"; +#endif /* LIBC_SCCS and not lint */ + +#ifndef lint +static const char rcsid[] = "$Id: parse_group.c,v 1.5 1996/06/24 00:12:09 wpaul Exp $"; +#endif + +/* + * This is a slightly modified chunk of getgrent(3). All the YP support + * and unneeded functions have been stripped out. + */ + +#include +#include +#include +#include +#include + +FILE *_gr_fp; +static struct group _gr_group; +static int _gr_stayopen; +static int grscan(), start_gr(); + +#define MAXGRP 200 +static char *members[MAXGRP]; +#define MAXLINELENGTH 1024 +static char line[MAXLINELENGTH]; + +struct group * +_getgrent() +{ + if (!_gr_fp && !start_gr()) { + return NULL; + } + + + if (!grscan(0, 0, NULL)) + return(NULL); + return(&_gr_group); +} + +static int +start_gr() +{ + return 1; +} + +int +_setgroupent(stayopen) + int stayopen; +{ + if (!start_gr()) + return(0); + _gr_stayopen = stayopen; + return(1); +} + +int +_setgrent() +{ + return(_setgroupent(0)); +} + +void +_endgrent() +{ + if (_gr_fp) { + (void)fclose(_gr_fp); + _gr_fp = NULL; + } +} + +static int +grscan(search, gid, name) + register int search, gid; + register char *name; +{ + register char *cp, **m; + char *bp; + for (;;) { + if (!fgets(line, sizeof(line), _gr_fp)) + return(0); + bp = line; + /* skip lines that are too big */ + if (!index(line, '\n')) { + int ch; + + while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) + ; + continue; + } + if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL) + break; + if (_gr_group.gr_name[0] == '+') + continue; + + if (search && name) { + if(strcmp(_gr_group.gr_name, name)) { + continue; + } + } + if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL) + break;; + if (!(cp = strsep(&bp, ":\n"))) + continue; + _gr_group.gr_gid = atoi(cp); + if (search && name == NULL && _gr_group.gr_gid != gid) + continue; + cp = NULL; + for (m = _gr_group.gr_mem = members;; bp++) { + if (m == &members[MAXGRP - 1]) + break; + if (*bp == ',') { + if (cp) { + *bp = '\0'; + *m++ = cp; + cp = NULL; + } + } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { + if (cp) { + *bp = '\0'; + *m++ = cp; + } + break; + } else if (cp == NULL) + cp = bp; + } + *m = NULL; + return(1); + } + /* NOTREACHED */ + return (0); +}