2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 2000, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2001 - 2006, Tomasz Kłoczko
|
2010-08-22 18:34:54 +05:30
|
|
|
* Copyright (c) 2007 - 2010, Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
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>
|
|
|
|
#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;
|
|
|
|
|
|
|
|
buf = xmalloc (len);
|
|
|
|
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
|
|
|
|