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 --- 11 unchanged lines hidden (view full) --- 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 244742 2012-12-27 19:33:43Z bapt $"); |
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; 52 53static const char group_line_format[] = "%s:%s:%ju:"; 54 --- 369 unchanged lines hidden (view full) --- 424} 425 426/* 427 * Duplicate a struct group. 428 */ 429struct group * 430gr_dup(const struct group *gr) 431{ |
432 struct group *newgr; |
433 char *dst; 434 size_t len; |
435 int ndx; 436 int num_mem; 437 438 /* Calculate size of the group. */ |
439 len = sizeof(*newgr); |
440 if (gr->gr_name != NULL) 441 len += strlen(gr->gr_name) + 1; 442 if (gr->gr_passwd != NULL) 443 len += strlen(gr->gr_passwd) + 1; 444 if (gr->gr_mem != NULL) { 445 for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++) 446 len += strlen(gr->gr_mem[num_mem]) + 1; 447 len += (num_mem + 1) * sizeof(*gr->gr_mem); 448 } else 449 num_mem = -1; |
450 /* Create new group and copy old group into it. */ |
451 if ((newgr = malloc(len)) == NULL) |
452 return (NULL); |
453 /* point new gr_mem to end of struct + 1 */ 454 if (gr->gr_mem != NULL) 455 newgr->gr_mem = (char **)newgr + sizeof(struct group); 456 else 457 newgr->gr_mem = NULL; 458 /* point dst after the end of all the gr_mem pointers in newgr */ 459 dst = (char *)newgr + sizeof(struct group) + 460 (num_mem + 1) * sizeof(*gr->gr_mem); |
461 if (gr->gr_name != NULL) { |
462 newgr->gr_name = dst; 463 dst = stpcpy(dst, gr->gr_name) + 1; |
464 } 465 if (gr->gr_passwd != NULL) { |
466 newgr->gr_passwd = dst; 467 dst = stpcpy(dst, gr->gr_passwd) + 1; |
468 } |
469 newgr->gr_gid = gr->gr_gid; |
470 if (gr->gr_mem != NULL) { |
471 for (ndx = 0; ndx < num_mem; ndx++) { |
472 newgr->gr_mem[ndx] = dst; 473 dst = stpcpy(dst, gr->gr_mem[ndx]) + 1; |
474 } |
475 newgr->gr_mem[ndx] = NULL; |
476 } |
477 return (newgr); |
478} 479 480/* 481 * Add a new member name to a struct group. 482 */ 483struct group * 484gr_add(struct group *gr, char *newmember) 485{ --- 102 unchanged lines hidden --- |