mv: make it NOEXEC
[oweals/busybox.git] / coreutils / cp.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini cp implementation for busybox
4  *
5  * Copyright (C) 2000 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 source tree.
9  */
10 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
11  *
12  * Size reduction.
13  */
14 //config:config CP
15 //config:       bool "cp (9.7 kb)"
16 //config:       default y
17 //config:       help
18 //config:       cp is used to copy files and directories.
19 //config:
20 //config:config FEATURE_CP_LONG_OPTIONS
21 //config:       bool "Enable long options"
22 //config:       default y
23 //config:       depends on CP && LONG_OPTS
24 //config:       help
25 //config:       Enable long options.
26 //config:       Also add support for --parents option.
27
28 //applet:IF_CP(APPLET_NOEXEC(cp, cp, BB_DIR_BIN, BB_SUID_DROP, cp))
29 /* NOEXEC despite cases when it can be a "runner" (cp -r LARGE_DIR NEW_DIR) */
30
31 //kbuild:lib-$(CONFIG_CP) += cp.o
32
33 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
34
35 //usage:#define cp_trivial_usage
36 //usage:       "[OPTIONS] SOURCE... DEST"
37 //usage:#define cp_full_usage "\n\n"
38 //usage:       "Copy SOURCE(s) to DEST\n"
39 //usage:     "\n        -a      Same as -dpR"
40 //usage:        IF_SELINUX(
41 //usage:     "\n        -c      Preserve security context"
42 //usage:        )
43 //usage:     "\n        -R,-r   Recurse"
44 //usage:     "\n        -d,-P   Preserve symlinks (default if -R)"
45 //usage:     "\n        -L      Follow all symlinks"
46 //usage:     "\n        -H      Follow symlinks on command line"
47 //usage:     "\n        -p      Preserve file attributes if possible"
48 //usage:     "\n        -f      Overwrite"
49 //usage:     "\n        -i      Prompt before overwrite"
50 //usage:     "\n        -l,-s   Create (sym)links"
51 //usage:     "\n        -u      Copy only newer files"
52
53 #include "libbb.h"
54 #include "libcoreutils/coreutils.h"
55
56 /* This is a NOEXEC applet. Be very careful! */
57
58 int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59 int cp_main(int argc, char **argv)
60 {
61         struct stat source_stat;
62         struct stat dest_stat;
63         const char *last;
64         const char *dest;
65         int s_flags;
66         int d_flags;
67         int flags;
68         int status;
69         enum {
70                 FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1,
71 #if ENABLE_FEATURE_CP_LONG_OPTIONS
72                 /*OPT_rmdest  = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */
73                 OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1),
74 #endif
75         };
76
77 #if ENABLE_FEATURE_CP_LONG_OPTIONS
78         flags = getopt32long(argv, "^"
79                 FILEUTILS_CP_OPTSTR
80                 "\0"
81                 // Need at least two arguments
82                 // Soft- and hardlinking doesn't mix
83                 // -P and -d are the same (-P is POSIX, -d is GNU)
84                 // -r and -R are the same
85                 // -R (and therefore -r) turns on -d (coreutils does this)
86                 // -a = -pdR
87                 "-2:l--s:s--l:Pd:rRd:Rd:apdR",
88                 "archive\0"        No_argument "a"
89                 "force\0"          No_argument "f"
90                 "interactive\0"    No_argument "i"
91                 "link\0"           No_argument "l"
92                 "dereference\0"    No_argument "L"
93                 "no-dereference\0" No_argument "P"
94                 "recursive\0"      No_argument "R"
95                 "symbolic-link\0"  No_argument "s"
96                 "verbose\0"        No_argument "v"
97                 "update\0"         No_argument "u"
98                 "remove-destination\0" No_argument "\xff"
99                 "parents\0"        No_argument "\xfe"
100         );
101 #else
102         flags = getopt32(argv, FILEUTILS_CP_OPTSTR);
103 #endif
104         /* Options of cp from GNU coreutils 6.10:
105          * -a, --archive
106          * -f, --force
107          * -i, --interactive
108          * -l, --link
109          * -L, --dereference
110          * -P, --no-dereference
111          * -R, -r, --recursive
112          * -s, --symbolic-link
113          * -v, --verbose
114          * -H   follow command-line symbolic links in SOURCE
115          * -d   same as --no-dereference --preserve=links
116          * -p   same as --preserve=mode,ownership,timestamps
117          * -c   same as --preserve=context
118          * -u, --update
119          *      copy only when the SOURCE file is newer than the destination
120          *      file or when the destination file is missing
121          * --remove-destination
122          *      remove each existing destination file before attempting to open
123          * --parents
124          *      use full source file name under DIRECTORY
125          * NOT SUPPORTED IN BBOX:
126          * --backup[=CONTROL]
127          *      make a backup of each existing destination file
128          * -b   like --backup but does not accept an argument
129          * --copy-contents
130          *      copy contents of special files when recursive
131          * --preserve[=ATTR_LIST]
132          *      preserve attributes (default: mode,ownership,timestamps),
133          *      if possible additional attributes: security context,links,all
134          * --no-preserve=ATTR_LIST
135          * --sparse=WHEN
136          *      control creation of sparse files
137          * --strip-trailing-slashes
138          *      remove any trailing slashes from each SOURCE argument
139          * -S, --suffix=SUFFIX
140          *      override the usual backup suffix
141          * -t, --target-directory=DIRECTORY
142          *      copy all SOURCE arguments into DIRECTORY
143          * -T, --no-target-directory
144          *      treat DEST as a normal file
145          * -x, --one-file-system
146          *      stay on this file system
147          * -Z, --context=CONTEXT
148          *      (SELinux) set SELinux security context of copy to CONTEXT
149          */
150         argc -= optind;
151         argv += optind;
152         /* Reverse this bit. If there is -d, bit is not set: */
153         flags ^= FILEUTILS_DEREFERENCE;
154         /* coreutils 6.9 compat:
155          * by default, "cp" derefs symlinks (creates regular dest files),
156          * but "cp -R" does not. We switch off deref if -r or -R (see above).
157          * However, "cp -RL" must still deref symlinks: */
158         if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
159                 flags |= FILEUTILS_DEREFERENCE;
160
161 #if ENABLE_SELINUX
162         if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
163                 selinux_or_die();
164         }
165 #endif
166
167         status = EXIT_SUCCESS;
168         last = argv[argc - 1];
169         /* If there are only two arguments and...  */
170         if (argc == 2) {
171                 s_flags = cp_mv_stat2(*argv, &source_stat,
172                                 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
173                 if (s_flags < 0)
174                         return EXIT_FAILURE;
175                 d_flags = cp_mv_stat(last, &dest_stat);
176                 if (d_flags < 0)
177                         return EXIT_FAILURE;
178
179 #if ENABLE_FEATURE_CP_LONG_OPTIONS
180                 //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
181                 //      flags, FILEUTILS_RMDEST, OPT_parents);
182                 if (flags & OPT_parents) {
183                         if (!(d_flags & 2)) {
184                                 bb_error_msg_and_die("with --parents, the destination must be a directory");
185                         }
186                 }
187                 if (flags & FILEUTILS_RMDEST) {
188                         flags |= FILEUTILS_FORCE;
189                 }
190 #endif
191
192                 /* ...if neither is a directory...  */
193                 if (!((s_flags | d_flags) & 2)
194                     /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
195                  || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
196                 ) {
197                         /* Do a simple copy */
198                         dest = last;
199                         goto DO_COPY; /* NB: argc==2 -> *++argv==last */
200                 }
201         }
202
203         while (1) {
204 #if ENABLE_FEATURE_CP_LONG_OPTIONS
205                 if (flags & OPT_parents) {
206                         char *dest_dup;
207                         char *dest_dir;
208                         dest = concat_path_file(last, *argv);
209                         dest_dup = xstrdup(dest);
210                         dest_dir = dirname(dest_dup);
211                         if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
212                                 return EXIT_FAILURE;
213                         }
214                         free(dest_dup);
215                         goto DO_COPY;
216                 }
217 #endif
218                 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
219  DO_COPY:
220                 if (copy_file(*argv, dest, flags) < 0) {
221                         status = EXIT_FAILURE;
222                 }
223                 if (*++argv == last) {
224                         /* possibly leaking dest... */
225                         break;
226                 }
227                 /* don't move up: dest may be == last and not malloced! */
228                 free((void*)dest);
229         }
230
231         /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
232         return status;
233 }