3
0

install.c 6.1 KB

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