build: Assert libmagic presence.
[ashd.git] / src / callcgi.c
CommitLineData
8cc51634
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <errno.h>
24#include <ctype.h>
25#include <signal.h>
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <utils.h>
31#include <log.h>
32
33static char **environ;
34
35static void passdata(FILE *in, FILE *out)
36{
37 int ret;
38 char *buf;
39
40 buf = smalloc(65536);
41 while(!feof(in)) {
42 ret = fread(buf, 1, 65536, in);
43 if(ferror(in)) {
44 flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
45 break;
46 }
47 if(fwrite(buf, 1, ret, out) != ret) {
48 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
49 break;
50 }
51 }
52 free(buf);
53}
54
bac8c1f9
FT
55static char *absolutify(char *file)
56{
57 char cwd[1024];
58
59 if(*file != '/') {
60 getcwd(cwd, sizeof(cwd));
61 return(sprintf2("%s/%s", cwd, file));
62 }
63 return(sstrdup(file));
64}
65
341b3f0b 66static void forkchild(int inpath, char *prog, char *file, char *method, char *url, char *rest, int *infd, int *outfd)
8cc51634
FT
67{
68 int i;
85059e82 69 char *qp, **env, *name;
8cc51634
FT
70 int inp[2], outp[2];
71 pid_t pid;
72
73 pipe(inp);
74 pipe(outp);
75 if((pid = fork()) < 0) {
76 flog(LOG_ERR, "callcgi: could not fork");
77 exit(1);
78 }
79 if(pid == 0) {
80 close(inp[1]);
81 close(outp[0]);
82 dup2(inp[0], 0);
83 dup2(outp[1], 1);
84 for(i = 3; i < FD_SETSIZE; i++)
85 close(i);
86 if((qp = strchr(url, '?')) != NULL)
87 *(qp++) = 0;
8cc51634
FT
88 putenv(sprintf2("SERVER_SOFTWARE=ashd/%s", VERSION));
89 putenv("GATEWAY_INTERFACE=CGI/1.1");
90 if(getenv("HTTP_VERSION"))
147c2b51 91 putenv(sprintf2("SERVER_PROTOCOL=%s", getenv("HTTP_VERSION")));
8cc51634 92 putenv(sprintf2("REQUEST_METHOD=%s", method));
2d65add0 93 putenv(sprintf2("PATH_INFO=%s", rest));
85059e82
FT
94 name = url;
95 /* XXX: This is an ugly hack (I think), but though I can think
96 * of several alternatives, none seem to be better. */
2d65add0
FT
97 if(*rest && (strlen(url) >= strlen(rest)) &&
98 !strcmp(rest, url + strlen(url) - strlen(rest))) {
99 name = sprintf2("%.*s", (int)(strlen(url) - strlen(rest)), url);
85059e82
FT
100 }
101 putenv(sprintf2("SCRIPT_NAME=%s", name));
8cc51634
FT
102 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
103 if(getenv("REQ_HOST"))
104 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
8626e489
FT
105 if(getenv("REQ_X_ASH_SERVER_PORT"))
106 putenv(sprintf2("SERVER_PORT=%s", getenv("REQ_X_ASH_SERVER_PORT")));
107 if(getenv("REQ_X_ASH_PROTOCOL") && !strcmp(getenv("REQ_X_ASH_PROTOCOL"), "https"))
f0a758cc 108 putenv("HTTPS=on");
8cc51634
FT
109 if(getenv("REQ_X_ASH_ADDRESS"))
110 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
111 if(getenv("REQ_CONTENT_TYPE"))
112 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
113 if(getenv("REQ_CONTENT_LENGTH"))
114 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
115 for(env = environ; *env; env++) {
116 if(!strncmp(*env, "REQ_", 4))
117 putenv(sprintf2("HTTP_%s", (*env) + 4));
118 }
119 /*
120 * This is (understandably) missing from the CGI
121 * specification, but PHP seems to require it.
122 */
bac8c1f9 123 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
341b3f0b
FT
124 if(inpath)
125 execlp(prog, prog, file, NULL);
126 else
127 execl(prog, prog, file, NULL);
8cc51634
FT
128 exit(127);
129 }
130 close(inp[0]);
131 close(outp[1]);
132 *infd = inp[1];
133 *outfd = outp[0];
134}
135
136static void trim(struct charbuf *buf)
137{
138 char *p;
139
140 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
141 memmove(buf->b, p, buf->d -= (p - buf->b));
142 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
143}
144
145static char **parseheaders(FILE *s)
146{
147 int c, state;
148 struct charvbuf hbuf;
149 struct charbuf buf;
150
151 bufinit(hbuf);
152 bufinit(buf);
153 state = 0;
154 while(1) {
155 c = fgetc(s);
156 again:
157 if(state == 0) {
158 if(c == '\r') {
159 } else if(c == '\n') {
160 break;
161 } else if(c == EOF) {
162 goto fail;
163 } else {
164 state = 1;
165 goto again;
166 }
167 } else if(state == 1) {
168 if(c == ':') {
169 trim(&buf);
170 bufadd(buf, 0);
171 bufadd(hbuf, buf.b);
172 bufinit(buf);
173 state = 2;
174 } else if(c == '\r') {
175 } else if(c == '\n') {
176 goto fail;
177 } else if(c == EOF) {
178 goto fail;
179 } else {
180 bufadd(buf, c);
181 }
182 } else if(state == 2) {
183 if(c == '\r') {
184 } else if(c == '\n') {
185 trim(&buf);
186 bufadd(buf, 0);
187 bufadd(hbuf, buf.b);
188 bufinit(buf);
189 state = 0;
190 } else if(c == EOF) {
191 goto fail;
192 } else {
193 bufadd(buf, c);
194 }
195 }
196 }
197 bufadd(hbuf, NULL);
198 return(hbuf.b);
199
200fail:
201 buffree(hbuf);
202 buffree(buf);
203 return(NULL);
204}
205
2e535ab0
FT
206static char *defstatus(int code)
207{
208 if(code == 200)
209 return("OK");
210 else if(code == 201)
211 return("Created");
212 else if(code == 202)
213 return("Accepted");
214 else if(code == 204)
215 return("No Content");
216 else if(code == 300)
217 return("Multiple Choices");
218 else if(code == 301)
219 return("Moved Permanently");
220 else if(code == 302)
221 return("Found");
222 else if(code == 303)
223 return("See Other");
224 else if(code == 304)
225 return("Not Modified");
226 else if(code == 307)
227 return("Moved Temporarily");
228 else if(code == 400)
229 return("Bad Request");
230 else if(code == 401)
231 return("Unauthorized");
232 else if(code == 403)
233 return("Forbidden");
234 else if(code == 404)
235 return("Not Found");
236 else if(code == 500)
237 return("Internal Server Error");
238 else if(code == 501)
239 return("Not Implemented");
240 else if(code == 503)
241 return("Service Unavailable");
242 else
243 return("Unknown status");
244}
245
8cc51634
FT
246static void sendstatus(char **headers, FILE *out)
247{
248 char **hp;
249 char *status, *location;
250
251 hp = headers;
252 status = location = NULL;
253 while(*hp) {
254 if(!strcasecmp(hp[0], "status")) {
255 status = hp[1];
256 /* Clear this header, so that it is not transmitted by sendheaders. */
257 **hp = 0;
258 } else if(!strcasecmp(hp[0], "location")) {
259 location = hp[1];
f9255ddd 260 hp += 2;
8cc51634
FT
261 } else {
262 hp += 2;
263 }
264 }
265 if(status) {
2e535ab0 266 if(strchr(status, ' '))
81cfca6c 267 fprintf(out, "HTTP/1.1 %s\n", status);
2e535ab0 268 else
81cfca6c 269 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
8cc51634 270 } else if(location) {
81cfca6c 271 fprintf(out, "HTTP/1.1 303 See Other\n");
8cc51634 272 } else {
81cfca6c 273 fprintf(out, "HTTP/1.1 200 OK\n");
8cc51634
FT
274 }
275}
276
277static void sendheaders(char **headers, FILE *out)
278{
279 while(*headers) {
280 if(**headers)
81cfca6c 281 fprintf(out, "%s: %s\n", headers[0], headers[1]);
8cc51634
FT
282 headers += 2;
283 }
284}
285
341b3f0b
FT
286static void usage(void)
287{
5e74f3f1 288 flog(LOG_ERR, "usage: callcgi [-c] [-p PROGRAM] METHOD URL REST");
341b3f0b
FT
289}
290
8cc51634
FT
291int main(int argc, char **argv, char **envp)
292{
341b3f0b 293 int c;
5e74f3f1
FT
294 char *file, *prog, *sp;
295 int inpath, cd;
341b3f0b
FT
296 int infd, outfd;
297 FILE *in, *out;
8cc51634
FT
298 char **headers;
299
300 environ = envp;
301 signal(SIGPIPE, SIG_IGN);
341b3f0b
FT
302
303 prog = NULL;
304 inpath = 0;
5e74f3f1
FT
305 cd = 0;
306 while((c = getopt(argc, argv, "cp:")) >= 0) {
341b3f0b 307 switch(c) {
5e74f3f1
FT
308 case 'c':
309 cd = 1;
310 break;
341b3f0b
FT
311 case 'p':
312 prog = optarg;
313 inpath = 1;
314 break;
315 default:
316 usage();
317 exit(1);
318 }
319 }
320
321 if(argc - optind < 3) {
322 usage();
8cc51634
FT
323 exit(1);
324 }
325 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
326 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
327 exit(1);
328 }
5e74f3f1
FT
329
330 if(cd) {
331 /* This behavior is encouraged by the CGI specification (RFC 3875, 7.2),
332 * but not strictly required, and I get the feeling it might break some
333 * relative paths here or there, so it's not the default for now. */
334 if((sp = strrchr(file, '/')) != NULL) {
335 *sp = 0;
336 if(chdir(file)) {
337 *sp = '/';
338 } else {
339 file = sp + 1;
340 }
341 }
342 }
343
341b3f0b
FT
344 if(prog == NULL)
345 prog = file;
346 forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
347 in = fdopen(infd, "w");
348 passdata(stdin, in);
349 fclose(in);
350 out = fdopen(outfd, "r");
351 if((headers = parseheaders(out)) == NULL) {
8cc51634
FT
352 flog(LOG_WARNING, "CGI handler returned invalid headers");
353 exit(1);
354 }
355 sendstatus(headers, stdout);
356 sendheaders(headers, stdout);
81cfca6c 357 printf("\n");
341b3f0b 358 passdata(out, stdout);
8cc51634
FT
359 return(0);
360}