]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/pam-module.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
27 #include <sys/capability.h>
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>
35 #include <systemd/sd-daemon.h>
40 #include "dbus-common.h"
42 #include "socket-util.h"
44 static int parse_argv(pam_handle_t
*handle
,
45 int argc
, const char **argv
,
47 char ***reset_controllers
,
49 char ***kill_only_users
,
50 char ***kill_exclude_users
,
56 assert(argc
== 0 || argv
);
58 for (i
= 0; i
< (unsigned) argc
; i
++) {
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.");
70 } else if (startswith(argv
[i
], "kill-session=")) {
71 /* As compatibility for old versions */
73 if ((k
= parse_boolean(argv
[i
] + 13)) < 0) {
74 pam_syslog(handle
, LOG_ERR
, "Failed to parse kill-session= argument.");
81 } else if (startswith(argv
[i
], "controllers=")) {
86 if (!(l
= strv_split(argv
[i
] + 12, ","))) {
87 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
91 strv_free(*controllers
);
95 } else if (startswith(argv
[i
], "reset-controllers=")) {
97 if (reset_controllers
) {
100 if (!(l
= strv_split(argv
[i
] + 18, ","))) {
101 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
105 strv_free(*reset_controllers
);
106 *reset_controllers
= l
;
109 } else if (startswith(argv
[i
], "kill-only-users=")) {
111 if (kill_only_users
) {
114 if (!(l
= strv_split(argv
[i
] + 16, ","))) {
115 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
119 strv_free(*kill_only_users
);
120 *kill_only_users
= l
;
123 } else if (startswith(argv
[i
], "kill-exclude-users=")) {
125 if (kill_exclude_users
) {
128 if (!(l
= strv_split(argv
[i
] + 19, ","))) {
129 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
133 strv_free(*kill_exclude_users
);
134 *kill_exclude_users
= l
;
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.");
146 } else if (startswith(argv
[i
], "create-session=") ||
147 startswith(argv
[i
], "kill-user=")) {
149 pam_syslog(handle
, LOG_WARNING
, "Option %s not supported anymore, ignoring.", argv
[i
]);
152 pam_syslog(handle
, LOG_ERR
, "Unknown parameter '%s'.", argv
[i
]);
160 static int get_user_data(
161 pam_handle_t
*handle
,
162 const char **ret_username
,
163 struct passwd
**ret_pw
) {
165 const char *username
= NULL
;
166 struct passwd
*pw
= NULL
;
171 assert(ret_username
);
174 r
= audit_loginuid_from_pid(0, &uid
);
176 pw
= pam_modutil_getpwuid(handle
, uid
);
178 r
= pam_get_user(handle
, &username
, NULL
);
179 if (r
!= PAM_SUCCESS
) {
180 pam_syslog(handle
, LOG_ERR
, "Failed to get user name.");
184 if (isempty(username
)) {
185 pam_syslog(handle
, LOG_ERR
, "User name not valid.");
189 pw
= pam_modutil_getpwnam(handle
, username
);
193 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
194 return PAM_USER_UNKNOWN
;
198 *ret_username
= username
? username
: pw
->pw_name
;
203 static bool check_user_lists(
204 pam_handle_t
*handle
,
206 char **kill_only_users
,
207 char **kill_exclude_users
) {
209 const char *name
= NULL
;
215 name
= "root"; /* Avoid obvious NSS requests, to suppress network traffic */
219 pw
= pam_modutil_getpwuid(handle
, uid
);
224 STRV_FOREACH(l
, kill_exclude_users
) {
227 if (parse_uid(*l
, &u
) >= 0)
231 if (name
&& streq(name
, *l
))
235 if (strv_isempty(kill_only_users
))
238 STRV_FOREACH(l
, kill_only_users
) {
241 if (parse_uid(*l
, &u
) >= 0)
245 if (name
&& streq(name
, *l
))
252 static int get_seat_from_display(const char *display
, const char **seat
, uint32_t *vtnr
) {
256 union sockaddr_union sa
;
266 /* We deduce the X11 socket from the display name, then use
267 * SO_PEERCRED to determine the X11 server process, ask for
268 * the controlling tty of that and if it's a VC then we know
269 * the seat and the virtual terminal. Sounds ugly, is only
272 r
= socket_from_display(display
, &p
);
276 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
283 sa
.un
.sun_family
= AF_UNIX
;
284 strncpy(sa
.un
.sun_path
, p
, sizeof(sa
.un
.sun_path
)-1);
287 if (connect(fd
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + strlen(sa
.un
.sun_path
)) < 0) {
288 close_nointr_nofail(fd
);
293 r
= getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
);
294 close_nointr_nofail(fd
);
299 r
= get_ctty(ucred
.pid
, NULL
, &tty
);
303 v
= vtnr_from_tty(tty
);
312 *vtnr
= (uint32_t) v
;
317 _public_ PAM_EXTERN
int pam_sm_open_session(
318 pam_handle_t
*handle
,
320 int argc
, const char **argv
) {
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
, *cvtnr
= NULL
;
325 char **controllers
= NULL
, **reset_controllers
= NULL
, **kill_only_users
= NULL
, **kill_exclude_users
= NULL
;
328 DBusMessageIter iter
;
331 DBusConnection
*bus
= NULL
;
332 DBusMessage
*m
= NULL
, *reply
= NULL
;
339 dbus_error_init(&error
);
341 /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
343 /* Make this a NOP on non-systemd systems */
344 if (sd_booted() <= 0)
347 if (parse_argv(handle
,
349 &controllers
, &reset_controllers
,
350 &kill_processes
, &kill_only_users
, &kill_exclude_users
,
356 r
= get_user_data(handle
, &username
, &pw
);
357 if (r
!= PAM_SUCCESS
)
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
366 pam_get_item(handle
, PAM_SERVICE
, (const void**) &service
);
367 if (streq_ptr(service
, "systemd-shared")) {
370 if (asprintf(&p
, "/run/systemd/users/%lu", (unsigned long) pw
->pw_uid
) < 0) {
375 r
= parse_env_file(p
, NEWLINE
,
380 if (r
< 0 && r
!= -ENOENT
) {
387 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", rt
, 0);
390 if (r
!= PAM_SUCCESS
) {
391 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
401 kill_processes
= check_user_lists(handle
, pw
->pw_uid
, kill_only_users
, kill_exclude_users
);
403 dbus_connection_set_change_sigpipe(FALSE
);
405 bus
= dbus_bus_get_private(DBUS_BUS_SYSTEM
, &error
);
407 pam_syslog(handle
, LOG_ERR
, "Failed to connect to system bus: %s", bus_error_message(&error
));
412 m
= dbus_message_new_method_call(
413 "org.freedesktop.login1",
414 "/org/freedesktop/login1",
415 "org.freedesktop.login1.Manager",
419 pam_syslog(handle
, LOG_ERR
, "Could not allocate create session message.");
427 pam_get_item(handle
, PAM_XDISPLAY
, (const void**) &display
);
428 pam_get_item(handle
, PAM_TTY
, (const void**) &tty
);
429 pam_get_item(handle
, PAM_RUSER
, (const void**) &remote_user
);
430 pam_get_item(handle
, PAM_RHOST
, (const void**) &remote_host
);
431 seat
= pam_getenv(handle
, "XDG_SEAT");
432 cvtnr
= pam_getenv(handle
, "XDG_VTNR");
434 service
= strempty(service
);
436 display
= strempty(display
);
437 remote_user
= strempty(remote_user
);
438 remote_host
= strempty(remote_host
);
439 seat
= strempty(seat
);
441 if (strchr(tty
, ':')) {
442 /* A tty with a colon is usually an X11 display, place
443 * there to show up in utmp. We rearrange things and
444 * don't pretend that an X display was a tty */
446 if (isempty(display
))
449 } else if (streq(tty
, "cron")) {
450 /* cron has been setting PAM_TTY to "cron" for a very long time
451 * and it cannot stop doing that for compatibility reasons. */
456 safe_atou32(cvtnr
, &vtnr
);
458 if (!isempty(display
) && isempty(seat
) && vtnr
<= 0)
459 get_seat_from_display(display
, &seat
, &vtnr
);
461 type
= !isempty(display
) ? "x11" :
462 !isempty(tty
) ? "tty" : "unspecified";
464 remote
= !isempty(remote_host
) && !streq(remote_host
, "localhost") && !streq(remote_host
, "localhost.localdomain");
466 if (!dbus_message_append_args(m
,
467 DBUS_TYPE_UINT32
, &uid
,
468 DBUS_TYPE_UINT32
, &pid
,
469 DBUS_TYPE_STRING
, &service
,
470 DBUS_TYPE_STRING
, &type
,
471 DBUS_TYPE_STRING
, &seat
,
472 DBUS_TYPE_UINT32
, &vtnr
,
473 DBUS_TYPE_STRING
, &tty
,
474 DBUS_TYPE_STRING
, &display
,
475 DBUS_TYPE_BOOLEAN
, &remote
,
476 DBUS_TYPE_STRING
, &remote_user
,
477 DBUS_TYPE_STRING
, &remote_host
,
478 DBUS_TYPE_INVALID
)) {
479 pam_syslog(handle
, LOG_ERR
, "Could not attach parameters to message.");
484 dbus_message_iter_init_append(m
, &iter
);
486 r
= bus_append_strv_iter(&iter
, controllers
);
488 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
493 r
= bus_append_strv_iter(&iter
, reset_controllers
);
495 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
501 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &kp
)) {
502 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
508 pam_syslog(handle
, LOG_DEBUG
, "Asking logind to create session: "
509 "uid=%u pid=%u service=%s type=%s seat=%s vtnr=%u tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
510 uid
, pid
, service
, type
, seat
, vtnr
, tty
, display
, yes_no(remote
), remote_user
, remote_host
);
512 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
514 pam_syslog(handle
, LOG_ERR
, "Failed to create session: %s", bus_error_message(&error
));
519 if (!dbus_message_get_args(reply
, &error
,
520 DBUS_TYPE_STRING
, &id
,
521 DBUS_TYPE_OBJECT_PATH
, &object_path
,
522 DBUS_TYPE_STRING
, &runtime_path
,
523 DBUS_TYPE_UNIX_FD
, &session_fd
,
524 DBUS_TYPE_STRING
, &seat
,
525 DBUS_TYPE_UINT32
, &vtnr
,
526 DBUS_TYPE_INVALID
)) {
527 pam_syslog(handle
, LOG_ERR
, "Failed to parse message: %s", bus_error_message(&error
));
533 pam_syslog(handle
, LOG_DEBUG
, "Reply from logind: "
534 "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u",
535 id
, object_path
, runtime_path
, session_fd
, seat
, vtnr
);
537 r
= pam_misc_setenv(handle
, "XDG_SESSION_ID", id
, 0);
538 if (r
!= PAM_SUCCESS
) {
539 pam_syslog(handle
, LOG_ERR
, "Failed to set session id.");
543 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", runtime_path
, 0);
544 if (r
!= PAM_SUCCESS
) {
545 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
549 if (!isempty(seat
)) {
550 r
= pam_misc_setenv(handle
, "XDG_SEAT", seat
, 0);
551 if (r
!= PAM_SUCCESS
) {
552 pam_syslog(handle
, LOG_ERR
, "Failed to set seat.");
559 snprintf(buf
, sizeof(buf
), "%u", vtnr
);
562 r
= pam_misc_setenv(handle
, "XDG_VTNR", buf
, 0);
563 if (r
!= PAM_SUCCESS
) {
564 pam_syslog(handle
, LOG_ERR
, "Failed to set virtual terminal number.");
569 if (session_fd
>= 0) {
570 r
= pam_set_data(handle
, "systemd.session-fd", INT_TO_PTR(session_fd
+1), NULL
);
571 if (r
!= PAM_SUCCESS
) {
572 pam_syslog(handle
, LOG_ERR
, "Failed to install session fd.");
582 strv_free(controllers
);
583 strv_free(reset_controllers
);
584 strv_free(kill_only_users
);
585 strv_free(kill_exclude_users
);
587 dbus_error_free(&error
);
590 dbus_connection_close(bus
);
591 dbus_connection_unref(bus
);
595 dbus_message_unref(m
);
598 dbus_message_unref(reply
);
601 close_nointr_nofail(session_fd
);
606 _public_ PAM_EXTERN
int pam_sm_close_session(
607 pam_handle_t
*handle
,
609 int argc
, const char **argv
) {
611 const void *p
= NULL
;
613 pam_get_data(handle
, "systemd.session-fd", &p
);
616 close_nointr(PTR_TO_INT(p
) - 1);