gr_util.c revision 247919
1/*- 2 * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org> 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 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/lib/libutil/gr_util.c 247919 2013-03-07 19:00:00Z db $"); 29 30#include <sys/param.h> 31#include <sys/errno.h> 32#include <sys/stat.h> 33 34#include <ctype.h> 35#include <err.h> 36#include <fcntl.h> 37#include <grp.h> 38#include <inttypes.h> 39#include <libutil.h> 40#include <paths.h> 41#include <stdbool.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46 47static int lockfd = -1; 48static char group_dir[PATH_MAX]; 49static char group_file[PATH_MAX]; 50static char tempname[PATH_MAX]; 51static int initialized; 52static size_t grmemlen(const struct group *, const char *, int *); 53static struct group *grcopy(const struct group *gr, struct group *newgr, const char *, int ndx); 54 55/* 56 * Initialize statics 57 */ 58int 59gr_init(const char *dir, const char *group) 60{ 61 62 if (dir == NULL) { 63 strcpy(group_dir, _PATH_ETC); 64 } else { 65 if (strlen(dir) >= sizeof(group_dir)) { 66 errno = ENAMETOOLONG; 67 return (-1); 68 } 69 strcpy(group_dir, dir); 70 } 71 72 if (group == NULL) { 73 if (dir == NULL) { 74 strcpy(group_file, _PATH_GROUP); 75 } else if (snprintf(group_file, sizeof(group_file), "%s/group", 76 group_dir) > (int)sizeof(group_file)) { 77 errno = ENAMETOOLONG; 78 return (-1); 79 } 80 } else { 81 if (strlen(group) >= sizeof(group_file)) { 82 errno = ENAMETOOLONG; 83 return (-1); 84 } 85 strcpy(group_file, group); 86 } 87 88 initialized = 1; 89 return (0); 90} 91 92/* 93 * Lock the group file 94 */ 95int 96gr_lock(void) 97{ 98 if (*group_file == '\0') 99 return (-1); 100 101 for (;;) { 102 struct stat st; 103 104 lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0); 105 if (lockfd == -1) { 106 if (errno == EWOULDBLOCK) { 107 errx(1, "the group file is busy"); 108 } else { 109 err(1, "could not lock the group file: "); 110 } 111 } 112 if (fstat(lockfd, &st) == -1) 113 err(1, "fstat() failed: "); 114 if (st.st_nlink != 0) 115 break; 116 close(lockfd); 117 lockfd = -1; 118 } 119 return (lockfd); 120} 121 122/* 123 * Create and open a presmuably safe temp file for editing group data 124 */ 125int 126gr_tmp(int mfd) 127{ 128 char buf[8192]; 129 ssize_t nr; 130 const char *p; 131 int tfd; 132 133 if (*group_file == '\0') 134 return (-1); 135 if ((p = strrchr(group_file, '/'))) 136 ++p; 137 else 138 p = group_file; 139 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX", 140 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) { 141 errno = ENAMETOOLONG; 142 return (-1); 143 } 144 if ((tfd = mkstemp(tempname)) == -1) 145 return (-1); 146 if (mfd != -1) { 147 while ((nr = read(mfd, buf, sizeof(buf))) > 0) 148 if (write(tfd, buf, (size_t)nr) != nr) 149 break; 150 if (nr != 0) { 151 unlink(tempname); 152 *tempname = '\0'; 153 close(tfd); 154 return (-1); 155 } 156 } 157 return (tfd); 158} 159 160/* 161 * Copy the group file from one descriptor to another, replacing, deleting 162 * or adding a single record on the way. 163 */ 164int 165gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr) 166{ 167 char buf[8192], *end, *line, *p, *q, *r, t; 168 struct group *fgr; 169 const struct group *sgr; 170 size_t len; 171 int eof, readlen; 172 173 sgr = gr; 174 if (gr == NULL) { 175 line = NULL; 176 if (old_gr == NULL) 177 return (-1); 178 sgr = old_gr; 179 } else if ((line = gr_make(gr)) == NULL) 180 return (-1); 181 182 eof = 0; 183 len = 0; 184 p = q = end = buf; 185 for (;;) { 186 /* find the end of the current line */ 187 for (p = q; q < end && *q != '\0'; ++q) 188 if (*q == '\n') 189 break; 190 191 /* if we don't have a complete line, fill up the buffer */ 192 if (q >= end) { 193 if (eof) 194 break; 195 if ((size_t)(q - p) >= sizeof(buf)) { 196 warnx("group line too long"); 197 errno = EINVAL; /* hack */ 198 goto err; 199 } 200 if (p < end) { 201 q = memmove(buf, p, end -p); 202 end -= p - buf; 203 } else { 204 p = q = end = buf; 205 } 206 readlen = read(ffd, end, sizeof(buf) - (end -buf)); 207 if (readlen == -1) 208 goto err; 209 else 210 len = (size_t)readlen; 211 if (len == 0 && p == buf) 212 break; 213 end += len; 214 len = end - buf; 215 if (len < (ssize_t)sizeof(buf)) { 216 eof = 1; 217 if (len > 0 && buf[len -1] != '\n') 218 ++len, *end++ = '\n'; 219 } 220 continue; 221 } 222 223 /* is it a blank line or a comment? */ 224 for (r = p; r < q && isspace(*r); ++r) 225 /* nothing */; 226 if (r == q || *r == '#') { 227 /* yep */ 228 if (write(tfd, p, q -p + 1) != q - p + 1) 229 goto err; 230 ++q; 231 continue; 232 } 233 234 /* is it the one we're looking for? */ 235 236 t = *q; 237 *q = '\0'; 238 239 fgr = gr_scan(r); 240 241 /* fgr is either a struct group for the current line, 242 * or NULL if the line is malformed. 243 */ 244 245 *q = t; 246 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) { 247 /* nope */ 248 if (fgr != NULL) 249 free(fgr); 250 if (write(tfd, p, q - p + 1) != q - p + 1) 251 goto err; 252 ++q; 253 continue; 254 } 255 if (old_gr && !gr_equal(fgr, old_gr)) { 256 warnx("entry inconsistent"); 257 free(fgr); 258 errno = EINVAL; /* hack */ 259 goto err; 260 } 261 free(fgr); 262 263 /* it is, replace or remove it */ 264 if (line != NULL) { 265 len = strlen(line); 266 if (write(tfd, line, len) != (int) len) 267 goto err; 268 } else { 269 /* when removed, avoid the \n */ 270 q++; 271 } 272 /* we're done, just copy the rest over */ 273 for (;;) { 274 if (write(tfd, q, end - q) != end - q) 275 goto err; 276 q = buf; 277 readlen = read(ffd, buf, sizeof(buf)); 278 if (readlen == 0) 279 break; 280 else 281 len = (size_t)readlen; 282 if (readlen == -1) 283 goto err; 284 end = buf + len; 285 } 286 goto done; 287 } 288 289 /* if we got here, we didn't find the old entry */ 290 if (line == NULL) { 291 errno = ENOENT; 292 goto err; 293 } 294 len = strlen(line); 295 if ((size_t)write(tfd, line, len) != len || 296 write(tfd, "\n", 1) != 1) 297 goto err; 298 done: 299 if (line != NULL) 300 free(line); 301 return (0); 302 err: 303 if (line != NULL) 304 free(line); 305 return (-1); 306} 307 308/* 309 * Regenerate the group file 310 */ 311int 312gr_mkdb(void) 313{ 314 if (chmod(tempname, 0644) != 0) 315 return (-1); 316 317 return (rename(tempname, group_file)); 318} 319 320/* 321 * Clean up. Preserves errno for the caller's convenience. 322 */ 323void 324gr_fini(void) 325{ 326 int serrno; 327 328 if (!initialized) 329 return; 330 initialized = 0; 331 serrno = errno; 332 if (*tempname != '\0') { 333 unlink(tempname); 334 *tempname = '\0'; 335 } 336 if (lockfd != -1) 337 close(lockfd); 338 errno = serrno; 339} 340 341/* 342 * Compares two struct group's. 343 */ 344int 345gr_equal(const struct group *gr1, const struct group *gr2) 346{ 347 int gr1_ndx; 348 int gr2_ndx; 349 350 /* Check that the non-member information is the same. */ 351 if (gr1->gr_name == NULL || gr2->gr_name == NULL) { 352 if (gr1->gr_name != gr2->gr_name) 353 return (false); 354 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0) 355 return (false); 356 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) { 357 if (gr1->gr_passwd != gr2->gr_passwd) 358 return (false); 359 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0) 360 return (false); 361 if (gr1->gr_gid != gr2->gr_gid) 362 return (false); 363 364 /* Check all members in both groups. */ 365 if (gr1->gr_mem == NULL || gr2->gr_mem == NULL) { 366 if (gr1->gr_mem != gr2->gr_mem) 367 return (false); 368 } else { 369 for (gr1_ndx = 0; gr1->gr_mem[gr1_ndx] != NULL; gr1_ndx++) { 370 for (gr2_ndx = 0;; gr2_ndx++) { 371 if (gr2->gr_mem[gr2_ndx] == NULL) 372 return (false); 373 if (strcmp(gr1->gr_mem[gr1_ndx], 374 gr2->gr_mem[gr2_ndx]) == 0) { 375 break; 376 } 377 } 378 } 379 380 /* Check that group2 does not have more members than group1. */ 381 if (gr2->gr_mem[gr1_ndx] != NULL) 382 return (false); 383 } 384 385 return (true); 386} 387 388/* 389 * Make a group line out of a struct group. 390 */ 391char * 392gr_make(const struct group *gr) 393{ 394 const char *group_line_format = "%s:%s:%ju:"; 395 const char *sep; 396 char *line; 397 char *p; 398 size_t line_size; 399 int ndx; 400 401 /* Calculate the length of the group line. */ 402 line_size = snprintf(NULL, 0, group_line_format, gr->gr_name, 403 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1; 404 if (gr->gr_mem != NULL) { 405 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) 406 line_size += strlen(gr->gr_mem[ndx]) + 1; 407 if (ndx > 0) 408 line_size--; 409 } 410 411 /* Create the group line and fill it. */ 412 if ((line = p = malloc(line_size)) == NULL) 413 return (NULL); 414 p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd, 415 (uintmax_t)gr->gr_gid); 416 if (gr->gr_mem != NULL) { 417 sep = ""; 418 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { 419 p = stpcpy(p, sep); 420 p = stpcpy(p, gr->gr_mem[ndx]); 421 sep = ","; 422 } 423 } 424 425 return (line); 426} 427 428/* 429 * Duplicate a struct group. 430 */ 431struct group * 432gr_dup(const struct group *gr) 433{ 434 return (gr_add(gr, NULL)); 435} 436/* 437 * Add a new member name to a struct group. 438 */ 439struct group * 440gr_add(const struct group *gr, const char *newmember) 441{ 442 struct group *newgr; 443 size_t len; 444 int num_mem; 445 446 num_mem = 0; 447 len = grmemlen(gr, newmember, &num_mem); 448 /* Create new group and copy old group into it. */ 449 if ((newgr = malloc(len)) == NULL) 450 return (NULL); 451 return (grcopy(gr, newgr, newmember, num_mem)); 452} 453 454/* It is safer to walk the pointers given at gr_mem since there is no 455 * guarantee the gr_mem + strings are continguous in the given struct group 456 * but compact the new group into the following form. 457 * 458 * The new struct is laid out like this in memory. The example given is 459 * for a group with two members only. 460 * 461 * { 462 * (char *name) 463 * (char *passwd) 464 * (int gid) 465 * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area 466 * gr_mem area 467 * (member1 *) 468 * (member2 *) 469 * (NULL) 470 * (name string) 471 * (passwd string) 472 * (member1 string) 473 * (member2 string) 474 * } 475 */ 476/* 477 * Copy the guts of a group plus given name to a preallocated group struct 478 */ 479static struct group * 480grcopy(const struct group *gr, struct group *newgr, const char *name, int ndx) 481{ 482 char *dst; 483 int i; 484 485 if (name != NULL) 486 ndx++; 487 /* point new gr_mem to end of struct + 1 if there are names */ 488 if (ndx != 0) 489 newgr->gr_mem = (char **)(newgr + 1); 490 else 491 newgr->gr_mem = NULL; 492 /* point dst after the end of all the gr_mem pointers in newgr */ 493 dst = (char *)&newgr->gr_mem[ndx + 1]; 494 if (gr->gr_name != NULL) { 495 newgr->gr_name = dst; 496 dst = stpcpy(dst, gr->gr_name) + 1; 497 } else 498 newgr->gr_name = NULL; 499 if (gr->gr_passwd != NULL) { 500 newgr->gr_passwd = dst; 501 dst = stpcpy(dst, gr->gr_passwd) + 1; 502 } else 503 newgr->gr_passwd = NULL; 504 newgr->gr_gid = gr->gr_gid; 505 if (ndx != 0) { 506 for (i = 0; gr->gr_mem[i] != NULL; i++) { 507 newgr->gr_mem[i] = dst; 508 dst = stpcpy(dst, gr->gr_mem[i]) + 1; 509 } 510 if (name != NULL) { 511 newgr->gr_mem[i++] = dst; 512 dst = stpcpy(dst, name) + 1; 513 } 514 newgr->gr_mem[i] = NULL; 515 } 516 return (newgr); 517} 518 519/* 520 * Calculate length of a struct group + given name 521 */ 522static size_t 523grmemlen(const struct group *gr, const char *name, int *num_mem) 524{ 525 size_t len; 526 int i; 527 528 if (gr == NULL) 529 return (0); 530 /* Calculate size of the group. */ 531 len = sizeof(*gr); 532 if (gr->gr_name != NULL) 533 len += strlen(gr->gr_name) + 1; 534 if (gr->gr_passwd != NULL) 535 len += strlen(gr->gr_passwd) + 1; 536 if (gr->gr_mem != NULL) { 537 for (len = i = 0; gr->gr_mem[i] != NULL; i++) { 538 len += strlen(gr->gr_mem[i]) + 1; 539 len += sizeof(*gr->gr_mem); 540 } 541 *num_mem = i; 542 } 543 if (name != NULL) { 544 len += strlen(name) + 1; 545 if (gr->gr_mem == NULL) 546 len += sizeof(*gr->gr_mem); 547 } 548 return(len); 549} 550 551/* 552 * Scan a line and place it into a group structure. 553 */ 554static bool 555__gr_scan(char *line, struct group *gr) 556{ 557 char *loc; 558 int ndx; 559 560 /* Assign non-member information to structure. */ 561 gr->gr_name = line; 562 if ((loc = strchr(line, ':')) == NULL) 563 return (false); 564 *loc = '\0'; 565 gr->gr_passwd = loc + 1; 566 if (*gr->gr_passwd == ':') 567 *gr->gr_passwd = '\0'; 568 else { 569 if ((loc = strchr(loc + 1, ':')) == NULL) 570 return (false); 571 *loc = '\0'; 572 } 573 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1) 574 return (false); 575 576 /* Assign member information to structure. */ 577 if ((loc = strchr(loc + 1, ':')) == NULL) 578 return (false); 579 line = loc + 1; 580 gr->gr_mem = NULL; 581 ndx = 0; 582 do { 583 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) * 584 (ndx + 1)); 585 if (gr->gr_mem == NULL) 586 return (false); 587 588 /* Skip locations without members (i.e., empty string). */ 589 do { 590 gr->gr_mem[ndx] = strsep(&line, ","); 591 } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0'); 592 } while (gr->gr_mem[ndx++] != NULL); 593 594 return (true); 595} 596 597/* 598 * Create a struct group from a line. 599 */ 600struct group * 601gr_scan(const char *line) 602{ 603 struct group gr; 604 char *line_copy; 605 struct group *new_gr; 606 607 if ((line_copy = strdup(line)) == NULL) 608 return (NULL); 609 if (!__gr_scan(line_copy, &gr)) { 610 free(line_copy); 611 return (NULL); 612 } 613 new_gr = gr_dup(&gr); 614 free(line_copy); 615 if (gr.gr_mem != NULL) 616 free(gr.gr_mem); 617 618 return (new_gr); 619} 620