cmdline.c 11 KB

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