chiark / gitweb /
5997fb5d7ad26b0648ab35ff5ecb80d89e584a38
[elogind.git] / src / logind-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 #include <unistd.h>
25 #include <pwd.h>
26
27 #include "logind.h"
28 #include "dbus-common.h"
29 #include "strv.h"
30 #include "polkit.h"
31
32 #define BUS_MANAGER_INTERFACE                                           \
33         " <interface name=\"org.freedesktop.login1.Manager\">\n"        \
34         "  <method name=\"GetSession\">\n"                              \
35         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
36         "   <arg name=\"session\" type=\"o\" direction=\"out\"/>\n"     \
37         "  </method>\n"                                                 \
38         "  <method name=\"GetUser\">\n"                                 \
39         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
40         "   <arg name=\"user\" type=\"o\" direction=\"out\"/>\n"        \
41         "  </method>\n"                                                 \
42         "  <method name=\"GetSeat\">\n"                                 \
43         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
44         "   <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n"        \
45         "  </method>\n"                                                 \
46         "  <method name=\"ListSessions\">\n"                            \
47         "   <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
48         "  </method>\n"                                                 \
49         "  <method name=\"ListUsers\">\n"                               \
50         "   <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n"  \
51         "  </method>\n"                                                 \
52         "  <method name=\"ListSeats\">\n"                               \
53         "   <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n"   \
54         "  </method>\n"                                                 \
55         "  <method name=\"CreateSession\">\n"                           \
56         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
57         "   <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n"       \
58         "   <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n"       \
59         "   <arg name=\"type\" type=\"s\" direction=\"in\"/>\n"         \
60         "   <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n"         \
61         "   <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n"         \
62         "   <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n"          \
63         "   <arg name=\"display\" type=\"s\" direction=\"in\"/>\n"      \
64         "   <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n"       \
65         "   <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n"  \
66         "   <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n"  \
67         "   <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
68         "   <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
69         "   <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
70         "   <arg name=\"id\" type=\"s\" direction=\"out\"/>\n"          \
71         "   <arg name=\"path\" type=\"o\" direction=\"out\"/>\n"        \
72         "   <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
73         "   <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n"          \
74         "  </method>\n"                                                 \
75         "  <method name=\"ActivateSession\">\n"                         \
76         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
77         "  </method>\n"                                                 \
78         "  <method name=\"TerminateSession\">\n"                        \
79         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
80         "  </method>\n"                                                 \
81         "  <method name=\"TerminateUser\">\n"                           \
82         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
83         "  </method>\n"                                                 \
84         "  <method name=\"TerminateSeat\">\n"                           \
85         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
86         "  </method>\n"                                                 \
87         "  <method name=\"SetUserLinger\">\n"                           \
88         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
89         "   <arg name=\"b\" type=\"b\" direction=\"in\"/>\n"            \
90         "   <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n"  \
91         "  </method>\n"                                                 \
92         "  <method name=\"AttachDevice\">\n"                            \
93         "   <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n"         \
94         "   <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n"        \
95         "   <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n"  \
96         "  </method>\n"                                                 \
97         "  <signal name=\"SessionNew\">\n"                              \
98         "   <arg name=\"id\" type=\"s\"/>\n"                            \
99         "   <arg name=\"path\" type=\"o\"/>\n"                          \
100         "  </signal>\n"                                                 \
101         "  <signal name=\"SessionRemoved\">\n"                          \
102         "   <arg name=\"id\" type=\"s\"/>\n"                            \
103         "   <arg name=\"path\" type=\"o\"/>\n"                          \
104         "  </signal>\n"                                                 \
105         "  <signal name=\"UserNew\">\n"                                 \
106         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
107         "   <arg name=\"path\" type=\"o\"/>\n"                          \
108         "  </signal>\n"                                                 \
109         "  <signal name=\"UserRemoved\">\n"                             \
110         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
111         "   <arg name=\"path\" type=\"o\"/>\n"                          \
112         "  </signal>\n"                                                 \
113         "  <signal name=\"SeatNew\">\n"                                 \
114         "   <arg name=\"id\" type=\"s\"/>\n"                            \
115         "   <arg name=\"path\" type=\"o\"/>\n"                          \
116         "  </signal>\n"                                                 \
117         "  <signal name=\"SeatRemoved\">\n"                             \
118         "   <arg name=\"id\" type=\"s\"/>\n"                            \
119         "   <arg name=\"path\" type=\"o\"/>\n"                          \
120         "  </signal>\n"                                                 \
121         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
122         "  <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
123         "  <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
124         "  <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
125         "  <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
126         "  <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
127         "  <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
128         "  <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n"  \
129         "  <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
130         "  <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
131         " </interface>\n"
132
133 #define INTROSPECTION_BEGIN                                             \
134         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
135         "<node>\n"                                                      \
136         BUS_MANAGER_INTERFACE                                           \
137         BUS_PROPERTIES_INTERFACE                                        \
138         BUS_PEER_INTERFACE                                              \
139         BUS_INTROSPECTABLE_INTERFACE
140
141 #define INTROSPECTION_END                                               \
142         "</node>\n"
143
144 #define INTERFACES_LIST                              \
145         BUS_GENERIC_INTERFACES_LIST                  \
146         "org.freedesktop.login1.Manager\0"
147
148 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
149         Manager *m = data;
150         dbus_bool_t b;
151
152         assert(i);
153         assert(property);
154         assert(m);
155
156         b = manager_get_idle_hint(m, NULL) > 0;
157         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
158                 return -ENOMEM;
159
160         return 0;
161 }
162
163 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
164         Manager *m = data;
165         dual_timestamp t;
166         uint64_t u;
167
168         assert(i);
169         assert(property);
170         assert(m);
171
172         manager_get_idle_hint(m, &t);
173         u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
174
175         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
176                 return -ENOMEM;
177
178         return 0;
179 }
180
181 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
182         Session *session = NULL;
183         User *user = NULL;
184         const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
185         uint32_t uid, leader, audit_id = 0;
186         dbus_bool_t remote, kill_processes;
187         char **controllers = NULL, **reset_controllers = NULL;
188         SessionType t;
189         Seat *s;
190         DBusMessageIter iter;
191         int r;
192         char *id = NULL, *p;
193         uint32_t vtnr = 0;
194         int pipe_fds[2] = { -1, -1 };
195         DBusMessage *reply = NULL;
196         bool b;
197
198         assert(m);
199         assert(message);
200         assert(_reply);
201
202         if (!dbus_message_iter_init(message, &iter) ||
203             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
204                 return -EINVAL;
205
206         dbus_message_iter_get_basic(&iter, &uid);
207
208         if (!dbus_message_iter_next(&iter) ||
209             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
210                 return -EINVAL;
211
212         dbus_message_iter_get_basic(&iter, &leader);
213
214         if (leader <= 0 ||
215             !dbus_message_iter_next(&iter) ||
216             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
217                 return -EINVAL;
218
219         dbus_message_iter_get_basic(&iter, &service);
220
221         if (!dbus_message_iter_next(&iter) ||
222             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
223                 return -EINVAL;
224
225         dbus_message_iter_get_basic(&iter, &type);
226         t = session_type_from_string(type);
227
228         if (t < 0 ||
229             !dbus_message_iter_next(&iter) ||
230             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
231                 return -EINVAL;
232
233         dbus_message_iter_get_basic(&iter, &seat);
234
235         if (isempty(seat))
236                 s = NULL;
237         else {
238                 s = hashmap_get(m->seats, seat);
239                 if (!s)
240                         return -ENOENT;
241         }
242
243         if (!dbus_message_iter_next(&iter) ||
244             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
245                 return -EINVAL;
246
247         dbus_message_iter_get_basic(&iter, &vtnr);
248
249         if (!dbus_message_iter_next(&iter) ||
250             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
251                 return -EINVAL;
252
253         dbus_message_iter_get_basic(&iter, &tty);
254
255         if (tty_is_vc(tty)) {
256                 int v;
257
258                 if (!s)
259                         s = m->vtconsole;
260                 else if (s != m->vtconsole)
261                         return -EINVAL;
262
263                 v = vtnr_from_tty(tty);
264
265                 if (v <= 0)
266                         return v < 0 ? v : -EINVAL;
267
268                 if (vtnr <= 0)
269                         vtnr = (uint32_t) v;
270                 else if (vtnr != (uint32_t) v)
271                         return -EINVAL;
272
273         } else if (!isempty(tty) && s && seat_is_vtconsole(s))
274                 return -EINVAL;
275
276         if (s) {
277                 if (seat_is_vtconsole(s)) {
278                         if (vtnr <= 0 || vtnr > 63)
279                                 return -EINVAL;
280                 } else {
281                         if (vtnr > 0)
282                                 return -EINVAL;
283                 }
284         }
285
286         if (!dbus_message_iter_next(&iter) ||
287             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
288                 return -EINVAL;
289
290         dbus_message_iter_get_basic(&iter, &display);
291
292         if (!dbus_message_iter_next(&iter) ||
293             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
294                 return -EINVAL;
295
296         dbus_message_iter_get_basic(&iter, &remote);
297
298         if (!dbus_message_iter_next(&iter) ||
299             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
300                 return -EINVAL;
301
302         dbus_message_iter_get_basic(&iter, &remote_user);
303
304         if (!dbus_message_iter_next(&iter) ||
305             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
306                 return -EINVAL;
307
308         dbus_message_iter_get_basic(&iter, &remote_host);
309
310         if (!dbus_message_iter_next(&iter) ||
311             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
312             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
313                 return -EINVAL;
314
315         r = bus_parse_strv_iter(&iter, &controllers);
316         if (r < 0)
317                 return -EINVAL;
318
319         if (!dbus_message_iter_next(&iter) ||
320             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
321             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
322                 r = -EINVAL;
323                 goto fail;
324         }
325
326         r = bus_parse_strv_iter(&iter, &reset_controllers);
327         if (r < 0)
328                 goto fail;
329
330         if (!dbus_message_iter_next(&iter) ||
331             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
332                 r = -EINVAL;
333                 goto fail;
334         }
335
336         dbus_message_iter_get_basic(&iter, &kill_processes);
337
338         r = manager_add_user_by_uid(m, uid, &user);
339         if (r < 0)
340                 goto fail;
341
342         audit_session_from_pid(leader, &audit_id);
343
344         if (audit_id > 0) {
345                 asprintf(&id, "%lu", (unsigned long) audit_id);
346
347                 if (!id) {
348                         r = -ENOMEM;
349                         goto fail;
350                 }
351
352                 session = hashmap_get(m->sessions, id);
353
354                 if (session) {
355
356                         /* Session already exists, client is probably
357                          * something like "su" which changes uid but
358                          * is still the same audit session */
359
360                         reply = dbus_message_new_method_return(message);
361                         if (!reply) {
362                                 r = -ENOMEM;
363                                 goto fail;
364                         }
365
366                         /* Create a throw-away fd */
367                         if (pipe(pipe_fds) < 0) {
368                                 r = -errno;
369                                 goto fail;
370                         }
371
372                         close_nointr_nofail(pipe_fds[0]);
373                         pipe_fds[0] = -1;
374
375                         p = session_bus_path(session);
376                         if (!p) {
377                                 r = -ENOMEM;
378                                 goto fail;
379                         }
380
381                         b = dbus_message_append_args(
382                                         reply,
383                                         DBUS_TYPE_STRING, &session->id,
384                                         DBUS_TYPE_OBJECT_PATH, &p,
385                                         DBUS_TYPE_STRING, &session->user->runtime_path,
386                                         DBUS_TYPE_UNIX_FD, &pipe_fds[1],
387                                         DBUS_TYPE_INVALID);
388                         free(p);
389
390                         if (!b) {
391                                 r = -ENOMEM;
392                                 goto fail;
393                         }
394
395                         close_nointr_nofail(pipe_fds[1]);
396                         *_reply = reply;
397
398                         return 0;
399                 }
400
401         } else {
402                 do {
403                         free(id);
404                         asprintf(&id, "c%lu", ++m->session_counter);
405
406                         if (!id) {
407                                 r = -ENOMEM;
408                                 goto fail;
409                         }
410
411                 } while (hashmap_get(m->sessions, id));
412         }
413
414         r = manager_add_session(m, user, id, &session);
415         free(id);
416         if (r < 0)
417                 goto fail;
418
419         session->leader = leader;
420         session->audit_id = audit_id;
421         session->type = t;
422         session->remote = remote;
423         session->controllers = controllers;
424         session->reset_controllers = reset_controllers;
425         session->kill_processes = kill_processes;
426         session->vtnr = vtnr;
427
428         controllers = reset_controllers = NULL;
429
430         if (!isempty(tty)) {
431                 session->tty = strdup(tty);
432                 if (!session->tty) {
433                         r = -ENOMEM;
434                         goto fail;
435                 }
436         }
437
438         if (!isempty(display)) {
439                 session->display = strdup(display);
440                 if (!session->display) {
441                         r = -ENOMEM;
442                         goto fail;
443                 }
444         }
445
446         if (!isempty(remote_user)) {
447                 session->remote_user = strdup(remote_user);
448                 if (!session->remote_user) {
449                         r = -ENOMEM;
450                         goto fail;
451                 }
452         }
453
454         if (!isempty(remote_host)) {
455                 session->remote_host = strdup(remote_host);
456                 if (!session->remote_host) {
457                         r = -ENOMEM;
458                         goto fail;
459                 }
460         }
461
462         if (!isempty(service)) {
463                 session->service = strdup(service);
464                 if (!session->service) {
465                         r = -ENOMEM;
466                         goto fail;
467                 }
468         }
469
470         if (pipe(pipe_fds) < 0) {
471                 r = -errno;
472                 goto fail;
473         }
474
475         r = session_set_pipe_fd(session, pipe_fds[0]);
476         if (r < 0)
477                 goto fail;
478         pipe_fds[0] = -1;
479
480         if (s) {
481                 r = seat_attach_session(s, session);
482                 if (r < 0)
483                         goto fail;
484         }
485
486         r = session_start(session);
487         if (r < 0)
488                 goto fail;
489
490         reply = dbus_message_new_method_return(message);
491         if (!reply) {
492                 r = -ENOMEM;
493                 goto fail;
494         }
495
496         p = session_bus_path(session);
497         if (!p) {
498                 r = -ENOMEM;
499                 goto fail;
500         }
501
502         b = dbus_message_append_args(
503                         reply,
504                         DBUS_TYPE_STRING, &session->id,
505                         DBUS_TYPE_OBJECT_PATH, &p,
506                         DBUS_TYPE_STRING, &session->user->runtime_path,
507                         DBUS_TYPE_UNIX_FD, &pipe_fds[1],
508                         DBUS_TYPE_INVALID);
509         free(p);
510
511         if (!b) {
512                 r = -ENOMEM;
513                 goto fail;
514         }
515
516         close_nointr_nofail(pipe_fds[1]);
517         *_reply = reply;
518
519         return 0;
520
521 fail:
522         strv_free(controllers);
523         strv_free(reset_controllers);
524
525         if (session)
526                 session_add_to_gc_queue(session);
527
528         if (user)
529                 user_add_to_gc_queue(user);
530
531         close_pipe(pipe_fds);
532
533         if (reply)
534                 dbus_message_unref(reply);
535
536         return r;
537 }
538
539 static bool device_has_tag(struct udev_device *d, const char *tag) {
540         struct udev_list_entry *first, *item;
541
542         assert(d);
543         assert(tag);
544
545         udev_device_get_is_initialized(d);
546
547         first = udev_device_get_tags_list_entry(d);
548         udev_list_entry_foreach(item, first)
549                 if (streq(udev_list_entry_get_name(item), tag))
550                         return true;
551
552         return false;
553 }
554
555 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
556         struct udev_device *d;
557         char *rule = NULL, *file = NULL;
558         const char *id_for_seat;
559         int r;
560         struct udev_enumerate *e;
561         struct udev_list_entry *first, *item;
562
563         assert(m);
564         assert(seat);
565         assert(sysfs);
566
567         d = udev_device_new_from_syspath(m->udev, sysfs);
568         if (!d)
569                 return -ENODEV;
570
571         if (!device_has_tag(d, "seat")) {
572                 r = -ENODEV;
573                 goto finish;
574         }
575
576         id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
577         if (!id_for_seat) {
578                 r = -ENODEV;
579                 goto finish;
580         }
581
582         if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
583                 r = -ENOMEM;
584                 goto finish;
585         }
586
587         if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
588                 r = -ENOMEM;
589                 goto finish;
590         }
591
592         mkdir_p("/etc/udev/rules.d", 0755);
593         r = write_one_line_file(file, rule);
594         if (r < 0)
595                 goto finish;
596
597         e = udev_enumerate_new(m->udev);
598         if (!e) {
599                 r = -ENOMEM;
600                 goto finish;
601         }
602
603         if (udev_enumerate_scan_devices(e) < 0) {
604                 r = -EIO;
605                 goto finish;
606         }
607
608         first = udev_enumerate_get_list_entry(e);
609         udev_list_entry_foreach(item, first) {
610                 char *t;
611                 const char *p;
612
613                 p = udev_list_entry_get_name(item);
614                 if (!startswith(p, sysfs))
615                         continue;
616
617                 t = strappend(p, "/uevent");
618                 if (!t) {
619                         r = -ENOMEM;
620                         goto finish;
621                 }
622
623                 write_one_line_file(t, "change");
624                 free(t);
625         }
626
627 finish:
628         free(rule);
629         free(file);
630
631         if (d)
632                 udev_device_unref(d);
633
634         if (e)
635                 udev_enumerate_unref(e);
636
637         return r;
638 }
639
640 static DBusHandlerResult manager_message_handler(
641                 DBusConnection *connection,
642                 DBusMessage *message,
643                 void *userdata) {
644
645         Manager *m = userdata;
646
647         const BusProperty properties[] = {
648                 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy",  bus_property_append_string,   "s",  m->cgroup_path          },
649                 { "org.freedesktop.login1.Manager", "Controllers",            bus_property_append_strv,     "as", m->controllers          },
650                 { "org.freedesktop.login1.Manager", "NAutoVTs",               bus_property_append_unsigned, "u",  &m->n_autovts           },
651                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",          bus_property_append_strv,     "as", m->kill_only_users      },
652                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",       bus_property_append_strv,     "as", m->kill_exclude_users   },
653                 { "org.freedesktop.login1.Manager", "KillUserProcesses",      bus_property_append_bool,     "b",  &m->kill_user_processes },
654                 { "org.freedesktop.login1.Manager", "IdleHint",               bus_manager_append_idle_hint, "b",  m                       },
655                 { "org.freedesktop.login1.Manager", "IdleSinceHint",          bus_manager_append_idle_hint_since, "t", m                  },
656                 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m                  },
657                 { NULL, NULL, NULL, NULL, NULL }
658         };
659
660         DBusError error;
661         DBusMessage *reply = NULL;
662         int r;
663
664         assert(connection);
665         assert(message);
666         assert(m);
667
668         dbus_error_init(&error);
669
670         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
671                 const char *name;
672                 char *p;
673                 Session *session;
674                 bool b;
675
676                 if (!dbus_message_get_args(
677                                     message,
678                                     &error,
679                                     DBUS_TYPE_STRING, &name,
680                                     DBUS_TYPE_INVALID))
681                         return bus_send_error_reply(connection, message, &error, -EINVAL);
682
683                 session = hashmap_get(m->sessions, name);
684                 if (!session)
685                         return bus_send_error_reply(connection, message, &error, -ENOENT);
686
687                 reply = dbus_message_new_method_return(message);
688                 if (!reply)
689                         goto oom;
690
691                 p = session_bus_path(session);
692                 if (!p)
693                         goto oom;
694
695                 b = dbus_message_append_args(
696                                 reply,
697                                 DBUS_TYPE_OBJECT_PATH, &p,
698                                 DBUS_TYPE_INVALID);
699                 free(p);
700
701                 if (!b)
702                         goto oom;
703
704         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
705                 uint32_t uid;
706                 char *p;
707                 User *user;
708                 bool b;
709
710                 if (!dbus_message_get_args(
711                                     message,
712                                     &error,
713                                     DBUS_TYPE_UINT32, &uid,
714                                     DBUS_TYPE_INVALID))
715                         return bus_send_error_reply(connection, message, &error, -EINVAL);
716
717                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
718                 if (!user)
719                         return bus_send_error_reply(connection, message, &error, -ENOENT);
720
721                 reply = dbus_message_new_method_return(message);
722                 if (!reply)
723                         goto oom;
724
725                 p = user_bus_path(user);
726                 if (!p)
727                         goto oom;
728
729                 b = dbus_message_append_args(
730                                 reply,
731                                 DBUS_TYPE_OBJECT_PATH, &p,
732                                 DBUS_TYPE_INVALID);
733                 free(p);
734
735                 if (!b)
736                         goto oom;
737
738         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
739                 const char *name;
740                 char *p;
741                 Seat *seat;
742                 bool b;
743
744                 if (!dbus_message_get_args(
745                                     message,
746                                     &error,
747                                     DBUS_TYPE_STRING, &name,
748                                     DBUS_TYPE_INVALID))
749                         return bus_send_error_reply(connection, message, &error, -EINVAL);
750
751                 seat = hashmap_get(m->seats, name);
752                 if (!seat)
753                         return bus_send_error_reply(connection, message, &error, -ENOENT);
754
755                 reply = dbus_message_new_method_return(message);
756                 if (!reply)
757                         goto oom;
758
759                 p = seat_bus_path(seat);
760                 if (!p)
761                         goto oom;
762
763                 b = dbus_message_append_args(
764                                 reply,
765                                 DBUS_TYPE_OBJECT_PATH, &p,
766                                 DBUS_TYPE_INVALID);
767                 free(p);
768
769                 if (!b)
770                         goto oom;
771
772         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
773                 char *p;
774                 Session *session;
775                 Iterator i;
776                 DBusMessageIter iter, sub;
777                 const char *empty = "";
778
779                 reply = dbus_message_new_method_return(message);
780                 if (!reply)
781                         goto oom;
782
783                 dbus_message_iter_init_append(reply, &iter);
784
785                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
786                         goto oom;
787
788                 HASHMAP_FOREACH(session, m->sessions, i) {
789                         DBusMessageIter sub2;
790                         uint32_t uid;
791
792                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
793                                 goto oom;
794
795                         uid = session->user->uid;
796
797                         p = session_bus_path(session);
798                         if (!p)
799                                 goto oom;
800
801                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
802                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
803                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
804                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
805                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
806                                 free(p);
807                                 goto oom;
808                         }
809
810                         free(p);
811
812                         if (!dbus_message_iter_close_container(&sub, &sub2))
813                                 goto oom;
814                 }
815
816                 if (!dbus_message_iter_close_container(&iter, &sub))
817                         goto oom;
818
819         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
820                 char *p;
821                 User *user;
822                 Iterator i;
823                 DBusMessageIter iter, sub;
824
825                 reply = dbus_message_new_method_return(message);
826                 if (!reply)
827                         goto oom;
828
829                 dbus_message_iter_init_append(reply, &iter);
830
831                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
832                         goto oom;
833
834                 HASHMAP_FOREACH(user, m->users, i) {
835                         DBusMessageIter sub2;
836                         uint32_t uid;
837
838                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
839                                 goto oom;
840
841                         uid = user->uid;
842
843                         p = user_bus_path(user);
844                         if (!p)
845                                 goto oom;
846
847                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
848                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
849                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
850                                 free(p);
851                                 goto oom;
852                         }
853
854                         free(p);
855
856                         if (!dbus_message_iter_close_container(&sub, &sub2))
857                                 goto oom;
858                 }
859
860                 if (!dbus_message_iter_close_container(&iter, &sub))
861                         goto oom;
862
863         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
864                 char *p;
865                 Seat *seat;
866                 Iterator i;
867                 DBusMessageIter iter, sub;
868
869                 reply = dbus_message_new_method_return(message);
870                 if (!reply)
871                         goto oom;
872
873                 dbus_message_iter_init_append(reply, &iter);
874
875                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
876                         goto oom;
877
878                 HASHMAP_FOREACH(seat, m->seats, i) {
879                         DBusMessageIter sub2;
880
881                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
882                                 goto oom;
883
884                         p = seat_bus_path(seat);
885                         if (!p)
886                                 goto oom;
887
888                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
889                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
890                                 free(p);
891                                 goto oom;
892                         }
893
894                         free(p);
895
896                         if (!dbus_message_iter_close_container(&sub, &sub2))
897                                 goto oom;
898                 }
899
900                 if (!dbus_message_iter_close_container(&iter, &sub))
901                         goto oom;
902
903         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
904
905                 r = bus_manager_create_session(m, message, &reply);
906                 if (r == -ENOMEM)
907                         goto oom;
908
909                 if (r < 0)
910                         return bus_send_error_reply(connection, message, &error, r);
911
912         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
913                 const char *name;
914                 Session *session;
915
916                 if (!dbus_message_get_args(
917                                     message,
918                                     &error,
919                                     DBUS_TYPE_STRING, &name,
920                                     DBUS_TYPE_INVALID))
921                         return bus_send_error_reply(connection, message, &error, -EINVAL);
922
923                 session = hashmap_get(m->sessions, name);
924                 if (!session)
925                         return bus_send_error_reply(connection, message, &error, -ENOENT);
926
927                 r = session_activate(session);
928                 if (r < 0)
929                         return bus_send_error_reply(connection, message, NULL, r);
930
931                 reply = dbus_message_new_method_return(message);
932                 if (!reply)
933                         goto oom;
934
935         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
936                 const char *name;
937                 Session *session;
938
939                 if (!dbus_message_get_args(
940                                     message,
941                                     &error,
942                                     DBUS_TYPE_STRING, &name,
943                                     DBUS_TYPE_INVALID))
944                         return bus_send_error_reply(connection, message, &error, -EINVAL);
945
946                 session = hashmap_get(m->sessions, name);
947                 if (!session)
948                         return bus_send_error_reply(connection, message, &error, -ENOENT);
949
950                 r = session_stop(session);
951                 if (r < 0)
952                         return bus_send_error_reply(connection, message, NULL, r);
953
954                 reply = dbus_message_new_method_return(message);
955                 if (!reply)
956                         goto oom;
957
958         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
959                 uint32_t uid;
960                 User *user;
961
962                 if (!dbus_message_get_args(
963                                     message,
964                                     &error,
965                                     DBUS_TYPE_UINT32, &uid,
966                                     DBUS_TYPE_INVALID))
967                         return bus_send_error_reply(connection, message, &error, -EINVAL);
968
969                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
970                 if (!user)
971                         return bus_send_error_reply(connection, message, &error, -ENOENT);
972
973                 r = user_stop(user);
974                 if (r < 0)
975                         return bus_send_error_reply(connection, message, NULL, r);
976
977                 reply = dbus_message_new_method_return(message);
978                 if (!reply)
979                         goto oom;
980
981         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
982                 const char *name;
983                 Seat *seat;
984
985                 if (!dbus_message_get_args(
986                                     message,
987                                     &error,
988                                     DBUS_TYPE_STRING, &name,
989                                     DBUS_TYPE_INVALID))
990                         return bus_send_error_reply(connection, message, &error, -EINVAL);
991
992                 seat = hashmap_get(m->seats, name);
993                 if (!seat)
994                         return bus_send_error_reply(connection, message, &error, -ENOENT);
995
996                 r = seat_stop_sessions(seat);
997                 if (r < 0)
998                         return bus_send_error_reply(connection, message, NULL, r);
999
1000                 reply = dbus_message_new_method_return(message);
1001                 if (!reply)
1002                         goto oom;
1003
1004         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1005                 uint32_t uid;
1006                 struct passwd *pw;
1007                 dbus_bool_t b, interactive;
1008                 char *path;
1009
1010                 if (!dbus_message_get_args(
1011                                     message,
1012                                     &error,
1013                                     DBUS_TYPE_UINT32, &uid,
1014                                     DBUS_TYPE_BOOLEAN, &b,
1015                                     DBUS_TYPE_BOOLEAN, &interactive,
1016                                     DBUS_TYPE_INVALID))
1017                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1018
1019                 errno = 0;
1020                 pw = getpwuid(uid);
1021                 if (!pw)
1022                         return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1023
1024                 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1025                 if (r < 0)
1026                         return bus_send_error_reply(connection, message, &error, r);
1027
1028                 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1029                 if (r < 0)
1030                         return bus_send_error_reply(connection, message, &error, r);
1031
1032                 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1033                 if (!path)
1034                         goto oom;
1035
1036                 if (b) {
1037                         User *u;
1038
1039                         r = touch(path);
1040                         free(path);
1041
1042                         if (r < 0)
1043                                 return bus_send_error_reply(connection, message, &error, r);
1044
1045                         if (manager_add_user_by_uid(m, uid, &u) >= 0)
1046                                 user_start(u);
1047
1048                 } else {
1049                         User *u;
1050
1051                         r = unlink(path);
1052                         free(path);
1053
1054                         if (r < 0 && errno != ENOENT)
1055                                 return bus_send_error_reply(connection, message, &error, -errno);
1056
1057                         u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1058                         if (u)
1059                                 user_add_to_gc_queue(u);
1060                 }
1061
1062                 reply = dbus_message_new_method_return(message);
1063                 if (!reply)
1064                         goto oom;
1065
1066         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1067                 const char *sysfs, *seat;
1068                 dbus_bool_t interactive;
1069
1070                 if (!dbus_message_get_args(
1071                                     message,
1072                                     &error,
1073                                     DBUS_TYPE_STRING, &seat,
1074                                     DBUS_TYPE_STRING, &sysfs,
1075                                     DBUS_TYPE_BOOLEAN, &interactive,
1076                                     DBUS_TYPE_INVALID))
1077                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1078
1079                 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1080                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1081
1082                 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1083                 if (r < 0)
1084                         return bus_send_error_reply(connection, message, &error, r);
1085
1086                 r = attach_device(m, seat, sysfs);
1087                 if (r < 0)
1088                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1089
1090                 reply = dbus_message_new_method_return(message);
1091                 if (!reply)
1092                         goto oom;
1093
1094         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1095                 char *introspection = NULL;
1096                 FILE *f;
1097                 Iterator i;
1098                 Session *session;
1099                 Seat *seat;
1100                 User *user;
1101                 size_t size;
1102                 char *p;
1103
1104                 if (!(reply = dbus_message_new_method_return(message)))
1105                         goto oom;
1106
1107                 /* We roll our own introspection code here, instead of
1108                  * relying on bus_default_message_handler() because we
1109                  * need to generate our introspection string
1110                  * dynamically. */
1111
1112                 if (!(f = open_memstream(&introspection, &size)))
1113                         goto oom;
1114
1115                 fputs(INTROSPECTION_BEGIN, f);
1116
1117                 HASHMAP_FOREACH(seat, m->seats, i) {
1118                         p = bus_path_escape(seat->id);
1119
1120                         if (p) {
1121                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
1122                                 free(p);
1123                         }
1124                 }
1125
1126                 HASHMAP_FOREACH(user, m->users, i)
1127                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1128
1129                 HASHMAP_FOREACH(session, m->sessions, i) {
1130                         p = bus_path_escape(session->id);
1131
1132                         if (p) {
1133                                 fprintf(f, "<node name=\"session/%s\"/>", p);
1134                                 free(p);
1135                         }
1136                 }
1137
1138                 fputs(INTROSPECTION_END, f);
1139
1140                 if (ferror(f)) {
1141                         fclose(f);
1142                         free(introspection);
1143                         goto oom;
1144                 }
1145
1146                 fclose(f);
1147
1148                 if (!introspection)
1149                         goto oom;
1150
1151                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1152                         free(introspection);
1153                         goto oom;
1154                 }
1155
1156                 free(introspection);
1157         } else
1158                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1159
1160         if (reply) {
1161                 if (!dbus_connection_send(connection, reply, NULL))
1162                         goto oom;
1163
1164                 dbus_message_unref(reply);
1165         }
1166
1167         return DBUS_HANDLER_RESULT_HANDLED;
1168
1169 oom:
1170         if (reply)
1171                 dbus_message_unref(reply);
1172
1173         dbus_error_free(&error);
1174
1175         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1176 }
1177
1178 const DBusObjectPathVTable bus_manager_vtable = {
1179         .message_function = manager_message_handler
1180 };
1181
1182 DBusHandlerResult bus_message_filter(
1183                 DBusConnection *connection,
1184                 DBusMessage *message,
1185                 void *userdata) {
1186
1187         Manager *m = userdata;
1188         DBusError error;
1189
1190         assert(m);
1191         assert(connection);
1192         assert(message);
1193
1194         dbus_error_init(&error);
1195
1196         if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1197                 const char *cgroup;
1198
1199                 if (!dbus_message_get_args(message, &error,
1200                                            DBUS_TYPE_STRING, &cgroup,
1201                                            DBUS_TYPE_INVALID))
1202                         log_error("Failed to parse Released message: %s", bus_error_message(&error));
1203                 else
1204                         manager_cgroup_notify_empty(m, cgroup);
1205         }
1206
1207         dbus_error_free(&error);
1208
1209         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1210 }
1211
1212 int manager_send_changed(Manager *manager, const char *properties) {
1213         DBusMessage *m;
1214         int r = -ENOMEM;
1215
1216         assert(manager);
1217
1218         m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1219         if (!m)
1220                 goto finish;
1221
1222         if (!dbus_connection_send(manager->bus, m, NULL))
1223                 goto finish;
1224
1225         r = 0;
1226
1227 finish:
1228         if (m)
1229                 dbus_message_unref(m);
1230
1231         return r;
1232 }