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