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