]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/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>
37 #include "sd-daemon.h"
39 #include "dbus-common.h"
41 #include "socket-util.h"
43 static int parse_argv(pam_handle_t
*handle
,
44 int argc
, const char **argv
,
46 char ***reset_controllers
,
48 char ***kill_only_users
,
49 char ***kill_exclude_users
,
55 assert(argc
== 0 || argv
);
57 for (i
= 0; i
< (unsigned) argc
; i
++) {
60 if (startswith(argv
[i
], "kill-session-processes=")) {
61 if ((k
= parse_boolean(argv
[i
] + 23)) < 0) {
62 pam_syslog(handle
, LOG_ERR
, "Failed to parse kill-session-processes= argument.");
69 } else if (startswith(argv
[i
], "kill-session=")) {
70 /* As compatibility for old versions */
72 if ((k
= parse_boolean(argv
[i
] + 13)) < 0) {
73 pam_syslog(handle
, LOG_ERR
, "Failed to parse kill-session= argument.");
80 } else if (startswith(argv
[i
], "controllers=")) {
85 if (!(l
= strv_split(argv
[i
] + 12, ","))) {
86 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
90 strv_free(*controllers
);
94 } else if (startswith(argv
[i
], "reset-controllers=")) {
96 if (reset_controllers
) {
99 if (!(l
= strv_split(argv
[i
] + 18, ","))) {
100 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
104 strv_free(*reset_controllers
);
105 *reset_controllers
= l
;
108 } else if (startswith(argv
[i
], "kill-only-users=")) {
110 if (kill_only_users
) {
113 if (!(l
= strv_split(argv
[i
] + 16, ","))) {
114 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
118 strv_free(*kill_only_users
);
119 *kill_only_users
= l
;
122 } else if (startswith(argv
[i
], "kill-exclude-users=")) {
124 if (kill_exclude_users
) {
127 if (!(l
= strv_split(argv
[i
] + 19, ","))) {
128 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
132 strv_free(*kill_exclude_users
);
133 *kill_exclude_users
= l
;
136 } else if (startswith(argv
[i
], "debug=")) {
137 if ((k
= parse_boolean(argv
[i
] + 6)) < 0) {
138 pam_syslog(handle
, LOG_ERR
, "Failed to parse debug= argument.");
145 } else if (startswith(argv
[i
], "create-session=") ||
146 startswith(argv
[i
], "kill-user=")) {
148 pam_syslog(handle
, LOG_WARNING
, "Option %s not supported anymore, ignoring.", argv
[i
]);
151 pam_syslog(handle
, LOG_ERR
, "Unknown parameter '%s'.", argv
[i
]);
159 static int get_user_data(
160 pam_handle_t
*handle
,
161 const char **ret_username
,
162 struct passwd
**ret_pw
) {
164 const char *username
= NULL
;
165 struct passwd
*pw
= NULL
;
167 bool have_loginuid
= false;
171 assert(ret_username
);
174 if (have_effective_cap(CAP_AUDIT_CONTROL
) > 0) {
175 /* Only use audit login uid if we are executed with
176 * sufficient capabilities so that pam_loginuid could
177 * do its job. If we are lacking the CAP_AUDIT_CONTROL
178 * capabality we most likely are being run in a
179 * container and /proc/self/loginuid is useless since
180 * it probably contains a uid of the host system. */
182 if (read_one_line_file("/proc/self/loginuid", &s
) >= 0) {
185 r
= parse_uid(s
, &uid
);
188 if (r
>= 0 && uid
!= (uint32_t) -1) {
189 have_loginuid
= true;
190 pw
= pam_modutil_getpwuid(handle
, uid
);
195 if (!have_loginuid
) {
196 if ((r
= pam_get_user(handle
, &username
, NULL
)) != PAM_SUCCESS
) {
197 pam_syslog(handle
, LOG_ERR
, "Failed to get user name.");
201 if (!username
|| !*username
) {
202 pam_syslog(handle
, LOG_ERR
, "User name not valid.");
206 pw
= pam_modutil_getpwnam(handle
, username
);
210 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
211 return PAM_USER_UNKNOWN
;
215 *ret_username
= username
? username
: pw
->pw_name
;
220 static bool check_user_lists(
221 pam_handle_t
*handle
,
223 char **kill_only_users
,
224 char **kill_exclude_users
) {
226 const char *name
= NULL
;
232 name
= "root"; /* Avoid obvious NSS requests, to suppress network traffic */
236 pw
= pam_modutil_getpwuid(handle
, uid
);
241 STRV_FOREACH(l
, kill_exclude_users
) {
244 if (parse_uid(*l
, &u
) >= 0)
248 if (name
&& streq(name
, *l
))
252 if (strv_isempty(kill_only_users
))
255 STRV_FOREACH(l
, kill_only_users
) {
258 if (parse_uid(*l
, &u
) >= 0)
262 if (name
&& streq(name
, *l
))
269 static int get_seat_from_display(const char *display
, const char **seat
, uint32_t *vtnr
) {
273 union sockaddr_union sa
;
283 /* We deduce the X11 socket from the display name, then use
284 * SO_PEERCRED to determine the X11 server process, ask for
285 * the controlling tty of that and if it's a VC then we know
286 * the seat and the virtual terminal. Sounds ugly, is only
289 r
= socket_from_display(display
, &p
);
293 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
300 sa
.un
.sun_family
= AF_UNIX
;
301 strncpy(sa
.un
.sun_path
, p
, sizeof(sa
.un
.sun_path
)-1);
304 if (connect(fd
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + strlen(sa
.un
.sun_path
)) < 0) {
305 close_nointr_nofail(fd
);
310 r
= getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
);
311 close_nointr_nofail(fd
);
316 r
= get_ctty(ucred
.pid
, NULL
, &tty
);
320 v
= vtnr_from_tty(tty
);
329 *vtnr
= (uint32_t) v
;
334 _public_ PAM_EXTERN
int pam_sm_open_session(
335 pam_handle_t
*handle
,
337 int argc
, const char **argv
) {
340 bool kill_processes
= false, debug
= false;
341 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
;
342 char **controllers
= NULL
, **reset_controllers
= NULL
, **kill_only_users
= NULL
, **kill_exclude_users
= NULL
;
345 DBusMessageIter iter
;
348 DBusConnection
*bus
= NULL
;
349 DBusMessage
*m
= NULL
, *reply
= NULL
;
356 dbus_error_init(&error
);
358 /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
360 /* Make this a NOP on non-systemd systems */
361 if (sd_booted() <= 0)
364 if (parse_argv(handle
,
366 &controllers
, &reset_controllers
,
367 &kill_processes
, &kill_only_users
, &kill_exclude_users
,
373 r
= get_user_data(handle
, &username
, &pw
);
374 if (r
!= PAM_SUCCESS
)
377 /* Make sure we don't enter a loop by talking to
378 * systemd-logind when it is actually waiting for the
379 * background to finish start-up. If the service is
380 * "systemd-shared" we simply set XDG_RUNTIME_DIR and
383 pam_get_item(handle
, PAM_SERVICE
, (const void**) &service
);
384 if (streq_ptr(service
, "systemd-shared")) {
387 if (asprintf(&p
, "/run/systemd/users/%lu", (unsigned long) pw
->pw_uid
) < 0) {
392 r
= parse_env_file(p
, NEWLINE
,
397 if (r
< 0 && r
!= -ENOENT
) {
404 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", rt
, 0);
407 if (r
!= PAM_SUCCESS
) {
408 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
418 kill_processes
= check_user_lists(handle
, pw
->pw_uid
, kill_only_users
, kill_exclude_users
);
420 dbus_connection_set_change_sigpipe(FALSE
);
422 bus
= dbus_bus_get_private(DBUS_BUS_SYSTEM
, &error
);
424 pam_syslog(handle
, LOG_ERR
, "Failed to connect to system bus: %s", bus_error_message(&error
));
429 m
= dbus_message_new_method_call(
430 "org.freedesktop.login1",
431 "/org/freedesktop/login1",
432 "org.freedesktop.login1.Manager",
436 pam_syslog(handle
, LOG_ERR
, "Could not allocate create session message.");
444 pam_get_item(handle
, PAM_XDISPLAY
, (const void**) &display
);
445 pam_get_item(handle
, PAM_TTY
, (const void**) &tty
);
446 pam_get_item(handle
, PAM_RUSER
, (const void**) &remote_user
);
447 pam_get_item(handle
, PAM_RHOST
, (const void**) &remote_host
);
448 seat
= pam_getenv(handle
, "XDG_SEAT");
449 cvtnr
= pam_getenv(handle
, "XDG_VTNR");
451 service
= strempty(service
);
453 display
= strempty(display
);
454 remote_user
= strempty(remote_user
);
455 remote_host
= strempty(remote_host
);
456 seat
= strempty(seat
);
458 if (strchr(tty
, ':')) {
459 /* A tty with a colon is usually an X11 display, place
460 * there to show up in utmp. We rearrange things and
461 * don't pretend that an X display was a tty */
463 if (isempty(display
))
469 safe_atou32(cvtnr
, &vtnr
);
471 if (!isempty(display
) && isempty(seat
) && vtnr
<= 0)
472 get_seat_from_display(display
, &seat
, &vtnr
);
474 type
= !isempty(display
) ? "x11" :
475 !isempty(tty
) ? "tty" : "unspecified";
477 remote
= !isempty(remote_host
) && !streq(remote_host
, "localhost") && !streq(remote_host
, "localhost.localdomain");
479 if (!dbus_message_append_args(m
,
480 DBUS_TYPE_UINT32
, &uid
,
481 DBUS_TYPE_UINT32
, &pid
,
482 DBUS_TYPE_STRING
, &service
,
483 DBUS_TYPE_STRING
, &type
,
484 DBUS_TYPE_STRING
, &seat
,
485 DBUS_TYPE_UINT32
, &vtnr
,
486 DBUS_TYPE_STRING
, &tty
,
487 DBUS_TYPE_STRING
, &display
,
488 DBUS_TYPE_BOOLEAN
, &remote
,
489 DBUS_TYPE_STRING
, &remote_user
,
490 DBUS_TYPE_STRING
, &remote_host
,
491 DBUS_TYPE_INVALID
)) {
492 pam_syslog(handle
, LOG_ERR
, "Could not attach parameters to message.");
497 dbus_message_iter_init_append(m
, &iter
);
499 r
= bus_append_strv_iter(&iter
, controllers
);
501 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
506 r
= bus_append_strv_iter(&iter
, reset_controllers
);
508 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
514 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &kp
)) {
515 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
520 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
522 pam_syslog(handle
, LOG_ERR
, "Failed to create session: %s", bus_error_message(&error
));
527 if (!dbus_message_get_args(reply
, &error
,
528 DBUS_TYPE_STRING
, &id
,
529 DBUS_TYPE_OBJECT_PATH
, &object_path
,
530 DBUS_TYPE_STRING
, &runtime_path
,
531 DBUS_TYPE_UNIX_FD
, &session_fd
,
532 DBUS_TYPE_STRING
, &seat
,
533 DBUS_TYPE_UINT32
, &vtnr
,
534 DBUS_TYPE_INVALID
)) {
535 pam_syslog(handle
, LOG_ERR
, "Failed to parse message: %s", bus_error_message(&error
));
540 r
= pam_misc_setenv(handle
, "XDG_SESSION_ID", id
, 0);
541 if (r
!= PAM_SUCCESS
) {
542 pam_syslog(handle
, LOG_ERR
, "Failed to set session id.");
546 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", runtime_path
, 0);
547 if (r
!= PAM_SUCCESS
) {
548 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
552 if (!isempty(seat
)) {
553 r
= pam_misc_setenv(handle
, "XDG_SEAT", seat
, 0);
554 if (r
!= PAM_SUCCESS
) {
555 pam_syslog(handle
, LOG_ERR
, "Failed to set seat.");
562 snprintf(buf
, sizeof(buf
), "%u", vtnr
);
565 r
= pam_misc_setenv(handle
, "XDG_VTNR", buf
, 0);
566 if (r
!= PAM_SUCCESS
) {
567 pam_syslog(handle
, LOG_ERR
, "Failed to set virtual terminal number.");
572 if (session_fd
>= 0) {
573 r
= pam_set_data(handle
, "systemd.session-fd", INT_TO_PTR(session_fd
+1), NULL
);
574 if (r
!= PAM_SUCCESS
) {
575 pam_syslog(handle
, LOG_ERR
, "Failed to install session fd.");
585 strv_free(controllers
);
586 strv_free(reset_controllers
);
587 strv_free(kill_only_users
);
588 strv_free(kill_exclude_users
);
590 dbus_error_free(&error
);
593 dbus_connection_close(bus
);
594 dbus_connection_unref(bus
);
598 dbus_message_unref(m
);
601 dbus_message_unref(reply
);
604 close_nointr_nofail(session_fd
);
609 _public_ PAM_EXTERN
int pam_sm_close_session(
610 pam_handle_t
*handle
,
612 int argc
, const char **argv
) {
614 const void *p
= NULL
;
616 pam_get_data(handle
, "systemd.session-fd", &p
);
619 close_nointr(PTR_TO_INT(p
) - 1);