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