install.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
  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 <libgen.h>
  12. #include <getopt.h> /* struct option */
  13. #include "libbb.h"
  14. #include "libcoreutils/coreutils.h"
  15. #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
  16. static const struct option install_long_options[] = {
  17. { "directory", 0, NULL, 'd' },
  18. { "preserve-timestamps", 0, NULL, 'p' },
  19. { "strip", 0, NULL, 's' },
  20. { "group", 0, NULL, 'g' },
  21. { "mode", 0, NULL, 'm' },
  22. { "owner", 0, NULL, 'o' },
  23. #if ENABLE_SELINUX
  24. { "context", 1, NULL, 'Z' },
  25. { "preserve_context", 0, NULL, 0xff },
  26. { "preserve-context", 0, NULL, 0xff },
  27. #endif
  28. { 0, 0, 0, 0 }
  29. };
  30. #endif
  31. #if ENABLE_SELINUX
  32. static bool use_default_selinux_context = 1;
  33. static void setdefaultfilecon(const char *path)
  34. {
  35. struct stat s;
  36. security_context_t scontext = NULL;
  37. if (!is_selinux_enabled()) {
  38. return;
  39. }
  40. if (lstat(path, &s) != 0) {
  41. return;
  42. }
  43. if (matchpathcon(path, s.st_mode, &scontext) < 0) {
  44. goto out;
  45. }
  46. if (strcmp(scontext, "<<none>>") == 0) {
  47. goto out;
  48. }
  49. if (lsetfilecon(path, scontext) < 0) {
  50. if (errno != ENOTSUP) {
  51. bb_perror_msg("warning: failed to change context of %s to %s", path, scontext);
  52. }
  53. }
  54. out:
  55. freecon(scontext);
  56. }
  57. #endif
  58. int install_main(int argc, char **argv);
  59. int install_main(int argc, char **argv)
  60. {
  61. struct stat statbuf;
  62. mode_t mode;
  63. uid_t uid;
  64. gid_t gid;
  65. const char *gid_str;
  66. const char *uid_str;
  67. const char *mode_str;
  68. int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
  69. int ret = EXIT_SUCCESS, flags, i, isdir;
  70. #if ENABLE_SELINUX
  71. security_context_t scontext;
  72. #endif
  73. enum {
  74. OPT_CMD = 0x1,
  75. OPT_DIRECTORY = 0x2,
  76. OPT_PRESERVE_TIME = 0x4,
  77. OPT_STRIP = 0x8,
  78. OPT_GROUP = 0x10,
  79. OPT_MODE = 0x20,
  80. OPT_OWNER = 0x40,
  81. #if ENABLE_SELINUX
  82. OPT_SET_SECURITY_CONTEXT = 0x80,
  83. OPT_PRESERVE_SECURITY_CONTEXT = 0x100,
  84. #endif
  85. };
  86. #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
  87. applet_long_options = install_long_options;
  88. #endif
  89. opt_complementary = "?:s--d:d--s" USE_SELINUX(":Z--\xff:\xff--Z");
  90. /* -c exists for backwards compatibility, it's needed */
  91. flags = getopt32(argc, argv, "cdpsg:m:o:" USE_SELINUX("Z:"), &gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext));
  92. #if ENABLE_SELINUX
  93. if (flags & OPT_PRESERVE_SECURITY_CONTEXT) {
  94. use_default_selinux_context = 0;
  95. copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  96. selinux_or_die();
  97. }
  98. if (flags & OPT_SET_SECURITY_CONTEXT) {
  99. selinux_or_die();
  100. setfscreatecon_or_die(scontext);
  101. use_default_selinux_context = 0;
  102. copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
  103. }
  104. #endif
  105. /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
  106. if (flags & OPT_PRESERVE_TIME) {
  107. copy_flags |= FILEUTILS_PRESERVE_STATUS;
  108. }
  109. mode = 0666;
  110. if (flags & OPT_MODE) bb_parse_mode(mode_str, &mode);
  111. uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
  112. gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
  113. if (flags & (OPT_OWNER|OPT_GROUP)) umask(0);
  114. /* Create directories
  115. * don't use bb_make_directory() as it can't change uid or gid
  116. * perhaps bb_make_directory() should be improved.
  117. */
  118. if (flags & OPT_DIRECTORY) {
  119. for (argv += optind; *argv; argv++) {
  120. char *old_argv_ptr = *argv + 1;
  121. char *argv_ptr;
  122. do {
  123. argv_ptr = strchr(old_argv_ptr, '/');
  124. old_argv_ptr = argv_ptr;
  125. if (argv_ptr) {
  126. *argv_ptr = '\0';
  127. old_argv_ptr++;
  128. }
  129. if (mkdir(*argv, mode | 0111) == -1) {
  130. if (errno != EEXIST) {
  131. bb_perror_msg("cannot create %s", *argv);
  132. ret = EXIT_FAILURE;
  133. break;
  134. }
  135. }
  136. if ((flags & (OPT_OWNER|OPT_GROUP))
  137. && lchown(*argv, uid, gid) == -1
  138. ) {
  139. bb_perror_msg("cannot change ownership of %s", *argv);
  140. ret = EXIT_FAILURE;
  141. break;
  142. }
  143. if (argv_ptr) {
  144. *argv_ptr = '/';
  145. }
  146. } while (old_argv_ptr);
  147. }
  148. return ret;
  149. }
  150. isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
  151. for (i = optind; i < argc - 1; i++) {
  152. char *dest;
  153. dest = argv[argc - 1];
  154. if (isdir)
  155. dest = concat_path_file(argv[argc - 1], basename(argv[i]));
  156. ret |= copy_file(argv[i], dest, copy_flags);
  157. /* Set the file mode */
  158. if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
  159. bb_perror_msg("cannot change permissions of %s", dest);
  160. ret = EXIT_FAILURE;
  161. }
  162. #if ENABLE_SELINUX
  163. if (use_default_selinux_context)
  164. setdefaultfilecon(dest);
  165. #endif
  166. /* Set the user and group id */
  167. if ((flags & (OPT_OWNER|OPT_GROUP))
  168. && lchown(dest, uid, gid) == -1
  169. ) {
  170. bb_perror_msg("cannot change ownership of %s", dest);
  171. ret = EXIT_FAILURE;
  172. }
  173. if (flags & OPT_STRIP) {
  174. if (BB_EXECLP("strip", "strip", dest, NULL) == -1) {
  175. bb_perror_msg("strip");
  176. ret = EXIT_FAILURE;
  177. }
  178. }
  179. if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
  180. }
  181. return ret;
  182. }