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