116728Swpaul/* 216728Swpaul * Copyright (c) 1989, 1993 316728Swpaul * The Regents of the University of California. All rights reserved. 416728Swpaul * 516728Swpaul * Redistribution and use in source and binary forms, with or without 616728Swpaul * modification, are permitted provided that the following conditions 716728Swpaul * are met: 816728Swpaul * 1. Redistributions of source code must retain the above copyright 916728Swpaul * notice, this list of conditions and the following disclaimer. 1016728Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1116728Swpaul * notice, this list of conditions and the following disclaimer in the 1216728Swpaul * documentation and/or other materials provided with the distribution. 13262435Sbrueffer * 3. Neither the name of the University nor the names of its contributors 1416728Swpaul * may be used to endorse or promote products derived from this software 1516728Swpaul * without specific prior written permission. 1616728Swpaul * 1716728Swpaul * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1816728Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1916728Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2016728Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2116728Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2216728Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2316728Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2416728Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2516728Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2616728Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2716728Swpaul * SUCH DAMAGE. 2816728Swpaul */ 2916728Swpaul 3031385Scharnier#ifndef lint 3131385Scharnier#if 0 3216728Swpaulstatic const char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94"; 3316728Swpaul#endif 3431385Scharnierstatic const char rcsid[] = 3550476Speter "$FreeBSD$"; 3631385Scharnier#endif /* not lint */ 3716728Swpaul 3816728Swpaul/* 3916728Swpaul * This is a slightly modified chunk of getgrent(3). All the YP support 4016728Swpaul * and unneeded functions have been stripped out. 4116728Swpaul */ 4216728Swpaul 4316728Swpaul#include <sys/types.h> 4431385Scharnier#include <grp.h> 4516728Swpaul#include <stdio.h> 4616728Swpaul#include <stdlib.h> 4716728Swpaul#include <string.h> 4816728Swpaul 4916728SwpaulFILE *_gr_fp; 5016728Swpaulstatic struct group _gr_group; 5116728Swpaulstatic int _gr_stayopen; 5290779Simpstatic int grscan(int, int); 5390779Simpstatic int start_gr(void); 5416728Swpaul 5516728Swpaul#define MAXGRP 200 5616728Swpaulstatic char *members[MAXGRP]; 5716728Swpaul#define MAXLINELENGTH 1024 5816728Swpaulstatic char line[MAXLINELENGTH]; 5916728Swpaul 6016728Swpaulstruct group * 6190779Simp_getgrent(void) 6216728Swpaul{ 6316728Swpaul if (!_gr_fp && !start_gr()) { 6416728Swpaul return NULL; 6516728Swpaul } 6616728Swpaul 6716728Swpaul 6890779Simp if (!grscan(0, 0)) 6916728Swpaul return(NULL); 7016728Swpaul return(&_gr_group); 7116728Swpaul} 7216728Swpaul 7316728Swpaulstatic int 7490779Simpstart_gr(void) 7516728Swpaul{ 7616728Swpaul return 1; 7716728Swpaul} 7816728Swpaul 7916728Swpaulint 8090779Simp_setgroupent(int stayopen) 8116728Swpaul{ 8216728Swpaul if (!start_gr()) 8316728Swpaul return(0); 8416728Swpaul _gr_stayopen = stayopen; 8516728Swpaul return(1); 8616728Swpaul} 8716728Swpaul 8816728Swpaulint 8990779Simp_setgrent(void) 9016728Swpaul{ 9116728Swpaul return(_setgroupent(0)); 9216728Swpaul} 9316728Swpaul 9416728Swpaulvoid 9590779Simp_endgrent(void) 9616728Swpaul{ 9716728Swpaul if (_gr_fp) { 9816728Swpaul (void)fclose(_gr_fp); 9916728Swpaul _gr_fp = NULL; 10016728Swpaul } 10116728Swpaul} 10216728Swpaul 10316728Swpaulstatic int 10490779Simpgrscan(int search, int gid) 10516728Swpaul{ 10690779Simp char *cp, **m; 10716728Swpaul char *bp; 10816728Swpaul for (;;) { 10916728Swpaul if (!fgets(line, sizeof(line), _gr_fp)) 11016728Swpaul return(0); 11116728Swpaul bp = line; 11216728Swpaul /* skip lines that are too big */ 113229403Sed if (!strchr(line, '\n')) { 11416728Swpaul int ch; 11516728Swpaul 11616728Swpaul while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 11716728Swpaul ; 11816728Swpaul continue; 11916728Swpaul } 12016728Swpaul if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL) 12116728Swpaul break; 12216728Swpaul if (_gr_group.gr_name[0] == '+') 12316728Swpaul continue; 12416728Swpaul if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL) 125216227Skevlo break; 12616728Swpaul if (!(cp = strsep(&bp, ":\n"))) 12716728Swpaul continue; 12816728Swpaul _gr_group.gr_gid = atoi(cp); 12990779Simp if (search && _gr_group.gr_gid != gid) 13016728Swpaul continue; 13116728Swpaul cp = NULL; 13225969Swpaul if (bp == NULL) /* !! Must check for this! */ 13325969Swpaul break; 13416728Swpaul for (m = _gr_group.gr_mem = members;; bp++) { 13516728Swpaul if (m == &members[MAXGRP - 1]) 13616728Swpaul break; 13716728Swpaul if (*bp == ',') { 13816728Swpaul if (cp) { 13916728Swpaul *bp = '\0'; 14016728Swpaul *m++ = cp; 14116728Swpaul cp = NULL; 14216728Swpaul } 14316728Swpaul } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { 14416728Swpaul if (cp) { 14516728Swpaul *bp = '\0'; 14616728Swpaul *m++ = cp; 14716728Swpaul } 14816728Swpaul break; 14916728Swpaul } else if (cp == NULL) 15016728Swpaul cp = bp; 15116728Swpaul } 15216728Swpaul *m = NULL; 15316728Swpaul return(1); 15416728Swpaul } 15516728Swpaul /* NOTREACHED */ 15616728Swpaul return (0); 15716728Swpaul} 158