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