cronie/forward-XDG_SESSION_CLASS-to-PAM-for-session-classification.patch
2026-05-19 19:47:50 -04:00

71 lines
2.6 KiB
Diff

From 5f9f16b5663becefdd0dd70df31c0ef5ac36f943 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Poho=C5=99elsk=C3=BD?= <opohorel@redhat.com>
Date: Thu, 27 Nov 2025 14:38:47 +0100
Subject: [PATCH] Forward XDG_SESSION_CLASS to PAM for session classification
Allow users to set the XDG_SESSION_CLASS environment variable in their
crontab to control the systemd session class for cron jobs. When set,
this value is forwarded to the PAM environment so that pam_systemd can
use it for session classification (e.g., "background-light").
This enables cron jobs to run in lighter-weight sessions that consume
fewer system resources.
---
man/crontab.5 | 10 ++++++++++
src/security.c | 21 +++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/man/crontab.5 b/man/crontab.5
index 68380c6..7487cc5 100644
--- a/man/crontab.5
+++ b/man/crontab.5
@@ -157,6 +157,16 @@ upper limit specified by the variable. The random scaling factor is
determined during the cron daemon startup so it remains constant for
the whole run time of the daemon.
.PP
+The
+.I XDG_SESSION_CLASS
+variable specifies the session class to be used when PAM creates a systemd
+session for the cron job. If set (e.g., to "background-light"), this value
+is forwarded to the PAM environment so that
+.BR pam_systemd (8)
+can use it for session classification. This allows cron jobs to run in
+lighter-weight sessions that consume fewer system resources. This variable
+has no effect if crond was built without PAM support.
+.PP
The format of a cron command is similar to the V7 standard, with a number
of upward-compatible extensions. Each line has five time-and-date fields
followed by a
diff --git a/src/security.c b/src/security.c
index 14a514a..67b6d26 100644
--- a/src/security.c
+++ b/src/security.c
@@ -131,6 +131,27 @@ int cron_set_job_security_context(entry *e, user *u ATTRIBUTE_UNUSED,
pam_strerror(pamh, ret), 0);
return -1;
}
+
+ /* Forward XDG_SESSION_CLASS from crontab environment to PAM
+ * so that pam_systemd.so can use it for session classification */
+#ifdef HAVE_PAM_PUTENV
+ if (pamh != NULL) {
+ char *xdg_session_class = env_get("XDG_SESSION_CLASS", e->envp);
+ if (xdg_session_class != NULL) {
+ char *xdg_session_class_env = NULL;
+ if (asprintf(&xdg_session_class_env, "XDG_SESSION_CLASS=%s",
+ xdg_session_class) >= 0) {
+ ret = pam_putenv(pamh, xdg_session_class_env);
+ if (ret != PAM_SUCCESS) {
+ log_it(e->pwd->pw_name, getpid(),
+ "WARNING: Failed to set XDG_SESSION_CLASS in PAM environment",
+ pam_strerror(pamh, ret), 0);
+ }
+ free(xdg_session_class_env);
+ }
+ }
+ }
+#endif
#endif
#ifdef WITH_SELINUX