From 7b0423d33cc70928ae1138d95a48d0031f60c90a Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 7 May 2025 12:03:51 +0200 Subject: [PATCH] logindefs: Add function to set PATH Let's avoid duplicate code in su() and login(1). Signed-off-by: Karel Zak --- include/logindefs.h | 3 +++ lib/logindefs.c | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/logindefs.h b/include/logindefs.h index 24732ca70..35d65621e 100644 --- a/include/logindefs.h +++ b/include/logindefs.h @@ -28,7 +28,10 @@ extern int getlogindefs_bool(const char *name, int dflt); extern unsigned long getlogindefs_num(const char *name, unsigned long dflt); extern const char *getlogindefs_str(const char *name, const char *dflt); extern void free_getlogindefs_data(void); + extern int logindefs_setenv(const char *name, const char *conf, const char *dflt); +extern int logindefs_setenv_path(uid_t uid); + extern int effective_access(const char *path, int mode); extern int get_hushlogin_status(struct passwd *pwd, const char *override_home, int force_check); diff --git a/lib/logindefs.c b/lib/logindefs.c index 4fee83957..079f2dabf 100644 --- a/lib/logindefs.c +++ b/lib/logindefs.c @@ -424,6 +424,22 @@ int logindefs_setenv(const char *name, const char *conf, const char *dflt) return val ? setenv(name, val, 1) : -1; } +/* + * logindefs based setenv("PATH") + */ +int logindefs_setenv_path(uid_t uid) +{ + int rc = 0; + + if (uid) + rc = logindefs_setenv("PATH", "ENV_PATH", _PATH_DEFPATH); + + else if ((rc = logindefs_setenv("PATH", "ENV_SUPATH", NULL)) != 0) + rc = logindefs_setenv("PATH", "ENV_ROOTPATH", _PATH_DEFPATH_ROOT); + + return rc; +} + /* * We need to check the effective UID/GID. For example, $HOME could be on a * root-squashed NFS or on an NFS with UID mapping, and access(2) uses the -- 2.47.2