1 /*- 2 * Copyright (c) 2003-2007 Tim Kientzle 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 * in this position and unchanged. 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(S) ``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(S) 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 28 #include "cpio_platform.h" 29 30 #ifdef HAVE_ERRNO_H 31 #include <errno.h> 32 #endif 33 #ifdef HAVE_GRP_H 34 #include <grp.h> 35 #endif 36 #ifdef HAVE_PWD_H 37 #include <pwd.h> 38 #endif 39 #include <stdio.h> 40 #ifdef HAVE_STDLIB_H 41 #include <stdlib.h> 42 #endif 43 #ifdef HAVE_STRING_H 44 #include <string.h> 45 #endif 46 47 #include "cpio.h" 48 #include "err.h" 49 50 /* 51 * Short options for cpio. Please keep this sorted. 52 */ 53 static const char *short_options = "067AaBC:cdE:F:f:H:hI:iJjLlmnO:opR:rtuVvW:yZz"; 54 55 /* 56 * Long options for cpio. Please keep this sorted. 57 */ 58 static const struct option { 59 const char *name; 60 int required; /* 1 if this option requires an argument */ 61 int equivalent; /* Equivalent short option. */ 62 } cpio_longopts[] = { 63 { "b64encode", 0, OPTION_B64ENCODE }, 64 { "binary", 0, '7' }, 65 { "create", 0, 'o' }, 66 { "dereference", 0, 'L' }, 67 { "dot", 0, 'V' }, 68 { "extract", 0, 'i' }, 69 { "file", 1, 'F' }, 70 { "format", 1, 'H' }, 71 { "grzip", 0, OPTION_GRZIP }, 72 { "help", 0, 'h' }, 73 { "insecure", 0, OPTION_INSECURE }, 74 { "link", 0, 'l' }, 75 { "list", 0, 't' }, 76 { "lrzip", 0, OPTION_LRZIP }, 77 { "lz4", 0, OPTION_LZ4 }, 78 { "lzma", 0, OPTION_LZMA }, 79 { "lzop", 0, OPTION_LZOP }, 80 { "make-directories", 0, 'd' }, 81 { "no-preserve-owner", 0, OPTION_NO_PRESERVE_OWNER }, 82 { "null", 0, '0' }, 83 { "numeric-uid-gid", 0, 'n' }, 84 { "owner", 1, 'R' }, 85 { "passphrase", 1, OPTION_PASSPHRASE }, 86 { "pass-through", 0, 'p' }, 87 { "preserve-modification-time", 0, 'm' }, 88 { "preserve-owner", 0, OPTION_PRESERVE_OWNER }, 89 { "pwb", 0, '6' }, 90 { "quiet", 0, OPTION_QUIET }, 91 { "unconditional", 0, 'u' }, 92 { "uuencode", 0, OPTION_UUENCODE }, 93 { "verbose", 0, 'v' }, 94 { "version", 0, OPTION_VERSION }, 95 { "xz", 0, 'J' }, 96 { "zstd", 0, OPTION_ZSTD }, 97 { NULL, 0, 0 } 98 }; 99 100 /* 101 * I used to try to select platform-provided getopt() or 102 * getopt_long(), but that caused a lot of headaches. In particular, 103 * I couldn't consistently use long options in the test harness 104 * because not all platforms have getopt_long(). That in turn led to 105 * overuse of the -W hack in the test harness, which made it rough to 106 * run the test harness against GNU cpio. (I periodically run the 107 * test harness here against GNU cpio as a sanity-check. Yes, 108 * I've found a couple of bugs in GNU cpio that way.) 109 */ 110 int 111 cpio_getopt(struct cpio *cpio) 112 { 113 enum { state_start = 0, state_next_word, state_short, state_long }; 114 static int state = state_start; 115 static char *opt_word; 116 117 const struct option *popt, *match = NULL, *match2 = NULL; 118 const char *p, *long_prefix = "--"; 119 size_t optlength; 120 int opt = '?'; 121 int required = 0; 122 123 cpio->argument = NULL; 124 125 /* First time through, initialize everything. */ 126 if (state == state_start) { 127 /* Skip program name. */ 128 ++cpio->argv; 129 --cpio->argc; 130 state = state_next_word; 131 } 132 133 /* 134 * We're ready to look at the next word in argv. 135 */ 136 if (state == state_next_word) { 137 /* No more arguments, so no more options. */ 138 if (cpio->argv[0] == NULL) 139 return (-1); 140 /* Doesn't start with '-', so no more options. */ 141 if (cpio->argv[0][0] != '-') 142 return (-1); 143 /* "--" marks end of options; consume it and return. */ 144 if (strcmp(cpio->argv[0], "--") == 0) { 145 ++cpio->argv; 146 --cpio->argc; 147 return (-1); 148 } 149 /* Get next word for parsing. */ 150 opt_word = *cpio->argv++; 151 --cpio->argc; 152 if (opt_word[1] == '-') { 153 /* Set up long option parser. */ 154 state = state_long; 155 opt_word += 2; /* Skip leading '--' */ 156 } else { 157 /* Set up short option parser. */ 158 state = state_short; 159 ++opt_word; /* Skip leading '-' */ 160 } 161 } 162 163 /* 164 * We're parsing a group of POSIX-style single-character options. 165 */ 166 if (state == state_short) { 167 /* Peel next option off of a group of short options. */ 168 opt = *opt_word++; 169 if (opt == '\0') { 170 /* End of this group; recurse to get next option. */ 171 state = state_next_word; 172 return cpio_getopt(cpio); 173 } 174 175 /* Does this option take an argument? */ 176 p = strchr(short_options, opt); 177 if (p == NULL) 178 return ('?'); 179 if (p[1] == ':') 180 required = 1; 181 182 /* If it takes an argument, parse that. */ 183 if (required) { 184 /* If arg is run-in, opt_word already points to it. */ 185 if (opt_word[0] == '\0') { 186 /* Otherwise, pick up the next word. */ 187 opt_word = *cpio->argv; 188 if (opt_word == NULL) { 189 lafe_warnc(0, 190 "Option -%c requires an argument", 191 opt); 192 return ('?'); 193 } 194 ++cpio->argv; 195 --cpio->argc; 196 } 197 if (opt == 'W') { 198 state = state_long; 199 long_prefix = "-W "; /* For clearer errors. */ 200 } else { 201 state = state_next_word; 202 cpio->argument = opt_word; 203 } 204 } 205 } 206 207 /* We're reading a long option, including -W long=arg convention. */ 208 if (state == state_long) { 209 /* After this long option, we'll be starting a new word. */ 210 state = state_next_word; 211 212 /* Option name ends at '=' if there is one. */ 213 p = strchr(opt_word, '='); 214 if (p != NULL) { 215 optlength = (size_t)(p - opt_word); 216 cpio->argument = (char *)(uintptr_t)(p + 1); 217 } else { 218 optlength = strlen(opt_word); 219 } 220 221 /* Search the table for an unambiguous match. */ 222 for (popt = cpio_longopts; popt->name != NULL; popt++) { 223 /* Short-circuit if first chars don't match. */ 224 if (popt->name[0] != opt_word[0]) 225 continue; 226 /* If option is a prefix of name in table, record it.*/ 227 if (strncmp(opt_word, popt->name, optlength) == 0) { 228 match2 = match; /* Record up to two matches. */ 229 match = popt; 230 /* If it's an exact match, we're done. */ 231 if (strlen(popt->name) == optlength) { 232 match2 = NULL; /* Forget the others. */ 233 break; 234 } 235 } 236 } 237 238 /* Fail if there wasn't a unique match. */ 239 if (match == NULL) { 240 lafe_warnc(0, 241 "Option %s%s is not supported", 242 long_prefix, opt_word); 243 return ('?'); 244 } 245 if (match2 != NULL) { 246 lafe_warnc(0, 247 "Ambiguous option %s%s (matches --%s and --%s)", 248 long_prefix, opt_word, match->name, match2->name); 249 return ('?'); 250 } 251 252 /* We've found a unique match; does it need an argument? */ 253 if (match->required) { 254 /* Argument required: get next word if necessary. */ 255 if (cpio->argument == NULL) { 256 cpio->argument = *cpio->argv; 257 if (cpio->argument == NULL) { 258 lafe_warnc(0, 259 "Option %s%s requires an argument", 260 long_prefix, match->name); 261 return ('?'); 262 } 263 ++cpio->argv; 264 --cpio->argc; 265 } 266 } else { 267 /* Argument forbidden: fail if there is one. */ 268 if (cpio->argument != NULL) { 269 lafe_warnc(0, 270 "Option %s%s does not allow an argument", 271 long_prefix, match->name); 272 return ('?'); 273 } 274 } 275 return (match->equivalent); 276 } 277 278 return (opt); 279 } 280 281 282 /* 283 * Parse the argument to the -R or --owner flag. 284 * 285 * The format is one of the following: 286 * <username|uid> - Override user but not group 287 * <username>: - Override both, group is user's default group 288 * <uid>: - Override user but not group 289 * <username|uid>:<groupname|gid> - Override both 290 * :<groupname|gid> - Override group but not user 291 * 292 * Where uid/gid are decimal representations and groupname/username 293 * are names to be looked up in system database. Note that we try 294 * to look up an argument as a name first, then try numeric parsing. 295 * 296 * A period can be used instead of the colon. 297 * 298 * Sets uid/gid return as appropriate, -1 indicates uid/gid not specified. 299 * TODO: If the spec uses uname/gname, then return those to the caller 300 * as well. If the spec provides uid/gid, just return names as NULL. 301 * 302 * Returns NULL if no error, otherwise returns error string for display. 303 * 304 */ 305 const char * 306 owner_parse(const char *spec, int *uid, int *gid) 307 { 308 static char errbuff[128]; 309 const char *u, *ue, *g; 310 311 *uid = -1; 312 *gid = -1; 313 314 if (spec[0] == '\0') 315 return ("Invalid empty user/group spec"); 316 317 /* 318 * Split spec into [user][:.][group] 319 * u -> first char of username, NULL if no username 320 * ue -> first char after username (colon, period, or \0) 321 * g -> first char of group name 322 */ 323 if (*spec == ':' || *spec == '.') { 324 /* If spec starts with ':' or '.', then just group. */ 325 ue = u = NULL; 326 g = spec + 1; 327 } else { 328 /* Otherwise, [user] or [user][:] or [user][:][group] */ 329 ue = u = spec; 330 while (*ue != ':' && *ue != '.' && *ue != '\0') 331 ++ue; 332 g = ue; 333 if (*g != '\0') /* Skip : or . to find first char of group. */ 334 ++g; 335 } 336 337 if (u != NULL) { 338 /* Look up user: ue is first char after end of user. */ 339 char *user; 340 struct passwd *pwent; 341 342 user = (char *)malloc(ue - u + 1); 343 if (user == NULL) 344 return ("Couldn't allocate memory"); 345 memcpy(user, u, ue - u); 346 user[ue - u] = '\0'; 347 if ((pwent = getpwnam(user)) != NULL) { 348 *uid = pwent->pw_uid; 349 if (*ue != '\0') 350 *gid = pwent->pw_gid; 351 } else { 352 char *end; 353 errno = 0; 354 *uid = (int)strtoul(user, &end, 10); 355 if (errno || *end != '\0') { 356 snprintf(errbuff, sizeof(errbuff), 357 "Couldn't lookup user ``%s''", user); 358 errbuff[sizeof(errbuff) - 1] = '\0'; 359 free(user); 360 return (errbuff); 361 } 362 } 363 free(user); 364 } 365 366 if (*g != '\0') { 367 struct group *grp; 368 if ((grp = getgrnam(g)) != NULL) { 369 *gid = grp->gr_gid; 370 } else { 371 char *end; 372 errno = 0; 373 *gid = (int)strtoul(g, &end, 10); 374 if (errno || *end != '\0') { 375 snprintf(errbuff, sizeof(errbuff), 376 "Couldn't lookup group ``%s''", g); 377 errbuff[sizeof(errbuff) - 1] = '\0'; 378 return (errbuff); 379 } 380 } 381 } 382 return (NULL); 383 } 384