First version of www gateway.
[egate.git] / www / wcgi.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  * $Log: wcgi.c,v $
44  * Revision 1.1  1995/10/20 11:49:25  adam
45  * First version of www gateway.
46  *
47  */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <sys/types.h>
53 #include <fcntl.h>
54 #include <sys/stat.h>
55
56 #define DEADSTRING "Your database server has terminated. To reactivate \
57 the server, please reload the server's 'front page'."
58
59 #include "wproto.h"
60
61 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
62
63 static char *prog = "cgi";
64
65 static char serverp[256] = {'\0'};
66
67 static void fatal(char *p)
68 {
69     printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
70     printf("<BODY>%s</BODY>\n", p);
71     if (*serverp)
72         unlink(serverp);
73     exit(0);
74 }
75
76 static int spawn(char *sprog)
77 {
78     int r;
79     char path[256];
80
81     sprintf(path, "%s/%s", CGIDIR, sprog);
82     switch(r = fork())
83     {
84         case -1: 
85             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); 
86             exit(1);
87         case 0: 
88             close (0);
89             close (1);
90             execl (path, sprog, 0); 
91             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
92             exit(0);
93         default: 
94             return r;
95     }
96 }
97
98
99 /*
100  * NOTE: In the (perhaps odd) terminology used within this software,
101  * the 'server' is the present program, which is executed by the httpd
102  * server. The 'client' is the process running outside.
103  * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
104  */
105 int main()
106 {
107     char clientp[256], tmp[256], *path_info, *p, *operation, *t;
108     char combuf[COMBUF];
109     int linein = -1, lineout, data, childpid;
110
111     gw_log_init ("egw");
112     gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
113     gw_log_level (GW_LOG_ALL);
114     gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
115
116     sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
117     if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
118     {
119         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
120         fatal("Internal error in server.");
121     }
122     sprintf(serverp, "%s/srv%d", tmp, getpid());
123     if (access(serverp, R_OK|W_OK) == 0)
124     {
125         if (unlink(serverp) < 0)
126         {
127             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
128                     "Failed to delete stale fifo.");
129             fatal("Internal error in server.");
130         }
131         else
132             gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
133     }
134     if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
135     {
136         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
137         fatal("Internal error in server.");
138     }
139     if (!(path_info = getenv("PATH_INFO")))
140     {
141         gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
142         fatal("Internal error in server.");
143     }
144     operation = ++path_info;
145     while (*path_info && *path_info != '/')
146         path_info++;
147     if (*path_info)
148         *(path_info++) = '\0';
149     if ((childpid = atoi(operation)) <= 0)
150     {
151         childpid = spawn(operation);
152         /* synchronize with client. */
153         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client.");
154         if ((linein = open(serverp, O_RDONLY)) < 0)
155         {
156             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
157             fatal("Internal error in server.");
158         }
159         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
160         {
161             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
162             fatal("Internal error in server");
163         }
164         gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
165     }
166     sprintf(clientp, "%s/clt%d", tmp, childpid);
167     gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
168     if ((lineout = open(clientp, O_WRONLY)) < 0)
169     {
170         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
171         fatal(DEADSTRING);
172     }
173     gw_log (GW_LOG_DEBUG, prog, "Decoding user data.");
174     p = combuf + sizeof(data);
175     strcpy(p, serverp);
176     p += strlen(p) + 1;
177     strcpy(p, path_info);
178     p += strlen(p) + 1;
179     *(p++) = '\0';               /* no envvars tranferred at present */
180     if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
181     {
182         if (read(0, p, data) < data)
183         {
184             gw_log (GW_LOG_FATAL, prog, "Failed to read input.");
185             fatal("Internal error in server.");
186         }
187     }
188     p += data;
189     *(p++) = '\0';
190     data = (p - combuf);
191     memcpy(combuf, &data, sizeof(data));
192     gw_log (GW_LOG_DEBUG, prog, "Writing data.");
193     if (write(lineout, combuf, data) < data)
194     {
195         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
196         fatal("Internal server error.");
197     }
198     if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
199     {
200         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
201         fatal("Internal error in server.");
202     }
203     gw_log (GW_LOG_DEBUG, prog, "Reading response.");
204     while ((data = read(linein, combuf, COMBUF)) > 0)
205     {
206         if (write(1, combuf, data) < data)
207         {
208             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
209             fatal("Internal server error.");
210         }
211     }
212     if (data < 0)
213     {
214         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
215         fatal("Internal server error.");
216     }
217     gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");
218     close(linein);
219     unlink(serverp);
220     close(lineout);
221     return 0;
222 }