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