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