1178431Sscf/*- 2178431Sscf * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org> 3178431Sscf * All rights reserved. 4178431Sscf * 5178431Sscf * Redistribution and use in source and binary forms, with or without 6178431Sscf * modification, are permitted provided that the following conditions 7178431Sscf * are met: 8178431Sscf * 1. Redistributions of source code must retain the above copyright 9178431Sscf * notice, this list of conditions and the following disclaimer, 10178431Sscf * without modification, immediately at the beginning of the file. 11178431Sscf * 2. Redistributions in binary form must reproduce the above copyright 12178431Sscf * notice, this list of conditions and the following disclaimer in the 13178431Sscf * documentation and/or other materials provided with the distribution. 14178431Sscf * 15178431Sscf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16178431Sscf * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17178431Sscf * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18178431Sscf * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19178431Sscf * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20178431Sscf * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21178431Sscf * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22178431Sscf * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23178431Sscf * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24178431Sscf * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25178431Sscf */ 26178431Sscf 27178431Sscf#include <sys/cdefs.h> 28178431Sscf__FBSDID("$FreeBSD: releng/11.0/lib/libutil/gr_util.c 285050 2015-07-02 17:30:59Z garga $"); 29178431Sscf 30178431Sscf#include <sys/param.h> 31228545Sbapt#include <sys/errno.h> 32228545Sbapt#include <sys/stat.h> 33184831Sscf 34228545Sbapt#include <ctype.h> 35228545Sbapt#include <err.h> 36228545Sbapt#include <fcntl.h> 37178431Sscf#include <grp.h> 38178431Sscf#include <inttypes.h> 39184831Sscf#include <libutil.h> 40228545Sbapt#include <paths.h> 41178431Sscf#include <stdbool.h> 42178431Sscf#include <stdio.h> 43178431Sscf#include <stdlib.h> 44178431Sscf#include <string.h> 45228545Sbapt#include <unistd.h> 46178431Sscf 47228545Sbaptstatic int lockfd = -1; 48228545Sbaptstatic char group_dir[PATH_MAX]; 49228545Sbaptstatic char group_file[PATH_MAX]; 50228545Sbaptstatic char tempname[PATH_MAX]; 51228545Sbaptstatic int initialized; 52247919Sdbstatic size_t grmemlen(const struct group *, const char *, int *); 53248102Sdbstatic struct group *grcopy(const struct group *gr, char *mem, const char *, int ndx); 54228545Sbapt 55178431Sscf/* 56228545Sbapt * Initialize statics 57228545Sbapt */ 58228545Sbaptint 59228545Sbaptgr_init(const char *dir, const char *group) 60228545Sbapt{ 61242319Sbapt 62228545Sbapt if (dir == NULL) { 63228545Sbapt strcpy(group_dir, _PATH_ETC); 64228545Sbapt } else { 65228545Sbapt if (strlen(dir) >= sizeof(group_dir)) { 66228545Sbapt errno = ENAMETOOLONG; 67228545Sbapt return (-1); 68228545Sbapt } 69228545Sbapt strcpy(group_dir, dir); 70228545Sbapt } 71228545Sbapt 72228545Sbapt if (group == NULL) { 73228545Sbapt if (dir == NULL) { 74228545Sbapt strcpy(group_file, _PATH_GROUP); 75228545Sbapt } else if (snprintf(group_file, sizeof(group_file), "%s/group", 76228545Sbapt group_dir) > (int)sizeof(group_file)) { 77228545Sbapt errno = ENAMETOOLONG; 78228545Sbapt return (-1); 79228545Sbapt } 80228545Sbapt } else { 81228545Sbapt if (strlen(group) >= sizeof(group_file)) { 82228545Sbapt errno = ENAMETOOLONG; 83228545Sbapt return (-1); 84228545Sbapt } 85228545Sbapt strcpy(group_file, group); 86228545Sbapt } 87242319Sbapt 88228545Sbapt initialized = 1; 89228545Sbapt return (0); 90228545Sbapt} 91228545Sbapt 92228545Sbapt/* 93228545Sbapt * Lock the group file 94228545Sbapt */ 95228545Sbaptint 96228545Sbaptgr_lock(void) 97228545Sbapt{ 98228545Sbapt if (*group_file == '\0') 99228545Sbapt return (-1); 100228545Sbapt 101228545Sbapt for (;;) { 102228545Sbapt struct stat st; 103228545Sbapt 104244744Sbapt lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0); 105244735Sbapt if (lockfd == -1) { 106228545Sbapt if (errno == EWOULDBLOCK) { 107228545Sbapt errx(1, "the group file is busy"); 108228545Sbapt } else { 109228545Sbapt err(1, "could not lock the group file: "); 110228545Sbapt } 111228545Sbapt } 112228545Sbapt if (fstat(lockfd, &st) == -1) 113228545Sbapt err(1, "fstat() failed: "); 114228545Sbapt if (st.st_nlink != 0) 115228545Sbapt break; 116228545Sbapt close(lockfd); 117228545Sbapt lockfd = -1; 118228545Sbapt } 119228545Sbapt return (lockfd); 120228545Sbapt} 121228545Sbapt 122228545Sbapt/* 123228545Sbapt * Create and open a presmuably safe temp file for editing group data 124228545Sbapt */ 125228545Sbaptint 126228545Sbaptgr_tmp(int mfd) 127228545Sbapt{ 128228545Sbapt char buf[8192]; 129228545Sbapt ssize_t nr; 130228545Sbapt const char *p; 131228545Sbapt int tfd; 132228545Sbapt 133228545Sbapt if (*group_file == '\0') 134228545Sbapt return (-1); 135228545Sbapt if ((p = strrchr(group_file, '/'))) 136228545Sbapt ++p; 137228545Sbapt else 138228545Sbapt p = group_file; 139228545Sbapt if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX", 140228545Sbapt (int)(p - group_file), group_file) >= (int)sizeof(tempname)) { 141228545Sbapt errno = ENAMETOOLONG; 142228545Sbapt return (-1); 143228545Sbapt } 144285050Sgarga if ((tfd = mkostemp(tempname, O_SYNC)) == -1) 145228545Sbapt return (-1); 146228545Sbapt if (mfd != -1) { 147228545Sbapt while ((nr = read(mfd, buf, sizeof(buf))) > 0) 148228545Sbapt if (write(tfd, buf, (size_t)nr) != nr) 149228545Sbapt break; 150228545Sbapt if (nr != 0) { 151228545Sbapt unlink(tempname); 152228545Sbapt *tempname = '\0'; 153228545Sbapt close(tfd); 154228545Sbapt return (-1); 155228545Sbapt } 156228545Sbapt } 157228545Sbapt return (tfd); 158228545Sbapt} 159228545Sbapt 160228545Sbapt/* 161228545Sbapt * Copy the group file from one descriptor to another, replacing, deleting 162228545Sbapt * or adding a single record on the way. 163228545Sbapt */ 164228545Sbaptint 165228545Sbaptgr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr) 166228545Sbapt{ 167228545Sbapt char buf[8192], *end, *line, *p, *q, *r, t; 168228545Sbapt struct group *fgr; 169228545Sbapt const struct group *sgr; 170228545Sbapt size_t len; 171228545Sbapt int eof, readlen; 172228545Sbapt 173273791Sbapt if (old_gr == NULL && gr == NULL) 174273791Sbapt return(-1); 175273791Sbapt 176273791Sbapt sgr = old_gr; 177273791Sbapt /* deleting a group */ 178228545Sbapt if (gr == NULL) { 179228545Sbapt line = NULL; 180273791Sbapt } else { 181273791Sbapt if ((line = gr_make(gr)) == NULL) 182228545Sbapt return (-1); 183273791Sbapt } 184228545Sbapt 185273791Sbapt /* adding a group */ 186273791Sbapt if (sgr == NULL) 187273791Sbapt sgr = gr; 188273791Sbapt 189228545Sbapt eof = 0; 190228545Sbapt len = 0; 191228545Sbapt p = q = end = buf; 192228545Sbapt for (;;) { 193228545Sbapt /* find the end of the current line */ 194228545Sbapt for (p = q; q < end && *q != '\0'; ++q) 195228545Sbapt if (*q == '\n') 196228545Sbapt break; 197228545Sbapt 198228545Sbapt /* if we don't have a complete line, fill up the buffer */ 199228545Sbapt if (q >= end) { 200228545Sbapt if (eof) 201228545Sbapt break; 202228545Sbapt if ((size_t)(q - p) >= sizeof(buf)) { 203228545Sbapt warnx("group line too long"); 204228545Sbapt errno = EINVAL; /* hack */ 205228545Sbapt goto err; 206228545Sbapt } 207228545Sbapt if (p < end) { 208228545Sbapt q = memmove(buf, p, end -p); 209228545Sbapt end -= p - buf; 210228545Sbapt } else { 211228545Sbapt p = q = end = buf; 212228545Sbapt } 213228545Sbapt readlen = read(ffd, end, sizeof(buf) - (end -buf)); 214228545Sbapt if (readlen == -1) 215228545Sbapt goto err; 216228545Sbapt else 217228545Sbapt len = (size_t)readlen; 218228545Sbapt if (len == 0 && p == buf) 219228545Sbapt break; 220228545Sbapt end += len; 221228545Sbapt len = end - buf; 222228545Sbapt if (len < (ssize_t)sizeof(buf)) { 223228545Sbapt eof = 1; 224228545Sbapt if (len > 0 && buf[len -1] != '\n') 225228545Sbapt ++len, *end++ = '\n'; 226228545Sbapt } 227228545Sbapt continue; 228228545Sbapt } 229228545Sbapt 230228545Sbapt /* is it a blank line or a comment? */ 231228545Sbapt for (r = p; r < q && isspace(*r); ++r) 232228545Sbapt /* nothing */; 233228545Sbapt if (r == q || *r == '#') { 234228545Sbapt /* yep */ 235228545Sbapt if (write(tfd, p, q -p + 1) != q - p + 1) 236228545Sbapt goto err; 237228545Sbapt ++q; 238228545Sbapt continue; 239228545Sbapt } 240228545Sbapt 241228545Sbapt /* is it the one we're looking for? */ 242228545Sbapt 243228545Sbapt t = *q; 244228545Sbapt *q = '\0'; 245228545Sbapt 246228545Sbapt fgr = gr_scan(r); 247228545Sbapt 248228545Sbapt /* fgr is either a struct group for the current line, 249228545Sbapt * or NULL if the line is malformed. 250228545Sbapt */ 251228545Sbapt 252228545Sbapt *q = t; 253228545Sbapt if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) { 254228545Sbapt /* nope */ 255228545Sbapt if (fgr != NULL) 256228545Sbapt free(fgr); 257228545Sbapt if (write(tfd, p, q - p + 1) != q - p + 1) 258228545Sbapt goto err; 259228545Sbapt ++q; 260228545Sbapt continue; 261228545Sbapt } 262228545Sbapt if (old_gr && !gr_equal(fgr, old_gr)) { 263228545Sbapt warnx("entry inconsistent"); 264228545Sbapt free(fgr); 265228545Sbapt errno = EINVAL; /* hack */ 266228545Sbapt goto err; 267228545Sbapt } 268228545Sbapt free(fgr); 269228545Sbapt 270228545Sbapt /* it is, replace or remove it */ 271228545Sbapt if (line != NULL) { 272228545Sbapt len = strlen(line); 273228545Sbapt if (write(tfd, line, len) != (int) len) 274228545Sbapt goto err; 275228545Sbapt } else { 276228545Sbapt /* when removed, avoid the \n */ 277228545Sbapt q++; 278228545Sbapt } 279228545Sbapt /* we're done, just copy the rest over */ 280228545Sbapt for (;;) { 281228545Sbapt if (write(tfd, q, end - q) != end - q) 282228545Sbapt goto err; 283228545Sbapt q = buf; 284228545Sbapt readlen = read(ffd, buf, sizeof(buf)); 285228545Sbapt if (readlen == 0) 286228545Sbapt break; 287228545Sbapt else 288228545Sbapt len = (size_t)readlen; 289228545Sbapt if (readlen == -1) 290228545Sbapt goto err; 291228545Sbapt end = buf + len; 292228545Sbapt } 293228545Sbapt goto done; 294228545Sbapt } 295228545Sbapt 296228545Sbapt /* if we got here, we didn't find the old entry */ 297228545Sbapt if (line == NULL) { 298228545Sbapt errno = ENOENT; 299228545Sbapt goto err; 300228545Sbapt } 301228545Sbapt len = strlen(line); 302228545Sbapt if ((size_t)write(tfd, line, len) != len || 303228545Sbapt write(tfd, "\n", 1) != 1) 304228545Sbapt goto err; 305228545Sbapt done: 306228545Sbapt if (line != NULL) 307228545Sbapt free(line); 308228545Sbapt return (0); 309228545Sbapt err: 310228545Sbapt if (line != NULL) 311228545Sbapt free(line); 312228545Sbapt return (-1); 313228545Sbapt} 314228545Sbapt 315228545Sbapt/* 316228545Sbapt * Regenerate the group file 317228545Sbapt */ 318228545Sbaptint 319228545Sbaptgr_mkdb(void) 320228545Sbapt{ 321285050Sgarga int fd; 322285050Sgarga 323243334Sbapt if (chmod(tempname, 0644) != 0) 324243334Sbapt return (-1); 325243328Sbapt 326285050Sgarga if (rename(tempname, group_file) != 0) 327285050Sgarga return (-1); 328285050Sgarga 329285050Sgarga /* 330285050Sgarga * Make sure new group file is safe on disk. To improve performance we 331285050Sgarga * will call fsync() to the directory where file lies 332285050Sgarga */ 333285050Sgarga if ((fd = open(group_dir, O_RDONLY|O_DIRECTORY)) == -1) 334285050Sgarga return (-1); 335285050Sgarga 336285050Sgarga if (fsync(fd) != 0) { 337285050Sgarga close(fd); 338285050Sgarga return (-1); 339285050Sgarga } 340285050Sgarga 341285050Sgarga close(fd); 342285050Sgarga return(0); 343228545Sbapt} 344228545Sbapt 345228545Sbapt/* 346245390Smjg * Clean up. Preserves errno for the caller's convenience. 347228545Sbapt */ 348228545Sbaptvoid 349228545Sbaptgr_fini(void) 350228545Sbapt{ 351228545Sbapt int serrno; 352228545Sbapt 353228545Sbapt if (!initialized) 354228545Sbapt return; 355228545Sbapt initialized = 0; 356228545Sbapt serrno = errno; 357228545Sbapt if (*tempname != '\0') { 358228545Sbapt unlink(tempname); 359228545Sbapt *tempname = '\0'; 360228545Sbapt } 361228545Sbapt if (lockfd != -1) 362228545Sbapt close(lockfd); 363228545Sbapt errno = serrno; 364228545Sbapt} 365228545Sbapt 366228545Sbapt/* 367178431Sscf * Compares two struct group's. 368178431Sscf */ 369178431Sscfint 370178431Sscfgr_equal(const struct group *gr1, const struct group *gr2) 371178431Sscf{ 372178431Sscf 373178431Sscf /* Check that the non-member information is the same. */ 374185237Sscf if (gr1->gr_name == NULL || gr2->gr_name == NULL) { 375185237Sscf if (gr1->gr_name != gr2->gr_name) 376185237Sscf return (false); 377185237Sscf } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0) 378185237Sscf return (false); 379185237Sscf if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) { 380185237Sscf if (gr1->gr_passwd != gr2->gr_passwd) 381185237Sscf return (false); 382185237Sscf } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0) 383185237Sscf return (false); 384185237Sscf if (gr1->gr_gid != gr2->gr_gid) 385185237Sscf return (false); 386178431Sscf 387277669Smarkj /* 388277669Smarkj * Check all members in both groups. 389248102Sdb * getgrnam can return gr_mem with a pointer to NULL. 390248102Sdb * gr_dup and gr_add strip out this superfluous NULL, setting 391248102Sdb * gr_mem to NULL for no members. 392248102Sdb */ 393248102Sdb if (gr1->gr_mem != NULL && gr2->gr_mem != NULL) { 394248102Sdb int i; 395248102Sdb 396277669Smarkj for (i = 0; 397277669Smarkj gr1->gr_mem[i] != NULL && gr2->gr_mem[i] != NULL; i++) { 398248102Sdb if (strcmp(gr1->gr_mem[i], gr2->gr_mem[i]) != 0) 399248102Sdb return (false); 400178431Sscf } 401277669Smarkj if (gr1->gr_mem[i] != NULL || gr2->gr_mem[i] != NULL) 402277669Smarkj return (false); 403277669Smarkj } else if (gr1->gr_mem != NULL && gr1->gr_mem[0] != NULL) { 404277669Smarkj return (false); 405277669Smarkj } else if (gr2->gr_mem != NULL && gr2->gr_mem[0] != NULL) { 406277669Smarkj return (false); 407178431Sscf } 408178431Sscf 409185237Sscf return (true); 410178431Sscf} 411178431Sscf 412178431Sscf/* 413178431Sscf * Make a group line out of a struct group. 414178431Sscf */ 415178431Sscfchar * 416178431Sscfgr_make(const struct group *gr) 417178431Sscf{ 418245386Smjg const char *group_line_format = "%s:%s:%ju:"; 419245387Smjg const char *sep; 420178431Sscf char *line; 421245387Smjg char *p; 422185237Sscf size_t line_size; 423178431Sscf int ndx; 424178431Sscf 425178431Sscf /* Calculate the length of the group line. */ 426185237Sscf line_size = snprintf(NULL, 0, group_line_format, gr->gr_name, 427178431Sscf gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1; 428185237Sscf if (gr->gr_mem != NULL) { 429185237Sscf for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) 430185237Sscf line_size += strlen(gr->gr_mem[ndx]) + 1; 431185237Sscf if (ndx > 0) 432185237Sscf line_size--; 433185237Sscf } 434178431Sscf 435178431Sscf /* Create the group line and fill it. */ 436245387Smjg if ((line = p = malloc(line_size)) == NULL) 437178431Sscf return (NULL); 438245387Smjg p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd, 439200423Sscf (uintmax_t)gr->gr_gid); 440245387Smjg if (gr->gr_mem != NULL) { 441245387Smjg sep = ""; 442185237Sscf for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { 443245387Smjg p = stpcpy(p, sep); 444245387Smjg p = stpcpy(p, gr->gr_mem[ndx]); 445245387Smjg sep = ","; 446185237Sscf } 447245387Smjg } 448178431Sscf 449178431Sscf return (line); 450178431Sscf} 451178431Sscf 452178431Sscf/* 453178431Sscf * Duplicate a struct group. 454178431Sscf */ 455178431Sscfstruct group * 456178431Sscfgr_dup(const struct group *gr) 457178431Sscf{ 458247919Sdb return (gr_add(gr, NULL)); 459247919Sdb} 460247919Sdb/* 461247919Sdb * Add a new member name to a struct group. 462247919Sdb */ 463247919Sdbstruct group * 464247919Sdbgr_add(const struct group *gr, const char *newmember) 465247919Sdb{ 466248102Sdb char *mem; 467184831Sscf size_t len; 468185237Sscf int num_mem; 469178431Sscf 470247919Sdb num_mem = 0; 471247919Sdb len = grmemlen(gr, newmember, &num_mem); 472178431Sscf /* Create new group and copy old group into it. */ 473248102Sdb if ((mem = malloc(len)) == NULL) 474178431Sscf return (NULL); 475248102Sdb return (grcopy(gr, mem, newmember, num_mem)); 476247919Sdb} 477247919Sdb 478247919Sdb/* It is safer to walk the pointers given at gr_mem since there is no 479248102Sdb * guarantee the gr_mem + strings are contiguous in the given struct group 480248102Sdb * but compactify the new group into the following form. 481247919Sdb * 482247919Sdb * The new struct is laid out like this in memory. The example given is 483247919Sdb * for a group with two members only. 484247919Sdb * 485247919Sdb * { 486247919Sdb * (char *name) 487247919Sdb * (char *passwd) 488247919Sdb * (int gid) 489247919Sdb * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area 490247919Sdb * gr_mem area 491247919Sdb * (member1 *) 492247919Sdb * (member2 *) 493247919Sdb * (NULL) 494247919Sdb * (name string) 495247919Sdb * (passwd string) 496247919Sdb * (member1 string) 497247919Sdb * (member2 string) 498247919Sdb * } 499247919Sdb */ 500247919Sdb/* 501248102Sdb * Copy the contents of a group plus given name to a preallocated group struct 502247919Sdb */ 503247919Sdbstatic struct group * 504248102Sdbgrcopy(const struct group *gr, char *dst, const char *name, int ndx) 505247919Sdb{ 506247919Sdb int i; 507248102Sdb struct group *newgr; 508247919Sdb 509248102Sdb newgr = (struct group *)(void *)dst; /* avoid alignment warning */ 510248102Sdb dst += sizeof(*newgr); 511248102Sdb if (ndx != 0) { 512248102Sdb newgr->gr_mem = (char **)(void *)(dst); /* avoid alignment warning */ 513248102Sdb dst += (ndx + 1) * sizeof(*newgr->gr_mem); 514248102Sdb } else 515244742Sbapt newgr->gr_mem = NULL; 516178431Sscf if (gr->gr_name != NULL) { 517244742Sbapt newgr->gr_name = dst; 518244742Sbapt dst = stpcpy(dst, gr->gr_name) + 1; 519247919Sdb } else 520244777Sbapt newgr->gr_name = NULL; 521178431Sscf if (gr->gr_passwd != NULL) { 522244742Sbapt newgr->gr_passwd = dst; 523244742Sbapt dst = stpcpy(dst, gr->gr_passwd) + 1; 524247919Sdb } else 525244777Sbapt newgr->gr_passwd = NULL; 526244742Sbapt newgr->gr_gid = gr->gr_gid; 527248102Sdb i = 0; 528248102Sdb /* Original group struct might have a NULL gr_mem */ 529248102Sdb if (gr->gr_mem != NULL) { 530248102Sdb for (; gr->gr_mem[i] != NULL; i++) { 531247919Sdb newgr->gr_mem[i] = dst; 532247919Sdb dst = stpcpy(dst, gr->gr_mem[i]) + 1; 533178431Sscf } 534248102Sdb } 535248102Sdb /* If name is not NULL, newgr->gr_mem is known to be not NULL */ 536248102Sdb if (name != NULL) { 537248102Sdb newgr->gr_mem[i++] = dst; 538248102Sdb dst = stpcpy(dst, name) + 1; 539248102Sdb } 540248102Sdb /* if newgr->gr_mem is not NULL add NULL marker */ 541248102Sdb if (newgr->gr_mem != NULL) 542247919Sdb newgr->gr_mem[i] = NULL; 543248102Sdb 544244742Sbapt return (newgr); 545178431Sscf} 546178431Sscf 547178431Sscf/* 548247919Sdb * Calculate length of a struct group + given name 549244736Sbapt */ 550247919Sdbstatic size_t 551247919Sdbgrmemlen(const struct group *gr, const char *name, int *num_mem) 552244736Sbapt{ 553247919Sdb size_t len; 554247919Sdb int i; 555244736Sbapt 556247919Sdb if (gr == NULL) 557247919Sdb return (0); 558247919Sdb /* Calculate size of the group. */ 559247919Sdb len = sizeof(*gr); 560247919Sdb if (gr->gr_name != NULL) 561247919Sdb len += strlen(gr->gr_name) + 1; 562247919Sdb if (gr->gr_passwd != NULL) 563247919Sdb len += strlen(gr->gr_passwd) + 1; 564248102Sdb i = 0; 565244736Sbapt if (gr->gr_mem != NULL) { 566248102Sdb for (; gr->gr_mem[i] != NULL; i++) { 567247919Sdb len += strlen(gr->gr_mem[i]) + 1; 568247919Sdb len += sizeof(*gr->gr_mem); 569244736Sbapt } 570244736Sbapt } 571247919Sdb if (name != NULL) { 572248102Sdb i++; 573247919Sdb len += strlen(name) + 1; 574248102Sdb len += sizeof(*gr->gr_mem); 575247919Sdb } 576248102Sdb /* Allow for NULL pointer */ 577248102Sdb if (i != 0) 578248102Sdb len += sizeof(*gr->gr_mem); 579248102Sdb *num_mem = i; 580247919Sdb return(len); 581244736Sbapt} 582244736Sbapt 583244736Sbapt/* 584178431Sscf * Scan a line and place it into a group structure. 585178431Sscf */ 586178431Sscfstatic bool 587178431Sscf__gr_scan(char *line, struct group *gr) 588178431Sscf{ 589178431Sscf char *loc; 590178431Sscf int ndx; 591178431Sscf 592178431Sscf /* Assign non-member information to structure. */ 593178431Sscf gr->gr_name = line; 594178431Sscf if ((loc = strchr(line, ':')) == NULL) 595178431Sscf return (false); 596178431Sscf *loc = '\0'; 597178431Sscf gr->gr_passwd = loc + 1; 598184831Sscf if (*gr->gr_passwd == ':') 599184831Sscf *gr->gr_passwd = '\0'; 600178431Sscf else { 601178431Sscf if ((loc = strchr(loc + 1, ':')) == NULL) 602178431Sscf return (false); 603178431Sscf *loc = '\0'; 604178431Sscf } 605184831Sscf if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1) 606178431Sscf return (false); 607178431Sscf 608178431Sscf /* Assign member information to structure. */ 609178431Sscf if ((loc = strchr(loc + 1, ':')) == NULL) 610178431Sscf return (false); 611178431Sscf line = loc + 1; 612178431Sscf gr->gr_mem = NULL; 613185237Sscf ndx = 0; 614185237Sscf do { 615185237Sscf gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) * 616185237Sscf (ndx + 1)); 617185237Sscf if (gr->gr_mem == NULL) 618185237Sscf return (false); 619185237Sscf 620185237Sscf /* Skip locations without members (i.e., empty string). */ 621178431Sscf do { 622178431Sscf gr->gr_mem[ndx] = strsep(&line, ","); 623185237Sscf } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0'); 624185237Sscf } while (gr->gr_mem[ndx++] != NULL); 625178431Sscf 626178431Sscf return (true); 627178431Sscf} 628178431Sscf 629178431Sscf/* 630178431Sscf * Create a struct group from a line. 631178431Sscf */ 632178431Sscfstruct group * 633178431Sscfgr_scan(const char *line) 634178431Sscf{ 635184831Sscf struct group gr; 636185237Sscf char *line_copy; 637185237Sscf struct group *new_gr; 638178431Sscf 639185237Sscf if ((line_copy = strdup(line)) == NULL) 640178431Sscf return (NULL); 641185237Sscf if (!__gr_scan(line_copy, &gr)) { 642185237Sscf free(line_copy); 643178431Sscf return (NULL); 644178431Sscf } 645185237Sscf new_gr = gr_dup(&gr); 646185237Sscf free(line_copy); 647178431Sscf if (gr.gr_mem != NULL) 648178431Sscf free(gr.gr_mem); 649178431Sscf 650185237Sscf return (new_gr); 651178431Sscf} 652