1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
10 #include <security/_pam_macros.h>
11 #include <security/pam_ext.h>
12 #include <security/pam_misc.h>
13 #include <security/pam_modules.h>
14 #include <security/pam_modutil.h>
17 #include "alloc-util.h"
18 #include "audit-util.h"
19 #include "bus-common-errors.h"
20 #include "bus-error.h"
25 #include "format-util.h"
26 #include "hostname-util.h"
27 #include "login-util.h"
29 #include "parse-util.h"
30 #include "process-util.h"
31 #include "socket-util.h"
33 #include "terminal-util.h"
35 #include "path-util.h"
36 //#include "cgroup-util.h"
38 static int parse_argv(
40 int argc, const char **argv,
48 assert(argc == 0 || argv);
50 for (i = 0; i < (unsigned) argc; i++) {
51 if (startswith(argv[i], "class=")) {
55 } else if (startswith(argv[i], "type=")) {
59 } else if (streq(argv[i], "debug")) {
63 } else if (startswith(argv[i], "debug=")) {
66 k = parse_boolean(argv[i] + 6);
68 pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring.");
73 pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]);
79 static int get_user_data(
81 const char **ret_username,
82 struct passwd **ret_pw) {
84 const char *username = NULL;
85 struct passwd *pw = NULL;
92 r = pam_get_user(handle, &username, NULL);
93 if (r != PAM_SUCCESS) {
94 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
98 if (isempty(username)) {
99 pam_syslog(handle, LOG_ERR, "User name not valid.");
103 pw = pam_modutil_getpwnam(handle, username);
105 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
106 return PAM_USER_UNKNOWN;
110 *ret_username = username;
115 static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
116 union sockaddr_union sa = {
117 .un.sun_family = AF_UNIX,
119 _cleanup_free_ char *p = NULL, *tty = NULL;
120 _cleanup_close_ int fd = -1;
127 /* We deduce the X11 socket from the display name, then use
128 * SO_PEERCRED to determine the X11 server process, ask for
129 * the controlling tty of that and if it's a VC then we know
130 * the seat and the virtual terminal. Sounds ugly, is only
133 r = socket_from_display(display, &p);
136 strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
138 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
142 if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
145 r = getpeercred(fd, &ucred);
149 r = get_ctty(ucred.pid, NULL, &tty);
153 v = vtnr_from_tty(tty);
161 *vtnr = (uint32_t) v;
166 static int export_legacy_dbus_address(
167 pam_handle_t *handle,
169 const char *runtime) {
171 _cleanup_free_ char *s = NULL;
174 /* FIXME: We *really* should move the access() check into the
175 * daemons that spawn dbus-daemon, instead of forcing
176 * DBUS_SESSION_BUS_ADDRESS= here. */
178 s = strjoin(runtime, "/bus");
182 if (access(s, F_OK) < 0)
186 if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0)
189 r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", s, 0);
190 if (r != PAM_SUCCESS)
196 pam_syslog(handle, LOG_ERR, "Failed to set bus variable.");
200 static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) {
207 if (streq(limit, "infinity")) {
208 r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1);
210 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
214 r = parse_percent(limit);
216 r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) UINT32_MAX * r) / 100U));
218 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
222 r = parse_size(limit, 1024, &val);
224 r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val);
226 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
230 pam_syslog(handle, LOG_WARNING, "Failed to parse elogind.limit: %s, ignoring.", limit);
237 static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit)
242 /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */
243 if (isempty(limit) || streq(limit, "infinity"))
246 r = safe_atou64(limit, &val);
248 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val);
250 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
254 pam_syslog(handle, LOG_WARNING, "Failed to parse elogind.limit: %s, ignoring.", limit);
259 static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) {
263 if (!isempty(limit)) {
264 r = cg_weight_parse(limit, &val);
266 r = sd_bus_message_append(m, "(sv)", field, "t", val);
268 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
271 } else if (streq(field, "CPUWeight"))
272 pam_syslog(handle, LOG_WARNING, "Failed to parse elogind.cpu_weight: %s, ignoring.", limit);
274 pam_syslog(handle, LOG_WARNING, "Failed to parse elogind.io_weight: %s, ignoring.", limit);
280 _public_ PAM_EXTERN int pam_sm_open_session(
281 pam_handle_t *handle,
283 int argc, const char **argv) {
285 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
286 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
288 *username, *id, *object_path, *runtime_path,
290 *tty = NULL, *display = NULL,
291 *remote_user = NULL, *remote_host = NULL,
293 *type = NULL, *class = NULL,
294 *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL,
295 *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL;
296 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
297 int session_fd = -1, existing, r;
298 bool debug = false, remote;
305 #if 0 /// with elogind, it is always a "logind system".
306 /* Make this a NOP on non-logind systems */
307 if (!logind_running())
311 if (parse_argv(handle,
316 return PAM_SESSION_ERR;
319 pam_syslog(handle, LOG_DEBUG, "pam-elogind initializing");
321 r = get_user_data(handle, &username, &pw);
322 if (r != PAM_SUCCESS) {
323 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
327 /* Make sure we don't enter a loop by talking to
328 * elogind when it is actually waiting for the
329 * background to finish start-up. If the service is
330 * "elogind-user" we simply set XDG_RUNTIME_DIR and
333 pam_get_item(handle, PAM_SERVICE, (const void**) &service);
334 if (streq_ptr(service, "elogind-user")) {
335 _cleanup_free_ char *rt = NULL;
337 if (asprintf(&rt, "/run/user/"UID_FMT, pw->pw_uid) < 0)
340 r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0);
341 if (r != PAM_SUCCESS) {
342 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
346 r = export_legacy_dbus_address(handle, pw->pw_uid, rt);
347 if (r != PAM_SUCCESS)
353 /* Otherwise, we ask logind to create a session for us */
355 pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
356 pam_get_item(handle, PAM_TTY, (const void**) &tty);
357 pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
358 pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
360 seat = pam_getenv(handle, "XDG_SEAT");
362 seat = getenv("XDG_SEAT");
364 cvtnr = pam_getenv(handle, "XDG_VTNR");
366 cvtnr = getenv("XDG_VTNR");
368 type = pam_getenv(handle, "XDG_SESSION_TYPE");
370 type = getenv("XDG_SESSION_TYPE");
374 class = pam_getenv(handle, "XDG_SESSION_CLASS");
376 class = getenv("XDG_SESSION_CLASS");
380 desktop = pam_getenv(handle, "XDG_SESSION_DESKTOP");
381 if (isempty(desktop))
382 desktop = getenv("XDG_SESSION_DESKTOP");
386 if (strchr(tty, ':')) {
387 /* A tty with a colon is usually an X11 display,
388 * placed there to show up in utmp. We rearrange
389 * things and don't pretend that an X display was a
392 if (isempty(display))
395 } else if (streq(tty, "cron")) {
396 /* cron has been setting PAM_TTY to "cron" for a very
397 * long time and it probably shouldn't stop doing that
398 * for compatibility reasons. */
399 type = "unspecified";
400 class = "background";
402 } else if (streq(tty, "ssh")) {
403 /* ssh has been setting PAM_TTY to "ssh" for a very
404 * long time and probably shouldn't stop doing that
405 * for compatibility reasons. */
410 /* Chop off leading /dev prefix that some clients specify, but others do not. */
411 tty = skip_dev_prefix(tty);
413 /* If this fails vtnr will be 0, that's intended */
415 (void) safe_atou32(cvtnr, &vtnr);
417 if (!isempty(display) && !vtnr) {
419 get_seat_from_display(display, &seat, &vtnr);
420 else if (streq(seat, "seat0"))
421 get_seat_from_display(display, NULL, &vtnr);
424 if (seat && !streq(seat, "seat0") && vtnr != 0) {
425 pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat);
430 type = !isempty(display) ? "x11" :
431 !isempty(tty) ? "tty" : "unspecified";
434 class = streq(type, "unspecified") ? "background" : "user";
436 remote = !isempty(remote_host) && !is_localhost(remote_host);
438 (void) pam_get_data(handle, "elogind.memory_max", (const void **)&memory_max);
439 (void) pam_get_data(handle, "elogind.tasks_max", (const void **)&tasks_max);
440 (void) pam_get_data(handle, "elogind.cpu_weight", (const void **)&cpu_weight);
441 (void) pam_get_data(handle, "elogind.io_weight", (const void **)&io_weight);
443 /* Talk to logind over the message bus */
445 r = sd_bus_open_system(&bus);
447 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r));
448 return PAM_SESSION_ERR;
452 pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
453 "uid="UID_FMT" pid="PID_FMT" service=%s type=%s class=%s desktop=%s seat=%s vtnr=%"PRIu32" tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
454 pw->pw_uid, getpid_cached(),
456 type, class, strempty(desktop),
457 strempty(seat), vtnr, strempty(tty), strempty(display),
458 yes_no(remote), strempty(remote_user), strempty(remote_host));
459 pam_syslog(handle, LOG_DEBUG, "Session limits: "
460 "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s",
461 strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight));
464 r = sd_bus_message_new_method_call(
467 "org.freedesktop.login1",
468 "/org/freedesktop/login1",
469 "org.freedesktop.login1.Manager",
472 pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror(-r));
473 return PAM_SESSION_ERR;
476 r = sd_bus_message_append(m, "uusssssussbss",
477 (uint32_t) pw->pw_uid,
478 (uint32_t) getpid_cached(),
491 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
492 return PAM_SESSION_ERR;
495 r = sd_bus_message_open_container(m, 'a', "(sv)");
497 pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror(-r));
498 return PAM_SYSTEM_ERR;
501 r = append_session_memory_max(handle, m, memory_max);
503 return PAM_SESSION_ERR;
505 r = append_session_tasks_max(handle, m, tasks_max);
507 return PAM_SESSION_ERR;
509 r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight");
511 return PAM_SESSION_ERR;
513 r = append_session_cg_weight(handle, m, io_weight, "IOWeight");
515 return PAM_SESSION_ERR;
517 r = sd_bus_message_close_container(m);
519 pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror(-r));
520 return PAM_SYSTEM_ERR;
523 r = sd_bus_call(bus, m, 0, &error, &reply);
525 if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) {
526 pam_syslog(handle, LOG_DEBUG, "Cannot create session: %s", bus_error_message(&error, r));
529 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r));
530 return PAM_SYSTEM_ERR;
534 r = sd_bus_message_read(reply,
545 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror(-r));
546 return PAM_SESSION_ERR;
550 pam_syslog(handle, LOG_DEBUG, "Reply from logind: "
551 "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u",
552 id, object_path, runtime_path, session_fd, seat, vtnr, original_uid);
554 r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
555 if (r != PAM_SUCCESS) {
556 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
560 if (original_uid == pw->pw_uid) {
561 /* Don't set $XDG_RUNTIME_DIR if the user we now
562 * authenticated for does not match the original user
563 * of the session. We do this in order not to result
564 * in privileged apps clobbering the runtime directory
567 r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
568 if (r != PAM_SUCCESS) {
569 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
573 r = export_legacy_dbus_address(handle, pw->pw_uid, runtime_path);
574 if (r != PAM_SUCCESS)
578 if (!isempty(seat)) {
579 r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0);
580 if (r != PAM_SUCCESS) {
581 pam_syslog(handle, LOG_ERR, "Failed to set seat.");
587 char buf[DECIMAL_STR_MAX(vtnr)];
588 sprintf(buf, "%u", vtnr);
590 r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
591 if (r != PAM_SUCCESS) {
592 pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number.");
597 r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL);
598 if (r != PAM_SUCCESS) {
599 pam_syslog(handle, LOG_ERR, "Failed to install existing flag.");
603 if (session_fd >= 0) {
604 session_fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3);
605 if (session_fd < 0) {
606 pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m");
607 return PAM_SESSION_ERR;
610 r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL);
611 if (r != PAM_SUCCESS) {
612 pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
613 safe_close(session_fd);
621 _public_ PAM_EXTERN int pam_sm_close_session(
622 pam_handle_t *handle,
624 int argc, const char **argv) {
626 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
627 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
628 const void *existing = NULL;
634 /* Only release session if it wasn't pre-existing when we
635 * tried to create it */
636 pam_get_data(handle, "systemd.existing", &existing);
638 id = pam_getenv(handle, "XDG_SESSION_ID");
639 if (id && !existing) {
641 /* Before we go and close the FIFO we need to tell
642 * logind that this is a clean session shutdown, so
643 * that it doesn't just go and slaughter us
644 * immediately after closing the fd */
646 r = sd_bus_open_system(&bus);
648 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r));
649 return PAM_SESSION_ERR;
652 r = sd_bus_call_method(bus,
653 "org.freedesktop.login1",
654 "/org/freedesktop/login1",
655 "org.freedesktop.login1.Manager",
662 pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error, r));
663 return PAM_SESSION_ERR;
667 /* Note that we are knowingly leaking the FIFO fd here. This
668 * way, logind can watch us die. If we closed it here it would
669 * not have any clue when that is completed. Given that one
670 * cannot really have multiple PAM sessions open from the same
671 * process this means we will leak one FD at max. */