2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2001 - 2006, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2007 - 2010, Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separated from setup.c. --marekm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2009-04-25 04:16:06 +05:30
|
|
|
#include <assert.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
2023-02-05 03:11:18 +05:30
|
|
|
|
|
|
|
#include "alloc.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include <pwd.h>
|
|
|
|
#include "getdef.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2007-10-07 17:17:22 +05:30
|
|
|
|
|
|
|
#ifndef USE_PAM
|
2007-10-07 17:14:02 +05:30
|
|
|
static void
|
2007-10-07 17:16:07 +05:30
|
|
|
addenv_path (const char *varname, const char *dirname, const char *filename)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *buf;
|
2009-04-25 04:16:06 +05:30
|
|
|
size_t len = strlen (dirname) + strlen (filename) + 2;
|
|
|
|
int wlen;
|
|
|
|
|
2023-02-05 03:11:18 +05:30
|
|
|
buf = XMALLOCARRAY (len, char);
|
2009-04-25 04:16:06 +05:30
|
|
|
wlen = snprintf (buf, len, "%s/%s", dirname, filename);
|
|
|
|
assert (wlen == (int) len - 1);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv (varname, buf);
|
|
|
|
free (buf);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
static void read_env_file (const char *filename)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buf[1024];
|
|
|
|
char *cp, *name, *val;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
fp = fopen (filename, "r");
|
2009-04-25 04:16:06 +05:30
|
|
|
if (NULL == fp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
2009-04-25 04:16:06 +05:30
|
|
|
}
|
* libmisc/limits.c: Avoid implicit conversion of integer to
boolean.
* libmisc/basename.c: Avoid implicit conversion of pointer to
boolean.
* libmisc/basename.c, lib/prototypes.h (Basename): Return a
constant string.
* libmisc/basename.c, libmisc/obscure.c, lib/prototypes.h,
libmisc/xmalloc.c, libmisc/getdate.h, libmisc/system.c,
libmisc/getgr_nam_gid.c, libmisc/failure.c, libmisc/valid.c: Add
splint annotations.
* libmisc/chowndir.c: Avoid memory leak.
* libmisc/chowndir.c: Do not check *printf/*puts return value.
* libmisc/chowntty.c: Avoid implicit conversion between integer
types.
* libmisc/obscure.c: Return a bool when possible instead of int.
* libmisc/shell.c: Do not check *printf/*puts return value.
* libmisc/shell.c: Do not check execle return value.
* libmisc/setupenv.c: Avoid implicit conversion between integer
types.
* libmisc/xmalloc.c: size should not be zero to avoid returning
NULL pointers.
* libmisc/hushed.c: Do not check *printf/*puts return value.
* libmisc/system.c: Avoid implicit conversion of integer to
boolean. safe_system last argument is a boolean.
* libmisc/system.c: Check return value of dup2.
* libmisc/system.c: Do not check *printf/*puts return value.
* libmisc/system.c: Do not check execve return value.
* libmisc/salt.c: Do not check *printf/*puts return value.
* libmisc/loginprompt.c: Do not check gethostname return value.
* libmisc/find_new_gid.c, libmisc/find_new_uid.c: Do not check
gr_rewind/pw_rewind return value.
* libmisc/ttytype.c: Limit the number of parsed characters in the
sscanf format.
* libmisc/ttytype.c: Test if a type was really read.
* libmisc/sub.c: Do not check *printf/*puts return value.
* libmisc/sub.c: Avoid implicit conversion of integer to boolean.
* src/userdel.c: Fix typo in comment.
* src/userdel.c: Avoid implicit conversion of boolean to integer.
* src/userdel.c: safe_system last argument is a boolean.
* src/newusers.c: Avoid implicit conversion of boolean to integer.
* src/newusers.c: Avoid implicit conversion of integer to boolean.
* src/usermod.c: Add brackets.
* src/usermod.c: Avoid implicit conversion of characters or
integers to booleans.
* src/vipw.c: Avoid implicit conversion of integer to boolean.
* src/su.c: Avoid implicit conversion of integer to boolean.
* src/su.c: Add brackets.
* src/useradd.c: Avoid implicit conversion of characters or
integers to booleans.
2010-08-23 00:43:53 +05:30
|
|
|
while (fgets (buf, (int)(sizeof buf), fp) == buf) {
|
2007-10-07 17:15:23 +05:30
|
|
|
cp = strrchr (buf, '\n');
|
2009-04-25 04:16:06 +05:30
|
|
|
if (NULL == cp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2009-04-25 04:16:06 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
*cp = '\0';
|
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
/* ignore whitespace and comments */
|
2009-04-23 23:15:42 +05:30
|
|
|
while (('\0' != *cp) && isspace (*cp)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
cp++;
|
2009-04-23 23:15:42 +05:30
|
|
|
}
|
|
|
|
if (('\0' == *cp) || ('#' == *cp)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2009-04-23 23:15:42 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* ignore lines which don't follow the name=value format
|
|
|
|
* (for example, the "export NAME" shell commands)
|
|
|
|
*/
|
|
|
|
name = cp;
|
2009-04-23 23:15:42 +05:30
|
|
|
while (('\0' != *cp) && !isspace (*cp) && ('=' != *cp)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
cp++;
|
2009-04-23 23:15:42 +05:30
|
|
|
}
|
|
|
|
if ('=' != *cp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2009-04-23 23:15:42 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
/* NUL-terminate the name */
|
2009-04-23 23:15:42 +05:30
|
|
|
*cp = '\0';
|
|
|
|
cp++;
|
2007-10-07 17:14:02 +05:30
|
|
|
val = cp;
|
2007-10-07 17:15:23 +05:30
|
|
|
#if 0 /* XXX untested, and needs rewrite with fewer goto's :-) */
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
(state, char_type) -> (state, action)
|
|
|
|
|
|
|
|
state: unquoted, single_quoted, double_quoted, escaped, double_quoted_escaped
|
|
|
|
char_type: normal, white, backslash, single, double
|
|
|
|
action: remove_curr, remove_curr_skip_next, remove_prev, finish XXX
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
no_quote:
|
2007-10-07 17:14:02 +05:30
|
|
|
if (*cp == '\\') {
|
|
|
|
/* remove the backslash */
|
2007-10-07 17:15:23 +05:30
|
|
|
remove_char (cp);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* skip over the next character */
|
|
|
|
if (*cp)
|
|
|
|
cp++;
|
|
|
|
goto no_quote;
|
|
|
|
} else if (*cp == '\'') {
|
|
|
|
/* remove the quote */
|
2007-10-07 17:15:23 +05:30
|
|
|
remove_char (cp);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* now within single quotes */
|
|
|
|
goto s_quote;
|
|
|
|
} else if (*cp == '"') {
|
|
|
|
/* remove the quote */
|
2007-10-07 17:15:23 +05:30
|
|
|
remove_char (cp);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* now within double quotes */
|
|
|
|
goto d_quote;
|
|
|
|
} else if (*cp == '\0') {
|
|
|
|
/* end of string */
|
|
|
|
goto finished;
|
2007-10-07 17:15:23 +05:30
|
|
|
} else if (isspace (*cp)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
/* unescaped whitespace - end of string */
|
|
|
|
*cp = '\0';
|
|
|
|
goto finished;
|
|
|
|
} else {
|
|
|
|
cp++;
|
|
|
|
goto no_quote;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
s_quote:
|
2007-10-07 17:14:02 +05:30
|
|
|
if (*cp == '\'') {
|
|
|
|
/* remove the quote */
|
2007-10-07 17:15:23 +05:30
|
|
|
remove_char (cp);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* unquoted again */
|
|
|
|
goto no_quote;
|
|
|
|
} else if (*cp == '\0') {
|
|
|
|
/* end of string */
|
|
|
|
goto finished;
|
|
|
|
} else {
|
|
|
|
/* preserve everything within single quotes */
|
|
|
|
cp++;
|
|
|
|
goto s_quote;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
d_quote:
|
2007-10-07 17:14:02 +05:30
|
|
|
if (*cp == '\"') {
|
|
|
|
/* remove the quote */
|
2007-10-07 17:15:23 +05:30
|
|
|
remove_char (cp);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* unquoted again */
|
|
|
|
goto no_quote;
|
|
|
|
} else if (*cp == '\\') {
|
|
|
|
cp++;
|
|
|
|
/* if backslash followed by double quote, remove backslash
|
|
|
|
else skip over the backslash and following char */
|
|
|
|
if (*cp == '"')
|
2007-10-07 17:15:23 +05:30
|
|
|
remove_char (cp - 1);
|
2007-10-07 17:14:02 +05:30
|
|
|
else if (*cp)
|
|
|
|
cp++;
|
|
|
|
goto d_quote;
|
2007-10-07 17:15:23 +05:30
|
|
|
}
|
2017-10-23 00:38:39 +05:30
|
|
|
else if (*cp == '\0') {
|
2007-10-07 17:14:02 +05:30
|
|
|
/* end of string */
|
|
|
|
goto finished;
|
|
|
|
} else {
|
|
|
|
/* preserve everything within double quotes */
|
|
|
|
goto d_quote;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
finished:
|
|
|
|
#endif /* 0 */
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* XXX - should handle quotes, backslash escapes, etc.
|
|
|
|
* like the shell does.
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv (name, val);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2009-04-23 23:15:42 +05:30
|
|
|
(void) fclose (fp);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* USE_PAM */
|
2007-10-07 17:14:59 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* change to the user's home directory
|
|
|
|
* set the HOME, SHELL, MAIL, PATH, and LOGNAME or USER environmental
|
|
|
|
* variables.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void setup_env (struct passwd *info)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
#ifndef USE_PAM
|
2010-08-22 00:52:32 +05:30
|
|
|
const char *envf;
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
* libmisc/console.c, libmisc/motd.c, libmisc/setupenv.c,
libmisc/sulog.c, libmisc/hushed.c, libmisc/failure.c,
libmisc/loginprompt.c, libmisc/ttytype.c,
libmisc/pam_pass_non_interractive.c, src/userdel.c, src/login.c,
lib/commonio.c, lib/commonio.h: Fix some const issues.
* libmisc/motd.c: Avoid multi-statements lines.
* libmisc/motd.c: Support long MOTD_FILE.
* libmisc/list.c, lib/prototypes.h: Revert previous change.
dup_list and is_on_list are used with members as defined for the
group structure, and thus even if the list is not modified, the
list elements cannot be constant strings.
* libmisc/system.c: Avoid C++ comments.
* src/vipw.c: WITH_TCB cannot be tested inside a gettextized
string. Split the Usage string.
* lib/commonio.h: Re-indent.
2010-08-21 21:02:53 +05:30
|
|
|
const char *cp;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Change the current working directory to be the home directory
|
|
|
|
* of the user. It is a fatal error for this process to be unable
|
|
|
|
* to change to that directory. There is no "default" home
|
|
|
|
* directory.
|
|
|
|
*
|
|
|
|
* We no longer do it as root - should work better on NFS-mounted
|
|
|
|
* home directories. Some systems default to HOME=/, so we make
|
|
|
|
* this a configurable option. --marekm
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (chdir (info->pw_dir) == -1) {
|
2007-10-07 17:14:02 +05:30
|
|
|
static char temp_pw_dir[] = "/";
|
2007-10-07 17:15:23 +05:30
|
|
|
|
|
|
|
if (!getdef_bool ("DEFAULT_HOME") || chdir ("/") == -1) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(), _("Unable to cd to '%s'\n"),
|
2007-10-07 17:15:23 +05:30
|
|
|
info->pw_dir);
|
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"unable to cd to `%s' for user `%s'\n",
|
|
|
|
info->pw_dir, info->pw_name));
|
|
|
|
closelog ();
|
2009-04-23 23:15:42 +05:30
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2009-04-23 23:15:42 +05:30
|
|
|
(void) puts (_("No directory, logging in with HOME=/"));
|
2013-08-02 23:58:24 +05:30
|
|
|
free (info->pw_dir);
|
2013-08-02 23:54:20 +05:30
|
|
|
info->pw_dir = xstrdup (temp_pw_dir);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the HOME environmental variable and export it.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv ("HOME", info->pw_dir);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the SHELL environmental variable and export it.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 05:20:03 +05:30
|
|
|
if ((NULL == info->pw_shell) || ('\0' == *info->pw_shell)) {
|
2009-07-22 19:00:06 +05:30
|
|
|
static char temp_pw_shell[] = SHELL;
|
2007-10-07 17:15:23 +05:30
|
|
|
|
2013-08-02 23:58:24 +05:30
|
|
|
free (info->pw_shell);
|
2013-08-02 23:54:20 +05:30
|
|
|
info->pw_shell = xstrdup (temp_pw_shell);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv ("SHELL", info->pw_shell);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:34 +05:30
|
|
|
/*
|
|
|
|
* Export the user name. For BSD derived systems, it's "USER", for
|
|
|
|
* all others it's "LOGNAME". We set both of them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addenv ("USER", info->pw_name);
|
|
|
|
addenv ("LOGNAME", info->pw_name);
|
|
|
|
|
2007-10-07 17:17:01 +05:30
|
|
|
/*
|
|
|
|
* Create the PATH environmental variable and export it.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
cp = getdef_str ((info->pw_uid == 0) ? "ENV_SUPATH" : "ENV_PATH");
|
2007-10-07 17:16:34 +05:30
|
|
|
|
2009-04-23 23:15:42 +05:30
|
|
|
if (NULL == cp) {
|
2007-10-07 17:14:14 +05:30
|
|
|
/* not specified, use a minimal default */
|
2010-01-24 22:35:37 +05:30
|
|
|
addenv ((info->pw_uid == 0) ? "PATH=/sbin:/bin:/usr/sbin:/usr/bin" : "PATH=/bin:/usr/bin", NULL);
|
2007-10-07 17:15:23 +05:30
|
|
|
} else if (strchr (cp, '=')) {
|
2007-10-07 17:14:14 +05:30
|
|
|
/* specified as name=value (PATH=...) */
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv (cp, NULL);
|
2007-10-07 17:14:14 +05:30
|
|
|
} else {
|
|
|
|
/* only value specified without "PATH=" */
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv ("PATH", cp);
|
2007-10-07 17:14:14 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-27 04:45:43 +05:30
|
|
|
#ifndef USE_PAM
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Create the MAIL environmental variable and export it. login.defs
|
|
|
|
* knows the prefix.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:17:33 +05:30
|
|
|
if (getdef_bool ("MAIL_CHECK_ENAB")) {
|
2009-04-23 23:15:42 +05:30
|
|
|
cp = getdef_str ("MAIL_DIR");
|
|
|
|
if (NULL != cp) {
|
2007-10-07 17:17:33 +05:30
|
|
|
addenv_path ("MAIL", cp, info->pw_name);
|
2009-04-23 23:15:42 +05:30
|
|
|
} else {
|
|
|
|
cp = getdef_str ("MAIL_FILE");
|
|
|
|
if (NULL != cp) {
|
|
|
|
addenv_path ("MAIL", info->pw_dir, cp);
|
|
|
|
} else {
|
2007-10-07 17:14:02 +05:30
|
|
|
#if defined(MAIL_SPOOL_FILE)
|
2009-04-23 23:15:42 +05:30
|
|
|
addenv_path ("MAIL", info->pw_dir, MAIL_SPOOL_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
#elif defined(MAIL_SPOOL_DIR)
|
2009-04-23 23:15:42 +05:30
|
|
|
addenv_path ("MAIL", MAIL_SPOOL_DIR, info->pw_name);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2009-04-23 23:15:42 +05:30
|
|
|
}
|
2007-10-07 17:17:33 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read environment from optional config file. --marekm
|
|
|
|
*/
|
2009-04-23 23:15:42 +05:30
|
|
|
envf = getdef_str ("ENVIRON_FILE");
|
|
|
|
if (NULL != envf) {
|
2007-10-07 17:15:23 +05:30
|
|
|
read_env_file (envf);
|
2009-04-23 23:15:42 +05:30
|
|
|
}
|
2007-10-07 17:16:34 +05:30
|
|
|
#endif /* !USE_PAM */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2009-04-23 23:15:42 +05:30
|
|
|
|