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.3 1993/07/30 08:22:26 mycroft 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 int 116 setgrent() 117 { 118 return(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 char *fgets(), *strsep(), *index(); 148 149 for (;;) { 150 #ifdef YP 151 if(__ypmode) { 152 char *key, *data; 153 int keylen, datalen; 154 int r; 155 156 if(!__ypdomain) { 157 if(yp_get_default_domain(&__ypdomain)) { 158 __ypmode = 0; 159 continue; 160 } 161 } 162 if(__ypcurrent) { 163 r = yp_next(__ypdomain, "group.byname", 164 __ypcurrent, __ypcurrentlen, 165 &key, &keylen, &data, &datalen); 166 free(__ypcurrent); 167 /*printf("yp_next %d\n", r);*/ 168 switch(r) { 169 case 0: 170 break; 171 default: 172 __ypcurrent = NULL; 173 __ypmode = 0; 174 free(data); 175 continue; 176 } 177 __ypcurrent = key; 178 __ypcurrentlen = keylen; 179 bcopy(data, line, datalen); 180 free(data); 181 } else { 182 r = yp_first(__ypdomain, "group.byname", 183 &__ypcurrent, &__ypcurrentlen, 184 &data, &datalen); 185 /*printf("yp_first %d\n", r);*/ 186 switch(r) { 187 case 0: 188 break; 189 default: 190 __ypmode = 0; 191 free(data); 192 continue; 193 } 194 bcopy(data, line, datalen); 195 free(data); 196 } 197 line[datalen] = '\0'; 198 /*printf("line = %s\n", line);*/ 199 bp = line; 200 goto parse; 201 } 202 #endif 203 if (!fgets(line, sizeof(line), _gr_fp)) 204 return(0); 205 bp = line; 206 /* skip lines that are too big */ 207 if (!index(line, '\n')) { 208 int ch; 209 210 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF) 211 ; 212 continue; 213 } 214 #ifdef YP 215 if ((strcmp("+\n", line) == 0) || (strncmp("+:*:0:", line, 5) == 0)) { 216 if(_yp_check(NULL)) { 217 __ypmode = 1; 218 continue; 219 } 220 } 221 parse: 222 #endif 223 _gr_group.gr_name = strsep(&bp, ":\n"); 224 if (search && name && strcmp(_gr_group.gr_name, name)) 225 continue; 226 _gr_group.gr_passwd = strsep(&bp, ":\n"); 227 if (!(cp = strsep(&bp, ":\n"))) 228 continue; 229 _gr_group.gr_gid = atoi(cp); 230 if (search && name == NULL && _gr_group.gr_gid != gid) 231 continue; 232 for (m = _gr_group.gr_mem = members;; ++m) { 233 if (m == &members[MAXGRP - 1]) { 234 *m = NULL; 235 break; 236 } 237 if ((*m = strsep(&bp, ", \n")) == NULL) 238 break; 239 } 240 return(1); 241 } 242 /* NOTREACHED */ 243 } 244