Index: usr.sbin/cron/cron/do_command.c =================================================================== --- usr.sbin/cron/cron/do_command.c +++ usr.sbin/cron/cron/do_command.c @@ -101,6 +101,7 @@ register FILE *mail; register int bytes = 1; int status = 0; + const char *homedir = NULL; # if defined(LOGIN_CAP) struct passwd *pwd; login_cap_t *lc; @@ -297,6 +298,14 @@ pwd = getpwuid(e->uid); lc = NULL; if (pwd != NULL) { + if (pwd->pw_dir != NULL + && pwd->pw_dir[0] != '\0') { + homedir = strdup(pwd->pw_dir); + if (homedir == NULL) { + warn("strdup"); + _exit(ERROR_EXIT); + } + } pwd->pw_gid = e->gid; if (e->class != NULL) lc = login_getclass(e->class); @@ -340,17 +349,36 @@ if (lc != NULL) login_close(lc); #endif - chdir(env_get("HOME", e->envp)); - /* exec the command. + /* For compatibility, we chdir to the value of HOME if it was + * specified explicitly in the crontab file, but not if it was + * set in the environment by some other mechanism. We chdir to + * the homedir given by the pw entry otherwise. + * + * XXX: probably should also consult PAM. + */ + { + char *new_home = env_get("HOME", e->envp); + if (new_home != NULL && new_home[0] != '\0') + chdir(new_home); + else if (homedir != NULL) + chdir(homedir); + else + chdir("/"); + } + + /* exec the command. Note that SHELL is not respected from + * either login.conf or pw_shell, only an explicit setting + * in the crontab. (default of _PATH_BSHELL is supplied when + * setting up the entry) */ { char *shell = env_get("SHELL", e->envp); char **p; - /* Apply the environment from the entry, overriding existing - * values (this will always set PATH, LOGNAME, etc.) putenv - * should not fail unless malloc does. + /* Apply the environment from the entry, overriding + * existing values (this will always set LOGNAME and + * SHELL). putenv should not fail unless malloc does. */ for (p = e->envp; *p; ++p) { if (putenv(*p) != 0) { @@ -359,6 +387,25 @@ } } + /* HOME in login.conf overrides pw, and HOME in the + * crontab overrides both. So set pw's value only if + * nothing was already set (overwrite==0). + */ + if (homedir != NULL + && setenv("HOME", homedir, 0) < 0) { + warn("setenv(HOME)"); + _exit(ERROR_EXIT); + } + + /* PATH in login.conf is respected, but the crontab + * overrides; set a default value only if nothing + * already set. + */ + if (setenv("PATH", _PATH_DEFPATH, 0) < 0) { + warn("setenv(PATH)"); + _exit(ERROR_EXIT); + } + # if DEBUGGING if (DebugFlags & DTEST) { fprintf(stderr, Index: usr.sbin/cron/lib/entry.c =================================================================== --- usr.sbin/cron/lib/entry.c +++ usr.sbin/cron/lib/entry.c @@ -369,7 +369,8 @@ e->gid = pw->pw_gid; /* copy and fix up environment. some variables are just defaults and - * others are overrides. + * others are overrides; we process only the overrides here, defaults + * are handled in do_command after login.conf is processed. */ e->envp = env_copy(envp); if (e->envp == NULL) { @@ -388,28 +389,6 @@ goto eof; } } - if (!env_get("HOME", e->envp)) { - prev_env = e->envp; - sprintf(envstr, "HOME=%s", pw->pw_dir); - e->envp = env_set(e->envp, envstr); - if (e->envp == NULL) { - warn("env_set(%s)", envstr); - env_free(prev_env); - ecode = e_mem; - goto eof; - } - } - if (!env_get("PATH", e->envp)) { - prev_env = e->envp; - sprintf(envstr, "PATH=%s", _PATH_DEFPATH); - e->envp = env_set(e->envp, envstr); - if (e->envp == NULL) { - warn("env_set(%s)", envstr); - env_free(prev_env); - ecode = e_mem; - goto eof; - } - } prev_env = e->envp; sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name); e->envp = env_set(e->envp, envstr);