]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3f49d45a LP |
2 | |
3 | #include <errno.h> | |
4 | ||
b5efdb8a | 5 | #include "alloc-util.h" |
269e4d2d | 6 | #include "bus-polkit.h" |
cc377381 | 7 | #include "bus-util.h" |
f97b34a6 | 8 | #include "format-util.h" |
6ecda0fb LP |
9 | #include "logind-dbus.h" |
10 | #include "logind-session-dbus.h" | |
11 | #include "logind-user-dbus.h" | |
b1d4f8e1 LP |
12 | #include "logind-user.h" |
13 | #include "logind.h" | |
36dd5ffd | 14 | #include "missing_capability.h" |
6eb7c172 | 15 | #include "signal-util.h" |
b1d4f8e1 LP |
16 | #include "strv.h" |
17 | #include "user-util.h" | |
3f49d45a | 18 | |
22c902fa LP |
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 | ||
01adcd69 YW |
73 | static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", User, user_get_state, user_state_to_string); |
74 | ||
cc377381 LP |
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, | |
ebcf1f97 LP |
81 | void *userdata, |
82 | sd_bus_error *error) { | |
3f49d45a | 83 | |
cc377381 LP |
84 | _cleanup_free_ char *p = NULL; |
85 | User *u = userdata; | |
3f49d45a | 86 | |
cc377381 LP |
87 | assert(bus); |
88 | assert(reply); | |
89 | assert(u); | |
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 |
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, | |
ebcf1f97 LP |
104 | void *userdata, |
105 | sd_bus_error *error) { | |
cc377381 LP |
106 | |
107 | User *u = userdata; | |
3f49d45a | 108 | Session *session; |
cc377381 | 109 | int r; |
3f49d45a | 110 | |
cc377381 LP |
111 | assert(bus); |
112 | assert(reply); | |
3f49d45a LP |
113 | assert(u); |
114 | ||
cc377381 LP |
115 | r = sd_bus_message_open_container(reply, 'a', "(so)"); |
116 | if (r < 0) | |
117 | return r; | |
3f49d45a LP |
118 | |
119 | LIST_FOREACH(sessions_by_user, session, u->sessions) { | |
cc377381 | 120 | _cleanup_free_ char *p = NULL; |
3f49d45a LP |
121 | |
122 | p = session_bus_path(session); | |
123 | if (!p) | |
124 | return -ENOMEM; | |
125 | ||
cc377381 LP |
126 | r = sd_bus_message_append(reply, "(so)", session->id, p); |
127 | if (r < 0) | |
128 | return r; | |
3f49d45a | 129 | |
3f49d45a LP |
130 | } |
131 | ||
0f826101 | 132 | return sd_bus_message_close_container(reply); |
3f49d45a LP |
133 | } |
134 | ||
cc377381 LP |
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, | |
ebcf1f97 LP |
141 | void *userdata, |
142 | sd_bus_error *error) { | |
a185c5aa | 143 | |
cc377381 | 144 | User *u = userdata; |
77527da0 | 145 | |
cc377381 LP |
146 | assert(bus); |
147 | assert(reply); | |
148 | assert(u); | |
a185c5aa | 149 | |
cc377381 | 150 | return sd_bus_message_append(reply, "b", user_get_idle_hint(u, NULL) > 0); |
a185c5aa LP |
151 | } |
152 | ||
cc377381 LP |
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, | |
ebcf1f97 LP |
159 | void *userdata, |
160 | sd_bus_error *error) { | |
cc377381 LP |
161 | |
162 | User *u = userdata; | |
5cb14b37 | 163 | dual_timestamp t = DUAL_TIMESTAMP_NULL; |
a185c5aa LP |
164 | uint64_t k; |
165 | ||
cc377381 LP |
166 | assert(bus); |
167 | assert(reply); | |
a185c5aa LP |
168 | assert(u); |
169 | ||
75bbdf47 | 170 | (void) user_get_idle_hint(u, &t); |
a185c5aa LP |
171 | k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic; |
172 | ||
cc377381 | 173 | return sd_bus_message_append(reply, "t", k); |
a185c5aa LP |
174 | } |
175 | ||
3a9f7a30 LP |
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, | |
ebcf1f97 LP |
182 | void *userdata, |
183 | sd_bus_error *error) { | |
3a9f7a30 LP |
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 | ||
19070062 | 197 | int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 198 | User *u = userdata; |
3f49d45a LP |
199 | int r; |
200 | ||
cc377381 LP |
201 | assert(message); |
202 | assert(u); | |
3f49d45a | 203 | |
c529695e LP |
204 | r = bus_verify_polkit_async( |
205 | message, | |
206 | CAP_KILL, | |
207 | "org.freedesktop.login1.manage", | |
403ed0e5 | 208 | NULL, |
c529695e | 209 | false, |
22c902fa | 210 | u->user_record->uid, |
c529695e LP |
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 | ||
9bb69af4 | 218 | r = user_stop(u, true); |
3f49d45a | 219 | if (r < 0) |
ebcf1f97 | 220 | return r; |
3f49d45a | 221 | |
df2d202e | 222 | return sd_bus_reply_method_return(message, NULL); |
3f49d45a LP |
223 | } |
224 | ||
19070062 | 225 | int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
226 | User *u = userdata; |
227 | int32_t signo; | |
a185c5aa LP |
228 | int r; |
229 | ||
3f49d45a | 230 | assert(message); |
cc377381 | 231 | assert(u); |
3f49d45a | 232 | |
c529695e LP |
233 | r = bus_verify_polkit_async( |
234 | message, | |
235 | CAP_KILL, | |
236 | "org.freedesktop.login1.manage", | |
403ed0e5 | 237 | NULL, |
c529695e | 238 | false, |
22c902fa | 239 | u->user_record->uid, |
c529695e LP |
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 | ||
cc377381 LP |
247 | r = sd_bus_message_read(message, "i", &signo); |
248 | if (r < 0) | |
ebcf1f97 | 249 | return r; |
a185c5aa | 250 | |
6eb7c172 | 251 | if (!SIGNAL_VALID(signo)) |
ebcf1f97 | 252 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); |
a185c5aa | 253 | |
cc377381 LP |
254 | r = user_kill(u, signo); |
255 | if (r < 0) | |
ebcf1f97 | 256 | return r; |
a185c5aa | 257 | |
df2d202e | 258 | return sd_bus_reply_method_return(message, NULL); |
3f49d45a LP |
259 | } |
260 | ||
cc377381 LP |
261 | const sd_bus_vtable user_vtable[] = { |
262 | SD_BUS_VTABLE_START(0), | |
263 | ||
22c902fa LP |
264 | SD_BUS_PROPERTY("UID", "u", property_get_uid, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
265 | SD_BUS_PROPERTY("GID", "u", property_get_gid, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
266 | SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
556089dc LP |
267 | BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST), |
268 | SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST), | |
269 | SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST), | |
270 | SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST), | |
952d3260 | 271 | SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
cc377381 | 272 | SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0), |
91ab7b01 | 273 | SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, 0), |
cc377381 LP |
274 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
275 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
276 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3a9f7a30 | 277 | SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0), |
cc377381 | 278 | |
c529695e LP |
279 | SD_BUS_METHOD("Terminate", NULL, NULL, bus_user_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED), |
280 | SD_BUS_METHOD("Kill", "i", NULL, bus_user_method_kill, SD_BUS_VTABLE_UNPRIVILEGED), | |
cc377381 LP |
281 | |
282 | SD_BUS_VTABLE_END | |
283 | }; | |
3f49d45a | 284 | |
f00c3121 | 285 | int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
3f49d45a | 286 | Manager *m = userdata; |
309a29df | 287 | uid_t uid; |
cc377381 | 288 | User *user; |
3f49d45a LP |
289 | int r; |
290 | ||
cc377381 LP |
291 | assert(bus); |
292 | assert(path); | |
293 | assert(interface); | |
294 | assert(found); | |
295 | assert(m); | |
3f49d45a | 296 | |
927b1649 LP |
297 | if (streq(path, "/org/freedesktop/login1/user/self")) { |
298 | sd_bus_message *message; | |
3f49d45a | 299 | |
19befb2d | 300 | message = sd_bus_get_current_message(bus); |
927b1649 | 301 | |
e4d2984b | 302 | r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); |
77c45ce3 LP |
303 | if (r == -ENXIO) { |
304 | sd_bus_error_free(error); | |
305 | return 0; | |
306 | } | |
927b1649 | 307 | if (r < 0) |
5b12334d | 308 | return r; |
927b1649 | 309 | } else { |
927b1649 | 310 | const char *p; |
3f49d45a | 311 | |
927b1649 LP |
312 | p = startswith(path, "/org/freedesktop/login1/user/_"); |
313 | if (!p) | |
314 | return 0; | |
315 | ||
309a29df | 316 | r = parse_uid(p, &uid); |
2ac0ab59 LP |
317 | if (r < 0) |
318 | return 0; | |
309a29df | 319 | |
2ac0ab59 LP |
320 | user = hashmap_get(m->users, UID_TO_PTR(uid)); |
321 | if (!user) | |
322 | return 0; | |
323 | } | |
3f49d45a | 324 | |
cc377381 LP |
325 | *found = user; |
326 | return 1; | |
3f49d45a LP |
327 | } |
328 | ||
3f49d45a LP |
329 | char *user_bus_path(User *u) { |
330 | char *s; | |
331 | ||
332 | assert(u); | |
333 | ||
22c902fa | 334 | if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->user_record->uid) < 0) |
3f49d45a LP |
335 | return NULL; |
336 | ||
337 | return s; | |
338 | } | |
da119395 | 339 | |
f00c3121 | 340 | int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
cc377381 | 341 | _cleanup_strv_free_ char **l = NULL; |
ca56b0a6 | 342 | sd_bus_message *message; |
cc377381 LP |
343 | Manager *m = userdata; |
344 | User *user; | |
345 | Iterator i; | |
346 | int r; | |
da119395 | 347 | |
cc377381 LP |
348 | assert(bus); |
349 | assert(path); | |
350 | assert(nodes); | |
da119395 | 351 | |
cc377381 LP |
352 | HASHMAP_FOREACH(user, m->users, i) { |
353 | char *p; | |
da119395 | 354 | |
cc377381 LP |
355 | p = user_bus_path(user); |
356 | if (!p) | |
357 | return -ENOMEM; | |
da119395 | 358 | |
6e18964d ZJS |
359 | r = strv_consume(&l, p); |
360 | if (r < 0) | |
cc377381 | 361 | return r; |
cc377381 | 362 | } |
da119395 | 363 | |
ca56b0a6 DH |
364 | message = sd_bus_get_current_message(bus); |
365 | if (message) { | |
4afd3348 | 366 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
ca56b0a6 DH |
367 | |
368 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
369 | if (r >= 0) { | |
77c45ce3 LP |
370 | uid_t uid; |
371 | ||
ca56b0a6 DH |
372 | r = sd_bus_creds_get_owner_uid(creds, &uid); |
373 | if (r >= 0) { | |
374 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
375 | if (user) { | |
376 | r = strv_extend(&l, "/org/freedesktop/login1/user/self"); | |
377 | if (r < 0) | |
378 | return r; | |
379 | } | |
380 | } | |
381 | } | |
382 | } | |
b298e984 | 383 | |
1cc6c93a | 384 | *nodes = TAKE_PTR(l); |
da119395 | 385 | |
cc377381 LP |
386 | return 1; |
387 | } | |
388 | ||
389 | int 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 | |
cc377381 | 406 | int user_send_changed(User *u, const char *properties, ...) { |
ce0fc5f5 | 407 | _cleanup_free_ char *p = NULL; |
cc377381 | 408 | char **l; |
9418f147 LP |
409 | |
410 | assert(u); | |
411 | ||
ed18b08b LP |
412 | if (!u->started) |
413 | return 0; | |
414 | ||
9418f147 LP |
415 | p = user_bus_path(u); |
416 | if (!p) | |
417 | return -ENOMEM; | |
418 | ||
cc377381 | 419 | l = strv_from_stdarg_alloca(properties); |
9418f147 | 420 | |
cc377381 | 421 | return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", l); |
9418f147 | 422 | } |