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 <kzak@redhat.com>
This commit is contained in:
Karel Zak 2012-02-28 11:16:16 +01:00
parent 7dc2009633
commit 607e6b7ce8
3 changed files with 42 additions and 7 deletions

View File

@ -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);

View File

@ -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[])
{

View File

@ -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 */