- if (flags & f_login) {
- env = mangled_env;
- env[1] = bc__makeEnv("USER", to_pw->pw_name);
- env[2] = bc__makeEnv("LOGNAME", to_pw->pw_name);
- env[3] = bc__makeEnv("HOME", to_pw->pw_dir);
+ if (flags & f_login) {
+
+ /* --- This is a login request --- *
+ *
+ * Erase the existing environment and build a new one.
+ */
+
+ if (!pass)
+ i += 4;
+ else {
+ env[i++] = "PATH=/usr/bin:/bin";
+ env[i++] = bc__makeEnv("USER", to_pw->pw_name);
+ env[i++] = bc__makeEnv("LOGNAME", to_pw->pw_name);
+ env[i++] = bc__makeEnv("HOME", to_pw->pw_dir);
+ }
+ } else {
+
+ /* --- Normal request --- *
+ *
+ * Remove dangerous variables from the list.
+ */
+
+ for (j = 0; environ[j]; j++) {
+ for (b = 0; banned[b]; b++) {
+ if (memcmp(environ[j], banned[b], strlen(banned[b])) == 0)
+ goto skip_var;
+ }
+ if (pass)
+ env[i] = environ[j];
+ i++;
+ skip_var:;
+ }
+ }
+
+ /* --- Now add our own variables --- *
+ *
+ * The following are supplied only to help people construct startup
+ * scripts. Anyone who relies on them being accurate for
+ * authentication purposes will get exactly what they deserve.
+ */
+
+ if (!pass)
+ i += 4;
+ else {
+ env[i++] = bc__makeEnv("BECOME_OLDUSER", from_pw->pw_name);
+ env[i++] = bc__makeEnv("BECOME_OLDHOME", from_pw->pw_dir);
+ env[i++] = bc__makeEnv("BECOME_USER", to_pw->pw_name);
+ env[i++] = bc__makeEnv("BECOME_HOME", to_pw->pw_dir);
+ }
+
+ /* --- Allocate memory after the first pass is complete --- */
+
+ if (pass)
+ env[i] = 0;
+ i++;
+
+ if (!pass)
+ env = xmalloc(i * sizeof(env[0]));
+ }