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