chiark / gitweb /
logind: use named pipes instead of fifos to keep track of sessions so that we can...
[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 fifo_fd = -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                         fifo_fd = session_create_fifo(session);
357                         if (fifo_fd < 0) {
358                                 r = fifo_fd;
359                                 goto fail;
360                         }
361
362                         /* Session already exists, client is probably
363                          * something like "su" which changes uid but
364                          * is still the same audit session */
365
366                         reply = dbus_message_new_method_return(message);
367                         if (!reply) {
368                                 r = -ENOMEM;
369                                 goto fail;
370                         }
371
372                         p = session_bus_path(session);
373                         if (!p) {
374                                 r = -ENOMEM;
375                                 goto fail;
376                         }
377
378                         b = dbus_message_append_args(
379                                         reply,
380                                         DBUS_TYPE_STRING, &session->id,
381                                         DBUS_TYPE_OBJECT_PATH, &p,
382                                         DBUS_TYPE_STRING, &session->user->runtime_path,
383                                         DBUS_TYPE_UNIX_FD, &fifo_fd,
384                                         DBUS_TYPE_INVALID);
385                         free(p);
386
387                         if (!b) {
388                                 r = -ENOMEM;
389                                 goto fail;
390                         }
391
392                         close_nointr_nofail(fifo_fd);
393                         *_reply = reply;
394
395                         return 0;
396                 }
397
398         } else {
399                 do {
400                         free(id);
401                         asprintf(&id, "c%lu", ++m->session_counter);
402
403                         if (!id) {
404                                 r = -ENOMEM;
405                                 goto fail;
406                         }
407
408                 } while (hashmap_get(m->sessions, id));
409         }
410
411         r = manager_add_session(m, user, id, &session);
412         free(id);
413         if (r < 0)
414                 goto fail;
415
416         session->leader = leader;
417         session->audit_id = audit_id;
418         session->type = t;
419         session->remote = remote;
420         session->controllers = controllers;
421         session->reset_controllers = reset_controllers;
422         session->kill_processes = kill_processes;
423         session->vtnr = vtnr;
424
425         controllers = reset_controllers = NULL;
426
427         if (!isempty(tty)) {
428                 session->tty = strdup(tty);
429                 if (!session->tty) {
430                         r = -ENOMEM;
431                         goto fail;
432                 }
433         }
434
435         if (!isempty(display)) {
436                 session->display = strdup(display);
437                 if (!session->display) {
438                         r = -ENOMEM;
439                         goto fail;
440                 }
441         }
442
443         if (!isempty(remote_user)) {
444                 session->remote_user = strdup(remote_user);
445                 if (!session->remote_user) {
446                         r = -ENOMEM;
447                         goto fail;
448                 }
449         }
450
451         if (!isempty(remote_host)) {
452                 session->remote_host = strdup(remote_host);
453                 if (!session->remote_host) {
454                         r = -ENOMEM;
455                         goto fail;
456                 }
457         }
458
459         if (!isempty(service)) {
460                 session->service = strdup(service);
461                 if (!session->service) {
462                         r = -ENOMEM;
463                         goto fail;
464                 }
465         }
466
467         fifo_fd = session_create_fifo(session);
468         if (fifo_fd < 0) {
469                 r = fifo_fd;
470                 goto fail;
471         }
472
473         if (s) {
474                 r = seat_attach_session(s, session);
475                 if (r < 0)
476                         goto fail;
477         }
478
479         r = session_start(session);
480         if (r < 0)
481                 goto fail;
482
483         reply = dbus_message_new_method_return(message);
484         if (!reply) {
485                 r = -ENOMEM;
486                 goto fail;
487         }
488
489         p = session_bus_path(session);
490         if (!p) {
491                 r = -ENOMEM;
492                 goto fail;
493         }
494
495         b = dbus_message_append_args(
496                         reply,
497                         DBUS_TYPE_STRING, &session->id,
498                         DBUS_TYPE_OBJECT_PATH, &p,
499                         DBUS_TYPE_STRING, &session->user->runtime_path,
500                         DBUS_TYPE_UNIX_FD, &fifo_fd,
501                         DBUS_TYPE_INVALID);
502         free(p);
503
504         if (!b) {
505                 r = -ENOMEM;
506                 goto fail;
507         }
508
509         close_nointr_nofail(fifo_fd);
510         *_reply = reply;
511
512         return 0;
513
514 fail:
515         strv_free(controllers);
516         strv_free(reset_controllers);
517
518         if (session)
519                 session_add_to_gc_queue(session);
520
521         if (user)
522                 user_add_to_gc_queue(user);
523
524         if (fifo_fd >= 0)
525                 close_nointr_nofail(fifo_fd);
526
527         if (reply)
528                 dbus_message_unref(reply);
529
530         return r;
531 }
532
533 static bool device_has_tag(struct udev_device *d, const char *tag) {
534         struct udev_list_entry *first, *item;
535
536         assert(d);
537         assert(tag);
538
539         udev_device_get_is_initialized(d);
540
541         first = udev_device_get_tags_list_entry(d);
542         udev_list_entry_foreach(item, first)
543                 if (streq(udev_list_entry_get_name(item), tag))
544                         return true;
545
546         return false;
547 }
548
549 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
550         struct udev_device *d;
551         char *rule = NULL, *file = NULL;
552         const char *id_for_seat;
553         int r;
554         struct udev_enumerate *e;
555         struct udev_list_entry *first, *item;
556
557         assert(m);
558         assert(seat);
559         assert(sysfs);
560
561         d = udev_device_new_from_syspath(m->udev, sysfs);
562         if (!d)
563                 return -ENODEV;
564
565         if (!device_has_tag(d, "seat")) {
566                 r = -ENODEV;
567                 goto finish;
568         }
569
570         id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
571         if (!id_for_seat) {
572                 r = -ENODEV;
573                 goto finish;
574         }
575
576         if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
577                 r = -ENOMEM;
578                 goto finish;
579         }
580
581         if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
582                 r = -ENOMEM;
583                 goto finish;
584         }
585
586         mkdir_p("/etc/udev/rules.d", 0755);
587         r = write_one_line_file(file, rule);
588         if (r < 0)
589                 goto finish;
590
591         e = udev_enumerate_new(m->udev);
592         if (!e) {
593                 r = -ENOMEM;
594                 goto finish;
595         }
596
597         if (udev_enumerate_scan_devices(e) < 0) {
598                 r = -EIO;
599                 goto finish;
600         }
601
602         first = udev_enumerate_get_list_entry(e);
603         udev_list_entry_foreach(item, first) {
604                 char *t;
605                 const char *p;
606
607                 p = udev_list_entry_get_name(item);
608                 if (!path_startswith(p, sysfs))
609                         continue;
610
611                 t = strappend(p, "/uevent");
612                 if (!t) {
613                         r = -ENOMEM;
614                         goto finish;
615                 }
616
617                 write_one_line_file(t, "change");
618                 free(t);
619         }
620
621 finish:
622         free(rule);
623         free(file);
624
625         if (d)
626                 udev_device_unref(d);
627
628         if (e)
629                 udev_enumerate_unref(e);
630
631         return r;
632 }
633
634 static DBusHandlerResult manager_message_handler(
635                 DBusConnection *connection,
636                 DBusMessage *message,
637                 void *userdata) {
638
639         Manager *m = userdata;
640
641         const BusProperty properties[] = {
642                 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy",  bus_property_append_string,   "s",  m->cgroup_path          },
643                 { "org.freedesktop.login1.Manager", "Controllers",            bus_property_append_strv,     "as", m->controllers          },
644                 { "org.freedesktop.login1.Manager", "NAutoVTs",               bus_property_append_unsigned, "u",  &m->n_autovts           },
645                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",          bus_property_append_strv,     "as", m->kill_only_users      },
646                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",       bus_property_append_strv,     "as", m->kill_exclude_users   },
647                 { "org.freedesktop.login1.Manager", "KillUserProcesses",      bus_property_append_bool,     "b",  &m->kill_user_processes },
648                 { "org.freedesktop.login1.Manager", "IdleHint",               bus_manager_append_idle_hint, "b",  m                       },
649                 { "org.freedesktop.login1.Manager", "IdleSinceHint",          bus_manager_append_idle_hint_since, "t", m                  },
650                 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m                  },
651                 { NULL, NULL, NULL, NULL, NULL }
652         };
653
654         DBusError error;
655         DBusMessage *reply = NULL;
656         int r;
657
658         assert(connection);
659         assert(message);
660         assert(m);
661
662         dbus_error_init(&error);
663
664         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
665                 const char *name;
666                 char *p;
667                 Session *session;
668                 bool b;
669
670                 if (!dbus_message_get_args(
671                                     message,
672                                     &error,
673                                     DBUS_TYPE_STRING, &name,
674                                     DBUS_TYPE_INVALID))
675                         return bus_send_error_reply(connection, message, &error, -EINVAL);
676
677                 session = hashmap_get(m->sessions, name);
678                 if (!session)
679                         return bus_send_error_reply(connection, message, &error, -ENOENT);
680
681                 reply = dbus_message_new_method_return(message);
682                 if (!reply)
683                         goto oom;
684
685                 p = session_bus_path(session);
686                 if (!p)
687                         goto oom;
688
689                 b = dbus_message_append_args(
690                                 reply,
691                                 DBUS_TYPE_OBJECT_PATH, &p,
692                                 DBUS_TYPE_INVALID);
693                 free(p);
694
695                 if (!b)
696                         goto oom;
697
698         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
699                 uint32_t uid;
700                 char *p;
701                 User *user;
702                 bool b;
703
704                 if (!dbus_message_get_args(
705                                     message,
706                                     &error,
707                                     DBUS_TYPE_UINT32, &uid,
708                                     DBUS_TYPE_INVALID))
709                         return bus_send_error_reply(connection, message, &error, -EINVAL);
710
711                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
712                 if (!user)
713                         return bus_send_error_reply(connection, message, &error, -ENOENT);
714
715                 reply = dbus_message_new_method_return(message);
716                 if (!reply)
717                         goto oom;
718
719                 p = user_bus_path(user);
720                 if (!p)
721                         goto oom;
722
723                 b = dbus_message_append_args(
724                                 reply,
725                                 DBUS_TYPE_OBJECT_PATH, &p,
726                                 DBUS_TYPE_INVALID);
727                 free(p);
728
729                 if (!b)
730                         goto oom;
731
732         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
733                 const char *name;
734                 char *p;
735                 Seat *seat;
736                 bool b;
737
738                 if (!dbus_message_get_args(
739                                     message,
740                                     &error,
741                                     DBUS_TYPE_STRING, &name,
742                                     DBUS_TYPE_INVALID))
743                         return bus_send_error_reply(connection, message, &error, -EINVAL);
744
745                 seat = hashmap_get(m->seats, name);
746                 if (!seat)
747                         return bus_send_error_reply(connection, message, &error, -ENOENT);
748
749                 reply = dbus_message_new_method_return(message);
750                 if (!reply)
751                         goto oom;
752
753                 p = seat_bus_path(seat);
754                 if (!p)
755                         goto oom;
756
757                 b = dbus_message_append_args(
758                                 reply,
759                                 DBUS_TYPE_OBJECT_PATH, &p,
760                                 DBUS_TYPE_INVALID);
761                 free(p);
762
763                 if (!b)
764                         goto oom;
765
766         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
767                 char *p;
768                 Session *session;
769                 Iterator i;
770                 DBusMessageIter iter, sub;
771                 const char *empty = "";
772
773                 reply = dbus_message_new_method_return(message);
774                 if (!reply)
775                         goto oom;
776
777                 dbus_message_iter_init_append(reply, &iter);
778
779                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
780                         goto oom;
781
782                 HASHMAP_FOREACH(session, m->sessions, i) {
783                         DBusMessageIter sub2;
784                         uint32_t uid;
785
786                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
787                                 goto oom;
788
789                         uid = session->user->uid;
790
791                         p = session_bus_path(session);
792                         if (!p)
793                                 goto oom;
794
795                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
796                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
797                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
798                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
799                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
800                                 free(p);
801                                 goto oom;
802                         }
803
804                         free(p);
805
806                         if (!dbus_message_iter_close_container(&sub, &sub2))
807                                 goto oom;
808                 }
809
810                 if (!dbus_message_iter_close_container(&iter, &sub))
811                         goto oom;
812
813         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
814                 char *p;
815                 User *user;
816                 Iterator i;
817                 DBusMessageIter iter, sub;
818
819                 reply = dbus_message_new_method_return(message);
820                 if (!reply)
821                         goto oom;
822
823                 dbus_message_iter_init_append(reply, &iter);
824
825                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
826                         goto oom;
827
828                 HASHMAP_FOREACH(user, m->users, i) {
829                         DBusMessageIter sub2;
830                         uint32_t uid;
831
832                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
833                                 goto oom;
834
835                         uid = user->uid;
836
837                         p = user_bus_path(user);
838                         if (!p)
839                                 goto oom;
840
841                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
842                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
843                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
844                                 free(p);
845                                 goto oom;
846                         }
847
848                         free(p);
849
850                         if (!dbus_message_iter_close_container(&sub, &sub2))
851                                 goto oom;
852                 }
853
854                 if (!dbus_message_iter_close_container(&iter, &sub))
855                         goto oom;
856
857         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
858                 char *p;
859                 Seat *seat;
860                 Iterator i;
861                 DBusMessageIter iter, sub;
862
863                 reply = dbus_message_new_method_return(message);
864                 if (!reply)
865                         goto oom;
866
867                 dbus_message_iter_init_append(reply, &iter);
868
869                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
870                         goto oom;
871
872                 HASHMAP_FOREACH(seat, m->seats, i) {
873                         DBusMessageIter sub2;
874
875                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
876                                 goto oom;
877
878                         p = seat_bus_path(seat);
879                         if (!p)
880                                 goto oom;
881
882                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
883                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
884                                 free(p);
885                                 goto oom;
886                         }
887
888                         free(p);
889
890                         if (!dbus_message_iter_close_container(&sub, &sub2))
891                                 goto oom;
892                 }
893
894                 if (!dbus_message_iter_close_container(&iter, &sub))
895                         goto oom;
896
897         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
898
899                 r = bus_manager_create_session(m, message, &reply);
900                 if (r == -ENOMEM)
901                         goto oom;
902
903                 if (r < 0)
904                         return bus_send_error_reply(connection, message, &error, r);
905
906         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
907                 const char *name;
908                 Session *session;
909
910                 if (!dbus_message_get_args(
911                                     message,
912                                     &error,
913                                     DBUS_TYPE_STRING, &name,
914                                     DBUS_TYPE_INVALID))
915                         return bus_send_error_reply(connection, message, &error, -EINVAL);
916
917                 session = hashmap_get(m->sessions, name);
918                 if (!session)
919                         return bus_send_error_reply(connection, message, &error, -ENOENT);
920
921                 r = session_activate(session);
922                 if (r < 0)
923                         return bus_send_error_reply(connection, message, NULL, r);
924
925                 reply = dbus_message_new_method_return(message);
926                 if (!reply)
927                         goto oom;
928
929         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
930                 const char *name;
931                 Session *session;
932
933                 if (!dbus_message_get_args(
934                                     message,
935                                     &error,
936                                     DBUS_TYPE_STRING, &name,
937                                     DBUS_TYPE_INVALID))
938                         return bus_send_error_reply(connection, message, &error, -EINVAL);
939
940                 session = hashmap_get(m->sessions, name);
941                 if (!session)
942                         return bus_send_error_reply(connection, message, &error, -ENOENT);
943
944                 r = session_stop(session);
945                 if (r < 0)
946                         return bus_send_error_reply(connection, message, NULL, r);
947
948                 reply = dbus_message_new_method_return(message);
949                 if (!reply)
950                         goto oom;
951
952         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
953                 uint32_t uid;
954                 User *user;
955
956                 if (!dbus_message_get_args(
957                                     message,
958                                     &error,
959                                     DBUS_TYPE_UINT32, &uid,
960                                     DBUS_TYPE_INVALID))
961                         return bus_send_error_reply(connection, message, &error, -EINVAL);
962
963                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
964                 if (!user)
965                         return bus_send_error_reply(connection, message, &error, -ENOENT);
966
967                 r = user_stop(user);
968                 if (r < 0)
969                         return bus_send_error_reply(connection, message, NULL, r);
970
971                 reply = dbus_message_new_method_return(message);
972                 if (!reply)
973                         goto oom;
974
975         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
976                 const char *name;
977                 Seat *seat;
978
979                 if (!dbus_message_get_args(
980                                     message,
981                                     &error,
982                                     DBUS_TYPE_STRING, &name,
983                                     DBUS_TYPE_INVALID))
984                         return bus_send_error_reply(connection, message, &error, -EINVAL);
985
986                 seat = hashmap_get(m->seats, name);
987                 if (!seat)
988                         return bus_send_error_reply(connection, message, &error, -ENOENT);
989
990                 r = seat_stop_sessions(seat);
991                 if (r < 0)
992                         return bus_send_error_reply(connection, message, NULL, r);
993
994                 reply = dbus_message_new_method_return(message);
995                 if (!reply)
996                         goto oom;
997
998         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
999                 uint32_t uid;
1000                 struct passwd *pw;
1001                 dbus_bool_t b, interactive;
1002                 char *path;
1003
1004                 if (!dbus_message_get_args(
1005                                     message,
1006                                     &error,
1007                                     DBUS_TYPE_UINT32, &uid,
1008                                     DBUS_TYPE_BOOLEAN, &b,
1009                                     DBUS_TYPE_BOOLEAN, &interactive,
1010                                     DBUS_TYPE_INVALID))
1011                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1012
1013                 errno = 0;
1014                 pw = getpwuid(uid);
1015                 if (!pw)
1016                         return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1017
1018                 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1019                 if (r < 0)
1020                         return bus_send_error_reply(connection, message, &error, r);
1021
1022                 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1023                 if (r < 0)
1024                         return bus_send_error_reply(connection, message, &error, r);
1025
1026                 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1027                 if (!path)
1028                         goto oom;
1029
1030                 if (b) {
1031                         User *u;
1032
1033                         r = touch(path);
1034                         free(path);
1035
1036                         if (r < 0)
1037                                 return bus_send_error_reply(connection, message, &error, r);
1038
1039                         if (manager_add_user_by_uid(m, uid, &u) >= 0)
1040                                 user_start(u);
1041
1042                 } else {
1043                         User *u;
1044
1045                         r = unlink(path);
1046                         free(path);
1047
1048                         if (r < 0 && errno != ENOENT)
1049                                 return bus_send_error_reply(connection, message, &error, -errno);
1050
1051                         u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1052                         if (u)
1053                                 user_add_to_gc_queue(u);
1054                 }
1055
1056                 reply = dbus_message_new_method_return(message);
1057                 if (!reply)
1058                         goto oom;
1059
1060         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1061                 const char *sysfs, *seat;
1062                 dbus_bool_t interactive;
1063
1064                 if (!dbus_message_get_args(
1065                                     message,
1066                                     &error,
1067                                     DBUS_TYPE_STRING, &seat,
1068                                     DBUS_TYPE_STRING, &sysfs,
1069                                     DBUS_TYPE_BOOLEAN, &interactive,
1070                                     DBUS_TYPE_INVALID))
1071                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1072
1073                 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1074                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1075
1076                 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1077                 if (r < 0)
1078                         return bus_send_error_reply(connection, message, &error, r);
1079
1080                 r = attach_device(m, seat, sysfs);
1081                 if (r < 0)
1082                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1083
1084                 reply = dbus_message_new_method_return(message);
1085                 if (!reply)
1086                         goto oom;
1087
1088         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1089                 char *introspection = NULL;
1090                 FILE *f;
1091                 Iterator i;
1092                 Session *session;
1093                 Seat *seat;
1094                 User *user;
1095                 size_t size;
1096                 char *p;
1097
1098                 if (!(reply = dbus_message_new_method_return(message)))
1099                         goto oom;
1100
1101                 /* We roll our own introspection code here, instead of
1102                  * relying on bus_default_message_handler() because we
1103                  * need to generate our introspection string
1104                  * dynamically. */
1105
1106                 if (!(f = open_memstream(&introspection, &size)))
1107                         goto oom;
1108
1109                 fputs(INTROSPECTION_BEGIN, f);
1110
1111                 HASHMAP_FOREACH(seat, m->seats, i) {
1112                         p = bus_path_escape(seat->id);
1113
1114                         if (p) {
1115                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
1116                                 free(p);
1117                         }
1118                 }
1119
1120                 HASHMAP_FOREACH(user, m->users, i)
1121                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1122
1123                 HASHMAP_FOREACH(session, m->sessions, i) {
1124                         p = bus_path_escape(session->id);
1125
1126                         if (p) {
1127                                 fprintf(f, "<node name=\"session/%s\"/>", p);
1128                                 free(p);
1129                         }
1130                 }
1131
1132                 fputs(INTROSPECTION_END, f);
1133
1134                 if (ferror(f)) {
1135                         fclose(f);
1136                         free(introspection);
1137                         goto oom;
1138                 }
1139
1140                 fclose(f);
1141
1142                 if (!introspection)
1143                         goto oom;
1144
1145                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1146                         free(introspection);
1147                         goto oom;
1148                 }
1149
1150                 free(introspection);
1151         } else
1152                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1153
1154         if (reply) {
1155                 if (!dbus_connection_send(connection, reply, NULL))
1156                         goto oom;
1157
1158                 dbus_message_unref(reply);
1159         }
1160
1161         return DBUS_HANDLER_RESULT_HANDLED;
1162
1163 oom:
1164         if (reply)
1165                 dbus_message_unref(reply);
1166
1167         dbus_error_free(&error);
1168
1169         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1170 }
1171
1172 const DBusObjectPathVTable bus_manager_vtable = {
1173         .message_function = manager_message_handler
1174 };
1175
1176 DBusHandlerResult bus_message_filter(
1177                 DBusConnection *connection,
1178                 DBusMessage *message,
1179                 void *userdata) {
1180
1181         Manager *m = userdata;
1182         DBusError error;
1183
1184         assert(m);
1185         assert(connection);
1186         assert(message);
1187
1188         dbus_error_init(&error);
1189
1190         if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1191                 const char *cgroup;
1192
1193                 if (!dbus_message_get_args(message, &error,
1194                                            DBUS_TYPE_STRING, &cgroup,
1195                                            DBUS_TYPE_INVALID))
1196                         log_error("Failed to parse Released message: %s", bus_error_message(&error));
1197                 else
1198                         manager_cgroup_notify_empty(m, cgroup);
1199         }
1200
1201         dbus_error_free(&error);
1202
1203         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1204 }
1205
1206 int manager_send_changed(Manager *manager, const char *properties) {
1207         DBusMessage *m;
1208         int r = -ENOMEM;
1209
1210         assert(manager);
1211
1212         m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1213         if (!m)
1214                 goto finish;
1215
1216         if (!dbus_connection_send(manager->bus, m, NULL))
1217                 goto finish;
1218
1219         r = 0;
1220
1221 finish:
1222         if (m)
1223                 dbus_message_unref(m);
1224
1225         return r;
1226 }