install.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2003 by Glenn McGrath
  4. * SELinux support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //config:config INSTALL
  9. //config: bool "install"
  10. //config: default y
  11. //config: help
  12. //config: Copy files and set attributes.
  13. //config:
  14. //config:config FEATURE_INSTALL_LONG_OPTIONS
  15. //config: bool "Enable long options"
  16. //config: default y
  17. //config: depends on INSTALL && LONG_OPTS
  18. //config: help
  19. //config: Support long options for the install applet.
  20. //applet:IF_INSTALL(APPLET(install, BB_DIR_USR_BIN, BB_SUID_DROP))
  21. //kbuild:lib-$(CONFIG_INSTALL) += install.o
  22. /* -v, -b, -c are ignored */
  23. //usage:#define install_trivial_usage
  24. //usage: "[-cdDsp] [-o USER] [-g GRP] [-m MODE] [-t DIR] [SOURCE]... DEST"
  25. //usage:#define install_full_usage "\n\n"
  26. //usage: "Copy files and set attributes\n"
  27. //usage: "\n -c Just copy (default)"
  28. //usage: "\n -d Create directories"
  29. //usage: "\n -D Create leading target directories"
  30. //usage: "\n -s Strip symbol table"
  31. //usage: "\n -p Preserve date"
  32. //usage: "\n -o USER Set ownership"
  33. //usage: "\n -g GRP Set group ownership"
  34. //usage: "\n -m MODE Set permissions"
  35. //usage: "\n -t DIR Install to DIR"
  36. //usage: IF_SELINUX(
  37. //usage: "\n -Z Set security context"
  38. //usage: )
  39. #include "libbb.h"
  40. #include "libcoreutils/coreutils.h"
  41. #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
  42. static const char install_longopts[] ALIGN1 =
  43. IF_FEATURE_VERBOSE(
  44. "verbose\0" No_argument "v"
  45. )
  46. "directory\0" No_argument "d"
  47. "preserve-timestamps\0" No_argument "p"
  48. "strip\0" No_argument "s"
  49. "group\0" Required_argument "g"
  50. "mode\0" Required_argument "m"
  51. "owner\0" Required_argument "o"
  52. "target-directory\0" Required_argument "t"
  53. /* autofs build insists of using -b --suffix=.orig */
  54. /* TODO? (short option for --suffix is -S) */
  55. #if ENABLE_SELINUX
  56. "context\0" Required_argument "Z"
  57. "preserve_context\0" No_argument "\xff"
  58. "preserve-context\0" No_argument "\xff"
  59. #endif
  60. ;
  61. #endif
  62. #if ENABLE_SELINUX
  63. static void setdefaultfilecon(const char *path)
  64. {
  65. struct stat s;
  66. security_context_t scontext = NULL;
  67. if (!is_selinux_enabled()) {
  68. return;
  69. }
  70. if (lstat(path, &s) != 0) {
  71. return;
  72. }
  73. if (matchpathcon(path, s.st_mode, &scontext) < 0) {
  74. goto out;
  75. }
  76. if (strcmp(scontext, "<<none>>") == 0) {
  77. goto out;
  78. }
  79. if (lsetfilecon(path, scontext) < 0) {
  80. if (errno != ENOTSUP) {
  81. bb_perror_msg("warning: can't change context"
  82. " of %s to %s", path, scontext);
  83. }
  84. }
  85. out:
  86. freecon(scontext);
  87. }
  88. #endif
  89. int install_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  90. int install_main(int argc, char **argv)
  91. {
  92. struct stat statbuf;
  93. mode_t mode;
  94. uid_t uid;
  95. gid_t gid;
  96. char *arg, *last;
  97. const char *gid_str;
  98. const char *uid_str;
  99. const char *mode_str;
  100. int mkdir_flags = FILEUTILS_RECUR;
  101. int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
  102. int opts;
  103. int ret = EXIT_SUCCESS;
  104. int isdir;
  105. #if ENABLE_SELINUX
  106. security_context_t scontext;
  107. bool use_default_selinux_context = 1;
  108. #endif
  109. enum {
  110. OPT_c = 1 << 0,
  111. OPT_v = 1 << 1,
  112. OPT_b = 1 << 2,
  113. OPT_MKDIR_LEADING = 1 << 3,
  114. OPT_DIRECTORY = 1 << 4,
  115. OPT_PRESERVE_TIME = 1 << 5,
  116. OPT_STRIP = 1 << 6,
  117. OPT_GROUP = 1 << 7,
  118. OPT_MODE = 1 << 8,
  119. OPT_OWNER = 1 << 9,
  120. OPT_TARGET = 1 << 10,
  121. #if ENABLE_SELINUX
  122. OPT_SET_SECURITY_CONTEXT = 1 << 11,
  123. OPT_PRESERVE_SECURITY_CONTEXT = 1 << 12,
  124. #endif
  125. };
  126. #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
  127. applet_long_options = install_longopts;
  128. #endif
  129. opt_complementary = "t--d:d--t:s--d:d--s" IF_FEATURE_INSTALL_LONG_OPTIONS(IF_SELINUX(":Z--\xff:\xff--Z"));
  130. /* -c exists for backwards compatibility, it's needed */
  131. /* -b is ignored ("make a backup of each existing destination file") */
  132. opts = getopt32(argv, "cvb" "Ddpsg:m:o:t:" IF_SELINUX("Z:"),
  133. &gid_str, &mode_str, &uid_str, &last
  134. IF_SELINUX(, &scontext));
  135. argc -= optind;
  136. argv += optind;
  137. #if ENABLE_SELINUX
  138. if (opts & (OPT_PRESERVE_SECURITY_CONTEXT|OPT_SET_SECURITY_CONTEXT)) {
  139. selinux_or_die();
  140. use_default_selinux_context = 0;
  141. if (opts & OPT_PRESERVE_SECURITY_CONTEXT) {
  142. copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  143. }
  144. if (opts & OPT_SET_SECURITY_CONTEXT) {
  145. setfscreatecon_or_die(scontext);
  146. copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
  147. }
  148. }
  149. #endif
  150. if ((opts & OPT_v) && FILEUTILS_VERBOSE) {
  151. mkdir_flags |= FILEUTILS_VERBOSE;
  152. copy_flags |= FILEUTILS_VERBOSE;
  153. }
  154. /* preserve access and modification time, this is GNU behaviour,
  155. * BSD only preserves modification time */
  156. if (opts & OPT_PRESERVE_TIME) {
  157. copy_flags |= FILEUTILS_PRESERVE_STATUS;
  158. }
  159. mode = 0755; /* GNU coreutils 6.10 compat */
  160. if (opts & OPT_MODE)
  161. mode = bb_parse_mode(mode_str, mode);
  162. uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
  163. gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
  164. /* If -t DIR is in use, then isdir=true, last="DIR" */
  165. isdir = (opts & OPT_TARGET);
  166. if (!(opts & (OPT_TARGET|OPT_DIRECTORY))) {
  167. /* Neither -t DIR nor -d is in use */
  168. argc--;
  169. last = argv[argc];
  170. argv[argc] = NULL;
  171. /* coreutils install resolves link in this case, don't use lstat */
  172. isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
  173. }
  174. if (argc < 1)
  175. bb_show_usage();
  176. while ((arg = *argv++) != NULL) {
  177. char *dest;
  178. if (opts & OPT_DIRECTORY) {
  179. dest = arg;
  180. /* GNU coreutils 6.9 does not set uid:gid
  181. * on intermediate created directories
  182. * (only on last one) */
  183. if (bb_make_directory(dest, 0755, mkdir_flags)) {
  184. ret = EXIT_FAILURE;
  185. goto next;
  186. }
  187. } else {
  188. dest = last;
  189. if (opts & OPT_MKDIR_LEADING) {
  190. char *ddir = xstrdup(dest);
  191. bb_make_directory(dirname(ddir), 0755, mkdir_flags);
  192. /* errors are not checked. copy_file
  193. * will fail if dir is not created.
  194. */
  195. free(ddir);
  196. }
  197. if (isdir)
  198. dest = concat_path_file(last, bb_basename(arg));
  199. if (copy_file(arg, dest, copy_flags) != 0) {
  200. /* copy is not made */
  201. ret = EXIT_FAILURE;
  202. goto next;
  203. }
  204. if (opts & OPT_STRIP) {
  205. char *args[4];
  206. args[0] = (char*)"strip";
  207. args[1] = (char*)"-p"; /* -p --preserve-dates */
  208. args[2] = dest;
  209. args[3] = NULL;
  210. if (spawn_and_wait(args)) {
  211. bb_perror_msg("strip");
  212. ret = EXIT_FAILURE;
  213. }
  214. }
  215. }
  216. /* Set the file mode (always, not only with -m).
  217. * GNU coreutils 6.10 is not affected by umask. */
  218. if (chmod(dest, mode) == -1) {
  219. bb_perror_msg("can't change %s of %s", "permissions", dest);
  220. ret = EXIT_FAILURE;
  221. }
  222. #if ENABLE_SELINUX
  223. if (use_default_selinux_context)
  224. setdefaultfilecon(dest);
  225. #endif
  226. /* Set the user and group id */
  227. if ((opts & (OPT_OWNER|OPT_GROUP))
  228. && lchown(dest, uid, gid) == -1
  229. ) {
  230. bb_perror_msg("can't change %s of %s", "ownership", dest);
  231. ret = EXIT_FAILURE;
  232. }
  233. next:
  234. if (ENABLE_FEATURE_CLEAN_UP && isdir)
  235. free(dest);
  236. }
  237. return ret;
  238. }