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