Merge branch 'master' of /srv/git/r/ashd
[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
341b3f0b 55static void forkchild(int inpath, char *prog, char *file, char *method, char *url, char *rest, int *infd, int *outfd)
8cc51634
FT
56{
57 int i;
58 char *qp, **env;
59 int inp[2], outp[2];
60 pid_t pid;
61
62 pipe(inp);
63 pipe(outp);
64 if((pid = fork()) < 0) {
65 flog(LOG_ERR, "callcgi: could not fork");
66 exit(1);
67 }
68 if(pid == 0) {
69 close(inp[1]);
70 close(outp[0]);
71 dup2(inp[0], 0);
72 dup2(outp[1], 1);
73 for(i = 3; i < FD_SETSIZE; i++)
74 close(i);
75 if((qp = strchr(url, '?')) != NULL)
76 *(qp++) = 0;
77 /*
78 * XXX: Currently missing:
79 * SERVER_NAME (Partially)
80 * SERVER_PORT
81 */
82 putenv(sprintf2("SERVER_SOFTWARE=ashd/%s", VERSION));
83 putenv("GATEWAY_INTERFACE=CGI/1.1");
84 if(getenv("HTTP_VERSION"))
85 putenv(sprintf2("SERVER_PROTOCOL=HTTP/%s", getenv("HTTP_VERSION")));
86 putenv(sprintf2("REQUEST_METHOD=%s", method));
87 putenv(sprintf2("PATH_INFO=%s", rest));
88 putenv(sprintf2("SCRIPT_NAME=%s", url));
89 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
90 if(getenv("REQ_HOST"))
91 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
92 if(getenv("REQ_X_ASH_ADDRESS"))
93 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
94 if(getenv("REQ_CONTENT_TYPE"))
95 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
96 if(getenv("REQ_CONTENT_LENGTH"))
97 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
98 for(env = environ; *env; env++) {
99 if(!strncmp(*env, "REQ_", 4))
100 putenv(sprintf2("HTTP_%s", (*env) + 4));
101 }
102 /*
103 * This is (understandably) missing from the CGI
104 * specification, but PHP seems to require it.
105 */
106 putenv(sprintf2("SCRIPT_FILENAME=%s", file));
341b3f0b
FT
107 if(inpath)
108 execlp(prog, prog, file, NULL);
109 else
110 execl(prog, prog, file, NULL);
8cc51634
FT
111 exit(127);
112 }
113 close(inp[0]);
114 close(outp[1]);
115 *infd = inp[1];
116 *outfd = outp[0];
117}
118
119static void trim(struct charbuf *buf)
120{
121 char *p;
122
123 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
124 memmove(buf->b, p, buf->d -= (p - buf->b));
125 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
126}
127
128static char **parseheaders(FILE *s)
129{
130 int c, state;
131 struct charvbuf hbuf;
132 struct charbuf buf;
133
134 bufinit(hbuf);
135 bufinit(buf);
136 state = 0;
137 while(1) {
138 c = fgetc(s);
139 again:
140 if(state == 0) {
141 if(c == '\r') {
142 } else if(c == '\n') {
143 break;
144 } else if(c == EOF) {
145 goto fail;
146 } else {
147 state = 1;
148 goto again;
149 }
150 } else if(state == 1) {
151 if(c == ':') {
152 trim(&buf);
153 bufadd(buf, 0);
154 bufadd(hbuf, buf.b);
155 bufinit(buf);
156 state = 2;
157 } else if(c == '\r') {
158 } else if(c == '\n') {
159 goto fail;
160 } else if(c == EOF) {
161 goto fail;
162 } else {
163 bufadd(buf, c);
164 }
165 } else if(state == 2) {
166 if(c == '\r') {
167 } else if(c == '\n') {
168 trim(&buf);
169 bufadd(buf, 0);
170 bufadd(hbuf, buf.b);
171 bufinit(buf);
172 state = 0;
173 } else if(c == EOF) {
174 goto fail;
175 } else {
176 bufadd(buf, c);
177 }
178 }
179 }
180 bufadd(hbuf, NULL);
181 return(hbuf.b);
182
183fail:
184 buffree(hbuf);
185 buffree(buf);
186 return(NULL);
187}
188
2e535ab0
FT
189static char *defstatus(int code)
190{
191 if(code == 200)
192 return("OK");
193 else if(code == 201)
194 return("Created");
195 else if(code == 202)
196 return("Accepted");
197 else if(code == 204)
198 return("No Content");
199 else if(code == 300)
200 return("Multiple Choices");
201 else if(code == 301)
202 return("Moved Permanently");
203 else if(code == 302)
204 return("Found");
205 else if(code == 303)
206 return("See Other");
207 else if(code == 304)
208 return("Not Modified");
209 else if(code == 307)
210 return("Moved Temporarily");
211 else if(code == 400)
212 return("Bad Request");
213 else if(code == 401)
214 return("Unauthorized");
215 else if(code == 403)
216 return("Forbidden");
217 else if(code == 404)
218 return("Not Found");
219 else if(code == 500)
220 return("Internal Server Error");
221 else if(code == 501)
222 return("Not Implemented");
223 else if(code == 503)
224 return("Service Unavailable");
225 else
226 return("Unknown status");
227}
228
8cc51634
FT
229static void sendstatus(char **headers, FILE *out)
230{
231 char **hp;
232 char *status, *location;
233
234 hp = headers;
235 status = location = NULL;
236 while(*hp) {
237 if(!strcasecmp(hp[0], "status")) {
238 status = hp[1];
239 /* Clear this header, so that it is not transmitted by sendheaders. */
240 **hp = 0;
241 } else if(!strcasecmp(hp[0], "location")) {
242 location = hp[1];
f9255ddd 243 hp += 2;
8cc51634
FT
244 } else {
245 hp += 2;
246 }
247 }
248 if(status) {
2e535ab0
FT
249 if(strchr(status, ' '))
250 fprintf(out, "HTTP/1.1 %s\r\n", status);
251 else
252 fprintf(out, "HTTP/1.1 %i %s\r\n", atoi(status), defstatus(atoi(status)));
8cc51634
FT
253 } else if(location) {
254 fprintf(out, "HTTP/1.1 303 See Other\r\n");
255 } else {
256 fprintf(out, "HTTP/1.1 200 OK\r\n");
257 }
258}
259
260static void sendheaders(char **headers, FILE *out)
261{
262 while(*headers) {
263 if(**headers)
264 fprintf(out, "%s: %s\r\n", headers[0], headers[1]);
265 headers += 2;
266 }
267}
268
341b3f0b
FT
269static void usage(void)
270{
271 flog(LOG_ERR, "usage: callcgi [-p PROGRAM] METHOD URL REST");
272}
273
8cc51634
FT
274int main(int argc, char **argv, char **envp)
275{
341b3f0b
FT
276 int c;
277 char *file, *prog;
278 int inpath;
279 int infd, outfd;
280 FILE *in, *out;
8cc51634
FT
281 char **headers;
282
283 environ = envp;
284 signal(SIGPIPE, SIG_IGN);
341b3f0b
FT
285
286 prog = NULL;
287 inpath = 0;
288 while((c = getopt(argc, argv, "p:")) >= 0) {
289 switch(c) {
290 case 'p':
291 prog = optarg;
292 inpath = 1;
293 break;
294 default:
295 usage();
296 exit(1);
297 }
298 }
299
300 if(argc - optind < 3) {
301 usage();
8cc51634
FT
302 exit(1);
303 }
304 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
305 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
306 exit(1);
307 }
341b3f0b
FT
308 if(prog == NULL)
309 prog = file;
310 forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
311 in = fdopen(infd, "w");
312 passdata(stdin, in);
313 fclose(in);
314 out = fdopen(outfd, "r");
315 if((headers = parseheaders(out)) == NULL) {
8cc51634
FT
316 flog(LOG_WARNING, "CGI handler returned invalid headers");
317 exit(1);
318 }
319 sendstatus(headers, stdout);
320 sendheaders(headers, stdout);
321 printf("\r\n");
341b3f0b 322 passdata(out, stdout);
8cc51634
FT
323 return(0);
324}