chiark / gitweb /
logind: check whether newly created session is active
[elogind.git] / src / pam-module.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 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 <fcntl.h>
24 #include <sys/file.h>
25 #include <pwd.h>
26 #include <endian.h>
27 #include <sys/capability.h>
28
29 #include <security/pam_modules.h>
30 #include <security/_pam_macros.h>
31 #include <security/pam_modutil.h>
32 #include <security/pam_ext.h>
33 #include <security/pam_misc.h>
34
35 #include "util.h"
36 #include "macro.h"
37 #include "sd-daemon.h"
38 #include "strv.h"
39 #include "dbus-common.h"
40 #include "def.h"
41 #include "socket-util.h"
42
43 static int parse_argv(pam_handle_t *handle,
44                       int argc, const char **argv,
45                       char ***controllers,
46                       char ***reset_controllers,
47                       bool *kill_processes,
48                       char ***kill_only_users,
49                       char ***kill_exclude_users,
50                       bool *debug) {
51
52         unsigned i;
53         bool reset_controller_set = false;
54         bool kill_exclude_users_set = false;
55
56         assert(argc >= 0);
57         assert(argc == 0 || argv);
58
59         for (i = 0; i < (unsigned) argc; i++) {
60                 int k;
61
62                 if (startswith(argv[i], "kill-processes=")) {
63                         if ((k = parse_boolean(argv[i] + 15)) < 0) {
64                                 pam_syslog(handle, LOG_ERR, "Failed to parse kill-processes= argument.");
65                                 return k;
66                         }
67
68                         if (kill_processes)
69                                 *kill_processes = k;
70
71                 } else if (startswith(argv[i], "kill-session=")) {
72                         /* As compatibility for old versions */
73
74                         if ((k = parse_boolean(argv[i] + 13)) < 0) {
75                                 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
76                                 return k;
77                         }
78
79                         if (kill_processes)
80                                 *kill_processes = k;
81
82                 } else if (startswith(argv[i], "controllers=")) {
83
84                         if (controllers) {
85                                 char **l;
86
87                                 if (!(l = strv_split(argv[i] + 12, ","))) {
88                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
89                                         return -ENOMEM;
90                                 }
91
92                                 strv_free(*controllers);
93                                 *controllers = l;
94                         }
95
96                 } else if (startswith(argv[i], "reset-controllers=")) {
97
98                         if (reset_controllers) {
99                                 char **l;
100
101                                 if (!(l = strv_split(argv[i] + 18, ","))) {
102                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
103                                         return -ENOMEM;
104                                 }
105
106                                 strv_free(*reset_controllers);
107                                 *reset_controllers = l;
108                         }
109
110                         reset_controller_set = true;
111
112                 } else if (startswith(argv[i], "kill-only-users=")) {
113
114                         if (kill_only_users) {
115                                 char **l;
116
117                                 if (!(l = strv_split(argv[i] + 16, ","))) {
118                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
119                                         return -ENOMEM;
120                                 }
121
122                                 strv_free(*kill_only_users);
123                                 *kill_only_users = l;
124                         }
125
126                 } else if (startswith(argv[i], "kill-exclude-users=")) {
127
128                         if (kill_exclude_users) {
129                                 char **l;
130
131                                 if (!(l = strv_split(argv[i] + 19, ","))) {
132                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
133                                         return -ENOMEM;
134                                 }
135
136                                 strv_free(*kill_exclude_users);
137                                 *kill_exclude_users = l;
138                         }
139
140                         kill_exclude_users_set = true;
141
142                 } else if (startswith(argv[i], "debug=")) {
143                         if ((k = parse_boolean(argv[i] + 6)) < 0) {
144                                 pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument.");
145                                 return k;
146                         }
147
148                         if (debug)
149                                 *debug = k;
150
151                 } else if (startswith(argv[i], "create-session=") ||
152                            startswith(argv[i], "kill-user=")) {
153
154                         pam_syslog(handle, LOG_WARNING, "Option %s not supported anymore, ignoring.", argv[i]);
155
156                 } else {
157                         pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
158                         return -EINVAL;
159                 }
160         }
161
162         if (!reset_controller_set && reset_controllers) {
163                 char **l;
164
165                 if (!(l = strv_new("cpu", NULL))) {
166                         pam_syslog(handle, LOG_ERR, "Out of memory");
167                         return -ENOMEM;
168                 }
169
170                 *reset_controllers = l;
171         }
172
173         if (controllers)
174                 strv_remove(*controllers, SYSTEMD_CGROUP_CONTROLLER);
175
176         if (reset_controllers)
177                 strv_remove(*reset_controllers, SYSTEMD_CGROUP_CONTROLLER);
178
179         if (!kill_exclude_users_set && kill_exclude_users) {
180                 char **l;
181
182                 if (!(l = strv_new("root", NULL))) {
183                         pam_syslog(handle, LOG_ERR, "Out of memory");
184                         return -ENOMEM;
185                 }
186
187                 *kill_exclude_users = l;
188         }
189
190         return 0;
191 }
192
193 static int get_user_data(
194                 pam_handle_t *handle,
195                 const char **ret_username,
196                 struct passwd **ret_pw) {
197
198         const char *username = NULL;
199         struct passwd *pw = NULL;
200         int r;
201         bool have_loginuid = false;
202         char *s;
203
204         assert(handle);
205         assert(ret_username);
206         assert(ret_pw);
207
208         if (have_effective_cap(CAP_AUDIT_CONTROL) > 0) {
209                 /* Only use audit login uid if we are executed with
210                  * sufficient capabilities so that pam_loginuid could
211                  * do its job. If we are lacking the CAP_AUDIT_CONTROL
212                  * capabality we most likely are being run in a
213                  * container and /proc/self/loginuid is useless since
214                  * it probably contains a uid of the host system. */
215
216                 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
217                         uint32_t u;
218
219                         r = safe_atou32(s, &u);
220                         free(s);
221
222                         if (r >= 0 && u != (uint32_t) -1 && u > 0) {
223                                 have_loginuid = true;
224                                 pw = pam_modutil_getpwuid(handle, u);
225                         }
226                 }
227         }
228
229         if (!have_loginuid) {
230                 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
231                         pam_syslog(handle, LOG_ERR, "Failed to get user name.");
232                         return r;
233                 }
234
235                 if (!username || !*username) {
236                         pam_syslog(handle, LOG_ERR, "User name not valid.");
237                         return PAM_AUTH_ERR;
238                 }
239
240                 pw = pam_modutil_getpwnam(handle, username);
241         }
242
243         if (!pw) {
244                 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
245                 return PAM_USER_UNKNOWN;
246         }
247
248         *ret_pw = pw;
249         *ret_username = username ? username : pw->pw_name;
250
251         return PAM_SUCCESS;
252 }
253
254 static bool check_user_lists(
255                 pam_handle_t *handle,
256                 uid_t uid,
257                 char **kill_only_users,
258                 char **kill_exclude_users) {
259
260         const char *name = NULL;
261         char **l;
262
263         assert(handle);
264
265         if (uid == 0)
266                 name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */
267         else {
268                 struct passwd *pw;
269
270                 pw = pam_modutil_getpwuid(handle, uid);
271                 if (pw)
272                         name = pw->pw_name;
273         }
274
275         STRV_FOREACH(l, kill_exclude_users) {
276                 uint32_t id;
277
278                 if (safe_atou32(*l, &id) >= 0)
279                         if ((uid_t) id == uid)
280                                 return false;
281
282                 if (name && streq(name, *l))
283                         return false;
284         }
285
286         if (strv_isempty(kill_only_users))
287                 return true;
288
289         STRV_FOREACH(l, kill_only_users) {
290                 uint32_t id;
291
292                 if (safe_atou32(*l, &id) >= 0)
293                         if ((uid_t) id == uid)
294                                 return true;
295
296                 if (name && streq(name, *l))
297                         return true;
298         }
299
300         return false;
301 }
302
303 static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
304         char *p = NULL;
305         int r;
306         int fd;
307         union sockaddr_union sa;
308         struct ucred ucred;
309         socklen_t l;
310         char *tty;
311         int v;
312
313         assert(display);
314         assert(seat);
315         assert(vtnr);
316
317         /* We deduce the X11 socket from the display name, then use
318          * SO_PEERCRED to determine the X11 server process, ask for
319          * the controlling tty of that and if it's a VC then we know
320          * the seat and the virtual terminal. Sounds ugly, is only
321          * semi-ugly. */
322
323         r = socket_from_display(display, &p);
324         if (r < 0)
325                 return r;
326
327         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
328         if (fd < 0) {
329                 free(p);
330                 return -errno;
331         }
332
333         zero(sa);
334         sa.un.sun_family = AF_UNIX;
335         strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
336         free(p);
337
338         if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
339                 close_nointr_nofail(fd);
340                 return -errno;
341         }
342
343         l = sizeof(ucred);
344         r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
345         close_nointr_nofail(fd);
346
347         if (r < 0)
348                 return -errno;
349
350         r = get_ctty(ucred.pid, NULL, &tty);
351         if (r < 0)
352                 return r;
353
354         v = vtnr_from_tty(tty);
355         free(tty);
356
357         if (v < 0)
358                 return v;
359         else if (v == 0)
360                 return -ENOENT;
361
362         *seat = "seat0";
363         *vtnr = (uint32_t) v;
364
365         return 0;
366 }
367
368 _public_ PAM_EXTERN int pam_sm_open_session(
369                 pam_handle_t *handle,
370                 int flags,
371                 int argc, const char **argv) {
372
373         struct passwd *pw;
374         bool kill_processes = false, debug = false;
375         const char *username, *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type, *cvtnr = NULL;
376         char **controllers = NULL, **reset_controllers = NULL, **kill_only_users = NULL, **kill_exclude_users = NULL;
377         DBusError error;
378         uint32_t uid, pid;
379         DBusMessageIter iter;
380         dbus_bool_t kp;
381         int session_fd = -1;
382         DBusConnection *bus = NULL;
383         DBusMessage *m = NULL, *reply = NULL;
384         dbus_bool_t remote;
385         int r;
386         uint32_t vtnr = 0;
387
388         assert(handle);
389
390         dbus_error_init(&error);
391
392         /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
393
394         /* Make this a NOP on non-systemd systems */
395         if (sd_booted() <= 0)
396                 return PAM_SUCCESS;
397
398         if (parse_argv(handle,
399                        argc, argv,
400                        &controllers, &reset_controllers,
401                        &kill_processes, &kill_only_users, &kill_exclude_users,
402                        &debug) < 0) {
403                 r = PAM_SESSION_ERR;
404                 goto finish;
405         }
406
407         r = get_user_data(handle, &username, &pw);
408         if (r != PAM_SUCCESS)
409                 goto finish;
410
411         if (kill_processes)
412                 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
413
414         dbus_connection_set_change_sigpipe(FALSE);
415
416         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
417         if (!bus) {
418                 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", bus_error_message(&error));
419                 r = PAM_SESSION_ERR;
420                 goto finish;
421         }
422
423         m = dbus_message_new_method_call(
424                         "org.freedesktop.login1",
425                         "/org/freedesktop/login1",
426                         "org.freedesktop.login1.Manager",
427                         "CreateSession");
428
429         if (!m) {
430                 pam_syslog(handle, LOG_ERR, "Could not allocate create session message.");
431                 r = PAM_BUF_ERR;
432                 goto finish;
433         }
434
435         uid = pw->pw_uid;
436         pid = getpid();
437
438         pam_get_item(handle, PAM_SERVICE, (const void**) &service);
439         pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
440         pam_get_item(handle, PAM_TTY, (const void**) &tty);
441         pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
442         pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
443         seat = pam_getenv(handle, "LOGIN_SEAT");
444         cvtnr = pam_getenv(handle, "LOGIN_VTNR");
445
446         service = strempty(service);
447         tty = strempty(tty);
448         display = strempty(display);
449         remote_user = strempty(remote_user);
450         remote_host = strempty(remote_host);
451         seat = strempty(seat);
452
453         if (strchr(tty, ':')) {
454                 /* A tty with a colon is usually an X11 display, place
455                  * there to show up in utmp. We rearrange things and
456                  * don't pretend that an X display was a tty */
457
458                 if (isempty(display))
459                         display = tty;
460                 tty = "";
461         }
462
463         if (!isempty(cvtnr))
464                 safe_atou32(cvtnr, &vtnr);
465
466         if (!isempty(display) && isempty(seat) && vtnr <= 0)
467                 get_seat_from_display(display, &seat, &vtnr);
468
469         type = !isempty(display) ? "x11" :
470                    !isempty(tty) ? "tty" : "other";
471
472         remote = !isempty(remote_host) && !streq(remote_host, "localhost") && !streq(remote_host, "localhost.localdomain");
473
474         if (!dbus_message_append_args(m,
475                                       DBUS_TYPE_UINT32, &uid,
476                                       DBUS_TYPE_UINT32, &pid,
477                                       DBUS_TYPE_STRING, &service,
478                                       DBUS_TYPE_STRING, &type,
479                                       DBUS_TYPE_STRING, &seat,
480                                       DBUS_TYPE_UINT32, &vtnr,
481                                       DBUS_TYPE_STRING, &tty,
482                                       DBUS_TYPE_STRING, &display,
483                                       DBUS_TYPE_BOOLEAN, &remote,
484                                       DBUS_TYPE_STRING, &remote_user,
485                                       DBUS_TYPE_STRING, &remote_host,
486                                       DBUS_TYPE_INVALID)) {
487                 pam_syslog(handle, LOG_ERR, "Could not attach parameters to message.");
488                 r = PAM_BUF_ERR;
489                 goto finish;
490         }
491
492         dbus_message_iter_init_append(m, &iter);
493
494         r = bus_append_strv_iter(&iter, controllers);
495         if (r < 0) {
496                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
497                 r = PAM_BUF_ERR;
498                 goto finish;
499         }
500
501         r = bus_append_strv_iter(&iter, reset_controllers);
502         if (r < 0) {
503                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
504                 r = PAM_BUF_ERR;
505                 goto finish;
506         }
507
508         kp = kill_processes;
509         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &kp)) {
510                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
511                 r = PAM_BUF_ERR;
512                 goto finish;
513         }
514
515         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
516         if (!reply) {
517                 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error));
518                 r = PAM_SESSION_ERR;
519                 goto finish;
520         }
521
522         if (!dbus_message_get_args(reply, &error,
523                                    DBUS_TYPE_STRING, &id,
524                                    DBUS_TYPE_OBJECT_PATH, &object_path,
525                                    DBUS_TYPE_STRING, &runtime_path,
526                                    DBUS_TYPE_UNIX_FD, &session_fd,
527                                    DBUS_TYPE_INVALID)) {
528                 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", bus_error_message(&error));
529                 r = PAM_SESSION_ERR;
530                 goto finish;
531         }
532
533         r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
534         if (r != PAM_SUCCESS) {
535                 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
536                 goto finish;
537         }
538
539         r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
540         if (r != PAM_SUCCESS) {
541                 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
542                 goto finish;
543         }
544
545         if (session_fd >= 0) {
546                 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
547                 if (r != PAM_SUCCESS) {
548                         pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
549                         return r;
550                 }
551         }
552
553         session_fd = -1;
554
555         r = PAM_SUCCESS;
556
557 finish:
558         strv_free(controllers);
559         strv_free(reset_controllers);
560         strv_free(kill_only_users);
561         strv_free(kill_exclude_users);
562
563         dbus_error_free(&error);
564
565         if (bus) {
566                 dbus_connection_close(bus);
567                 dbus_connection_unref(bus);
568         }
569
570         if (m)
571                 dbus_message_unref(m);
572
573         if (reply)
574                 dbus_message_unref(reply);
575
576         if (session_fd >= 0)
577                 close_nointr_nofail(session_fd);
578
579         return r;
580 }
581
582 _public_ PAM_EXTERN int pam_sm_close_session(
583                 pam_handle_t *handle,
584                 int flags,
585                 int argc, const char **argv) {
586
587         const void *p = NULL;
588
589         pam_get_data(handle, "systemd.session-fd", &p);
590
591         if (p)
592                 close_nointr(PTR_TO_INT(p) - 1);
593
594         return PAM_SUCCESS;
595 }