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