From 607e6b7ce8658e552fdffbb99c51cdb01828e27b Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 28 Feb 2012 11:16:16 +0100 Subject: [PATCH] login: support crazy shadow-utils syntax in login.defs standard syntax for FOO=data: ENV_FOO data additional syntax: ENV_FOO FOO=data Reported-by: Colin Guthrie colin@mageia.org Signed-off-by: Karel Zak --- login-utils/login.c | 11 ++++------- login-utils/logindefs.c | 37 +++++++++++++++++++++++++++++++++++++ login-utils/logindefs.h | 1 + 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/login-utils/login.c b/login-utils/login.c index 84d8b1bdb..bc0eaec26 100644 --- a/login-utils/login.c +++ b/login-utils/login.c @@ -1145,13 +1145,10 @@ static void init_environ(struct login_context *cxt) setenv("TERM", termenv, 1); if (pwd->pw_uid) - setenv("PATH", getlogindefs_str("ENV_PATH", _PATH_DEFPATH), 1); - else { - const char *x = getlogindefs_str("ENV_ROOTPATH", NULL); - if (!x) - x = getlogindefs_str("ENV_SUPATH", _PATH_DEFPATH_ROOT); - setenv("PATH", x, 1); - } + logindefs_setenv("PATH", "ENV_PATH", _PATH_DEFPATH); + + else if (logindefs_setenv("PATH", "ENV_ROOTPATH", NULL) != 0) + logindefs_setenv("PATH", "ENV_SUPATH", _PATH_DEFPATH_ROOT); /* mailx will give a funny error msg if you forget this one */ len = snprintf(tmp, sizeof(tmp), "%s/%s", _PATH_MAILDIR, pwd->pw_name); diff --git a/login-utils/logindefs.c b/login-utils/logindefs.c index fe590e990..e9517acac 100644 --- a/login-utils/logindefs.c +++ b/login-utils/logindefs.c @@ -211,6 +211,43 @@ const char *getlogindefs_str(const char *name, const char *dflt) return ptr->value; } +/* + * For compatibililty with shadow-utils we have tu support additional + * syntax for environment variables in login.defs(5) file. The standard + * syntax is: + * + * ENV_FOO data + * + * but shadow-utils supports also + * + * ENV_FOO FOO=data + * + * the FOO= prefix has to be remove before we call setenv(). + */ +int logindefs_setenv(const char *name, const char *conf, const char *dflt) +{ + const char *val = getlogindefs_str(conf, dflt); + const char *p; + + if (!val) + return -1; + + p = strchr(val, '='); + if (p) { + size_t sz = strlen(name); + + if (strncmp(val, name, sz) == 0 && *(p + 1)) { + val = p + 1; + if (*val == '"') + val++; + if (!*val) + val = dflt; + } + } + + return val ? setenv(name, val, 1) : -1; +} + #ifdef TEST_PROGRAM int main(int argc, char *argv[]) { diff --git a/login-utils/logindefs.h b/login-utils/logindefs.h index 37d19e1f7..163869dfd 100644 --- a/login-utils/logindefs.h +++ b/login-utils/logindefs.h @@ -5,5 +5,6 @@ extern int getlogindefs_bool(const char *name, int dflt); extern long getlogindefs_num(const char *name, 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); #endif /* UTIL_LINUX_LOGINDEFS_H */