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.8 1994/03/07 00:58:10 deraadt 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 #ifdef YP 111 __ypmode = 0; 112 if(__ypcurrent) 113 free(__ypcurrent); 114 __ypcurrent = NULL; 115 #endif 116 return(1); 117 } 118 return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0); 119 } 120 121 void 122 setgrent() 123 { 124 (void) setgroupent(0); 125 } 126 127 int 128 setgroupent(stayopen) 129 int stayopen; 130 { 131 if (!start_gr()) 132 return(0); 133 _gr_stayopen = stayopen; 134 return(1); 135 } 136 137 void 138 endgrent() 139 { 140 if (_gr_fp) { 141 (void)fclose(_gr_fp); 142 _gr_fp = NULL; 143 #ifdef YP 144 __ypmode = 0; 145 if(__ypcurrent) 146 free(__ypcurrent); 147 __ypcurrent = NULL; 148 #endif 149 } 150 } 151 152 static int 153 grscan(search, gid, name) 154 register int search, gid; 155 register char *name; 156 { 157 register char *cp, **m; 158 char *bp; 159 160 for (;;) { 161 #ifdef YP 162 if(__ypmode) { 163 char *key, *data; 164 int keylen, datalen; 165 int r; 166 167 if(!__ypdomain) { 168 if(yp_get_default_domain(&__ypdomain)) { 169 __ypmode = 0; 170 continue; 171 } 172 } 173 if(__ypcurrent) { 174 r = yp_next(__ypdomain, "group.byname", 175 __ypcurrent, __ypcurrentlen, 176 &key, &keylen, &data, &datalen); 177 free(__ypcurrent); 178 /*printf("yp_next %d\n", r);*/ 179 switch(r) { 180 case 0: 181 break; 182 default: 183 __ypcurrent = NULL; 184 __ypmode = 0; 185 free(data); 186 continue; 187 } 188 __ypcurrent = key; 189 __ypcurrentlen = keylen; 190 bcopy(data, line, datalen); 191 free(data); 192 } else { 193 r = yp_first(__ypdomain, "group.byname", 194 &__ypcurrent, &__ypcurrentlen, 195 &data, &datalen); 196 /*printf("yp_first %d\n", r);*/ 197 switch(r) { 198 case 0: 199 break; 200 default: 201 __ypmode = 0; 202 free(data); 203 continue; 204 } 205 bcopy(data, line, datalen); 206 free(data); 207 } 208 line[datalen] = '\0'; 209 /*printf("line = %s\n", line);*/ 210 bp = line; 211 goto parse; 212 } 213 #endif 214 if (!fgets(line, sizeof(line), _gr_fp)) 215 return(0); 216 bp = line; 217 /* skip lines that are too big */ 218 if (!strchr(line, '\n')) { 219 int ch; 220 221 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 222 ; 223 continue; 224 } 225 #ifdef YP 226 if ((strcmp("+\n", line) == 0) || (strncmp("+:*:0:", line, 5) == 0)) { 227 if(_yp_check(NULL)) { 228 __ypmode = 1; 229 continue; 230 } 231 } 232 parse: 233 #endif 234 _gr_group.gr_name = strsep(&bp, ":\n"); 235 if (search && name && strcmp(_gr_group.gr_name, name)) 236 continue; 237 _gr_group.gr_passwd = strsep(&bp, ":\n"); 238 if (!(cp = strsep(&bp, ":\n"))) 239 continue; 240 _gr_group.gr_gid = atoi(cp); 241 if (search && name == NULL && _gr_group.gr_gid != gid) 242 continue; 243 cp = NULL; 244 for (m = _gr_group.gr_mem = members;; bp++) { 245 if (m == &members[MAXGRP - 1]) 246 break; 247 if (*bp == ',') { 248 if (cp) { 249 *bp = '\0'; 250 *m++ = cp; 251 cp = NULL; 252 } 253 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { 254 if (cp) { 255 *bp = '\0'; 256 *m++ = cp; 257 } 258 break; 259 } else if (cp == NULL) 260 cp = bp; 261 } 262 *m = NULL; 263 return(1); 264 } 265 /* NOTREACHED */ 266 } 267