copy_file: comment out one condition which is always false.
[oweals/busybox.git] / libbb / copy_file.c
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
12 #include "libbb.h"
13
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...
22
23 #define DO_POSIX_CP 0  /* 1 - POSIX behavior, 0 - safe behavior */
24
25
26 static int ask_and_unlink(const char *dest, int flags)
27 {
28         // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
29         // "'file' exists" msg, we want unlink to be done (silently unless -i
30         // is also in effect).
31         // This prevents safe way from asking more questions than POSIX does.
32 #if DO_POSIX_CP
33         if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
34                 fprintf(stderr, "'%s' exists\n", dest);
35                 return -1;
36         }
37 #endif
38
39         // TODO: maybe we should do it only if ctty is present?
40         if (flags & FILEUTILS_INTERACTIVE) {
41                 // We would not do POSIX insanity. -i asks,
42                 // then _unlinks_ the offender. Presto.
43                 // (No opening without O_EXCL, no unlinks only if -f)
44                 // Or else we will end up having 3 open()s!
45                 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
46                 if (!bb_ask_confirmation())
47                         return 0; // not allowed to overwrite
48         }
49         if (unlink(dest) < 0) {
50                 bb_perror_msg("cannot remove '%s'", dest);
51                 return -1; // error
52         }
53         return 1; // ok (to try again)
54 }
55
56 /* Return:
57  * -1 error, copy not made
58  *  0 copy is made or user answered "no" in interactive mode
59  *    (failures to preserve mode/owner/times are not reported in exit code)
60  */
61 int copy_file(const char *source, const char *dest, int flags)
62 {
63         struct stat source_stat;
64         struct stat dest_stat;
65         int status = 0;
66         signed char dest_exists = 0;
67         signed char ovr;
68
69 #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
70
71         if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
72                 // This may be a dangling symlink.
73                 // Making [sym]links to dangling symlinks works, so...
74                 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
75                         goto make_links;
76                 bb_perror_msg("cannot stat '%s'", source);
77                 return -1;
78         }
79
80         if (lstat(dest, &dest_stat) < 0) {
81                 if (errno != ENOENT) {
82                         bb_perror_msg("cannot stat '%s'", dest);
83                         return -1;
84                 }
85         } else {
86                 if (source_stat.st_dev == dest_stat.st_dev
87                  && source_stat.st_ino == dest_stat.st_ino
88                 ) {
89                         bb_error_msg("'%s' and '%s' are the same file", source, dest);
90                         return -1;
91                 }
92                 dest_exists = 1;
93         }
94
95 #if ENABLE_SELINUX
96         if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
97                 security_context_t con;
98                 if (lgetfilecon(source, &con) >= 0) {
99                         if (setfscreatecon(con) < 0) {
100                                 bb_perror_msg("cannot set setfscreatecon %s", con);
101                                 freecon(con);
102                                 return -1;
103                         }
104                 } else if (errno == ENOTSUP || errno == ENODATA) {
105                         setfscreatecon_or_die(NULL);
106                 } else {
107                         bb_perror_msg("cannot lgetfilecon %s", source);
108                         return -1;
109                 }
110         }
111 #endif
112
113         if (S_ISDIR(source_stat.st_mode)) {
114                 DIR *dp;
115                 struct dirent *d;
116                 mode_t saved_umask = 0;
117
118                 if (!(flags & FILEUTILS_RECUR)) {
119                         bb_error_msg("omitting directory '%s'", source);
120                         return -1;
121                 }
122
123                 /* Create DEST.  */
124                 if (dest_exists) {
125                         if (!S_ISDIR(dest_stat.st_mode)) {
126                                 bb_error_msg("target '%s' is not a directory", dest);
127                                 return -1;
128                         }
129                 } else {
130                         mode_t mode;
131                         saved_umask = umask(0);
132
133                         mode = source_stat.st_mode;
134                         if (!(flags & FILEUTILS_PRESERVE_STATUS))
135                                 mode = source_stat.st_mode & ~saved_umask;
136                         mode |= S_IRWXU;
137
138                         if (mkdir(dest, mode) < 0) {
139                                 umask(saved_umask);
140                                 bb_perror_msg("cannot create directory '%s'", dest);
141                                 return -1;
142                         }
143
144                         umask(saved_umask);
145                 }
146
147                 /* Recursively copy files in SOURCE.  */
148                 dp = opendir(source);
149                 if (dp == NULL) {
150                         status = -1;
151                         goto preserve_status;
152                 }
153
154                 while ((d = readdir(dp)) != NULL) {
155                         char *new_source, *new_dest;
156
157                         new_source = concat_subpath_file(source, d->d_name);
158                         if (new_source == NULL)
159                                 continue;
160                         new_dest = concat_path_file(dest, d->d_name);
161                         if (copy_file(new_source, new_dest, flags) < 0)
162                                 status = -1;
163                         free(new_source);
164                         free(new_dest);
165                 }
166                 closedir(dp);
167
168                 if (!dest_exists
169                  && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
170                 ) {
171                         bb_perror_msg("cannot change permissions of '%s'", dest);
172                         status = -1;
173                 }
174
175         } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
176                 int (*lf)(const char *oldpath, const char *newpath);
177  make_links:
178                 // Hmm... maybe
179                 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
180                 // (but realpath returns NULL on dangling symlinks...)
181                 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
182                 if (lf(source, dest) < 0) {
183                         ovr = ask_and_unlink(dest, flags);
184                         if (ovr <= 0)
185                                 return ovr;
186                         if (lf(source, dest) < 0) {
187                                 bb_perror_msg("cannot create link '%s'", dest);
188                                 return -1;
189                         }
190                 }
191                 return 0;
192
193         } else if (S_ISREG(source_stat.st_mode)
194          /* Huh? DEREF uses stat, which never returns links! */
195          /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
196         ) {
197                 int src_fd;
198                 int dst_fd;
199                 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
200                         char *link_name;
201
202                         if (!FLAGS_DEREF) {
203                                 link_name = is_in_ino_dev_hashtable(&source_stat);
204                                 if (link_name) {
205                                         if (link(link_name, dest) < 0) {
206                                                 ovr = ask_and_unlink(dest, flags);
207                                                 if (ovr <= 0)
208                                                         return ovr;
209                                                 if (link(link_name, dest) < 0) {
210                                                         bb_perror_msg("cannot create link '%s'", dest);
211                                                         return -1;
212                                                 }
213                                         }
214                                         return 0;
215                                 }
216                         }
217                         add_to_ino_dev_hashtable(&source_stat, dest);
218                 }
219
220                 src_fd = open(source, O_RDONLY);
221                 if (src_fd == -1) {
222                         bb_perror_msg("cannot open '%s'", source);
223                         return -1;
224                 }
225
226 #if DO_POSIX_CP  /* POSIX way (a security problem versus symlink attacks!): */
227                 dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
228                                 ? O_WRONLY|O_CREAT|O_EXCL
229                                 : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
230 #else  /* safe way: */
231                 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
232 #endif
233                 if (dst_fd == -1) {
234                         ovr = ask_and_unlink(dest, flags);
235                         if (ovr <= 0) {
236                                 close(src_fd);
237                                 return ovr;
238                         }
239                         /* It shouldn't exist. If it exists, do not open (symlink attack?) */
240                         dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
241                         if (dst_fd == -1) {
242                                 bb_perror_msg("cannot open '%s'", dest);
243                                 close(src_fd);
244                                 return -1;
245                         }
246                 }
247
248 #if ENABLE_SELINUX
249                 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
250                     || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
251                  && is_selinux_enabled() > 0
252                 ) {
253                         security_context_t con;  
254                         if (getfscreatecon(&con) == -1) {
255                                 bb_perror_msg("getfscreatecon");
256                                 return -1;
257                         }                               
258                         if (con) {
259                                 if(setfilecon(dest, con) == -1) {
260                                         bb_perror_msg("setfilecon:%s,%s", dest, con);
261                                         freecon(con);
262                                         return -1;
263                                 }
264                                 freecon(con);
265                         }
266                 }
267 #endif
268                 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
269                         status = -1;
270                 if (close(dst_fd) < 0) {
271                         bb_perror_msg("cannot close '%s'", dest);
272                         status = -1;
273                 }
274                 if (close(src_fd) < 0) {
275                         bb_perror_msg("cannot close '%s'", source);
276                         status = -1;
277                 }
278
279         } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
280          || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
281          || S_ISLNK(source_stat.st_mode)
282         ) {
283                 // We are lazy here, a bit lax with races...
284                 if (dest_exists) {
285                         ovr = ask_and_unlink(dest, flags);
286                         if (ovr <= 0)
287                                 return ovr;
288                 }
289                 if (S_ISFIFO(source_stat.st_mode)) {
290                         if (mkfifo(dest, source_stat.st_mode) < 0) {
291                                 bb_perror_msg("cannot create fifo '%s'", dest);
292                                 return -1;
293                         }
294                 } else if (S_ISLNK(source_stat.st_mode)) {
295                         char *lpath;
296
297                         lpath = xmalloc_readlink_or_warn(source);
298                         if (lpath && symlink(lpath, dest) < 0) {
299                                 bb_perror_msg("cannot create symlink '%s'", dest);
300                                 free(lpath);
301                                 return -1;
302                         }
303                         free(lpath);
304
305                         if (flags & FILEUTILS_PRESERVE_STATUS)
306                                 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
307                                         bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
308
309                         return 0;
310
311                 } else {
312                         if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
313                                 bb_perror_msg("cannot create '%s'", dest);
314                                 return -1;
315                         }
316                 }
317         } else {
318                 bb_error_msg("internal error: unrecognized file type");
319                 return -1;
320         }
321
322  preserve_status:
323
324         if (flags & FILEUTILS_PRESERVE_STATUS
325         /* Cannot happen: */
326         /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
327         ) {
328                 struct utimbuf times;
329
330                 times.actime = source_stat.st_atime;
331                 times.modtime = source_stat.st_mtime;
332                 if (utime(dest, &times) < 0)
333                         bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
334                 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
335                         source_stat.st_mode &= ~(S_ISUID | S_ISGID);
336                         bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
337                 }
338                 if (chmod(dest, source_stat.st_mode) < 0)
339                         bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
340         }
341
342         return status;
343 }