summaryrefslogtreecommitdiffstats
path: root/src/shared/pam-util.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-10-04 14:19:12 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-10-10 09:55:21 +0200
commitb1eff892bb7c4ba2fac2b1fdf54d86534aaf2d08 (patch)
treec91027fa63ac201f47041ac56fb46497c9b93025 /src/shared/pam-util.c
parentbasic/errno-util: add helper to protect and set errno in one step (diff)
downloadsystemd-b1eff892bb7c4ba2fac2b1fdf54d86534aaf2d08.tar.xz
systemd-b1eff892bb7c4ba2fac2b1fdf54d86534aaf2d08.zip
shared/pam-util: add pam_syslog_errno() wrapper that sets errno
So far our pam code was using strerror_safe(). But that's not a good approach, because strerror_safe() is not thread-safe, and the pam code is "library code" that should be thread-safe. In fact, the whole effort to use strerror() is unnecessary, because pam_syslog() is documented to support %m. The implementation in linux-pam simply uses vasprintf(). If we use %m too, we get rid of the issue. The wrapper sets errno temporarily from the argument. Apparently some PAM consumers run multiple PAM stacks in threads, so we should avoid non-thread-safe code. The new helper returns PAM_BUF_ERR for ENOMEM, and PAM_SERVICE_ERR in other cases. This may change the returned code in some cases, but I think a) it doesn't matter much, b) it's probably for the better. E.g. we might now return PAM_SERVICE_ERR if the dbus message is borked, and PAM_SERVICE_ERR seems appropriate.
Diffstat (limited to 'src/shared/pam-util.c')
-rw-r--r--src/shared/pam-util.c28
1 files changed, 10 insertions, 18 deletions
diff --git a/src/shared/pam-util.c b/src/shared/pam-util.c
index 621e7fe802..d9fee22cc6 100644
--- a/src/shared/pam-util.c
+++ b/src/shared/pam-util.c
@@ -9,22 +9,16 @@
#include "macro.h"
#include "pam-util.h"
-int pam_log_oom(pam_handle_t *handle) {
- /* This is like log_oom(), but uses PAM logging */
- pam_syslog(handle, LOG_ERR, "Out of memory.");
- return PAM_BUF_ERR;
-}
+int pam_syslog_errno(pam_handle_t *handle, int level, int error, const char *format, ...) {
+ va_list ap;
-int pam_bus_log_create_error(pam_handle_t *handle, int r) {
- /* This is like bus_log_create_error(), but uses PAM logging */
- pam_syslog(handle, LOG_ERR, "Failed to create bus message: %s", strerror_safe(r));
- return PAM_BUF_ERR;
-}
+ LOCAL_ERRNO(error);
-int pam_bus_log_parse_error(pam_handle_t *handle, int r) {
- /* This is like bus_log_parse_error(), but uses PAM logging */
- pam_syslog(handle, LOG_ERR, "Failed to parse bus message: %s", strerror_safe(r));
- return PAM_BUF_ERR;
+ va_start(ap, format);
+ pam_vsyslog(handle, LOG_ERR, format, ap);
+ va_end(ap);
+
+ return error == -ENOMEM ? PAM_BUF_ERR : PAM_SERVICE_ERR;
}
static void cleanup_system_bus(pam_handle_t *handle, void *data, int error_status) {
@@ -50,10 +44,8 @@ int pam_acquire_bus_connection(pam_handle_t *handle, sd_bus **ret) {
}
r = sd_bus_open_system(&bus);
- if (r < 0) {
- pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror_safe(r));
- return PAM_SERVICE_ERR;
- }
+ if (r < 0)
+ return pam_syslog_errno(handle, LOG_ERR, r, "Failed to connect to system bus: %m");
r = pam_set_data(handle, "systemd-system-bus", bus, cleanup_system_bus);
if (r != PAM_SUCCESS) {