]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3f49d45a LP |
2 | |
3 | #include <errno.h> | |
a185c5aa | 4 | #include <string.h> |
3f49d45a | 5 | |
b5efdb8a | 6 | #include "alloc-util.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 | |
01adcd69 YW |
19 | static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", User, user_get_state, user_state_to_string); |
20 | ||
cc377381 LP |
21 | static int property_get_display( |
22 | sd_bus *bus, | |
23 | const char *path, | |
24 | const char *interface, | |
25 | const char *property, | |
26 | sd_bus_message *reply, | |
ebcf1f97 LP |
27 | void *userdata, |
28 | sd_bus_error *error) { | |
3f49d45a | 29 | |
cc377381 LP |
30 | _cleanup_free_ char *p = NULL; |
31 | User *u = userdata; | |
3f49d45a | 32 | |
cc377381 LP |
33 | assert(bus); |
34 | assert(reply); | |
35 | assert(u); | |
3f49d45a | 36 | |
cc377381 LP |
37 | p = u->display ? session_bus_path(u->display) : strdup("/"); |
38 | if (!p) | |
3f49d45a LP |
39 | return -ENOMEM; |
40 | ||
cc377381 | 41 | return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p); |
3f49d45a LP |
42 | } |
43 | ||
cc377381 LP |
44 | static int property_get_sessions( |
45 | sd_bus *bus, | |
46 | const char *path, | |
47 | const char *interface, | |
48 | const char *property, | |
49 | sd_bus_message *reply, | |
ebcf1f97 LP |
50 | void *userdata, |
51 | sd_bus_error *error) { | |
cc377381 LP |
52 | |
53 | User *u = userdata; | |
3f49d45a | 54 | Session *session; |
cc377381 | 55 | int r; |
3f49d45a | 56 | |
cc377381 LP |
57 | assert(bus); |
58 | assert(reply); | |
3f49d45a LP |
59 | assert(u); |
60 | ||
cc377381 LP |
61 | r = sd_bus_message_open_container(reply, 'a', "(so)"); |
62 | if (r < 0) | |
63 | return r; | |
3f49d45a LP |
64 | |
65 | LIST_FOREACH(sessions_by_user, session, u->sessions) { | |
cc377381 | 66 | _cleanup_free_ char *p = NULL; |
3f49d45a LP |
67 | |
68 | p = session_bus_path(session); | |
69 | if (!p) | |
70 | return -ENOMEM; | |
71 | ||
cc377381 LP |
72 | r = sd_bus_message_append(reply, "(so)", session->id, p); |
73 | if (r < 0) | |
74 | return r; | |
3f49d45a | 75 | |
3f49d45a LP |
76 | } |
77 | ||
0f826101 | 78 | return sd_bus_message_close_container(reply); |
3f49d45a LP |
79 | } |
80 | ||
cc377381 LP |
81 | static int property_get_idle_hint( |
82 | sd_bus *bus, | |
83 | const char *path, | |
84 | const char *interface, | |
85 | const char *property, | |
86 | sd_bus_message *reply, | |
ebcf1f97 LP |
87 | void *userdata, |
88 | sd_bus_error *error) { | |
a185c5aa | 89 | |
cc377381 | 90 | User *u = userdata; |
77527da0 | 91 | |
cc377381 LP |
92 | assert(bus); |
93 | assert(reply); | |
94 | assert(u); | |
a185c5aa | 95 | |
cc377381 | 96 | return sd_bus_message_append(reply, "b", user_get_idle_hint(u, NULL) > 0); |
a185c5aa LP |
97 | } |
98 | ||
cc377381 LP |
99 | static int property_get_idle_since_hint( |
100 | sd_bus *bus, | |
101 | const char *path, | |
102 | const char *interface, | |
103 | const char *property, | |
104 | sd_bus_message *reply, | |
ebcf1f97 LP |
105 | void *userdata, |
106 | sd_bus_error *error) { | |
cc377381 LP |
107 | |
108 | User *u = userdata; | |
5cb14b37 | 109 | dual_timestamp t = DUAL_TIMESTAMP_NULL; |
a185c5aa LP |
110 | uint64_t k; |
111 | ||
cc377381 LP |
112 | assert(bus); |
113 | assert(reply); | |
a185c5aa LP |
114 | assert(u); |
115 | ||
75bbdf47 | 116 | (void) user_get_idle_hint(u, &t); |
a185c5aa LP |
117 | k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic; |
118 | ||
cc377381 | 119 | return sd_bus_message_append(reply, "t", k); |
a185c5aa LP |
120 | } |
121 | ||
3a9f7a30 LP |
122 | static int property_get_linger( |
123 | sd_bus *bus, | |
124 | const char *path, | |
125 | const char *interface, | |
126 | const char *property, | |
127 | sd_bus_message *reply, | |
ebcf1f97 LP |
128 | void *userdata, |
129 | sd_bus_error *error) { | |
3a9f7a30 LP |
130 | |
131 | User *u = userdata; | |
132 | int r; | |
133 | ||
134 | assert(bus); | |
135 | assert(reply); | |
136 | assert(u); | |
137 | ||
138 | r = user_check_linger_file(u); | |
139 | ||
140 | return sd_bus_message_append(reply, "b", r > 0); | |
141 | } | |
142 | ||
19070062 | 143 | int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 144 | User *u = userdata; |
3f49d45a LP |
145 | int r; |
146 | ||
cc377381 LP |
147 | assert(message); |
148 | assert(u); | |
3f49d45a | 149 | |
c529695e LP |
150 | r = bus_verify_polkit_async( |
151 | message, | |
152 | CAP_KILL, | |
153 | "org.freedesktop.login1.manage", | |
403ed0e5 | 154 | NULL, |
c529695e LP |
155 | false, |
156 | u->uid, | |
157 | &u->manager->polkit_registry, | |
158 | error); | |
159 | if (r < 0) | |
160 | return r; | |
161 | if (r == 0) | |
162 | return 1; /* Will call us back */ | |
163 | ||
9bb69af4 | 164 | r = user_stop(u, true); |
3f49d45a | 165 | if (r < 0) |
ebcf1f97 | 166 | return r; |
3f49d45a | 167 | |
df2d202e | 168 | return sd_bus_reply_method_return(message, NULL); |
3f49d45a LP |
169 | } |
170 | ||
19070062 | 171 | int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
172 | User *u = userdata; |
173 | int32_t signo; | |
a185c5aa LP |
174 | int r; |
175 | ||
3f49d45a | 176 | assert(message); |
cc377381 | 177 | assert(u); |
3f49d45a | 178 | |
c529695e LP |
179 | r = bus_verify_polkit_async( |
180 | message, | |
181 | CAP_KILL, | |
182 | "org.freedesktop.login1.manage", | |
403ed0e5 | 183 | NULL, |
c529695e LP |
184 | false, |
185 | u->uid, | |
186 | &u->manager->polkit_registry, | |
187 | error); | |
188 | if (r < 0) | |
189 | return r; | |
190 | if (r == 0) | |
191 | return 1; /* Will call us back */ | |
192 | ||
cc377381 LP |
193 | r = sd_bus_message_read(message, "i", &signo); |
194 | if (r < 0) | |
ebcf1f97 | 195 | return r; |
a185c5aa | 196 | |
6eb7c172 | 197 | if (!SIGNAL_VALID(signo)) |
ebcf1f97 | 198 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); |
a185c5aa | 199 | |
cc377381 LP |
200 | r = user_kill(u, signo); |
201 | if (r < 0) | |
ebcf1f97 | 202 | return r; |
a185c5aa | 203 | |
df2d202e | 204 | return sd_bus_reply_method_return(message, NULL); |
3f49d45a LP |
205 | } |
206 | ||
cc377381 LP |
207 | const sd_bus_vtable user_vtable[] = { |
208 | SD_BUS_VTABLE_START(0), | |
209 | ||
556089dc LP |
210 | SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(User, uid), SD_BUS_VTABLE_PROPERTY_CONST), |
211 | SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(User, gid), SD_BUS_VTABLE_PROPERTY_CONST), | |
212 | SD_BUS_PROPERTY("Name", "s", NULL, offsetof(User, name), SD_BUS_VTABLE_PROPERTY_CONST), | |
213 | BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST), | |
214 | SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST), | |
215 | SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST), | |
216 | SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST), | |
952d3260 | 217 | SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
cc377381 | 218 | SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0), |
91ab7b01 | 219 | SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, 0), |
cc377381 LP |
220 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
221 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
222 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3a9f7a30 | 223 | SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0), |
cc377381 | 224 | |
c529695e LP |
225 | SD_BUS_METHOD("Terminate", NULL, NULL, bus_user_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED), |
226 | SD_BUS_METHOD("Kill", "i", NULL, bus_user_method_kill, SD_BUS_VTABLE_UNPRIVILEGED), | |
cc377381 LP |
227 | |
228 | SD_BUS_VTABLE_END | |
229 | }; | |
3f49d45a | 230 | |
f00c3121 | 231 | int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
3f49d45a | 232 | Manager *m = userdata; |
309a29df | 233 | uid_t uid; |
cc377381 | 234 | User *user; |
3f49d45a LP |
235 | int r; |
236 | ||
cc377381 LP |
237 | assert(bus); |
238 | assert(path); | |
239 | assert(interface); | |
240 | assert(found); | |
241 | assert(m); | |
3f49d45a | 242 | |
927b1649 LP |
243 | if (streq(path, "/org/freedesktop/login1/user/self")) { |
244 | sd_bus_message *message; | |
3f49d45a | 245 | |
19befb2d | 246 | message = sd_bus_get_current_message(bus); |
927b1649 LP |
247 | if (!message) |
248 | return 0; | |
249 | ||
e4d2984b | 250 | r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); |
77c45ce3 LP |
251 | if (r == -ENXIO) { |
252 | sd_bus_error_free(error); | |
253 | return 0; | |
254 | } | |
927b1649 | 255 | if (r < 0) |
5b12334d | 256 | return r; |
927b1649 | 257 | } else { |
927b1649 | 258 | const char *p; |
3f49d45a | 259 | |
927b1649 LP |
260 | p = startswith(path, "/org/freedesktop/login1/user/_"); |
261 | if (!p) | |
262 | return 0; | |
263 | ||
309a29df | 264 | r = parse_uid(p, &uid); |
2ac0ab59 LP |
265 | if (r < 0) |
266 | return 0; | |
309a29df | 267 | |
2ac0ab59 LP |
268 | user = hashmap_get(m->users, UID_TO_PTR(uid)); |
269 | if (!user) | |
270 | return 0; | |
271 | } | |
3f49d45a | 272 | |
cc377381 LP |
273 | *found = user; |
274 | return 1; | |
3f49d45a LP |
275 | } |
276 | ||
3f49d45a LP |
277 | char *user_bus_path(User *u) { |
278 | char *s; | |
279 | ||
280 | assert(u); | |
281 | ||
de0671ee | 282 | if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->uid) < 0) |
3f49d45a LP |
283 | return NULL; |
284 | ||
285 | return s; | |
286 | } | |
da119395 | 287 | |
f00c3121 | 288 | int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
cc377381 | 289 | _cleanup_strv_free_ char **l = NULL; |
ca56b0a6 | 290 | sd_bus_message *message; |
cc377381 LP |
291 | Manager *m = userdata; |
292 | User *user; | |
293 | Iterator i; | |
294 | int r; | |
da119395 | 295 | |
cc377381 LP |
296 | assert(bus); |
297 | assert(path); | |
298 | assert(nodes); | |
da119395 | 299 | |
cc377381 LP |
300 | HASHMAP_FOREACH(user, m->users, i) { |
301 | char *p; | |
da119395 | 302 | |
cc377381 LP |
303 | p = user_bus_path(user); |
304 | if (!p) | |
305 | return -ENOMEM; | |
da119395 | 306 | |
6e18964d ZJS |
307 | r = strv_consume(&l, p); |
308 | if (r < 0) | |
cc377381 | 309 | return r; |
cc377381 | 310 | } |
da119395 | 311 | |
ca56b0a6 DH |
312 | message = sd_bus_get_current_message(bus); |
313 | if (message) { | |
4afd3348 | 314 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
ca56b0a6 DH |
315 | |
316 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
317 | if (r >= 0) { | |
77c45ce3 LP |
318 | uid_t uid; |
319 | ||
ca56b0a6 DH |
320 | r = sd_bus_creds_get_owner_uid(creds, &uid); |
321 | if (r >= 0) { | |
322 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
323 | if (user) { | |
324 | r = strv_extend(&l, "/org/freedesktop/login1/user/self"); | |
325 | if (r < 0) | |
326 | return r; | |
327 | } | |
328 | } | |
329 | } | |
330 | } | |
b298e984 | 331 | |
1cc6c93a | 332 | *nodes = TAKE_PTR(l); |
da119395 | 333 | |
cc377381 LP |
334 | return 1; |
335 | } | |
336 | ||
337 | int user_send_signal(User *u, bool new_user) { | |
338 | _cleanup_free_ char *p = NULL; | |
da119395 | 339 | |
cc377381 LP |
340 | assert(u); |
341 | ||
342 | p = user_bus_path(u); | |
343 | if (!p) | |
4654e558 | 344 | return -ENOMEM; |
da119395 | 345 | |
cc377381 LP |
346 | return sd_bus_emit_signal( |
347 | u->manager->bus, | |
348 | "/org/freedesktop/login1", | |
349 | "org.freedesktop.login1.Manager", | |
350 | new_user ? "UserNew" : "UserRemoved", | |
351 | "uo", (uint32_t) u->uid, p); | |
da119395 | 352 | } |
9418f147 | 353 | |
cc377381 | 354 | int user_send_changed(User *u, const char *properties, ...) { |
ce0fc5f5 | 355 | _cleanup_free_ char *p = NULL; |
cc377381 | 356 | char **l; |
9418f147 LP |
357 | |
358 | assert(u); | |
359 | ||
ed18b08b LP |
360 | if (!u->started) |
361 | return 0; | |
362 | ||
9418f147 LP |
363 | p = user_bus_path(u); |
364 | if (!p) | |
365 | return -ENOMEM; | |
366 | ||
cc377381 | 367 | l = strv_from_stdarg_alloca(properties); |
9418f147 | 368 | |
cc377381 | 369 | return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", l); |
9418f147 | 370 | } |