--- suexec.c 2008-11-30 15:47:31.000000000 +0000 +++ suexec-phpfcgi.c 2006-04-07 17:05:04.000000000 +0000 @@ -259,6 +259,7 @@ char *cmd; /* command to be executed */ char cwd[AP_MAXPATH]; /* current working directory */ char dwd[AP_MAXPATH]; /* docroot working directory */ + char nwd[AP_MAXPATH]; /* after-chroot working dir */ struct passwd *pw; /* password entry holder */ struct group *gr; /* group entry holder */ struct stat dir_info; /* directory info holder */ @@ -456,7 +455,6 @@ log_err("cannot run as forbidden uid (%d/%s)\n", uid, cmd); exit(107); } - /* * Error out if attempt is made to execute as root group * or as a GID less than AP_GID_MIN. Tsk tsk. @@ -465,6 +463,39 @@ log_err("cannot run as forbidden gid (%d/%s)\n", gid, cmd); exit(108); } + + + int striplen = strlen (target_homedir); + + char* tlen = strchr(target_homedir, '/'); + char* hlen = strchr(tlen+1, '/'); + char* ulen = strchr(hlen+1, '/'); + char* chroot_dir = strndup(target_homedir, ulen-target_homedir); + char* pt = getenv("PATH_TRANSLATED"); + if (pt != 0) { + setenv("PATH_TRANSLATED", pt + (ulen - target_homedir), 1); + } + + setenv("DOCUMENT_ROOT", "/", 1); + + if (getcwd(nwd, AP_MAXPATH) == NULL) { + log_err("cannot get current working directory (prechroot)\n"); + exit(111); + } + + char* trunc_nwd = nwd+(ulen-target_homedir); + + if (chdir(chroot_dir)) { + log_err("crit: can't chdir to chroot dir (%s)",chroot_dir); + exit(121); + } + + if (chroot(chroot_dir) != 0) { + log_err("emerg: failed to chroot (%s, %s)\n", chroot_dir, cmd); + exit(122); + } + + chdir (trunc_nwd); /* * Change UID/GID here so that the following tests work over NFS. @@ -498,22 +529,11 @@ exit(111); } - if (userdir) { - if (((chdir(target_homedir)) != 0) || - ((chdir(AP_USERDIR_SUFFIX)) != 0) || - ((getcwd(dwd, AP_MAXPATH)) == NULL) || - ((chdir(cwd)) != 0)) { - log_err("cannot get docroot information (%s)\n", target_homedir); - exit(112); - } - } - else { - if (((chdir(AP_DOC_ROOT)) != 0) || - ((getcwd(dwd, AP_MAXPATH)) == NULL) || - ((chdir(cwd)) != 0)) { - log_err("cannot get docroot information (%s)\n", AP_DOC_ROOT); - exit(113); - } + if (((chdir(getenv("DOCUMENT_ROOT"))) != 0) || + ((getcwd(dwd, AP_MAXPATH)) == NULL) || + ((chdir(cwd)) != 0)) { + log_err("cannot get docroot information (%s)\n", AP_DOC_ROOT); + exit(113); } if ((strncmp(cwd, dwd, strlen(dwd))) != 0) { @@ -565,7 +585,7 @@ * Error out if the target name/group is different from * the name/group of the cwd or the program. */ - if ((uid != dir_info.st_uid) || +/* if ((uid != dir_info.st_uid) || (gid != dir_info.st_gid) || (uid != prg_info.st_uid) || (gid != prg_info.st_gid)) { @@ -575,7 +595,7 @@ dir_info.st_uid, dir_info.st_gid, prg_info.st_uid, prg_info.st_gid); exit(120); - } + }*/ /* * Error out if the program is not executable for the user. * Otherwise, she won't find any error in the logs except for