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