1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 /*static char *sccsid = "from: @(#)getgrent.c 5.9 (Berkeley) 4/1/91";*/ 36 static char *rcsid = "$Id: getgrent.c,v 1.6 1993/11/24 19:43:54 jtc Exp $"; 37 #endif /* LIBC_SCCS and not lint */ 38 39 #include <sys/types.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <grp.h> 44 #ifdef YP 45 #include <rpc/rpc.h> 46 #include <rpcsvc/yp_prot.h> 47 #include <rpcsvc/ypclnt.h> 48 #endif 49 50 static FILE *_gr_fp; 51 static struct group _gr_group; 52 static int _gr_stayopen; 53 static int grscan(), start_gr(); 54 55 #define MAXGRP 200 56 static char *members[MAXGRP]; 57 #define MAXLINELENGTH 1024 58 static char line[MAXLINELENGTH]; 59 60 #ifdef YP 61 static char *__ypcurrent, *__ypdomain; 62 static int __ypcurrentlen, __ypmode=0; 63 #endif 64 65 struct group * 66 getgrent() 67 { 68 if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL)) 69 return(NULL); 70 return(&_gr_group); 71 } 72 73 struct group * 74 getgrnam(name) 75 const char *name; 76 { 77 int rval; 78 79 if (!start_gr()) 80 return(NULL); 81 rval = grscan(1, 0, name); 82 if (!_gr_stayopen) 83 endgrent(); 84 return(rval ? &_gr_group : NULL); 85 } 86 87 struct group * 88 #ifdef __STDC__ 89 getgrgid(gid_t gid) 90 #else 91 getgrgid(gid) 92 gid_t gid; 93 #endif 94 { 95 int rval; 96 97 if (!start_gr()) 98 return(NULL); 99 rval = grscan(1, gid, NULL); 100 if (!_gr_stayopen) 101 endgrent(); 102 return(rval ? &_gr_group : NULL); 103 } 104 105 static int 106 start_gr() 107 { 108 if (_gr_fp) { 109 rewind(_gr_fp); 110 return(1); 111 } 112 return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0); 113 } 114 115 void 116 setgrent() 117 { 118 (void) setgroupent(0); 119 } 120 121 int 122 setgroupent(stayopen) 123 int stayopen; 124 { 125 if (!start_gr()) 126 return(0); 127 _gr_stayopen = stayopen; 128 return(1); 129 } 130 131 void 132 endgrent() 133 { 134 if (_gr_fp) { 135 (void)fclose(_gr_fp); 136 _gr_fp = NULL; 137 } 138 } 139 140 static int 141 grscan(search, gid, name) 142 register int search, gid; 143 register char *name; 144 { 145 register char *cp, **m; 146 char *bp; 147 148 for (;;) { 149 #ifdef YP 150 if(__ypmode) { 151 char *key, *data; 152 int keylen, datalen; 153 int r; 154 155 if(!__ypdomain) { 156 if(yp_get_default_domain(&__ypdomain)) { 157 __ypmode = 0; 158 continue; 159 } 160 } 161 if(__ypcurrent) { 162 r = yp_next(__ypdomain, "group.byname", 163 __ypcurrent, __ypcurrentlen, 164 &key, &keylen, &data, &datalen); 165 free(__ypcurrent); 166 /*printf("yp_next %d\n", r);*/ 167 switch(r) { 168 case 0: 169 break; 170 default: 171 __ypcurrent = NULL; 172 __ypmode = 0; 173 free(data); 174 continue; 175 } 176 __ypcurrent = key; 177 __ypcurrentlen = keylen; 178 bcopy(data, line, datalen); 179 free(data); 180 } else { 181 r = yp_first(__ypdomain, "group.byname", 182 &__ypcurrent, &__ypcurrentlen, 183 &data, &datalen); 184 /*printf("yp_first %d\n", r);*/ 185 switch(r) { 186 case 0: 187 break; 188 default: 189 __ypmode = 0; 190 free(data); 191 continue; 192 } 193 bcopy(data, line, datalen); 194 free(data); 195 } 196 line[datalen] = '\0'; 197 /*printf("line = %s\n", line);*/ 198 bp = line; 199 goto parse; 200 } 201 #endif 202 if (!fgets(line, sizeof(line), _gr_fp)) 203 return(0); 204 bp = line; 205 /* skip lines that are too big */ 206 if (!strchr(line, '\n')) { 207 int ch; 208 209 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 210 ; 211 continue; 212 } 213 #ifdef YP 214 if ((strcmp("+\n", line) == 0) || (strncmp("+:*:0:", line, 5) == 0)) { 215 if(_yp_check(NULL)) { 216 __ypmode = 1; 217 continue; 218 } 219 } 220 parse: 221 #endif 222 _gr_group.gr_name = strsep(&bp, ":\n"); 223 if (search && name && strcmp(_gr_group.gr_name, name)) 224 continue; 225 _gr_group.gr_passwd = strsep(&bp, ":\n"); 226 if (!(cp = strsep(&bp, ":\n"))) 227 continue; 228 _gr_group.gr_gid = atoi(cp); 229 if (search && name == NULL && _gr_group.gr_gid != gid) 230 continue; 231 for (m = _gr_group.gr_mem = members;; ++m) { 232 if (m == &members[MAXGRP - 1]) { 233 *m = NULL; 234 break; 235 } 236 if ((*m = strsep(&bp, ", \n")) == NULL) 237 break; 238 } 239 return(1); 240 } 241 /* NOTREACHED */ 242 } 243