gr_util.c (244739) | gr_util.c (244742) |
---|---|
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> | 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 244739 2012-12-27 16:51:29Z bapt $"); | 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 | 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 |
47struct group_storage { 48 struct group gr; 49 char *members[]; 50}; 51 | |
52static int lockfd = -1; 53static char group_dir[PATH_MAX]; 54static char group_file[PATH_MAX]; 55static char tempname[PATH_MAX]; 56static int initialized; 57 58static const char group_line_format[] = "%s:%s:%ju:"; 59 --- 369 unchanged lines hidden (view full) --- 429} 430 431/* 432 * Duplicate a struct group. 433 */ 434struct group * 435gr_dup(const struct group *gr) 436{ | 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; |
|
437 char *dst; 438 size_t len; | 433 char *dst; 434 size_t len; |
439 struct group_storage *gs; | |
440 int ndx; 441 int num_mem; 442 443 /* Calculate size of the group. */ | 435 int ndx; 436 int num_mem; 437 438 /* Calculate size of the group. */ |
444 len = sizeof(*gs); | 439 len = sizeof(*newgr); |
445 if (gr->gr_name != NULL) 446 len += strlen(gr->gr_name) + 1; 447 if (gr->gr_passwd != NULL) 448 len += strlen(gr->gr_passwd) + 1; 449 if (gr->gr_mem != NULL) { 450 for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++) 451 len += strlen(gr->gr_mem[num_mem]) + 1; 452 len += (num_mem + 1) * sizeof(*gr->gr_mem); 453 } else 454 num_mem = -1; | 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; |
455 | |
456 /* Create new group and copy old group into it. */ | 450 /* Create new group and copy old group into it. */ |
457 if ((gs = calloc(1, len)) == NULL) | 451 if ((newgr = malloc(len)) == NULL) |
458 return (NULL); | 452 return (NULL); |
459 dst = (char *)&gs->members[num_mem + 1]; | 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); |
460 if (gr->gr_name != NULL) { | 461 if (gr->gr_name != NULL) { |
461 gs->gr.gr_name = dst; 462 dst = stpcpy(gs->gr.gr_name, gr->gr_name) + 1; | 462 newgr->gr_name = dst; 463 dst = stpcpy(dst, gr->gr_name) + 1; |
463 } 464 if (gr->gr_passwd != NULL) { | 464 } 465 if (gr->gr_passwd != NULL) { |
465 gs->gr.gr_passwd = dst; 466 dst = stpcpy(gs->gr.gr_passwd, gr->gr_passwd) + 1; | 466 newgr->gr_passwd = dst; 467 dst = stpcpy(dst, gr->gr_passwd) + 1; |
467 } | 468 } |
468 gs->gr.gr_gid = gr->gr_gid; | 469 newgr->gr_gid = gr->gr_gid; |
469 if (gr->gr_mem != NULL) { | 470 if (gr->gr_mem != NULL) { |
470 gs->gr.gr_mem = gs->members; | |
471 for (ndx = 0; ndx < num_mem; ndx++) { | 471 for (ndx = 0; ndx < num_mem; ndx++) { |
472 gs->gr.gr_mem[ndx] = dst; 473 dst = stpcpy(gs->gr.gr_mem[ndx], gr->gr_mem[ndx]) + 1; | 472 newgr->gr_mem[ndx] = dst; 473 dst = stpcpy(dst, gr->gr_mem[ndx]) + 1; |
474 } | 474 } |
475 gs->gr.gr_mem[ndx] = NULL; | 475 newgr->gr_mem[ndx] = NULL; |
476 } | 476 } |
477 478 return (&gs->gr); | 477 return (newgr); |
479} 480 481/* 482 * Add a new member name to a struct group. 483 */ 484struct group * 485gr_add(struct group *gr, char *newmember) 486{ --- 102 unchanged lines hidden --- | 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 --- |