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