Debian Codegraph @

src/login/logind-shutdown.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-bus.h"
6 #include "sd-event.h"
7 #include "sd-varlink.h"
8
9 #include "bus-common-errors.h"
10 #include "bus-polkit.h"
11 #include "cgroup-util.h"
12 #include "format-util.h"
13 #include "fs-util.h"
14 #include "hashmap.h"
15 #include "log.h"
16 #include "login-util.h"
17 #include "logind.h"
18 #include "logind-dbus.h"
19 #include "logind-inhibit.h"
20 #include "logind-session.h"
21 #include "logind-shutdown.h"
22 #include "logind-user.h"
23 #include "pidref.h"
24 #include "process-util.h"
25 #include "user-record.h"
26
27 int manager_have_multiple_sessions(
28 Manager *m,
29 uid_t uid) {
30
31 Session *session;
32
33 assert(m);
34
35 /* Check for other users' sessions. Greeter sessions do not
36 * count, and non-login sessions do not count either. */
37 HASHMAP_FOREACH(session, m->sessions)
38 if (SESSION_CLASS_IS_INHIBITOR_LIKE(session->class) &&
39 session->user->user_record->uid != uid)
40 return true;
41
42 return false;
43 }
44
45 void log_shutdown_caller(const PidRef *caller, const char *method) {
46 _cleanup_free_ char *comm = NULL, *unit = NULL;
47
48 assert(method);
49
50 if (!pidref_is_set(caller)) {
51 return log_notice("%s requested from unknown client PID...", method);
52 }
53
54 (void) pidref_get_comm(caller, &comm);
55 (void) cg_pidref_get_unit(caller, &unit);
56
57 log_notice("%s requested from client PID " PID_FMT "%s%s%s%s%s%s...",
58 method, caller->pid,
59 comm ? " ('" : "", strempty(comm), comm ? "')" : "",
60 unit ? " (unit " : "", strempty(unit), unit ? ")" : "");
61 }
62
63 int manager_verify_shutdown_creds(
64 Manager *m,
65 sd_bus_message *message,
66 sd_varlink *link,
67 const HandleActionData *a,
68 uint64_t flags,
69 sd_bus_error *error) {
70
71 bool multiple_sessions, blocked, interactive;
72 _unused_ bool error_or_denial = false;
73 Inhibitor *offending = NULL;
74 uid_t uid;
75 int r;
76
77 assert(m);
78 assert(a);
79 assert(!!message != !!link); /* exactly one transport */
80 assert(!link || !error); /* varlink doesn't use sd_bus_error */
81
82 if (message) {
83 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
84
85 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
86 if (r < 0)
87 return r;
88
89 r = sd_bus_creds_get_euid(creds, &uid);
90 if (r < 0)
91 return r;
92 } else {
93 r = sd_varlink_get_peer_uid(link, &uid);
94 if (r < 0)
95 return r;
96 }
97
98 r = manager_have_multiple_sessions(m, uid);
99 if (r < 0)
100 return r;
101
102 multiple_sessions = r > 0;
103 blocked = manager_is_inhibited(m, a->inhibit_what, NULL, /* flags= */ 0, uid, &offending);
104 interactive = flags & SD_LOGIND_INTERACTIVE;
105
106 if (multiple_sessions) {
107 if (message)
108 r = bus_verify_polkit_async_full(
109 message,
110 a->polkit_action_multiple_sessions,
111 /* details= */ NULL,
112 /* good_user= */ UID_INVALID,
113 interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
114 &m->polkit_registry,
115 error);
116 else
117 r = varlink_verify_polkit_async_full(
118 link,
119 m->bus,
120 a->polkit_action_multiple_sessions,
121 /* details= */ NULL,
122 /* good_user= */ UID_INVALID,
123 interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
124 &m->polkit_registry);
125
126 if (r < 0) {
127 /* If we get -EBUSY, it means a polkit decision was made, but not for
128 * this action in particular. Assuming we are blocked on inhibitors,
129 * ignore that error and allow the decision to be revealed below. */
130 if (blocked && r == -EBUSY)
131 error_or_denial = true;
132 else
133 return r;
134 }
135 if (r == 0)
136 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
137 }
138
139 if (blocked) {
140 PolkitFlags polkit_flags = 0;
141
142 /* With a strong inhibitor, if the skip flag is not set, reject outright.
143 * With a weak inhibitor, if root is asking and the root flag is set, reject outright.
144 * All else, check polkit first. */
145 if (!FLAGS_SET(flags, SD_LOGIND_SKIP_INHIBITORS) &&
146 (offending->mode != INHIBIT_BLOCK_WEAK ||
147 (uid == 0 && FLAGS_SET(flags, SD_LOGIND_ROOT_CHECK_INHIBITORS)))) {
148 if (link)
149 return sd_varlink_errorbo(
150 link,
151 "io.systemd.Shutdown.BlockedByInhibitor",
152 SD_JSON_BUILD_PAIR_STRING("who", offending->who),
153 SD_JSON_BUILD_PAIR_STRING("why", offending->why));
154 if (error)
155 return sd_bus_error_set(error, BUS_ERROR_BLOCKED_BY_INHIBITOR_LOCK,
156 "Operation denied due to active block inhibitor");
157 return -EACCES;
158 }
159
160 /* We want to always ask here, even for root, to only allow bypassing if explicitly allowed
161 * by polkit, unless a weak blocker is used, in which case it will be authorized. */
162 if (offending->mode != INHIBIT_BLOCK_WEAK)
163 polkit_flags |= POLKIT_ALWAYS_QUERY;
164
165 if (interactive)
166 polkit_flags |= POLKIT_ALLOW_INTERACTIVE;
167
168 if (message)
169 r = bus_verify_polkit_async_full(
170 message,
171 a->polkit_action_ignore_inhibit,
172 /* details= */ NULL,
173 /* good_user= */ UID_INVALID,
174 polkit_flags,
175 &m->polkit_registry,
176 error);
177 else
178 r = varlink_verify_polkit_async_full(
179 link,
180 m->bus,
181 a->polkit_action_ignore_inhibit,
182 /* details= */ NULL,
183 /* good_user= */ UID_INVALID,
184 polkit_flags,
185 &m->polkit_registry);
186
187 if (r < 0)
188 return r;
189 if (r == 0)
190 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
191 }
192
193 if (!multiple_sessions && !blocked) {
194 if (message)
195 r = bus_verify_polkit_async_full(
196 message,
197 a->polkit_action,
198 /* details= */ NULL,
199 /* good_user= */ UID_INVALID,
200 interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
201 &m->polkit_registry,
202 error);
203 else
204 r = varlink_verify_polkit_async_full(
205 link,
206 m->bus,
207 a->polkit_action,
208 /* details= */ NULL,
209 /* good_user= */ UID_INVALID,
210 interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
211 &m->polkit_registry);
212
213 if (r < 0)
214 return r;
215 if (r == 0)
216 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
217 }
218
219 /* If error_or_denial was set above, it means that a polkit denial or
220 * error was deferred for a future call to bus_verify_polkit_async_full()
221 * to catch. In any case, it also means that the payload guarded by
222 * these polkit calls should never be executed, and hence we should
223 * never reach this point. */
224 assert(!error_or_denial);
225
226 return 0;
227 }
228
229 void manager_reset_scheduled_shutdown(Manager *m) {
230 assert(m);
231
232 m->scheduled_shutdown_timeout_source = sd_event_source_disable_unref(m->scheduled_shutdown_timeout_source);
233 m->wall_message_timeout_source = sd_event_source_disable_unref(m->wall_message_timeout_source);
234 m->nologin_timeout_source = sd_event_source_disable_unref(m->nologin_timeout_source);
235
236 m->scheduled_shutdown_action = _HANDLE_ACTION_INVALID;
237 m->scheduled_shutdown_timeout = USEC_INFINITY;
238 m->scheduled_shutdown_uid = UID_INVALID;
239 m->scheduled_shutdown_tty = mfree(m->scheduled_shutdown_tty);
240 m->shutdown_dry_run = false;
241
242 if (m->unlink_nologin) {
243 (void) unlink_or_warn("/run/nologin");
244 m->unlink_nologin = false;
245 }
246
247 (void) unlink(SHUTDOWN_SCHEDULE_FILE);
248
249 manager_send_changed(m, "ScheduledShutdown");
250 }