gr_util.c revision 178431
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: head/lib/libutil/gr_util.c 178431 2008-04-23 00:49:13Z scf $"); 29178431Sscf 30178431Sscf#include <sys/param.h> 31178431Sscf#include <grp.h> 32178431Sscf#include <inttypes.h> 33178431Sscf#include <stdbool.h> 34178431Sscf#include <stdio.h> 35178431Sscf#include <stdlib.h> 36178431Sscf#include <string.h> 37178431Sscf 38178431Sscf#include <libutil.h> 39178431Sscf 40178431Sscf 41178431Sscfstatic const char GroupLineFormat[] = "%s:%s:%ju:"; 42178431Sscf 43178431Sscf 44178431Sscf/* 45178431Sscf * Compares two struct group's. 46178431Sscf */ 47178431Sscfint 48178431Sscfgr_equal(const struct group *gr1, const struct group *gr2) 49178431Sscf{ 50178431Sscf bool found; 51178431Sscf bool equal; 52178431Sscf int gr1Ndx; 53178431Sscf int gr2Ndx; 54178431Sscf 55178431Sscf /* Check that the non-member information is the same. */ 56178431Sscf equal = strcmp(gr1->gr_name, gr2->gr_name) == 0 && 57178431Sscf strcmp(gr1->gr_passwd, gr2->gr_passwd) == 0 && 58178431Sscf gr1->gr_gid == gr2->gr_gid; 59178431Sscf 60178431Sscf /* Check all members in both groups. */ 61178431Sscf if (equal) { 62178431Sscf for (found = false, gr1Ndx = 0; gr1->gr_mem[gr1Ndx] != NULL; 63178431Sscf gr1Ndx++) { 64178431Sscf for (gr2Ndx = 0; gr2->gr_mem[gr2Ndx] != NULL; gr2Ndx++) 65178431Sscf if (strcmp(gr1->gr_mem[gr1Ndx], 66178431Sscf gr2->gr_mem[gr2Ndx]) == 0) { 67178431Sscf found = true; 68178431Sscf break; 69178431Sscf } 70178431Sscf if (! found) { 71178431Sscf equal = false; 72178431Sscf break; 73178431Sscf } 74178431Sscf } 75178431Sscf 76178431Sscf /* Check that group2 does not have more members than group1. */ 77178431Sscf if (gr2->gr_mem[gr1Ndx] != NULL) 78178431Sscf equal = false; 79178431Sscf } 80178431Sscf 81178431Sscf return (equal); 82178431Sscf} 83178431Sscf 84178431Sscf 85178431Sscf/* 86178431Sscf * Make a group line out of a struct group. 87178431Sscf */ 88178431Sscfchar * 89178431Sscfgr_make(const struct group *gr) 90178431Sscf{ 91178431Sscf char *line; 92178431Sscf int ndx; 93178431Sscf size_t lineSize; 94178431Sscf 95178431Sscf /* Calculate the length of the group line. */ 96178431Sscf lineSize = snprintf(NULL, 0, GroupLineFormat, gr->gr_name, 97178431Sscf gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1; 98178431Sscf for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) 99178431Sscf lineSize += strlen(gr->gr_mem[ndx]) + 1; 100178431Sscf if (ndx > 0) 101178431Sscf lineSize--; 102178431Sscf 103178431Sscf /* Create the group line and fill it. */ 104178431Sscf if ((line = malloc(lineSize)) == NULL) 105178431Sscf return (NULL); 106178431Sscf lineSize = snprintf(line, lineSize, GroupLineFormat, gr->gr_name, 107178431Sscf gr->gr_passwd, (uintmax_t)gr->gr_gid); 108178431Sscf for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { 109178431Sscf strcat(line, gr->gr_mem[ndx]); 110178431Sscf if (gr->gr_mem[ndx + 1] != NULL) 111178431Sscf strcat(line, ","); 112178431Sscf } 113178431Sscf 114178431Sscf return (line); 115178431Sscf} 116178431Sscf 117178431Sscf 118178431Sscf/* 119178431Sscf * Duplicate a struct group. 120178431Sscf */ 121178431Sscfstruct group * 122178431Sscfgr_dup(const struct group *gr) 123178431Sscf{ 124178431Sscf int ndx; 125178431Sscf int numMem; 126178431Sscf size_t len; 127178431Sscf struct group *ngr; 128178431Sscf 129178431Sscf /* Calculate size of group. */ 130178431Sscf len = sizeof(*gr) + 131178431Sscf (gr->gr_name != NULL ? strlen(gr->gr_name) + 1 : 0) + 132178431Sscf (gr->gr_passwd != NULL ? strlen(gr->gr_passwd) + 1 : 0); 133178431Sscf numMem = 0; 134178431Sscf if (gr->gr_mem != NULL) { 135178431Sscf for (; gr->gr_mem[numMem] != NULL; numMem++) 136178431Sscf len += strlen(gr->gr_mem[numMem]) + 1; 137178431Sscf len += (numMem + 1) * sizeof(*(gr->gr_mem)); 138178431Sscf } 139178431Sscf 140178431Sscf /* Create new group and copy old group into it. */ 141178431Sscf if ((ngr = calloc(1, len)) == NULL) 142178431Sscf return (NULL); 143178431Sscf len = sizeof(*ngr); 144178431Sscf ngr->gr_gid = gr->gr_gid; 145178431Sscf if (gr->gr_name != NULL) { 146178431Sscf ngr->gr_name = (char *)ngr + len; 147178431Sscf len += sprintf(ngr->gr_name, "%s", gr->gr_name) + 1; 148178431Sscf } 149178431Sscf if (gr->gr_passwd != NULL) { 150178431Sscf ngr->gr_passwd = (char *)ngr + len; 151178431Sscf len += sprintf(ngr->gr_passwd, "%s", gr->gr_passwd) + 1; 152178431Sscf } 153178431Sscf if (gr->gr_mem != NULL) { 154178431Sscf ngr->gr_mem = (char **)((char *)ngr + len); 155178431Sscf len += (numMem + 1) * sizeof(*(ngr->gr_mem)); 156178431Sscf for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { 157178431Sscf ngr->gr_mem[ndx] = (char *)ngr + len; 158178431Sscf len += sprintf(ngr->gr_mem[ndx], "%s", 159178431Sscf gr->gr_mem[ndx]) + 1; 160178431Sscf } 161178431Sscf ngr->gr_mem[ndx] = NULL; 162178431Sscf } 163178431Sscf 164178431Sscf return (ngr); 165178431Sscf} 166178431Sscf 167178431Sscf 168178431Sscf/* 169178431Sscf * Scan a line and place it into a group structure. 170178431Sscf */ 171178431Sscfstatic bool 172178431Sscf__gr_scan(char *line, struct group *gr) 173178431Sscf{ 174178431Sscf char *loc; 175178431Sscf int ndx; 176178431Sscf 177178431Sscf /* Assign non-member information to structure. */ 178178431Sscf gr->gr_name = line; 179178431Sscf if ((loc = strchr(line, ':')) == NULL) 180178431Sscf return (false); 181178431Sscf *loc = '\0'; 182178431Sscf gr->gr_passwd = loc + 1; 183178431Sscf if (*(gr->gr_passwd) == ':') 184178431Sscf *(gr->gr_passwd) = '\0'; 185178431Sscf else { 186178431Sscf if ((loc = strchr(loc + 1, ':')) == NULL) 187178431Sscf return (false); 188178431Sscf *loc = '\0'; 189178431Sscf } 190178431Sscf if (sscanf(loc + 1, "%u", &(gr->gr_gid)) != 1) 191178431Sscf return (false); 192178431Sscf 193178431Sscf /* Assign member information to structure. */ 194178431Sscf if ((loc = strchr(loc + 1, ':')) == NULL) 195178431Sscf return (false); 196178431Sscf line = loc + 1; 197178431Sscf gr->gr_mem = NULL; 198178431Sscf if (*line != '\0') { 199178431Sscf ndx = 0; 200178431Sscf do { 201178431Sscf if ((gr->gr_mem = reallocf(gr->gr_mem, 202178431Sscf sizeof(*(gr->gr_mem)) * (ndx + 1))) == NULL) 203178431Sscf return (false); 204178431Sscf gr->gr_mem[ndx] = strsep(&line, ","); 205178431Sscf } while (gr->gr_mem[ndx++] != NULL); 206178431Sscf } 207178431Sscf 208178431Sscf return (true); 209178431Sscf} 210178431Sscf 211178431Sscf 212178431Sscf/* 213178431Sscf * Create a struct group from a line. 214178431Sscf */ 215178431Sscfstruct group * 216178431Sscfgr_scan(const char *line) 217178431Sscf{ 218178431Sscf char *lineCopy; 219178431Sscf struct group *newGr; 220178431Sscf struct group gr; 221178431Sscf 222178431Sscf if ((lineCopy = strdup(line)) == NULL) 223178431Sscf return (NULL); 224178431Sscf if (!__gr_scan(lineCopy, &gr)) { 225178431Sscf free(lineCopy); 226178431Sscf return (NULL); 227178431Sscf } 228178431Sscf newGr = gr_dup(&gr); 229178431Sscf free(lineCopy); 230178431Sscf if (gr.gr_mem != NULL) 231178431Sscf free(gr.gr_mem); 232178431Sscf 233178431Sscf return (newGr); 234178431Sscf} 235