chiark / gitweb /
logind: parse configuration file
[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", "ResetControllers",       bus_property_append_strv,     "as", m->reset_controllers    },
645                 { "org.freedesktop.login1.Manager", "NAutoVTs",               bus_property_append_unsigned, "u",  &m->n_autovts           },
646                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",          bus_property_append_strv,     "as", m->kill_only_users      },
647                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",       bus_property_append_strv,     "as", m->kill_exclude_users   },
648                 { "org.freedesktop.login1.Manager", "KillUserProcesses",      bus_property_append_bool,     "b",  &m->kill_user_processes },
649                 { "org.freedesktop.login1.Manager", "IdleHint",               bus_manager_append_idle_hint, "b",  m                       },
650                 { "org.freedesktop.login1.Manager", "IdleSinceHint",          bus_manager_append_idle_hint_since, "t", m                  },
651                 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m                  },
652                 { NULL, NULL, NULL, NULL, NULL }
653         };
654
655         DBusError error;
656         DBusMessage *reply = NULL;
657         int r;
658
659         assert(connection);
660         assert(message);
661         assert(m);
662
663         dbus_error_init(&error);
664
665         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
666                 const char *name;
667                 char *p;
668                 Session *session;
669                 bool b;
670
671                 if (!dbus_message_get_args(
672                                     message,
673                                     &error,
674                                     DBUS_TYPE_STRING, &name,
675                                     DBUS_TYPE_INVALID))
676                         return bus_send_error_reply(connection, message, &error, -EINVAL);
677
678                 session = hashmap_get(m->sessions, name);
679                 if (!session)
680                         return bus_send_error_reply(connection, message, &error, -ENOENT);
681
682                 reply = dbus_message_new_method_return(message);
683                 if (!reply)
684                         goto oom;
685
686                 p = session_bus_path(session);
687                 if (!p)
688                         goto oom;
689
690                 b = dbus_message_append_args(
691                                 reply,
692                                 DBUS_TYPE_OBJECT_PATH, &p,
693                                 DBUS_TYPE_INVALID);
694                 free(p);
695
696                 if (!b)
697                         goto oom;
698
699         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
700                 uint32_t uid;
701                 char *p;
702                 User *user;
703                 bool b;
704
705                 if (!dbus_message_get_args(
706                                     message,
707                                     &error,
708                                     DBUS_TYPE_UINT32, &uid,
709                                     DBUS_TYPE_INVALID))
710                         return bus_send_error_reply(connection, message, &error, -EINVAL);
711
712                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
713                 if (!user)
714                         return bus_send_error_reply(connection, message, &error, -ENOENT);
715
716                 reply = dbus_message_new_method_return(message);
717                 if (!reply)
718                         goto oom;
719
720                 p = user_bus_path(user);
721                 if (!p)
722                         goto oom;
723
724                 b = dbus_message_append_args(
725                                 reply,
726                                 DBUS_TYPE_OBJECT_PATH, &p,
727                                 DBUS_TYPE_INVALID);
728                 free(p);
729
730                 if (!b)
731                         goto oom;
732
733         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
734                 const char *name;
735                 char *p;
736                 Seat *seat;
737                 bool b;
738
739                 if (!dbus_message_get_args(
740                                     message,
741                                     &error,
742                                     DBUS_TYPE_STRING, &name,
743                                     DBUS_TYPE_INVALID))
744                         return bus_send_error_reply(connection, message, &error, -EINVAL);
745
746                 seat = hashmap_get(m->seats, name);
747                 if (!seat)
748                         return bus_send_error_reply(connection, message, &error, -ENOENT);
749
750                 reply = dbus_message_new_method_return(message);
751                 if (!reply)
752                         goto oom;
753
754                 p = seat_bus_path(seat);
755                 if (!p)
756                         goto oom;
757
758                 b = dbus_message_append_args(
759                                 reply,
760                                 DBUS_TYPE_OBJECT_PATH, &p,
761                                 DBUS_TYPE_INVALID);
762                 free(p);
763
764                 if (!b)
765                         goto oom;
766
767         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
768                 char *p;
769                 Session *session;
770                 Iterator i;
771                 DBusMessageIter iter, sub;
772                 const char *empty = "";
773
774                 reply = dbus_message_new_method_return(message);
775                 if (!reply)
776                         goto oom;
777
778                 dbus_message_iter_init_append(reply, &iter);
779
780                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
781                         goto oom;
782
783                 HASHMAP_FOREACH(session, m->sessions, i) {
784                         DBusMessageIter sub2;
785                         uint32_t uid;
786
787                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
788                                 goto oom;
789
790                         uid = session->user->uid;
791
792                         p = session_bus_path(session);
793                         if (!p)
794                                 goto oom;
795
796                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
797                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
798                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
799                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
800                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
801                                 free(p);
802                                 goto oom;
803                         }
804
805                         free(p);
806
807                         if (!dbus_message_iter_close_container(&sub, &sub2))
808                                 goto oom;
809                 }
810
811                 if (!dbus_message_iter_close_container(&iter, &sub))
812                         goto oom;
813
814         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
815                 char *p;
816                 User *user;
817                 Iterator i;
818                 DBusMessageIter iter, sub;
819
820                 reply = dbus_message_new_method_return(message);
821                 if (!reply)
822                         goto oom;
823
824                 dbus_message_iter_init_append(reply, &iter);
825
826                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
827                         goto oom;
828
829                 HASHMAP_FOREACH(user, m->users, i) {
830                         DBusMessageIter sub2;
831                         uint32_t uid;
832
833                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
834                                 goto oom;
835
836                         uid = user->uid;
837
838                         p = user_bus_path(user);
839                         if (!p)
840                                 goto oom;
841
842                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
843                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
844                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
845                                 free(p);
846                                 goto oom;
847                         }
848
849                         free(p);
850
851                         if (!dbus_message_iter_close_container(&sub, &sub2))
852                                 goto oom;
853                 }
854
855                 if (!dbus_message_iter_close_container(&iter, &sub))
856                         goto oom;
857
858         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
859                 char *p;
860                 Seat *seat;
861                 Iterator i;
862                 DBusMessageIter iter, sub;
863
864                 reply = dbus_message_new_method_return(message);
865                 if (!reply)
866                         goto oom;
867
868                 dbus_message_iter_init_append(reply, &iter);
869
870                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
871                         goto oom;
872
873                 HASHMAP_FOREACH(seat, m->seats, i) {
874                         DBusMessageIter sub2;
875
876                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
877                                 goto oom;
878
879                         p = seat_bus_path(seat);
880                         if (!p)
881                                 goto oom;
882
883                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
884                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
885                                 free(p);
886                                 goto oom;
887                         }
888
889                         free(p);
890
891                         if (!dbus_message_iter_close_container(&sub, &sub2))
892                                 goto oom;
893                 }
894
895                 if (!dbus_message_iter_close_container(&iter, &sub))
896                         goto oom;
897
898         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
899
900                 r = bus_manager_create_session(m, message, &reply);
901                 if (r == -ENOMEM)
902                         goto oom;
903
904                 if (r < 0)
905                         return bus_send_error_reply(connection, message, &error, r);
906
907         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
908                 const char *name;
909                 Session *session;
910
911                 if (!dbus_message_get_args(
912                                     message,
913                                     &error,
914                                     DBUS_TYPE_STRING, &name,
915                                     DBUS_TYPE_INVALID))
916                         return bus_send_error_reply(connection, message, &error, -EINVAL);
917
918                 session = hashmap_get(m->sessions, name);
919                 if (!session)
920                         return bus_send_error_reply(connection, message, &error, -ENOENT);
921
922                 r = session_activate(session);
923                 if (r < 0)
924                         return bus_send_error_reply(connection, message, NULL, r);
925
926                 reply = dbus_message_new_method_return(message);
927                 if (!reply)
928                         goto oom;
929
930         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
931                 const char *name;
932                 Session *session;
933
934                 if (!dbus_message_get_args(
935                                     message,
936                                     &error,
937                                     DBUS_TYPE_STRING, &name,
938                                     DBUS_TYPE_INVALID))
939                         return bus_send_error_reply(connection, message, &error, -EINVAL);
940
941                 session = hashmap_get(m->sessions, name);
942                 if (!session)
943                         return bus_send_error_reply(connection, message, &error, -ENOENT);
944
945                 r = session_stop(session);
946                 if (r < 0)
947                         return bus_send_error_reply(connection, message, NULL, r);
948
949                 reply = dbus_message_new_method_return(message);
950                 if (!reply)
951                         goto oom;
952
953         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
954                 uint32_t uid;
955                 User *user;
956
957                 if (!dbus_message_get_args(
958                                     message,
959                                     &error,
960                                     DBUS_TYPE_UINT32, &uid,
961                                     DBUS_TYPE_INVALID))
962                         return bus_send_error_reply(connection, message, &error, -EINVAL);
963
964                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
965                 if (!user)
966                         return bus_send_error_reply(connection, message, &error, -ENOENT);
967
968                 r = user_stop(user);
969                 if (r < 0)
970                         return bus_send_error_reply(connection, message, NULL, r);
971
972                 reply = dbus_message_new_method_return(message);
973                 if (!reply)
974                         goto oom;
975
976         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
977                 const char *name;
978                 Seat *seat;
979
980                 if (!dbus_message_get_args(
981                                     message,
982                                     &error,
983                                     DBUS_TYPE_STRING, &name,
984                                     DBUS_TYPE_INVALID))
985                         return bus_send_error_reply(connection, message, &error, -EINVAL);
986
987                 seat = hashmap_get(m->seats, name);
988                 if (!seat)
989                         return bus_send_error_reply(connection, message, &error, -ENOENT);
990
991                 r = seat_stop_sessions(seat);
992                 if (r < 0)
993                         return bus_send_error_reply(connection, message, NULL, r);
994
995                 reply = dbus_message_new_method_return(message);
996                 if (!reply)
997                         goto oom;
998
999         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1000                 uint32_t uid;
1001                 struct passwd *pw;
1002                 dbus_bool_t b, interactive;
1003                 char *path;
1004
1005                 if (!dbus_message_get_args(
1006                                     message,
1007                                     &error,
1008                                     DBUS_TYPE_UINT32, &uid,
1009                                     DBUS_TYPE_BOOLEAN, &b,
1010                                     DBUS_TYPE_BOOLEAN, &interactive,
1011                                     DBUS_TYPE_INVALID))
1012                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1013
1014                 errno = 0;
1015                 pw = getpwuid(uid);
1016                 if (!pw)
1017                         return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1018
1019                 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1020                 if (r < 0)
1021                         return bus_send_error_reply(connection, message, &error, r);
1022
1023                 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1024                 if (r < 0)
1025                         return bus_send_error_reply(connection, message, &error, r);
1026
1027                 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1028                 if (!path)
1029                         goto oom;
1030
1031                 if (b) {
1032                         User *u;
1033
1034                         r = touch(path);
1035                         free(path);
1036
1037                         if (r < 0)
1038                                 return bus_send_error_reply(connection, message, &error, r);
1039
1040                         if (manager_add_user_by_uid(m, uid, &u) >= 0)
1041                                 user_start(u);
1042
1043                 } else {
1044                         User *u;
1045
1046                         r = unlink(path);
1047                         free(path);
1048
1049                         if (r < 0 && errno != ENOENT)
1050                                 return bus_send_error_reply(connection, message, &error, -errno);
1051
1052                         u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1053                         if (u)
1054                                 user_add_to_gc_queue(u);
1055                 }
1056
1057                 reply = dbus_message_new_method_return(message);
1058                 if (!reply)
1059                         goto oom;
1060
1061         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1062                 const char *sysfs, *seat;
1063                 dbus_bool_t interactive;
1064
1065                 if (!dbus_message_get_args(
1066                                     message,
1067                                     &error,
1068                                     DBUS_TYPE_STRING, &seat,
1069                                     DBUS_TYPE_STRING, &sysfs,
1070                                     DBUS_TYPE_BOOLEAN, &interactive,
1071                                     DBUS_TYPE_INVALID))
1072                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1073
1074                 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1075                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1076
1077                 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1078                 if (r < 0)
1079                         return bus_send_error_reply(connection, message, &error, r);
1080
1081                 r = attach_device(m, seat, sysfs);
1082                 if (r < 0)
1083                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1084
1085                 reply = dbus_message_new_method_return(message);
1086                 if (!reply)
1087                         goto oom;
1088
1089         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1090                 char *introspection = NULL;
1091                 FILE *f;
1092                 Iterator i;
1093                 Session *session;
1094                 Seat *seat;
1095                 User *user;
1096                 size_t size;
1097                 char *p;
1098
1099                 if (!(reply = dbus_message_new_method_return(message)))
1100                         goto oom;
1101
1102                 /* We roll our own introspection code here, instead of
1103                  * relying on bus_default_message_handler() because we
1104                  * need to generate our introspection string
1105                  * dynamically. */
1106
1107                 if (!(f = open_memstream(&introspection, &size)))
1108                         goto oom;
1109
1110                 fputs(INTROSPECTION_BEGIN, f);
1111
1112                 HASHMAP_FOREACH(seat, m->seats, i) {
1113                         p = bus_path_escape(seat->id);
1114
1115                         if (p) {
1116                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
1117                                 free(p);
1118                         }
1119                 }
1120
1121                 HASHMAP_FOREACH(user, m->users, i)
1122                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1123
1124                 HASHMAP_FOREACH(session, m->sessions, i) {
1125                         p = bus_path_escape(session->id);
1126
1127                         if (p) {
1128                                 fprintf(f, "<node name=\"session/%s\"/>", p);
1129                                 free(p);
1130                         }
1131                 }
1132
1133                 fputs(INTROSPECTION_END, f);
1134
1135                 if (ferror(f)) {
1136                         fclose(f);
1137                         free(introspection);
1138                         goto oom;
1139                 }
1140
1141                 fclose(f);
1142
1143                 if (!introspection)
1144                         goto oom;
1145
1146                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1147                         free(introspection);
1148                         goto oom;
1149                 }
1150
1151                 free(introspection);
1152         } else
1153                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1154
1155         if (reply) {
1156                 if (!dbus_connection_send(connection, reply, NULL))
1157                         goto oom;
1158
1159                 dbus_message_unref(reply);
1160         }
1161
1162         return DBUS_HANDLER_RESULT_HANDLED;
1163
1164 oom:
1165         if (reply)
1166                 dbus_message_unref(reply);
1167
1168         dbus_error_free(&error);
1169
1170         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1171 }
1172
1173 const DBusObjectPathVTable bus_manager_vtable = {
1174         .message_function = manager_message_handler
1175 };
1176
1177 DBusHandlerResult bus_message_filter(
1178                 DBusConnection *connection,
1179                 DBusMessage *message,
1180                 void *userdata) {
1181
1182         Manager *m = userdata;
1183         DBusError error;
1184
1185         assert(m);
1186         assert(connection);
1187         assert(message);
1188
1189         dbus_error_init(&error);
1190
1191         if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1192                 const char *cgroup;
1193
1194                 if (!dbus_message_get_args(message, &error,
1195                                            DBUS_TYPE_STRING, &cgroup,
1196                                            DBUS_TYPE_INVALID))
1197                         log_error("Failed to parse Released message: %s", bus_error_message(&error));
1198                 else
1199                         manager_cgroup_notify_empty(m, cgroup);
1200         }
1201
1202         dbus_error_free(&error);
1203
1204         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1205 }
1206
1207 int manager_send_changed(Manager *manager, const char *properties) {
1208         DBusMessage *m;
1209         int r = -ENOMEM;
1210
1211         assert(manager);
1212
1213         m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1214         if (!m)
1215                 goto finish;
1216
1217         if (!dbus_connection_send(manager->bus, m, NULL))
1218                 goto finish;
1219
1220         r = 0;
1221
1222 finish:
1223         if (m)
1224                 dbus_message_unref(m);
1225
1226         return r;
1227 }