chiark / gitweb /
9767f7d30c58773cc79e35d893820a391225c69f
[elogind.git] / src / login / logind-session-dbus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <string.h>
24
25 #include "logind.h"
26 #include "logind-session.h"
27 #include "dbus-common.h"
28 #include "util.h"
29
30 #define BUS_SESSION_INTERFACE \
31         " <interface name=\"org.freedesktop.login1.Session\">\n"        \
32         "  <method name=\"Terminate\"/>\n"                              \
33         "  <method name=\"Activate\"/>\n"                               \
34         "  <method name=\"Lock\"/>\n"                                   \
35         "  <method name=\"Unlock\"/>\n"                                 \
36         "  <method name=\"SetIdleHint\">\n"                             \
37         "   <arg name=\"b\" type=\"b\"/>\n"                             \
38         "  </method>\n"                                                 \
39         "  <method name=\"Kill\">\n"                                    \
40         "   <arg name=\"who\" type=\"s\"/>\n"                           \
41         "   <arg name=\"signal\" type=\"s\"/>\n"                        \
42         "  </method>\n"                                                 \
43         "  <property name=\"Id\" type=\"s\" access=\"read\"/>\n"        \
44         "  <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n"   \
45         "  <property name=\"Name\" type=\"s\" access=\"read\"/>\n"      \
46         "  <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
47         "  <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
48         "  <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
49         "  <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n"      \
50         "  <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n"   \
51         "  <property name=\"TTY\" type=\"s\" access=\"read\"/>\n"       \
52         "  <property name=\"Display\" type=\"s\" access=\"read\"/>\n"   \
53         "  <property name=\"Remote\" type=\"b\" access=\"read\"/>\n"    \
54         "  <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
55         "  <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
56         "  <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \
57         "  <property name=\"Leader\" type=\"u\" access=\"read\"/>\n"    \
58         "  <property name=\"Audit\" type=\"u\" access=\"read\"/>\n"     \
59         "  <property name=\"Type\" type=\"s\" access=\"read\"/>\n"      \
60         "  <property name=\"Active\" type=\"b\" access=\"read\"/>\n"    \
61         "  <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
62         "  <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
63         "  <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
64         "  <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n"  \
65         "  <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
66         "  <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
67         " </interface>\n"
68
69 #define INTROSPECTION                                                   \
70         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
71         "<node>\n"                                                      \
72         BUS_SESSION_INTERFACE                                           \
73         BUS_PROPERTIES_INTERFACE                                        \
74         BUS_PEER_INTERFACE                                              \
75         BUS_INTROSPECTABLE_INTERFACE                                    \
76         "</node>\n"
77
78 #define INTERFACES_LIST                              \
79         BUS_GENERIC_INTERFACES_LIST                  \
80         "org.freedesktop.login1.Session\0"
81
82 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
83         DBusMessageIter sub;
84         Session *s = data;
85         const char *id, *path;
86         char *p = NULL;
87
88         assert(i);
89         assert(property);
90         assert(s);
91
92         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
93                 return -ENOMEM;
94
95         if (s->seat) {
96                 id = s->seat->id;
97                 path = p = seat_bus_path(s->seat);
98
99                 if (!p)
100                         return -ENOMEM;
101         } else {
102                 id = "";
103                 path = "/";
104         }
105
106         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
107             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
108                 free(p);
109                 return -ENOMEM;
110         }
111
112         free(p);
113
114         if (!dbus_message_iter_close_container(i, &sub))
115                 return -ENOMEM;
116
117         return 0;
118 }
119
120 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
121         DBusMessageIter sub;
122         User *u = data;
123         char *p = NULL;
124
125         assert(i);
126         assert(property);
127         assert(u);
128
129         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
130                 return -ENOMEM;
131
132         p = user_bus_path(u);
133         if (!p)
134                 return -ENOMEM;
135
136         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->uid) ||
137             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
138                 free(p);
139                 return -ENOMEM;
140         }
141
142         free(p);
143
144         if (!dbus_message_iter_close_container(i, &sub))
145                 return -ENOMEM;
146
147         return 0;
148 }
149
150 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
151         Session *s = data;
152         dbus_bool_t b;
153
154         assert(i);
155         assert(property);
156         assert(s);
157
158         b = session_is_active(s);
159         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
160                 return -ENOMEM;
161
162         return 0;
163 }
164
165 static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
166         Session *s = data;
167         int b;
168
169         assert(i);
170         assert(property);
171         assert(s);
172
173         b = session_get_idle_hint(s, NULL) > 0;
174         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
175                 return -ENOMEM;
176
177         return 0;
178 }
179
180 static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
181         Session *s = data;
182         dual_timestamp t;
183         uint64_t u;
184
185         assert(i);
186         assert(property);
187         assert(s);
188
189         session_get_idle_hint(s, &t);
190         u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
191
192         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
193                 return -ENOMEM;
194
195         return 0;
196 }
197
198 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
199
200 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
201         Session *s;
202         char *id;
203
204         assert(m);
205         assert(path);
206         assert(_s);
207
208         if (!startswith(path, "/org/freedesktop/login1/session/"))
209                 return -EINVAL;
210
211         id = bus_path_unescape(path + 32);
212         if (!id)
213                 return -ENOMEM;
214
215         s = hashmap_get(m->sessions, id);
216         free(id);
217
218         if (!s)
219                 return -ENOENT;
220
221         *_s = s;
222         return 0;
223 }
224
225 static const BusProperty bus_login_session_properties[] = {
226         { "Id",                     bus_property_append_string,         "s", offsetof(Session, id),                 true },
227         { "Timestamp",              bus_property_append_usec,           "t", offsetof(Session, timestamp.realtime)  },
228         { "TimestampMonotonic",     bus_property_append_usec,           "t", offsetof(Session, timestamp.monotonic) },
229         { "ControlGroupPath",       bus_property_append_string,         "s", offsetof(Session, cgroup_path),        true },
230         { "VTNr",                   bus_property_append_uint32,         "u", offsetof(Session, vtnr)                },
231         { "Seat",                   bus_session_append_seat,         "(so)", 0 },
232         { "TTY",                    bus_property_append_string,         "s", offsetof(Session, tty),                true },
233         { "Display",                bus_property_append_string,         "s", offsetof(Session, display),            true },
234         { "Remote",                 bus_property_append_bool,           "b", offsetof(Session, remote)              },
235         { "RemoteUser",             bus_property_append_string,         "s", offsetof(Session, remote_user),        true },
236         { "RemoteHost",             bus_property_append_string,         "s", offsetof(Session, remote_host),        true },
237         { "Service",                bus_property_append_string,         "s", offsetof(Session, service),            true },
238         { "Leader",                 bus_property_append_pid,            "u", offsetof(Session, leader)              },
239         { "Audit",                  bus_property_append_uint32,         "u", offsetof(Session, audit_id)            },
240         { "Type",                   bus_session_append_type,            "s", offsetof(Session, type)                },
241         { "Active",                 bus_session_append_active,          "b", 0 },
242         { "Controllers",            bus_property_append_strv,          "as", offsetof(Session, controllers),        true },
243         { "ResetControllers",       bus_property_append_strv,          "as", offsetof(Session, reset_controllers),  true },
244         { "KillProcesses",          bus_property_append_bool,           "b", offsetof(Session, kill_processes)      },
245         { "IdleHint",               bus_session_append_idle_hint,       "b", 0 },
246         { "IdleSinceHint",          bus_session_append_idle_hint_since, "t", 0 },
247         { "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", 0 },
248         { NULL, }
249 };
250
251 static const BusProperty bus_login_session_user_properties[] = {
252         { "User",                   bus_session_append_user,         "(uo)", 0 },
253         { "Name",                   bus_property_append_string,         "s", offsetof(User, name),                  true },
254         { NULL, }
255 };
256
257 static DBusHandlerResult session_message_dispatch(
258                 Session *s,
259                 DBusConnection *connection,
260                 DBusMessage *message) {
261
262         DBusError error;
263         DBusMessage *reply = NULL;
264         int r;
265
266         assert(s);
267         assert(connection);
268         assert(message);
269
270         dbus_error_init(&error);
271
272         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) {
273
274                 r = session_stop(s);
275                 if (r < 0)
276                         return bus_send_error_reply(connection, message, NULL, r);
277
278                 reply = dbus_message_new_method_return(message);
279                 if (!reply)
280                         goto oom;
281
282         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) {
283
284                 r = session_activate(s);
285                 if (r < 0)
286                         return bus_send_error_reply(connection, message, NULL, r);
287
288                 reply = dbus_message_new_method_return(message);
289                 if (!reply)
290                         goto oom;
291
292         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") ||
293                    dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) {
294
295                 if (session_send_lock(s, streq(dbus_message_get_member(message), "Lock")) < 0)
296                         goto oom;
297
298                 reply = dbus_message_new_method_return(message);
299                 if (!reply)
300                         goto oom;
301
302         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) {
303                 dbus_bool_t b;
304                 unsigned long ul;
305
306                 if (!dbus_message_get_args(
307                                     message,
308                                     &error,
309                                     DBUS_TYPE_BOOLEAN, &b,
310                                     DBUS_TYPE_INVALID))
311                         return bus_send_error_reply(connection, message, &error, -EINVAL);
312
313                 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
314                 if (ul == (unsigned long) -1)
315                         return bus_send_error_reply(connection, message, &error, -EIO);
316
317                 if (ul != 0 && ul != s->user->uid)
318                         return bus_send_error_reply(connection, message, NULL, -EPERM);
319
320                 session_set_idle_hint(s, b);
321
322                 reply = dbus_message_new_method_return(message);
323                 if (!reply)
324                         goto oom;
325
326         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Kill")) {
327                 const char *swho;
328                 int32_t signo;
329                 KillWho who;
330
331                 if (!dbus_message_get_args(
332                                     message,
333                                     &error,
334                                     DBUS_TYPE_STRING, &swho,
335                                     DBUS_TYPE_INT32, &signo,
336                                     DBUS_TYPE_INVALID))
337                         return bus_send_error_reply(connection, message, &error, -EINVAL);
338
339                 if (isempty(swho))
340                         who = KILL_ALL;
341                 else {
342                         who = kill_who_from_string(swho);
343                         if (who < 0)
344                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
345                 }
346
347                 if (signo <= 0 || signo >= _NSIG)
348                         return bus_send_error_reply(connection, message, &error, -EINVAL);
349
350                 r = session_kill(s, who, signo);
351                 if (r < 0)
352                         return bus_send_error_reply(connection, message, NULL, r);
353
354                 reply = dbus_message_new_method_return(message);
355                 if (!reply)
356                         goto oom;
357
358         } else {
359                 const BusBoundProperties bps[] = {
360                         { "org.freedesktop.login1.Session", bus_login_session_properties,      s       },
361                         { "org.freedesktop.login1.Session", bus_login_session_user_properties, s->user },
362                         { NULL, }
363                 };
364                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
365         }
366
367         if (reply) {
368                 if (!dbus_connection_send(connection, reply, NULL))
369                         goto oom;
370
371                 dbus_message_unref(reply);
372         }
373
374         return DBUS_HANDLER_RESULT_HANDLED;
375
376 oom:
377         if (reply)
378                 dbus_message_unref(reply);
379
380         dbus_error_free(&error);
381
382         return DBUS_HANDLER_RESULT_NEED_MEMORY;
383 }
384
385 static DBusHandlerResult session_message_handler(
386                 DBusConnection *connection,
387                 DBusMessage *message,
388                 void *userdata) {
389
390         Manager *m = userdata;
391         Session *s;
392         int r;
393
394         r = get_session_for_path(m, dbus_message_get_path(message), &s);
395         if (r < 0) {
396
397                 if (r == -ENOMEM)
398                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
399
400                 if (r == -ENOENT) {
401                         DBusError e;
402
403                         dbus_error_init(&e);
404                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
405                         return bus_send_error_reply(connection, message, &e, r);
406                 }
407
408                 return bus_send_error_reply(connection, message, NULL, r);
409         }
410
411         return session_message_dispatch(s, connection, message);
412 }
413
414 const DBusObjectPathVTable bus_session_vtable = {
415         .message_function = session_message_handler
416 };
417
418 char *session_bus_path(Session *s) {
419         char *t, *r;
420
421         assert(s);
422
423         t = bus_path_escape(s->id);
424         if (!t)
425                 return NULL;
426
427         r = strappend("/org/freedesktop/login1/session/", t);
428         free(t);
429
430         return r;
431 }
432
433 int session_send_signal(Session *s, bool new_session) {
434         DBusMessage *m;
435         int r = -ENOMEM;
436         char *p = NULL;
437
438         assert(s);
439
440         m = dbus_message_new_signal("/org/freedesktop/login1",
441                                     "org.freedesktop.login1.Manager",
442                                     new_session ? "SessionNew" : "SessionRemoved");
443
444         if (!m)
445                 return -ENOMEM;
446
447         p = session_bus_path(s);
448         if (!p)
449                 goto finish;
450
451         if (!dbus_message_append_args(
452                             m,
453                             DBUS_TYPE_STRING, &s->id,
454                             DBUS_TYPE_OBJECT_PATH, &p,
455                             DBUS_TYPE_INVALID))
456                 goto finish;
457
458         if (!dbus_connection_send(s->manager->bus, m, NULL))
459                 goto finish;
460
461         r = 0;
462
463 finish:
464         dbus_message_unref(m);
465         free(p);
466
467         return r;
468 }
469
470 int session_send_changed(Session *s, const char *properties) {
471         DBusMessage *m;
472         int r = -ENOMEM;
473         char *p = NULL;
474
475         assert(s);
476
477         if (!s->started)
478                 return 0;
479
480         p = session_bus_path(s);
481         if (!p)
482                 return -ENOMEM;
483
484         m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties);
485         if (!m)
486                 goto finish;
487
488         if (!dbus_connection_send(s->manager->bus, m, NULL))
489                 goto finish;
490
491         r = 0;
492
493 finish:
494         if (m)
495                 dbus_message_unref(m);
496         free(p);
497
498         return r;
499 }
500
501 int session_send_lock(Session *s, bool lock) {
502         DBusMessage *m;
503         bool b;
504         char *p;
505
506         assert(s);
507
508         p = session_bus_path(s);
509         if (!p)
510                 return -ENOMEM;
511
512         m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock");
513         free(p);
514
515         if (!m)
516                 return -ENOMEM;
517
518         b = dbus_connection_send(s->manager->bus, m, NULL);
519         dbus_message_unref(m);
520
521         if (!b)
522                 return -ENOMEM;
523
524         return 0;
525 }