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