install.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <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 char install_longopts[] ALIGN1 =
  17. "directory\0" No_argument "d"
  18. "preserve-timestamps\0" No_argument "p"
  19. "strip\0" No_argument "s"
  20. "group\0" No_argument "g"
  21. "mode\0" No_argument "m"
  22. "owner\0" No_argument "o"
  23. /* autofs build insists of using -b --suffix=.orig */
  24. /* TODO? (short option for --suffix is -S) */
  25. #if ENABLE_SELINUX
  26. "context\0" Required_argument "Z"
  27. "preserve_context\0" No_argument "\xff"
  28. "preserve-context\0" No_argument "\xff"
  29. #endif
  30. ;
  31. #endif
  32. #if ENABLE_SELINUX
  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) MAIN_EXTERNALLY_VISIBLE;
  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. char *arg, *last;
  66. const char *gid_str;
  67. const char *uid_str;
  68. const char *mode_str;
  69. int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
  70. int flags;
  71. int ret = EXIT_SUCCESS;
  72. int isdir;
  73. #if ENABLE_SELINUX
  74. security_context_t scontext;
  75. bool use_default_selinux_context = 1;
  76. #endif
  77. enum {
  78. OPT_c = 1 << 0,
  79. OPT_v = 1 << 1,
  80. OPT_b = 1 << 2,
  81. OPT_DIRECTORY = 1 << 3,
  82. OPT_PRESERVE_TIME = 1 << 4,
  83. OPT_STRIP = 1 << 5,
  84. OPT_GROUP = 1 << 6,
  85. OPT_MODE = 1 << 7,
  86. OPT_OWNER = 1 << 8,
  87. #if ENABLE_SELINUX
  88. OPT_SET_SECURITY_CONTEXT = 1 << 9,
  89. OPT_PRESERVE_SECURITY_CONTEXT = 1 << 10,
  90. #endif
  91. };
  92. #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
  93. applet_long_options = install_longopts;
  94. #endif
  95. opt_complementary = "s--d:d--s" USE_SELINUX(":Z--\xff:\xff--Z");
  96. /* -c exists for backwards compatibility, it's needed */
  97. /* -v is ignored ("print name of each created directory") */
  98. /* -b is ignored ("make a backup of each existing destination file") */
  99. flags = getopt32(argv, "cvb" "dpsg:m:o:" USE_SELINUX("Z:"),
  100. &gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext));
  101. argc -= optind;
  102. argv += optind;
  103. #if ENABLE_SELINUX
  104. if (flags & (OPT_PRESERVE_SECURITY_CONTEXT|OPT_SET_SECURITY_CONTEXT)) {
  105. selinux_or_die();
  106. use_default_selinux_context = 0;
  107. if (flags & OPT_PRESERVE_SECURITY_CONTEXT) {
  108. copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  109. }
  110. if (flags & OPT_SET_SECURITY_CONTEXT) {
  111. setfscreatecon_or_die(scontext);
  112. copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
  113. }
  114. }
  115. #endif
  116. /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
  117. if (flags & OPT_PRESERVE_TIME) {
  118. copy_flags |= FILEUTILS_PRESERVE_STATUS;
  119. }
  120. mode = 0666;
  121. if (flags & OPT_MODE)
  122. bb_parse_mode(mode_str, &mode);
  123. uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
  124. gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
  125. if (flags & (OPT_OWNER|OPT_GROUP))
  126. umask(0);
  127. /* Create directories
  128. * don't use bb_make_directory() as it can't change uid or gid
  129. * perhaps bb_make_directory() should be improved.
  130. */
  131. if (flags & OPT_DIRECTORY) {
  132. while ((arg = *argv++) != NULL) {
  133. char *slash = arg;
  134. while (1) {
  135. slash = strchr(slash + 1, '/');
  136. if (slash)
  137. *slash = '\0';
  138. if (mkdir(arg, mode | 0111) == -1) {
  139. if (errno != EEXIST) {
  140. bb_perror_msg("cannot create %s", arg);
  141. ret = EXIT_FAILURE;
  142. break;
  143. }
  144. } /* dir was created, chown? */
  145. else if ((flags & (OPT_OWNER|OPT_GROUP))
  146. && lchown(arg, uid, gid) == -1
  147. ) {
  148. bb_perror_msg("cannot change ownership of %s", arg);
  149. ret = EXIT_FAILURE;
  150. break;
  151. }
  152. if (!slash)
  153. break;
  154. *slash = '/';
  155. }
  156. }
  157. return ret;
  158. }
  159. if (argc < 2)
  160. bb_show_usage();
  161. last = argv[argc - 1];
  162. argv[argc - 1] = NULL;
  163. /* coreutils install resolves link in this case, don't use lstat */
  164. isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
  165. while ((arg = *argv++) != NULL) {
  166. char *dest = last;
  167. if (isdir)
  168. dest = concat_path_file(last, basename(arg));
  169. if (copy_file(arg, dest, copy_flags)) {
  170. /* copy is not made */
  171. ret = EXIT_FAILURE;
  172. goto next;
  173. }
  174. /* Set the file mode */
  175. if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
  176. bb_perror_msg("cannot change permissions of %s", dest);
  177. ret = EXIT_FAILURE;
  178. }
  179. #if ENABLE_SELINUX
  180. if (use_default_selinux_context)
  181. setdefaultfilecon(dest);
  182. #endif
  183. /* Set the user and group id */
  184. if ((flags & (OPT_OWNER|OPT_GROUP))
  185. && lchown(dest, uid, gid) == -1
  186. ) {
  187. bb_perror_msg("cannot change ownership of %s", dest);
  188. ret = EXIT_FAILURE;
  189. }
  190. if (flags & OPT_STRIP) {
  191. char *args[3];
  192. args[0] = (char*)"strip";
  193. args[1] = dest;
  194. args[2] = NULL;
  195. if (spawn_and_wait(args)) {
  196. bb_perror_msg("strip");
  197. ret = EXIT_FAILURE;
  198. }
  199. }
  200. next:
  201. if (ENABLE_FEATURE_CLEAN_UP && isdir)
  202. free(dest);
  203. }
  204. return ret;
  205. }