A lot of changes - really.
[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.2  1995/10/23 16:55:36  adam
45  * A lot of changes - really.
46  *
47  * Revision 1.1  1995/10/20  11:49:25  adam
48  * First version of www gateway.
49  *
50  */
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <sys/types.h>
56 #include <fcntl.h>
57 #include <sys/stat.h>
58
59 #define DEADSTRING "Your database server has terminated. To reactivate \
60 the server, please reload the server's 'front page'."
61
62 #include "wproto.h"
63
64 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
65
66 static char *prog = "cgi";
67
68 static char serverp[256] = {'\0'};
69
70 static void fatal(char *p)
71 {
72     printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
73     printf("<BODY>%s</BODY>\n", p);
74     if (*serverp)
75         unlink(serverp);
76     exit(0);
77 }
78
79 static int spawn(char *sprog)
80 {
81     int r;
82     char path[256];
83
84     sprintf(path, "%s/%s", CGIDIR, sprog);
85     switch(r = fork())
86     {
87         case -1: 
88             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); 
89             exit(1);
90         case 0: 
91             close (0);
92             close (1);
93             gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
94             execl (path, sprog, 0); 
95             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
96             exit(0);
97         default: 
98             return r;
99     }
100 }
101
102
103 /*
104  * NOTE: In the (perhaps odd) terminology used within this software,
105  * the 'server' is the present program, which is executed by the httpd
106  * server. The 'client' is the process running outside.
107  * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
108  */
109 int main()
110 {
111     char clientp[256], tmp[256], *path_info, *p, *operation, *t;
112     char combuf[COMBUF];
113     int linein = -1, lineout, data, childpid;
114
115     gw_log_init ("egw");
116     gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
117     gw_log_level (GW_LOG_ALL);
118     gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
119
120     sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
121     if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
122     {
123         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
124         fatal("Internal error in server.");
125     }
126     sprintf(serverp, "%s/srv%d", tmp, getpid());
127     if (access(serverp, R_OK|W_OK) == 0)
128     {
129         if (unlink(serverp) < 0)
130         {
131             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
132                     "Failed to delete stale fifo.");
133             fatal("Internal error in server.");
134         }
135         else
136             gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
137     }
138     if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
139     {
140         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
141         fatal("Internal error in server.");
142     }
143     if (!(path_info = getenv("PATH_INFO")))
144     {
145         gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
146         fatal("Internal error in server.");
147     }
148     operation = ++path_info;
149     while (*path_info && *path_info != '/')
150         path_info++;
151     if (*path_info)
152         *(path_info++) = '\0';
153     if ((childpid = atoi(operation)) <= 0)
154     {
155         childpid = spawn(operation);
156         /* synchronize with client. */
157         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client.");
158         if ((linein = open(serverp, O_RDONLY)) < 0)
159         {
160             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
161             fatal("Internal error in server.");
162         }
163         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
164         {
165             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
166             fatal("Internal error in server");
167         }
168         gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
169     }
170     sprintf(clientp, "%s/clt%d", tmp, childpid);
171     gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
172     if ((lineout = open(clientp, O_WRONLY)) < 0)
173     {
174         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
175         fatal(DEADSTRING);
176     }
177     gw_log (GW_LOG_DEBUG, prog, "Decoding user data.");
178     p = combuf + sizeof(data);
179     strcpy(p, serverp);
180     p += strlen(p) + 1;
181     strcpy(p, path_info);
182     p += strlen(p) + 1;
183     *(p++) = '\0';               /* no envvars tranferred at present */
184     if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
185     {
186         if (read(0, p, data) < data)
187         {
188             gw_log (GW_LOG_FATAL, prog, "Failed to read input.");
189             fatal("Internal error in server.");
190         }
191     }
192     p += data;
193     *(p++) = '\0';
194     data = (p - combuf);
195     memcpy(combuf, &data, sizeof(data));
196     gw_log (GW_LOG_DEBUG, prog, "Writing data.");
197     if (write(lineout, combuf, data) < data)
198     {
199         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
200         fatal("Internal server error.");
201     }
202     if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
203     {
204         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
205         fatal("Internal error in server.");
206     }
207     gw_log (GW_LOG_DEBUG, prog, "Reading response.");
208     while ((data = read(linein, combuf, COMBUF)) > 0)
209     {
210         if (write(1, combuf, data) < data)
211         {
212             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
213             fatal("Internal server error.");
214         }
215     }
216     if (data < 0)
217     {
218         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
219         fatal("Internal server error.");
220     }
221     gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");
222     close(linein);
223     unlink(serverp);
224     close(lineout);
225     return 0;
226 }