Ensure that $HOME and $USER are always set, #139

This commit is contained in:
Roy Marples 2009-01-11 17:09:13 +00:00
parent 26f70a5277
commit baf0ce64f5

View File

@ -645,8 +645,6 @@ int start_stop_daemon(int argc, char **argv)
RC_STRINGLIST *env_list; RC_STRINGLIST *env_list;
RC_STRING *env; RC_STRING *env;
char *tmp, *newpath, *np; char *tmp, *newpath, *np;
bool sethome = false;
bool setuser = false;
char *p; char *p;
char *token; char *token;
char exec_file[PATH_MAX]; char exec_file[PATH_MAX];
@ -674,13 +672,20 @@ int start_stop_daemon(int argc, char **argv)
eerror("%s: invalid nice level `%s' (SSD_NICELEVEL)", eerror("%s: invalid nice level `%s' (SSD_NICELEVEL)",
applet, tmp); applet, tmp);
/* Get our initial dir */ /* Get our user name and initial dir */
p = getenv("USER");
home = getenv("HOME"); home = getenv("HOME");
if (!home) { if (home == NULL || p == NULL) {
pw = getpwuid(getuid()); pw = getpwuid(getuid());
if (pw) if (pw != NULL) {
if (p == NULL)
setenv("USER", pw->pw_name, 1);
if (home == NULL) {
setenv("HOME", home, 1);
home = pw->pw_dir; home = pw->pw_dir;
} }
}
}
while ((opt = getopt_long(argc, argv, getoptstring, longopts, while ((opt = getopt_long(argc, argv, getoptstring, longopts,
(int *) 0)) != -1) (int *) 0)) != -1)
@ -715,14 +720,20 @@ int start_stop_daemon(int argc, char **argv)
if (sscanf(tmp, "%d", &tid) != 1) if (sscanf(tmp, "%d", &tid) != 1)
pw = getpwnam(tmp); pw = getpwnam(tmp);
else else
pw = getpwuid((uid_t) tid); pw = getpwuid((uid_t)tid);
if (!pw) if (pw == NULL)
eerrorx("%s: user `%s' not found", eerrorx("%s: user `%s' not found",
applet, tmp); applet, tmp);
uid = pw->pw_uid; uid = pw->pw_uid;
home = pw->pw_dir; home = pw->pw_dir;
if (!gid) unsetenv("HOME");
if (pw->pw_dir)
setenv("HOME", pw->pw_dir, 1);
unsetenv("USER");
if (pw->pw_name)
setenv("USER", pw->pw_name, 1);
if (gid == 0)
gid = pw->pw_gid; gid = pw->pw_gid;
if (p) { if (p) {
@ -732,7 +743,7 @@ int start_stop_daemon(int argc, char **argv)
else else
gr = getgrgid((gid_t) tid); gr = getgrgid((gid_t) tid);
if (!gr) if (gr == NULL)
eerrorx("%s: group `%s'" eerrorx("%s: group `%s'"
" not found", " not found",
applet, tmp); applet, tmp);
@ -746,27 +757,18 @@ int start_stop_daemon(int argc, char **argv)
break; break;
case 'e': /* --env */ case 'e': /* --env */
if (putenv(optarg) == 0) { putenv(optarg);
if (strncmp("HOME=", optarg, 5) == 0) {
sethome = true;
home = strchr(optarg, '=') + 1;
} else if (strncmp("USER=", optarg, 5) == 0)
setuser = true;
}
break; break;
case 'g': /* --group <group>|<gid> */ case 'g': /* --group <group>|<gid> */
{
if (sscanf(optarg, "%d", &tid) != 1) if (sscanf(optarg, "%d", &tid) != 1)
gr = getgrnam(optarg); gr = getgrnam(optarg);
else else
gr = getgrgid((gid_t) tid); gr = getgrgid((gid_t)tid);
if (gr == NULL)
if (!gr)
eerrorx("%s: group `%s' not found", eerrorx("%s: group `%s' not found",
applet, optarg); applet, optarg);
gid = gr->gr_gid; gid = gr->gr_gid;
}
break; break;
case 'k': case 'k':
@ -1089,23 +1091,8 @@ int start_stop_daemon(int argc, char **argv)
eerrorx("%s: unable to set groupid to %d", applet, gid); eerrorx("%s: unable to set groupid to %d", applet, gid);
if (changeuser && initgroups(changeuser, gid)) if (changeuser && initgroups(changeuser, gid))
eerrorx("%s: initgroups (%s, %d)", applet, changeuser, gid); eerrorx("%s: initgroups (%s, %d)", applet, changeuser, gid);
if (uid) { if (uid && setuid(uid))
if (setuid(uid))
eerrorx ("%s: unable to set userid to %d", applet, uid); eerrorx ("%s: unable to set userid to %d", applet, uid);
pw = getpwuid(uid);
if (pw) {
if (!sethome) {
unsetenv("HOME");
if (pw->pw_dir)
setenv("HOME", pw->pw_dir, 1);
}
if (!setuser) {
unsetenv("USER");
if (pw->pw_name)
setenv("USER", pw->pw_name, 1);
}
}
}
/* Close any fd's to the passwd database */ /* Close any fd's to the passwd database */
endpwent(); endpwent();