copy_file.c 11 KB

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