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"
42 static int parse_argv(pam_handle_t
*handle
,
43 int argc
, const char **argv
,
45 char ***reset_controllers
,
47 char ***kill_only_users
,
48 char ***kill_exclude_users
,
52 bool reset_controller_set
= false;
53 bool kill_exclude_users_set
= false;
56 assert(argc
== 0 || argv
);
58 for (i
= 0; i
< (unsigned) argc
; i
++) {
61 if (startswith(argv
[i
], "kill-processes=")) {
62 if ((k
= parse_boolean(argv
[i
] + 15)) < 0) {
63 pam_syslog(handle
, LOG_ERR
, "Failed to parse kill-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 reset_controller_set
= true;
111 } else if (startswith(argv
[i
], "kill-only-users=")) {
113 if (kill_only_users
) {
116 if (!(l
= strv_split(argv
[i
] + 16, ","))) {
117 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
121 strv_free(*kill_only_users
);
122 *kill_only_users
= l
;
125 } else if (startswith(argv
[i
], "kill-exclude-users=")) {
127 if (kill_exclude_users
) {
130 if (!(l
= strv_split(argv
[i
] + 19, ","))) {
131 pam_syslog(handle
, LOG_ERR
, "Out of memory.");
135 strv_free(*kill_exclude_users
);
136 *kill_exclude_users
= l
;
139 kill_exclude_users_set
= true;
141 } else if (startswith(argv
[i
], "debug=")) {
142 if ((k
= parse_boolean(argv
[i
] + 6)) < 0) {
143 pam_syslog(handle
, LOG_ERR
, "Failed to parse debug= argument.");
150 } else if (startswith(argv
[i
], "create-session=") ||
151 startswith(argv
[i
], "kill-user=")) {
153 pam_syslog(handle
, LOG_WARNING
, "Option %s not supported anymore, ignoring.", argv
[i
]);
156 pam_syslog(handle
, LOG_ERR
, "Unknown parameter '%s'.", argv
[i
]);
161 if (!reset_controller_set
&& reset_controllers
) {
164 if (!(l
= strv_new("cpu", NULL
))) {
165 pam_syslog(handle
, LOG_ERR
, "Out of memory");
169 *reset_controllers
= l
;
173 strv_remove(*controllers
, SYSTEMD_CGROUP_CONTROLLER
);
175 if (reset_controllers
)
176 strv_remove(*reset_controllers
, SYSTEMD_CGROUP_CONTROLLER
);
178 if (!kill_exclude_users_set
&& kill_exclude_users
) {
181 if (!(l
= strv_new("root", NULL
))) {
182 pam_syslog(handle
, LOG_ERR
, "Out of memory");
186 *kill_exclude_users
= l
;
192 static int get_user_data(
193 pam_handle_t
*handle
,
194 const char **ret_username
,
195 struct passwd
**ret_pw
) {
197 const char *username
= NULL
;
198 struct passwd
*pw
= NULL
;
200 bool have_loginuid
= false;
204 assert(ret_username
);
207 if (have_effective_cap(CAP_AUDIT_CONTROL
) > 0) {
208 /* Only use audit login uid if we are executed with
209 * sufficient capabilities so that pam_loginuid could
210 * do its job. If we are lacking the CAP_AUDIT_CONTROL
211 * capabality we most likely are being run in a
212 * container and /proc/self/loginuid is useless since
213 * it probably contains a uid of the host system. */
215 if (read_one_line_file("/proc/self/loginuid", &s
) >= 0) {
218 r
= safe_atou32(s
, &u
);
221 if (r
>= 0 && u
!= (uint32_t) -1 && u
> 0) {
222 have_loginuid
= true;
223 pw
= pam_modutil_getpwuid(handle
, u
);
228 if (!have_loginuid
) {
229 if ((r
= pam_get_user(handle
, &username
, NULL
)) != PAM_SUCCESS
) {
230 pam_syslog(handle
, LOG_ERR
, "Failed to get user name.");
234 if (!username
|| !*username
) {
235 pam_syslog(handle
, LOG_ERR
, "User name not valid.");
239 pw
= pam_modutil_getpwnam(handle
, username
);
243 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
244 return PAM_USER_UNKNOWN
;
248 *ret_username
= username
? username
: pw
->pw_name
;
253 static bool check_user_lists(
254 pam_handle_t
*handle
,
256 char **kill_only_users
,
257 char **kill_exclude_users
) {
259 const char *name
= NULL
;
265 name
= "root"; /* Avoid obvious NSS requests, to suppress network traffic */
269 pw
= pam_modutil_getpwuid(handle
, uid
);
274 STRV_FOREACH(l
, kill_exclude_users
) {
277 if (safe_atou32(*l
, &id
) >= 0)
278 if ((uid_t
) id
== uid
)
281 if (name
&& streq(name
, *l
))
285 if (strv_isempty(kill_only_users
))
288 STRV_FOREACH(l
, kill_only_users
) {
291 if (safe_atou32(*l
, &id
) >= 0)
292 if ((uid_t
) id
== uid
)
295 if (name
&& streq(name
, *l
))
302 _public_ PAM_EXTERN
int pam_sm_open_session(
303 pam_handle_t
*handle
,
305 int argc
, const char **argv
) {
308 bool kill_processes
= false, debug
= false;
309 const char *username
, *id
, *object_path
, *runtime_path
, *service
= NULL
, *tty
= NULL
, *display
= NULL
, *remote_user
= NULL
, *remote_host
= NULL
, *seat
= NULL
, *type
;
310 char **controllers
= NULL
, **reset_controllers
= NULL
, **kill_only_users
= NULL
, **kill_exclude_users
= NULL
;
313 DBusMessageIter iter
;
316 DBusConnection
*bus
= NULL
;
317 DBusMessage
*m
= NULL
, *reply
= NULL
;
323 dbus_error_init(&error
);
325 /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
327 /* Make this a NOP on non-systemd systems */
328 if (sd_booted() <= 0)
331 if (parse_argv(handle
,
333 &controllers
, &reset_controllers
,
334 &kill_processes
, &kill_only_users
, &kill_exclude_users
,
340 r
= get_user_data(handle
, &username
, &pw
);
341 if (r
!= PAM_SUCCESS
)
345 kill_processes
= check_user_lists(handle
, pw
->pw_uid
, kill_only_users
, kill_exclude_users
);
347 dbus_connection_set_change_sigpipe(FALSE
);
349 bus
= dbus_bus_get_private(DBUS_BUS_SYSTEM
, &error
);
351 pam_syslog(handle
, LOG_ERR
, "Failed to connect to system bus: %s", bus_error_message(&error
));
356 m
= dbus_message_new_method_call(
357 "org.freedesktop.login1",
358 "/org/freedesktop/login1",
359 "org.freedesktop.login1.Manager",
363 pam_syslog(handle
, LOG_ERR
, "Could not allocate create session message.");
371 pam_get_item(handle
, PAM_SERVICE
, (const void**) &service
);
372 pam_get_item(handle
, PAM_XDISPLAY
, (const void**) &display
);
373 pam_get_item(handle
, PAM_TTY
, (const void**) &tty
);
374 pam_get_item(handle
, PAM_RUSER
, (const void**) &remote_user
);
375 pam_get_item(handle
, PAM_RHOST
, (const void**) &remote_host
);
376 seat
= pam_getenv(handle
, "XDG_SEAT");
378 service
= strempty(service
);
380 display
= strempty(display
);
381 remote_user
= strempty(remote_user
);
382 remote_host
= strempty(remote_host
);
383 seat
= strempty(seat
);
385 type
= !isempty(display
) ? "x11" :
386 !isempty(tty
) ? "tty" : "other";
388 remote
= !isempty(remote_host
) && !streq(remote_host
, "localhost") && !streq(remote_host
, "localhost.localdomain");
390 if (!dbus_message_append_args(m
,
391 DBUS_TYPE_UINT32
, &uid
,
392 DBUS_TYPE_UINT32
, &pid
,
393 DBUS_TYPE_STRING
, &service
,
394 DBUS_TYPE_STRING
, &type
,
395 DBUS_TYPE_STRING
, &seat
,
396 DBUS_TYPE_STRING
, &tty
,
397 DBUS_TYPE_STRING
, &display
,
398 DBUS_TYPE_BOOLEAN
, &remote
,
399 DBUS_TYPE_STRING
, &remote_user
,
400 DBUS_TYPE_STRING
, &remote_host
,
401 DBUS_TYPE_INVALID
)) {
402 pam_syslog(handle
, LOG_ERR
, "Could not attach parameters to message.");
407 dbus_message_iter_init_append(m
, &iter
);
409 r
= bus_append_strv_iter(&iter
, controllers
);
411 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
416 r
= bus_append_strv_iter(&iter
, reset_controllers
);
418 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
424 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &kp
)) {
425 pam_syslog(handle
, LOG_ERR
, "Could not attach parameter to message.");
430 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
432 pam_syslog(handle
, LOG_ERR
, "Failed to create session: %s", bus_error_message(&error
));
437 if (!dbus_message_get_args(reply
, &error
,
438 DBUS_TYPE_STRING
, &id
,
439 DBUS_TYPE_OBJECT_PATH
, &object_path
,
440 DBUS_TYPE_STRING
, &runtime_path
,
441 DBUS_TYPE_UNIX_FD
, &session_fd
,
442 DBUS_TYPE_INVALID
)) {
443 pam_syslog(handle
, LOG_ERR
, "Failed to parse message: %s", bus_error_message(&error
));
448 r
= pam_misc_setenv(handle
, "XDG_SESSION_ID", id
, 0);
449 if (r
!= PAM_SUCCESS
) {
450 pam_syslog(handle
, LOG_ERR
, "Failed to set session id.");
454 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", runtime_path
, 0);
455 if (r
!= PAM_SUCCESS
) {
456 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
460 if (session_fd
>= 0) {
461 r
= pam_set_data(handle
, "systemd.session-fd", INT_TO_PTR(session_fd
+1), NULL
);
462 if (r
!= PAM_SUCCESS
) {
463 pam_syslog(handle
, LOG_ERR
, "Failed to install session fd.");
473 strv_free(controllers
);
474 strv_free(reset_controllers
);
475 strv_free(kill_only_users
);
476 strv_free(kill_exclude_users
);
478 dbus_error_free(&error
);
481 dbus_connection_close(bus
);
482 dbus_connection_unref(bus
);
486 dbus_message_unref(m
);
489 dbus_message_unref(reply
);
492 close_nointr_nofail(session_fd
);
497 _public_ PAM_EXTERN
int pam_sm_close_session(
498 pam_handle_t
*handle
,
500 int argc
, const char **argv
) {
502 const void *p
= NULL
;
504 pam_get_data(handle
, "systemd.session-fd", &p
);
507 close_nointr(PTR_TO_INT(p
) - 1);