1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
4 * SELinux support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 * TODO: -d option, need a way of recursively making directories and changing
9 * owner/group, will probably modify bb_make_directory(...)
13 #include "libcoreutils/coreutils.h"
15 #include <getopt.h> /* struct option */
17 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
18 static const struct option install_long_options[] = {
19 { "directory", 0, NULL, 'd' },
20 { "preserve-timestamps", 0, NULL, 'p' },
21 { "strip", 0, NULL, 's' },
22 { "group", 0, NULL, 'g' },
23 { "mode", 0, NULL, 'm' },
24 { "owner", 0, NULL, 'o' },
26 { "context", 1, NULL, 'Z' },
27 { "preserve_context", 0, NULL, 0xff },
28 { "preserve-context", 0, NULL, 0xff },
36 static bool use_default_selinux_context = 1;
38 static void setdefaultfilecon(const char *path) {
40 security_context_t scontext = NULL;
42 if (!is_selinux_enabled()) {
45 if (lstat(path, &s) != 0) {
49 if (matchpathcon(path, s.st_mode, &scontext) < 0) {
52 if (strcmp(scontext, "<<none>>") == 0) {
56 if (lsetfilecon(path, scontext) < 0) {
57 if (errno != ENOTSUP) {
58 bb_perror_msg("warning: failed to change context of %s to %s", path, scontext);
68 int install_main(int argc, char **argv);
69 int install_main(int argc, char **argv)
78 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
79 int ret = EXIT_SUCCESS, flags, i, isdir;
81 security_context_t scontext;
86 OPT_PRESERVE_TIME = 0x4,
92 OPT_SET_SECURITY_CONTEXT = 0x80,
93 OPT_PRESERVE_SECURITY_CONTEXT = 0x100,
97 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
98 applet_long_options = install_long_options;
100 opt_complementary = "?:s--d:d--s" USE_SELINUX(":Z--\xff:\xff--Z");
101 /* -c exists for backwards compatibility, it's needed */
103 flags = getopt32(argc, argv, "cdpsg:m:o:" USE_SELINUX("Z:"), &gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext));
106 if (flags & OPT_PRESERVE_SECURITY_CONTEXT) {
107 use_default_selinux_context = 0;
108 copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
111 if (flags & OPT_SET_SECURITY_CONTEXT) {
113 setfscreatecon_or_die(scontext);
114 use_default_selinux_context = 0;
115 copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
119 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
120 if (flags & OPT_PRESERVE_TIME) {
121 copy_flags |= FILEUTILS_PRESERVE_STATUS;
124 if (flags & OPT_MODE) bb_parse_mode(mode_str, &mode);
125 uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
126 gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
127 if (flags & (OPT_OWNER|OPT_GROUP)) umask(0);
129 /* Create directories
130 * don't use bb_make_directory() as it can't change uid or gid
131 * perhaps bb_make_directory() should be improved.
133 if (flags & OPT_DIRECTORY) {
134 for (argv += optind; *argv; argv++) {
135 char *old_argv_ptr = *argv + 1;
138 argv_ptr = strchr(old_argv_ptr, '/');
139 old_argv_ptr = argv_ptr;
144 if (mkdir(*argv, mode | 0111) == -1) {
145 if (errno != EEXIST) {
146 bb_perror_msg("cannot create %s", *argv);
151 if ((flags & (OPT_OWNER|OPT_GROUP))
152 && lchown(*argv, uid, gid) == -1
154 bb_perror_msg("cannot change ownership of %s", *argv);
161 } while (old_argv_ptr);
166 isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
168 for (i = optind; i < argc - 1; i++) {
171 dest = argv[argc - 1];
173 dest = concat_path_file(argv[argc - 1], basename(argv[i]));
174 ret |= copy_file(argv[i], dest, copy_flags);
176 /* Set the file mode */
177 if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
178 bb_perror_msg("cannot change permissions of %s", dest);
182 if (use_default_selinux_context)
183 setdefaultfilecon(dest);
185 /* Set the user and group id */
186 if ((flags & (OPT_OWNER|OPT_GROUP))
187 && lchown(dest, uid, gid) == -1
189 bb_perror_msg("cannot change ownership of %s", dest);
192 if (flags & OPT_STRIP) {
193 if (BB_EXECLP("strip", "strip", dest, NULL) == -1) {
194 bb_perror_msg("strip");
198 if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);