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