Skip to content

Commit

Permalink
Better diagnostics for 403 errors
Browse files Browse the repository at this point in the history
Passed via FastCGI stderr, so should end up in webserver's error log
  • Loading branch information
gnosek committed Feb 28, 2009
1 parent 7282f33 commit cdd6b84
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions fcgiwrap.c
Expand Up @@ -433,6 +433,18 @@ static void inherit_environment()
}
}

static void error_403(const char *reason, const char *filename)
{
FCGI_fputs("Status: 403 Forbidden\nContent-type: text/plain\n\n403", FCGI_stdout);
if (filename) {
FCGI_fprintf(FCGI_stderr, "%s (%s)\n", reason, filename);
} else {
FCGI_fputs(reason, FCGI_stderr);
FCGI_fputc('\n', FCGI_stderr);
}
exit(99);
}

static void handle_fcgi_request()
{
int pipe_in[2];
Expand All @@ -455,22 +467,17 @@ static void handle_fcgi_request()
case 0: /* child */
filename = get_cgi_filename();
inherit_environment();
if (!filename) {
puts("Status: 403 Forbidden\nContent-type: text/plain\n\n403");
exit(99);
}
if (!filename)
error_403("Cannot get script name, is DOCUMENT_ROOT and SCRIPT_NAME set and is the script executable?", NULL);

last_slash = strrchr(filename, '/');
if (!last_slash) {
puts("Status: 403 Forbidden\nContent-type: text/plain\n\n403");
exit(99);
}
if (!last_slash)
error_403("Script name must be a fully qualified path", filename);

*last_slash = 0;
if (chdir(filename) < 0) {
puts("Status: 403 Forbidden\nContent-type: text/plain\n\n403");
exit(99);
}
if (chdir(filename) < 0)
error_403("Cannot chdir to script directory", filename);

*last_slash = '/';

close(pipe_in[1]);
Expand Down

0 comments on commit cdd6b84

Please sign in to comment.