1 /* vi: set sw=4 ts=4: */
3 * Mini copy_file implementation for busybox
5 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 // POSIX: if exists and -i, ask (w/o -i assume yes).
15 // Then open w/o EXCL (yes, not unlink!).
16 // If open still fails and -f, try unlink, then try open again.
18 // If dest is a softlink, we overwrite softlink's destination!
19 // (or fail, if it points to dir/nonexistent location/etc).
20 // This is strange, but POSIX-correct.
21 // coreutils cp has --remove-destination to override this...
23 // NB: we have special code which still allows for "cp file /dev/node"
24 // to work POSIX-ly (the only realistic case where it makes sense)
26 #define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */
28 // errno must be set to relevant value ("why we cannot create dest?")
29 // for POSIX mode to give reasonable error message
30 static int ask_and_unlink(const char *dest, int flags)
34 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
35 // Either it exists, or the *path* doesnt exist
36 bb_perror_msg("cannot create '%s'", dest);
40 // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
41 // "cannot create" msg, we want unlink to be done (silently unless -i).
43 // TODO: maybe we should do it only if ctty is present?
44 if (flags & FILEUTILS_INTERACTIVE) {
45 // We would not do POSIX insanity. -i asks,
46 // then _unlinks_ the offender. Presto.
47 // (No "opening without O_EXCL", no "unlink only if -f")
48 // Or else we will end up having 3 open()s!
49 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
50 if (!bb_ask_confirmation())
51 return 0; // not allowed to overwrite
53 if (unlink(dest) < 0) {
54 #if ENABLE_FEATURE_VERBOSE_CP_MESSAGE
55 if (e == errno && e == ENOENT) {
56 /* e == ENOTDIR is similar: path has non-dir component,
57 * but in this case we don't even reach copy_file() */
58 bb_error_msg("cannot create '%s': Path does not exist", dest);
63 bb_perror_msg("cannot create '%s'", dest);
66 return 1; // ok (to try again)
70 * -1 error, copy not made
71 * 0 copy is made or user answered "no" in interactive mode
72 * (failures to preserve mode/owner/times are not reported in exit code)
74 int copy_file(const char *source, const char *dest, int flags)
76 /* This is a recursive function, try to minimize stack usage */
77 /* NB: each struct stat is ~100 bytes */
78 struct stat source_stat;
79 struct stat dest_stat;
80 signed char retval = 0;
81 signed char dest_exists = 0;
84 #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
86 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
87 // This may be a dangling symlink.
88 // Making [sym]links to dangling symlinks works, so...
89 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
91 bb_perror_msg("cannot stat '%s'", source);
95 if (lstat(dest, &dest_stat) < 0) {
96 if (errno != ENOENT) {
97 bb_perror_msg("cannot stat '%s'", dest);
101 if (source_stat.st_dev == dest_stat.st_dev
102 && source_stat.st_ino == dest_stat.st_ino
104 bb_error_msg("'%s' and '%s' are the same file", source, dest);
111 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
112 security_context_t con;
113 if (lgetfilecon(source, &con) >= 0) {
114 if (setfscreatecon(con) < 0) {
115 bb_perror_msg("cannot set setfscreatecon %s", con);
119 } else if (errno == ENOTSUP || errno == ENODATA) {
120 setfscreatecon_or_die(NULL);
122 bb_perror_msg("cannot lgetfilecon %s", source);
128 if (S_ISDIR(source_stat.st_mode)) {
132 mode_t saved_umask = 0;
134 if (!(flags & FILEUTILS_RECUR)) {
135 bb_error_msg("omitting directory '%s'", source);
139 /* Did we ever create source ourself before? */
140 tp = is_in_ino_dev_hashtable(&source_stat);
142 /* We did! it's a recursion! man the lifeboats... */
143 bb_error_msg("recursion detected, omitting directory '%s'",
150 if (!S_ISDIR(dest_stat.st_mode)) {
151 bb_error_msg("target '%s' is not a directory", dest);
154 /* race here: user can substitute a symlink between
155 * this check and actual creation of files inside dest */
158 saved_umask = umask(0);
160 mode = source_stat.st_mode;
161 if (!(flags & FILEUTILS_PRESERVE_STATUS))
162 mode = source_stat.st_mode & ~saved_umask;
163 /* Allow owner to access new dir (at least for now) */
165 if (mkdir(dest, mode) < 0) {
167 bb_perror_msg("cannot create directory '%s'", dest);
171 /* need stat info for add_to_ino_dev_hashtable */
172 if (lstat(dest, &dest_stat) < 0) {
173 bb_perror_msg("cannot stat '%s'", dest);
177 /* remember (dev,inode) of each created dir.
178 * NULL: name is not remembered */
179 add_to_ino_dev_hashtable(&dest_stat, NULL);
181 /* Recursively copy files in SOURCE */
182 dp = opendir(source);
185 goto preserve_mode_ugid_time;
188 while ((d = readdir(dp)) != NULL) {
189 char *new_source, *new_dest;
191 new_source = concat_subpath_file(source, d->d_name);
192 if (new_source == NULL)
194 new_dest = concat_path_file(dest, d->d_name);
195 if (copy_file(new_source, new_dest, flags) < 0)
203 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
205 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
206 /* retval = -1; - WRONG! copy *WAS* made */
208 goto preserve_mode_ugid_time;
211 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
212 int (*lf)(const char *oldpath, const char *newpath);
215 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
216 // (but realpath returns NULL on dangling symlinks...)
217 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
218 if (lf(source, dest) < 0) {
219 ovr = ask_and_unlink(dest, flags);
222 if (lf(source, dest) < 0) {
223 bb_perror_msg("cannot create link '%s'", dest);
227 /* _Not_ jumping to preserve_mode_ugid_time:
228 * hard/softlinks don't have those */
232 if (S_ISREG(source_stat.st_mode)
233 /* DEREF uses stat, which never returns S_ISLNK() == true. */
234 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
239 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
240 const char *link_target;
241 link_target = is_in_ino_dev_hashtable(&source_stat);
243 if (link(link_target, dest) < 0) {
244 ovr = ask_and_unlink(dest, flags);
247 if (link(link_target, dest) < 0) {
248 bb_perror_msg("cannot create link '%s'", dest);
254 add_to_ino_dev_hashtable(&source_stat, dest);
257 src_fd = open_or_warn(source, O_RDONLY);
261 /* POSIX way is a security problem versus symlink attacks,
262 * we do it only for non-symlinks, and only for non-recursive,
263 * non-interactive cp. NB: it is still racy
264 * for "cp file /home/bad_user/file" case
265 * (user can rm file and create a link to /etc/passwd) */
267 || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
268 && !S_ISLNK(dest_stat.st_mode))
270 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
271 } else /* safe way: */
272 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
274 ovr = ask_and_unlink(dest, flags);
279 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
280 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
288 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
289 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
290 && is_selinux_enabled() > 0
292 security_context_t con;
293 if (getfscreatecon(&con) == -1) {
294 bb_perror_msg("getfscreatecon");
298 if (setfilecon(dest, con) == -1) {
299 bb_perror_msg("setfilecon:%s,%s", dest, con);
307 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
309 /* Ok, writing side I can understand... */
310 if (close(dst_fd) < 0) {
311 bb_perror_msg("cannot close '%s'", dest);
314 /* ...but read size is already checked by bb_copyfd_eof */
316 goto preserve_mode_ugid_time;
319 /* Source is a symlink or a special file */
320 /* We are lazy here, a bit lax with races... */
323 ovr = ask_and_unlink(dest, flags);
327 if (S_ISLNK(source_stat.st_mode)) {
328 char *lpath = xmalloc_readlink_or_warn(source);
330 int r = symlink(lpath, dest);
333 bb_perror_msg("cannot create symlink '%s'", dest);
336 if (flags & FILEUTILS_PRESERVE_STATUS)
337 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
338 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
340 /* _Not_ jumping to preserve_mode_ugid_time:
341 * symlinks don't have those */
344 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
345 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
347 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
348 bb_perror_msg("cannot create '%s'", dest);
352 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
356 preserve_mode_ugid_time:
358 if (flags & FILEUTILS_PRESERVE_STATUS
360 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
362 struct utimbuf times;
364 times.actime = source_stat.st_atime;
365 times.modtime = source_stat.st_mtime;
366 /* BTW, utimes sets usec-precision time - just FYI */
367 if (utime(dest, ×) < 0)
368 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
369 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
370 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
371 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
373 if (chmod(dest, source_stat.st_mode) < 0)
374 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);