-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscgi_run.c
229 lines (180 loc) · 6.42 KB
/
scgi_run.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright (c) 2011, Barry Pederson <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Execute a CGI script over a SCGI connection
Compile and strip with:
cc -o scgi_run scgi_run.c
strip scgi_run
For optional debugging, define a DEBUG_FILENAME macro as in:
cc -o scgi_run -DDEBUG_FILENAME=/tmp/foo.log scgi_run.c
2011-03-30 Barry Pederson <[email protected]>
*/
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
extern char **environ;
extern int errno;
#define MAX_HEADER_LENGTH 262144 // just for a sanity check on what the server sends
/**
Debugging code, requires stdarg.h, stdio.h, and time.h
**/
#ifdef DEBUG_FILENAME
#define QUOTE(name) #name
#define STR(macro) QUOTE(macro)
static void debug_log(const char *msg, ...) {
char timestamp[32]; // really only need 21 bytes here
time_t now;
va_list ap;
FILE *f;
now = time(NULL);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S ", localtime(&now));
f = fopen(STR(DEBUG_FILENAME), "a");
fputs(timestamp, f);
va_start(ap, msg);
vfprintf(f, msg, ap);
va_end(ap);
fputc('\n', f);
fclose(f);
}
#else
#define debug_log
#endif
void return_error(char *status_msg, char *msg, ...) {
va_list ap;
char *formatted_msg;
va_start(ap, msg);
vasprintf(&formatted_msg, msg, ap);
va_end(ap);
fprintf(stdout, "Status: %s\r\nContent-Type: text/plain\r\n\r\n%s\r\n",
status_msg, formatted_msg);
exit(1);
}
int read_char() {
int ch;
if (read(0, &ch, 1) != 1)
return_error("500 Internal Error", "SCGI stream truncated");
return ch;
}
char * read_scgi_environment() {
char *headers;
char *name;
char *value;
int length;
char ch;
ch = read_char();
if (isdigit(ch)) {
length = ch - '0';
} else {
return_error("500 Internal Error", "SCGI stream didn't start with a digit, started with char 0x%x", ch);
}
while (1) {
ch = read_char();
if (isdigit(ch)) {
length = (length * 10) + (ch - '0');
if (length < 0 || length > MAX_HEADER_LENGTH) {
return_error("500 Internal Error", "SCGI Header length is not in the range 0..%d", MAX_HEADER_LENGTH);
}
} else {
if (ch == ':') {
break;
} else {
return_error("500 Invalid SCGI header", "Invalid character 0x%x in length", ch);
}
}
}
debug_log("SCGI header length = %d", length);
headers = (char *) malloc(length+1); // +1 is for the comma after the headers
if (!headers) {
return_error("500 Internal Error", "Can't allocate memory to read SCGI header");
}
if (read(0, headers, length+1) != length+1) {
return_error("500 Internal Error", "SCGI Header truncated");
}
if (headers[length] != ',') {
return_error("500 Internal Error", "SCGI Header: Incomplete netstring, missing comma");
}
if (length) {
name = headers;
while (name < (headers + length)) {
value = name + strlen(name) + 1;
if (value >= (headers + length))
return_error("500 Internal Error", "SCGI Header: Corrupt name/value table");
setenv(name, value, 1);
debug_log("Set [%s]=[%s]", name, value);
name = value + strlen(value) + 1;
}
}
}
int main(int argc, char **argv) {
int i;
char *script_filename;
char *check_directory = NULL;
char *new_argv[2];
char **exec_args = new_argv;
debug_log("-------- Starting, argc=%d", argc);
for (i = 0; i < argc; i++) {
debug_log("argv[%d] == [%s]", i, argv[i]);
}
read_scgi_environment();
/* Make things look like a CGI environment */
unsetenv("SCGI");
setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
script_filename = getenv("SCRIPT_FILENAME");
new_argv[0] = script_filename;
new_argv[1] = NULL;
if (argc > 1) {
if (argv[1][strlen(argv[1]) - 1] == '/') {
check_directory = argv[1];
} else {
/* specific script and maybe args in inetd config */
script_filename = argv[1];
exec_args = &argv[1];
}
}
if (!script_filename) {
return_error("500 Internal Error", "CGI environment missing SCRIPT_FILENAME");
}
if (strstr(script_filename, "../")) {
return_error("500 Internal Error", "SCRIPT_FILENAME should not include \"../\"");
}
if (check_directory) {
if (strncmp(check_directory, script_filename, strlen(check_directory))) {
return_error("500 Internal Error",
"[%s] doesn't reside under [%s]",
script_filename, check_directory);
}
}
execve(script_filename, exec_args, environ);
/* If execve returned, something went wrong */
if (errno == ENOENT) {
return_error("404 Not Found", "Can't locate CGI script\n");
}
return_error("500 Internal Error",
"Unable to execute CGI script, please contact the system administrator\n%s\n", strerror(errno));
}