copy_file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini copy_file implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. *
  10. */
  11. #include "libbb.h"
  12. // POSIX: if exists and -i, ask (w/o -i assume yes).
  13. // Then open w/o EXCL (yes, not unlink!).
  14. // If open still fails and -f, try unlink, then try open again.
  15. // Result: a mess:
  16. // If dest is a softlink, we overwrite softlink's destination!
  17. // (or fail, if it points to dir/nonexistent location/etc).
  18. // This is strange, but POSIX-correct.
  19. // coreutils cp has --remove-destination to override this...
  20. //
  21. // NB: we have special code which still allows for "cp file /dev/node"
  22. // to work POSIX-ly (the only realistic case where it makes sense)
  23. #define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */
  24. // errno must be set to relevant value ("why we cannot create dest?")
  25. // for POSIX mode to give reasonable error message
  26. static int ask_and_unlink(const char *dest, int flags)
  27. {
  28. #if DO_POSIX_CP
  29. if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
  30. // Either it exists, or the *path* doesnt exist
  31. bb_perror_msg("cannot create '%s'", dest);
  32. return -1;
  33. }
  34. #endif
  35. // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
  36. // "cannot create" msg, we want unlink to be done (silently unless -i).
  37. // TODO: maybe we should do it only if ctty is present?
  38. if (flags & FILEUTILS_INTERACTIVE) {
  39. // We would not do POSIX insanity. -i asks,
  40. // then _unlinks_ the offender. Presto.
  41. // (No "opening without O_EXCL", no "unlink only if -f")
  42. // Or else we will end up having 3 open()s!
  43. fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
  44. if (!bb_ask_confirmation())
  45. return 0; // not allowed to overwrite
  46. }
  47. if (unlink(dest) < 0) {
  48. bb_perror_msg("cannot remove '%s'", dest);
  49. return -1; // error
  50. }
  51. return 1; // ok (to try again)
  52. }
  53. /* Return:
  54. * -1 error, copy not made
  55. * 0 copy is made or user answered "no" in interactive mode
  56. * (failures to preserve mode/owner/times are not reported in exit code)
  57. */
  58. int copy_file(const char *source, const char *dest, int flags)
  59. {
  60. /* This is a recursive function, try to minimize stack usage */
  61. /* NB: each struct stat is ~100 bytes */
  62. struct stat source_stat;
  63. struct stat dest_stat;
  64. signed char retval = 0;
  65. signed char dest_exists = 0;
  66. signed char ovr;
  67. #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
  68. if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
  69. // This may be a dangling symlink.
  70. // Making [sym]links to dangling symlinks works, so...
  71. if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
  72. goto make_links;
  73. bb_perror_msg("cannot stat '%s'", source);
  74. return -1;
  75. }
  76. if (lstat(dest, &dest_stat) < 0) {
  77. if (errno != ENOENT) {
  78. bb_perror_msg("cannot stat '%s'", dest);
  79. return -1;
  80. }
  81. } else {
  82. if (source_stat.st_dev == dest_stat.st_dev
  83. && source_stat.st_ino == dest_stat.st_ino
  84. ) {
  85. bb_error_msg("'%s' and '%s' are the same file", source, dest);
  86. return -1;
  87. }
  88. dest_exists = 1;
  89. }
  90. #if ENABLE_SELINUX
  91. if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
  92. security_context_t con;
  93. if (lgetfilecon(source, &con) >= 0) {
  94. if (setfscreatecon(con) < 0) {
  95. bb_perror_msg("cannot set setfscreatecon %s", con);
  96. freecon(con);
  97. return -1;
  98. }
  99. } else if (errno == ENOTSUP || errno == ENODATA) {
  100. setfscreatecon_or_die(NULL);
  101. } else {
  102. bb_perror_msg("cannot lgetfilecon %s", source);
  103. return -1;
  104. }
  105. }
  106. #endif
  107. if (S_ISDIR(source_stat.st_mode)) {
  108. DIR *dp;
  109. const char *tp;
  110. struct dirent *d;
  111. mode_t saved_umask = 0;
  112. if (!(flags & FILEUTILS_RECUR)) {
  113. bb_error_msg("omitting directory '%s'", source);
  114. return -1;
  115. }
  116. /* Did we ever create source ourself before? */
  117. tp = is_in_ino_dev_hashtable(&source_stat);
  118. if (tp) {
  119. /* We did! it's a recursion! man the lifeboats... */
  120. bb_error_msg("recursion detected, omitting directory '%s'",
  121. source);
  122. return -1;
  123. }
  124. /* Create DEST */
  125. if (dest_exists) {
  126. if (!S_ISDIR(dest_stat.st_mode)) {
  127. bb_error_msg("target '%s' is not a directory", dest);
  128. return -1;
  129. }
  130. /* race here: user can substitute a symlink between
  131. * this check and actual creation of files inside dest */
  132. } else {
  133. mode_t mode;
  134. saved_umask = umask(0);
  135. mode = source_stat.st_mode;
  136. if (!(flags & FILEUTILS_PRESERVE_STATUS))
  137. mode = source_stat.st_mode & ~saved_umask;
  138. /* Allow owner to access new dir (at least for now) */
  139. mode |= S_IRWXU;
  140. if (mkdir(dest, mode) < 0) {
  141. umask(saved_umask);
  142. bb_perror_msg("cannot create directory '%s'", dest);
  143. return -1;
  144. }
  145. umask(saved_umask);
  146. /* need stat info for add_to_ino_dev_hashtable */
  147. if (lstat(dest, &dest_stat) < 0) {
  148. bb_perror_msg("cannot stat '%s'", dest);
  149. return -1;
  150. }
  151. }
  152. /* remember (dev,inode) of each created dir.
  153. * NULL: name is not remembered */
  154. add_to_ino_dev_hashtable(&dest_stat, NULL);
  155. /* Recursively copy files in SOURCE */
  156. dp = opendir(source);
  157. if (dp == NULL) {
  158. retval = -1;
  159. goto preserve_mode_ugid_time;
  160. }
  161. while ((d = readdir(dp)) != NULL) {
  162. char *new_source, *new_dest;
  163. new_source = concat_subpath_file(source, d->d_name);
  164. if (new_source == NULL)
  165. continue;
  166. new_dest = concat_path_file(dest, d->d_name);
  167. if (copy_file(new_source, new_dest, flags) < 0)
  168. retval = -1;
  169. free(new_source);
  170. free(new_dest);
  171. }
  172. closedir(dp);
  173. if (!dest_exists
  174. && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
  175. ) {
  176. bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
  177. /* retval = -1; - WRONG! copy *WAS* made */
  178. }
  179. goto preserve_mode_ugid_time;
  180. }
  181. if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
  182. int (*lf)(const char *oldpath, const char *newpath);
  183. make_links:
  184. // Hmm... maybe
  185. // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
  186. // (but realpath returns NULL on dangling symlinks...)
  187. lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
  188. if (lf(source, dest) < 0) {
  189. ovr = ask_and_unlink(dest, flags);
  190. if (ovr <= 0)
  191. return ovr;
  192. if (lf(source, dest) < 0) {
  193. bb_perror_msg("cannot create link '%s'", dest);
  194. return -1;
  195. }
  196. }
  197. /* _Not_ jumping to preserve_mode_ugid_time:
  198. * hard/softlinks don't have those */
  199. return 0;
  200. }
  201. if (S_ISREG(source_stat.st_mode)
  202. /* DEREF uses stat, which never returns S_ISLNK() == true. */
  203. /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
  204. ) {
  205. int src_fd;
  206. int dst_fd;
  207. if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
  208. const char *link_target;
  209. link_target = is_in_ino_dev_hashtable(&source_stat);
  210. if (link_target) {
  211. if (link(link_target, dest) < 0) {
  212. ovr = ask_and_unlink(dest, flags);
  213. if (ovr <= 0)
  214. return ovr;
  215. if (link(link_target, dest) < 0) {
  216. bb_perror_msg("cannot create link '%s'", dest);
  217. return -1;
  218. }
  219. }
  220. return 0;
  221. }
  222. add_to_ino_dev_hashtable(&source_stat, dest);
  223. }
  224. src_fd = open_or_warn(source, O_RDONLY);
  225. if (src_fd < 0)
  226. return -1;
  227. /* POSIX way is a security problem versus symlink attacks,
  228. * we do it only for non-symlinks, and only for non-recursive,
  229. * non-interactive cp. NB: it is still racy
  230. * for "cp file /home/bad_user/file" case
  231. * (user can rm file and create a link to /etc/passwd) */
  232. if (DO_POSIX_CP
  233. || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
  234. && !S_ISLNK(dest_stat.st_mode))
  235. ) {
  236. dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
  237. } else /* safe way: */
  238. dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
  239. if (dst_fd == -1) {
  240. ovr = ask_and_unlink(dest, flags);
  241. if (ovr <= 0) {
  242. close(src_fd);
  243. return ovr;
  244. }
  245. /* It shouldn't exist. If it exists, do not open (symlink attack?) */
  246. dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
  247. if (dst_fd < 0) {
  248. close(src_fd);
  249. return -1;
  250. }
  251. }
  252. #if ENABLE_SELINUX
  253. if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
  254. || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
  255. && is_selinux_enabled() > 0
  256. ) {
  257. security_context_t con;
  258. if (getfscreatecon(&con) == -1) {
  259. bb_perror_msg("getfscreatecon");
  260. return -1;
  261. }
  262. if (con) {
  263. if (setfilecon(dest, con) == -1) {
  264. bb_perror_msg("setfilecon:%s,%s", dest, con);
  265. freecon(con);
  266. return -1;
  267. }
  268. freecon(con);
  269. }
  270. }
  271. #endif
  272. if (bb_copyfd_eof(src_fd, dst_fd) == -1)
  273. retval = -1;
  274. /* Ok, writing side I can understand... */
  275. if (close(dst_fd) < 0) {
  276. bb_perror_msg("cannot close '%s'", dest);
  277. retval = -1;
  278. }
  279. /* ...but read size is already checked by bb_copyfd_eof */
  280. close(src_fd);
  281. goto preserve_mode_ugid_time;
  282. }
  283. /* Source is a symlink or a special file */
  284. /* We are lazy here, a bit lax with races... */
  285. if (dest_exists) {
  286. errno = EEXIST;
  287. ovr = ask_and_unlink(dest, flags);
  288. if (ovr <= 0)
  289. return ovr;
  290. }
  291. if (S_ISLNK(source_stat.st_mode)) {
  292. char *lpath = xmalloc_readlink_or_warn(source);
  293. if (lpath) {
  294. int r = symlink(lpath, dest);
  295. free(lpath);
  296. if (r < 0) {
  297. bb_perror_msg("cannot create symlink '%s'", dest);
  298. return -1;
  299. }
  300. if (flags & FILEUTILS_PRESERVE_STATUS)
  301. if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
  302. bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
  303. }
  304. /* _Not_ jumping to preserve_mode_ugid_time:
  305. * symlinks don't have those */
  306. return 0;
  307. }
  308. if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
  309. || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
  310. ) {
  311. if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
  312. bb_perror_msg("cannot create '%s'", dest);
  313. return -1;
  314. }
  315. } else {
  316. bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
  317. return -1;
  318. }
  319. preserve_mode_ugid_time:
  320. if (flags & FILEUTILS_PRESERVE_STATUS
  321. /* Cannot happen: */
  322. /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
  323. ) {
  324. struct utimbuf times;
  325. times.actime = source_stat.st_atime;
  326. times.modtime = source_stat.st_mtime;
  327. /* BTW, utimes sets usec-precision time - just FYI */
  328. if (utime(dest, &times) < 0)
  329. bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
  330. if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
  331. source_stat.st_mode &= ~(S_ISUID | S_ISGID);
  332. bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
  333. }
  334. if (chmod(dest, source_stat.st_mode) < 0)
  335. bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
  336. }
  337. return retval;
  338. }