1 /* $NetBSD: getgrent.c,v 1.12 1995/03/16 11:48:01 pk Exp $ */ 2 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94"; 39 #else 40 static char rcsid[] = "$NetBSD: getgrent.c,v 1.12 1995/03/16 11:48:01 pk Exp $"; 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include <sys/types.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <grp.h> 49 #ifdef YP 50 #include <rpc/rpc.h> 51 #include <rpcsvc/yp_prot.h> 52 #include <rpcsvc/ypclnt.h> 53 #endif 54 55 static FILE *_gr_fp; 56 static struct group _gr_group; 57 static int _gr_stayopen; 58 static int grscan(), start_gr(); 59 60 #define MAXGRP 200 61 static char *members[MAXGRP]; 62 #define MAXLINELENGTH 1024 63 static char line[MAXLINELENGTH]; 64 65 #ifdef YP 66 static char *__ypcurrent, *__ypdomain; 67 static int __ypcurrentlen, __ypmode=0; 68 #endif 69 70 struct group * 71 getgrent() 72 { 73 if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL)) 74 return(NULL); 75 return(&_gr_group); 76 } 77 78 struct group * 79 getgrnam(name) 80 const char *name; 81 { 82 int rval; 83 84 if (!start_gr()) 85 return(NULL); 86 rval = grscan(1, 0, name); 87 if (!_gr_stayopen) 88 endgrent(); 89 return(rval ? &_gr_group : NULL); 90 } 91 92 struct group * 93 #ifdef __STDC__ 94 getgrgid(gid_t gid) 95 #else 96 getgrgid(gid) 97 gid_t gid; 98 #endif 99 { 100 int rval; 101 102 if (!start_gr()) 103 return(NULL); 104 rval = grscan(1, gid, NULL); 105 if (!_gr_stayopen) 106 endgrent(); 107 return(rval ? &_gr_group : NULL); 108 } 109 110 static int 111 start_gr() 112 { 113 if (_gr_fp) { 114 rewind(_gr_fp); 115 #ifdef YP 116 __ypmode = 0; 117 if(__ypcurrent) 118 free(__ypcurrent); 119 __ypcurrent = NULL; 120 #endif 121 return(1); 122 } 123 return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0); 124 } 125 126 void 127 setgrent() 128 { 129 (void) setgroupent(0); 130 } 131 132 int 133 setgroupent(stayopen) 134 int stayopen; 135 { 136 if (!start_gr()) 137 return(0); 138 _gr_stayopen = stayopen; 139 return(1); 140 } 141 142 void 143 endgrent() 144 { 145 if (_gr_fp) { 146 (void)fclose(_gr_fp); 147 _gr_fp = NULL; 148 #ifdef YP 149 __ypmode = 0; 150 if(__ypcurrent) 151 free(__ypcurrent); 152 __ypcurrent = NULL; 153 #endif 154 } 155 } 156 157 static int 158 grscan(search, gid, name) 159 register int search, gid; 160 register char *name; 161 { 162 register char *cp, **m; 163 char *bp; 164 165 for (;;) { 166 #ifdef YP 167 if(__ypmode) { 168 char *key, *data; 169 int keylen, datalen; 170 int r; 171 172 if(!__ypdomain) { 173 if(yp_get_default_domain(&__ypdomain)) { 174 __ypmode = 0; 175 continue; 176 } 177 } 178 if(__ypcurrent) { 179 r = yp_next(__ypdomain, "group.byname", 180 __ypcurrent, __ypcurrentlen, 181 &key, &keylen, &data, &datalen); 182 free(__ypcurrent); 183 /*printf("yp_next %d\n", r);*/ 184 switch(r) { 185 case 0: 186 break; 187 default: 188 __ypcurrent = NULL; 189 __ypmode = 0; 190 free(data); 191 continue; 192 } 193 __ypcurrent = key; 194 __ypcurrentlen = keylen; 195 bcopy(data, line, datalen); 196 free(data); 197 } else { 198 r = yp_first(__ypdomain, "group.byname", 199 &__ypcurrent, &__ypcurrentlen, 200 &data, &datalen); 201 /*printf("yp_first %d\n", r);*/ 202 switch(r) { 203 case 0: 204 break; 205 default: 206 __ypmode = 0; 207 free(data); 208 continue; 209 } 210 bcopy(data, line, datalen); 211 free(data); 212 } 213 line[datalen] = '\0'; 214 /*printf("line = %s\n", line);*/ 215 bp = line; 216 goto parse; 217 } 218 #endif 219 if (!fgets(line, sizeof(line), _gr_fp)) 220 return(0); 221 bp = line; 222 /* skip lines that are too big */ 223 if (!strchr(line, '\n')) { 224 int ch; 225 226 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 227 ; 228 continue; 229 } 230 #ifdef YP 231 if ((strcmp("+\n", line) == 0) || 232 (strncmp("+:", line, 2) == 0)) { 233 if(_yp_check(NULL)) { 234 char gidbuf[20], *data; 235 int r, datalen; 236 237 if (!search) { 238 __ypmode = 1; 239 continue; 240 } 241 242 if(!__ypdomain) { 243 if(_yp_check(&__ypdomain) == 0) { 244 continue; 245 } 246 } 247 if (name == NULL) { 248 sprintf(gidbuf, "%d", gid); 249 r = yp_match(__ypdomain, "group.bygid", 250 gidbuf, strlen(gidbuf), 251 &data, &datalen); 252 } else { 253 r = yp_match(__ypdomain, "group.byname", 254 name, strlen(name), 255 &data, &datalen); 256 } 257 switch(r) { 258 case 0: 259 break; 260 case YPERR_MAP: 261 /* 262 * No "group.bygid" map on server, 263 * try enumeration of "group.byname". 264 */ 265 if (name == NULL) 266 __ypmode = 1; 267 default: 268 continue; 269 } 270 bcopy(data, line, datalen); 271 free(data); 272 } 273 } 274 parse: 275 #endif 276 _gr_group.gr_name = strsep(&bp, ":\n"); 277 if (search && name && strcmp(_gr_group.gr_name, name)) 278 continue; 279 _gr_group.gr_passwd = strsep(&bp, ":\n"); 280 if (!(cp = strsep(&bp, ":\n"))) 281 continue; 282 _gr_group.gr_gid = atoi(cp); 283 if (search && name == NULL && _gr_group.gr_gid != gid) 284 continue; 285 cp = NULL; 286 if (bp == NULL) 287 continue; 288 for (m = _gr_group.gr_mem = members;; bp++) { 289 if (m == &members[MAXGRP - 1]) 290 break; 291 if (*bp == ',') { 292 if (cp) { 293 *bp = '\0'; 294 *m++ = cp; 295 cp = NULL; 296 } 297 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { 298 if (cp) { 299 *bp = '\0'; 300 *m++ = cp; 301 } 302 break; 303 } else if (cp == NULL) 304 cp = bp; 305 } 306 *m = NULL; 307 return(1); 308 } 309 /* NOTREACHED */ 310 } 311