Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 3 | * Mini copy_file implementation for busybox |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 4 | * |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 6 | * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 7 | * |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | * |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 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. |
| 17 | // Result: a mess: |
| 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... |
Denis Vlasenko | fa05074 | 2007-09-11 10:39:13 +0000 | [diff] [blame] | 22 | // |
| 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) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 25 | |
| 26 | #define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */ |
| 27 | |
Denis Vlasenko | 3d82962 | 2007-08-24 14:23:57 +0000 | [diff] [blame] | 28 | // errno must be set to relevant value ("why we cannot create dest?") |
| 29 | // for POSIX mode to give reasonable error message |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 30 | static int ask_and_unlink(const char *dest, int flags) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 31 | { |
Denis Vlasenko | d5fe880 | 2008-02-13 16:52:00 +0000 | [diff] [blame] | 32 | int e = errno; |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 33 | #if DO_POSIX_CP |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 34 | if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) { |
Denis Vlasenko | 3d82962 | 2007-08-24 14:23:57 +0000 | [diff] [blame] | 35 | // Either it exists, or the *path* doesnt exist |
| 36 | bb_perror_msg("cannot create '%s'", dest); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 37 | return -1; |
| 38 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 39 | #endif |
Denis Vlasenko | 3d82962 | 2007-08-24 14:23:57 +0000 | [diff] [blame] | 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). |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 42 | |
| 43 | // TODO: maybe we should do it only if ctty is present? |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 44 | if (flags & FILEUTILS_INTERACTIVE) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 45 | // We would not do POSIX insanity. -i asks, |
| 46 | // then _unlinks_ the offender. Presto. |
Denis Vlasenko | 3d82962 | 2007-08-24 14:23:57 +0000 | [diff] [blame] | 47 | // (No "opening without O_EXCL", no "unlink only if -f") |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 48 | // Or else we will end up having 3 open()s! |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 49 | fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest); |
| 50 | if (!bb_ask_confirmation()) |
| 51 | return 0; // not allowed to overwrite |
| 52 | } |
| 53 | if (unlink(dest) < 0) { |
Denis Vlasenko | d5fe880 | 2008-02-13 16:52:00 +0000 | [diff] [blame] | 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); |
| 59 | return -1; // error |
| 60 | } |
| 61 | #endif |
| 62 | errno = e; |
| 63 | bb_perror_msg("cannot create '%s'", dest); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 64 | return -1; // error |
| 65 | } |
| 66 | return 1; // ok (to try again) |
| 67 | } |
| 68 | |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 69 | /* Return: |
| 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) |
| 73 | */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 74 | int copy_file(const char *source, const char *dest, int flags) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 75 | { |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 76 | /* This is a recursive function, try to minimize stack usage */ |
| 77 | /* NB: each struct stat is ~100 bytes */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 78 | struct stat source_stat; |
| 79 | struct stat dest_stat; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 80 | signed char retval = 0; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 81 | signed char dest_exists = 0; |
| 82 | signed char ovr; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 84 | #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE) |
| 85 | |
| 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)) |
| 90 | goto make_links; |
| 91 | bb_perror_msg("cannot stat '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 92 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 93 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 94 | |
Eric Andersen | 02b8dfc | 2002-09-16 10:23:38 +0000 | [diff] [blame] | 95 | if (lstat(dest, &dest_stat) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 96 | if (errno != ENOENT) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 97 | bb_perror_msg("cannot stat '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 98 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 99 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 100 | } else { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 101 | if (source_stat.st_dev == dest_stat.st_dev |
| 102 | && source_stat.st_ino == dest_stat.st_ino |
| 103 | ) { |
| 104 | bb_error_msg("'%s' and '%s' are the same file", source, dest); |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 105 | return -1; |
| 106 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 107 | dest_exists = 1; |
| 108 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 110 | #if ENABLE_SELINUX |
| 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); |
| 116 | freecon(con); |
| 117 | return -1; |
| 118 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 119 | } else if (errno == ENOTSUP || errno == ENODATA) { |
| 120 | setfscreatecon_or_die(NULL); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 121 | } else { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 122 | bb_perror_msg("cannot lgetfilecon %s", source); |
| 123 | return -1; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | #endif |
| 127 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 128 | if (S_ISDIR(source_stat.st_mode)) { |
| 129 | DIR *dp; |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 130 | const char *tp; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 131 | struct dirent *d; |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 132 | mode_t saved_umask = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 133 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 134 | if (!(flags & FILEUTILS_RECUR)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 135 | bb_error_msg("omitting directory '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 136 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 137 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 139 | /* Did we ever create source ourself before? */ |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 140 | tp = is_in_ino_dev_hashtable(&source_stat); |
| 141 | if (tp) { |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 142 | /* We did! it's a recursion! man the lifeboats... */ |
| 143 | bb_error_msg("recursion detected, omitting directory '%s'", |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 144 | source); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 145 | return -1; |
| 146 | } |
| 147 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 148 | /* Create DEST */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 149 | if (dest_exists) { |
| 150 | if (!S_ISDIR(dest_stat.st_mode)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 151 | bb_error_msg("target '%s' is not a directory", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 152 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 153 | } |
Denis Vlasenko | a9335ea | 2007-09-11 16:28:14 +0000 | [diff] [blame] | 154 | /* race here: user can substitute a symlink between |
| 155 | * this check and actual creation of files inside dest */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 156 | } else { |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 157 | mode_t mode; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 158 | saved_umask = umask(0); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 159 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 160 | mode = source_stat.st_mode; |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 161 | if (!(flags & FILEUTILS_PRESERVE_STATUS)) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 162 | mode = source_stat.st_mode & ~saved_umask; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 163 | /* Allow owner to access new dir (at least for now) */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 164 | mode |= S_IRWXU; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 165 | if (mkdir(dest, mode) < 0) { |
| 166 | umask(saved_umask); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 167 | bb_perror_msg("cannot create directory '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 168 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 169 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 170 | umask(saved_umask); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 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); |
| 174 | return -1; |
| 175 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 176 | } |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 177 | /* remember (dev,inode) of each created dir. |
| 178 | * NULL: name is not remembered */ |
| 179 | add_to_ino_dev_hashtable(&dest_stat, NULL); |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 181 | /* Recursively copy files in SOURCE */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 182 | dp = opendir(source); |
| 183 | if (dp == NULL) { |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 184 | retval = -1; |
| 185 | goto preserve_mode_ugid_time; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | while ((d = readdir(dp)) != NULL) { |
| 189 | char *new_source, *new_dest; |
| 190 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 191 | new_source = concat_subpath_file(source, d->d_name); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 192 | if (new_source == NULL) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 193 | continue; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 194 | new_dest = concat_path_file(dest, d->d_name); |
| 195 | if (copy_file(new_source, new_dest, flags) < 0) |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 196 | retval = -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 197 | free(new_source); |
| 198 | free(new_dest); |
| 199 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 200 | closedir(dp); |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 202 | if (!dest_exists |
| 203 | && chmod(dest, source_stat.st_mode & ~saved_umask) < 0 |
| 204 | ) { |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 205 | bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); |
| 206 | /* retval = -1; - WRONG! copy *WAS* made */ |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 207 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 208 | goto preserve_mode_ugid_time; |
| 209 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 210 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 211 | if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 212 | int (*lf)(const char *oldpath, const char *newpath); |
| 213 | make_links: |
| 214 | // Hmm... maybe |
| 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) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 219 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 220 | if (ovr <= 0) |
| 221 | return ovr; |
| 222 | if (lf(source, dest) < 0) { |
| 223 | bb_perror_msg("cannot create link '%s'", dest); |
| 224 | return -1; |
| 225 | } |
| 226 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 227 | /* _Not_ jumping to preserve_mode_ugid_time: |
| 228 | * hard/softlinks don't have those */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 229 | return 0; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 230 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 231 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 232 | if (S_ISREG(source_stat.st_mode) |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 233 | /* DEREF uses stat, which never returns S_ISLNK() == true. */ |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 234 | /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 235 | ) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 236 | int src_fd; |
| 237 | int dst_fd; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 238 | |
| 239 | if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) { |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 240 | const char *link_target; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 241 | link_target = is_in_ino_dev_hashtable(&source_stat); |
| 242 | if (link_target) { |
| 243 | if (link(link_target, dest) < 0) { |
| 244 | ovr = ask_and_unlink(dest, flags); |
| 245 | if (ovr <= 0) |
| 246 | return ovr; |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 247 | if (link(link_target, dest) < 0) { |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 248 | bb_perror_msg("cannot create link '%s'", dest); |
| 249 | return -1; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 250 | } |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 251 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 252 | return 0; |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 253 | } |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 254 | add_to_ino_dev_hashtable(&source_stat, dest); |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 255 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 257 | src_fd = open_or_warn(source, O_RDONLY); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 258 | if (src_fd < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 259 | return -1; |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 260 | |
Denis Vlasenko | fa05074 | 2007-09-11 10:39:13 +0000 | [diff] [blame] | 261 | /* POSIX way is a security problem versus symlink attacks, |
Denis Vlasenko | a9335ea | 2007-09-11 16:28:14 +0000 | [diff] [blame] | 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) */ |
Denis Vlasenko | fa05074 | 2007-09-11 10:39:13 +0000 | [diff] [blame] | 266 | if (DO_POSIX_CP |
| 267 | || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE)) |
Denis Vlasenko | a9335ea | 2007-09-11 16:28:14 +0000 | [diff] [blame] | 268 | && !S_ISLNK(dest_stat.st_mode)) |
Denis Vlasenko | fa05074 | 2007-09-11 10:39:13 +0000 | [diff] [blame] | 269 | ) { |
| 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); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 273 | if (dst_fd == -1) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 274 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 275 | if (ovr <= 0) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 276 | close(src_fd); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 277 | return ovr; |
| 278 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 279 | /* It shouldn't exist. If it exists, do not open (symlink attack?) */ |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 280 | dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); |
| 281 | if (dst_fd < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 282 | close(src_fd); |
| 283 | return -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 287 | #if ENABLE_SELINUX |
| 288 | if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) |
| 289 | || (flags & FILEUTILS_SET_SECURITY_CONTEXT)) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 290 | && is_selinux_enabled() > 0 |
| 291 | ) { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 292 | security_context_t con; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 293 | if (getfscreatecon(&con) == -1) { |
| 294 | bb_perror_msg("getfscreatecon"); |
| 295 | return -1; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 296 | } |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 297 | if (con) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 298 | if (setfilecon(dest, con) == -1) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 299 | bb_perror_msg("setfilecon:%s,%s", dest, con); |
| 300 | freecon(con); |
| 301 | return -1; |
| 302 | } |
| 303 | freecon(con); |
| 304 | } |
| 305 | } |
| 306 | #endif |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 307 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 308 | retval = -1; |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 309 | /* Ok, writing side I can understand... */ |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 310 | if (close(dst_fd) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 311 | bb_perror_msg("cannot close '%s'", dest); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 312 | retval = -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 313 | } |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 314 | /* ...but read size is already checked by bb_copyfd_eof */ |
| 315 | close(src_fd); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 316 | goto preserve_mode_ugid_time; |
| 317 | } |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 318 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 319 | /* Source is a symlink or a special file */ |
| 320 | /* We are lazy here, a bit lax with races... */ |
| 321 | if (dest_exists) { |
| 322 | errno = EEXIST; |
| 323 | ovr = ask_and_unlink(dest, flags); |
| 324 | if (ovr <= 0) |
| 325 | return ovr; |
| 326 | } |
| 327 | if (S_ISLNK(source_stat.st_mode)) { |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 328 | char *lpath = xmalloc_readlink_or_warn(source); |
| 329 | if (lpath) { |
| 330 | int r = symlink(lpath, dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 331 | free(lpath); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 332 | if (r < 0) { |
| 333 | bb_perror_msg("cannot create symlink '%s'", dest); |
| 334 | return -1; |
| 335 | } |
| 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); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 339 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 340 | /* _Not_ jumping to preserve_mode_ugid_time: |
| 341 | * symlinks don't have those */ |
| 342 | return 0; |
| 343 | } |
| 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) |
| 346 | ) { |
| 347 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { |
| 348 | bb_perror_msg("cannot create '%s'", dest); |
| 349 | return -1; |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 350 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 351 | } else { |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 352 | bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode); |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 353 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 356 | preserve_mode_ugid_time: |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 357 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 358 | if (flags & FILEUTILS_PRESERVE_STATUS |
| 359 | /* Cannot happen: */ |
| 360 | /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ |
| 361 | ) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 362 | struct utimbuf times; |
| 363 | |
| 364 | times.actime = source_stat.st_atime; |
| 365 | times.modtime = source_stat.st_mtime; |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 366 | /* BTW, utimes sets usec-precision time - just FYI */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 367 | if (utime(dest, ×) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 368 | bb_perror_msg("cannot preserve %s of '%s'", "times", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 369 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { |
| 370 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 371 | bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 372 | } |
| 373 | if (chmod(dest, source_stat.st_mode) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 374 | bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 377 | return retval; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 378 | } |