introduce and use setfscreatecon_or_die
[platform/upstream/busybox.git] / coreutils / install.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
4  * SELinux support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7  *
8  * TODO: -d option, need a way of recursively making directories and changing
9  *           owner/group, will probably modify bb_make_directory(...)
10  */
11
12 #include "busybox.h"
13 #include "libcoreutils/coreutils.h"
14 #include <libgen.h>
15 #include <getopt.h> /* struct option */
16
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' },
25 #if ENABLE_SELINUX
26         { "context",             1, NULL, 'Z' },
27         { "preserve_context",    0, NULL, 0xff },
28         { "preserve-context",    0, NULL, 0xff },
29 #endif
30         { 0, 0, 0, 0 }
31 };
32 #endif
33
34
35 #if ENABLE_SELINUX
36 static bool use_default_selinux_context = 1;
37
38 static void setdefaultfilecon(const char *path) {
39         struct stat s;
40         security_context_t scontext = NULL;
41
42         if (!is_selinux_enabled()) {
43                 return;
44         }       
45         if (lstat(path, &s) != 0) {
46                 return;
47         }
48
49         if (matchpathcon(path, s.st_mode, &scontext) < 0) {
50                 goto out;
51         }
52         if (strcmp(scontext, "<<none>>") == 0) {
53                 goto out;
54         }
55
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);
59                 }
60         }
61
62  out:
63         freecon(scontext);
64 }
65
66 #endif
67
68 int install_main(int argc, char **argv);
69 int install_main(int argc, char **argv)
70 {
71         struct stat statbuf;
72         mode_t mode;
73         uid_t uid;
74         gid_t gid;
75         const char *gid_str;
76         const char *uid_str;
77         const char *mode_str;
78         int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
79         int ret = EXIT_SUCCESS, flags, i, isdir;
80 #if ENABLE_SELINUX
81         security_context_t scontext;
82 #endif
83         enum {
84                 OPT_CMD           =  0x1,
85                 OPT_DIRECTORY     =  0x2,
86                 OPT_PRESERVE_TIME =  0x4,
87                 OPT_STRIP         =  0x8,
88                 OPT_GROUP         = 0x10,
89                 OPT_MODE          = 0x20,
90                 OPT_OWNER         = 0x40,
91 #if ENABLE_SELINUX
92                 OPT_SET_SECURITY_CONTEXT = 0x80,
93                 OPT_PRESERVE_SECURITY_CONTEXT = 0x100,
94 #endif
95         };
96
97 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
98         applet_long_options = install_long_options;
99 #endif
100         opt_complementary = "?:s--d:d--s" USE_SELINUX(":Z--\xff:\xff--Z");
101         /* -c exists for backwards compatibility, it's needed */
102
103         flags = getopt32(argc, argv, "cdpsg:m:o:" USE_SELINUX("Z:"), &gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext));
104         
105 #if ENABLE_SELINUX
106         if (flags & OPT_PRESERVE_SECURITY_CONTEXT) {
107                 use_default_selinux_context = 0;
108                 copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
109                 selinux_or_die();
110         }
111         if (flags & OPT_SET_SECURITY_CONTEXT) {
112                 selinux_or_die();
113                 setfscreatecon_or_die(scontext);
114                 use_default_selinux_context = 0;
115                 copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
116         }
117 #endif
118
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;
122         }
123         mode = 0666;
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);
128
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.
132          */
133         if (flags & OPT_DIRECTORY) {
134                 for (argv += optind; *argv; argv++) {
135                         char *old_argv_ptr = *argv + 1;
136                         char *argv_ptr;
137                         do {
138                                 argv_ptr = strchr(old_argv_ptr, '/');
139                                 old_argv_ptr = argv_ptr;
140                                 if (argv_ptr) {
141                                         *argv_ptr = '\0';
142                                         old_argv_ptr++;
143                                 }
144                                 if (mkdir(*argv, mode | 0111) == -1) {
145                                         if (errno != EEXIST) {
146                                                 bb_perror_msg("cannot create %s", *argv);
147                                                 ret = EXIT_FAILURE;
148                                                 break;
149                                         }
150                                 }
151                                 if ((flags & (OPT_OWNER|OPT_GROUP))
152                                  && lchown(*argv, uid, gid) == -1
153                                 ) {
154                                         bb_perror_msg("cannot change ownership of %s", *argv);
155                                         ret = EXIT_FAILURE;
156                                         break;
157                                 }
158                                 if (argv_ptr) {
159                                         *argv_ptr = '/';
160                                 }
161                         } while (old_argv_ptr);
162                 }
163                 return ret;
164         }
165
166         isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
167
168         for (i = optind; i < argc - 1; i++) {
169                 char *dest;
170
171                 dest = argv[argc - 1];
172                 if (isdir)
173                         dest = concat_path_file(argv[argc - 1], basename(argv[i]));
174                 ret |= copy_file(argv[i], dest, copy_flags);
175
176                 /* Set the file mode */
177                 if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
178                         bb_perror_msg("cannot change permissions of %s", dest);
179                         ret = EXIT_FAILURE;
180                 }
181 #if ENABLE_SELINUX
182                 if (use_default_selinux_context)
183                         setdefaultfilecon(dest);
184 #endif
185                 /* Set the user and group id */
186                 if ((flags & (OPT_OWNER|OPT_GROUP))
187                  && lchown(dest, uid, gid) == -1
188                 ) {
189                         bb_perror_msg("cannot change ownership of %s", dest);
190                         ret = EXIT_FAILURE;
191                 }
192                 if (flags & OPT_STRIP) {
193                         if (BB_EXECLP("strip", "strip", dest, NULL) == -1) {
194                                 bb_perror_msg("strip");
195                                 ret = EXIT_FAILURE;
196                         }
197                 }
198                 if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
199         }
200
201         return ret;
202 }