* converted to autoconf and began port to Digital UNIX
[citadel.git] / webcit / webcit.c
1 /*
2  * webcit.c
3  *
4  * This is the actual program called by the webserver.  It maintains a
5  * persistent session to the Citadel server, handling HTTP WebCit requests as
6  * they arrive and presenting a user interface.
7  *
8  * $Id$
9  */
10
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <sys/stat.h>
18 #include <stdarg.h>
19 #include "webcit.h"
20 #include "child.h"
21
22 int wc_session;
23 char wc_host[256];
24 char wc_port[256];
25 char wc_username[256];
26 char wc_password[256];
27 char wc_roomname[256];
28 int TransactionCount = 0;
29 int connected = 0;
30 int logged_in = 0;
31 int axlevel;
32
33 struct webcontent *wlist = NULL;
34 struct webcontent *wlast = NULL;
35
36 struct urlcontent *urlstrings = NULL;
37
38
39 void unescape_input(char *buf)
40 {
41         int a,b;
42         char hex[3];
43
44         while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
45                 buf[strlen(buf)-1] = 0;
46
47         for (a=0; a<strlen(buf); ++a) {
48                 if (buf[a]=='+') buf[a]=' ';    
49                 if (buf[a]=='%') {
50                         hex[0]=buf[a+1];
51                         hex[1]=buf[a+2];
52                         hex[2]=0;
53                         sscanf(hex,"%02x",&b);
54                         buf[a] = (char) b;
55                         strcpy(&buf[a+1],&buf[a+3]);
56                         }
57                 }
58
59         }
60
61
62 void addurls(char *url) {
63         char *up, *ptr;
64         char buf[256];
65         int a,b;
66         struct urlcontent *u;
67
68         up = url;
69         while (strlen(up)>0) {
70                 
71                 /* locate the = sign */
72                 strncpy(buf,up,255);
73                 b = (-1);
74                 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
75                 if (b<0) return;
76                 buf[b]=0;
77         
78                 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
79                 u->next = urlstrings;
80                 urlstrings = u;
81                 strcpy(u->url_key, buf);
82         
83                 /* now chop that part off */
84                 for (a=0; a<=b; ++a) ++up;
85         
86                 /* locate the & sign */
87                 ptr = up;
88                 b = strlen(up);
89                 for (a=0; a<strlen(up); ++a) {
90                         if (!strncmp(ptr,"&",1)) {
91                                 b=a;
92                                 break;
93                                 }
94                         ++ptr;
95                         }
96                 ptr = up;
97                 for (a=0; a<b; ++a) ++ptr;
98                 strcpy(ptr,"");
99                 
100                 u->url_data = malloc(strlen(up));
101                 strcpy(u->url_data, up);
102                 unescape_input(u->url_data);
103
104                 up = ptr;
105                 ++up;
106                 }
107         }
108
109 void free_urls(void) {
110         struct urlcontent *u;
111
112         while (urlstrings != NULL) {
113                 free(urlstrings->url_data);
114                 u = urlstrings->next;
115                 free(urlstrings);
116                 urlstrings = u;
117                 }
118         }
119
120 char *bstr(char *key) {
121         struct urlcontent *u;
122
123         for (u = urlstrings; u != NULL; u = u->next) {
124                 if (!strcasecmp(u->url_key, key)) return(u->url_data);
125                 }
126         return("");
127         }
128
129
130 void wprintf(const char *format, ...) {   
131         va_list arg_ptr;   
132         struct webcontent *wptr;
133
134         wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
135         wptr->next = NULL;
136         if (wlist == NULL) {
137                 wlist = wptr;
138                 wlast = wptr;
139                 }
140         else {
141                 wlast->next = wptr;
142                 wlast = wptr;
143                 }
144   
145         va_start(arg_ptr, format);   
146         vsprintf(wptr->w_data, format, arg_ptr);   
147         va_end(arg_ptr);   
148   
149         }
150
151 int wContentLength(void) {
152         struct webcontent *wptr;
153         int len = 0;
154
155         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
156                 len = len + strlen(wptr->w_data);
157                 }
158
159         return(len);
160         }
161
162 void wDumpContent(void) {
163         struct webcontent *wptr;
164
165         printf("Content-type: text/html\n");
166         printf("Content-length: %d\n", wContentLength());
167         printf("\n");
168
169         while (wlist != NULL) {
170                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
171                 wptr = wlist->next;
172                 free(wlist);
173                 wlist = wptr;
174                 }
175         wlast = NULL;
176         }
177
178
179 void escputs1(char *strbuf, int nbsp)
180 {
181         int a;
182
183         for (a=0; a<strlen(strbuf); ++a) {
184                 if (strbuf[a]=='<') wprintf("&lt;");
185                 else if (strbuf[a]=='>') wprintf("&gt;");
186                 else if (strbuf[a]=='&') wprintf("&amp;");
187                 else if (strbuf[a]==34) wprintf("&quot;");
188                 else if (strbuf[a]==LB) wprintf("<");
189                 else if (strbuf[a]==RB) wprintf(">");
190                 else if (strbuf[a]==QU) wprintf("\"");
191                 else if ((strbuf[a]==32)&&(nbsp==1)) {
192                         wprintf("&nbsp;");
193                         }
194                 else {
195                         wprintf("%c", strbuf[a]);
196                         }
197                 }
198         }
199
200 void escputs(char *strbuf)
201 {
202         escputs1(strbuf,0);
203         }
204
205
206
207 char *urlesc(char *strbuf)
208 {
209         int a,b,c;
210         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
211         static char outbuf[512];
212         
213         strcpy(outbuf,"");
214
215         for (a=0; a<strlen(strbuf); ++a) {
216                 c = 0;
217                 for (b=0; b<strlen(ec); ++b) {
218                         if (strbuf[a]==ec[b]) c=1;
219                         }
220                 b = strlen(outbuf);
221                 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
222                 else sprintf(&outbuf[b],"%c",strbuf[a]);
223                 }
224         return(outbuf);
225         }
226
227 void urlescputs(char *strbuf)
228 {
229         wprintf("%s",urlesc(strbuf));
230         }
231
232
233 /*
234  * Look for URL's embedded in a buffer and make them linkable.  We use a
235  * target window in order to keep the BBS session in its own window.
236  */
237 void url(char *buf)
238 {
239
240         int pos;
241         int start,end;
242         char ench;
243         char urlbuf[256];
244         char outbuf[256];
245
246         start = (-1);
247         end = strlen(buf);
248         ench = 0;
249
250         for (pos=0; pos<strlen(buf); ++pos) {
251                 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
252                 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
253                 }
254
255         if (start<0) return;
256
257         if ((start>0)&&(buf[start-1]=='<')) ench = '>';
258         if ((start>0)&&(buf[start-1]=='[')) ench = ']';
259         if ((start>0)&&(buf[start-1]=='(')) ench = ')';
260         if ((start>0)&&(buf[start-1]=='{')) ench = '}';
261
262         for (pos=strlen(buf); pos>start; --pos) {
263                 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
264                 }
265
266         strncpy(urlbuf,&buf[start],end-start);
267         urlbuf[end-start] = 0;
268
269
270         strncpy(outbuf,buf,start);
271         sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c", 
272                 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
273         strcat(outbuf,&buf[end]);
274         strcpy(buf,outbuf);
275         }
276
277
278
279
280 void getz(char *buf) {
281         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
282         else {
283                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
284                         buf[strlen(buf)-1] = 0;
285                 }
286         }
287
288 /*
289  * Output all that important stuff that the browser will want to see
290  */
291 void output_headers(void) {
292         printf("Server: %s\n", SERVER);
293         printf("Connection: close\n");
294         printf("Set-cookie: wc_session=%d\n", wc_session);
295         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
296         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
297         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
298                 wc_username);
299         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
300                 wc_password);
301         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
302                 wc_roomname);
303         }
304
305 void output_static(char *what) {
306         char buf[256];
307         FILE *fp;
308         struct stat statbuf;
309         off_t bytes;
310
311         sprintf(buf, "static/%s", what);
312         fp = fopen(buf, "rb");
313         if (fp == NULL) {
314                 printf("HTTP/1.0 404 %s\n", strerror(errno));
315                 output_headers();
316                 printf("Content-Type: text/plain\n");
317                 sprintf(buf, "%s: %s\n", what, strerror(errno));
318                 printf("Content-length: %d\n", strlen(buf));
319                 printf("\n");
320                 fwrite(buf, strlen(buf), 1, stdout);
321                 }
322         else {
323                 printf("HTTP/1.0 200 OK\n");
324                 output_headers();
325
326                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
327                         printf("Content-type: image/gif\n");
328                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
329                         printf("Content-type: text/html\n");
330                 else
331                         printf("Content-type: application/octet-stream\n");
332
333                 fstat(fileno(fp), &statbuf);
334                 bytes = statbuf.st_size;
335                 printf("Content-length: %ld\n", (long)bytes);
336                 printf("\n");
337                 while (bytes--) {
338                         putc(getc(fp), stdout);
339                         }
340                 fclose(fp);
341                 }
342         }
343
344 static const char *defaulthost = DEFAULT_HOST;
345 static const char *defaultport = DEFAULT_PORT;
346
347 void session_loop(void) {
348         char cmd[256];
349         char buf[256];
350         int a, b;
351         int ContentLength = 0;
352         char *content;
353
354         /* We stuff these with the values coming from the client cookies,
355          * so we can use them to reconnect a timed out session if we have to.
356          */
357         char c_host[256];
358         char c_port[256];
359         char c_username[256];
360         char c_password[256];
361         char c_roomname[256];
362
363         strcpy(c_host, defaulthost);
364         strcpy(c_port, defaultport);
365         strcpy(c_username, "");
366         strcpy(c_password, "");
367         strcpy(c_roomname, "");
368
369         getz(cmd);
370
371         do {
372                 getz(buf);
373
374                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
375                         strcpy(c_host, &buf[16]);
376                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
377                         strcpy(c_port, &buf[16]);
378                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
379                         strcpy(c_username, &buf[20]);
380                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
381                         strcpy(c_password, &buf[20]);
382                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
383                         strcpy(c_roomname, &buf[20]);
384
385                 if (!strncasecmp(buf, "Content-length: ", 16)) {
386                         ContentLength = atoi(&buf[16]);
387                         }
388
389                 } while(strlen(buf)>0);
390
391         ++TransactionCount;
392
393         if (ContentLength > 0) {
394                 content = malloc(ContentLength+1);
395                 fread(content, ContentLength, 1, stdin);
396                 content[ContentLength] = 0;
397                 addurls(content);
398                 }
399         else {
400                 content = NULL;
401                 }
402
403         /*
404          * If we're not connected to a Citadel server, try to hook up the
405          * connection now.  Preference is given to the host and port specified
406          * by browser cookies, if cookies have been supplied.
407          */
408         if (!connected) {
409                 serv_sock = connectsock(c_host, c_port, "tcp");
410                 connected = 1;
411                 serv_gets(buf); /* get the server welcome message */
412                 strcpy(wc_host, c_host);
413                 strcpy(wc_port, c_port);
414                 get_serv_info();
415                 }
416
417
418         /*
419          * If we're not logged in, but we have username and password cookies
420          * supplied by the browser, try using them to log in.
421          */
422         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
423                 serv_printf("USER %s", c_username);
424                 serv_gets(buf);
425                 if (buf[0]=='3') {
426                         serv_printf("PASS %s", c_password);
427                         serv_gets(buf);
428                         if (buf[0]=='2') {
429                                 become_logged_in(c_username, c_password, buf);
430                                 }
431                         }
432                 }
433
434         /*
435          * If we don't have a current room, but a cookie specifying the
436          * current room is supplied, make an effort to go there.
437          */
438         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
439                 serv_printf("GOTO %s", c_roomname);
440                 serv_gets(buf);
441                 if (buf[0]=='2') {
442                         strcpy(wc_roomname, c_roomname);
443                         }
444                 }
445
446         /* If there are variables in the URL, we must grab them now */  
447         for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
448                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
449                 addurls(&cmd[a+1]);
450                 cmd[a] = 0;
451                 }
452
453         if (!strncasecmp(cmd, "GET /static/", 12)) {
454                 strcpy(buf, &cmd[12]);
455                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
456                 output_static(buf);
457                 }
458
459
460         else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
461                 do_login();
462                 }
463
464         else if (!logged_in) {
465                 display_login();
466                 }
467
468         /* Various commands... */
469         
470         else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
471                 do_welcome();
472                 }
473
474         else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
475                 display_main_menu();
476                 }
477
478         else if (!strncasecmp(cmd, "GET /advanced", 13)) {
479                 display_advanced_menu();
480                 }
481
482         else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
483                 whobbs();
484                 }
485
486         else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
487                 list_all_rooms_by_floor();
488                 }
489
490         else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
491                 slrp_highest();
492                 gotonext();
493                 }
494
495         else if (!strncasecmp(cmd, "GET /skip", 9)) {
496                 gotonext();
497                 }
498
499         else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
500                 ungoto();
501                 }
502
503         else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
504                 slrp_highest();
505                 dotgoto();
506                 }
507
508         else if (!strncasecmp(cmd, "GET /termquit", 13)) {
509                 do_logout();
510                 }
511
512         /* When all else fails... */
513         else {
514                 printf("HTTP/1.0 200 OK\n");
515                 output_headers();
516         
517                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
518                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
519                 wprintf("You're in session %d<BR>\n", wc_session);
520                 wprintf("</BODY></HTML>\n");
521                 wDumpContent();
522                 }
523
524         fflush(stdout);
525         if (content != NULL) {
526                 free(content);
527                 content = NULL;
528                 }
529         free_urls();
530         }
531
532 int main(int argc, char *argv[]) {
533
534         if (argc < 2 || argc > 4) {
535                 fprintf(stderr,
536                         "webcit: usage: webcit <session_id> [host [port]]\n");
537                 return 1;
538                 }
539
540         wc_session = atoi(argv[1]);
541
542         if (argc > 2) {
543                 defaulthost = argv[2];
544                 if (argc > 3)
545                         defaultport = argv[3];
546                 }
547
548         strcpy(wc_host, "");
549         strcpy(wc_port, "");
550         strcpy(wc_username, "");
551         strcpy(wc_password, "");
552         strcpy(wc_roomname, "");
553
554         while (1) {
555                 session_loop();
556                 }
557         }