gr_util.c revision 184831
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 184831 2008-11-11 00:32:55Z scf $"); 29 30#include <sys/param.h> 31 32#include <grp.h> 33#include <inttypes.h> 34#include <libutil.h> 35#include <stdbool.h> 36#include <stdio.h> 37#include <stdlib.h> 38#include <string.h> 39 40static const char GroupLineFormat[] = "%s:%s:%ju:"; 41 42/* 43 * Compares two struct group's. 44 */ 45int 46gr_equal(const struct group *gr1, const struct group *gr2) 47{ 48 int gr1Ndx; 49 int gr2Ndx; 50 bool equal; 51 bool found; 52 53 /* Check that the non-member information is the same. */ 54 equal = strcmp(gr1->gr_name, gr2->gr_name) == 0 && 55 strcmp(gr1->gr_passwd, gr2->gr_passwd) == 0 && 56 gr1->gr_gid == gr2->gr_gid; 57 58 /* Check all members in both groups. */ 59 if (equal) { 60 for (found = false, gr1Ndx = 0; gr1->gr_mem[gr1Ndx] != NULL; 61 gr1Ndx++) { 62 for (gr2Ndx = 0; gr2->gr_mem[gr2Ndx] != NULL; gr2Ndx++) 63 if (strcmp(gr1->gr_mem[gr1Ndx], 64 gr2->gr_mem[gr2Ndx]) == 0) { 65 found = true; 66 break; 67 } 68 if (! found) { 69 equal = false; 70 break; 71 } 72 } 73 74 /* Check that group2 does not have more members than group1. */ 75 if (gr2->gr_mem[gr1Ndx] != NULL) 76 equal = false; 77 } 78 79 return (equal); 80} 81 82/* 83 * Make a group line out of a struct group. 84 */ 85char * 86gr_make(const struct group *gr) 87{ 88 char *line; 89 size_t lineSize; 90 int ndx; 91 92 /* Calculate the length of the group line. */ 93 lineSize = snprintf(NULL, 0, GroupLineFormat, gr->gr_name, 94 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1; 95 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) 96 lineSize += strlen(gr->gr_mem[ndx]) + 1; 97 if (ndx > 0) 98 lineSize--; 99 100 /* Create the group line and fill it. */ 101 if ((line = malloc(lineSize)) == NULL) 102 return (NULL); 103 lineSize = snprintf(line, lineSize, GroupLineFormat, gr->gr_name, 104 gr->gr_passwd, (uintmax_t)gr->gr_gid); 105 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { 106 strcat(line, gr->gr_mem[ndx]); 107 if (gr->gr_mem[ndx + 1] != NULL) 108 strcat(line, ","); 109 } 110 111 return (line); 112} 113 114/* 115 * Duplicate a struct group. 116 */ 117struct group * 118gr_dup(const struct group *gr) 119{ 120 size_t len; 121 struct group *ngr; 122 int ndx; 123 int numMem; 124 125 /* Calculate size of group. */ 126 len = sizeof(*gr) + 127 (gr->gr_name != NULL ? strlen(gr->gr_name) + 1 : 0) + 128 (gr->gr_passwd != NULL ? strlen(gr->gr_passwd) + 1 : 0); 129 numMem = 0; 130 if (gr->gr_mem != NULL) { 131 for (; gr->gr_mem[numMem] != NULL; numMem++) 132 len += strlen(gr->gr_mem[numMem]) + 1; 133 len += (numMem + 1) * sizeof(*gr->gr_mem); 134 } 135 136 /* Create new group and copy old group into it. */ 137 if ((ngr = calloc(1, len)) == NULL) 138 return (NULL); 139 len = sizeof(*ngr); 140 ngr->gr_gid = gr->gr_gid; 141 if (gr->gr_name != NULL) { 142 ngr->gr_name = (char *)ngr + len; 143 len += sprintf(ngr->gr_name, "%s", gr->gr_name) + 1; 144 } 145 if (gr->gr_passwd != NULL) { 146 ngr->gr_passwd = (char *)ngr + len; 147 len += sprintf(ngr->gr_passwd, "%s", gr->gr_passwd) + 1; 148 } 149 if (gr->gr_mem != NULL) { 150 ngr->gr_mem = (char **)((char *)ngr + len); 151 len += (numMem + 1) * sizeof(*ngr->gr_mem); 152 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { 153 ngr->gr_mem[ndx] = (char *)ngr + len; 154 len += sprintf(ngr->gr_mem[ndx], "%s", 155 gr->gr_mem[ndx]) + 1; 156 } 157 ngr->gr_mem[ndx] = NULL; 158 } 159 160 return (ngr); 161} 162 163/* 164 * Scan a line and place it into a group structure. 165 */ 166static bool 167__gr_scan(char *line, struct group *gr) 168{ 169 char *loc; 170 int ndx; 171 172 /* Assign non-member information to structure. */ 173 gr->gr_name = line; 174 if ((loc = strchr(line, ':')) == NULL) 175 return (false); 176 *loc = '\0'; 177 gr->gr_passwd = loc + 1; 178 if (*gr->gr_passwd == ':') 179 *gr->gr_passwd = '\0'; 180 else { 181 if ((loc = strchr(loc + 1, ':')) == NULL) 182 return (false); 183 *loc = '\0'; 184 } 185 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1) 186 return (false); 187 188 /* Assign member information to structure. */ 189 if ((loc = strchr(loc + 1, ':')) == NULL) 190 return (false); 191 line = loc + 1; 192 gr->gr_mem = NULL; 193 if (*line != '\0') { 194 ndx = 0; 195 do { 196 if ((gr->gr_mem = reallocf(gr->gr_mem, 197 sizeof(*gr->gr_mem) * (ndx + 1))) == NULL) 198 return (false); 199 gr->gr_mem[ndx] = strsep(&line, ","); 200 } while (gr->gr_mem[ndx++] != NULL); 201 } 202 203 return (true); 204} 205 206/* 207 * Create a struct group from a line. 208 */ 209struct group * 210gr_scan(const char *line) 211{ 212 struct group gr; 213 char *lineCopy; 214 struct group *newGr; 215 216 if ((lineCopy = strdup(line)) == NULL) 217 return (NULL); 218 if (!__gr_scan(lineCopy, &gr)) { 219 free(lineCopy); 220 return (NULL); 221 } 222 newGr = gr_dup(&gr); 223 free(lineCopy); 224 if (gr.gr_mem != NULL) 225 free(gr.gr_mem); 226 227 return (newGr); 228} 229