cmdline.c 12 KB

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