install.c 5.5 KB

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