install.c 6.5 KB

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