1 |
/* vi: set sw=4 ts=4: */ |
/* vi: set sw=4 ts=4: */ |
2 |
/* |
/* |
3 |
* Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> |
* Copyright (C) 2003 by Glenn McGrath |
4 |
|
* SELinux support: by Yuichi Nakamura <ynakam@hitachisoft.jp> |
5 |
* |
* |
6 |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
|
* |
|
|
* TODO: -d option, need a way of recursively making directories and changing |
|
|
* owner/group, will probably modify bb_make_directory(...) |
|
7 |
*/ |
*/ |
8 |
|
|
9 |
#include "busybox.h" |
#include "libbb.h" |
10 |
#include "libcoreutils/coreutils.h" |
#include "libcoreutils/coreutils.h" |
|
#include <libgen.h> |
|
|
#include <getopt.h> /* struct option */ |
|
11 |
|
|
12 |
#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
13 |
static const struct option install_long_options[] = { |
static const char install_longopts[] ALIGN1 = |
14 |
{ "directory", 0, NULL, 'd' }, |
"directory\0" No_argument "d" |
15 |
{ "preserve-timestamps", 0, NULL, 'p' }, |
"preserve-timestamps\0" No_argument "p" |
16 |
{ "strip", 0, NULL, 's' }, |
"strip\0" No_argument "s" |
17 |
{ "group", 0, NULL, 'g' }, |
"group\0" Required_argument "g" |
18 |
{ "mode", 0, NULL, 'm' }, |
"mode\0" Required_argument "m" |
19 |
{ "owner", 0, NULL, 'o' }, |
"owner\0" Required_argument "o" |
20 |
{ 0, 0, 0, 0 } |
/* autofs build insists of using -b --suffix=.orig */ |
21 |
}; |
/* TODO? (short option for --suffix is -S) */ |
22 |
|
#if ENABLE_SELINUX |
23 |
|
"context\0" Required_argument "Z" |
24 |
|
"preserve_context\0" No_argument "\xff" |
25 |
|
"preserve-context\0" No_argument "\xff" |
26 |
|
#endif |
27 |
|
; |
28 |
|
#endif |
29 |
|
|
30 |
|
|
31 |
|
#if ENABLE_SELINUX |
32 |
|
static void setdefaultfilecon(const char *path) |
33 |
|
{ |
34 |
|
struct stat s; |
35 |
|
security_context_t scontext = NULL; |
36 |
|
|
37 |
|
if (!is_selinux_enabled()) { |
38 |
|
return; |
39 |
|
} |
40 |
|
if (lstat(path, &s) != 0) { |
41 |
|
return; |
42 |
|
} |
43 |
|
|
44 |
|
if (matchpathcon(path, s.st_mode, &scontext) < 0) { |
45 |
|
goto out; |
46 |
|
} |
47 |
|
if (strcmp(scontext, "<<none>>") == 0) { |
48 |
|
goto out; |
49 |
|
} |
50 |
|
|
51 |
|
if (lsetfilecon(path, scontext) < 0) { |
52 |
|
if (errno != ENOTSUP) { |
53 |
|
bb_perror_msg("warning: failed to change context" |
54 |
|
" of %s to %s", path, scontext); |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
|
out: |
59 |
|
freecon(scontext); |
60 |
|
} |
61 |
|
|
62 |
#endif |
#endif |
63 |
|
|
64 |
|
int install_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
65 |
int install_main(int argc, char **argv) |
int install_main(int argc, char **argv) |
66 |
{ |
{ |
67 |
struct stat statbuf; |
struct stat statbuf; |
68 |
mode_t mode; |
mode_t mode; |
69 |
uid_t uid; |
uid_t uid; |
70 |
gid_t gid; |
gid_t gid; |
71 |
|
char *arg, *last; |
72 |
const char *gid_str; |
const char *gid_str; |
73 |
const char *uid_str; |
const char *uid_str; |
74 |
const char *mode_str; |
const char *mode_str; |
75 |
int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE; |
int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE; |
76 |
int ret = EXIT_SUCCESS, flags, i, isdir; |
int opts; |
77 |
|
int min_args = 1; |
78 |
|
int ret = EXIT_SUCCESS; |
79 |
|
int isdir = 0; |
80 |
|
#if ENABLE_SELINUX |
81 |
|
security_context_t scontext; |
82 |
|
bool use_default_selinux_context = 1; |
83 |
|
#endif |
84 |
enum { |
enum { |
85 |
OPT_CMD = 0x1, |
OPT_c = 1 << 0, |
86 |
OPT_DIRECTORY = 0x2, |
OPT_v = 1 << 1, |
87 |
OPT_PRESERVE_TIME = 0x4, |
OPT_b = 1 << 2, |
88 |
OPT_STRIP = 0x8, |
OPT_MKDIR_LEADING = 1 << 3, |
89 |
OPT_GROUP = 0x10, |
OPT_DIRECTORY = 1 << 4, |
90 |
OPT_MODE = 0x20, |
OPT_PRESERVE_TIME = 1 << 5, |
91 |
OPT_OWNER = 0x40, |
OPT_STRIP = 1 << 6, |
92 |
|
OPT_GROUP = 1 << 7, |
93 |
|
OPT_MODE = 1 << 8, |
94 |
|
OPT_OWNER = 1 << 9, |
95 |
|
#if ENABLE_SELINUX |
96 |
|
OPT_SET_SECURITY_CONTEXT = 1 << 10, |
97 |
|
OPT_PRESERVE_SECURITY_CONTEXT = 1 << 11, |
98 |
|
#endif |
99 |
}; |
}; |
100 |
|
|
101 |
#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
102 |
applet_long_options = install_long_options; |
applet_long_options = install_longopts; |
103 |
|
#endif |
104 |
|
opt_complementary = "s--d:d--s" USE_SELINUX(":Z--\xff:\xff--Z"); |
105 |
|
/* -c exists for backwards compatibility, it's needed */ |
106 |
|
/* -v is ignored ("print name of each created directory") */ |
107 |
|
/* -b is ignored ("make a backup of each existing destination file") */ |
108 |
|
opts = getopt32(argv, "cvb" "Ddpsg:m:o:" USE_SELINUX("Z:"), |
109 |
|
&gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext)); |
110 |
|
argc -= optind; |
111 |
|
argv += optind; |
112 |
|
|
113 |
|
#if ENABLE_SELINUX |
114 |
|
if (opts & (OPT_PRESERVE_SECURITY_CONTEXT|OPT_SET_SECURITY_CONTEXT)) { |
115 |
|
selinux_or_die(); |
116 |
|
use_default_selinux_context = 0; |
117 |
|
if (opts & OPT_PRESERVE_SECURITY_CONTEXT) { |
118 |
|
copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT; |
119 |
|
} |
120 |
|
if (opts & OPT_SET_SECURITY_CONTEXT) { |
121 |
|
setfscreatecon_or_die(scontext); |
122 |
|
copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT; |
123 |
|
} |
124 |
|
} |
125 |
#endif |
#endif |
|
opt_complementary = "?:s--d:d--s"; |
|
|
/* -c exists for backwards compatibility, its needed */ |
|
|
flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); |
|
126 |
|
|
127 |
/* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */ |
/* preserve access and modification time, this is GNU behaviour, |
128 |
if (flags & OPT_PRESERVE_TIME) { |
* BSD only preserves modification time */ |
129 |
|
if (opts & OPT_PRESERVE_TIME) { |
130 |
copy_flags |= FILEUTILS_PRESERVE_STATUS; |
copy_flags |= FILEUTILS_PRESERVE_STATUS; |
131 |
} |
} |
132 |
mode = 0666; |
mode = 0666; |
133 |
if (flags & OPT_MODE) bb_parse_mode(mode_str, &mode); |
if (opts & OPT_MODE) |
134 |
uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); |
bb_parse_mode(mode_str, &mode); |
135 |
gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); |
uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); |
136 |
if (flags & (OPT_OWNER|OPT_GROUP)) umask(0); |
gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); |
137 |
|
|
138 |
/* Create directories |
last = argv[argc - 1]; |
139 |
* don't use bb_make_directory() as it can't change uid or gid |
if (!(opts & OPT_DIRECTORY)) { |
140 |
* perhaps bb_make_directory() should be improved. |
argv[argc - 1] = NULL; |
141 |
*/ |
min_args++; |
142 |
if (flags & OPT_DIRECTORY) { |
|
143 |
for (argv += optind; *argv; argv++) { |
/* coreutils install resolves link in this case, don't use lstat */ |
144 |
char *old_argv_ptr = *argv + 1; |
isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode); |
145 |
char *argv_ptr; |
} |
146 |
do { |
|
147 |
argv_ptr = strchr(old_argv_ptr, '/'); |
if (argc < min_args) |
148 |
old_argv_ptr = argv_ptr; |
bb_show_usage(); |
149 |
if (argv_ptr) { |
|
150 |
*argv_ptr = '\0'; |
while ((arg = *argv++) != NULL) { |
151 |
old_argv_ptr++; |
char *dest = last; |
152 |
} |
if (opts & OPT_DIRECTORY) { |
153 |
if (mkdir(*argv, mode | 0111) == -1) { |
dest = arg; |
154 |
if (errno != EEXIST) { |
/* GNU coreutils 6.9 does not set uid:gid |
155 |
bb_perror_msg("cannot create %s", *argv); |
* on intermediate created directories |
156 |
ret = EXIT_FAILURE; |
* (only on last one) */ |
157 |
break; |
if (bb_make_directory(dest, 0755, FILEUTILS_RECUR)) { |
158 |
} |
ret = EXIT_FAILURE; |
159 |
} |
goto next; |
160 |
if ((flags & (OPT_OWNER|OPT_GROUP)) |
} |
161 |
&& lchown(*argv, uid, gid) == -1 |
} else { |
162 |
) { |
if (opts & OPT_MKDIR_LEADING) { |
163 |
bb_perror_msg("cannot change ownership of %s", *argv); |
char *ddir = xstrdup(dest); |
164 |
ret = EXIT_FAILURE; |
bb_make_directory(dirname(ddir), 0755, FILEUTILS_RECUR); |
165 |
break; |
/* errors are not checked. copy_file |
166 |
} |
* will fail if dir is not created. */ |
167 |
if (argv_ptr) { |
free(ddir); |
168 |
*argv_ptr = '/'; |
} |
169 |
} |
if (isdir) |
170 |
} while (old_argv_ptr); |
dest = concat_path_file(last, basename(arg)); |
171 |
} |
if (copy_file(arg, dest, copy_flags)) { |
172 |
return ret; |
/* copy is not made */ |
173 |
} |
ret = EXIT_FAILURE; |
174 |
|
goto next; |
175 |
isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode); |
} |
176 |
|
} |
|
for (i = optind; i < argc - 1; i++) { |
|
|
char *dest; |
|
|
|
|
|
dest = argv[argc - 1]; |
|
|
if (isdir) |
|
|
dest = concat_path_file(argv[argc - 1], basename(argv[i])); |
|
|
ret |= copy_file(argv[i], dest, copy_flags); |
|
177 |
|
|
178 |
/* Set the file mode */ |
/* Set the file mode */ |
179 |
if ((flags & OPT_MODE) && chmod(dest, mode) == -1) { |
if ((opts & OPT_MODE) && chmod(dest, mode) == -1) { |
180 |
bb_perror_msg("cannot change permissions of %s", dest); |
bb_perror_msg("can't change %s of %s", "permissions", dest); |
181 |
ret = EXIT_FAILURE; |
ret = EXIT_FAILURE; |
182 |
} |
} |
183 |
|
#if ENABLE_SELINUX |
184 |
|
if (use_default_selinux_context) |
185 |
|
setdefaultfilecon(dest); |
186 |
|
#endif |
187 |
/* Set the user and group id */ |
/* Set the user and group id */ |
188 |
if ((flags & (OPT_OWNER|OPT_GROUP)) |
if ((opts & (OPT_OWNER|OPT_GROUP)) |
189 |
&& lchown(dest, uid, gid) == -1 |
&& lchown(dest, uid, gid) == -1 |
190 |
) { |
) { |
191 |
bb_perror_msg("cannot change ownership of %s", dest); |
bb_perror_msg("can't change %s of %s", "ownership", dest); |
192 |
ret = EXIT_FAILURE; |
ret = EXIT_FAILURE; |
193 |
} |
} |
194 |
if (flags & OPT_STRIP) { |
if (opts & OPT_STRIP) { |
195 |
if (execlp("strip", "strip", dest, NULL) == -1) { |
char *args[3]; |
196 |
|
args[0] = (char*)"strip"; |
197 |
|
args[1] = dest; |
198 |
|
args[2] = NULL; |
199 |
|
if (spawn_and_wait(args)) { |
200 |
bb_perror_msg("strip"); |
bb_perror_msg("strip"); |
201 |
ret = EXIT_FAILURE; |
ret = EXIT_FAILURE; |
202 |
} |
} |
203 |
} |
} |
204 |
if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest); |
next: |
205 |
|
if (ENABLE_FEATURE_CLEAN_UP && isdir) |
206 |
|
free(dest); |
207 |
} |
} |
208 |
|
|
209 |
return ret; |
return ret; |