/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1989, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* This is a slightly modified chunk of getgrent(3). All the YP support33* and unneeded functions have been stripped out.34*/3536#include <sys/types.h>37#include <grp.h>38#include <stdio.h>39#include <stdlib.h>40#include <string.h>4142FILE *_gr_fp;43static struct group _gr_group;44static int _gr_stayopen;45static int grscan(int, int);46static int start_gr(void);4748#define MAXGRP 20049static char *members[MAXGRP];50#define MAXLINELENGTH 102451static char line[MAXLINELENGTH];5253struct group *54_getgrent(void)55{56if (!_gr_fp && !start_gr()) {57return NULL;58}596061if (!grscan(0, 0))62return(NULL);63return(&_gr_group);64}6566static int67start_gr(void)68{69return 1;70}7172int73_setgroupent(int stayopen)74{75if (!start_gr())76return(0);77_gr_stayopen = stayopen;78return(1);79}8081int82_setgrent(void)83{84return(_setgroupent(0));85}8687void88_endgrent(void)89{90if (_gr_fp) {91(void)fclose(_gr_fp);92_gr_fp = NULL;93}94}9596static int97grscan(int search, int gid)98{99char *cp, **m;100char *bp;101for (;;) {102if (!fgets(line, sizeof(line), _gr_fp))103return(0);104bp = line;105/* skip lines that are too big */106if (!strchr(line, '\n')) {107int ch;108109while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)110;111continue;112}113if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)114break;115if (_gr_group.gr_name[0] == '+')116continue;117if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL)118break;119if (!(cp = strsep(&bp, ":\n")))120continue;121_gr_group.gr_gid = atoi(cp);122if (search && _gr_group.gr_gid != gid)123continue;124cp = NULL;125if (bp == NULL) /* !! Must check for this! */126break;127for (m = _gr_group.gr_mem = members;; bp++) {128if (m == &members[MAXGRP - 1])129break;130if (*bp == ',') {131if (cp) {132*bp = '\0';133*m++ = cp;134cp = NULL;135}136} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {137if (cp) {138*bp = '\0';139*m++ = cp;140}141break;142} else if (cp == NULL)143cp = bp;144}145*m = NULL;146return(1);147}148/* NOTREACHED */149return (0);150}151152153