1 /* Author: Wietse Venema <wietse@wzv.win.tue.nl> */
5 RCSID("$Id: sysv_environ.c,v 1.23 1997/12/14 23:50:44 assar Exp $");
15 #include "sysv_default.h"
22 read_etc_environment (void)
27 f = fopen(_PATH_ETC_ENVIRONMENT, "r");
31 while (fgets (buf, sizeof(buf), f) != NULL) {
32 if (buf[0] == '\n' || buf[0] == '#')
34 buf[strlen(buf) - 1] = '\0';
35 val = strchr (buf, '=');
39 setenv(buf, val + 1, 1);
46 * Environment variables that are preserved (but may still be overruled by
47 * other means). Only TERM and TZ appear to survive (SunOS 5.1). These are
48 * typically inherited from the ttymon process.
51 static struct preserved {
61 * Environment variables that are not preserved and that cannot be specified
62 * via commandline or stdin. Except for the LD_xxx (runtime linker) stuff,
63 * the list applies to most SYSV systems. The manpage mentions only that
64 * SHELL and PATH are censored. HOME, LOGNAME and MAIL are always
65 * overwritten; they are in the list to make the censoring explicit.
68 static struct censored {
72 {"SHELL=", sizeof("SHELL=") - 1},
73 {"HOME=", sizeof("HOME=") - 1},
74 {"LOGNAME=", sizeof("LOGNAME=") - 1},
75 {"MAIL=", sizeof("MAIL=") - 1},
76 {"CDPATH=", sizeof("CDPATH=") - 1},
77 {"IFS=", sizeof("IFS=") - 1},
78 {"PATH=", sizeof("PATH=") - 1},
79 {"LD_", sizeof("LD_") - 1},
83 /* sysv_newenv - set up final environment after logging in */
85 void sysv_newenv(int argc, char **argv, struct passwd *pwd,
86 char *term, int pflag)
94 /* Preserve a selection of the environment. */
96 for (pp = preserved; pp->name; pp++)
97 pp->value = getenv(pp->name);
100 * Note: it is a bad idea to assign a static array to the global environ
101 * variable. Reason is that putenv() can run into problems when it tries
102 * to realloc() the environment table. Instead, we just clear environ[0]
103 * and let putenv() work things out.
106 if (!pflag && environ)
109 /* Restore preserved environment variables. */
111 for (pp = preserved; pp->name; pp++)
113 setenv(pp->name, pp->value, 1);
115 /* The TERM definition from e.g. rlogind can override an existing one. */
118 setenv("TERM", term, 1);
121 * Environment definitions from the command line overrule existing ones,
122 * but can be overruled by definitions from stdin. Some variables are
125 * Omission: we do not support environment definitions from stdin.
128 #define STREQN(x,y,l) (x[0] == y[0] && strncmp(x,y,l) == 0)
130 while (argc && *argv) {
131 if (strchr(*argv, '=') == 0) {
132 snprintf(buf, sizeof(buf), "L%d", count++);
133 setenv(buf, *argv, 1);
135 for (cp = censored; cp->prefix; cp++)
136 if (STREQN(*argv, cp->prefix, cp->length))
144 /* PATH is always reset. */
146 setenv("PATH", pwd->pw_uid ? default_path : default_supath, 1);
148 /* Undocumented: HOME, MAIL and LOGNAME are always reset (SunOS 5.1). */
150 setenv("HOME", pwd->pw_dir, 1);
153 if(KRB4_MAILDIR[strlen(KRB4_MAILDIR) - 1] == '/')
155 roken_concat(buf, sizeof(buf), KRB4_MAILDIR, sep, pwd->pw_name, NULL);
157 setenv("MAIL", buf, 1);
158 setenv("LOGNAME", pwd->pw_name, 1);
159 setenv("USER", pwd->pw_name, 1);
162 * Variables that may be set according to specifications in the defaults
163 * file. HZ and TZ are set only if they are still uninitialized.
165 * Extension: when ALTSHELL=YES, we set the SHELL variable even if it is
169 if (strcasecmp(default_altsh, "YES") == 0)
170 setenv("SHELL", pwd->pw_shell, 1);
172 setenv("HZ", default_hz, 0);
173 if (default_timezone)
174 setenv("TZ", default_timezone, 0);
176 /* Non-environment stuff. */
179 if (sscanf(default_umask, "%o", &umask_val) == 1 && umask_val)
183 if (default_ulimit) {
186 if (sscanf(default_ulimit, "%ld", &limit_val) == 1 && limit_val)
187 if (ulimit(UL_SETFSIZE, limit_val) < 0)
188 warn ("ulimit(UL_SETFSIZE, %ld)", limit_val);
191 read_etc_environment();