]>
Commit | Line | Data |
---|---|---|
a9f0f5e5 ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <stdint.h> | |
4 | #include <sys/mount.h> | |
5 | ||
07ee5adb LP |
6 | #include "sd-bus.h" |
7 | ||
8 | #include "bus-error.h" | |
a9f0f5e5 | 9 | #include "fs-util.h" |
ca78ad1d | 10 | #include "format-util.h" |
a9f0f5e5 | 11 | #include "label.h" |
5e332028 | 12 | #include "main-func.h" |
a9f0f5e5 | 13 | #include "mkdir.h" |
049af8ad | 14 | #include "mountpoint-util.h" |
a9f0f5e5 ZJS |
15 | #include "path-util.h" |
16 | #include "rm-rf.h" | |
81375d80 | 17 | #include "selinux-util.h" |
a9f0f5e5 ZJS |
18 | #include "smack-util.h" |
19 | #include "stdio-util.h" | |
20 | #include "string-util.h" | |
21 | #include "strv.h" | |
22 | #include "user-util.h" | |
23 | ||
07ee5adb LP |
24 | static int acquire_runtime_dir_size(uint64_t *ret) { |
25 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
92e31da1 | 26 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a9f0f5e5 ZJS |
27 | int r; |
28 | ||
07ee5adb LP |
29 | r = sd_bus_default_system(&bus); |
30 | if (r < 0) | |
31 | return log_error_errno(r, "Failed to connect to system bus: %m"); | |
a9f0f5e5 | 32 | |
07ee5adb | 33 | r = sd_bus_get_property_trivial(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "RuntimeDirectorySize", &error, 't', ret); |
a9f0f5e5 | 34 | if (r < 0) |
07ee5adb | 35 | return log_error_errno(r, "Failed to acquire runtime directory size: %s", bus_error_message(&error, r)); |
a9f0f5e5 | 36 | |
a9f0f5e5 ZJS |
37 | return 0; |
38 | } | |
39 | ||
07ee5adb LP |
40 | static int user_mkdir_runtime_path( |
41 | const char *runtime_path, | |
42 | uid_t uid, | |
43 | gid_t gid, | |
44 | uint64_t runtime_dir_size) { | |
45 | ||
a9f0f5e5 ZJS |
46 | int r; |
47 | ||
48 | assert(runtime_path); | |
49 | assert(path_is_absolute(runtime_path)); | |
50 | assert(uid_is_valid(uid)); | |
51 | assert(gid_is_valid(gid)); | |
52 | ||
53 | r = mkdir_safe_label("/run/user", 0755, 0, 0, MKDIR_WARN_MODE); | |
54 | if (r < 0) | |
55 | return log_error_errno(r, "Failed to create /run/user: %m"); | |
56 | ||
57 | if (path_is_mount_point(runtime_path, NULL, 0) >= 0) | |
58 | log_debug("%s is already a mount point", runtime_path); | |
59 | else { | |
60 | char options[sizeof("mode=0700,uid=,gid=,size=,smackfsroot=*") | |
61 | + DECIMAL_STR_MAX(uid_t) | |
62 | + DECIMAL_STR_MAX(gid_t) | |
07ee5adb | 63 | + DECIMAL_STR_MAX(uint64_t)]; |
a9f0f5e5 ZJS |
64 | |
65 | xsprintf(options, | |
07ee5adb | 66 | "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%" PRIu64 "%s", |
a9f0f5e5 ZJS |
67 | uid, gid, runtime_dir_size, |
68 | mac_smack_use() ? ",smackfsroot=*" : ""); | |
69 | ||
70 | (void) mkdir_label(runtime_path, 0700); | |
71 | ||
72 | r = mount("tmpfs", runtime_path, "tmpfs", MS_NODEV|MS_NOSUID, options); | |
73 | if (r < 0) { | |
74 | if (!IN_SET(errno, EPERM, EACCES)) { | |
75 | r = log_error_errno(errno, "Failed to mount per-user tmpfs directory %s: %m", runtime_path); | |
76 | goto fail; | |
77 | } | |
78 | ||
79 | log_debug_errno(errno, "Failed to mount per-user tmpfs directory %s.\n" | |
80 | "Assuming containerized execution, ignoring: %m", runtime_path); | |
81 | ||
82 | r = chmod_and_chown(runtime_path, 0700, uid, gid); | |
83 | if (r < 0) { | |
84 | log_error_errno(r, "Failed to change ownership and mode of \"%s\": %m", runtime_path); | |
85 | goto fail; | |
86 | } | |
87 | } | |
88 | ||
89 | r = label_fix(runtime_path, 0); | |
90 | if (r < 0) | |
91 | log_warning_errno(r, "Failed to fix label of \"%s\", ignoring: %m", runtime_path); | |
92 | } | |
93 | ||
94 | return 0; | |
95 | ||
96 | fail: | |
97 | /* Try to clean up, but ignore errors */ | |
98 | (void) rmdir(runtime_path); | |
99 | return r; | |
100 | } | |
101 | ||
102 | static int user_remove_runtime_path(const char *runtime_path) { | |
103 | int r; | |
104 | ||
105 | assert(runtime_path); | |
106 | assert(path_is_absolute(runtime_path)); | |
107 | ||
108 | r = rm_rf(runtime_path, 0); | |
109 | if (r < 0) | |
3a13442b | 110 | log_debug_errno(r, "Failed to remove runtime directory %s (before unmounting), ignoring: %m", runtime_path); |
a9f0f5e5 | 111 | |
3a13442b LP |
112 | /* Ignore cases where the directory isn't mounted, as that's quite possible, if we lacked the permissions to |
113 | * mount something */ | |
a9f0f5e5 ZJS |
114 | r = umount2(runtime_path, MNT_DETACH); |
115 | if (r < 0 && !IN_SET(errno, EINVAL, ENOENT)) | |
3a13442b | 116 | log_debug_errno(errno, "Failed to unmount user runtime directory %s, ignoring: %m", runtime_path); |
a9f0f5e5 ZJS |
117 | |
118 | r = rm_rf(runtime_path, REMOVE_ROOT); | |
3a13442b LP |
119 | if (r < 0 && r != -ENOENT) |
120 | return log_error_errno(r, "Failed to remove runtime directory %s (after unmounting): %m", runtime_path); | |
a9f0f5e5 | 121 | |
3a13442b | 122 | return 0; |
a9f0f5e5 ZJS |
123 | } |
124 | ||
86d18f3b YW |
125 | static int do_mount(const char *user) { |
126 | char runtime_path[sizeof("/run/user") + DECIMAL_STR_MAX(uid_t)]; | |
07ee5adb | 127 | uint64_t runtime_dir_size; |
86d18f3b YW |
128 | uid_t uid; |
129 | gid_t gid; | |
130 | int r; | |
131 | ||
fafff8f1 | 132 | r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0); |
86d18f3b YW |
133 | if (r < 0) |
134 | return log_error_errno(r, | |
135 | r == -ESRCH ? "No such user \"%s\"" : | |
136 | r == -ENOMSG ? "UID \"%s\" is invalid or has an invalid main group" | |
137 | : "Failed to look up user \"%s\": %m", | |
138 | user); | |
139 | ||
07ee5adb LP |
140 | r = acquire_runtime_dir_size(&runtime_dir_size); |
141 | if (r < 0) | |
142 | return r; | |
a9f0f5e5 | 143 | |
07ee5adb | 144 | xsprintf(runtime_path, "/run/user/" UID_FMT, uid); |
a9f0f5e5 ZJS |
145 | |
146 | log_debug("Will mount %s owned by "UID_FMT":"GID_FMT, runtime_path, uid, gid); | |
147 | return user_mkdir_runtime_path(runtime_path, uid, gid, runtime_dir_size); | |
148 | } | |
149 | ||
86d18f3b YW |
150 | static int do_umount(const char *user) { |
151 | char runtime_path[sizeof("/run/user") + DECIMAL_STR_MAX(uid_t)]; | |
152 | uid_t uid; | |
153 | int r; | |
154 | ||
155 | /* The user may be already removed. So, first try to parse the string by parse_uid(), | |
156 | * and if it fails, fallback to get_user_creds().*/ | |
157 | if (parse_uid(user, &uid) < 0) { | |
fafff8f1 | 158 | r = get_user_creds(&user, &uid, NULL, NULL, NULL, 0); |
86d18f3b YW |
159 | if (r < 0) |
160 | return log_error_errno(r, | |
161 | r == -ESRCH ? "No such user \"%s\"" : | |
162 | r == -ENOMSG ? "UID \"%s\" is invalid or has an invalid main group" | |
163 | : "Failed to look up user \"%s\": %m", | |
164 | user); | |
165 | } | |
166 | ||
167 | xsprintf(runtime_path, "/run/user/" UID_FMT, uid); | |
168 | ||
a9f0f5e5 ZJS |
169 | log_debug("Will remove %s", runtime_path); |
170 | return user_remove_runtime_path(runtime_path); | |
171 | } | |
172 | ||
cc639ee7 | 173 | static int run(int argc, char *argv[]) { |
a9f0f5e5 ZJS |
174 | int r; |
175 | ||
176 | log_parse_environment(); | |
177 | log_open(); | |
178 | ||
baaa35ad ZJS |
179 | if (argc != 3) |
180 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
181 | "This program takes two arguments."); | |
182 | if (!STR_IN_SET(argv[1], "start", "stop")) | |
183 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
184 | "First argument must be either \"start\" or \"stop\"."); | |
a9f0f5e5 | 185 | |
81375d80 | 186 | r = mac_selinux_init(); |
cc639ee7 ZJS |
187 | if (r < 0) |
188 | return log_error_errno(r, "Could not initialize labelling: %m\n"); | |
81375d80 | 189 | |
a9f0f5e5 ZJS |
190 | umask(0022); |
191 | ||
a9f0f5e5 | 192 | if (streq(argv[1], "start")) |
cc639ee7 ZJS |
193 | return do_mount(argv[2]); |
194 | if (streq(argv[1], "stop")) | |
195 | return do_umount(argv[2]); | |
196 | assert_not_reached("Unknown verb!"); | |
a9f0f5e5 | 197 | } |
cc639ee7 ZJS |
198 | |
199 | DEFINE_MAIN_FUNCTION(run); |