1 /* $NetBSD: getgrent.c,v 1.34.2.1 1999/04/27 14:10:58 perry Exp $ */ 2 /* 3 * Copyright (c) 1989, 1993 4 * The Regents of the University of California. All rights reserved. 5 * Portions Copyright (c) 1994, Jason Downs. 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 static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94"; 38 static char rcsid[] = 39 "$FreeBSD: src/lib/libc/gen/getgrent.c,v 1.17.6.1 2001/03/05 08:56:02 obrien Exp $"; 40 #endif /* LIBC_SCCS and not lint */ 41 42 #include <errno.h> 43 #include <limits.h> 44 #include <sys/types.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <grp.h> 49 #include <syslog.h> 50 51 static FILE *_gr_fp; 52 static struct group _gr_group; 53 static int _gr_stayopen; 54 static int grscan(), start_gr(); 55 #ifdef YP 56 #include <rpc/rpc.h> 57 #include <rpcsvc/yp_prot.h> 58 #include <rpcsvc/ypclnt.h> 59 static int _gr_stepping_yp; 60 static int _gr_yp_enabled; 61 static int _getypgroup(struct group *, const char *, char *); 62 static int _nextypgroup(struct group *); 63 #endif 64 65 /* initial size for malloc and increase steps for realloc */ 66 #define MAXGRP 64 67 #define MAXLINELENGTH 256 68 69 static char **members; /* list of group members */ 70 static int maxgrp; /* current length of **mebers */ 71 static char *line; /* temp buffer for group line */ 72 static int maxlinelength; /* current length of *line */ 73 74 /* 75 * Lines longer than MAXLINELENGTHLIMIT will be counted as an error. 76 * <= 0 disable check for maximum line length 77 * 256K is enough for 64,000 uids 78 */ 79 #define MAXLINELENGTHLIMIT (256 * 1024) 80 #define GROUP_IGNORE_COMMENTS 1 /* allow comments in /etc/group */ 81 82 struct group * 83 getgrent() 84 { 85 if (!_gr_fp && !start_gr()) { 86 return NULL; 87 } 88 89 #ifdef YP 90 if (_gr_stepping_yp) { 91 if (_nextypgroup(&_gr_group)) 92 return(&_gr_group); 93 } 94 tryagain: 95 #endif 96 97 if (!grscan(0, 0, NULL)) 98 return(NULL); 99 #ifdef YP 100 if(_gr_group.gr_name[0] == '+' && _gr_group.gr_name[1]) { 101 _getypgroup(&_gr_group, &_gr_group.gr_name[1], 102 "group.byname"); 103 } else if(_gr_group.gr_name[0] == '+') { 104 if (!_nextypgroup(&_gr_group)) 105 goto tryagain; 106 else 107 return(&_gr_group); 108 } 109 #endif 110 return(&_gr_group); 111 } 112 113 struct group * 114 getgrnam(name) 115 const char *name; 116 { 117 int rval; 118 119 if (!start_gr()) 120 return(NULL); 121 #ifdef YP 122 tryagain: 123 #endif 124 rval = grscan(1, 0, name); 125 #ifdef YP 126 if(rval == -1 && (_gr_yp_enabled < 0 || (_gr_yp_enabled && 127 _gr_group.gr_name[0] == '+'))) { 128 if (!(rval = _getypgroup(&_gr_group, name, "group.byname"))) 129 goto tryagain; 130 } 131 #endif 132 if (!_gr_stayopen) 133 endgrent(); 134 return (rval) ? &_gr_group : NULL; 135 } 136 137 struct group * 138 getgrgid(gid) 139 gid_t gid; 140 { 141 int rval; 142 143 if (!start_gr()) 144 return(NULL); 145 #ifdef YP 146 tryagain: 147 #endif 148 rval = grscan(1, gid, NULL); 149 #ifdef YP 150 if(rval == -1 && _gr_yp_enabled) { 151 char buf[16]; 152 snprintf(buf, sizeof buf, "%d", (unsigned)gid); 153 if (!(rval = _getypgroup(&_gr_group, buf, "group.bygid"))) 154 goto tryagain; 155 } 156 #endif 157 if (!_gr_stayopen) 158 endgrent(); 159 return (rval) ? &_gr_group : NULL; 160 } 161 162 static int 163 start_gr() 164 { 165 if (_gr_fp) { 166 rewind(_gr_fp); 167 return(1); 168 } 169 _gr_fp = fopen(_PATH_GROUP, "r"); 170 if(!_gr_fp) return 0; 171 #ifdef YP 172 /* 173 * This is a disgusting hack, used to determine when YP is enabled. 174 * This would be easier if we had a group database to go along with 175 * the password database. 176 */ 177 { 178 char *line; 179 size_t linelen; 180 _gr_yp_enabled = 0; 181 while((line = fgetln(_gr_fp, &linelen)) != NULL) { 182 if(line[0] == '+') { 183 if(line[1] && line[1] != ':' && !_gr_yp_enabled) { 184 _gr_yp_enabled = 1; 185 } else { 186 _gr_yp_enabled = -1; 187 break; 188 } 189 } 190 } 191 rewind(_gr_fp); 192 } 193 #endif 194 195 if (maxlinelength == 0) { 196 if ((line = (char *)malloc(MAXLINELENGTH)) == NULL) 197 return 0; 198 maxlinelength += MAXLINELENGTH; 199 } 200 201 if (maxgrp == 0) { 202 if ((members = (char **) malloc(sizeof(char **) * 203 MAXGRP)) == NULL) 204 return 0; 205 maxgrp += MAXGRP; 206 } 207 208 return 1; 209 } 210 211 int 212 setgrent(void) 213 { 214 return setgroupent(0); 215 } 216 217 int 218 setgroupent(stayopen) 219 int stayopen; 220 { 221 if (!start_gr()) 222 return 0; 223 _gr_stayopen = stayopen; 224 #ifdef YP 225 _gr_stepping_yp = 0; 226 #endif 227 return 1; 228 } 229 230 void 231 endgrent() 232 { 233 #ifdef YP 234 _gr_stepping_yp = 0; 235 #endif 236 if (_gr_fp) { 237 (void)fclose(_gr_fp); 238 _gr_fp = NULL; 239 } 240 } 241 242 static int 243 grscan(search, gid, name) 244 register int search, gid; 245 register char *name; 246 { 247 register char *cp, **m; 248 char *bp; 249 250 251 #ifdef YP 252 int _ypfound; 253 #endif 254 for (;;) { 255 #ifdef YP 256 _ypfound = 0; 257 #endif 258 if (fgets(line, maxlinelength, _gr_fp) == NULL) 259 return(0); 260 261 if (!index(line, '\n')) { 262 do { 263 if (feof(_gr_fp)) 264 return(0); 265 266 /* don't allocate infinite memory */ 267 if (MAXLINELENGTHLIMIT > 0 && 268 maxlinelength >= MAXLINELENGTHLIMIT) 269 return(0); 270 271 if ((line = (char *)reallocf(line, 272 sizeof(char) * 273 (maxlinelength + MAXLINELENGTH))) == NULL) 274 return(0); 275 276 if (fgets(line + maxlinelength - 1, 277 MAXLINELENGTH + 1, _gr_fp) == NULL) 278 return(0); 279 280 maxlinelength += MAXLINELENGTH; 281 } while (!index(line + maxlinelength - 282 MAXLINELENGTH - 1, '\n')); 283 } 284 285 #ifdef GROUP_IGNORE_COMMENTS 286 /* 287 * Ignore comments: ^[ \t]*# 288 */ 289 for (cp = line; *cp != '\0'; cp++) 290 if (*cp != ' ' && *cp != '\t') 291 break; 292 if (*cp == '#' || *cp == '\0') 293 continue; 294 #endif 295 296 bp = line; 297 298 if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL) 299 break; 300 #ifdef YP 301 /* 302 * XXX We need to be careful to avoid proceeding 303 * past this point under certain circumstances or 304 * we risk dereferencing null pointers down below. 305 */ 306 if (_gr_group.gr_name[0] == '+') { 307 if (strlen(_gr_group.gr_name) == 1) { 308 switch(search) { 309 case 0: 310 return(1); 311 case 1: 312 return(-1); 313 default: 314 return(0); 315 } 316 } else { 317 cp = &_gr_group.gr_name[1]; 318 if (search && name != NULL) 319 if (strcmp(cp, name)) 320 continue; 321 if (!_getypgroup(&_gr_group, cp, 322 "group.byname")) 323 continue; 324 if (search && name == NULL) 325 if (gid != _gr_group.gr_gid) 326 continue; 327 /* We're going to override -- tell the world. */ 328 _ypfound++; 329 } 330 } 331 #else 332 if (_gr_group.gr_name[0] == '+') 333 continue; 334 #endif /* YP */ 335 if (search && name) { 336 if(strcmp(_gr_group.gr_name, name)) { 337 continue; 338 } 339 } 340 #ifdef YP 341 if ((cp = strsep(&bp, ":\n")) == NULL) 342 if (_ypfound) 343 return(1); 344 else 345 break; 346 if (strlen(cp) || !_ypfound) 347 _gr_group.gr_passwd = cp; 348 #else 349 if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL) 350 break; 351 #endif 352 if (!(cp = strsep(&bp, ":\n"))) 353 #ifdef YP 354 if (_ypfound) 355 return(1); 356 else 357 #endif 358 continue; 359 #ifdef YP 360 /* 361 * Hurm. Should we be doing this? We allow UIDs to 362 * be overridden -- what about GIDs? 363 */ 364 if (!_ypfound) 365 #endif 366 _gr_group.gr_gid = atoi(cp); 367 if (search && name == NULL && _gr_group.gr_gid != gid) 368 continue; 369 cp = NULL; 370 if (bp == NULL) /* !!! Must check for this! */ 371 break; 372 #ifdef YP 373 if ((cp = strsep(&bp, ":\n")) == NULL) 374 break; 375 376 if (!strlen(cp) && _ypfound) 377 return(1); 378 else 379 members[0] = NULL; 380 bp = cp; 381 cp = NULL; 382 #endif 383 for (m = members; ; bp++) { 384 if (m == (members + maxgrp - 1)) { 385 if ((members = (char **) 386 reallocf(members, 387 sizeof(char **) * 388 (maxgrp + MAXGRP))) == NULL) 389 return(0); 390 m = members + maxgrp - 1; 391 maxgrp += MAXGRP; 392 } 393 if (*bp == ',') { 394 if (cp) { 395 *bp = '\0'; 396 *m++ = cp; 397 cp = NULL; 398 } 399 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { 400 if (cp) { 401 *bp = '\0'; 402 *m++ = cp; 403 } 404 break; 405 } else if (cp == NULL) 406 cp = bp; 407 408 } 409 _gr_group.gr_mem = members; 410 *m = NULL; 411 return(1); 412 } 413 /* NOTREACHED */ 414 return (0); 415 } 416 417 #ifdef YP 418 419 static int 420 _gr_breakout_yp(struct group *gr, char *result) 421 { 422 char *s, *cp; 423 char **m; 424 425 /* 426 * XXX If 's' ends up being a NULL pointer, punt on this group. 427 * It means the NIS group entry is badly formatted and should 428 * be skipped. 429 */ 430 if ((s = strsep(&result, ":")) == NULL) return 0; /* name */ 431 gr->gr_name = s; 432 433 if ((s = strsep(&result, ":")) == NULL) return 0; /* password */ 434 gr->gr_passwd = s; 435 436 if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */ 437 gr->gr_gid = atoi(s); 438 439 if ((s = result) == NULL) return 0; 440 cp = 0; 441 442 for (m = members; ; s++) { 443 if (m == members + maxgrp - 1) { 444 if ((members = (char **)reallocf(members, 445 sizeof(char **) * (maxgrp + MAXGRP))) == NULL) 446 return(0); 447 m = members + maxgrp - 1; 448 maxgrp += MAXGRP; 449 } 450 if (*s == ',') { 451 if (cp) { 452 *s = '\0'; 453 *m++ = cp; 454 cp = NULL; 455 } 456 } else if (*s == '\0' || *s == '\n' || *s == ' ') { 457 if (cp) { 458 *s = '\0'; 459 *m++ = cp; 460 } 461 break; 462 } else if (cp == NULL) { 463 cp = s; 464 } 465 } 466 _gr_group.gr_mem = members; 467 *m = NULL; 468 469 return 1; 470 } 471 472 static char *_gr_yp_domain; 473 474 static int 475 _getypgroup(struct group *gr, const char *name, char *map) 476 { 477 char *result, *s; 478 static char resultbuf[YPMAXRECORD + 2]; 479 int resultlen; 480 481 if(!_gr_yp_domain) { 482 if(yp_get_default_domain(&_gr_yp_domain)) 483 return 0; 484 } 485 486 if(yp_match(_gr_yp_domain, map, name, strlen(name), 487 &result, &resultlen)) 488 return 0; 489 490 s = strchr(result, '\n'); 491 if(s) *s = '\0'; 492 493 if(resultlen >= sizeof resultbuf) return 0; 494 strncpy(resultbuf, result, resultlen); 495 resultbuf[resultlen] = '\0'; 496 free(result); 497 return(_gr_breakout_yp(gr, resultbuf)); 498 499 } 500 501 502 static int 503 _nextypgroup(struct group *gr) 504 { 505 static char *key; 506 static int keylen; 507 char *lastkey, *result; 508 static char resultbuf[YPMAXRECORD + 2]; 509 int resultlen; 510 int rv; 511 512 if(!_gr_yp_domain) { 513 if(yp_get_default_domain(&_gr_yp_domain)) 514 return 0; 515 } 516 517 if(!_gr_stepping_yp) { 518 if(key) free(key); 519 rv = yp_first(_gr_yp_domain, "group.byname", 520 &key, &keylen, &result, &resultlen); 521 if(rv) { 522 return 0; 523 } 524 _gr_stepping_yp = 1; 525 goto unpack; 526 } else { 527 tryagain: 528 lastkey = key; 529 rv = yp_next(_gr_yp_domain, "group.byname", key, keylen, 530 &key, &keylen, &result, &resultlen); 531 free(lastkey); 532 unpack: 533 if(rv) { 534 _gr_stepping_yp = 0; 535 return 0; 536 } 537 538 if(resultlen > sizeof(resultbuf)) { 539 free(result); 540 goto tryagain; 541 } 542 543 strncpy(resultbuf, result, resultlen); 544 resultbuf[resultlen] = '\0'; 545 free(result); 546 if((result = strchr(resultbuf, '\n')) != NULL) 547 *result = '\0'; 548 if (_gr_breakout_yp(gr, resultbuf)) 549 return(1); 550 else 551 goto tryagain; 552 } 553 } 554 555 #endif /* YP */ 556