install.c 6.9 KB

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