chiark / gitweb /
udev: fix test-udev binary
[elogind.git] / src / login / 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 <systemd/sd-daemon.h>
36
37 #include "util.h"
38 #include "macro.h"
39 #include "strv.h"
40 #include "dbus-common.h"
41 #include "def.h"
42 #include "socket-util.h"
43
44 static int parse_argv(pam_handle_t *handle,
45                       int argc, const char **argv,
46                       char ***controllers,
47                       char ***reset_controllers,
48                       bool *kill_processes,
49                       char ***kill_only_users,
50                       char ***kill_exclude_users,
51                       bool *debug) {
52
53         unsigned i;
54
55         assert(argc >= 0);
56         assert(argc == 0 || argv);
57
58         for (i = 0; i < (unsigned) argc; i++) {
59                 int k;
60
61                 if (startswith(argv[i], "kill-session-processes=")) {
62                         if ((k = parse_boolean(argv[i] + 23)) < 0) {
63                                 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session-processes= argument.");
64                                 return k;
65                         }
66
67                         if (kill_processes)
68                                 *kill_processes = k;
69
70                 } else if (startswith(argv[i], "kill-session=")) {
71                         /* As compatibility for old versions */
72
73                         if ((k = parse_boolean(argv[i] + 13)) < 0) {
74                                 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
75                                 return k;
76                         }
77
78                         if (kill_processes)
79                                 *kill_processes = k;
80
81                 } else if (startswith(argv[i], "controllers=")) {
82
83                         if (controllers) {
84                                 char **l;
85
86                                 if (!(l = strv_split(argv[i] + 12, ","))) {
87                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
88                                         return -ENOMEM;
89                                 }
90
91                                 strv_free(*controllers);
92                                 *controllers = l;
93                         }
94
95                 } else if (startswith(argv[i], "reset-controllers=")) {
96
97                         if (reset_controllers) {
98                                 char **l;
99
100                                 if (!(l = strv_split(argv[i] + 18, ","))) {
101                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
102                                         return -ENOMEM;
103                                 }
104
105                                 strv_free(*reset_controllers);
106                                 *reset_controllers = l;
107                         }
108
109                 } else if (startswith(argv[i], "kill-only-users=")) {
110
111                         if (kill_only_users) {
112                                 char **l;
113
114                                 if (!(l = strv_split(argv[i] + 16, ","))) {
115                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
116                                         return -ENOMEM;
117                                 }
118
119                                 strv_free(*kill_only_users);
120                                 *kill_only_users = l;
121                         }
122
123                 } else if (startswith(argv[i], "kill-exclude-users=")) {
124
125                         if (kill_exclude_users) {
126                                 char **l;
127
128                                 if (!(l = strv_split(argv[i] + 19, ","))) {
129                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
130                                         return -ENOMEM;
131                                 }
132
133                                 strv_free(*kill_exclude_users);
134                                 *kill_exclude_users = l;
135                         }
136
137                 } else if (startswith(argv[i], "debug=")) {
138                         if ((k = parse_boolean(argv[i] + 6)) < 0) {
139                                 pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument.");
140                                 return k;
141                         }
142
143                         if (debug)
144                                 *debug = k;
145
146                 } else if (startswith(argv[i], "create-session=") ||
147                            startswith(argv[i], "kill-user=")) {
148
149                         pam_syslog(handle, LOG_WARNING, "Option %s not supported anymore, ignoring.", argv[i]);
150
151                 } else {
152                         pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
153                         return -EINVAL;
154                 }
155         }
156
157         return 0;
158 }
159
160 static int get_user_data(
161                 pam_handle_t *handle,
162                 const char **ret_username,
163                 struct passwd **ret_pw) {
164
165         const char *username = NULL;
166         struct passwd *pw = NULL;
167         uid_t uid;
168         int r;
169
170         assert(handle);
171         assert(ret_username);
172         assert(ret_pw);
173
174         r = audit_loginuid_from_pid(0, &uid);
175         if (r >= 0)
176                 pw = pam_modutil_getpwuid(handle, uid);
177         else {
178                 r = pam_get_user(handle, &username, NULL);
179                 if (r != PAM_SUCCESS) {
180                         pam_syslog(handle, LOG_ERR, "Failed to get user name.");
181                         return r;
182                 }
183
184                 if (isempty(username)) {
185                         pam_syslog(handle, LOG_ERR, "User name not valid.");
186                         return PAM_AUTH_ERR;
187                 }
188
189                 pw = pam_modutil_getpwnam(handle, username);
190         }
191
192         if (!pw) {
193                 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
194                 return PAM_USER_UNKNOWN;
195         }
196
197         *ret_pw = pw;
198         *ret_username = username ? username : pw->pw_name;
199
200         return PAM_SUCCESS;
201 }
202
203 static bool check_user_lists(
204                 pam_handle_t *handle,
205                 uid_t uid,
206                 char **kill_only_users,
207                 char **kill_exclude_users) {
208
209         const char *name = NULL;
210         char **l;
211
212         assert(handle);
213
214         if (uid == 0)
215                 name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */
216         else {
217                 struct passwd *pw;
218
219                 pw = pam_modutil_getpwuid(handle, uid);
220                 if (pw)
221                         name = pw->pw_name;
222         }
223
224         STRV_FOREACH(l, kill_exclude_users) {
225                 uid_t u;
226
227                 if (parse_uid(*l, &u) >= 0)
228                         if (u == uid)
229                                 return false;
230
231                 if (name && streq(name, *l))
232                         return false;
233         }
234
235         if (strv_isempty(kill_only_users))
236                 return true;
237
238         STRV_FOREACH(l, kill_only_users) {
239                 uid_t u;
240
241                 if (parse_uid(*l, &u) >= 0)
242                         if (u == uid)
243                                 return true;
244
245                 if (name && streq(name, *l))
246                         return true;
247         }
248
249         return false;
250 }
251
252 static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
253         char *p = NULL;
254         int r;
255         int fd;
256         union sockaddr_union sa;
257         struct ucred ucred;
258         socklen_t l;
259         char *tty;
260         int v;
261
262         assert(display);
263         assert(vtnr);
264
265         /* We deduce the X11 socket from the display name, then use
266          * SO_PEERCRED to determine the X11 server process, ask for
267          * the controlling tty of that and if it's a VC then we know
268          * the seat and the virtual terminal. Sounds ugly, is only
269          * semi-ugly. */
270
271         r = socket_from_display(display, &p);
272         if (r < 0)
273                 return r;
274
275         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
276         if (fd < 0) {
277                 free(p);
278                 return -errno;
279         }
280
281         zero(sa);
282         sa.un.sun_family = AF_UNIX;
283         strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
284         free(p);
285
286         if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
287                 close_nointr_nofail(fd);
288                 return -errno;
289         }
290
291         l = sizeof(ucred);
292         r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
293         close_nointr_nofail(fd);
294
295         if (r < 0)
296                 return -errno;
297
298         r = get_ctty(ucred.pid, NULL, &tty);
299         if (r < 0)
300                 return r;
301
302         v = vtnr_from_tty(tty);
303         free(tty);
304
305         if (v < 0)
306                 return v;
307         else if (v == 0)
308                 return -ENOENT;
309
310         if (seat)
311                 *seat = "seat0";
312         *vtnr = (uint32_t) v;
313
314         return 0;
315 }
316
317 _public_ PAM_EXTERN int pam_sm_open_session(
318                 pam_handle_t *handle,
319                 int flags,
320                 int argc, const char **argv) {
321
322         struct passwd *pw;
323         bool kill_processes = false, debug = false;
324         const char *username, *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type, *class, *cvtnr = NULL;
325         char **controllers = NULL, **reset_controllers = NULL, **kill_only_users = NULL, **kill_exclude_users = NULL;
326         DBusError error;
327         uint32_t uid, pid;
328         DBusMessageIter iter;
329         dbus_bool_t kp;
330         int session_fd = -1;
331         DBusConnection *bus = NULL;
332         DBusMessage *m = NULL, *reply = NULL;
333         dbus_bool_t remote;
334         int r;
335         uint32_t vtnr = 0;
336
337         assert(handle);
338
339         dbus_error_init(&error);
340
341         /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
342
343         /* Make this a NOP on non-systemd systems */
344         if (sd_booted() <= 0)
345                 return PAM_SUCCESS;
346
347         if (parse_argv(handle,
348                        argc, argv,
349                        &controllers, &reset_controllers,
350                        &kill_processes, &kill_only_users, &kill_exclude_users,
351                        &debug) < 0) {
352                 r = PAM_SESSION_ERR;
353                 goto finish;
354         }
355
356         r = get_user_data(handle, &username, &pw);
357         if (r != PAM_SUCCESS)
358                 goto finish;
359
360         /* Make sure we don't enter a loop by talking to
361          * systemd-logind when it is actually waiting for the
362          * background to finish start-up. If the service is
363          * "systemd-shared" we simply set XDG_RUNTIME_DIR and
364          * leave. */
365
366         pam_get_item(handle, PAM_SERVICE, (const void**) &service);
367         if (streq_ptr(service, "systemd-shared")) {
368                 char *p, *rt = NULL;
369
370                 if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) pw->pw_uid) < 0) {
371                         r = PAM_BUF_ERR;
372                         goto finish;
373                 }
374
375                 r = parse_env_file(p, NEWLINE,
376                                    "RUNTIME", &rt,
377                                    NULL);
378                 free(p);
379
380                 if (r < 0 && r != -ENOENT) {
381                         r = PAM_SESSION_ERR;
382                         free(rt);
383                         goto finish;
384                 }
385
386                 if (rt)  {
387                         r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0);
388                         free(rt);
389
390                         if (r != PAM_SUCCESS) {
391                                 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
392                                 goto finish;
393                         }
394                 }
395
396                 r = PAM_SUCCESS;
397                 goto finish;
398         }
399
400         if (kill_processes)
401                 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
402
403         dbus_connection_set_change_sigpipe(FALSE);
404
405         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
406         if (!bus) {
407                 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", bus_error_message(&error));
408                 r = PAM_SESSION_ERR;
409                 goto finish;
410         }
411
412         m = dbus_message_new_method_call(
413                         "org.freedesktop.login1",
414                         "/org/freedesktop/login1",
415                         "org.freedesktop.login1.Manager",
416                         "CreateSession");
417         if (!m) {
418                 pam_syslog(handle, LOG_ERR, "Could not allocate create session message.");
419                 r = PAM_BUF_ERR;
420                 goto finish;
421         }
422
423         uid = pw->pw_uid;
424         pid = getpid();
425
426         pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
427         pam_get_item(handle, PAM_TTY, (const void**) &tty);
428         pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
429         pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
430         seat = pam_getenv(handle, "XDG_SEAT");
431         cvtnr = pam_getenv(handle, "XDG_VTNR");
432
433         service = strempty(service);
434         tty = strempty(tty);
435         display = strempty(display);
436         remote_user = strempty(remote_user);
437         remote_host = strempty(remote_host);
438         seat = strempty(seat);
439
440         if (strchr(tty, ':')) {
441                 /* A tty with a colon is usually an X11 display, place
442                  * there to show up in utmp. We rearrange things and
443                  * don't pretend that an X display was a tty */
444
445                 if (isempty(display))
446                         display = tty;
447                 tty = "";
448         } else if (streq(tty, "cron")) {
449                 /* cron has been setting PAM_TTY to "cron" for a very long time
450                  * and it cannot stop doing that for compatibility reasons. */
451                 tty = "";
452         }
453
454         if (!isempty(cvtnr))
455                 safe_atou32(cvtnr, &vtnr);
456
457         if (!isempty(display) && vtnr <= 0) {
458                 if (isempty(seat))
459                         get_seat_from_display(display, &seat, &vtnr);
460                 else if (streq(seat, "seat0"))
461                         get_seat_from_display(display, NULL, &vtnr);
462         }
463
464         type = !isempty(display) ? "x11" :
465                    !isempty(tty) ? "tty" : "unspecified";
466
467         class = pam_getenv(handle, "XDG_SESSION_CLASS");
468         if (isempty(class))
469                 class = "user";
470
471         remote = !isempty(remote_host) &&
472                 !streq(remote_host, "localhost") &&
473                 !streq(remote_host, "localhost.localdomain");
474
475         if (!dbus_message_append_args(m,
476                                       DBUS_TYPE_UINT32, &uid,
477                                       DBUS_TYPE_UINT32, &pid,
478                                       DBUS_TYPE_STRING, &service,
479                                       DBUS_TYPE_STRING, &type,
480                                       DBUS_TYPE_STRING, &class,
481                                       DBUS_TYPE_STRING, &seat,
482                                       DBUS_TYPE_UINT32, &vtnr,
483                                       DBUS_TYPE_STRING, &tty,
484                                       DBUS_TYPE_STRING, &display,
485                                       DBUS_TYPE_BOOLEAN, &remote,
486                                       DBUS_TYPE_STRING, &remote_user,
487                                       DBUS_TYPE_STRING, &remote_host,
488                                       DBUS_TYPE_INVALID)) {
489                 pam_syslog(handle, LOG_ERR, "Could not attach parameters to message.");
490                 r = PAM_BUF_ERR;
491                 goto finish;
492         }
493
494         dbus_message_iter_init_append(m, &iter);
495
496         r = bus_append_strv_iter(&iter, controllers);
497         if (r < 0) {
498                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
499                 r = PAM_BUF_ERR;
500                 goto finish;
501         }
502
503         r = bus_append_strv_iter(&iter, reset_controllers);
504         if (r < 0) {
505                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
506                 r = PAM_BUF_ERR;
507                 goto finish;
508         }
509
510         kp = kill_processes;
511         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &kp)) {
512                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
513                 r = PAM_BUF_ERR;
514                 goto finish;
515         }
516
517         if (debug)
518                 pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
519                            "uid=%u pid=%u service=%s type=%s seat=%s vtnr=%u tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
520                            uid, pid, service, type, seat, vtnr, tty, display, yes_no(remote), remote_user, remote_host);
521
522         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
523         if (!reply) {
524                 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error));
525                 r = PAM_SESSION_ERR;
526                 goto finish;
527         }
528
529         if (!dbus_message_get_args(reply, &error,
530                                    DBUS_TYPE_STRING, &id,
531                                    DBUS_TYPE_OBJECT_PATH, &object_path,
532                                    DBUS_TYPE_STRING, &runtime_path,
533                                    DBUS_TYPE_UNIX_FD, &session_fd,
534                                    DBUS_TYPE_STRING, &seat,
535                                    DBUS_TYPE_UINT32, &vtnr,
536                                    DBUS_TYPE_INVALID)) {
537                 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", bus_error_message(&error));
538                 r = PAM_SESSION_ERR;
539                 goto finish;
540         }
541
542         if (debug)
543                 pam_syslog(handle, LOG_DEBUG, "Reply from logind: "
544                            "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u",
545                            id, object_path, runtime_path, session_fd, seat, vtnr);
546
547         r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
548         if (r != PAM_SUCCESS) {
549                 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
550                 goto finish;
551         }
552
553         r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
554         if (r != PAM_SUCCESS) {
555                 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
556                 goto finish;
557         }
558
559         if (!isempty(seat)) {
560                 r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0);
561                 if (r != PAM_SUCCESS) {
562                         pam_syslog(handle, LOG_ERR, "Failed to set seat.");
563                         goto finish;
564                 }
565         }
566
567         if (vtnr > 0) {
568                 char buf[11];
569                 snprintf(buf, sizeof(buf), "%u", vtnr);
570                 char_array_0(buf);
571
572                 r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
573                 if (r != PAM_SUCCESS) {
574                         pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number.");
575                         goto finish;
576                 }
577         }
578
579         if (session_fd >= 0) {
580                 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
581                 if (r != PAM_SUCCESS) {
582                         pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
583                         return r;
584                 }
585         }
586
587         session_fd = -1;
588
589         r = PAM_SUCCESS;
590
591 finish:
592         strv_free(controllers);
593         strv_free(reset_controllers);
594         strv_free(kill_only_users);
595         strv_free(kill_exclude_users);
596
597         dbus_error_free(&error);
598
599         if (bus) {
600                 dbus_connection_close(bus);
601                 dbus_connection_unref(bus);
602         }
603
604         if (m)
605                 dbus_message_unref(m);
606
607         if (reply)
608                 dbus_message_unref(reply);
609
610         if (session_fd >= 0)
611                 close_nointr_nofail(session_fd);
612
613         return r;
614 }
615
616 _public_ PAM_EXTERN int pam_sm_close_session(
617                 pam_handle_t *handle,
618                 int flags,
619                 int argc, const char **argv) {
620
621         const void *p = NULL;
622         const char *id;
623         DBusConnection *bus = NULL;
624         DBusMessage *m = NULL, *reply = NULL;
625         DBusError error;
626         int r;
627
628         assert(handle);
629
630         dbus_error_init(&error);
631
632         id = pam_getenv(handle, "XDG_SESSION_ID");
633         if (id) {
634
635                 /* Before we go and close the FIFO we need to tell
636                  * logind that this is a clean session shutdown, so
637                  * that it doesn't just go and slaughter us
638                  * immediately after closing the fd */
639
640                 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
641                 if (!bus) {
642                         pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", bus_error_message(&error));
643                         r = PAM_SESSION_ERR;
644                         goto finish;
645                 }
646
647                 m = dbus_message_new_method_call(
648                                 "org.freedesktop.login1",
649                                 "/org/freedesktop/login1",
650                                 "org.freedesktop.login1.Manager",
651                                 "ReleaseSession");
652                 if (!m) {
653                         pam_syslog(handle, LOG_ERR, "Could not allocate release session message.");
654                         r = PAM_BUF_ERR;
655                         goto finish;
656                 }
657
658                 if (!dbus_message_append_args(m,
659                                               DBUS_TYPE_STRING, &id,
660                                               DBUS_TYPE_INVALID)) {
661                         pam_syslog(handle, LOG_ERR, "Could not attach parameters to message.");
662                         r = PAM_BUF_ERR;
663                         goto finish;
664                 }
665
666                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
667                 if (!reply) {
668                         pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error));
669                         r = PAM_SESSION_ERR;
670                         goto finish;
671                 }
672         }
673
674         r = PAM_SUCCESS;
675
676 finish:
677         pam_get_data(handle, "systemd.session-fd", &p);
678         if (p)
679                 close_nointr(PTR_TO_INT(p) - 1);
680
681         dbus_error_free(&error);
682
683         if (bus) {
684                 dbus_connection_close(bus);
685                 dbus_connection_unref(bus);
686         }
687
688         if (m)
689                 dbus_message_unref(m);
690
691         if (reply)
692                 dbus_message_unref(reply);
693
694         return r;
695 }