]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
d8182dba17f83dc394426e3f3ec1800f5f4a8112
[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 the & sign */
96                 ptr = up;
97                 b = strlen(up);
98                 for (a = 0; a < strlen(up); ++a) {
99                         if (!strncmp(ptr, "&", 1)) {
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  *
280  * Bit 3: suppress check for express messages
281  */
282 void output_headers(int controlcode)
283 {
284         char cookie[SIZ];
285         int print_standard_html_head = 0;
286         int refresh30 = 0;
287         int suppress_check = 0;
288         char httpnow[SIZ];
289         static int pageseq = 0;
290         print_standard_html_head        =       controlcode & 0x03;
291         refresh30                       =       ((controlcode & 0x04) >> 2);
292         suppress_check                  =       ((controlcode & 0x08) >> 3);
293
294         wprintf("HTTP/1.0 200 OK\n");
295
296         httpdate(httpnow, time(NULL));
297
298         if (print_standard_html_head > 0) {
299                 wprintf("Content-type: text/html\n"
300                         "Server: %s\n", SERVER
301                 );
302                 wprintf("Connection: close\n"
303                         "Pragma: no-cache\n"
304                         "Cache-Control: no-store\n"
305                 );
306         }
307
308         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
309                         WC->wc_password, WC->wc_roomname);
310
311         if (print_standard_html_head == 2) {
312                 wprintf("Set-cookie: webcit=%s\n", unset);
313         } else {
314                 wprintf("Set-cookie: webcit=%s\n", cookie);
315                 if (server_cookie != NULL) {
316                         wprintf("%s\n", server_cookie);
317                 }
318         }
319
320         if (print_standard_html_head > 0) {
321                 wprintf("\n");
322
323                 if (refresh30) svprintf("REFRESHTAG", WCS_STRING,
324                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
325                 else svprintf("REFRESHTAG", WCS_STRING,
326                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n");
327                 /* script for checking for pages (not always launched) */
328                 svprintf("PAGERSCRIPT", WCS_STRING,
329                         "<SCRIPT LANGUAGE=\"JavaScript\">\n"
330                         "function launch_page_popup() {\n"
331                         "pwin = window.open('/page_popup', 'CitaPage%d', "
332                         "'toolbar=no,location=no,copyhistory=no,status=no,"
333                         "scrollbars=yes,resizable=no,height=250,width=400');\n"
334                         "}\n"
335                         "</SCRIPT>\n",
336                         ++pageseq
337                 );
338                 /* end script */
339
340                 do_template("head");
341                 clear_local_substs();
342
343                 if (!suppress_check) if (WC->HaveExpressMessages) {
344                         svprintf("extrabodyparms", WCS_STRING, "%s", 
345                                 "onload=\"launch_page_popup()\" ");
346                         WC->HaveExpressMessages = 0;
347                 }
348
349                 do_template("background");
350                 clear_local_substs();
351         }
352
353         if (print_standard_html_head == 1) {
354                 wprintf("<A NAME=\"TheTop\"></A>");
355                 embed_room_banner(NULL);
356         }
357
358         if (strlen(WC->ImportantMessage) > 0) {
359                 do_template("beginbox_nt");
360                 wprintf("<SPAN CLASS=\"errormsg\">"
361                         "%s</SPAN><BR>\n", WC->ImportantMessage);
362                 do_template("endbox");
363                 strcpy(WC->ImportantMessage, "");
364         }       
365 }
366
367
368 /*
369  *
370  */
371 void http_redirect(char *whichpage) {
372         wprintf("HTTP/1.0 302 Moved Temporarily\n");
373         wprintf("Location: %s\n", whichpage);
374         wprintf("URI: %s\n", whichpage);
375         wprintf("Content-type: text/html\n\n");
376         wprintf("<html><body>\n");
377         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
378                 whichpage);
379         wprintf("</body></html>\n");
380 }
381
382
383
384 void check_for_express_messages()
385 {
386         char buf[SIZ];
387
388         serv_puts("NOOP");
389         serv_gets(buf);
390         if (buf[3] == '*') WC->HaveExpressMessages = 1;
391 }
392
393
394
395
396 /* 
397  * Output a piece of content to the web browser
398  */
399 void http_transmit_thing(char *thing, size_t length, char *content_type) {
400         output_headers(0);
401         wprintf("Content-type: %s\n"
402                 "Content-length: %ld\n"
403                 "Server: %s\n"
404                 "Connection: close\n"
405                 "\n",
406                 content_type,
407                 (long) length,
408                 SERVER
409         );
410         write(WC->http_sock, thing, (size_t)length);
411 }
412
413
414
415
416 void output_static(char *what)
417 {
418         char buf[4096];
419         FILE *fp;
420         struct stat statbuf;
421         off_t bytes;
422         char *bigbuffer;
423         char content_type[SIZ];
424
425         lprintf(9, "output_static(%s)\n", what);
426         sprintf(buf, "static/%s", what);
427         fp = fopen(buf, "rb");
428         if (fp == NULL) {
429                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
430                 wprintf("Content-Type: text/plain\n");
431                 wprintf("\n");
432                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
433         } else {
434                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
435                         strcpy(content_type, "image/gif");
436                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
437                         strcpy(content_type, "text/plain");
438                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
439                         strcpy(content_type, "text/css");
440                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
441                         strcpy(content_type, "image/jpeg");
442                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
443                         strcpy(content_type, "image/png");
444                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
445                         strcpy(content_type, "text/html");
446                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
447                         strcpy(content_type, "text/vnd.wap.wml");
448                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
449                         strcpy(content_type, "text/vnd.wap.wmlscript");
450                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
451                         strcpy(content_type, "application/vnd.wap.wmlc");
452                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
453                         strcpy(content_type, "application/vnd.wap.wmlscriptc");
454                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
455                         wprintf("Content-type: image/vnd.wap.wbmp");
456                 else
457                         wprintf("Content-type: application/octet-stream");
458
459                 fstat(fileno(fp), &statbuf);
460                 bytes = statbuf.st_size;
461                 lprintf(3, "Static: %s, %ld bytes\n", what, bytes);
462                 bigbuffer = malloc(bytes);
463                 fread(bigbuffer, bytes, 1, fp);
464                 fclose(fp);
465
466                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type);
467                 free(bigbuffer);
468         }
469         if (!strcasecmp(bstr("force_close_session"), "yes")) {
470                 end_webcit_session();
471         }
472 }
473
474
475 /*
476  * When the browser requests an image file from the Citadel server,
477  * this function is called to transmit it.
478  */
479 void output_image()
480 {
481         char buf[SIZ];
482         char *xferbuf = NULL;
483         off_t bytes;
484
485         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
486         serv_gets(buf);
487         if (buf[0] == '2') {
488                 bytes = extract_long(&buf[4], 0);
489                 xferbuf = malloc(bytes);
490
491                 /* Read it from the server */
492                 read_server_binary(xferbuf, bytes);
493                 serv_puts("CLOS");
494                 serv_gets(buf);
495
496                 /* Write it to the browser */
497                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif");
498                 free(xferbuf);
499
500         } else {
501
502                 /* Instead of an ugly 404, send a 1x1 transparent GIF
503                  * when there's no such image on the server.
504                  */
505                 output_static("blank.gif");
506
507                 /*
508                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
509                 output_headers(0);
510                 wprintf("Content-Type: text/plain\n"
511                         "\n"
512                         "Error retrieving image: %s\n",
513                         &buf[4]
514                 );
515                 */
516
517         }
518
519
520
521 }
522
523 /*
524  */
525 void output_mimepart()
526 {
527         char buf[SIZ];
528         off_t bytes;
529         char content_type[SIZ];
530         char *content = NULL;
531         
532         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
533         serv_gets(buf);
534         if (buf[0] == '2') {
535                 bytes = extract_long(&buf[4], 0);
536                 content = malloc(bytes);
537                 extract(content_type, &buf[4], 3);
538                 output_headers(0);
539                 read_server_binary(content, bytes);
540                 serv_puts("CLOS");
541                 serv_gets(buf);
542                 http_transmit_thing(content, bytes, content_type);
543                 free(content);
544         } else {
545                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
546                 output_headers(0);
547                 wprintf("Content-Type: text/plain\n");
548                 wprintf("\n");
549                 wprintf("Error retrieving part: %s\n", &buf[4]);
550         }
551
552 }
553
554
555 /*
556  */
557 char *load_mimepart(long msgnum, char *partnum)
558 {
559         char buf[SIZ];
560         off_t bytes;
561         char content_type[SIZ];
562         char *content;
563         
564         serv_printf("OPNA %ld|%s", msgnum, partnum);
565         serv_gets(buf);
566         if (buf[0] == '2') {
567                 bytes = extract_long(&buf[4], 0);
568                 extract(content_type, &buf[4], 3);
569
570                 content = malloc(bytes + 1);
571                 read_server_binary(content, bytes);
572
573                 serv_puts("CLOS");
574                 serv_gets(buf);
575                 content[bytes] = 0;     /* null terminate for good measure */
576                 return(content);
577         }
578         else {
579                 return(NULL);
580         }
581
582 }
583
584
585 /*
586  * Convenience functions to display a page containing only a string
587  */
588 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
589 {
590         wprintf("HTTP/1.0 200 OK\n");
591         output_headers(1);
592         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
593         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
594         wprintf("</TD></TR></TABLE><BR>\n");
595         escputs(messagetext);
596
597         wprintf("<HR>\n");
598         wDumpContent(1);
599 }
600
601
602 /*
603  * Display a blank page.
604  */
605 void blank_page(void) {
606         output_headers(7);
607         wDumpContent(2);
608 }
609
610
611 /*
612  * A template has been requested
613  */
614 void url_do_template(void) {
615         do_template(bstr("template"));
616 }
617
618
619
620 /*
621  * Offer to make any page the user's "start page."
622  */
623 void offer_start_page(void) {
624         wprintf("<A HREF=\"/change_start_page?startpage=");
625         urlescputs(WC->this_page);
626         wprintf("\">"
627                 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
628                 "</A>"
629         );
630 }
631
632
633 /* 
634  * Change the user's start page
635  */
636 void change_start_page(void) {
637
638         if (bstr("startpage") == NULL) {
639                 strcpy(WC->ImportantMessage,
640                         "startpage set to null");
641                 display_main_menu();
642                 return;
643         }
644
645         set_preference("startpage", bstr("startpage"));
646
647         output_headers(3);
648         do_template("newstartpage");
649         wDumpContent(1);
650 }
651
652
653
654
655 void display_success(char *successmessage)
656 {
657         convenience_page("007700", "OK", successmessage);
658 }
659
660
661
662 void extract_action(char *actbuf, char *cmdbuf)
663 {
664         int i;
665
666         strcpy(actbuf, cmdbuf);
667         if (!strncasecmp(actbuf, "GET /", 5))
668                 strcpy(actbuf, &actbuf[5]);
669         if (!strncasecmp(actbuf, "PUT /", 5))
670                 strcpy(actbuf, &actbuf[5]);
671         if (!strncasecmp(actbuf, "POST /", 6))
672                 strcpy(actbuf, &actbuf[6]);
673
674         for (i = 0; i < strlen(actbuf); ++i) {
675                 if (actbuf[i] == ' ') {
676                         actbuf[i] = 0;
677                         i = 0;
678                 }
679                 if (actbuf[i] == '/') {
680                         actbuf[i] = 0;
681                         i = 0;
682                 }
683                 if (actbuf[i] == '?') {
684                         actbuf[i] = 0;
685                         i = 0;
686                 }
687                 if (actbuf[i] == '&') {
688                         actbuf[i] = 0;
689                         i = 0;
690                 }
691         }
692 }
693
694
695 void upload_handler(char *name, char *filename, char *partnum, char *disp,
696                         void *content, char *cbtype, size_t length,
697                         char *encoding, void *userdata)
698 {
699         struct urlcontent *u;
700
701         /* Form fields */
702         if ( (length > 0) && (strlen(cbtype) == 0) ) {
703                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
704                 u->next = WC->urlstrings;
705                 WC->urlstrings = u;
706                 safestrncpy(u->url_key, name, sizeof(u->url_key));
707                 u->url_data = malloc(length + 1);
708                 memcpy(u->url_data, content, length);
709                 u->url_data[length] = 0;
710         }
711
712         /* Uploaded files */
713         if ( (length > 0) && (strlen(cbtype) > 0) ) {
714                 WC->upload = malloc(length);
715                 if (WC->upload != NULL) {
716                         WC->upload_length = length;
717                         safestrncpy(WC->upload_filename, filename,
718                                         sizeof(WC->upload_filename));
719                         safestrncpy(WC->upload_content_type, cbtype,
720                                         sizeof(WC->upload_content_type));
721                         memcpy(WC->upload, content, length);
722                 }
723                 else {
724                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
725                 }
726         }
727
728 }
729
730
731
732
733 /*
734  * Entry point for WebCit transaction
735  */
736 void session_loop(struct httprequest *req)
737 {
738         char cmd[SIZ];
739         char action[SIZ];
740         char buf[SIZ];
741         int a, b;
742         int ContentLength = 0;
743         int BytesRead;
744         char ContentType[512];
745         char *content;
746         char *content_end;
747         struct httprequest *hptr;
748         char browser_host[SIZ];
749         char user_agent[SIZ];
750         int body_start;
751
752         /* We stuff these with the values coming from the client cookies,
753          * so we can use them to reconnect a timed out session if we have to.
754          */
755         char c_username[SIZ];
756         char c_password[SIZ];
757         char c_roomname[SIZ];
758         char cookie[SIZ];
759
760         strcpy(c_username, "");
761         strcpy(c_password, "");
762         strcpy(c_roomname, "");
763
764         WC->upload_length = 0;
765         WC->upload = NULL;
766
767         WC->is_wap = 0;
768
769         hptr = req;
770         if (hptr == NULL) return;
771
772         strcpy(cmd, hptr->line);
773         hptr = hptr->next;
774         extract_action(action, cmd);
775
776         while (hptr != NULL) {
777                 strcpy(buf, hptr->line);
778                 hptr = hptr->next;
779
780                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
781                         strcpy(cookie, &buf[15]);
782                         cookie_to_stuff(cookie, NULL,
783                                       c_username, c_password, c_roomname);
784                 }
785                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
786                         ContentLength = atoi(&buf[16]);
787                 }
788                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
789                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
790                 }
791                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
792                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
793                 }
794                 else if (!strncasecmp(buf, "Host: ", 6)) {
795                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
796                 }
797                 /* Only WAP gateways explicitly name this content-type */
798                 else if (strstr(buf, "text/vnd.wap.wml")) {
799                         WC->is_wap = 1;
800                 }
801         }
802
803         if (ContentLength > 0) {
804                 content = malloc(ContentLength + SIZ);
805                 memset(content, 0, ContentLength + SIZ);
806                 sprintf(content, "Content-type: %s\n"
807                                 "Content-length: %d\n\n",
808                                 ContentType, ContentLength);
809                 body_start = strlen(content);
810
811                 BytesRead = 0;
812                 while (BytesRead < ContentLength) {
813                         a=read(WC->http_sock, &content[BytesRead+body_start],
814                                 ContentLength - BytesRead);
815                         if (a <= 0) BytesRead = ContentLength;
816                         else BytesRead += a;
817                 }
818
819                 if (!strncasecmp(ContentType,
820                               "application/x-www-form-urlencoded", 33)) {
821                         addurls(&content[body_start]);
822                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
823                         content_end = content + ContentLength + body_start;
824                         mime_parser(content, content_end, *upload_handler,
825                                         NULL, NULL, NULL, 0);
826                 }
827         } else {
828                 content = NULL;
829         }
830
831         /* make a note of where we are in case the user wants to save it */
832         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
833         remove_token(WC->this_page, 2, ' ');
834         remove_token(WC->this_page, 0, ' ');
835
836         /* If there are variables in the URL, we must grab them now */
837         for (a = 0; a < strlen(cmd); ++a) {
838                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
839                         for (b = a; b < strlen(cmd); ++b)
840                                 if (isspace(cmd[b]))
841                                         cmd[b] = 0;
842                         addurls(&cmd[a + 1]);
843                         cmd[a] = 0;
844                 }
845         }
846
847         /* Static content can be sent without connecting to Citadel. */
848         if (!strcasecmp(action, "static")) {
849                 strcpy(buf, &cmd[12]);
850                 for (a = 0; a < strlen(buf); ++a)
851                         if (isspace(buf[a]))
852                                 buf[a] = 0;
853                 output_static(buf);
854                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
855         }
856
857         /*
858          * If we're not connected to a Citadel server, try to hook up the
859          * connection now.
860          */
861         if (!WC->connected) {
862                 if (!strcasecmp(ctdlhost, "uds")) {
863                         /* unix domain socket */
864                         sprintf(buf, "%s/citadel.socket", ctdlport);
865                         WC->serv_sock = uds_connectsock(buf);
866                 }
867                 else {
868                         /* tcp socket */
869                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
870                 }
871
872                 if (WC->serv_sock < 0) {
873                         do_logout();
874                         goto SKIP_ALL_THIS_CRAP;
875                 }
876                 else {
877                         WC->connected = 1;
878                         serv_gets(buf); /* get the server welcome message */
879                         locate_host(browser_host, WC->http_sock);
880                         get_serv_info(browser_host, user_agent);
881                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
882                                 wprintf("You are connected to a Citadel "
883                                         "server running Citadel %d.%02d;\nin "
884                                         "order to run this version of WebCit "
885                                         "you must also have Citadel %d.%02d or"
886                                         " newer.\n\n\n",
887                                                 serv_info.serv_rev_level / 100,
888                                                 serv_info.serv_rev_level % 100,
889                                                 MINIMUM_CIT_VERSION / 100,
890                                                 MINIMUM_CIT_VERSION % 100
891                                         );
892                                 end_webcit_session();
893                                 goto SKIP_ALL_THIS_CRAP;
894                         }
895                 }
896         }
897
898         /*
899          * Functions which can be performed without logging in
900          */
901         if (!strcasecmp(action, "listsub")) {
902                 do_listsub();
903                 goto SKIP_ALL_THIS_CRAP;
904         }
905 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
906         if (!strcasecmp(action, "freebusy")) {
907                 do_freebusy(cmd);
908                 goto SKIP_ALL_THIS_CRAP;
909         }
910 #endif
911
912         check_for_express_messages();
913
914         /*
915          * If we're not logged in, but we have username and password cookies
916          * supplied by the browser, try using them to log in.
917          */
918         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
919                 serv_printf("USER %s", c_username);
920                 serv_gets(buf);
921                 if (buf[0] == '3') {
922                         serv_printf("PASS %s", c_password);
923                         serv_gets(buf);
924                         if (buf[0] == '2') {
925                                 become_logged_in(c_username, c_password, buf);
926                         }
927                 }
928         }
929         /*
930          * If we don't have a current room, but a cookie specifying the
931          * current room is supplied, make an effort to go there.
932          */
933         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
934                 serv_printf("GOTO %s", c_roomname);
935                 serv_gets(buf);
936                 if (buf[0] == '2') {
937                         strcpy(WC->wc_roomname, c_roomname);
938                 }
939         }
940
941         if (!strcasecmp(action, "image")) {
942                 output_image();
943
944         /*
945          * All functions handled below this point ... make sure we log in
946          * before doing anything else!
947          */
948         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
949                 do_login();
950         } else if (!WC->logged_in) {
951                 display_login(NULL);
952         }
953
954         /*
955          * Various commands...
956          */
957
958         else if (!strcasecmp(action, "do_welcome")) {
959                 do_welcome();
960         } else if (!strcasecmp(action, "blank")) {
961                 blank_page();
962         } else if (!strcasecmp(action, "do_template")) {
963                 url_do_template();
964         } else if (!strcasecmp(action, "display_main_menu")) {
965                 display_main_menu();
966         } else if (!strcasecmp(action, "whobbs")) {
967                 whobbs();
968         } else if (!strcasecmp(action, "knrooms")) {
969                 knrooms();
970         } else if (!strcasecmp(action, "gotonext")) {
971                 slrp_highest();
972                 gotonext();
973         } else if (!strcasecmp(action, "skip")) {
974                 gotonext();
975         } else if (!strcasecmp(action, "ungoto")) {
976                 ungoto();
977         } else if (!strcasecmp(action, "dotgoto")) {
978                 slrp_highest();
979                 smart_goto(bstr("room"));
980         } else if (!strcasecmp(action, "dotskip")) {
981                 smart_goto(bstr("room"));
982         } else if (!strcasecmp(action, "termquit")) {
983                 do_logout();
984         } else if (!strcasecmp(action, "readnew")) {
985                 readloop("readnew");
986         } else if (!strcasecmp(action, "readold")) {
987                 readloop("readold");
988         } else if (!strcasecmp(action, "readfwd")) {
989                 readloop("readfwd");
990         } else if (!strcasecmp(action, "headers")) {
991                 readloop("headers");
992         } else if (!strcasecmp(action, "display_enter")) {
993                 display_enter();
994         } else if (!strcasecmp(action, "post")) {
995                 post_message();
996         } else if (!strcasecmp(action, "delete_msg")) {
997                 delete_msg();
998         } else if (!strcasecmp(action, "confirm_move_msg")) {
999                 confirm_move_msg();
1000         } else if (!strcasecmp(action, "move_msg")) {
1001                 move_msg();
1002         } else if (!strcasecmp(action, "userlist")) {
1003                 userlist();
1004         } else if (!strcasecmp(action, "showuser")) {
1005                 showuser();
1006         } else if (!strcasecmp(action, "display_page")) {
1007                 display_page();
1008         } else if (!strcasecmp(action, "page_user")) {
1009                 page_user();
1010         } else if (!strcasecmp(action, "chat")) {
1011                 do_chat();
1012         } else if (!strcasecmp(action, "display_private")) {
1013                 display_private("", 0);
1014         } else if (!strcasecmp(action, "goto_private")) {
1015                 goto_private();
1016         } else if (!strcasecmp(action, "zapped_list")) {
1017                 zapped_list();
1018         } else if (!strcasecmp(action, "display_zap")) {
1019                 display_zap();
1020         } else if (!strcasecmp(action, "zap")) {
1021                 zap();
1022         } else if (!strcasecmp(action, "display_entroom")) {
1023                 display_entroom();
1024         } else if (!strcasecmp(action, "entroom")) {
1025                 entroom();
1026         } else if (!strcasecmp(action, "display_editroom")) {
1027                 display_editroom();
1028         } else if (!strcasecmp(action, "netedit")) {
1029                 netedit();
1030         } else if (!strcasecmp(action, "editroom")) {
1031                 editroom();
1032         } else if (!strcasecmp(action, "display_whok")) {
1033                 display_whok();
1034         } else if (!strcasecmp(action, "display_editinfo")) {
1035                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1036         } else if (!strcasecmp(action, "editinfo")) {
1037                 save_edit("Room info", "EINF 1", 1);
1038         } else if (!strcasecmp(action, "display_editbio")) {
1039                 sprintf(buf, "RBIO %s", WC->wc_username);
1040                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1041         } else if (!strcasecmp(action, "editbio")) {
1042                 save_edit("Your bio", "EBIO", 0);
1043         } else if (!strcasecmp(action, "confirm_delete_room")) {
1044                 confirm_delete_room();
1045         } else if (!strcasecmp(action, "delete_room")) {
1046                 delete_room();
1047         } else if (!strcasecmp(action, "validate")) {
1048                 validate();
1049         } else if (!strcasecmp(action, "display_editpic")) {
1050                 display_graphics_upload("your photo",
1051                                         "UIMG 0|_userpic_",
1052                                         "/editpic");
1053         } else if (!strcasecmp(action, "editpic")) {
1054                 do_graphics_upload("UIMG 1|_userpic_");
1055         } else if (!strcasecmp(action, "display_editroompic")) {
1056                 display_graphics_upload("the graphic for this room",
1057                                         "UIMG 0|_roompic_",
1058                                         "/editroompic");
1059         } else if (!strcasecmp(action, "editroompic")) {
1060                 do_graphics_upload("UIMG 1|_roompic_");
1061         } else if (!strcasecmp(action, "delete_floor")) {
1062                 delete_floor();
1063         } else if (!strcasecmp(action, "rename_floor")) {
1064                 rename_floor();
1065         } else if (!strcasecmp(action, "create_floor")) {
1066                 create_floor();
1067         } else if (!strcasecmp(action, "display_editfloorpic")) {
1068                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1069                         bstr("which_floor"));
1070                 display_graphics_upload("the graphic for this floor",
1071                                         buf,
1072                                         "/editfloorpic");
1073         } else if (!strcasecmp(action, "editfloorpic")) {
1074                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1075                         bstr("which_floor"));
1076                 do_graphics_upload(buf);
1077         } else if (!strcasecmp(action, "display_reg")) {
1078                 display_reg(0);
1079         } else if (!strcasecmp(action, "display_changepw")) {
1080                 display_changepw();
1081         } else if (!strcasecmp(action, "changepw")) {
1082                 changepw();
1083         } else if (!strcasecmp(action, "display_edit_node")) {
1084                 display_edit_node();
1085         } else if (!strcasecmp(action, "edit_node")) {
1086                 edit_node();
1087         } else if (!strcasecmp(action, "display_netconf")) {
1088                 display_netconf();
1089         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1090                 display_confirm_delete_node();
1091         } else if (!strcasecmp(action, "delete_node")) {
1092                 delete_node();
1093         } else if (!strcasecmp(action, "display_add_node")) {
1094                 display_add_node();
1095         } else if (!strcasecmp(action, "add_node")) {
1096                 add_node();
1097         } else if (!strcasecmp(action, "terminate_session")) {
1098                 slrp_highest();
1099                 terminate_session();
1100         } else if (!strcasecmp(action, "edit_me")) {
1101                 edit_me();
1102         } else if (!strcasecmp(action, "display_siteconfig")) {
1103                 display_siteconfig();
1104         } else if (!strcasecmp(action, "page_popup")) {
1105                 page_popup();
1106         } else if (!strcasecmp(action, "siteconfig")) {
1107                 siteconfig();
1108         } else if (!strcasecmp(action, "display_generic")) {
1109                 display_generic();
1110         } else if (!strcasecmp(action, "do_generic")) {
1111                 do_generic();
1112         } else if (!strcasecmp(action, "display_menubar")) {
1113                 display_menubar(1);
1114         } else if (!strcasecmp(action, "output_mimepart")) {
1115                 output_mimepart();
1116         } else if (!strcasecmp(action, "edit_vcard")) {
1117                 edit_vcard();
1118         } else if (!strcasecmp(action, "submit_vcard")) {
1119                 submit_vcard();
1120         } else if (!strcasecmp(action, "select_user_to_edit")) {
1121                 select_user_to_edit(NULL, NULL);
1122         } else if (!strcasecmp(action, "display_edituser")) {
1123                 display_edituser(NULL);
1124         } else if (!strcasecmp(action, "edituser")) {
1125                 edituser();
1126         } else if (!strcasecmp(action, "create_user")) {
1127                 create_user();
1128         } else if (!strcasecmp(action, "changeview")) {
1129                 change_view();
1130         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1131                 do_stuff_to_msgs();
1132         } else if (!strcasecmp(action, "change_start_page")) {
1133                 change_start_page();
1134         } else if (!strcasecmp(action, "display_floorconfig")) {
1135                 display_floorconfig(NULL);
1136         } else if (!strcasecmp(action, "toggle_self_service")) {
1137                 toggle_self_service();
1138 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1139         } else if (!strcasecmp(action, "display_edit_task")) {
1140                 display_edit_task();
1141         } else if (!strcasecmp(action, "save_task")) {
1142                 save_task();
1143         } else if (!strcasecmp(action, "display_edit_event")) {
1144                 display_edit_event();
1145         } else if (!strcasecmp(action, "save_event")) {
1146                 save_event();
1147         } else if (!strcasecmp(action, "respond_to_request")) {
1148                 respond_to_request();
1149         } else if (!strcasecmp(action, "handle_rsvp")) {
1150                 handle_rsvp();
1151 #endif
1152         } else if (!strcasecmp(action, "summary")) {
1153                 summary();
1154         } else if (!strcasecmp(action, "diagnostics")) {
1155                 output_headers(1);
1156
1157                 wprintf("You're in session %d<HR>\n", WC->wc_session);
1158                 wprintf("Command: <BR><PRE>\n");
1159                 escputs(cmd);
1160                 wprintf("</PRE><HR>\n");
1161                 wprintf("Variables: <BR><PRE>\n");
1162                 dump_vars();
1163                 wprintf("</PRE><HR>\n");
1164                 wDumpContent(1);
1165         }
1166         /* When all else fais, display the main menu. */
1167         else {
1168                 display_main_menu();
1169         }
1170
1171 SKIP_ALL_THIS_CRAP:
1172         fflush(stdout);
1173         if (content != NULL) {
1174                 free(content);
1175                 content = NULL;
1176         }
1177         free_urls();
1178         if (WC->upload_length > 0) {
1179                 free(WC->upload);
1180                 WC->upload_length = 0;
1181         }
1182
1183 }