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
|
|
|
|
* Copyright (c) 2007 , 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
|
|
|
|
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"
|
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;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
buf = xmalloc (strlen (dirname) + strlen (filename) + 2);
|
|
|
|
sprintf (buf, "%s/%s", dirname, filename);
|
|
|
|
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");
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!fp)
|
|
|
|
return;
|
2007-10-07 17:15:23 +05:30
|
|
|
while (fgets (buf, sizeof buf, fp) == buf) {
|
|
|
|
cp = strrchr (buf, '\n');
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!cp)
|
|
|
|
break;
|
|
|
|
*cp = '\0';
|
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
/* ignore whitespace and comments */
|
2007-10-07 17:15:23 +05:30
|
|
|
while (*cp && isspace (*cp))
|
2007-10-07 17:14:02 +05:30
|
|
|
cp++;
|
|
|
|
if (*cp == '\0' || *cp == '#')
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* ignore lines which don't follow the name=value format
|
|
|
|
* (for example, the "export NAME" shell commands)
|
|
|
|
*/
|
|
|
|
name = cp;
|
2007-10-07 17:15:23 +05:30
|
|
|
while (*cp && !isspace (*cp) && *cp != '=')
|
2007-10-07 17:14:02 +05:30
|
|
|
cp++;
|
|
|
|
if (*cp != '=')
|
|
|
|
continue;
|
|
|
|
/* NUL-terminate the name */
|
|
|
|
*cp++ = '\0';
|
|
|
|
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
|
|
|
}
|
|
|
|
eise 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
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
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
|
|
|
|
char *envf;
|
2007-10-07 17:17:33 +05:30
|
|
|
#endif
|
2007-12-27 04:45:43 +05:30
|
|
|
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) {
|
2007-10-07 17:17:33 +05:30
|
|
|
fprintf (stderr, _("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 ();
|
2007-10-07 17:14:02 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
puts (_("No directory, logging in with HOME=/"));
|
2007-10-07 17:14:02 +05:30
|
|
|
info->pw_dir = temp_pw_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (info->pw_shell == (char *) 0 || !*info->pw_shell) {
|
2007-10-07 17:14:02 +05:30
|
|
|
static char temp_pw_shell[] = "/bin/sh";
|
2007-10-07 17:15:23 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
info->pw_shell = temp_pw_shell;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2007-10-07 17:14:14 +05:30
|
|
|
if (!cp) {
|
|
|
|
/* not specified, use a minimal default */
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv ("PATH=/bin:/usr/bin", NULL);
|
|
|
|
} 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")) {
|
|
|
|
if ((cp = getdef_str ("MAIL_DIR")))
|
|
|
|
addenv_path ("MAIL", cp, info->pw_name);
|
|
|
|
else if ((cp = getdef_str ("MAIL_FILE")))
|
|
|
|
addenv_path ("MAIL", info->pw_dir, cp);
|
|
|
|
else {
|
2007-10-07 17:14:02 +05:30
|
|
|
#if defined(MAIL_SPOOL_FILE)
|
2007-10-07 17:17:33 +05:30
|
|
|
addenv_path ("MAIL", info->pw_dir, MAIL_SPOOL_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
#elif defined(MAIL_SPOOL_DIR)
|
2007-10-07 17:17:33 +05:30
|
|
|
addenv_path ("MAIL", MAIL_SPOOL_DIR, info->pw_name);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:17:33 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read environment from optional config file. --marekm
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
if ((envf = getdef_str ("ENVIRON_FILE")))
|
|
|
|
read_env_file (envf);
|
2007-10-07 17:16:34 +05:30
|
|
|
#endif /* !USE_PAM */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|