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