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,
53 bool reset_controller_set = false;
54 bool kill_exclude_users_set = false;
57 assert(argc == 0 || argv);
59 for (i = 0; i < (unsigned) argc; i++) {
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.");
71 } else if (startswith(argv[i], "kill-session=")) {
72 /* As compatibility for old versions */
74 if ((k = parse_boolean(argv[i] + 13)) < 0) {
75 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
82 } else if (startswith(argv[i], "controllers=")) {
87 if (!(l = strv_split(argv[i] + 12, ","))) {
88 pam_syslog(handle, LOG_ERR, "Out of memory.");
92 strv_free(*controllers);
96 } else if (startswith(argv[i], "reset-controllers=")) {
98 if (reset_controllers) {
101 if (!(l = strv_split(argv[i] + 18, ","))) {
102 pam_syslog(handle, LOG_ERR, "Out of memory.");
106 strv_free(*reset_controllers);
107 *reset_controllers = l;
110 reset_controller_set = true;
112 } else if (startswith(argv[i], "kill-only-users=")) {
114 if (kill_only_users) {
117 if (!(l = strv_split(argv[i] + 16, ","))) {
118 pam_syslog(handle, LOG_ERR, "Out of memory.");
122 strv_free(*kill_only_users);
123 *kill_only_users = l;
126 } else if (startswith(argv[i], "kill-exclude-users=")) {
128 if (kill_exclude_users) {
131 if (!(l = strv_split(argv[i] + 19, ","))) {
132 pam_syslog(handle, LOG_ERR, "Out of memory.");
136 strv_free(*kill_exclude_users);
137 *kill_exclude_users = l;
140 kill_exclude_users_set = true;
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.");
151 } else if (startswith(argv[i], "create-session=") ||
152 startswith(argv[i], "kill-user=")) {
154 pam_syslog(handle, LOG_WARNING, "Option %s not supported anymore, ignoring.", argv[i]);
157 pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
162 if (!reset_controller_set && reset_controllers) {
165 if (!(l = strv_new("cpu", NULL))) {
166 pam_syslog(handle, LOG_ERR, "Out of memory");
170 *reset_controllers = l;
174 strv_remove(*controllers, SYSTEMD_CGROUP_CONTROLLER);
176 if (reset_controllers)
177 strv_remove(*reset_controllers, SYSTEMD_CGROUP_CONTROLLER);
179 if (!kill_exclude_users_set && kill_exclude_users) {
182 if (!(l = strv_new("root", NULL))) {
183 pam_syslog(handle, LOG_ERR, "Out of memory");
187 *kill_exclude_users = l;
193 static int get_user_data(
194 pam_handle_t *handle,
195 const char **ret_username,
196 struct passwd **ret_pw) {
198 const char *username = NULL;
199 struct passwd *pw = NULL;
201 bool have_loginuid = false;
205 assert(ret_username);
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. */
216 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
219 r = safe_atou32(s, &u);
222 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
223 have_loginuid = true;
224 pw = pam_modutil_getpwuid(handle, u);
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.");
235 if (!username || !*username) {
236 pam_syslog(handle, LOG_ERR, "User name not valid.");
240 pw = pam_modutil_getpwnam(handle, username);
244 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
245 return PAM_USER_UNKNOWN;
249 *ret_username = username ? username : pw->pw_name;
254 static bool check_user_lists(
255 pam_handle_t *handle,
257 char **kill_only_users,
258 char **kill_exclude_users) {
260 const char *name = NULL;
266 name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */
270 pw = pam_modutil_getpwuid(handle, uid);
275 STRV_FOREACH(l, kill_exclude_users) {
278 if (safe_atou32(*l, &id) >= 0)
279 if ((uid_t) id == uid)
282 if (name && streq(name, *l))
286 if (strv_isempty(kill_only_users))
289 STRV_FOREACH(l, kill_only_users) {
292 if (safe_atou32(*l, &id) >= 0)
293 if ((uid_t) id == uid)
296 if (name && streq(name, *l))
303 static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
307 union sockaddr_union sa;
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
323 r = socket_from_display(display, &p);
327 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
334 sa.un.sun_family = AF_UNIX;
335 strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
338 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
339 close_nointr_nofail(fd);
344 r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
345 close_nointr_nofail(fd);
350 r = get_ctty(ucred.pid, NULL, &tty);
354 v = vtnr_from_tty(tty);
363 *vtnr = (uint32_t) v;
368 _public_ PAM_EXTERN int pam_sm_open_session(
369 pam_handle_t *handle,
371 int argc, const char **argv) {
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;
379 DBusMessageIter iter;
382 DBusConnection *bus = NULL;
383 DBusMessage *m = NULL, *reply = NULL;
390 dbus_error_init(&error);
392 /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
394 /* Make this a NOP on non-systemd systems */
395 if (sd_booted() <= 0)
398 if (parse_argv(handle,
400 &controllers, &reset_controllers,
401 &kill_processes, &kill_only_users, &kill_exclude_users,
407 r = get_user_data(handle, &username, &pw);
408 if (r != PAM_SUCCESS)
412 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
414 dbus_connection_set_change_sigpipe(FALSE);
416 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
418 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", bus_error_message(&error));
423 m = dbus_message_new_method_call(
424 "org.freedesktop.login1",
425 "/org/freedesktop/login1",
426 "org.freedesktop.login1.Manager",
430 pam_syslog(handle, LOG_ERR, "Could not allocate create session message.");
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");
446 service = strempty(service);
448 display = strempty(display);
449 remote_user = strempty(remote_user);
450 remote_host = strempty(remote_host);
451 seat = strempty(seat);
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 */
458 if (isempty(display))
464 safe_atou32(cvtnr, &vtnr);
466 if (!isempty(display) && isempty(seat) && vtnr <= 0)
467 get_seat_from_display(display, &seat, &vtnr);
469 type = !isempty(display) ? "x11" :
470 !isempty(tty) ? "tty" : "other";
472 remote = !isempty(remote_host) && !streq(remote_host, "localhost") && !streq(remote_host, "localhost.localdomain");
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.");
492 dbus_message_iter_init_append(m, &iter);
494 r = bus_append_strv_iter(&iter, controllers);
496 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
501 r = bus_append_strv_iter(&iter, reset_controllers);
503 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
509 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &kp)) {
510 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
515 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
517 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error));
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));
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.");
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.");
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.");
558 strv_free(controllers);
559 strv_free(reset_controllers);
560 strv_free(kill_only_users);
561 strv_free(kill_exclude_users);
563 dbus_error_free(&error);
566 dbus_connection_close(bus);
567 dbus_connection_unref(bus);
571 dbus_message_unref(m);
574 dbus_message_unref(reply);
577 close_nointr_nofail(session_fd);
582 _public_ PAM_EXTERN int pam_sm_close_session(
583 pam_handle_t *handle,
585 int argc, const char **argv) {
587 const void *p = NULL;
589 pam_get_data(handle, "systemd.session-fd", &p);
592 close_nointr(PTR_TO_INT(p) - 1);