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