Home | History | Annotate | Line # | Download | only in cpio
cmdline.c revision 1.1.1.1.4.2
      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 __FBSDID("$FreeBSD: src/usr.bin/cpio/cmdline.c,v 1.3 2008/06/21 02:20:20 kientzle Exp $");
     30 
     31 #ifdef HAVE_ERRNO_H
     32 #include <errno.h>
     33 #endif
     34 #ifdef HAVE_GETOPT_LONG
     35 #include <getopt.h>
     36 #else
     37 struct option {
     38 	const char *name;
     39 	int has_arg;
     40 	int *flag;
     41 	int val;
     42 };
     43 #define	no_argument 0
     44 #define	required_argument 1
     45 #endif
     46 #ifdef HAVE_GRP_H
     47 #include <grp.h>
     48 #endif
     49 #ifdef HAVE_PWD_H
     50 #include <pwd.h>
     51 #endif
     52 #include <stdio.h>
     53 #ifdef HAVE_STDLIB_H
     54 #include <stdlib.h>
     55 #endif
     56 #ifdef HAVE_STRING_H
     57 #include <string.h>
     58 #endif
     59 
     60 #include "cpio.h"
     61 
     62 /*
     63  *
     64  * Option parsing routines for bsdcpio.
     65  *
     66  */
     67 
     68 
     69 static const char *cpio_opts = "0AaBC:F:O:cdE:f:H:hijLlmopR:rtuvW:yZz";
     70 
     71 /*
     72  * On systems that lack getopt_long, long options can be specified
     73  * using -W longopt and -W longopt=value, e.g. "-W version" is the
     74  * same as "--version" and "-W format=ustar" is the same as "--format
     75  * ustar".  This does not rely the GNU getopt() "W;" extension, so
     76  * should work correctly on any system with a POSIX-compliant
     77  * getopt().
     78  */
     79 
     80 /*
     81  * If you add anything, be very careful to keep this list properly
     82  * sorted, as the -W logic below relies on it.
     83  */
     84 static const struct option cpio_longopts[] = {
     85 	{ "create",		no_argument,	   NULL, 'o' },
     86 	{ "extract",		no_argument,       NULL, 'i' },
     87 	{ "file",		required_argument, NULL, 'F' },
     88 	{ "format",             required_argument, NULL, 'H' },
     89 	{ "help",		no_argument,	   NULL, 'h' },
     90 	{ "insecure",		no_argument,	   NULL, OPTION_INSECURE },
     91 	{ "link",		no_argument,	   NULL, 'l' },
     92 	{ "list",		no_argument,	   NULL, 't' },
     93 	{ "make-directories",	no_argument,	   NULL, 'd' },
     94 	{ "null",		no_argument,	   NULL, '0' },
     95 	{ "owner",		required_argument, NULL, 'R' },
     96 	{ "pass-through",	no_argument,	   NULL, 'p' },
     97 	{ "preserve-modification-time", no_argument, NULL, 'm' },
     98 	{ "quiet",		no_argument,	   NULL, OPTION_QUIET },
     99 	{ "unconditional",	no_argument,	   NULL, 'u' },
    100 	{ "verbose",            no_argument,       NULL, 'v' },
    101 	{ "version",            no_argument,       NULL, OPTION_VERSION },
    102 	{ NULL, 0, NULL, 0 }
    103 };
    104 
    105 /*
    106  * Parse command-line options using system-provided getopt() or getopt_long().
    107  * If option is -W, then parse argument as a long option.
    108  */
    109 int
    110 cpio_getopt(struct cpio *cpio)
    111 {
    112 	char *p, *q;
    113 	const struct option *option, *option2;
    114 	int opt;
    115 	int option_index;
    116 	size_t option_length;
    117 
    118 	option_index = -1;
    119 
    120 #ifdef HAVE_GETOPT_LONG
    121 	opt = getopt_long(cpio->argc, cpio->argv, cpio_opts,
    122 	    cpio_longopts, &option_index);
    123 #else
    124 	opt = getopt(cpio->argc, cpio->argv, cpio_opts);
    125 #endif
    126 
    127 	/* Support long options through -W longopt=value */
    128 	if (opt == 'W') {
    129 		p = optarg;
    130 		q = strchr(optarg, '=');
    131 		if (q != NULL) {
    132 			option_length = (size_t)(q - p);
    133 			optarg = q + 1;
    134 		} else {
    135 			option_length = strlen(p);
    136 			optarg = NULL;
    137 		}
    138 		option = cpio_longopts;
    139 		while (option->name != NULL &&
    140 		    (strlen(option->name) < option_length ||
    141 		    strncmp(p, option->name, option_length) != 0 )) {
    142 			option++;
    143 		}
    144 
    145 		if (option->name != NULL) {
    146 			option2 = option;
    147 			opt = option->val;
    148 
    149 			/* If the first match was exact, we're done. */
    150 			if (strncmp(p, option->name, strlen(option->name)) == 0) {
    151 				while (option->name != NULL)
    152 					option++;
    153 			} else {
    154 				/* Check if there's another match. */
    155 				option++;
    156 				while (option->name != NULL &&
    157 				    (strlen(option->name) < option_length ||
    158 				    strncmp(p, option->name, option_length) != 0)) {
    159 					option++;
    160 				}
    161 			}
    162 			if (option->name != NULL)
    163 				cpio_errc(1, 0,
    164 				    "Ambiguous option %s "
    165 				    "(matches both %s and %s)",
    166 				    p, option2->name, option->name);
    167 
    168 			if (option2->has_arg == required_argument
    169 			    && optarg == NULL)
    170 				cpio_errc(1, 0,
    171 				    "Option \"%s\" requires argument", p);
    172 		} else {
    173 			opt = '?';
    174 		}
    175 	}
    176 
    177 	return (opt);
    178 }
    179 
    180 
    181 /*
    182  * Parse the argument to the -R or --owner flag.
    183  *
    184  * The format is one of the following:
    185  *   <user>    - Override user but not group
    186  *   <user>:   - Override both, group is user's default group
    187  *   <user>:<group> - Override both
    188  *   :<group>  - Override group but not user
    189  *
    190  * A period can be used instead of the colon.
    191  *
    192  * Sets uid/gid as appropriate, -1 indicates uid/gid not specified.
    193  *
    194  */
    195 int
    196 owner_parse(const char *spec, int *uid, int *gid)
    197 {
    198 	const char *u, *ue, *g;
    199 
    200 	*uid = -1;
    201 	*gid = -1;
    202 
    203 	/*
    204 	 * Split spec into [user][:.][group]
    205 	 *  u -> first char of username, NULL if no username
    206 	 *  ue -> first char after username (colon, period, or \0)
    207 	 *  g -> first char of group name
    208 	 */
    209 	if (*spec == ':' || *spec == '.') {
    210 		/* If spec starts with ':' or '.', then just group. */
    211 		ue = u = NULL;
    212 		g = spec + 1;
    213 	} else {
    214 		/* Otherwise, [user] or [user][:] or [user][:][group] */
    215 		ue = u = spec;
    216 		while (*ue != ':' && *ue != '.' && *ue != '\0')
    217 			++ue;
    218 		g = ue;
    219 		if (*g != '\0') /* Skip : or . to find first char of group. */
    220 			++g;
    221 	}
    222 
    223 	if (u != NULL) {
    224 		/* Look up user: ue is first char after end of user. */
    225 		char *user;
    226 		struct passwd *pwent;
    227 
    228 		user = (char *)malloc(ue - u + 1);
    229 		if (user == NULL) {
    230 			cpio_warnc(errno, "Couldn't allocate memory");
    231 			return (1);
    232 		}
    233 		memcpy(user, u, ue - u);
    234 		user[ue - u] = '\0';
    235 		pwent = getpwnam(user);
    236 		if (pwent == NULL) {
    237 			cpio_warnc(errno, "Couldn't lookup user ``%s''", user);
    238 			return (1);
    239 		}
    240 		free(user);
    241 		*uid = pwent->pw_uid;
    242 		if (*ue != '\0' && *g == '\0')
    243 			*gid = pwent->pw_gid;
    244 	}
    245 	if (*g != '\0') {
    246 		struct group *grp;
    247 		grp = getgrnam(g);
    248 		if (grp != NULL)
    249 			*gid = grp->gr_gid;
    250 		else {
    251 			cpio_warnc(errno, "Couldn't look up group ``%s''", g);
    252 			return (1);
    253 		}
    254 	}
    255 	return (0);
    256 }
    257