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