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