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"
22 #include "format-util.h"
23 #include "hostname-util.h"
24 #include "login-util.h"
26 #include "parse-util.h"
27 #include "process-util.h"
28 #include "socket-util.h"
30 #include "terminal-util.h"
32 #include "path-util.h"
33 #include "cgroup-util.h"
35 static int parse_argv(
37 int argc
, const char **argv
,
45 assert(argc
== 0 || argv
);
47 for (i
= 0; i
< (unsigned) argc
; i
++) {
48 if (startswith(argv
[i
], "class=")) {
52 } else if (startswith(argv
[i
], "type=")) {
56 } else if (streq(argv
[i
], "debug")) {
60 } else if (startswith(argv
[i
], "debug=")) {
63 k
= parse_boolean(argv
[i
] + 6);
65 pam_syslog(handle
, LOG_WARNING
, "Failed to parse debug= argument, ignoring.");
70 pam_syslog(handle
, LOG_WARNING
, "Unknown parameter '%s', ignoring", argv
[i
]);
76 static int get_user_data(
78 const char **ret_username
,
79 struct passwd
**ret_pw
) {
81 const char *username
= NULL
;
82 struct passwd
*pw
= NULL
;
89 r
= pam_get_user(handle
, &username
, NULL
);
90 if (r
!= PAM_SUCCESS
) {
91 pam_syslog(handle
, LOG_ERR
, "Failed to get user name.");
95 if (isempty(username
)) {
96 pam_syslog(handle
, LOG_ERR
, "User name not valid.");
100 pw
= pam_modutil_getpwnam(handle
, username
);
102 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
103 return PAM_USER_UNKNOWN
;
107 *ret_username
= username
;
112 static int get_seat_from_display(const char *display
, const char **seat
, uint32_t *vtnr
) {
113 union sockaddr_union sa
= {
114 .un
.sun_family
= AF_UNIX
,
116 _cleanup_free_
char *p
= NULL
, *tty
= NULL
;
117 _cleanup_close_
int fd
= -1;
124 /* We deduce the X11 socket from the display name, then use
125 * SO_PEERCRED to determine the X11 server process, ask for
126 * the controlling tty of that and if it's a VC then we know
127 * the seat and the virtual terminal. Sounds ugly, is only
130 r
= socket_from_display(display
, &p
);
133 strncpy(sa
.un
.sun_path
, p
, sizeof(sa
.un
.sun_path
)-1);
135 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
139 if (connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
142 r
= getpeercred(fd
, &ucred
);
146 r
= get_ctty(ucred
.pid
, NULL
, &tty
);
150 v
= vtnr_from_tty(tty
);
158 *vtnr
= (uint32_t) v
;
163 static int export_legacy_dbus_address(
164 pam_handle_t
*handle
,
166 const char *runtime
) {
168 _cleanup_free_
char *s
= NULL
;
171 /* FIXME: We *really* should move the access() check into the
172 * daemons that spawn dbus-daemon, instead of forcing
173 * DBUS_SESSION_BUS_ADDRESS= here. */
175 s
= strjoin(runtime
, "/bus");
179 if (access(s
, F_OK
) < 0)
183 if (asprintf(&s
, DEFAULT_USER_BUS_ADDRESS_FMT
, runtime
) < 0)
186 r
= pam_misc_setenv(handle
, "DBUS_SESSION_BUS_ADDRESS", s
, 0);
187 if (r
!= PAM_SUCCESS
)
193 pam_syslog(handle
, LOG_ERR
, "Failed to set bus variable.");
197 static int append_session_memory_max(pam_handle_t
*handle
, sd_bus_message
*m
, const char *limit
) {
204 if (streq(limit
, "infinity")) {
205 r
= sd_bus_message_append(m
, "(sv)", "MemoryMax", "t", (uint64_t)-1);
207 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
211 r
= parse_percent(limit
);
213 r
= sd_bus_message_append(m
, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) UINT32_MAX
* r
) / 100U));
215 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
219 r
= parse_size(limit
, 1024, &val
);
221 r
= sd_bus_message_append(m
, "(sv)", "MemoryMax", "t", val
);
223 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
227 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.limit: %s, ignoring.", limit
);
234 static int append_session_tasks_max(pam_handle_t
*handle
, sd_bus_message
*m
, const char *limit
)
239 /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */
240 if (isempty(limit
) || streq(limit
, "infinity"))
243 r
= safe_atou64(limit
, &val
);
245 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", val
);
247 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
251 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.limit: %s, ignoring.", limit
);
256 static int append_session_cg_weight(pam_handle_t
*handle
, sd_bus_message
*m
, const char *limit
, const char *field
) {
260 if (!isempty(limit
)) {
261 r
= cg_weight_parse(limit
, &val
);
263 r
= sd_bus_message_append(m
, "(sv)", field
, "t", val
);
265 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
268 } else if (streq(field
, "CPUWeight"))
269 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.cpu_weight: %s, ignoring.", limit
);
271 pam_syslog(handle
, LOG_WARNING
, "Failed to parse systemd.io_weight: %s, ignoring.", limit
);
277 _public_ PAM_EXTERN
int pam_sm_open_session(
278 pam_handle_t
*handle
,
280 int argc
, const char **argv
) {
282 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
283 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
285 *username
, *id
, *object_path
, *runtime_path
,
287 *tty
= NULL
, *display
= NULL
,
288 *remote_user
= NULL
, *remote_host
= NULL
,
290 *type
= NULL
, *class = NULL
,
291 *class_pam
= NULL
, *type_pam
= NULL
, *cvtnr
= NULL
, *desktop
= NULL
,
292 *memory_max
= NULL
, *tasks_max
= NULL
, *cpu_weight
= NULL
, *io_weight
= NULL
;
293 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
294 int session_fd
= -1, existing
, r
;
295 bool debug
= false, remote
;
302 /* Make this a NOP on non-logind systems */
303 if (!logind_running())
306 if (parse_argv(handle
,
311 return PAM_SESSION_ERR
;
314 pam_syslog(handle
, LOG_DEBUG
, "pam-systemd initializing");
316 r
= get_user_data(handle
, &username
, &pw
);
317 if (r
!= PAM_SUCCESS
) {
318 pam_syslog(handle
, LOG_ERR
, "Failed to get user data.");
322 /* Make sure we don't enter a loop by talking to
323 * systemd-logind when it is actually waiting for the
324 * background to finish start-up. If the service is
325 * "systemd-user" we simply set XDG_RUNTIME_DIR and
328 pam_get_item(handle
, PAM_SERVICE
, (const void**) &service
);
329 if (streq_ptr(service
, "systemd-user")) {
330 _cleanup_free_
char *rt
= NULL
;
332 if (asprintf(&rt
, "/run/user/"UID_FMT
, pw
->pw_uid
) < 0)
335 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", rt
, 0);
336 if (r
!= PAM_SUCCESS
) {
337 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
341 r
= export_legacy_dbus_address(handle
, pw
->pw_uid
, rt
);
342 if (r
!= PAM_SUCCESS
)
348 /* Otherwise, we ask logind to create a session for us */
350 pam_get_item(handle
, PAM_XDISPLAY
, (const void**) &display
);
351 pam_get_item(handle
, PAM_TTY
, (const void**) &tty
);
352 pam_get_item(handle
, PAM_RUSER
, (const void**) &remote_user
);
353 pam_get_item(handle
, PAM_RHOST
, (const void**) &remote_host
);
355 seat
= pam_getenv(handle
, "XDG_SEAT");
357 seat
= getenv("XDG_SEAT");
359 cvtnr
= pam_getenv(handle
, "XDG_VTNR");
361 cvtnr
= getenv("XDG_VTNR");
363 type
= pam_getenv(handle
, "XDG_SESSION_TYPE");
365 type
= getenv("XDG_SESSION_TYPE");
369 class = pam_getenv(handle
, "XDG_SESSION_CLASS");
371 class = getenv("XDG_SESSION_CLASS");
375 desktop
= pam_getenv(handle
, "XDG_SESSION_DESKTOP");
376 if (isempty(desktop
))
377 desktop
= getenv("XDG_SESSION_DESKTOP");
381 if (strchr(tty
, ':')) {
382 /* A tty with a colon is usually an X11 display,
383 * placed there to show up in utmp. We rearrange
384 * things and don't pretend that an X display was a
387 if (isempty(display
))
390 } else if (streq(tty
, "cron")) {
391 /* cron has been setting PAM_TTY to "cron" for a very
392 * long time and it probably shouldn't stop doing that
393 * for compatibility reasons. */
394 type
= "unspecified";
395 class = "background";
397 } else if (streq(tty
, "ssh")) {
398 /* ssh has been setting PAM_TTY to "ssh" for a very
399 * long time and probably shouldn't stop doing that
400 * for compatibility reasons. */
405 /* Chop off leading /dev prefix that some clients specify, but others do not. */
406 tty
= skip_dev_prefix(tty
);
408 /* If this fails vtnr will be 0, that's intended */
410 (void) safe_atou32(cvtnr
, &vtnr
);
412 if (!isempty(display
) && !vtnr
) {
414 get_seat_from_display(display
, &seat
, &vtnr
);
415 else if (streq(seat
, "seat0"))
416 get_seat_from_display(display
, NULL
, &vtnr
);
419 if (seat
&& !streq(seat
, "seat0") && vtnr
!= 0) {
420 pam_syslog(handle
, LOG_DEBUG
, "Ignoring vtnr %"PRIu32
" for %s which is not seat0", vtnr
, seat
);
425 type
= !isempty(display
) ? "x11" :
426 !isempty(tty
) ? "tty" : "unspecified";
429 class = streq(type
, "unspecified") ? "background" : "user";
431 remote
= !isempty(remote_host
) && !is_localhost(remote_host
);
433 (void) pam_get_data(handle
, "systemd.memory_max", (const void **)&memory_max
);
434 (void) pam_get_data(handle
, "systemd.tasks_max", (const void **)&tasks_max
);
435 (void) pam_get_data(handle
, "systemd.cpu_weight", (const void **)&cpu_weight
);
436 (void) pam_get_data(handle
, "systemd.io_weight", (const void **)&io_weight
);
438 /* Talk to logind over the message bus */
440 r
= sd_bus_open_system(&bus
);
442 pam_syslog(handle
, LOG_ERR
, "Failed to connect to system bus: %s", strerror(-r
));
443 return PAM_SESSION_ERR
;
447 pam_syslog(handle
, LOG_DEBUG
, "Asking logind to create session: "
448 "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",
449 pw
->pw_uid
, getpid_cached(),
451 type
, class, strempty(desktop
),
452 strempty(seat
), vtnr
, strempty(tty
), strempty(display
),
453 yes_no(remote
), strempty(remote_user
), strempty(remote_host
));
454 pam_syslog(handle
, LOG_DEBUG
, "Session limits: "
455 "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s",
456 strna(memory_max
), strna(tasks_max
), strna(cpu_weight
), strna(io_weight
));
459 r
= sd_bus_message_new_method_call(
462 "org.freedesktop.login1",
463 "/org/freedesktop/login1",
464 "org.freedesktop.login1.Manager",
467 pam_syslog(handle
, LOG_ERR
, "Failed to create CreateSession method call: %s", strerror(-r
));
468 return PAM_SESSION_ERR
;
471 r
= sd_bus_message_append(m
, "uusssssussbss",
472 (uint32_t) pw
->pw_uid
,
473 (uint32_t) getpid_cached(),
486 pam_syslog(handle
, LOG_ERR
, "Failed to append to bus message: %s", strerror(-r
));
487 return PAM_SESSION_ERR
;
490 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
492 pam_syslog(handle
, LOG_ERR
, "Failed to open message container: %s", strerror(-r
));
493 return PAM_SYSTEM_ERR
;
496 r
= append_session_memory_max(handle
, m
, memory_max
);
498 return PAM_SESSION_ERR
;
500 r
= append_session_tasks_max(handle
, m
, tasks_max
);
502 return PAM_SESSION_ERR
;
504 r
= append_session_cg_weight(handle
, m
, cpu_weight
, "CPUWeight");
506 return PAM_SESSION_ERR
;
508 r
= append_session_cg_weight(handle
, m
, io_weight
, "IOWeight");
510 return PAM_SESSION_ERR
;
512 r
= sd_bus_message_close_container(m
);
514 pam_syslog(handle
, LOG_ERR
, "Failed to close message container: %s", strerror(-r
));
515 return PAM_SYSTEM_ERR
;
518 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
520 if (sd_bus_error_has_name(&error
, BUS_ERROR_SESSION_BUSY
)) {
521 pam_syslog(handle
, LOG_DEBUG
, "Cannot create session: %s", bus_error_message(&error
, r
));
524 pam_syslog(handle
, LOG_ERR
, "Failed to create session: %s", bus_error_message(&error
, r
));
525 return PAM_SYSTEM_ERR
;
529 r
= sd_bus_message_read(reply
,
540 pam_syslog(handle
, LOG_ERR
, "Failed to parse message: %s", strerror(-r
));
541 return PAM_SESSION_ERR
;
545 pam_syslog(handle
, LOG_DEBUG
, "Reply from logind: "
546 "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u",
547 id
, object_path
, runtime_path
, session_fd
, seat
, vtnr
, original_uid
);
549 r
= pam_misc_setenv(handle
, "XDG_SESSION_ID", id
, 0);
550 if (r
!= PAM_SUCCESS
) {
551 pam_syslog(handle
, LOG_ERR
, "Failed to set session id.");
555 if (original_uid
== pw
->pw_uid
) {
556 /* Don't set $XDG_RUNTIME_DIR if the user we now
557 * authenticated for does not match the original user
558 * of the session. We do this in order not to result
559 * in privileged apps clobbering the runtime directory
562 r
= pam_misc_setenv(handle
, "XDG_RUNTIME_DIR", runtime_path
, 0);
563 if (r
!= PAM_SUCCESS
) {
564 pam_syslog(handle
, LOG_ERR
, "Failed to set runtime dir.");
568 r
= export_legacy_dbus_address(handle
, pw
->pw_uid
, runtime_path
);
569 if (r
!= PAM_SUCCESS
)
573 if (!isempty(seat
)) {
574 r
= pam_misc_setenv(handle
, "XDG_SEAT", seat
, 0);
575 if (r
!= PAM_SUCCESS
) {
576 pam_syslog(handle
, LOG_ERR
, "Failed to set seat.");
582 char buf
[DECIMAL_STR_MAX(vtnr
)];
583 sprintf(buf
, "%u", vtnr
);
585 r
= pam_misc_setenv(handle
, "XDG_VTNR", buf
, 0);
586 if (r
!= PAM_SUCCESS
) {
587 pam_syslog(handle
, LOG_ERR
, "Failed to set virtual terminal number.");
592 r
= pam_set_data(handle
, "systemd.existing", INT_TO_PTR(!!existing
), NULL
);
593 if (r
!= PAM_SUCCESS
) {
594 pam_syslog(handle
, LOG_ERR
, "Failed to install existing flag.");
598 if (session_fd
>= 0) {
599 session_fd
= fcntl(session_fd
, F_DUPFD_CLOEXEC
, 3);
600 if (session_fd
< 0) {
601 pam_syslog(handle
, LOG_ERR
, "Failed to dup session fd: %m");
602 return PAM_SESSION_ERR
;
605 r
= pam_set_data(handle
, "systemd.session-fd", FD_TO_PTR(session_fd
), NULL
);
606 if (r
!= PAM_SUCCESS
) {
607 pam_syslog(handle
, LOG_ERR
, "Failed to install session fd.");
608 safe_close(session_fd
);
616 _public_ PAM_EXTERN
int pam_sm_close_session(
617 pam_handle_t
*handle
,
619 int argc
, const char **argv
) {
621 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
622 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
623 const void *existing
= NULL
;
629 /* Only release session if it wasn't pre-existing when we
630 * tried to create it */
631 pam_get_data(handle
, "systemd.existing", &existing
);
633 id
= pam_getenv(handle
, "XDG_SESSION_ID");
634 if (id
&& !existing
) {
636 /* Before we go and close the FIFO we need to tell
637 * logind that this is a clean session shutdown, so
638 * that it doesn't just go and slaughter us
639 * immediately after closing the fd */
641 r
= sd_bus_open_system(&bus
);
643 pam_syslog(handle
, LOG_ERR
, "Failed to connect to system bus: %s", strerror(-r
));
644 return PAM_SESSION_ERR
;
647 r
= sd_bus_call_method(bus
,
648 "org.freedesktop.login1",
649 "/org/freedesktop/login1",
650 "org.freedesktop.login1.Manager",
657 pam_syslog(handle
, LOG_ERR
, "Failed to release session: %s", bus_error_message(&error
, r
));
658 return PAM_SESSION_ERR
;
662 /* Note that we are knowingly leaking the FIFO fd here. This
663 * way, logind can watch us die. If we closed it here it would
664 * not have any clue when that is completed. Given that one
665 * cannot really have multiple PAM sessions open from the same
666 * process this means we will leak one FD at max. */