]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-user-dbus.c
polkit: simplify bus_verify_polkit_async() + drop auth-by-cap dbus feature
[thirdparty/systemd.git] / src / login / logind-user-dbus.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4
5 #include "alloc-util.h"
6 #include "bus-get-properties.h"
7 #include "bus-polkit.h"
8 #include "bus-util.h"
9 #include "format-util.h"
10 #include "logind-dbus.h"
11 #include "logind-session-dbus.h"
12 #include "logind-user-dbus.h"
13 #include "logind-user.h"
14 #include "logind.h"
15 #include "missing_capability.h"
16 #include "signal-util.h"
17 #include "strv.h"
18 #include "user-util.h"
19
20 static int property_get_uid(
21 sd_bus *bus,
22 const char *path,
23 const char *interface,
24 const char *property,
25 sd_bus_message *reply,
26 void *userdata,
27 sd_bus_error *error) {
28
29 User *u = ASSERT_PTR(userdata);
30
31 assert(bus);
32 assert(reply);
33
34 return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->uid);
35 }
36
37 static int property_get_gid(
38 sd_bus *bus,
39 const char *path,
40 const char *interface,
41 const char *property,
42 sd_bus_message *reply,
43 void *userdata,
44 sd_bus_error *error) {
45
46 User *u = ASSERT_PTR(userdata);
47
48 assert(bus);
49 assert(reply);
50
51 return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->gid);
52 }
53
54 static int property_get_name(
55 sd_bus *bus,
56 const char *path,
57 const char *interface,
58 const char *property,
59 sd_bus_message *reply,
60 void *userdata,
61 sd_bus_error *error) {
62
63 User *u = ASSERT_PTR(userdata);
64
65 assert(bus);
66 assert(reply);
67
68 return sd_bus_message_append(reply, "s", u->user_record->user_name);
69 }
70
71 static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", User, user_get_state, user_state_to_string);
72
73 static int property_get_display(
74 sd_bus *bus,
75 const char *path,
76 const char *interface,
77 const char *property,
78 sd_bus_message *reply,
79 void *userdata,
80 sd_bus_error *error) {
81
82 _cleanup_free_ char *p = NULL;
83 User *u = ASSERT_PTR(userdata);
84
85 assert(bus);
86 assert(reply);
87
88 p = u->display ? session_bus_path(u->display) : strdup("/");
89 if (!p)
90 return -ENOMEM;
91
92 return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p);
93 }
94
95 static int property_get_sessions(
96 sd_bus *bus,
97 const char *path,
98 const char *interface,
99 const char *property,
100 sd_bus_message *reply,
101 void *userdata,
102 sd_bus_error *error) {
103
104 User *u = ASSERT_PTR(userdata);
105 int r;
106
107 assert(bus);
108 assert(reply);
109
110 r = sd_bus_message_open_container(reply, 'a', "(so)");
111 if (r < 0)
112 return r;
113
114 LIST_FOREACH(sessions_by_user, session, u->sessions) {
115 _cleanup_free_ char *p = NULL;
116
117 p = session_bus_path(session);
118 if (!p)
119 return -ENOMEM;
120
121 r = sd_bus_message_append(reply, "(so)", session->id, p);
122 if (r < 0)
123 return r;
124
125 }
126
127 return sd_bus_message_close_container(reply);
128 }
129
130 static int property_get_idle_hint(
131 sd_bus *bus,
132 const char *path,
133 const char *interface,
134 const char *property,
135 sd_bus_message *reply,
136 void *userdata,
137 sd_bus_error *error) {
138
139 User *u = ASSERT_PTR(userdata);
140
141 assert(bus);
142 assert(reply);
143
144 return sd_bus_message_append(reply, "b", user_get_idle_hint(u, NULL) > 0);
145 }
146
147 static int property_get_idle_since_hint(
148 sd_bus *bus,
149 const char *path,
150 const char *interface,
151 const char *property,
152 sd_bus_message *reply,
153 void *userdata,
154 sd_bus_error *error) {
155
156 User *u = ASSERT_PTR(userdata);
157 dual_timestamp t = DUAL_TIMESTAMP_NULL;
158 uint64_t k;
159
160 assert(bus);
161 assert(reply);
162
163 (void) user_get_idle_hint(u, &t);
164 k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
165
166 return sd_bus_message_append(reply, "t", k);
167 }
168
169 static int property_get_linger(
170 sd_bus *bus,
171 const char *path,
172 const char *interface,
173 const char *property,
174 sd_bus_message *reply,
175 void *userdata,
176 sd_bus_error *error) {
177
178 User *u = ASSERT_PTR(userdata);
179 int r;
180
181 assert(bus);
182 assert(reply);
183
184 r = user_check_linger_file(u);
185
186 return sd_bus_message_append(reply, "b", r > 0);
187 }
188
189 int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
190 User *u = ASSERT_PTR(userdata);
191 int r;
192
193 assert(message);
194
195 r = bus_verify_polkit_async_full(
196 message,
197 "org.freedesktop.login1.manage",
198 /* details= */ NULL,
199 /* interactive= */ false,
200 u->user_record->uid,
201 &u->manager->polkit_registry,
202 error);
203 if (r < 0)
204 return r;
205 if (r == 0)
206 return 1; /* Will call us back */
207
208 r = user_stop(u, /* force = */ true);
209 if (r < 0)
210 return r;
211
212 return sd_bus_reply_method_return(message, NULL);
213 }
214
215 int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
216 User *u = ASSERT_PTR(userdata);
217 int32_t signo;
218 int r;
219
220 assert(message);
221
222 r = bus_verify_polkit_async_full(
223 message,
224 "org.freedesktop.login1.manage",
225 /* details= */ NULL,
226 /* interactive= */ false,
227 u->user_record->uid,
228 &u->manager->polkit_registry,
229 error);
230 if (r < 0)
231 return r;
232 if (r == 0)
233 return 1; /* Will call us back */
234
235 r = sd_bus_message_read(message, "i", &signo);
236 if (r < 0)
237 return r;
238
239 if (!SIGNAL_VALID(signo))
240 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
241
242 r = user_kill(u, signo);
243 if (r < 0)
244 return r;
245
246 return sd_bus_reply_method_return(message, NULL);
247 }
248
249 static int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
250 Manager *m = ASSERT_PTR(userdata);
251 uid_t uid;
252 User *user;
253 int r;
254
255 assert(bus);
256 assert(path);
257 assert(interface);
258 assert(found);
259
260 if (streq(path, "/org/freedesktop/login1/user/self")) {
261 sd_bus_message *message;
262
263 message = sd_bus_get_current_message(bus);
264
265 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
266 if (r == -ENXIO) {
267 sd_bus_error_free(error);
268 return 0;
269 }
270 if (r < 0)
271 return r;
272 } else {
273 const char *p;
274
275 p = startswith(path, "/org/freedesktop/login1/user/_");
276 if (!p)
277 return 0;
278
279 r = parse_uid(p, &uid);
280 if (r < 0)
281 return 0;
282
283 user = hashmap_get(m->users, UID_TO_PTR(uid));
284 if (!user)
285 return 0;
286 }
287
288 *found = user;
289 return 1;
290 }
291
292 char *user_bus_path(User *u) {
293 char *s;
294
295 assert(u);
296
297 if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->user_record->uid) < 0)
298 return NULL;
299
300 return s;
301 }
302
303 static int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
304 _cleanup_strv_free_ char **l = NULL;
305 sd_bus_message *message;
306 Manager *m = userdata;
307 User *user;
308 int r;
309
310 assert(bus);
311 assert(path);
312 assert(nodes);
313
314 HASHMAP_FOREACH(user, m->users) {
315 char *p;
316
317 p = user_bus_path(user);
318 if (!p)
319 return -ENOMEM;
320
321 r = strv_consume(&l, p);
322 if (r < 0)
323 return r;
324 }
325
326 message = sd_bus_get_current_message(bus);
327 if (message) {
328 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
329
330 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
331 if (r >= 0) {
332 uid_t uid;
333
334 r = sd_bus_creds_get_owner_uid(creds, &uid);
335 if (r >= 0) {
336 user = hashmap_get(m->users, UID_TO_PTR(uid));
337 if (user) {
338 r = strv_extend(&l, "/org/freedesktop/login1/user/self");
339 if (r < 0)
340 return r;
341 }
342 }
343 }
344 }
345
346 *nodes = TAKE_PTR(l);
347
348 return 1;
349 }
350
351 static const sd_bus_vtable user_vtable[] = {
352 SD_BUS_VTABLE_START(0),
353
354 SD_BUS_PROPERTY("UID", "u", property_get_uid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
355 SD_BUS_PROPERTY("GID", "u", property_get_gid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
356 SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST),
357 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
358 SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST),
359 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST),
360 SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST),
361 SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
362 SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
363 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, 0),
364 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
365 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
366 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
367 SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0),
368
369 SD_BUS_METHOD("Terminate", NULL, NULL, bus_user_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
370 SD_BUS_METHOD_WITH_ARGS("Kill",
371 SD_BUS_ARGS("i", signal_number),
372 SD_BUS_NO_RESULT,
373 bus_user_method_kill,
374 SD_BUS_VTABLE_UNPRIVILEGED),
375
376 SD_BUS_VTABLE_END
377 };
378
379 const BusObjectImplementation user_object = {
380 "/org/freedesktop/login1/user",
381 "org.freedesktop.login1.User",
382 .fallback_vtables = BUS_FALLBACK_VTABLES({user_vtable, user_object_find}),
383 .node_enumerator = user_node_enumerator,
384 };
385
386 int user_send_signal(User *u, bool new_user) {
387 _cleanup_free_ char *p = NULL;
388
389 assert(u);
390
391 p = user_bus_path(u);
392 if (!p)
393 return -ENOMEM;
394
395 return sd_bus_emit_signal(
396 u->manager->bus,
397 "/org/freedesktop/login1",
398 "org.freedesktop.login1.Manager",
399 new_user ? "UserNew" : "UserRemoved",
400 "uo", (uint32_t) u->user_record->uid, p);
401 }
402
403 int user_send_changed(User *u, const char *properties, ...) {
404 _cleanup_free_ char *p = NULL;
405 char **l;
406
407 assert(u);
408
409 if (!u->started)
410 return 0;
411
412 p = user_bus_path(u);
413 if (!p)
414 return -ENOMEM;
415
416 l = strv_from_stdarg_alloca(properties);
417
418 return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", l);
419 }