3
0

install.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 (12 kb)"
  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. # define GETOPT32 getopt32long
  60. # define LONGOPTS install_longopts,
  61. #else
  62. # define GETOPT32 getopt32
  63. # define LONGOPTS
  64. #endif
  65. #if ENABLE_SELINUX
  66. static void setdefaultfilecon(const char *path)
  67. {
  68. struct stat s;
  69. security_context_t scontext = NULL;
  70. if (!is_selinux_enabled()) {
  71. return;
  72. }
  73. if (lstat(path, &s) != 0) {
  74. return;
  75. }
  76. if (matchpathcon(path, s.st_mode, &scontext) < 0) {
  77. goto out;
  78. }
  79. if (strcmp(scontext, "<<none>>") == 0) {
  80. goto out;
  81. }
  82. if (lsetfilecon(path, scontext) < 0) {
  83. if (errno != ENOTSUP) {
  84. bb_perror_msg("warning: can't change context"
  85. " of %s to %s", path, scontext);
  86. }
  87. }
  88. out:
  89. freecon(scontext);
  90. }
  91. #endif
  92. int install_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  93. int install_main(int argc, char **argv)
  94. {
  95. struct stat statbuf;
  96. mode_t mode;
  97. uid_t uid;
  98. gid_t gid;
  99. char *arg, *last;
  100. const char *gid_str;
  101. const char *uid_str;
  102. const char *mode_str;
  103. int mkdir_flags = FILEUTILS_RECUR;
  104. int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
  105. int opts;
  106. int ret = EXIT_SUCCESS;
  107. int isdir;
  108. #if ENABLE_SELINUX
  109. security_context_t scontext;
  110. bool use_default_selinux_context = 1;
  111. #endif
  112. enum {
  113. OPT_c = 1 << 0,
  114. OPT_v = 1 << 1,
  115. OPT_b = 1 << 2,
  116. OPT_MKDIR_LEADING = 1 << 3,
  117. OPT_DIRECTORY = 1 << 4,
  118. OPT_PRESERVE_TIME = 1 << 5,
  119. OPT_STRIP = 1 << 6,
  120. OPT_GROUP = 1 << 7,
  121. OPT_MODE = 1 << 8,
  122. OPT_OWNER = 1 << 9,
  123. OPT_TARGET = 1 << 10,
  124. #if ENABLE_SELINUX
  125. OPT_SET_SECURITY_CONTEXT = 1 << 11,
  126. OPT_PRESERVE_SECURITY_CONTEXT = 1 << 12,
  127. #endif
  128. };
  129. /* -c exists for backwards compatibility, it's needed */
  130. /* -b is ignored ("make a backup of each existing destination file") */
  131. opts = GETOPT32(argv, "^"
  132. "cvb" "Ddpsg:m:o:t:" IF_SELINUX("Z:")
  133. "\0"
  134. "t--d:d--t:s--d:d--s"
  135. IF_FEATURE_INSTALL_LONG_OPTIONS(IF_SELINUX(":Z--\xff:\xff--Z")),
  136. LONGOPTS
  137. &gid_str, &mode_str, &uid_str, &last
  138. IF_SELINUX(, &scontext)
  139. );
  140. argc -= optind;
  141. argv += optind;
  142. #if ENABLE_SELINUX
  143. if (opts & (OPT_PRESERVE_SECURITY_CONTEXT|OPT_SET_SECURITY_CONTEXT)) {
  144. selinux_or_die();
  145. use_default_selinux_context = 0;
  146. if (opts & OPT_PRESERVE_SECURITY_CONTEXT) {
  147. copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  148. }
  149. if (opts & OPT_SET_SECURITY_CONTEXT) {
  150. setfscreatecon_or_die(scontext);
  151. copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
  152. }
  153. }
  154. #endif
  155. if ((opts & OPT_v) && FILEUTILS_VERBOSE) {
  156. mkdir_flags |= FILEUTILS_VERBOSE;
  157. copy_flags |= FILEUTILS_VERBOSE;
  158. }
  159. /* preserve access and modification time, this is GNU behaviour,
  160. * BSD only preserves modification time */
  161. if (opts & OPT_PRESERVE_TIME) {
  162. copy_flags |= FILEUTILS_PRESERVE_STATUS;
  163. }
  164. mode = 0755; /* GNU coreutils 6.10 compat */
  165. if (opts & OPT_MODE)
  166. mode = bb_parse_mode(mode_str, mode);
  167. uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
  168. gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
  169. /* If -t DIR is in use, then isdir=true, last="DIR" */
  170. isdir = (opts & OPT_TARGET);
  171. if (!(opts & (OPT_TARGET|OPT_DIRECTORY))) {
  172. /* Neither -t DIR nor -d is in use */
  173. argc--;
  174. last = argv[argc];
  175. argv[argc] = NULL;
  176. /* coreutils install resolves link in this case, don't use lstat */
  177. isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
  178. }
  179. if (argc < 1)
  180. bb_show_usage();
  181. while ((arg = *argv++) != NULL) {
  182. char *dest;
  183. if (opts & OPT_DIRECTORY) {
  184. dest = arg;
  185. /* GNU coreutils 6.9 does not set uid:gid
  186. * on intermediate created directories
  187. * (only on last one) */
  188. if (bb_make_directory(dest, 0755, mkdir_flags)) {
  189. ret = EXIT_FAILURE;
  190. goto next;
  191. }
  192. } else {
  193. dest = last;
  194. if (opts & OPT_MKDIR_LEADING) {
  195. char *ddir = xstrdup(dest);
  196. /*
  197. * -D -t DIR1/DIR2/F3 FILE: create DIR1/DIR2/F3, copy FILE there
  198. * -D FILE DIR1/DIR2/F3: create DIR1/DIR2, copy FILE there as F3
  199. */
  200. bb_make_directory((opts & OPT_TARGET) ? ddir : dirname(ddir), 0755, mkdir_flags);
  201. /* errors are not checked. copy_file
  202. * will fail if dir is not created.
  203. */
  204. free(ddir);
  205. }
  206. if (isdir)
  207. dest = concat_path_file(last, bb_basename(arg));
  208. if (copy_file(arg, dest, copy_flags) != 0) {
  209. /* copy is not made */
  210. ret = EXIT_FAILURE;
  211. goto next;
  212. }
  213. if (opts & OPT_STRIP) {
  214. char *args[4];
  215. args[0] = (char*)"strip";
  216. args[1] = (char*)"-p"; /* -p --preserve-dates */
  217. args[2] = dest;
  218. args[3] = NULL;
  219. if (spawn_and_wait(args)) {
  220. bb_perror_msg("strip");
  221. ret = EXIT_FAILURE;
  222. }
  223. }
  224. }
  225. /* Set the file mode (always, not only with -m).
  226. * GNU coreutils 6.10 is not affected by umask. */
  227. if (chmod(dest, mode) == -1) {
  228. bb_perror_msg("can't change %s of %s", "permissions", dest);
  229. ret = EXIT_FAILURE;
  230. }
  231. #if ENABLE_SELINUX
  232. if (use_default_selinux_context)
  233. setdefaultfilecon(dest);
  234. #endif
  235. /* Set the user and group id */
  236. if ((opts & (OPT_OWNER|OPT_GROUP))
  237. && lchown(dest, uid, gid) == -1
  238. ) {
  239. bb_perror_msg("can't change %s of %s", "ownership", dest);
  240. ret = EXIT_FAILURE;
  241. }
  242. next:
  243. if (ENABLE_FEATURE_CLEAN_UP && isdir)
  244. free(dest);
  245. }
  246. return ret;
  247. }