]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Minor change to the upload handler, to fix graphics uploads
[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], ".jpg", 4))
426                         wprintf("Content-type: image/jpeg\n");
427                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
428                         wprintf("Content-type: text/html\n");
429                 else
430                         wprintf("Content-type: application/octet-stream\n");
431
432                 fstat(fileno(fp), &statbuf);
433                 bytes = statbuf.st_size;
434                 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
435                 wprintf("Content-length: %ld\n", (long) bytes);
436                 wprintf("\n");
437                 bigbuffer = malloc(bytes);
438                 fread(bigbuffer, bytes, 1, fp);
439                 fclose(fp);
440                 write(WC->http_sock, bigbuffer, bytes);
441                 free(bigbuffer);
442         }
443         if (!strcasecmp(bstr("force_close_session"), "yes")) {
444                 end_webcit_session();
445         }
446 }
447
448
449 /*
450  * When the browser requests an image file from the Citadel server,
451  * this function is called to transmit it.
452  */
453 void output_image()
454 {
455         char buf[SIZ];
456         char xferbuf[4096];
457         off_t bytes;
458         off_t thisblock;
459         off_t accomplished = 0L;
460
461
462         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
463         serv_gets(buf);
464         if (buf[0] == '2') {
465                 bytes = extract_long(&buf[4], 0);
466                 output_headers(0);
467                 wprintf("Content-type: image/gif\n");
468                 wprintf("Content-length: %ld\n", (long) bytes);
469                 wprintf("\n");
470
471                 while (bytes > (off_t) 0) {
472                         thisblock = (off_t) sizeof(xferbuf);
473                         if (thisblock > bytes) {
474                                 thisblock = bytes;
475                         }
476                         serv_printf("READ %ld|%ld", accomplished, thisblock);
477                         serv_gets(buf);
478                         if (buf[0] == '6') {
479                                 thisblock = extract_long(&buf[4], 0);
480                                 serv_read(xferbuf, (int) thisblock);
481                         }
482                         else {
483                                 memset(xferbuf, 0, thisblock);
484                         }
485                         write(WC->http_sock, xferbuf, thisblock);
486                         bytes = bytes - thisblock;
487                         accomplished = accomplished + thisblock;
488                 }
489                 serv_puts("CLOS");
490                 serv_gets(buf);
491         } else {
492                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
493                 output_headers(0);
494                 wprintf("Content-Type: text/plain\n");
495                 wprintf("\n");
496                 wprintf("Error retrieving image: %s\n", &buf[4]);
497         }
498
499 }
500
501 /*
502  */
503 void output_mimepart()
504 {
505         char buf[SIZ];
506         char xferbuf[4096];
507         off_t bytes;
508         off_t thisblock;
509         off_t accomplished = 0L;
510         char content_type[SIZ];
511         
512         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
513         serv_gets(buf);
514         if (buf[0] == '2') {
515                 bytes = extract_long(&buf[4], 0);
516                 extract(content_type, &buf[4], 3);
517                 output_headers(0);
518                 wprintf("Content-type: %s\n", content_type);
519                 wprintf("Content-length: %ld\n", (long) bytes);
520                 wprintf("\n");
521
522                 while (bytes > (off_t) 0) {
523                         thisblock = (off_t) sizeof(xferbuf);
524                         if (thisblock > bytes) {
525                                 thisblock = bytes;
526                         }
527                         serv_printf("READ %ld|%ld", accomplished, thisblock);
528                         serv_gets(buf);
529                         if (buf[0] == '6') {
530                                 thisblock = extract_long(&buf[4], 0);
531                                 serv_read(xferbuf, (int) thisblock);
532                         }
533                         else {
534                                 memset(xferbuf, 0, thisblock);
535                         }
536                         write(WC->http_sock, xferbuf, thisblock);
537                         bytes = bytes - thisblock;
538                         accomplished = accomplished + thisblock;
539                 }
540                 serv_puts("CLOS");
541                 serv_gets(buf);
542         } else {
543                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
544                 output_headers(0);
545                 wprintf("Content-Type: text/plain\n");
546                 wprintf("\n");
547                 wprintf("Error retrieving part: %s\n", &buf[4]);
548         }
549
550 }
551
552
553 /*
554  * Convenience functions to display a page containing only a string
555  */
556 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
557 {
558         wprintf("HTTP/1.0 200 OK\n");
559         output_headers(1);
560         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
561         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
562         wprintf("<B>%s</B>\n", titlebarmsg);
563         wprintf("</FONT></TD></TR></TABLE><BR>\n");
564         escputs(messagetext);
565
566         wprintf("<HR>\n");
567         wDumpContent(1);
568 }
569
570
571 /*
572  * Display a blank page.
573  */
574 void blank_page(void) {
575         output_headers(7);
576         wDumpContent(2);
577 }
578
579
580 void display_error(char *errormessage)
581 {
582         convenience_page("770000", "Error", errormessage);
583 }
584
585 void display_success(char *successmessage)
586 {
587         convenience_page("007700", "OK", successmessage);
588 }
589
590
591
592 void extract_action(char *actbuf, char *cmdbuf)
593 {
594         int i;
595
596         strcpy(actbuf, cmdbuf);
597         if (!strncasecmp(actbuf, "GET /", 5))
598                 strcpy(actbuf, &actbuf[5]);
599         if (!strncasecmp(actbuf, "PUT /", 5))
600                 strcpy(actbuf, &actbuf[5]);
601         if (!strncasecmp(actbuf, "POST /", 6))
602                 strcpy(actbuf, &actbuf[6]);
603
604         for (i = 0; i < strlen(actbuf); ++i) {
605                 if (actbuf[i] == ' ') {
606                         actbuf[i] = 0;
607                         i = 0;
608                 }
609                 if (actbuf[i] == '/') {
610                         actbuf[i] = 0;
611                         i = 0;
612                 }
613                 if (actbuf[i] == '?') {
614                         actbuf[i] = 0;
615                         i = 0;
616                 }
617                 if (actbuf[i] == '&') {
618                         actbuf[i] = 0;
619                         i = 0;
620                 }
621         }
622 }
623
624
625 void upload_handler(char *name, char *filename, char *partnum, char *disp,
626                         void *content, char *cbtype, size_t length,
627                         char *encoding, void *userdata)
628 {
629
630         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
631         fprintf(stderr, "    name = %s\n", name);
632         fprintf(stderr, "filename = %s\n", filename);
633         fprintf(stderr, "encoding = %s\n", encoding);
634         fprintf(stderr, "    type = %s\n", cbtype);
635         fprintf(stderr, "  length = %ld\n", (long)length);
636
637         if (length > 0) {
638                 WC->upload = malloc(length);
639                 if (WC->upload != NULL) {
640                         WC->upload_length = length;
641                         memcpy(WC->upload, content, length);
642                 }
643                 else {
644                         fprintf(stderr, "malloc() failed: %s\n",
645                                 strerror(errno));
646                 }
647         }
648 }
649
650
651 /*
652  * Entry point for WebCit transaction
653  */
654 void session_loop(struct httprequest *req)
655 {
656         char cmd[SIZ];
657         char action[SIZ];
658         char buf[SIZ];
659         int a, b;
660         int ContentLength = 0;
661         int BytesRead;
662         char ContentType[512];
663         char *content;
664         char *content_end;
665         struct httprequest *hptr;
666         char browser_host[SIZ];
667         char user_agent[SIZ];
668
669         /* We stuff these with the values coming from the client cookies,
670          * so we can use them to reconnect a timed out session if we have to.
671          */
672         char c_host[SIZ];
673         char c_port[SIZ];
674         char c_username[SIZ];
675         char c_password[SIZ];
676         char c_roomname[SIZ];
677         char cookie[SIZ];
678
679         strcpy(c_host, defaulthost);
680         strcpy(c_port, defaultport);
681         strcpy(c_username, "");
682         strcpy(c_password, "");
683         strcpy(c_roomname, "");
684
685         WC->upload_length = 0;
686         WC->upload = NULL;
687
688         hptr = req;
689         if (hptr == NULL) return;
690
691         strcpy(cmd, hptr->line);
692         hptr = hptr->next;
693         extract_action(action, cmd);
694
695         while (hptr != NULL) {
696                 strcpy(buf, hptr->line);
697                 hptr = hptr->next;
698
699                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
700                         strcpy(cookie, &buf[15]);
701                         cookie_to_stuff(cookie, NULL,
702                                       c_username, c_password, c_roomname);
703                 }
704                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
705                         ContentLength = atoi(&buf[16]);
706                 }
707                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
708                         strcpy(ContentType, &buf[14]);
709                 }
710                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
711                         strcpy(user_agent, &buf[12]);
712                 }
713         }
714
715         if (ContentLength > 0) {
716                 fprintf(stderr, "Content length: %d\n", ContentLength);
717                 content = malloc(ContentLength + 1);
718                 memset(content, 0, ContentLength+1);
719                 BytesRead = 0;
720
721                 while (BytesRead < ContentLength) {
722                         a=read(WC->http_sock, &content[BytesRead],
723                                 ContentLength - BytesRead);
724                         if (a <= 0) BytesRead = ContentLength;
725                         else BytesRead += a;
726                 }
727
728                 if (!strncasecmp(ContentType,
729                               "application/x-www-form-urlencoded", 33)) {
730                         addurls(content);
731                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
732                         content_end = content + ContentLength;
733                         mime_parser(content, content_end, *upload_handler,
734                                         NULL, NULL, NULL, 0);
735                 }
736         } else {
737                 content = NULL;
738         }
739
740         /* If there are variables in the URL, we must grab them now */
741         for (a = 0; a < strlen(cmd); ++a)
742                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
743                         for (b = a; b < strlen(cmd); ++b)
744                                 if (isspace(cmd[b]))
745                                         cmd[b] = 0;
746                         addurls(&cmd[a + 1]);
747                         cmd[a] = 0;
748                 }
749         /*
750          * If we're not connected to a Citadel server, try to hook up the
751          * connection now.  Preference is given to the host and port specified
752          * by browser cookies, if cookies have been supplied.
753          */
754         if (!WC->connected) {
755                 if (strlen(bstr("host")) > 0)
756                         strcpy(c_host, bstr("host"));
757                 if (strlen(bstr("port")) > 0)
758                         strcpy(c_port, bstr("port"));
759
760                 if (!strcasecmp(c_host, "uds")) {
761                         /* unix domain socket */
762                         sprintf(buf, "%s/citadel.socket", c_port);
763                         WC->serv_sock = uds_connectsock(buf);
764                 }
765                 else {
766                         /* tcp socket */
767                         WC->serv_sock = tcp_connectsock(c_host, c_port);
768                 }
769
770                 if (WC->serv_sock < 0) {
771                         do_logout();
772                         goto SKIP_ALL_THIS_CRAP;
773                 }
774                 else {
775                         WC->connected = 1;
776                         serv_gets(buf); /* get the server welcome message */
777                         locate_host(browser_host, WC->http_sock);
778                         get_serv_info(browser_host, user_agent);
779                 }
780         }
781
782         check_for_express_messages();
783
784         /*
785          * If we're not logged in, but we have username and password cookies
786          * supplied by the browser, try using them to log in.
787          */
788         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
789                 serv_printf("USER %s", c_username);
790                 serv_gets(buf);
791                 if (buf[0] == '3') {
792                         serv_printf("PASS %s", c_password);
793                         serv_gets(buf);
794                         if (buf[0] == '2') {
795                                 become_logged_in(c_username, c_password, buf);
796                         }
797                 }
798         }
799         /*
800          * If we don't have a current room, but a cookie specifying the
801          * current room is supplied, make an effort to go there.
802          */
803         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
804                 serv_printf("GOTO %s", c_roomname);
805                 serv_gets(buf);
806                 if (buf[0] == '2') {
807                         strcpy(WC->wc_roomname, c_roomname);
808                 }
809         }
810         if (!strcasecmp(action, "static")) {
811                 strcpy(buf, &cmd[12]);
812                 for (a = 0; a < strlen(buf); ++a)
813                         if (isspace(buf[a]))
814                                 buf[a] = 0;
815                 output_static(buf);
816         } else if (!strcasecmp(action, "image")) {
817                 output_image();
818         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
819                 do_login();
820         } else if (!WC->logged_in) {
821                 display_login(NULL);
822         }
823         /* Various commands... */
824
825         else if (!strcasecmp(action, "do_welcome")) {
826                 do_welcome();
827         } else if (!strcasecmp(action, "blank")) {
828                 blank_page();
829         } else if (!strcasecmp(action, "display_main_menu")) {
830                 display_main_menu();
831         } else if (!strcasecmp(action, "whobbs")) {
832                 whobbs();
833         } else if (!strcasecmp(action, "knrooms")) {
834                 list_all_rooms_by_floor();
835         } else if (!strcasecmp(action, "gotonext")) {
836                 slrp_highest();
837                 gotonext();
838         } else if (!strcasecmp(action, "skip")) {
839                 gotonext();
840         } else if (!strcasecmp(action, "ungoto")) {
841                 ungoto();
842         } else if (!strcasecmp(action, "dotgoto")) {
843                 slrp_highest();
844                 smart_goto(bstr("room"));
845         } else if (!strcasecmp(action, "dotskip")) {
846                 smart_goto(bstr("room"));
847         } else if (!strcasecmp(action, "termquit")) {
848                 do_logout();
849         } else if (!strcasecmp(action, "readnew")) {
850                 readloop("readnew");
851         } else if (!strcasecmp(action, "readold")) {
852                 readloop("readold");
853         } else if (!strcasecmp(action, "readfwd")) {
854                 readloop("readfwd");
855         } else if (!strcasecmp(action, "headers")) {
856                 readloop("headers");
857         } else if (!strcasecmp(action, "display_enter")) {
858                 display_enter();
859         } else if (!strcasecmp(action, "post")) {
860                 post_message();
861         } else if (!strcasecmp(action, "delete_msg")) {
862                 delete_msg();
863         } else if (!strcasecmp(action, "confirm_move_msg")) {
864                 confirm_move_msg();
865         } else if (!strcasecmp(action, "move_msg")) {
866                 move_msg();
867         } else if (!strcasecmp(action, "userlist")) {
868                 userlist();
869         } else if (!strcasecmp(action, "showuser")) {
870                 showuser();
871         } else if (!strcasecmp(action, "display_page")) {
872                 display_page();
873         } else if (!strcasecmp(action, "page_user")) {
874                 page_user();
875         } else if (!strcasecmp(action, "chat")) {
876                 do_chat();
877         } else if (!strcasecmp(action, "display_private")) {
878                 display_private("", 0);
879         } else if (!strcasecmp(action, "goto_private")) {
880                 goto_private();
881         } else if (!strcasecmp(action, "zapped_list")) {
882                 zapped_list();
883         } else if (!strcasecmp(action, "display_zap")) {
884                 display_zap();
885         } else if (!strcasecmp(action, "zap")) {
886                 zap();
887         } else if (!strcasecmp(action, "display_entroom")) {
888                 display_entroom();
889         } else if (!strcasecmp(action, "entroom")) {
890                 entroom();
891         } else if (!strcasecmp(action, "display_editroom")) {
892                 display_editroom();
893         } else if (!strcasecmp(action, "netedit")) {
894                 netedit();
895         } else if (!strcasecmp(action, "editroom")) {
896                 editroom();
897         } else if (!strcasecmp(action, "display_whok")) {
898                 display_whok();
899         } else if (!strcasecmp(action, "display_editinfo")) {
900                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
901         } else if (!strcasecmp(action, "editinfo")) {
902                 save_edit("Room info", "EINF 1", 1);
903         } else if (!strcasecmp(action, "display_editbio")) {
904                 sprintf(buf, "RBIO %s", WC->wc_username);
905                 display_edit("Your bio", "NOOP", buf, "editbio");
906         } else if (!strcasecmp(action, "editbio")) {
907                 save_edit("Your bio", "EBIO", 0);
908         } else if (!strcasecmp(action, "confirm_delete_room")) {
909                 confirm_delete_room();
910         } else if (!strcasecmp(action, "delete_room")) {
911                 delete_room();
912         } else if (!strcasecmp(action, "validate")) {
913                 validate();
914         } else if (!strcasecmp(action, "display_editpic")) {
915                 display_graphics_upload("your photo",
916                                         "UIMG 0|_userpic_",
917                                         "/editpic");
918         } else if (!strcasecmp(action, "editpic")) {
919                 do_graphics_upload("UIMG 1|_userpic_");
920         } else if (!strcasecmp(action, "display_editroompic")) {
921                 display_graphics_upload("the graphic for this room",
922                                         "UIMG 0|_roompic_",
923                                         "/editroompic");
924         } else if (!strcasecmp(action, "editroompic")) {
925                 do_graphics_upload("UIMG 1|_roompic_");
926         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
927                 select_floor_to_edit_pic();
928         } else if (!strcasecmp(action, "display_editfloorpic")) {
929                 sprintf(buf, "UIMG 0|_floorpic_|%s",
930                         bstr("which_floor"));
931                 display_graphics_upload("the graphic for this floor",
932                                         buf,
933                                         "/editfloorpic");
934         } else if (!strcasecmp(action, "editfloorpic")) {
935                 sprintf(buf, "UIMG 1|_floorpic_|%s",
936                         bstr("which_floor"));
937                 do_graphics_upload(buf);
938         } else if (!strcasecmp(action, "display_reg")) {
939                 display_reg(0);
940         } else if (!strcasecmp(action, "register")) {
941                 register_user();
942         } else if (!strcasecmp(action, "display_changepw")) {
943                 display_changepw();
944         } else if (!strcasecmp(action, "changepw")) {
945                 changepw();
946         } else if (!strcasecmp(action, "display_edit_node")) {
947                 display_edit_node();
948         } else if (!strcasecmp(action, "edit_node")) {
949                 edit_node();
950         } else if (!strcasecmp(action, "display_netconf")) {
951                 display_netconf();
952         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
953                 display_confirm_delete_node();
954         } else if (!strcasecmp(action, "delete_node")) {
955                 delete_node();
956         } else if (!strcasecmp(action, "display_add_node")) {
957                 display_add_node();
958         } else if (!strcasecmp(action, "add_node")) {
959                 add_node();
960         } else if (!strcasecmp(action, "terminate_session")) {
961                 slrp_highest();
962                 terminate_session();
963         } else if (!strcasecmp(action, "edit_me")) {
964                 edit_me();
965         } else if (!strcasecmp(action, "display_siteconfig")) {
966                 display_siteconfig();
967         } else if (!strcasecmp(action, "page_popup")) {
968                 page_popup();
969         } else if (!strcasecmp(action, "siteconfig")) {
970                 siteconfig();
971         } else if (!strcasecmp(action, "display_generic")) {
972                 display_generic();
973         } else if (!strcasecmp(action, "do_generic")) {
974                 do_generic();
975         } else if (!strcasecmp(action, "display_menubar")) {
976                 display_menubar(1);
977         } else if (!strcasecmp(action, "output_mimepart")) {
978                 output_mimepart();
979         } else if (!strcasecmp(action, "diagnostics")) {
980                 output_headers(1);
981
982                 wprintf("You're in session %d<HR>\n", WC->wc_session);
983                 wprintf("Command: <BR><PRE>\n");
984                 escputs(cmd);
985                 wprintf("</PRE><HR>\n");
986                 wprintf("Variables: <BR><PRE>\n");
987                 dump_vars();
988                 wprintf("</PRE><HR>\n");
989                 wDumpContent(1);
990         }
991         /* When all else fais, display the main menu. */
992         else {
993                 display_main_menu();
994         }
995
996 SKIP_ALL_THIS_CRAP:
997         fflush(stdout);
998         if (content != NULL) {
999                 free(content);
1000                 content = NULL;
1001         }
1002         free_urls();
1003         if (WC->upload_length > 0) {
1004                 free(WC->upload);
1005                 WC->upload_length = 0;
1006         }
1007 }