1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * This is a slightly modified chunk of getgrent(3). All the YP support 34 * and unneeded functions have been stripped out. 35 */ 36 37 #include <sys/types.h> 38 #include <grp.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 FILE *_gr_fp; 44 static struct group _gr_group; 45 static int _gr_stayopen; 46 static int grscan(int, int); 47 static int start_gr(void); 48 49 #define MAXGRP 200 50 static char *members[MAXGRP]; 51 #define MAXLINELENGTH 1024 52 static char line[MAXLINELENGTH]; 53 54 struct group * 55 _getgrent(void) 56 { 57 if (!_gr_fp && !start_gr()) { 58 return NULL; 59 } 60 61 62 if (!grscan(0, 0)) 63 return(NULL); 64 return(&_gr_group); 65 } 66 67 static int 68 start_gr(void) 69 { 70 return 1; 71 } 72 73 int 74 _setgroupent(int stayopen) 75 { 76 if (!start_gr()) 77 return(0); 78 _gr_stayopen = stayopen; 79 return(1); 80 } 81 82 int 83 _setgrent(void) 84 { 85 return(_setgroupent(0)); 86 } 87 88 void 89 _endgrent(void) 90 { 91 if (_gr_fp) { 92 (void)fclose(_gr_fp); 93 _gr_fp = NULL; 94 } 95 } 96 97 static int 98 grscan(int search, int gid) 99 { 100 char *cp, **m; 101 char *bp; 102 for (;;) { 103 if (!fgets(line, sizeof(line), _gr_fp)) 104 return(0); 105 bp = line; 106 /* skip lines that are too big */ 107 if (!strchr(line, '\n')) { 108 int ch; 109 110 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 111 ; 112 continue; 113 } 114 if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL) 115 break; 116 if (_gr_group.gr_name[0] == '+') 117 continue; 118 if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL) 119 break; 120 if (!(cp = strsep(&bp, ":\n"))) 121 continue; 122 _gr_group.gr_gid = atoi(cp); 123 if (search && _gr_group.gr_gid != gid) 124 continue; 125 cp = NULL; 126 if (bp == NULL) /* !! Must check for this! */ 127 break; 128 for (m = _gr_group.gr_mem = members;; bp++) { 129 if (m == &members[MAXGRP - 1]) 130 break; 131 if (*bp == ',') { 132 if (cp) { 133 *bp = '\0'; 134 *m++ = cp; 135 cp = NULL; 136 } 137 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { 138 if (cp) { 139 *bp = '\0'; 140 *m++ = cp; 141 } 142 break; 143 } else if (cp == NULL) 144 cp = bp; 145 } 146 *m = NULL; 147 return(1); 148 } 149 /* NOTREACHED */ 150 return (0); 151 } 152