1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <security/_pam_macros.h>
8 #include <security/pam_ext.h>
9 #include <security/pam_misc.h>
10 #include <security/pam_modules.h>
11 #include <security/pam_modutil.h>
14 #include "alloc-util.h"
15 #include "audit-util.h"
16 #include "bus-common-errors.h"
17 #include "bus-error.h"
19 #include "cgroup-util.h"
23 #include "format-util.h"
24 #include "hostname-util.h"
25 #include "login-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "socket-util.h"
32 #include "terminal-util.h"
35 static int parse_argv(
37 int argc
, const char **argv
,
46 assert(argc
== 0 || argv
);
48 for (i
= 0; i
< (unsigned) argc
; i
++) {
49 if (startswith(argv
[i
], "class=")) {
53 } else if (startswith(argv
[i
], "type=")) {
57 } else if (startswith(argv
[i
], "desktop=")) {
59 *desktop
= argv
[i
] + 8;
61 } else if (streq(argv
[i
], "debug")) {
65 } else if (startswith(argv
[i
], "debug=")) {
68 k
= parse_boolean(argv
[i
] + 6);
70 pam_syslog(handle
, LOG_WARNING
, "Failed to parse debug= argument, ignoring.");
75 pam_syslog(handle
, LOG_WARNING
, "Unknown parameter '%s', ignoring", argv
[i
]);
81 static int get_user_data(
83 const char **ret_username
,
84 struct passwd
**ret_pw
) {
86 const char *username
= NULL
;
87 struct passwd
*pw
= NULL
;
94 r
= pam_get_user(handle
, &username
, NULL
);
95 if (r
!= PAM_SUCCESS
) {
96 pam_syslog(handle
, LOG_ERR
, "Failed to get user name.");
100 if (isempty(username
)) {
101 pam_syslog(handle
, LOG_ERR
, "User name not valid.");
105 pw
= pam_modutil_getpwnam(handle
, username
);
107 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
108 return PAM_USER_UNKNOWN
;
112 *ret_username
= username
;
117 static int socket_from_display(const char *display
, char **path
) {
124 if (!display_is_local(display
))
127 k
= strspn(display
+1, "0123456789");
129 f
= new(char, STRLEN("/tmp/.X11-unix/X") + k
+ 1);
133 c
= stpcpy(f
, "/tmp/.X11-unix/X");
134 memcpy(c
, display
+1, k
);
142 static int get_seat_from_display(const char *display
, const char **seat
, uint32_t *vtnr
) {
143 union sockaddr_union sa
= {
144 .un
.sun_family
= AF_UNIX
,
146 _cleanup_free_
char *p
= NULL
, *tty
= NULL
;
147 _cleanup_close_
int fd
= -1;
154 /* We deduce the X11 socket from the display name, then use
155 * SO_PEERCRED to determine the X11 server process, ask for
156 * the controlling tty of that and if it's a VC then we know
157 * the seat and the virtual terminal. Sounds ugly, is only
160 r
= socket_from_display(display
, &p
);
163 strncpy(sa
.un
.sun_path
, p
, sizeof(sa
.un
.sun_path
)-1);
165 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
169 if (connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
172 r
= getpeercred(fd
, &ucred
);
176 r
= get_ctty(ucred
.pid
, NULL
, &tty
);
180 v
= vtnr_from_tty(tty
);
188 *vtnr
= (uint32_t) v
;
193 static int append_session_memory_max(pam_handle_t
*handle
, sd_bus_message
*m
, const char *limit
) {
200 if (streq(limit
, "infinity")) {
201 r
= sd_bus_message_append(m
, "(sv)", "MemoryMax", "t", (uint64_t)-1);
203 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
207 r
= parse_permille(limit
);
209 r
= sd_bus_message_append(m
, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) r
* UINT32_MAX
) / 1000U));
211 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
215 r
= parse_size(limit
, 1024, &val
);
217 r
= sd_bus_message_append(m
, "(sv)", "MemoryMax", "t", val
);
219 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
223 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.limit: %s, ignoring.", limit
);
230 static int append_session_tasks_max(pam_handle_t
*handle
, sd_bus_message
*m
, const char *limit
) {
234 /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */
235 if (isempty(limit
) || streq(limit
, "infinity"))
238 r
= safe_atou64(limit
, &val
);
240 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", val
);
242 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
246 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.limit: %s, ignoring.", limit
);
251 static int append_session_cg_weight(pam_handle_t
*handle
, sd_bus_message
*m
, const char *limit
, const char *field
) {
258 r
= cg_weight_parse(limit
, &val
);
260 r
= sd_bus_message_append(m
, "(sv)", field
, "t", val
);
262 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
265 } else if (streq(field
, "CPUWeight"))
266 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.cpu_weight: %s, ignoring.", limit
);
268 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.io_weight: %s, ignoring.", limit
);
273 static const char* getenv_harder(pam_handle_t
*handle
, const char *key
, const char *fallback
) {
279 /* Looks for an environment variable, preferrably in the environment block associated with the specified PAM
280 * handle, falling back to the process' block instead. */
282 v
= pam_getenv(handle
, key
);
293 static int update_environment(pam_handle_t
*handle
, const char *key
, const char *value
) {
299 /* Updates the environment, but only if there's actually a value set. Also, log about errors */
304 r
= pam_misc_setenv(handle
, key
, value
, 0);
305 if (r
!= PAM_SUCCESS
)
306 pam_syslog(handle
, LOG_ERR
, "Failed to set environment variable %s.", key
);
311 _public_ PAM_EXTERN
int pam_sm_open_session(
312 pam_handle_t
*handle
,
314 int argc
, const char **argv
) {
316 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
317 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
319 *username
, *id
, *object_path
, *runtime_path
,
321 *tty
= NULL
, *display
= NULL
,
322 *remote_user
= NULL
, *remote_host
= NULL
,
324 *type
= NULL
, *class = NULL
,
325 *class_pam
= NULL
, *type_pam
= NULL
, *cvtnr
= NULL
, *desktop
= NULL
, *desktop_pam
= NULL
,
326 *memory_max
= NULL
, *tasks_max
= NULL
, *cpu_weight
= NULL
, *io_weight
= NULL
;
327 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
328 int session_fd
= -1, existing
, r
;
329 bool debug
= false, remote
;
336 /* Make this a NOP on non-logind systems */
337 if (!logind_running())
340 if (parse_argv(handle
,
346 return PAM_SESSION_ERR
;
349 pam_syslog(handle
, LOG_DEBUG
, "pam-systemd initializing");
351 r
= get_user_data(handle
, &username
, &pw
);
352 if (r
!= PAM_SUCCESS
) {
353 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
357 /* Make sure we don't enter a loop by talking to
358 * systemd-logind when it is actually waiting for the
359 * background to finish start-up. If the service is
360 * "systemd-user" we simply set XDG_RUNTIME_DIR and
363 pam_get_item(handle
, PAM_SERVICE
, (const void**) &service
);
364 if (streq_ptr(service
, "systemd-user")) {
365 _cleanup_free_
char *rt
= NULL
;
367 if (asprintf(&rt
, "/run/user/"UID_FMT
, pw
->pw_uid
) < 0)
370 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", rt
, 0);
371 if (r
!= PAM_SUCCESS
) {
372 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
379 /* Otherwise, we ask logind to create a session for us */
381 pam_get_item(handle
, PAM_XDISPLAY
, (const void**) &display
);
382 pam_get_item(handle
, PAM_TTY
, (const void**) &tty
);
383 pam_get_item(handle
, PAM_RUSER
, (const void**) &remote_user
);
384 pam_get_item(handle
, PAM_RHOST
, (const void**) &remote_host
);
386 seat
= getenv_harder(handle
, "XDG_SEAT", NULL
);
387 cvtnr
= getenv_harder(handle
, "XDG_VTNR", NULL
);
388 type
= getenv_harder(handle
, "XDG_SESSION_TYPE", type_pam
);
389 class = getenv_harder(handle
, "XDG_SESSION_CLASS", class_pam
);
390 desktop
= getenv_harder(handle
, "XDG_SESSION_DESKTOP", desktop_pam
);
394 if (strchr(tty
, ':')) {
395 /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things
396 * and don't pretend that an X display was a tty. */
397 if (isempty(display
))
401 } else if (streq(tty
, "cron")) {
402 /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but
403 * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set
404 * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked
406 type
= "unspecified";
407 class = "background";
410 } else if (streq(tty
, "ssh")) {
411 /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further
412 * details look for "PAM_TTY_KLUDGE" in the openssh sources). */
415 tty
= NULL
; /* This one is particularly sad, as this means that ssh sessions — even though usually
416 * associated with a pty — won't be tracked by their tty in logind. This is because ssh
417 * does the PAM session registration early for new connections, and registers a pty only
418 * much later (this is because it doesn't know yet if it needs one at all, as whether to
419 * register a pty or not is negotiated much later in the protocol). */
422 /* Chop off leading /dev prefix that some clients specify, but others do not. */
423 tty
= skip_dev_prefix(tty
);
425 /* If this fails vtnr will be 0, that's intended */
427 (void) safe_atou32(cvtnr
, &vtnr
);
429 if (!isempty(display
) && !vtnr
) {
431 (void) get_seat_from_display(display
, &seat
, &vtnr
);
432 else if (streq(seat
, "seat0"))
433 (void) get_seat_from_display(display
, NULL
, &vtnr
);
436 if (seat
&& !streq(seat
, "seat0") && vtnr
!= 0) {
437 pam_syslog(handle
, LOG_DEBUG
, "Ignoring vtnr %"PRIu32
" for %s which is not seat0", vtnr
, seat
);
442 type
= !isempty(display
) ? "x11" :
443 !isempty(tty
) ? "tty" : "unspecified";
446 class = streq(type
, "unspecified") ? "background" : "user";
448 remote
= !isempty(remote_host
) && !is_localhost(remote_host
);
450 (void) pam_get_data(handle
, "systemd.memory_max", (const void **)&memory_max
);
451 (void) pam_get_data(handle
, "systemd.tasks_max", (const void **)&tasks_max
);
452 (void) pam_get_data(handle
, "systemd.cpu_weight", (const void **)&cpu_weight
);
453 (void) pam_get_data(handle
, "systemd.io_weight", (const void **)&io_weight
);
455 /* Talk to logind over the message bus */
457 r
= sd_bus_open_system(&bus
);
459 pam_syslog(handle
, LOG_ERR
, "Failed to connect to system bus: %s", strerror(-r
));
460 return PAM_SESSION_ERR
;
464 pam_syslog(handle
, LOG_DEBUG
, "Asking logind to create session: "
465 "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",
466 pw
->pw_uid
, getpid_cached(),
468 type
, class, strempty(desktop
),
469 strempty(seat
), vtnr
, strempty(tty
), strempty(display
),
470 yes_no(remote
), strempty(remote_user
), strempty(remote_host
));
471 pam_syslog(handle
, LOG_DEBUG
, "Session limits: "
472 "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s",
473 strna(memory_max
), strna(tasks_max
), strna(cpu_weight
), strna(io_weight
));
476 r
= sd_bus_message_new_method_call(
479 "org.freedesktop.login1",
480 "/org/freedesktop/login1",
481 "org.freedesktop.login1.Manager",
484 pam_syslog(handle
, LOG_ERR
, "Failed to create CreateSession method call: %s", strerror(-r
));
485 return PAM_SESSION_ERR
;
488 r
= sd_bus_message_append(m
, "uusssssussbss",
489 (uint32_t) pw
->pw_uid
,
490 (uint32_t) getpid_cached(),
503 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
504 return PAM_SESSION_ERR
;
507 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
509 pam_syslog(handle
, LOG_ERR
, "Failed to open message container: %s", strerror(-r
));
510 return PAM_SYSTEM_ERR
;
513 r
= append_session_memory_max(handle
, m
, memory_max
);
515 return PAM_SESSION_ERR
;
517 r
= append_session_tasks_max(handle
, m
, tasks_max
);
519 return PAM_SESSION_ERR
;
521 r
= append_session_cg_weight(handle
, m
, cpu_weight
, "CPUWeight");
523 return PAM_SESSION_ERR
;
525 r
= append_session_cg_weight(handle
, m
, io_weight
, "IOWeight");
527 return PAM_SESSION_ERR
;
529 r
= sd_bus_message_close_container(m
);
531 pam_syslog(handle
, LOG_ERR
, "Failed to close message container: %s", strerror(-r
));
532 return PAM_SYSTEM_ERR
;
535 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
537 if (sd_bus_error_has_name(&error
, BUS_ERROR_SESSION_BUSY
)) {
538 pam_syslog(handle
, LOG_DEBUG
, "Cannot create session: %s", bus_error_message(&error
, r
));
541 pam_syslog(handle
, LOG_ERR
, "Failed to create session: %s", bus_error_message(&error
, r
));
542 return PAM_SYSTEM_ERR
;
546 r
= sd_bus_message_read(reply
,
557 pam_syslog(handle
, LOG_ERR
, "Failed to parse message: %s", strerror(-r
));
558 return PAM_SESSION_ERR
;
562 pam_syslog(handle
, LOG_DEBUG
, "Reply from logind: "
563 "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u",
564 id
, object_path
, runtime_path
, session_fd
, seat
, vtnr
, original_uid
);
566 r
= update_environment(handle
, "XDG_SESSION_ID", id
);
567 if (r
!= PAM_SUCCESS
)
570 if (original_uid
== pw
->pw_uid
) {
571 /* Don't set $XDG_RUNTIME_DIR if the user we now
572 * authenticated for does not match the original user
573 * of the session. We do this in order not to result
574 * in privileged apps clobbering the runtime directory
577 r
= update_environment(handle
, "XDG_RUNTIME_DIR", runtime_path
);
578 if (r
!= PAM_SUCCESS
)
582 /* Most likely we got the session/type/class from environment variables, but might have gotten the data
583 * somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this
584 * data is inherited into the session processes, and programs can rely on them to be initialized. */
586 r
= update_environment(handle
, "XDG_SESSION_TYPE", type
);
587 if (r
!= PAM_SUCCESS
)
590 r
= update_environment(handle
, "XDG_SESSION_CLASS", class);
591 if (r
!= PAM_SUCCESS
)
594 r
= update_environment(handle
, "XDG_SESSION_DESKTOP", desktop
);
595 if (r
!= PAM_SUCCESS
)
598 r
= update_environment(handle
, "XDG_SEAT", seat
);
599 if (r
!= PAM_SUCCESS
)
603 char buf
[DECIMAL_STR_MAX(vtnr
)];
604 sprintf(buf
, "%u", vtnr
);
606 r
= update_environment(handle
, "XDG_VTNR", buf
);
607 if (r
!= PAM_SUCCESS
)
611 r
= pam_set_data(handle
, "systemd.existing", INT_TO_PTR(!!existing
), NULL
);
612 if (r
!= PAM_SUCCESS
) {
613 pam_syslog(handle
, LOG_ERR
, "Failed to install existing flag.");
617 if (session_fd
>= 0) {
618 session_fd
= fcntl(session_fd
, F_DUPFD_CLOEXEC
, 3);
619 if (session_fd
< 0) {
620 pam_syslog(handle
, LOG_ERR
, "Failed to dup session fd: %m");
621 return PAM_SESSION_ERR
;
624 r
= pam_set_data(handle
, "systemd.session-fd", FD_TO_PTR(session_fd
), NULL
);
625 if (r
!= PAM_SUCCESS
) {
626 pam_syslog(handle
, LOG_ERR
, "Failed to install session fd.");
627 safe_close(session_fd
);
635 _public_ PAM_EXTERN
int pam_sm_close_session(
636 pam_handle_t
*handle
,
638 int argc
, const char **argv
) {
640 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
641 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
642 const void *existing
= NULL
;
648 /* Only release session if it wasn't pre-existing when we
649 * tried to create it */
650 (void) pam_get_data(handle
, "systemd.existing", &existing
);
652 id
= pam_getenv(handle
, "XDG_SESSION_ID");
653 if (id
&& !existing
) {
655 /* Before we go and close the FIFO we need to tell
656 * logind that this is a clean session shutdown, so
657 * that it doesn't just go and slaughter us
658 * immediately after closing the fd */
660 r
= sd_bus_open_system(&bus
);
662 pam_syslog(handle
, LOG_ERR
, "Failed to connect to system bus: %s", strerror(-r
));
663 return PAM_SESSION_ERR
;
666 r
= sd_bus_call_method(bus
,
667 "org.freedesktop.login1",
668 "/org/freedesktop/login1",
669 "org.freedesktop.login1.Manager",
676 pam_syslog(handle
, LOG_ERR
, "Failed to release session: %s", bus_error_message(&error
, r
));
677 return PAM_SESSION_ERR
;
681 /* Note that we are knowingly leaking the FIFO fd here. This
682 * way, logind can watch us die. If we closed it here it would
683 * not have any clue when that is completed. Given that one
684 * cannot really have multiple PAM sessions open from the same
685 * process this means we will leak one FD at max. */