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