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