install.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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" No_argument "g"
  19. "mode\0" No_argument "m"
  20. "owner\0" No_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 ret = EXIT_SUCCESS;
  70. int isdir;
  71. #if ENABLE_SELINUX
  72. security_context_t scontext;
  73. bool use_default_selinux_context = 1;
  74. #endif
  75. enum {
  76. OPT_c = 1 << 0,
  77. OPT_v = 1 << 1,
  78. OPT_b = 1 << 2,
  79. OPT_DIRECTORY = 1 << 3,
  80. OPT_PRESERVE_TIME = 1 << 4,
  81. OPT_STRIP = 1 << 5,
  82. OPT_GROUP = 1 << 6,
  83. OPT_MODE = 1 << 7,
  84. OPT_OWNER = 1 << 8,
  85. #if ENABLE_SELINUX
  86. OPT_SET_SECURITY_CONTEXT = 1 << 9,
  87. OPT_PRESERVE_SECURITY_CONTEXT = 1 << 10,
  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" USE_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. flags = getopt32(argv, "cvb" "dpsg:m:o:" USE_SELINUX("Z:"),
  98. &gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext));
  99. argc -= optind;
  100. argv += optind;
  101. #if ENABLE_SELINUX
  102. if (flags & (OPT_PRESERVE_SECURITY_CONTEXT|OPT_SET_SECURITY_CONTEXT)) {
  103. selinux_or_die();
  104. use_default_selinux_context = 0;
  105. if (flags & OPT_PRESERVE_SECURITY_CONTEXT) {
  106. copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  107. }
  108. if (flags & 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, BSD only preserves modification time */
  115. if (flags & OPT_PRESERVE_TIME) {
  116. copy_flags |= FILEUTILS_PRESERVE_STATUS;
  117. }
  118. mode = 0666;
  119. if (flags & OPT_MODE)
  120. bb_parse_mode(mode_str, &mode);
  121. uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
  122. gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
  123. if (flags & (OPT_OWNER|OPT_GROUP))
  124. umask(0);
  125. /* Create directories
  126. * don't use bb_make_directory() as it can't change uid or gid
  127. * perhaps bb_make_directory() should be improved.
  128. */
  129. if (flags & OPT_DIRECTORY) {
  130. while ((arg = *argv++) != NULL) {
  131. char *slash = arg;
  132. while (1) {
  133. slash = strchr(slash + 1, '/');
  134. if (slash)
  135. *slash = '\0';
  136. if (mkdir(arg, mode | 0111) == -1) {
  137. if (errno != EEXIST) {
  138. bb_perror_msg("cannot create %s", arg);
  139. ret = EXIT_FAILURE;
  140. break;
  141. }
  142. } /* dir was created, chown? */
  143. else if ((flags & (OPT_OWNER|OPT_GROUP))
  144. && lchown(arg, uid, gid) == -1
  145. ) {
  146. bb_perror_msg("cannot change ownership of %s", arg);
  147. ret = EXIT_FAILURE;
  148. break;
  149. }
  150. if (!slash)
  151. break;
  152. *slash = '/';
  153. }
  154. }
  155. return ret;
  156. }
  157. if (argc < 2)
  158. bb_show_usage();
  159. last = argv[argc - 1];
  160. argv[argc - 1] = NULL;
  161. /* coreutils install resolves link in this case, don't use lstat */
  162. isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
  163. while ((arg = *argv++) != NULL) {
  164. char *dest = last;
  165. if (isdir)
  166. dest = concat_path_file(last, basename(arg));
  167. if (copy_file(arg, dest, copy_flags)) {
  168. /* copy is not made */
  169. ret = EXIT_FAILURE;
  170. goto next;
  171. }
  172. /* Set the file mode */
  173. if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
  174. bb_perror_msg("cannot change permissions of %s", 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 ((flags & (OPT_OWNER|OPT_GROUP))
  183. && lchown(dest, uid, gid) == -1
  184. ) {
  185. bb_perror_msg("cannot change ownership of %s", dest);
  186. ret = EXIT_FAILURE;
  187. }
  188. if (flags & OPT_STRIP) {
  189. char *args[3];
  190. args[0] = (char*)"strip";
  191. args[1] = dest;
  192. args[2] = NULL;
  193. if (spawn_and_wait(args)) {
  194. bb_perror_msg("strip");
  195. ret = EXIT_FAILURE;
  196. }
  197. }
  198. next:
  199. if (ENABLE_FEATURE_CLEAN_UP && isdir)
  200. free(dest);
  201. }
  202. return ret;
  203. }