Added a simple CGI endcap.
[ashd.git] / src / callcgi.c
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
33 static char **environ;
34
35 static 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
55 static void forkchild(char *prog, char *file, char *method, char *url, char *rest, int *infd, int *outfd)
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));
107         execlp(prog, prog, file, NULL);
108         exit(127);
109     }
110     close(inp[0]);
111     close(outp[1]);
112     *infd = inp[1];
113     *outfd = outp[0];
114 }
115
116 static void trim(struct charbuf *buf)
117 {
118     char *p;
119     
120     for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
121     memmove(buf->b, p, buf->d -= (p - buf->b));
122     for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
123 }
124
125 static char **parseheaders(FILE *s)
126 {
127     int c, state;
128     struct charvbuf hbuf;
129     struct charbuf buf;
130     
131     bufinit(hbuf);
132     bufinit(buf);
133     state = 0;
134     while(1) {
135         c = fgetc(s);
136     again:
137         if(state == 0) {
138             if(c == '\r') {
139             } else if(c == '\n') {
140                 break;
141             } else if(c == EOF) {
142                 goto fail;
143             } else {
144                 state = 1;
145                 goto again;
146             }
147         } else if(state == 1) {
148             if(c == ':') {
149                 trim(&buf);
150                 bufadd(buf, 0);
151                 bufadd(hbuf, buf.b);
152                 bufinit(buf);
153                 state = 2;
154             } else if(c == '\r') {
155             } else if(c == '\n') {
156                 goto fail;
157             } else if(c == EOF) {
158                 goto fail;
159             } else {
160                 bufadd(buf, c);
161             }
162         } else if(state == 2) {
163             if(c == '\r') {
164             } else if(c == '\n') {
165                 trim(&buf);
166                 bufadd(buf, 0);
167                 bufadd(hbuf, buf.b);
168                 bufinit(buf);
169                 state = 0;
170             } else if(c == EOF) {
171                 goto fail;
172             } else {
173                 bufadd(buf, c);
174             }
175         }
176     }
177     bufadd(hbuf, NULL);
178     return(hbuf.b);
179     
180 fail:
181     buffree(hbuf);
182     buffree(buf);
183     return(NULL);
184 }
185
186 static void sendstatus(char **headers, FILE *out)
187 {
188     char **hp;
189     char *status, *location;
190     
191     hp = headers;
192     status = location = NULL;
193     while(*hp) {
194         if(!strcasecmp(hp[0], "status")) {
195             status = hp[1];
196             /* Clear this header, so that it is not transmitted by sendheaders. */
197             **hp = 0;
198         } else if(!strcasecmp(hp[0], "location")) {
199             location = hp[1];
200         } else {
201             hp += 2;
202         }
203     }
204     if(status) {
205         fprintf(out, "HTTP/1.1 %s\r\n", status);
206     } else if(location) {
207         fprintf(out, "HTTP/1.1 303 See Other\r\n");
208     } else {
209         fprintf(out, "HTTP/1.1 200 OK\r\n");
210     }
211 }
212
213 static void sendheaders(char **headers, FILE *out)
214 {
215     while(*headers) {
216         if(**headers)
217             fprintf(out, "%s: %s\r\n", headers[0], headers[1]);
218         headers += 2;
219     }
220 }
221
222 int main(int argc, char **argv, char **envp)
223 {
224     char *file;
225     int in, out;
226     FILE *ins, *outs;
227     char **headers;
228     
229     environ = envp;
230     signal(SIGPIPE, SIG_IGN);
231     if(argc < 5) {
232         flog(LOG_ERR, "usage: callcgi PROGRAM METHOD URL REST");
233         exit(1);
234     }
235     if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
236         flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
237         exit(1);
238     }
239     forkchild(argv[1], file, argv[2], argv[3], argv[4], &in, &out);
240     ins = fdopen(in, "w");
241     passdata(stdin, ins);
242     fclose(ins);
243     outs = fdopen(out, "r");
244     if((headers = parseheaders(outs)) == NULL) {
245         flog(LOG_WARNING, "CGI handler returned invalid headers");
246         exit(1);
247     }
248     sendstatus(headers, stdout);
249     sendheaders(headers, stdout);
250     printf("\r\n");
251     passdata(outs, stdout);
252     return(0);
253 }