]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
d86c7a763db4f53677949257efb43314ef35608e
[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 <ctype.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <sys/socket.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <limits.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include "webcit.h"
32 #include "webserver.h"
33 #include "mime_parser.h"
34
35 /*
36  * String to unset the cookie.
37  * Any date "in the past" will work, so I chose my birthday, right down to
38  * the exact minute.  :)
39  */
40 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
41
42 void unescape_input(char *buf)
43 {
44         int a, b;
45         char hex[3];
46
47         while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
48                 buf[strlen(buf) - 1] = 0;
49
50         for (a = 0; a < strlen(buf); ++a) {
51                 if (buf[a] == '+')
52                         buf[a] = ' ';
53                 if (buf[a] == '%') {
54                         hex[0] = buf[a + 1];
55                         hex[1] = buf[a + 2];
56                         hex[2] = 0;
57                         sscanf(hex, "%02x", &b);
58                         buf[a] = (char) b;
59                         strcpy(&buf[a + 1], &buf[a + 3]);
60                 }
61         }
62
63 }
64
65
66 void addurls(char *url)
67 {
68         char *up, *ptr;
69         char buf[SIZ];
70         int a, b;
71         struct urlcontent *u;
72
73         up = url;
74         while (strlen(up) > 0) {
75
76                 /* locate the = sign */
77                 safestrncpy(buf, up, sizeof buf);
78                 b = (-1);
79                 for (a = 255; a >= 0; --a)
80                         if (buf[a] == '=')
81                                 b = a;
82                 if (b < 0)
83                         return;
84                 buf[b] = 0;
85
86                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
87                 u->next = WC->urlstrings;
88                 WC->urlstrings = u;
89                 strcpy(u->url_key, buf);
90
91                 /* now chop that part off */
92                 for (a = 0; a <= b; ++a)
93                         ++up;
94
95                 /* locate "&" and "?" delimiters */
96                 ptr = up;
97                 b = strlen(up);
98                 for (a = 0; a < strlen(up); ++a) {
99                         if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
100                                 b = a;
101                                 break;
102                         }
103                         ++ptr;
104                 }
105                 ptr = up;
106                 for (a = 0; a < b; ++a)
107                         ++ptr;
108                 strcpy(ptr, "");
109
110                 u->url_data = malloc(strlen(up) + 1);
111                 strcpy(u->url_data, up);
112                 u->url_data[b] = 0;
113                 unescape_input(u->url_data);
114                 up = ptr;
115                 ++up;
116         }
117 }
118
119 void free_urls(void)
120 {
121         struct urlcontent *u;
122
123         while (WC->urlstrings != NULL) {
124                 free(WC->urlstrings->url_data);
125                 u = WC->urlstrings->next;
126                 free(WC->urlstrings);
127                 WC->urlstrings = u;
128         }
129 }
130
131 /*
132  * Diagnostic function to display the contents of all variables
133  */
134 void dump_vars(void)
135 {
136         struct urlcontent *u;
137
138         for (u = WC->urlstrings; u != NULL; u = u->next) {
139                 wprintf("%38s = %s\n", u->url_key, u->url_data);
140         }
141 }
142
143 char *bstr(char *key)
144 {
145         struct urlcontent *u;
146
147         for (u = WC->urlstrings; u != NULL; u = u->next) {
148                 if (!strcasecmp(u->url_key, key))
149                         return (u->url_data);
150         }
151         return ("");
152 }
153
154
155 void wprintf(const char *format,...)
156 {
157         va_list arg_ptr;
158         char wbuf[4096];
159
160         va_start(arg_ptr, format);
161         vsprintf(wbuf, format, arg_ptr);
162         va_end(arg_ptr);
163
164         write(WC->http_sock, wbuf, strlen(wbuf));
165 }
166
167
168 /*
169  * wDumpContent() wraps up an HTTP session, closes tags, etc.
170  *
171  * print_standard_html_footer should be set to 0 to transmit only, 1 to
172  * append the main menu and closing tags, or 2 to
173  * append the closing tags only.
174  */
175 void wDumpContent(int print_standard_html_footer)
176 {
177         if (print_standard_html_footer) {
178                 do_template("trailing");
179         }
180
181
182 }
183
184
185 /*
186  * Copy a string, escaping characters which have meaning in HTML.  If
187  * nbsp is nonzero, spaces are converted to non-breaking spaces.
188  */
189 void stresc(char *target, char *strbuf, int nbsp)
190 {
191         int a;
192         strcpy(target, "");
193
194         for (a = 0; a < strlen(strbuf); ++a) {
195                 if (strbuf[a] == '<')
196                         strcat(target, "&lt;");
197                 else if (strbuf[a] == '>')
198                         strcat(target, "&gt;");
199                 else if (strbuf[a] == '&')
200                         strcat(target, "&amp;");
201                 else if (strbuf[a] == '\"')
202                         strcat(target, "&quot;");
203                 else if (strbuf[a] == '\'') 
204                         strcat(target, "&#39;");
205                 else if (strbuf[a] == LB)
206                         strcat(target, "<");
207                 else if (strbuf[a] == RB)
208                         strcat(target, ">");
209                 else if (strbuf[a] == QU)
210                         strcat(target, "\"");
211                 else if ((strbuf[a] == 32) && (nbsp == 1)) {
212                         strcat(target, "&nbsp;");
213                 } else {
214                         strncat(target, &strbuf[a], 1);
215                 }
216         }
217 }
218
219 void escputs1(char *strbuf, int nbsp)
220 {
221         char buf[1024];
222         stresc(buf, strbuf, nbsp);
223         wprintf("%s", buf);
224 }
225
226 void escputs(char *strbuf)
227 {
228         escputs1(strbuf, 0);
229 }
230
231 /*
232  * Escape a string for feeding out as a URL.
233  * Returns a pointer to a buffer that must be freed by the caller!
234  */
235 void urlesc(char *outbuf, char *strbuf)
236 {
237         int a, b, c;
238         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
239
240         strcpy(outbuf, "");
241
242         for (a = 0; a < strlen(strbuf); ++a) {
243                 c = 0;
244                 for (b = 0; b < strlen(ec); ++b) {
245                         if (strbuf[a] == ec[b])
246                                 c = 1;
247                 }
248                 b = strlen(outbuf);
249                 if (c == 1)
250                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
251                 else
252                         sprintf(&outbuf[b], "%c", strbuf[a]);
253         }
254 }
255
256 void urlescputs(char *strbuf)
257 {
258         char outbuf[SIZ];
259         
260         urlesc(outbuf, strbuf);
261         wprintf("%s", outbuf);
262 }
263
264
265
266
267 /*
268  * Output all that important stuff that the browser will want to see
269  *
270  * control codes:
271  * 
272  * Bits 0 and 1:
273  * 0 = Nothing.  Do not display any leading HTTP or HTML.
274  * 1 = HTTP headers plus the room banner
275  * 2 = HTTP headers required to terminate the session (unset cookies)
276  * 3 = HTTP and HTML headers, but no room banner
277  *
278  * Bit 2: Set to 1 to auto-refresh page every 30 seconds
279  * Bit 3: suppress check for express messages
280  * Bit 4: Allow browser to cache this document
281  *
282  */
283 void output_headers(int controlcode)
284 {
285         char cookie[SIZ];
286         int print_standard_html_head = 0;
287         int refresh30 = 0;
288         int suppress_check = 0;
289         int cache = 0;
290         char httpnow[SIZ];
291         char onload_fcn[SIZ];
292         static int pageseq = 0;
293         print_standard_html_head        =       controlcode & 0x03;
294         refresh30                       =       ((controlcode & 0x04) >> 2);
295         suppress_check                  =       ((controlcode & 0x08) >> 3);
296         cache                           =       ((controlcode & 0x10) >> 4);
297
298         wprintf("HTTP/1.0 200 OK\n");
299
300         httpdate(httpnow, time(NULL));
301
302         if (print_standard_html_head > 0) {
303                 wprintf("Content-type: text/html\n"
304                         "Server: %s\n", SERVER
305                 );
306                 if (!cache)
307                         wprintf("Connection: close\n"
308                                 "Pragma: no-cache\n"
309                                 "Cache-Control: no-store\n"
310                         );
311         }
312
313         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
314                         WC->wc_password, WC->wc_roomname);
315
316         if (print_standard_html_head == 2) {
317                 wprintf("Set-cookie: webcit=%s\n", unset);
318         } else {
319                 wprintf("Set-cookie: webcit=%s\n", cookie);
320                 if (server_cookie != NULL) {
321                         wprintf("%s\n", server_cookie);
322                 }
323         }
324
325         if (print_standard_html_head > 0) {
326                 wprintf("\n");
327
328                 if (refresh30) svprintf("REFRESHTAG", WCS_STRING,
329                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
330                 else svprintf("REFRESHTAG", WCS_STRING,
331                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n");
332                 /* script for checking for pages (not always launched) */
333
334                 sprintf(onload_fcn, "function onload_fcn() { \n");
335                 if (!WC->outside_frameset_allowed) {
336                         strcat(onload_fcn, "  force_frameset();  \n");
337                 }
338                 if (!suppress_check) if (WC->HaveExpressMessages) {
339                         strcat(onload_fcn, "  launch_page_popup();  \n");
340                         WC->HaveExpressMessages = 0;
341                 }
342                 strcat(onload_fcn, "} \n");
343
344                 svprintf("PAGERSCRIPT", WCS_STRING,
345                         "<SCRIPT LANGUAGE=\"JavaScript\">\n"
346                         "function launch_page_popup() {\n"
347                         "pwin = window.open('/page_popup', 'CitaPage%d', "
348                         "'toolbar=no,location=no,copyhistory=no,status=no,"
349                         "scrollbars=yes,resizable=no,height=250,width=400');\n"
350                         "}\n"
351                         "function force_frameset() { \n"
352                         " if (top.frames.length == 0) { \n"
353                         "  top.location.replace('/do_welcome'); \n"
354                         " } \n"
355                         "} \n"
356                         "%s\n"
357                         "</SCRIPT>\n",
358                         ++pageseq,
359                         onload_fcn
360                 );
361                 /* end script */
362
363
364                 do_template("head");
365                 clear_local_substs();
366
367                 svprintf("extrabodyparms", WCS_STRING, "%s", 
368                         "onload='onload_fcn();' ");
369
370                 do_template("background");
371                 clear_local_substs();
372         }
373
374         if (print_standard_html_head == 1) {
375                 wprintf("<A NAME=\"TheTop\"></A>");
376                 embed_room_banner(NULL);
377         }
378
379         if (strlen(WC->ImportantMessage) > 0) {
380                 do_template("beginbox_nt");
381                 wprintf("<SPAN CLASS=\"errormsg\">"
382                         "%s</SPAN><BR>\n", WC->ImportantMessage);
383                 do_template("endbox");
384                 strcpy(WC->ImportantMessage, "");
385         }       
386 }
387
388
389 /*
390  *
391  */
392 void http_redirect(char *whichpage) {
393         wprintf("HTTP/1.0 302 Moved Temporarily\n");
394         wprintf("Location: %s\n", whichpage);
395         wprintf("URI: %s\n", whichpage);
396         wprintf("Content-type: text/html\n\n");
397         wprintf("<html><body>\n");
398         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
399                 whichpage);
400         wprintf("</body></html>\n");
401 }
402
403
404
405 void check_for_express_messages()
406 {
407         char buf[SIZ];
408
409         serv_puts("NOOP");
410         serv_gets(buf);
411         if (buf[3] == '*') WC->HaveExpressMessages = 1;
412 }
413
414
415
416
417 /* 
418  * Output a piece of content to the web browser
419  */
420 void http_transmit_thing(char *thing, size_t length, char *content_type,
421                          int is_static) {
422         output_headers(is_static ? 0x10 : 0x00);
423         wprintf("Content-type: %s\n"
424                 "Content-length: %ld\n"
425                 "Server: %s\n"
426                 "Connection: close\n"
427                 "\n",
428                 content_type,
429                 (long) length,
430                 SERVER
431         );
432         write(WC->http_sock, thing, (size_t)length);
433 }
434
435
436
437
438 void output_static(char *what)
439 {
440         char buf[4096];
441         FILE *fp;
442         struct stat statbuf;
443         off_t bytes;
444         char *bigbuffer;
445         char content_type[SIZ];
446
447         lprintf(9, "output_static(%s)\n", what);
448         sprintf(buf, "static/%s", what);
449         fp = fopen(buf, "rb");
450         if (fp == NULL) {
451                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
452                 wprintf("Content-Type: text/plain\n");
453                 wprintf("\n");
454                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
455         } else {
456                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
457                         strcpy(content_type, "image/gif");
458                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
459                         strcpy(content_type, "text/plain");
460                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
461                         strcpy(content_type, "text/css");
462                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
463                         strcpy(content_type, "image/jpeg");
464                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
465                         strcpy(content_type, "image/png");
466                 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
467                         strcpy(content_type, "image/x-icon");
468                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
469                         strcpy(content_type, "text/html");
470                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
471                         strcpy(content_type, "text/vnd.wap.wml");
472                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
473                         strcpy(content_type, "text/vnd.wap.wmlscript");
474                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
475                         strcpy(content_type, "application/vnd.wap.wmlc");
476                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
477                         strcpy(content_type, "application/vnd.wap.wmlscriptc");
478                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
479                         wprintf("Content-type: image/vnd.wap.wbmp");
480                 else
481                         wprintf("Content-type: application/octet-stream");
482
483                 fstat(fileno(fp), &statbuf);
484                 bytes = statbuf.st_size;
485                 lprintf(3, "Static: %s, %ld bytes\n", what, bytes);
486                 bigbuffer = malloc(bytes);
487                 fread(bigbuffer, bytes, 1, fp);
488                 fclose(fp);
489
490                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
491                 free(bigbuffer);
492         }
493         if (!strcasecmp(bstr("force_close_session"), "yes")) {
494                 end_webcit_session();
495         }
496 }
497
498
499 /*
500  * When the browser requests an image file from the Citadel server,
501  * this function is called to transmit it.
502  */
503 void output_image()
504 {
505         char buf[SIZ];
506         char *xferbuf = NULL;
507         off_t bytes;
508
509         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
510         serv_gets(buf);
511         if (buf[0] == '2') {
512                 bytes = extract_long(&buf[4], 0);
513                 xferbuf = malloc(bytes);
514
515                 /* Read it from the server */
516                 read_server_binary(xferbuf, bytes);
517                 serv_puts("CLOS");
518                 serv_gets(buf);
519
520                 /* Write it to the browser */
521                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
522                 free(xferbuf);
523
524         } else {
525
526                 /* Instead of an ugly 404, send a 1x1 transparent GIF
527                  * when there's no such image on the server.
528                  */
529                 output_static("blank.gif");
530
531                 /*
532                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
533                 output_headers(0);
534                 wprintf("Content-Type: text/plain\n"
535                         "\n"
536                         "Error retrieving image: %s\n",
537                         &buf[4]
538                 );
539                 */
540
541         }
542
543
544
545 }
546
547 /*
548  */
549 void output_mimepart()
550 {
551         char buf[SIZ];
552         off_t bytes;
553         char content_type[SIZ];
554         char *content = NULL;
555         
556         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
557         serv_gets(buf);
558         if (buf[0] == '2') {
559                 bytes = extract_long(&buf[4], 0);
560                 content = malloc(bytes);
561                 extract(content_type, &buf[4], 3);
562                 output_headers(0);
563                 read_server_binary(content, bytes);
564                 serv_puts("CLOS");
565                 serv_gets(buf);
566                 http_transmit_thing(content, bytes, content_type, 0);
567                 free(content);
568         } else {
569                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
570                 output_headers(0);
571                 wprintf("Content-Type: text/plain\n");
572                 wprintf("\n");
573                 wprintf("Error retrieving part: %s\n", &buf[4]);
574         }
575
576 }
577
578
579 /*
580  */
581 char *load_mimepart(long msgnum, char *partnum)
582 {
583         char buf[SIZ];
584         off_t bytes;
585         char content_type[SIZ];
586         char *content;
587         
588         serv_printf("OPNA %ld|%s", msgnum, partnum);
589         serv_gets(buf);
590         if (buf[0] == '2') {
591                 bytes = extract_long(&buf[4], 0);
592                 extract(content_type, &buf[4], 3);
593
594                 content = malloc(bytes + 1);
595                 read_server_binary(content, bytes);
596
597                 serv_puts("CLOS");
598                 serv_gets(buf);
599                 content[bytes] = 0;     /* null terminate for good measure */
600                 return(content);
601         }
602         else {
603                 return(NULL);
604         }
605
606 }
607
608
609 /*
610  * Convenience functions to display a page containing only a string
611  */
612 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
613 {
614         wprintf("HTTP/1.0 200 OK\n");
615         output_headers(1);
616         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
617         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
618         wprintf("</TD></TR></TABLE><BR>\n");
619         escputs(messagetext);
620
621         wprintf("<HR>\n");
622         wDumpContent(1);
623 }
624
625
626 /*
627  * Display a blank page.
628  */
629 void blank_page(void) {
630         output_headers(7);
631         wDumpContent(2);
632 }
633
634
635 /*
636  * A template has been requested
637  */
638 void url_do_template(void) {
639         do_template(bstr("template"));
640 }
641
642
643
644 /*
645  * Offer to make any page the user's "start page."
646  */
647 void offer_start_page(void) {
648         wprintf("<A HREF=\"/change_start_page?startpage=");
649         urlescputs(WC->this_page);
650         wprintf("\">"
651                 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
652                 "</A>"
653         );
654 }
655
656
657 /* 
658  * Change the user's start page
659  */
660 void change_start_page(void) {
661
662         if (bstr("startpage") == NULL) {
663                 strcpy(WC->ImportantMessage,
664                         "startpage set to null");
665                 display_main_menu();
666                 return;
667         }
668
669         set_preference("startpage", bstr("startpage"));
670
671         output_headers(3);
672         do_template("newstartpage");
673         wDumpContent(1);
674 }
675
676
677
678
679 void display_success(char *successmessage)
680 {
681         convenience_page("007700", "OK", successmessage);
682 }
683
684
685
686 void extract_action(char *actbuf, char *cmdbuf)
687 {
688         int i;
689
690         strcpy(actbuf, cmdbuf);
691         if (!strncasecmp(actbuf, "GET /", 5))
692                 strcpy(actbuf, &actbuf[5]);
693         if (!strncasecmp(actbuf, "PUT /", 5))
694                 strcpy(actbuf, &actbuf[5]);
695         if (!strncasecmp(actbuf, "POST /", 6))
696                 strcpy(actbuf, &actbuf[6]);
697
698         for (i = 0; i < strlen(actbuf); ++i) {
699                 if (actbuf[i] == ' ') {
700                         actbuf[i] = 0;
701                         i = 0;
702                 }
703                 if (actbuf[i] == '/') {
704                         actbuf[i] = 0;
705                         i = 0;
706                 }
707                 if (actbuf[i] == '?') {
708                         actbuf[i] = 0;
709                         i = 0;
710                 }
711                 if (actbuf[i] == '&') {
712                         actbuf[i] = 0;
713                         i = 0;
714                 }
715         }
716 }
717
718
719 void upload_handler(char *name, char *filename, char *partnum, char *disp,
720                         void *content, char *cbtype, size_t length,
721                         char *encoding, void *userdata)
722 {
723         struct urlcontent *u;
724
725         /* Form fields */
726         if ( (length > 0) && (strlen(cbtype) == 0) ) {
727                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
728                 u->next = WC->urlstrings;
729                 WC->urlstrings = u;
730                 safestrncpy(u->url_key, name, sizeof(u->url_key));
731                 u->url_data = malloc(length + 1);
732                 memcpy(u->url_data, content, length);
733                 u->url_data[length] = 0;
734         }
735
736         /* Uploaded files */
737         if ( (length > 0) && (strlen(cbtype) > 0) ) {
738                 WC->upload = malloc(length);
739                 if (WC->upload != NULL) {
740                         WC->upload_length = length;
741                         safestrncpy(WC->upload_filename, filename,
742                                         sizeof(WC->upload_filename));
743                         safestrncpy(WC->upload_content_type, cbtype,
744                                         sizeof(WC->upload_content_type));
745                         memcpy(WC->upload, content, length);
746                 }
747                 else {
748                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
749                 }
750         }
751
752 }
753
754
755
756
757 /*
758  * Entry point for WebCit transaction
759  */
760 void session_loop(struct httprequest *req)
761 {
762         char cmd[SIZ];
763         char action[SIZ];
764         char buf[SIZ];
765         int a, b;
766         int ContentLength = 0;
767         int BytesRead;
768         char ContentType[512];
769         char *content;
770         char *content_end;
771         struct httprequest *hptr;
772         char browser_host[SIZ];
773         char user_agent[SIZ];
774         int body_start;
775
776         /* We stuff these with the values coming from the client cookies,
777          * so we can use them to reconnect a timed out session if we have to.
778          */
779         char c_username[SIZ];
780         char c_password[SIZ];
781         char c_roomname[SIZ];
782         char cookie[SIZ];
783
784         strcpy(c_username, "");
785         strcpy(c_password, "");
786         strcpy(c_roomname, "");
787
788         WC->upload_length = 0;
789         WC->upload = NULL;
790
791         WC->is_wap = 0;
792
793         hptr = req;
794         if (hptr == NULL) return;
795
796         strcpy(cmd, hptr->line);
797         hptr = hptr->next;
798         extract_action(action, cmd);
799
800         while (hptr != NULL) {
801                 strcpy(buf, hptr->line);
802                 hptr = hptr->next;
803
804                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
805                         strcpy(cookie, &buf[15]);
806                         cookie_to_stuff(cookie, NULL,
807                                       c_username, c_password, c_roomname);
808                 }
809                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
810                         ContentLength = atoi(&buf[16]);
811                 }
812                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
813                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
814                 }
815                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
816                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
817                 }
818                 else if (!strncasecmp(buf, "Host: ", 6)) {
819                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
820                 }
821                 /* Only WAP gateways explicitly name this content-type */
822                 else if (strstr(buf, "text/vnd.wap.wml")) {
823                         WC->is_wap = 1;
824                 }
825         }
826
827         if (ContentLength > 0) {
828                 content = malloc(ContentLength + SIZ);
829                 memset(content, 0, ContentLength + SIZ);
830                 sprintf(content, "Content-type: %s\n"
831                                 "Content-length: %d\n\n",
832                                 ContentType, ContentLength);
833                 body_start = strlen(content);
834
835                 BytesRead = 0;
836                 while (BytesRead < ContentLength) {
837                         a=read(WC->http_sock, &content[BytesRead+body_start],
838                                 ContentLength - BytesRead);
839                         if (a <= 0) BytesRead = ContentLength;
840                         else BytesRead += a;
841                 }
842
843                 if (!strncasecmp(ContentType,
844                               "application/x-www-form-urlencoded", 33)) {
845                         addurls(&content[body_start]);
846                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
847                         content_end = content + ContentLength + body_start;
848                         mime_parser(content, content_end, *upload_handler,
849                                         NULL, NULL, NULL, 0);
850                 }
851         } else {
852                 content = NULL;
853         }
854
855         /* make a note of where we are in case the user wants to save it */
856         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
857         remove_token(WC->this_page, 2, ' ');
858         remove_token(WC->this_page, 0, ' ');
859
860         /* If there are variables in the URL, we must grab them now */
861         for (a = 0; a < strlen(cmd); ++a) {
862                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
863                         for (b = a; b < strlen(cmd); ++b)
864                                 if (isspace(cmd[b]))
865                                         cmd[b] = 0;
866                         addurls(&cmd[a + 1]);
867                         cmd[a] = 0;
868                 }
869         }
870
871         /* Static content can be sent without connecting to Citadel. */
872         if (!strcasecmp(action, "static")) {
873                 strcpy(buf, &cmd[12]);
874                 for (a = 0; a < strlen(buf); ++a)
875                         if (isspace(buf[a]))
876                                 buf[a] = 0;
877                 output_static(buf);
878                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
879         }
880
881         /*
882          * If we're not connected to a Citadel server, try to hook up the
883          * connection now.
884          */
885         if (!WC->connected) {
886                 if (!strcasecmp(ctdlhost, "uds")) {
887                         /* unix domain socket */
888                         sprintf(buf, "%s/citadel.socket", ctdlport);
889                         WC->serv_sock = uds_connectsock(buf);
890                 }
891                 else {
892                         /* tcp socket */
893                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
894                 }
895
896                 if (WC->serv_sock < 0) {
897                         do_logout();
898                         goto SKIP_ALL_THIS_CRAP;
899                 }
900                 else {
901                         WC->connected = 1;
902                         serv_gets(buf); /* get the server welcome message */
903                         locate_host(browser_host, WC->http_sock);
904                         get_serv_info(browser_host, user_agent);
905                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
906                                 wprintf("You are connected to a Citadel "
907                                         "server running Citadel %d.%02d;\nin "
908                                         "order to run this version of WebCit "
909                                         "you must also have Citadel %d.%02d or"
910                                         " newer.\n\n\n",
911                                                 serv_info.serv_rev_level / 100,
912                                                 serv_info.serv_rev_level % 100,
913                                                 MINIMUM_CIT_VERSION / 100,
914                                                 MINIMUM_CIT_VERSION % 100
915                                         );
916                                 end_webcit_session();
917                                 goto SKIP_ALL_THIS_CRAP;
918                         }
919                 }
920         }
921
922         /*
923          * Functions which can be performed without logging in
924          */
925         if (!strcasecmp(action, "listsub")) {
926                 do_listsub();
927                 goto SKIP_ALL_THIS_CRAP;
928         }
929 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
930         if (!strcasecmp(action, "freebusy")) {
931                 do_freebusy(cmd);
932                 goto SKIP_ALL_THIS_CRAP;
933         }
934 #endif
935
936         check_for_express_messages();
937
938         /*
939          * If we're not logged in, but we have username and password cookies
940          * supplied by the browser, try using them to log in.
941          */
942         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
943                 serv_printf("USER %s", c_username);
944                 serv_gets(buf);
945                 if (buf[0] == '3') {
946                         serv_printf("PASS %s", c_password);
947                         serv_gets(buf);
948                         if (buf[0] == '2') {
949                                 become_logged_in(c_username, c_password, buf);
950                         }
951                 }
952         }
953         /*
954          * If we don't have a current room, but a cookie specifying the
955          * current room is supplied, make an effort to go there.
956          */
957         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
958                 serv_printf("GOTO %s", c_roomname);
959                 serv_gets(buf);
960                 if (buf[0] == '2') {
961                         strcpy(WC->wc_roomname, c_roomname);
962                 }
963         }
964
965         if (!strcasecmp(action, "image")) {
966                 output_image();
967
968         /*
969          * All functions handled below this point ... make sure we log in
970          * before doing anything else!
971          */
972         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
973                 do_login();
974         } else if (!WC->logged_in) {
975                 display_login(NULL);
976         }
977
978         /*
979          * Various commands...
980          */
981
982         else if (!strcasecmp(action, "do_welcome")) {
983                 do_welcome();
984         } else if (!strcasecmp(action, "blank")) {
985                 blank_page();
986         } else if (!strcasecmp(action, "do_template")) {
987                 url_do_template();
988         } else if (!strcasecmp(action, "display_main_menu")) {
989                 display_main_menu();
990         } else if (!strcasecmp(action, "whobbs")) {
991                 whobbs();
992         } else if (!strcasecmp(action, "knrooms")) {
993                 knrooms();
994         } else if (!strcasecmp(action, "gotonext")) {
995                 slrp_highest();
996                 gotonext();
997         } else if (!strcasecmp(action, "skip")) {
998                 gotonext();
999         } else if (!strcasecmp(action, "ungoto")) {
1000                 ungoto();
1001         } else if (!strcasecmp(action, "dotgoto")) {
1002                 slrp_highest();
1003                 smart_goto(bstr("room"));
1004         } else if (!strcasecmp(action, "dotskip")) {
1005                 smart_goto(bstr("room"));
1006         } else if (!strcasecmp(action, "termquit")) {
1007                 do_logout();
1008         } else if (!strcasecmp(action, "readnew")) {
1009                 readloop("readnew");
1010         } else if (!strcasecmp(action, "readold")) {
1011                 readloop("readold");
1012         } else if (!strcasecmp(action, "readfwd")) {
1013                 readloop("readfwd");
1014         } else if (!strcasecmp(action, "headers")) {
1015                 readloop("headers");
1016         } else if (!strcasecmp(action, "display_enter")) {
1017                 display_enter();
1018         } else if (!strcasecmp(action, "post")) {
1019                 post_message();
1020         } else if (!strcasecmp(action, "delete_msg")) {
1021                 delete_msg();
1022         } else if (!strcasecmp(action, "confirm_move_msg")) {
1023                 confirm_move_msg();
1024         } else if (!strcasecmp(action, "move_msg")) {
1025                 move_msg();
1026         } else if (!strcasecmp(action, "userlist")) {
1027                 userlist();
1028         } else if (!strcasecmp(action, "showuser")) {
1029                 showuser();
1030         } else if (!strcasecmp(action, "display_page")) {
1031                 display_page();
1032         } else if (!strcasecmp(action, "page_user")) {
1033                 page_user();
1034         } else if (!strcasecmp(action, "chat")) {
1035                 do_chat();
1036         } else if (!strcasecmp(action, "display_private")) {
1037                 display_private("", 0);
1038         } else if (!strcasecmp(action, "goto_private")) {
1039                 goto_private();
1040         } else if (!strcasecmp(action, "zapped_list")) {
1041                 zapped_list();
1042         } else if (!strcasecmp(action, "display_zap")) {
1043                 display_zap();
1044         } else if (!strcasecmp(action, "zap")) {
1045                 zap();
1046         } else if (!strcasecmp(action, "display_entroom")) {
1047                 display_entroom();
1048         } else if (!strcasecmp(action, "entroom")) {
1049                 entroom();
1050         } else if (!strcasecmp(action, "display_editroom")) {
1051                 display_editroom();
1052         } else if (!strcasecmp(action, "netedit")) {
1053                 netedit();
1054         } else if (!strcasecmp(action, "editroom")) {
1055                 editroom();
1056         } else if (!strcasecmp(action, "display_whok")) {
1057                 display_whok();
1058         } else if (!strcasecmp(action, "display_editinfo")) {
1059                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1060         } else if (!strcasecmp(action, "editinfo")) {
1061                 save_edit("Room info", "EINF 1", 1);
1062         } else if (!strcasecmp(action, "display_editbio")) {
1063                 sprintf(buf, "RBIO %s", WC->wc_username);
1064                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1065         } else if (!strcasecmp(action, "editbio")) {
1066                 save_edit("Your bio", "EBIO", 0);
1067         } else if (!strcasecmp(action, "confirm_delete_room")) {
1068                 confirm_delete_room();
1069         } else if (!strcasecmp(action, "delete_room")) {
1070                 delete_room();
1071         } else if (!strcasecmp(action, "validate")) {
1072                 validate();
1073         } else if (!strcasecmp(action, "display_editpic")) {
1074                 display_graphics_upload("your photo",
1075                                         "UIMG 0|_userpic_",
1076                                         "/editpic");
1077         } else if (!strcasecmp(action, "editpic")) {
1078                 do_graphics_upload("UIMG 1|_userpic_");
1079         } else if (!strcasecmp(action, "display_editroompic")) {
1080                 display_graphics_upload("the graphic for this room",
1081                                         "UIMG 0|_roompic_",
1082                                         "/editroompic");
1083         } else if (!strcasecmp(action, "editroompic")) {
1084                 do_graphics_upload("UIMG 1|_roompic_");
1085         } else if (!strcasecmp(action, "delete_floor")) {
1086                 delete_floor();
1087         } else if (!strcasecmp(action, "rename_floor")) {
1088                 rename_floor();
1089         } else if (!strcasecmp(action, "create_floor")) {
1090                 create_floor();
1091         } else if (!strcasecmp(action, "display_editfloorpic")) {
1092                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1093                         bstr("which_floor"));
1094                 display_graphics_upload("the graphic for this floor",
1095                                         buf,
1096                                         "/editfloorpic");
1097         } else if (!strcasecmp(action, "editfloorpic")) {
1098                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1099                         bstr("which_floor"));
1100                 do_graphics_upload(buf);
1101         } else if (!strcasecmp(action, "display_reg")) {
1102                 display_reg(0);
1103         } else if (!strcasecmp(action, "display_changepw")) {
1104                 display_changepw();
1105         } else if (!strcasecmp(action, "changepw")) {
1106                 changepw();
1107         } else if (!strcasecmp(action, "display_edit_node")) {
1108                 display_edit_node();
1109         } else if (!strcasecmp(action, "edit_node")) {
1110                 edit_node();
1111         } else if (!strcasecmp(action, "display_netconf")) {
1112                 display_netconf();
1113         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1114                 display_confirm_delete_node();
1115         } else if (!strcasecmp(action, "delete_node")) {
1116                 delete_node();
1117         } else if (!strcasecmp(action, "display_add_node")) {
1118                 display_add_node();
1119         } else if (!strcasecmp(action, "add_node")) {
1120                 add_node();
1121         } else if (!strcasecmp(action, "terminate_session")) {
1122                 slrp_highest();
1123                 terminate_session();
1124         } else if (!strcasecmp(action, "edit_me")) {
1125                 edit_me();
1126         } else if (!strcasecmp(action, "display_siteconfig")) {
1127                 display_siteconfig();
1128         } else if (!strcasecmp(action, "chat_recv")) {
1129                 chat_recv();
1130         } else if (!strcasecmp(action, "chat_send")) {
1131                 chat_send();
1132         } else if (!strcasecmp(action, "page_popup")) {
1133                 page_popup();
1134         } else if (!strcasecmp(action, "siteconfig")) {
1135                 siteconfig();
1136         } else if (!strcasecmp(action, "display_generic")) {
1137                 display_generic();
1138         } else if (!strcasecmp(action, "do_generic")) {
1139                 do_generic();
1140         } else if (!strcasecmp(action, "display_menubar")) {
1141                 display_menubar(1);
1142         } else if (!strcasecmp(action, "output_mimepart")) {
1143                 output_mimepart();
1144         } else if (!strcasecmp(action, "edit_vcard")) {
1145                 edit_vcard();
1146         } else if (!strcasecmp(action, "submit_vcard")) {
1147                 submit_vcard();
1148         } else if (!strcasecmp(action, "select_user_to_edit")) {
1149                 select_user_to_edit(NULL, NULL);
1150         } else if (!strcasecmp(action, "display_edituser")) {
1151                 display_edituser(NULL);
1152         } else if (!strcasecmp(action, "edituser")) {
1153                 edituser();
1154         } else if (!strcasecmp(action, "create_user")) {
1155                 create_user();
1156         } else if (!strcasecmp(action, "changeview")) {
1157                 change_view();
1158         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1159                 do_stuff_to_msgs();
1160         } else if (!strcasecmp(action, "change_start_page")) {
1161                 change_start_page();
1162         } else if (!strcasecmp(action, "display_floorconfig")) {
1163                 display_floorconfig(NULL);
1164         } else if (!strcasecmp(action, "toggle_self_service")) {
1165                 toggle_self_service();
1166 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1167         } else if (!strcasecmp(action, "display_edit_task")) {
1168                 display_edit_task();
1169         } else if (!strcasecmp(action, "save_task")) {
1170                 save_task();
1171         } else if (!strcasecmp(action, "display_edit_event")) {
1172                 display_edit_event();
1173         } else if (!strcasecmp(action, "save_event")) {
1174                 save_event();
1175         } else if (!strcasecmp(action, "respond_to_request")) {
1176                 respond_to_request();
1177         } else if (!strcasecmp(action, "handle_rsvp")) {
1178                 handle_rsvp();
1179 #endif
1180         } else if (!strcasecmp(action, "summary")) {
1181                 summary();
1182         } else if (!strcasecmp(action, "iconbar")) {
1183                 do_iconbar();
1184         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1185                 display_customize_iconbar();
1186         } else if (!strcasecmp(action, "commit_iconbar")) {
1187                 commit_iconbar();
1188         } else if (!strcasecmp(action, "diagnostics")) {
1189                 output_headers(1);
1190
1191                 wprintf("You're in session %d<HR>\n", WC->wc_session);
1192                 wprintf("Command: <BR><PRE>\n");
1193                 escputs(cmd);
1194                 wprintf("</PRE><HR>\n");
1195                 wprintf("Variables: <BR><PRE>\n");
1196                 dump_vars();
1197                 wprintf("</PRE><HR>\n");
1198                 wDumpContent(1);
1199         }
1200         /* When all else fais, display the main menu. */
1201         else {
1202                 display_main_menu();
1203         }
1204
1205 SKIP_ALL_THIS_CRAP:
1206         fflush(stdout);
1207         if (content != NULL) {
1208                 free(content);
1209                 content = NULL;
1210         }
1211         free_urls();
1212         if (WC->upload_length > 0) {
1213                 free(WC->upload);
1214                 WC->upload_length = 0;
1215         }
1216
1217 }