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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
$NetBSD: patch-ae,v 1.2 2002/06/26 10:53:34 lukem Exp $
--- bozohttpd.c.orig Sat Sep 22 20:10:00 2001
+++ bozohttpd.c
@@ -189,6 +189,9 @@
char *content_length;
char *allow;
char *host; /* HTTP/1.1 Host: */
+ char *remotehost;
+ char *remoteaddr;
+ char *serverport;
SIMPLEQ_HEAD(, _headers) headers;
int nheaders;
} http_req;
@@ -205,6 +208,10 @@
static char *cgibin; /* cgi-bin directory */
static void process_cgi __P((http_req *));
+
+#define CGIBIN_PREFIX "cgi-bin/"
+#define CGIBIN_PREFIX_LEN (sizeof(CGIBIN_PREFIX)-1)
+
#endif /* NO_CGIBIN_SUPPORT */
@@ -359,6 +366,7 @@
case 'c':
#ifndef NO_CGIBIN_SUPPORT
cgibin = optarg;
+ debug((DEBUG_OBESE, "cgibin (cgi-bin directory) is %s", cgibin));
break;
#else
error(1, "CGI is not enabled");
@@ -460,7 +468,7 @@
*/
if (bflag) {
struct addrinfo h, *r;
- int s, e;
+ int e;
daemon(1, 0);
@@ -490,7 +498,7 @@
* read and process the HTTP request.
*/
do {
- if (http_request = read_request()) {
+ if ((http_request = read_request()) != NULL) {
process_request(http_request);
return (0);
}
@@ -558,14 +566,20 @@
{
struct sigaction sa;
char *str, *val, *method, *url, *proto;
+ char *host, *addr, *port;
+ char hbuf[NI_MAXHOST], abuf[NI_MAXSERV], bufport[10];
ssize_t len;
+#ifndef SOCKOLD
+ struct sockaddr_storage ss;
+#else
+ struct sockaddr ss;
+#endif
int line = 0;
+ socklen_t slen;
http_req *request;
#ifndef NO_DAEMON_MODE
if (bflag) {
- struct sockaddr_storage ss;
- socklen_t slen;
int fd;
/*
@@ -603,12 +617,67 @@
#endif /* NO_DAEMON_MODE */
- request = malloc(sizeof *request);
+ request = calloc(1, sizeof *request);
if (request == NULL)
error(1, "malloc of request descriptor");
request->allow = request->host = request->content_type =
request->content_length = NULL;
+ slen = sizeof(ss);
+ if (getpeername(0, (struct sockaddr *)&ss, &slen) < 0)
+ host = addr = NULL;
+ else {
+#ifndef SOCKOLD
+ addr = host = NULL;
+ if (getnameinfo((struct sockaddr *)&ss, slen,
+ abuf, sizeof abuf, NULL, 0, NI_NUMERICHOST) == 0)
+ addr = abuf;
+ if (nflag == 0 && getnameinfo((struct sockaddr *)&ss, slen,
+ hbuf, sizeof hbuf, NULL, 0, 0) == 0)
+ host = hbuf;
+#else
+ struct hostent *hp;
+ struct sockaddr_in *sin;
+
+ switch (((struct sockaddr *)&ss)->sa_family) {
+ case AF_INET:
+ sin = (struct sockaddr_in *)&ss;
+ addr = inet_ntoa(sin->sin_addr);
+ if (nflag != 0 ||
+ (hp = gethostbyaddr((char *)&sin->sin_addr,
+ sizeof(sin->sin_addr), AF_INET)) == NULL)
+ host = NULL;
+ else
+ host = hp->h_name;
+ break;
+ default:
+ host = addr = NULL;
+ break;
+ }
+#endif
+ }
+ if (host != NULL && (request->remotehost = strdup(host)) == NULL)
+ http_error(500, request, "malloc failed");
+ if (addr != NULL && (request->remoteaddr = strdup(addr)) == NULL)
+ http_error(500, request, "malloc failed");
+ slen = sizeof(ss);
+ if (getsockname(0, (struct sockaddr *)&ss, &slen) < 0)
+ port = NULL;
+ else {
+#ifndef SOCKOLD
+ if (getnameinfo((struct sockaddr *)&ss, slen, NULL, 0,
+ bufport, sizeof bufport, NI_NUMERICSERV) == 0)
+ port = bufport;
+ else
+ port = NULL;
+#else
+ (void)snprintf(port = bufport, sizeof bufport, "%d",
+ ntohs(((struct sockaddr_in *)&ss)->sin_port));
+#endif
+ }
+ if (port != NULL && (request->serverport = strdup(port)) == NULL)
+ http_error(500, request, "malloc failed");
+
/*
* setup a timer to make sure the request is not hung
*/
@@ -628,7 +697,10 @@
if (line == 1) {
if (len < 1)
http_error(404, NULL, "null method");
- warning("got request ``%s''", str);
+ warning("got request ``%s'' from host %s to port %s",
+ str,
+ host ? host : addr ? addr : "<unknown>",
+ port ? port : "<unknown>");
debug((DEBUG_FAT, "read_req, getting request: ``%s''",
str));
@@ -980,7 +1052,7 @@
* - ``expand'' %20 crapola
* - punt if it doesn't start with /
* - look for "http://myname/" and deal with it.
- * - check if /cgi-bin/ and call process_cgi() if so
+ * - check if "/" + CGIBIN_PREFIX and call process_cgi() if so
* - check for ~user and call transform_user() if so
* - if the length > 1, check for trailing slash. if so,
* add the index.html file
@@ -1006,7 +1078,7 @@
if (url[0] != '/') {
http_error(404, request, "unknown URL");
#ifndef NO_CGIBIN_SUPPORT
- } else if (strncmp(url + 1, "cgi-bin/", 8) == 0) {
+ } else if (strncmp(url + 1, CGIBIN_PREFIX, CGIBIN_PREFIX_LEN) == 0) {
debug((DEBUG_FAT, "calling process_cgi"));
process_cgi(request);
#endif /* NO_CGIBIN_SUPPORT */
@@ -1234,17 +1306,16 @@
http_req *request;
{
struct _headers *headp;
-#ifndef SOCKOLD
- struct sockaddr_storage ss;
-#else
- struct sockaddr ss;
-#endif
- char *host, *addr, *port, *info, *type, *clen, *query, *s, *t,
- *path, *env, *command = NULL,
- *url = strdup(request->url + strlen("/cgi-bin"));
+ char *info, *type, *clen, *query, *s, *t,
+ *path, *env, *command = NULL, *fullcommand,
+ *url = strdup(request->url + CGIBIN_PREFIX_LEN);
char **envp, *argv[3];
- char bufport[10];
- int envpsize, ix, slen = sizeof(ss);
+ int envpsize, ix;
+
+ if (url == NULL)
+ http_error(500, request, "malloc failed");
+ query = path = NULL;
+ envp = NULL;
debug((DEBUG_FAT, "process_cgi: writing HTTP header .."));
@@ -1261,8 +1332,11 @@
} else
info = "";
- if (command == NULL)
+ if (command == NULL) {
command = url;
+ if (*command == '/')
+ command++;
+ }
if ((s = strchr(url, '?')) != NULL) {
*s++ = '\0';
@@ -1270,71 +1344,20 @@
} else
query = NULL;
- debug((DEBUG_FAT, "process_cgi: query %s cmd %s info %s", query,
- command, info));
+ debug((DEBUG_FAT, "process_cgi: cmd %s info %s query %s",
+ command, info, query ? query : "<null>"));
type = request->content_type;
clen = request->content_length;
- if (getpeername(0, (struct sockaddr *)&ss, &slen) < 0)
- host = addr = NULL;
- else {
-#ifndef SOCKOLD
- char hbuf[NI_MAXHOST], abuf[NI_MAXSERV];
-
- addr = host = NULL;
- if (getnameinfo((struct sockaddr *)&ss, slen,
- abuf, sizeof abuf, NULL, 0, NI_NUMERICHOST) == 0)
- addr = abuf;
- if (nflag != 0 && getnameinfo((struct sockaddr *)&ss, slen,
- hbuf, sizeof hbuf, NULL, 0, 0) == 0)
- host = hbuf;
-#else
- struct hostent *hp;
- struct sockaddr_in *sin;
-
- switch (((struct sockaddr *)&ss)->sa_family) {
- case AF_INET:
- sin = (struct sockaddr_in *)&ss;
- addr = inet_ntoa(sin->sin_addr);
- if (nflag == 0 ||
- (hp = gethostbyaddr((char *)&sin->sin_addr,
- sizeof(sin->sin_addr), AF_INET)) == NULL)
- host = NULL;
- else
- host = hp->h_name;
- break;
- default:
- host = addr = NULL;
- break;
- }
-#endif
- }
-
- slen = sizeof(ss);
- if (getsockname(0, (struct sockaddr *)&ss, &slen) < 0)
- port = NULL;
- else {
-#ifndef SOCKOLD
- if (getnameinfo((struct sockaddr *)&ss, slen, NULL, 0,
- bufport, sizeof bufport, NI_NUMERICSERV) == 0)
- port = bufport;
- else
- port = NULL;
-#else
- (void)snprintf(port = bufport, sizeof bufport, "%d",
- ntohs(((struct sockaddr_in *)&ss)->sin_port));
-#endif
- }
-
envpsize = 10 + request->nheaders +
(info && *info ? 1 : 0) +
(query && query ? 1 : 0) +
(type && *type ? 1 : 0) +
(clen && *clen ? 1 : 0) +
- (host && *host ? 1 : 0) +
- (addr && *addr ? 1 : 0) +
- (port && *port ? 1 : 0);
+ (request->remotehost && *request->remotehost ? 1 : 0) +
+ (request->remoteaddr && *request->remoteaddr ? 1 : 0) +
+ (request->serverport && *request->serverport ? 1 : 0);
envp = malloc(sizeof(*envp) * envpsize);
if (envp == NULL)
@@ -1368,8 +1391,11 @@
for (s = headp->header; *s; t++, s++)
if (islower(*s))
*t = toupper(*s);
+ else if (*s == '-')
+ *t = '_';
else
*t = *s;
+ *t = '\0';
debug((DEBUG_OBESE, "setting header %s as %s = %s",
headp->header, env, headp->value));
spsetenv(env, headp->value);
@@ -1382,7 +1408,12 @@
spsetenv("GATEWAY_INTERFACE", "CGI/1.1");
spsetenv("SERVER_PROTOCOL", request->proto);
spsetenv("REQUEST_METHOD", request->methodstr);
- spsetenv("SCRIPT_NAME", command);
+ fullcommand = malloc(CGIBIN_PREFIX_LEN + strlen(command) + 1);
+ if (fullcommand == NULL)
+ http_error(500, request, "malloc failed");
+ strcpy(fullcommand, CGIBIN_PREFIX);
+ strcat(fullcommand, command);
+ spsetenv("SCRIPT_NAME", fullcommand);
spsetenv("SERVER_SOFTWARE", server_software);
if (query && *query)
spsetenv("QUERY_STRING", query);
@@ -1392,12 +1423,12 @@
spsetenv("CONTENT_TYPE", type);
if (clen && *clen)
spsetenv("CONTENT_LENGTH", clen);
- if (port && *port)
- spsetenv("SERVER_PORT", port);
- if (host && *host)
- spsetenv("REMOTE_HOST", host);
- if (addr && *addr)
- spsetenv("REMOTE_ADDR", addr);
+ if (request->serverport && *request->serverport)
+ spsetenv("SERVER_PORT", request->serverport);
+ if (request->remotehost && *request->remotehost)
+ spsetenv("REMOTE_HOST", request->remotehost);
+ if (request->remoteaddr && *request->remoteaddr)
+ spsetenv("REMOTE_ADDR", request->remoteaddr);
#undef spsetenv
path = malloc(strlen(cgibin) + 1 + strlen(command) + 1);
@@ -1419,7 +1450,8 @@
argv[0] = command;
argv[1] = query;
argv[2] = NULL;
- debug((DEBUG_FAT, "going exec %s %s %s", path, command, query));
+ debug((DEBUG_FAT, "going exec %s %s %s", path, command,
+ query ? query : "<null>"));
if (execve(path, argv, envp) < 0)
error(1, "child exec failed");
}
@@ -1734,7 +1766,7 @@
int savederrno;
/* only log if the level is low enough */
- if (level < dflag)
+ if (dflag < level)
return;
savederrno = errno;
|