install.c 5.2 KB

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