]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/cpio/cmdline.c
Remove GNU cpio after fix of CVE-2010-0624.
[FreeBSD/FreeBSD.git] / usr.bin / cpio / cmdline.c
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$");
30
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_GRP_H
35 #include <grp.h>
36 #endif
37 #ifdef HAVE_PWD_H
38 #include <pwd.h>
39 #endif
40 #include <stdio.h>
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47
48 #include "cpio.h"
49
50 /*
51  * Short options for cpio.  Please keep this sorted.
52  */
53 static const char *short_options = "0AaBC:F:O:cdE:f:H:hijLlmnopR:rtuvVW:yZz";
54
55 /*
56  * Long options for cpio.  Please keep this sorted.
57  */
58 static const struct option {
59         const char *name;
60         int required;   /* 1 if this option requires an argument */
61         int equivalent; /* Equivalent short option. */
62 } cpio_longopts[] = {
63         { "create",                     0, 'o' },
64         { "dot",                        0, 'V' },
65         { "extract",                    0, 'i' },
66         { "file",                       1, 'F' },
67         { "format",                     1, 'H' },
68         { "help",                       0, 'h' },
69         { "insecure",                   0, OPTION_INSECURE },
70         { "link",                       0, 'l' },
71         { "list",                       0, 't' },
72         { "make-directories",           0, 'd' },
73         { "no-preserve-owner",          0, OPTION_NO_PRESERVE_OWNER },
74         { "null",                       0, '0' },
75         { "numeric-uid-gid",            0, 'n' },
76         { "owner",                      1, 'R' },
77         { "pass-through",               0, 'p' },
78         { "preserve-modification-time", 0, 'm' },
79         { "quiet",                      0, OPTION_QUIET },
80         { "unconditional",              0, 'u' },
81         { "verbose",                    0, 'v' },
82         { "version",                    0, OPTION_VERSION },
83         { NULL, 0, 0 }
84 };
85
86 /*
87  * I used to try to select platform-provided getopt() or
88  * getopt_long(), but that caused a lot of headaches.  In particular,
89  * I couldn't consistently use long options in the test harness
90  * because not all platforms have getopt_long().  That in turn led to
91  * overuse of the -W hack in the test harness, which made it rough to
92  * run the test harness against GNU cpio.  (I periodically run the
93  * test harness here against GNU cpio as a sanity-check.  Yes,
94  * I've found a couple of bugs in GNU cpio that way.)
95  */
96 int
97 cpio_getopt(struct cpio *cpio)
98 {
99         enum { state_start = 0, state_next_word, state_short, state_long };
100         static int state = state_start;
101         static char *opt_word;
102
103         const struct option *popt, *match = NULL, *match2 = NULL;
104         const char *p, *long_prefix = "--";
105         size_t optlength;
106         int opt = '?';
107         int required = 0;
108
109         cpio->optarg = NULL;
110
111         /* First time through, initialize everything. */
112         if (state == state_start) {
113                 /* Skip program name. */
114                 ++cpio->argv;
115                 --cpio->argc;
116                 state = state_next_word;
117         }
118
119         /*
120          * We're ready to look at the next word in argv.
121          */
122         if (state == state_next_word) {
123                 /* No more arguments, so no more options. */
124                 if (cpio->argv[0] == NULL)
125                         return (-1);
126                 /* Doesn't start with '-', so no more options. */
127                 if (cpio->argv[0][0] != '-')
128                         return (-1);
129                 /* "--" marks end of options; consume it and return. */
130                 if (strcmp(cpio->argv[0], "--") == 0) {
131                         ++cpio->argv;
132                         --cpio->argc;
133                         return (-1);
134                 }
135                 /* Get next word for parsing. */
136                 opt_word = *cpio->argv++;
137                 --cpio->argc;
138                 if (opt_word[1] == '-') {
139                         /* Set up long option parser. */
140                         state = state_long;
141                         opt_word += 2; /* Skip leading '--' */
142                 } else {
143                         /* Set up short option parser. */
144                         state = state_short;
145                         ++opt_word;  /* Skip leading '-' */
146                 }
147         }
148
149         /*
150          * We're parsing a group of POSIX-style single-character options.
151          */
152         if (state == state_short) {
153                 /* Peel next option off of a group of short options. */
154                 opt = *opt_word++;
155                 if (opt == '\0') {
156                         /* End of this group; recurse to get next option. */
157                         state = state_next_word;
158                         return cpio_getopt(cpio);
159                 }
160
161                 /* Does this option take an argument? */
162                 p = strchr(short_options, opt);
163                 if (p == NULL)
164                         return ('?');
165                 if (p[1] == ':')
166                         required = 1;
167
168                 /* If it takes an argument, parse that. */
169                 if (required) {
170                         /* If arg is run-in, opt_word already points to it. */
171                         if (opt_word[0] == '\0') {
172                                 /* Otherwise, pick up the next word. */
173                                 opt_word = *cpio->argv;
174                                 if (opt_word == NULL) {
175                                         cpio_warnc(0,
176                                             "Option -%c requires an argument",
177                                             opt);
178                                         return ('?');
179                                 }
180                                 ++cpio->argv;
181                                 --cpio->argc;
182                         }
183                         if (opt == 'W') {
184                                 state = state_long;
185                                 long_prefix = "-W "; /* For clearer errors. */
186                         } else {
187                                 state = state_next_word;
188                                 cpio->optarg = opt_word;
189                         }
190                 }
191         }
192
193         /* We're reading a long option, including -W long=arg convention. */
194         if (state == state_long) {
195                 /* After this long option, we'll be starting a new word. */
196                 state = state_next_word;
197
198                 /* Option name ends at '=' if there is one. */
199                 p = strchr(opt_word, '=');
200                 if (p != NULL) {
201                         optlength = (size_t)(p - opt_word);
202                         cpio->optarg = (char *)(uintptr_t)(p + 1);
203                 } else {
204                         optlength = strlen(opt_word);
205                 }
206
207                 /* Search the table for an unambiguous match. */
208                 for (popt = cpio_longopts; popt->name != NULL; popt++) {
209                         /* Short-circuit if first chars don't match. */
210                         if (popt->name[0] != opt_word[0])
211                                 continue;
212                         /* If option is a prefix of name in table, record it.*/
213                         if (strncmp(opt_word, popt->name, optlength) == 0) {
214                                 match2 = match; /* Record up to two matches. */
215                                 match = popt;
216                                 /* If it's an exact match, we're done. */
217                                 if (strlen(popt->name) == optlength) {
218                                         match2 = NULL; /* Forget the others. */
219                                         break;
220                                 }
221                         }
222                 }
223
224                 /* Fail if there wasn't a unique match. */
225                 if (match == NULL) {
226                         cpio_warnc(0,
227                             "Option %s%s is not supported",
228                             long_prefix, opt_word);
229                         return ('?');
230                 }
231                 if (match2 != NULL) {
232                         cpio_warnc(0,
233                             "Ambiguous option %s%s (matches --%s and --%s)",
234                             long_prefix, opt_word, match->name, match2->name);
235                         return ('?');
236                 }
237
238                 /* We've found a unique match; does it need an argument? */
239                 if (match->required) {
240                         /* Argument required: get next word if necessary. */
241                         if (cpio->optarg == NULL) {
242                                 cpio->optarg = *cpio->argv;
243                                 if (cpio->optarg == NULL) {
244                                         cpio_warnc(0,
245                                             "Option %s%s requires an argument",
246                                             long_prefix, match->name);
247                                         return ('?');
248                                 }
249                                 ++cpio->argv;
250                                 --cpio->argc;
251                         }
252                 } else {
253                         /* Argument forbidden: fail if there is one. */
254                         if (cpio->optarg != NULL) {
255                                 cpio_warnc(0,
256                                     "Option %s%s does not allow an argument",
257                                     long_prefix, match->name);
258                                 return ('?');
259                         }
260                 }
261                 return (match->equivalent);
262         }
263
264         return (opt);
265 }
266
267
268 /*
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 group
273  *   <username>:   - Override both, group is user's default group
274  *   <uid>:    - Override user but not group
275  *   <username|uid>:<groupname|gid> - Override both
276  *   :<groupname|gid>  - Override group but not user
277  *
278  * Where uid/gid are decimal representations and groupname/username
279  * are names to be looked up in system database.  Note that we try
280  * 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  *
286  */
287 int
288 owner_parse(const char *spec, int *uid, int *gid)
289 {
290         const char *u, *ue, *g;
291
292         *uid = -1;
293         *gid = -1;
294
295         if (spec[0] == '\0')
296                 return (1);
297
298         /*
299          * Split spec into [user][:.][group]
300          *  u -> first char of username, NULL if no username
301          *  ue -> first char after username (colon, period, or \0)
302          *  g -> first char of group name
303          */
304         if (*spec == ':' || *spec == '.') {
305                 /* If spec starts with ':' or '.', then just group. */
306                 ue = u = NULL;
307                 g = spec + 1;
308         } else {
309                 /* Otherwise, [user] or [user][:] or [user][:][group] */
310                 ue = u = spec;
311                 while (*ue != ':' && *ue != '.' && *ue != '\0')
312                         ++ue;
313                 g = ue;
314                 if (*g != '\0') /* Skip : or . to find first char of group. */
315                         ++g;
316         }
317
318         if (u != NULL) {
319                 /* Look up user: ue is first char after end of user. */
320                 char *user;
321                 struct passwd *pwent;
322
323                 user = (char *)malloc(ue - u + 1);
324                 if (user == NULL) {
325                         cpio_warnc(errno, "Couldn't allocate memory");
326                         return (1);
327                 }
328                 memcpy(user, u, ue - u);
329                 user[ue - u] = '\0';
330                 if ((pwent = getpwnam(user)) != NULL) {
331                         *uid = pwent->pw_uid;
332                         if (*ue != '\0')
333                                 *gid = pwent->pw_gid;
334                 } else {
335                         char *end;
336                         errno = 0;
337                         *uid = strtoul(user, &end, 10);
338                         if (errno || *end != '\0') {
339                                 cpio_warnc(errno,
340                                     "Couldn't lookup user ``%s''", user);
341                                 return (1);
342                         }
343                 }
344                 free(user);
345         }
346
347         if (*g != '\0') {
348                 struct group *grp;
349                 if ((grp = getgrnam(g)) != NULL) {
350                         *gid = grp->gr_gid;
351                 } else {
352                         char *end;
353                         errno = 0;
354                         *gid = strtoul(g, &end, 10);
355                         if (errno || *end != '\0') {
356                                 cpio_warnc(errno,
357                                     "Couldn't lookup group ``%s''", g);
358                                 return (1);
359                         }
360                 }
361         }
362         return (0);
363 }