From: Zbigniew Jędrzejewski-Szmek Date: Mon, 12 Jun 2023 07:47:00 +0000 (+0200) Subject: pam: add macro wrapper to make code shorter X-Git-Tag: v254-rc1~151 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=27ccba2621c2c62fa1b6b5f9f68a87db77504842;p=thirdparty%2Fsystemd.git pam: add macro wrapper to make code shorter The same pattern is repeated so many times that it seems worth making a shorthand for it. Follow-up for f71b55b51075e7ce42fa0ad4ae5569ba1aea6ee2 and 2675747f3cdd6f1e6236bbb2f79abfa53fb307f1. --- diff --git a/src/home/pam_systemd_home.c b/src/home/pam_systemd_home.c index aa93a86ca5c..5ef773bc8c7 100644 --- a/src/home/pam_systemd_home.c +++ b/src/home/pam_systemd_home.c @@ -149,18 +149,21 @@ static int acquire_user_record( r = bus_call_method(bus, bus_home_mgr, "GetUserRecordByName", &error, &reply, "s", username); if (r < 0) { if (bus_error_is_unknown_service(&error)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "systemd-homed is not available: %s", bus_error_message(&error, r)); + pam_debug_syslog(handle, debug, + "systemd-homed is not available: %s", + bus_error_message(&error, r)); goto user_unknown; } if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_HOME)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Not a user managed by systemd-homed: %s", bus_error_message(&error, r)); + pam_debug_syslog(handle, debug, + "Not a user managed by systemd-homed: %s", + bus_error_message(&error, r)); goto user_unknown; } - pam_syslog(handle, LOG_ERR, "Failed to query user record: %s", bus_error_message(&error, r)); + pam_syslog(handle, LOG_ERR, + "Failed to query user record: %s", bus_error_message(&error, r)); return PAM_SERVICE_ERR; } @@ -306,8 +309,7 @@ static int handle_generic_user_record_error( return PAM_CONV_ERR; /* no logging here */ if (isempty(newp)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Password request aborted."); + pam_debug_syslog(handle, debug, "Password request aborted."); return PAM_AUTHTOK_ERR; } @@ -332,8 +334,7 @@ static int handle_generic_user_record_error( return PAM_CONV_ERR; /* no logging here */ if (isempty(newp)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Recovery key request aborted."); + pam_debug_syslog(handle, debug, "Recovery key request aborted."); return PAM_AUTHTOK_ERR; } @@ -357,8 +358,7 @@ static int handle_generic_user_record_error( return PAM_CONV_ERR; /* no logging here */ if (isempty(newp)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Password request aborted."); + pam_debug_syslog(handle, debug, "Password request aborted."); return PAM_AUTHTOK_ERR; } @@ -377,8 +377,7 @@ static int handle_generic_user_record_error( return PAM_CONV_ERR; /* no logging here */ if (isempty(newp)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); + pam_debug_syslog(handle, debug, "PIN request aborted."); return PAM_AUTHTOK_ERR; } @@ -435,8 +434,7 @@ static int handle_generic_user_record_error( return PAM_CONV_ERR; /* no logging here */ if (isempty(newp)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); + pam_debug_syslog(handle, debug, "PIN request aborted."); return PAM_AUTHTOK_ERR; } @@ -455,8 +453,7 @@ static int handle_generic_user_record_error( return PAM_CONV_ERR; /* no logging here */ if (isempty(newp)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); + pam_debug_syslog(handle, debug, "PIN request aborted."); return PAM_AUTHTOK_ERR; } @@ -475,8 +472,7 @@ static int handle_generic_user_record_error( return PAM_CONV_ERR; /* no logging here */ if (isempty(newp)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); + pam_debug_syslog(handle, debug, "PIN request aborted."); return PAM_AUTHTOK_ERR; } @@ -720,8 +716,7 @@ _public_ PAM_EXTERN int pam_sm_authenticate( &debug) < 0) return PAM_AUTH_ERR; - if (debug) - pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed authenticating"); + pam_debug_syslog(handle, debug, "pam-systemd-homed authenticating"); return acquire_home(handle, /* please_authenticate= */ true, suspend_please, debug, NULL); } @@ -751,8 +746,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( &debug) < 0) return PAM_SESSION_ERR; - if (debug) - pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed session start"); + pam_debug_syslog(handle, debug, "pam-systemd-homed session start"); r = acquire_home(handle, /* please_authenticate = */ false, suspend_please, debug, &d); if (r == PAM_USER_UNKNOWN) /* Not managed by us? Don't complain. */ @@ -791,8 +785,7 @@ _public_ PAM_EXTERN int pam_sm_close_session( &debug) < 0) return PAM_SESSION_ERR; - if (debug) - pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed session end"); + pam_debug_syslog(handle, debug, "pam-systemd-homed session end"); r = pam_get_user(handle, &username, NULL); if (r != PAM_SUCCESS) @@ -823,11 +816,11 @@ _public_ PAM_EXTERN int pam_sm_close_session( r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL); if (r < 0) { - if (sd_bus_error_has_name(&error, BUS_ERROR_HOME_BUSY)) - pam_syslog(handle, LOG_NOTICE, "Not deactivating home directory of %s, as it is still used.", username); - else + if (!sd_bus_error_has_name(&error, BUS_ERROR_HOME_BUSY)) return pam_syslog_pam_error(handle, LOG_ERR, PAM_SESSION_ERR, "Failed to release user home: %s", bus_error_message(&error, r)); + + pam_syslog(handle, LOG_NOTICE, "Not deactivating home directory of %s, as it is still used.", username); } return PAM_SUCCESS; @@ -853,8 +846,7 @@ _public_ PAM_EXTERN int pam_sm_acct_mgmt( &debug) < 0) return PAM_AUTH_ERR; - if (debug) - pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed account management"); + pam_debug_syslog(handle, debug, "pam-systemd-homed account management"); r = acquire_home(handle, /* please_authenticate = */ false, please_suspend, debug, NULL); if (r != PAM_SUCCESS) @@ -965,8 +957,7 @@ _public_ PAM_EXTERN int pam_sm_chauthtok( &debug) < 0) return PAM_AUTH_ERR; - if (debug) - pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed account management"); + pam_debug_syslog(handle, debug, "pam-systemd-homed account management"); r = pam_acquire_bus_connection(handle, "pam-systemd-home", &bus, NULL); if (r != PAM_SUCCESS) @@ -994,8 +985,7 @@ _public_ PAM_EXTERN int pam_sm_chauthtok( return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get new password: @PAMERR@"); if (isempty(new_password)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Password request aborted."); + pam_debug_syslog(handle, debug, "Password request aborted."); return PAM_AUTHTOK_ERR; } diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c index 021b380507e..0c5d51c3a34 100644 --- a/src/login/pam_systemd.c +++ b/src/login/pam_systemd.c @@ -594,8 +594,7 @@ static int pam_putenv_and_log(pam_handle_t *handle, const char *e, bool debug) { return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to set PAM environment variable %s: @PAMERR@", e); - if (debug) - pam_syslog(handle, LOG_DEBUG, "PAM environment variable %s set based on user record.", e); + pam_debug_syslog(handle, debug, "PAM environment variable %s set based on user record.", e); return PAM_SUCCESS; } @@ -613,9 +612,7 @@ static int apply_user_record_settings( if (ur->umask != MODE_INVALID) { umask(ur->umask); - - if (debug) - pam_syslog(handle, LOG_DEBUG, "Set user umask to %04o based on user record.", ur->umask); + pam_debug_syslog(handle, debug, "Set user umask to %04o based on user record.", ur->umask); } STRV_FOREACH(i, ur->environment) { @@ -629,8 +626,8 @@ static int apply_user_record_settings( return pam_log_oom(handle); if (pam_getenv(handle, n)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PAM environment variable $%s already set, not changing based on record.", *i); + pam_debug_syslog(handle, debug, + "PAM environment variable $%s already set, not changing based on record.", *i); continue; } @@ -640,10 +637,10 @@ static int apply_user_record_settings( } if (ur->email_address) { - if (pam_getenv(handle, "EMAIL")) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PAM environment variable $EMAIL already set, not changing based on user record."); - } else { + if (pam_getenv(handle, "EMAIL")) + pam_debug_syslog(handle, debug, + "PAM environment variable $EMAIL already set, not changing based on user record."); + else { _cleanup_free_ char *joined = NULL; joined = strjoin("EMAIL=", ur->email_address); @@ -657,13 +654,13 @@ static int apply_user_record_settings( } if (ur->time_zone) { - if (pam_getenv(handle, "TZ")) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PAM environment variable $TZ already set, not changing based on user record."); - } else if (!timezone_is_valid(ur->time_zone, LOG_DEBUG)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Time zone specified in user record is not valid locally, not setting $TZ."); - } else { + if (pam_getenv(handle, "TZ")) + pam_debug_syslog(handle, debug, + "PAM environment variable $TZ already set, not changing based on user record."); + else if (!timezone_is_valid(ur->time_zone, LOG_DEBUG)) + pam_debug_syslog(handle, debug, + "Time zone specified in user record is not valid locally, not setting $TZ."); + else { _cleanup_free_ char *joined = NULL; joined = strjoin("TZ=:", ur->time_zone); @@ -677,13 +674,13 @@ static int apply_user_record_settings( } if (ur->preferred_language) { - if (pam_getenv(handle, "LANG")) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "PAM environment variable $LANG already set, not changing based on user record."); - } else if (locale_is_installed(ur->preferred_language) <= 0) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Preferred language specified in user record is not valid or not installed, not setting $LANG."); - } else { + if (pam_getenv(handle, "LANG")) + pam_debug_syslog(handle, debug, + "PAM environment variable $LANG already set, not changing based on user record."); + else if (locale_is_installed(ur->preferred_language) <= 0) + pam_debug_syslog(handle, debug, + "Preferred language specified in user record is not valid or not installed, not setting $LANG."); + else { _cleanup_free_ char *joined = NULL; joined = strjoin("LANG=", ur->preferred_language); @@ -700,9 +697,9 @@ static int apply_user_record_settings( if (nice(ur->nice_level) < 0) pam_syslog_errno(handle, LOG_ERR, errno, "Failed to set nice level to %i, ignoring: %m", ur->nice_level); - else if (debug) - pam_syslog(handle, LOG_DEBUG, - "Nice level set to %i, based on user record.", ur->nice_level); + else + pam_debug_syslog(handle, debug, + "Nice level set to %i, based on user record.", ur->nice_level); } for (int rl = 0; rl < _RLIMIT_MAX; rl++) { @@ -714,9 +711,9 @@ static int apply_user_record_settings( if (r < 0) pam_syslog_errno(handle, LOG_ERR, r, "Failed to set resource limit %s, ignoring: %m", rlimit_to_string(rl)); - else if (debug) - pam_syslog(handle, LOG_DEBUG, - "Resource limit %s set, based on user record.", rlimit_to_string(rl)); + else + pam_debug_syslog(handle, debug, + "Resource limit %s set, based on user record.", rlimit_to_string(rl)); } uint64_t a, b; @@ -823,8 +820,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( &default_capability_ambient_set) < 0) return PAM_SESSION_ERR; - if (debug) - pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); + pam_debug_syslog(handle, debug, "pam-systemd initializing"); r = acquire_user_record(handle, &ur); if (r != PAM_SUCCESS) @@ -920,8 +916,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( } if (seat && !streq(seat, "seat0") && vtnr != 0) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); + pam_debug_syslog(handle, debug, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); vtnr = 0; } @@ -955,18 +950,18 @@ _public_ PAM_EXTERN int pam_sm_open_session( if (r != PAM_SUCCESS) return r; - if (debug) { - pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " - "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", - ur->uid, getpid_cached(), - strempty(service), - type, class, strempty(desktop), - strempty(seat), vtnr, strempty(tty), strempty(display), - yes_no(remote), strempty(remote_user), strempty(remote_host)); - pam_syslog(handle, LOG_DEBUG, "Session limits: " - "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s runtime_max_sec=%s", - strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight), strna(runtime_max_sec)); - } + pam_debug_syslog(handle, debug, + "Asking logind to create session: " + "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", + ur->uid, getpid_cached(), + strempty(service), + type, class, strempty(desktop), + strempty(seat), vtnr, strempty(tty), strempty(display), + yes_no(remote), strempty(remote_user), strempty(remote_host)); + pam_debug_syslog(handle, debug, + "Session limits: " + "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s runtime_max_sec=%s", + strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight), strna(runtime_max_sec)); r = bus_message_new_method_call(bus, &m, bus_login_mgr, "CreateSession"); if (r < 0) @@ -1020,13 +1015,13 @@ _public_ PAM_EXTERN int pam_sm_open_session( r = sd_bus_call(bus, m, LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC, &error, &reply); if (r < 0) { if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { - if (debug) - pam_syslog(handle, LOG_DEBUG, "Not creating session: %s", bus_error_message(&error, r)); - + pam_debug_syslog(handle, debug, + "Not creating session: %s", bus_error_message(&error, r)); /* We are already in a session, don't do anything */ goto success; } else { - pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); + pam_syslog(handle, LOG_ERR, + "Failed to create session: %s", bus_error_message(&error, r)); return PAM_SESSION_ERR; } } @@ -1044,10 +1039,10 @@ _public_ PAM_EXTERN int pam_sm_open_session( if (r < 0) return pam_bus_log_parse_error(handle, r); - if (debug) - pam_syslog(handle, LOG_DEBUG, "Reply from logind: " - "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", - id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); + pam_debug_syslog(handle, debug, + "Reply from logind: " + "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", + id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); r = update_environment(handle, "XDG_SESSION_ID", id); if (r != PAM_SUCCESS) @@ -1136,8 +1131,7 @@ _public_ PAM_EXTERN int pam_sm_close_session( NULL) < 0) return PAM_SESSION_ERR; - if (debug) - pam_syslog(handle, LOG_DEBUG, "pam-systemd shutting down"); + pam_debug_syslog(handle, debug, "pam-systemd shutting down"); /* Only release session if it wasn't pre-existing when we * tried to create it */ diff --git a/src/shared/pam-util.h b/src/shared/pam-util.h index 5afabf257b7..5a05fb71f11 100644 --- a/src/shared/pam-util.h +++ b/src/shared/pam-util.h @@ -9,6 +9,12 @@ int pam_syslog_errno(pam_handle_t *handle, int level, int error, const char *for int pam_syslog_pam_error(pam_handle_t *handle, int level, int error, const char *format, ...) _printf_(4,5); +/* Call pam_vsyslog if debug is enabled */ +#define pam_debug_syslog(handle, debug, fmt, ...) ({ \ + if (debug) \ + pam_syslog(handle, LOG_DEBUG, fmt, ## __VA_ARGS__); \ + }) + static inline int pam_log_oom(pam_handle_t *handle) { /* This is like log_oom(), but uses PAM logging */ return pam_syslog_errno(handle, LOG_ERR, ENOMEM, "Out of memory.");