6c29b38e08949234851850d23811aff9e80e7736
[citadel.git] / webcit / webcit.c
1 /*
2  * $Id$
3  *
4  * This is the main transaction loop of the web service.  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  */
9
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/socket.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
21 #include <limits.h>
22 #include <netinet/in.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include "webcit.h"
31 #include "groupdav.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                         b = 0;
58                         sscanf(hex, "%02x", &b);
59                         buf[a] = (char) b;
60                         strcpy(&buf[a + 1], &buf[a + 3]);
61                 }
62         }
63
64 }
65
66
67 void addurls(char *url)
68 {
69         char *up, *ptr;
70         char buf[SIZ];
71         int a, b;
72         struct urlcontent *u;
73
74         up = url;
75         while (strlen(up) > 0) {
76
77                 /* locate the = sign */
78                 safestrncpy(buf, up, sizeof buf);
79                 b = (-1);
80                 for (a = 255; a >= 0; --a)
81                         if (buf[a] == '=')
82                                 b = a;
83                 if (b < 0)
84                         return;
85                 buf[b] = 0;
86
87                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
88                 u->next = WC->urlstrings;
89                 WC->urlstrings = u;
90                 safestrncpy(u->url_key, buf, sizeof u->url_key);
91
92                 /* now chop that part off */
93                 for (a = 0; a <= b; ++a)
94                         ++up;
95
96                 /* locate "&" and "?" delimiters */
97                 ptr = up;
98                 b = strlen(up);
99                 for (a = 0; a < strlen(up); ++a) {
100                         if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
101                                 b = a;
102                                 break;
103                         }
104                         ++ptr;
105                 }
106                 ptr = up;
107                 for (a = 0; a < b; ++a)
108                         ++ptr;
109                 strcpy(ptr, "");
110
111                 u->url_data = malloc(strlen(up) + 2);
112                 safestrncpy(u->url_data, up, strlen(up) + 1);
113                 u->url_data[b] = 0;
114                 unescape_input(u->url_data);
115                 up = ptr;
116                 ++up;
117         }
118 }
119
120 void free_urls(void)
121 {
122         struct urlcontent *u;
123
124         while (WC->urlstrings != NULL) {
125                 free(WC->urlstrings->url_data);
126                 u = WC->urlstrings->next;
127                 free(WC->urlstrings);
128                 WC->urlstrings = u;
129         }
130 }
131
132 /*
133  * Diagnostic function to display the contents of all variables
134  */
135 void dump_vars(void)
136 {
137         struct urlcontent *u;
138
139         for (u = WC->urlstrings; u != NULL; u = u->next) {
140                 wprintf("%38s = %s\n", u->url_key, u->url_data);
141         }
142 }
143
144 char *bstr(char *key)
145 {
146         struct urlcontent *u;
147
148         for (u = WC->urlstrings; u != NULL; u = u->next) {
149                 if (!strcasecmp(u->url_key, key))
150                         return (u->url_data);
151         }
152         return ("");
153 }
154
155
156 void wprintf(const char *format,...)
157 {
158         va_list arg_ptr;
159         char wbuf[4096];
160
161         va_start(arg_ptr, format);
162         vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
163         va_end(arg_ptr);
164
165         client_write(wbuf, strlen(wbuf));
166 }
167
168
169 /*
170  * wDumpContent() wraps up an HTTP session, closes tags, etc.
171  *
172  * print_standard_html_footer should be set to 0 to transmit only, 1 to
173  * append the main menu and closing tags, or 2 to
174  * append the closing tags only.
175  */
176 void wDumpContent(int print_standard_html_footer)
177 {
178         if (print_standard_html_footer) {
179                 wprintf("</DIV>\n");    /* end of "text" div */
180                 do_template("trailing");
181         }
182
183         /* If we've been saving it all up for one big output burst,
184          * go ahead and do that now.
185          */
186         end_burst();
187 }
188
189
190 /*
191  * Copy a string, escaping characters which have meaning in HTML.  If
192  * nbsp is nonzero, spaces are converted to non-breaking spaces.
193  */
194 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
195 {
196         int a;
197         strcpy(target, "");
198
199         for (a = 0; a < strlen(strbuf); ++a) {
200                 if (strbuf[a] == '<')
201                         strcat(target, "&lt;");
202                 else if (strbuf[a] == '>')
203                         strcat(target, "&gt;");
204                 else if (strbuf[a] == '&')
205                         strcat(target, "&amp;");
206                 else if (strbuf[a] == '\"')
207                         strcat(target, "&quot;");
208                 else if (strbuf[a] == '\'') 
209                         strcat(target, "&#39;");
210                 else if (strbuf[a] == LB)
211                         strcat(target, "<");
212                 else if (strbuf[a] == RB)
213                         strcat(target, ">");
214                 else if (strbuf[a] == QU)
215                         strcat(target, "\"");
216                 else if ((strbuf[a] == 32) && (nbsp == 1))
217                         strcat(target, "&nbsp;");
218                 else if ((strbuf[a] == '\n') && (nolinebreaks))
219                         strcat(target, "");     /* nothing */
220                 else if ((strbuf[a] == '\r') && (nolinebreaks))
221                         strcat(target, "");     /* nothing */
222                 else
223                         strncat(target, &strbuf[a], 1);
224         }
225 }
226
227 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
228 {
229         char *buf;
230
231         if (strbuf == NULL) return;
232         buf = malloc( (3 * strlen(strbuf)) + SIZ );
233         stresc(buf, strbuf, nbsp, nolinebreaks);
234         wprintf("%s", buf);
235         free(buf);
236 }
237
238 void escputs(char *strbuf)
239 {
240         escputs1(strbuf, 0, 0);
241 }
242
243 /*
244  * Escape a string for feeding out as a URL.
245  * Returns a pointer to a buffer that must be freed by the caller!
246  */
247 void urlesc(char *outbuf, char *strbuf)
248 {
249         int a, b, c;
250         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
251
252         strcpy(outbuf, "");
253
254         for (a = 0; a < strlen(strbuf); ++a) {
255                 c = 0;
256                 for (b = 0; b < strlen(ec); ++b) {
257                         if (strbuf[a] == ec[b])
258                                 c = 1;
259                 }
260                 b = strlen(outbuf);
261                 if (c == 1)
262                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
263                 else
264                         sprintf(&outbuf[b], "%c", strbuf[a]);
265         }
266 }
267
268 void urlescputs(char *strbuf)
269 {
270         char outbuf[SIZ];
271         
272         urlesc(outbuf, strbuf);
273         wprintf("%s", outbuf);
274 }
275
276
277 /*
278  * Copy a string, escaping characters for JavaScript strings.
279  */
280 void jsesc(char *target, char *strbuf)
281 {
282         int a;
283         strcpy(target, "");
284
285         for (a = 0; a < strlen(strbuf); ++a) {
286                 if (strbuf[a] == '<')
287                         strcat(target, "[");
288                 else if (strbuf[a] == '>')
289                         strcat(target, "]");
290                 else if (strbuf[a] == '\"')
291                         strcat(target, "&quot;");
292                 else if (strbuf[a] == '&')
293                         strcat(target, "&amp;;");
294                 else if (strbuf[a] == '\'') 
295                         strcat(target, "\\'");
296                 else {
297                         strncat(target, &strbuf[a], 1);
298                 }
299         }
300 }
301
302 void jsescputs(char *strbuf)
303 {
304         char outbuf[SIZ];
305         
306         jsesc(outbuf, strbuf);
307         wprintf("%s", outbuf);
308 }
309
310 /*
311  * Copy a string, escaping characters for message text hold
312  */
313 void msgesc(char *target, char *strbuf)
314 {
315         int a;
316         strcpy(target, "");
317
318         for (a = 0; a < strlen(strbuf); ++a) {
319                 if (strbuf[a] == '\'') 
320                         strcat(target, "\\'");
321                 else if (strbuf[a] == '\n')
322                         strcat(target, " ");
323                 else if (strbuf[a] == '\r')
324                         strcat(target, " ");
325                 else {
326                         strncat(target, &strbuf[a], 1);
327                 }
328         }
329 }
330
331 void msgescputs(char *strbuf) {
332         char *outbuf;
333
334         if (strbuf == NULL) return;
335         outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
336         msgesc(outbuf, strbuf);
337         wprintf("%s", outbuf);
338         free(outbuf);
339 }
340
341
342
343
344 /*
345  * Output all that important stuff that the browser will want to see
346  */
347 void output_headers(    int do_httpheaders,     /* 1 = output HTTP headers                          */
348                         int do_htmlhead,        /* 1 = output HTML <head> section and <body> opener */
349
350                         int do_room_banner,     /* 0=no, 1=yes,                                     */
351                                                 /* 2 = I'm going to embed my own, so don't open the */
352                                                 /*     <div id="content"> either.                   */
353
354                         int unset_cookies,      /* 1 = session is terminating, so unset the cookies */
355                         int refresh30,          /* 1 = automatically refresh page every 30 seconds  */
356                         int suppress_check,     /* 1 = suppress check for instant messages          */
357                         int cache               /* 1 = allow browser to cache this page             */
358 ) {
359         char cookie[SIZ];
360         char httpnow[SIZ];
361
362         wprintf("HTTP/1.0 200 OK\n");
363         httpdate(httpnow, time(NULL));
364
365         if (do_httpheaders) {
366                 wprintf("Content-type: text/html; charset=utf-8\r\n"
367                         "Server: %s / %s\n"
368                         "Connection: close\r\n",
369                         SERVER, serv_info.serv_software
370                 );
371         }
372
373         if (cache) {
374                 wprintf("Pragma: public\r\n"
375                         "Cache-Control: max-age=3600, must-revalidate\r\n"
376                         "Last-modified: %s\r\n",
377                         httpnow
378                 );
379         }
380         else {
381                 wprintf("Pragma: no-cache\r\n"
382                         "Cache-Control: no-store\r\n"
383                 );
384         }
385
386         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
387                         WC->wc_password, WC->wc_roomname);
388
389         if (unset_cookies) {
390                 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
391         } else {
392                 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
393                 if (server_cookie != NULL) {
394                         wprintf("%s\n", server_cookie);
395                 }
396         }
397
398         if (do_htmlhead) {
399                 /* wprintf("\n"); */
400                 begin_burst();
401
402                 if (refresh30) {
403                         svprintf("REFRESHTAG", WCS_STRING, "%s",
404                                 "<meta http-equiv=\"refresh\" content=\"30\" />\n");
405                 }
406                 else {
407                         svprintf("REFRESHTAG", WCS_STRING, "%s",
408                                 "<meta http-equiv=\"refresh\" content=\"500363689;\" />\n");
409                 }
410
411                 do_template("head");
412         }
413
414         /* ICONBAR */
415         if (do_htmlhead) {
416
417                 if (WC->HaveInstantMessages) {
418                         wprintf("<div id=\"page_popup\">\n");
419                         page_popup();
420                         wprintf("</div>\n");
421                 }
422                 if ( (WC->logged_in) && (!unset_cookies) ) {
423                         wprintf("<div id=\"iconbar\">");
424                         do_iconbar();
425                         wprintf("</div>\n");
426                 }
427                 if (do_room_banner == 1) {
428                         wprintf("<div id=\"banner\">\n");
429                         embed_room_banner(NULL, navbar_default);
430                         wprintf("</div>\n");
431                 }
432         }
433
434         if (do_room_banner == 1) {
435                 wprintf("<div id=\"content\">\n");
436
437                 if (strlen(WC->ImportantMessage) > 0) {
438                         wprintf("<div id=\"fix_scrollbar_bug\">\n");
439                         do_template("beginbox_nt");
440                         wprintf("<SPAN CLASS=\"errormsg\">"
441                                 "%s</SPAN><br />\n", WC->ImportantMessage);
442                         do_template("endbox");
443                         wprintf("</div>\n");
444                         safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
445                 }
446
447         }
448 }
449
450
451 /*
452  * Generic function to do an HTTP redirect.  Easy and fun.
453  */
454 void http_redirect(char *whichpage) {
455         wprintf("HTTP/1.0 302 Moved Temporarily\n");
456         wprintf("Location: %s\r\n", whichpage);
457         wprintf("URI: %s\r\n", whichpage);
458         wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
459         wprintf("<html><body>\n");
460         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
461                 whichpage);
462         wprintf("</body></html>\n");
463 }
464
465
466
467 void check_for_instant_messages()
468 {
469         char buf[SIZ];
470
471         serv_puts("NOOP");
472         serv_getln(buf, sizeof buf);
473         if (buf[3] == '*') WC->HaveInstantMessages = 1;
474 }
475
476
477
478
479 /* 
480  * Output a piece of content to the web browser
481  */
482 void http_transmit_thing(char *thing, size_t length, char *content_type,
483                          int is_static) {
484
485         output_headers(0, 0, 0, 0, 0, 0, is_static);
486
487         wprintf("Content-type: %s\r\n"
488                 "Server: %s\r\n"
489                 "Connection: close\r\n",
490                 content_type,
491                 SERVER);
492
493 #ifdef HAVE_ZLIB
494         /* If we can send the data out compressed, please do so. */
495         if (WC->gzip_ok) {
496                 char *compressed_data = NULL;
497                 uLongf compressed_len;
498
499                 compressed_len = (uLongf) ((length * 101) / 100) + 100;
500                 compressed_data = malloc(compressed_len);
501
502                 if (compress_gzip((Bytef *) compressed_data,
503                                   &compressed_len,
504                                   (Bytef *) thing,
505                                   (uLongf) length, Z_BEST_SPEED) == Z_OK) {
506                         wprintf("Content-encoding: gzip\r\n"
507                                 "Content-length: %ld\r\n"
508                                 "\r\n",
509                                 (long) compressed_len
510                         );
511                         client_write(compressed_data, (size_t)compressed_len);
512                         free(compressed_data);
513                         return;
514                 }
515         }
516 #endif
517
518         /* No compression ... just send it out as-is */
519         wprintf("Content-length: %ld\r\n"
520                 "\r\n",
521                 (long) length
522         );
523         client_write(thing, (size_t)length);
524 }
525
526
527
528
529 void output_static(char *what)
530 {
531         char buf[256];
532         FILE *fp;
533         struct stat statbuf;
534         off_t bytes;
535         char *bigbuffer;
536         char content_type[128];
537
538         sprintf(buf, "static/%s", what);
539         fp = fopen(buf, "rb");
540         if (fp == NULL) {
541                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
542                 wprintf("Content-Type: text/plain\r\n");
543                 wprintf("\r\n");
544                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
545         } else {
546                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
547                         safestrncpy(content_type, "image/gif", sizeof content_type);
548                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
549                         safestrncpy(content_type, "text/plain", sizeof content_type);
550                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
551                         safestrncpy(content_type, "text/css", sizeof content_type);
552                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
553                         safestrncpy(content_type, "image/jpeg", sizeof content_type);
554                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
555                         safestrncpy(content_type, "image/png", sizeof content_type);
556                 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
557                         safestrncpy(content_type, "image/x-icon", sizeof content_type);
558                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
559                         safestrncpy(content_type, "text/html", sizeof content_type);
560                 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
561                         safestrncpy(content_type, "text/html", sizeof content_type);
562                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
563                         safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
564                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
565                         safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
566                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
567                         safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
568                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
569                         safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
570                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
571                         safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
572                 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
573                         safestrncpy(content_type, "text/javascript", sizeof content_type);
574                 else
575                         safestrncpy(content_type, "application/octet-stream", sizeof content_type);
576
577                 fstat(fileno(fp), &statbuf);
578                 bytes = statbuf.st_size;
579                 bigbuffer = malloc(bytes + 2);
580                 fread(bigbuffer, bytes, 1, fp);
581                 fclose(fp);
582
583                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
584                 free(bigbuffer);
585         }
586         if (!strcasecmp(bstr("force_close_session"), "yes")) {
587                 end_webcit_session();
588         }
589 }
590
591
592 /*
593  * When the browser requests an image file from the Citadel server,
594  * this function is called to transmit it.
595  */
596 void output_image()
597 {
598         char buf[SIZ];
599         char *xferbuf = NULL;
600         off_t bytes;
601
602         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
603         serv_getln(buf, sizeof buf);
604         if (buf[0] == '2') {
605                 bytes = extract_long(&buf[4], 0);
606                 xferbuf = malloc(bytes + 2);
607
608                 /* Read it from the server */
609                 read_server_binary(xferbuf, bytes);
610                 serv_puts("CLOS");
611                 serv_getln(buf, sizeof buf);
612
613                 /* Write it to the browser */
614                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
615                 free(xferbuf);
616
617         } else {
618
619                 /* Instead of an ugly 404, send a 1x1 transparent GIF
620                  * when there's no such image on the server.
621                  */
622                 output_static("blank.gif");
623
624                 /*
625                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
626                 output_headers(0, 0, 0, 0, 0, 0, 0);
627                 wprintf("Content-Type: text/plain\r\n"
628                         "\r\n"
629                         "Error retrieving image: %s\n",
630                         &buf[4]
631                 );
632                 */
633
634         }
635
636
637
638 }
639
640 /*
641  */
642 void output_mimepart()
643 {
644         char buf[SIZ];
645         off_t bytes;
646         char content_type[SIZ];
647         char *content = NULL;
648         
649         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
650         serv_getln(buf, sizeof buf);
651         if (buf[0] == '2') {
652                 bytes = extract_long(&buf[4], 0);
653                 content = malloc(bytes + 2);
654                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
655                 output_headers(0, 0, 0, 0, 0, 0, 0);
656                 read_server_binary(content, bytes);
657                 serv_puts("CLOS");
658                 serv_getln(buf, sizeof buf);
659                 http_transmit_thing(content, bytes, content_type, 0);
660                 free(content);
661         } else {
662                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
663                 output_headers(0, 0, 0, 0, 0, 0, 0);
664                 wprintf("Content-Type: text/plain\r\n");
665                 wprintf("\r\n");
666                 wprintf("Error retrieving part: %s\n", &buf[4]);
667         }
668
669 }
670
671
672 /*
673  */
674 char *load_mimepart(long msgnum, char *partnum)
675 {
676         char buf[SIZ];
677         off_t bytes;
678         char content_type[SIZ];
679         char *content;
680         
681         serv_printf("OPNA %ld|%s", msgnum, partnum);
682         serv_getln(buf, sizeof buf);
683         if (buf[0] == '2') {
684                 bytes = extract_long(&buf[4], 0);
685                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
686
687                 content = malloc(bytes + 2);
688                 read_server_binary(content, bytes);
689
690                 serv_puts("CLOS");
691                 serv_getln(buf, sizeof buf);
692                 content[bytes] = 0;     /* null terminate for good measure */
693                 return(content);
694         }
695         else {
696                 return(NULL);
697         }
698
699 }
700
701
702 /*
703  * Convenience functions to display a page containing only a string
704  */
705 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
706 {
707         wprintf("HTTP/1.0 200 OK\n");
708         output_headers(1, 1, 2, 0, 0, 0, 0);
709         wprintf("<div id=\"banner\">\n");
710         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
711         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
712         wprintf("</TD></TR></TABLE>\n");
713         wprintf("</div>\n<div id=\"content\">\n");
714         escputs(messagetext);
715
716         wprintf("<hr />\n");
717         wDumpContent(1);
718 }
719
720
721 /*
722  * Display a blank page.
723  */
724 void blank_page(void) {
725         output_headers(1, 1, 0, 0, 1, 0, 0);
726         wDumpContent(2);
727 }
728
729
730 /*
731  * A template has been requested
732  */
733 void url_do_template(void) {
734         do_template(bstr("template"));
735 }
736
737
738
739 /*
740  * Offer to make any page the user's "start page."
741  */
742 void offer_start_page(void) {
743         wprintf("<A HREF=\"/change_start_page?startpage=");
744         urlescputs(WC->this_page);
745         wprintf("\">"
746                 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
747                 "</A>"
748         );
749 }
750
751
752 /* 
753  * Change the user's start page
754  */
755 void change_start_page(void) {
756
757         if (bstr("startpage") == NULL) {
758                 safestrncpy(WC->ImportantMessage,
759                         "startpage set to null",
760                         sizeof WC->ImportantMessage);
761                 display_main_menu();
762                 return;
763         }
764
765         set_preference("startpage", bstr("startpage"), 1);
766
767         output_headers(1, 1, 0, 0, 0, 0, 0);
768         do_template("newstartpage");
769         wDumpContent(1);
770 }
771
772
773
774
775 void display_success(char *successmessage)
776 {
777         convenience_page("007700", "OK", successmessage);
778 }
779
780
781
782 void extract_action(char *actbuf, char *cmdbuf)
783 {
784         int i;
785
786         strcpy(actbuf, cmdbuf);
787
788         /*
789          * First strip out the http method
790          */
791         remove_token(actbuf, 0, ' ');
792         if (actbuf[0] == ' ') strcpy(actbuf, &actbuf[1]);
793         if (actbuf[0] == '/') strcpy(actbuf, &actbuf[1]);
794
795         /*
796          * Now kill invalid (for webcit) characters
797          */
798         for (i = 0; i < strlen(actbuf); ++i) {
799                 if (actbuf[i] == ' ') {
800                         actbuf[i] = 0;
801                         i = 0;
802                 }
803                 if (actbuf[i] == '/') {
804                         actbuf[i] = 0;
805                         i = 0;
806                 }
807                 if (actbuf[i] == '?') {
808                         actbuf[i] = 0;
809                         i = 0;
810                 }
811                 if (actbuf[i] == '&') {
812                         actbuf[i] = 0;
813                         i = 0;
814                 }
815         }
816 }
817
818
819 void upload_handler(char *name, char *filename, char *partnum, char *disp,
820                         void *content, char *cbtype, char *cbcharset,
821                         size_t length, char *encoding, void *userdata)
822 {
823         struct urlcontent *u;
824
825         /* Form fields */
826         if ( (length > 0) && (strlen(cbtype) == 0) ) {
827                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
828                 u->next = WC->urlstrings;
829                 WC->urlstrings = u;
830                 safestrncpy(u->url_key, name, sizeof(u->url_key));
831                 u->url_data = malloc(length + 1);
832                 memcpy(u->url_data, content, length);
833                 u->url_data[length] = 0;
834         }
835
836         /* Uploaded files */
837         if ( (length > 0) && (strlen(cbtype) > 0) ) {
838                 WC->upload = malloc(length);
839                 if (WC->upload != NULL) {
840                         WC->upload_length = length;
841                         safestrncpy(WC->upload_filename, filename,
842                                         sizeof(WC->upload_filename));
843                         safestrncpy(WC->upload_content_type, cbtype,
844                                         sizeof(WC->upload_content_type));
845                         memcpy(WC->upload, content, length);
846                 }
847                 else {
848                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
849                 }
850         }
851
852 }
853
854
855
856
857 /*
858  * Entry point for WebCit transaction
859  */
860 void session_loop(struct httprequest *req)
861 {
862         char cmd[SIZ];
863         char method[SIZ];
864         char action[SIZ];
865         char buf[SIZ];
866         int a, b;
867         int ContentLength = 0;
868         int BytesRead = 0;
869         char ContentType[512];
870         char *content = NULL;
871         char *content_end = NULL;
872         struct httprequest *hptr;
873         char browser_host[SIZ];
874         char user_agent[SIZ];
875         int body_start = 0;
876
877         /* We stuff these with the values coming from the client cookies,
878          * so we can use them to reconnect a timed out session if we have to.
879          */
880         char c_username[SIZ];
881         char c_password[SIZ];
882         char c_roomname[SIZ];
883         char c_httpauth_string[SIZ];
884         char c_httpauth_user[SIZ];
885         char c_httpauth_pass[SIZ];
886         char cookie[SIZ];
887
888         safestrncpy(c_username, "", sizeof c_username);
889         safestrncpy(c_password, "", sizeof c_password);
890         safestrncpy(c_roomname, "", sizeof c_roomname);
891         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
892         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
893         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
894
895         WC->upload_length = 0;
896         WC->upload = NULL;
897         WC->vars = NULL;
898
899         WC->is_wap = 0;
900
901         hptr = req;
902         if (hptr == NULL) return;
903
904         safestrncpy(cmd, hptr->line, sizeof cmd);
905         hptr = hptr->next;
906         extract_token(method, cmd, 0, ' ', sizeof method);
907         extract_action(action, cmd);
908
909         while (hptr != NULL) {
910                 safestrncpy(buf, hptr->line, sizeof buf);
911                 hptr = hptr->next;
912
913                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
914                         safestrncpy(cookie, &buf[15], sizeof cookie);
915                         cookie_to_stuff(cookie, NULL,
916                                         c_username, sizeof c_username,
917                                         c_password, sizeof c_password,
918                                         c_roomname, sizeof c_roomname);
919                 }
920                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
921                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
922                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
923                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
924                 }
925                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
926                         ContentLength = atoi(&buf[16]);
927                 }
928                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
929                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
930                 }
931                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
932                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
933                 }
934                 else if (!strncasecmp(buf, "Host: ", 6)) {
935                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
936                 }
937                 /* Only WAP gateways explicitly name this content-type */
938                 else if (strstr(buf, "text/vnd.wap.wml")) {
939                         WC->is_wap = 1;
940                 }
941         }
942
943         if (ContentLength > 0) {
944                 content = malloc(ContentLength + SIZ);
945                 memset(content, 0, ContentLength + SIZ);
946                 sprintf(content, "Content-type: %s\n"
947                                 "Content-length: %d\n\n",
948                                 ContentType, ContentLength);
949                 body_start = strlen(content);
950
951                 /* Read the entire input data at once. */
952                 client_read(WC->http_sock, &content[BytesRead+body_start],
953                         ContentLength);
954
955                 if (!strncasecmp(ContentType,
956                               "application/x-www-form-urlencoded", 33)) {
957                         addurls(&content[body_start]);
958                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
959                         content_end = content + ContentLength + body_start;
960                         mime_parser(content, content_end, *upload_handler,
961                                         NULL, NULL, NULL, 0);
962                 }
963         } else {
964                 content = NULL;
965         }
966
967         /* make a note of where we are in case the user wants to save it */
968         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
969         remove_token(WC->this_page, 2, ' ');
970         remove_token(WC->this_page, 0, ' ');
971
972         /* If there are variables in the URL, we must grab them now */
973         for (a = 0; a < strlen(cmd); ++a) {
974                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
975                         for (b = a; b < strlen(cmd); ++b)
976                                 if (isspace(cmd[b]))
977                                         cmd[b] = 0;
978                         addurls(&cmd[a + 1]);
979                         cmd[a] = 0;
980                 }
981         }
982
983         /* Static content can be sent without connecting to Citadel. */
984         if (!strcasecmp(action, "static")) {
985                 safestrncpy(buf, &cmd[12], sizeof buf);
986                 for (a = 0; a < strlen(buf); ++a)
987                         if (isspace(buf[a]))
988                                 buf[a] = 0;
989                 output_static(buf);
990                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
991         }
992
993         /*
994          * If we're not connected to a Citadel server, try to hook up the
995          * connection now.
996          */
997         if (!WC->connected) {
998                 if (!strcasecmp(ctdlhost, "uds")) {
999                         /* unix domain socket */
1000                         sprintf(buf, "%s/citadel.socket", ctdlport);
1001                         WC->serv_sock = uds_connectsock(buf);
1002                 }
1003                 else {
1004                         /* tcp socket */
1005                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1006                 }
1007
1008                 if (WC->serv_sock < 0) {
1009                         do_logout();
1010                         goto SKIP_ALL_THIS_CRAP;
1011                 }
1012                 else {
1013                         WC->connected = 1;
1014                         serv_getln(buf, sizeof buf);    /* get the server welcome message */
1015                         locate_host(browser_host, WC->http_sock);
1016                         get_serv_info(browser_host, user_agent);
1017                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1018                                 wprintf("You are connected to a Citadel "
1019                                         "server running Citadel %d.%02d;\nin "
1020                                         "order to run this version of WebCit "
1021                                         "you must also have Citadel %d.%02d or"
1022                                         " newer.\n\n\n",
1023                                                 serv_info.serv_rev_level / 100,
1024                                                 serv_info.serv_rev_level % 100,
1025                                                 MINIMUM_CIT_VERSION / 100,
1026                                                 MINIMUM_CIT_VERSION % 100
1027                                         );
1028                                 end_webcit_session();
1029                                 goto SKIP_ALL_THIS_CRAP;
1030                         }
1031                 }
1032         }
1033
1034         /*
1035          * Functions which can be performed without logging in
1036          */
1037         if (!strcasecmp(action, "listsub")) {
1038                 do_listsub();
1039                 goto SKIP_ALL_THIS_CRAP;
1040         }
1041 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1042         if (!strcasecmp(action, "freebusy")) {
1043                 do_freebusy(cmd);
1044                 goto SKIP_ALL_THIS_CRAP;
1045         }
1046 #endif
1047
1048         /*
1049          * If we're not logged in, but we have HTTP Authentication data,
1050          * try logging in to Citadel using that.
1051          */
1052         if ((!WC->logged_in)
1053            && (strlen(c_httpauth_user) > 0)
1054            && (strlen(c_httpauth_pass) > 0)) {
1055                 serv_printf("USER %s", c_httpauth_user);
1056                 serv_getln(buf, sizeof buf);
1057                 if (buf[0] == '3') {
1058                         serv_printf("PASS %s", c_httpauth_pass);
1059                         serv_getln(buf, sizeof buf);
1060                         if (buf[0] == '2') {
1061                                 become_logged_in(c_httpauth_user,
1062                                                 c_httpauth_pass, buf);
1063                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1064                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1065                         }
1066                 }
1067         }
1068
1069         /* 
1070          * The GroupDAV stuff relies on HTTP authentication instead of
1071          * our session's authentication.
1072          */
1073         if (!strncasecmp(action, "groupdav", 8)) {
1074                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1075                         ContentLength, content+body_start);
1076                 if (!WC->logged_in) {
1077                         WC->killthis = 1;       /* If not logged in, don't */
1078                 }                               /* keep the session active */
1079                 goto SKIP_ALL_THIS_CRAP;
1080         }
1081
1082
1083         /*
1084          * Automatically send requests with any method other than GET or
1085          * POST to the GroupDAV code as well.
1086          */
1087         if ((strcasecmp(method, "GET")) && (strcasecmp(method, "POST"))) {
1088                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1089                         ContentLength, content+body_start);
1090                 if (!WC->logged_in) {
1091                         WC->killthis = 1;       /* If not logged in, don't */
1092                 }                               /* keep the session active */
1093                 goto SKIP_ALL_THIS_CRAP;
1094         }
1095
1096         /*
1097          * If we're not logged in, but we have username and password cookies
1098          * supplied by the browser, try using them to log in.
1099          */
1100         if ((!WC->logged_in)
1101            && (strlen(c_username) > 0)
1102            && (strlen(c_password) > 0)) {
1103                 serv_printf("USER %s", c_username);
1104                 serv_getln(buf, sizeof buf);
1105                 if (buf[0] == '3') {
1106                         serv_printf("PASS %s", c_password);
1107                         serv_getln(buf, sizeof buf);
1108                         if (buf[0] == '2') {
1109                                 become_logged_in(c_username, c_password, buf);
1110                         }
1111                 }
1112         }
1113         /*
1114          * If we don't have a current room, but a cookie specifying the
1115          * current room is supplied, make an effort to go there.
1116          */
1117         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1118                 serv_printf("GOTO %s", c_roomname);
1119                 serv_getln(buf, sizeof buf);
1120                 if (buf[0] == '2') {
1121                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1122                 }
1123         }
1124
1125         /*
1126          * If there are instant messages waiting, retrieve them for display.
1127          */
1128         check_for_instant_messages();
1129
1130         if (!strcasecmp(action, "image")) {
1131                 output_image();
1132
1133         /*
1134          * All functions handled below this point ... make sure we log in
1135          * before doing anything else!
1136          */
1137         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1138                 do_login();
1139         } else if (!WC->logged_in) {
1140                 display_login(NULL);
1141         }
1142
1143         /*
1144          * Various commands...
1145          */
1146
1147         else if (!strcasecmp(action, "do_welcome")) {
1148                 do_welcome();
1149         } else if (!strcasecmp(action, "blank")) {
1150                 blank_page();
1151         } else if (!strcasecmp(action, "do_template")) {
1152                 url_do_template();
1153         } else if (!strcasecmp(action, "display_aide_menu")) {
1154                 display_aide_menu();
1155         } else if (!strcasecmp(action, "display_main_menu")) {
1156                 display_main_menu();
1157         } else if (!strcasecmp(action, "whobbs")) {
1158                 whobbs();
1159         } else if (!strcasecmp(action, "knrooms")) {
1160                 knrooms();
1161         } else if (!strcasecmp(action, "gotonext")) {
1162                 slrp_highest();
1163                 gotonext();
1164         } else if (!strcasecmp(action, "skip")) {
1165                 gotonext();
1166         } else if (!strcasecmp(action, "ungoto")) {
1167                 ungoto();
1168         } else if (!strcasecmp(action, "dotgoto")) {
1169                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1170                         slrp_highest();
1171                 }
1172                 smart_goto(bstr("room"));
1173         } else if (!strcasecmp(action, "dotskip")) {
1174                 smart_goto(bstr("room"));
1175         } else if (!strcasecmp(action, "termquit")) {
1176                 do_logout();
1177         } else if (!strcasecmp(action, "readnew")) {
1178                 readloop("readnew");
1179         } else if (!strcasecmp(action, "readold")) {
1180                 readloop("readold");
1181         } else if (!strcasecmp(action, "readfwd")) {
1182                 readloop("readfwd");
1183         } else if (!strcasecmp(action, "headers")) {
1184                 readloop("headers");
1185         } else if (!strcasecmp(action, "display_enter")) {
1186                 display_enter();
1187         } else if (!strcasecmp(action, "post")) {
1188                 post_message();
1189         } else if (!strcasecmp(action, "move_msg")) {
1190                 move_msg();
1191         } else if (!strcasecmp(action, "delete_msg")) {
1192                 delete_msg();
1193         } else if (!strcasecmp(action, "userlist")) {
1194                 userlist();
1195         } else if (!strcasecmp(action, "showuser")) {
1196                 showuser();
1197         } else if (!strcasecmp(action, "display_page")) {
1198                 display_page();
1199         } else if (!strcasecmp(action, "page_user")) {
1200                 page_user();
1201         } else if (!strcasecmp(action, "chat")) {
1202                 do_chat();
1203         } else if (!strcasecmp(action, "display_private")) {
1204                 display_private("", 0);
1205         } else if (!strcasecmp(action, "goto_private")) {
1206                 goto_private();
1207         } else if (!strcasecmp(action, "zapped_list")) {
1208                 zapped_list();
1209         } else if (!strcasecmp(action, "display_zap")) {
1210                 display_zap();
1211         } else if (!strcasecmp(action, "zap")) {
1212                 zap();
1213         } else if (!strcasecmp(action, "display_entroom")) {
1214                 display_entroom();
1215         } else if (!strcasecmp(action, "entroom")) {
1216                 entroom();
1217         } else if (!strcasecmp(action, "display_whok")) {
1218                 display_whok();
1219         } else if (!strcasecmp(action, "do_invt_kick")) {
1220                 do_invt_kick();
1221         } else if (!strcasecmp(action, "display_editroom")) {
1222                 display_editroom();
1223         } else if (!strcasecmp(action, "netedit")) {
1224                 netedit();
1225         } else if (!strcasecmp(action, "editroom")) {
1226                 editroom();
1227         } else if (!strcasecmp(action, "display_editinfo")) {
1228                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1229         } else if (!strcasecmp(action, "editinfo")) {
1230                 save_edit("Room info", "EINF 1", 1);
1231         } else if (!strcasecmp(action, "display_editbio")) {
1232                 sprintf(buf, "RBIO %s", WC->wc_username);
1233                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1234         } else if (!strcasecmp(action, "editbio")) {
1235                 save_edit("Your bio", "EBIO", 0);
1236         } else if (!strcasecmp(action, "confirm_move_msg")) {
1237                 confirm_move_msg();
1238         } else if (!strcasecmp(action, "delete_room")) {
1239                 delete_room();
1240         } else if (!strcasecmp(action, "validate")) {
1241                 validate();
1242         } else if (!strcasecmp(action, "display_editpic")) {
1243                 display_graphics_upload("your photo",
1244                                         "UIMG 0|_userpic_",
1245                                         "/editpic");
1246         } else if (!strcasecmp(action, "editpic")) {
1247                 do_graphics_upload("UIMG 1|_userpic_");
1248         } else if (!strcasecmp(action, "display_editroompic")) {
1249                 display_graphics_upload("the icon for this room",
1250                                         "UIMG 0|_roompic_",
1251                                         "/editroompic");
1252         } else if (!strcasecmp(action, "editroompic")) {
1253                 do_graphics_upload("UIMG 1|_roompic_");
1254         } else if (!strcasecmp(action, "delete_floor")) {
1255                 delete_floor();
1256         } else if (!strcasecmp(action, "rename_floor")) {
1257                 rename_floor();
1258         } else if (!strcasecmp(action, "create_floor")) {
1259                 create_floor();
1260         } else if (!strcasecmp(action, "display_editfloorpic")) {
1261                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1262                         bstr("which_floor"));
1263                 display_graphics_upload("the icon for this floor",
1264                                         buf,
1265                                         "/editfloorpic");
1266         } else if (!strcasecmp(action, "editfloorpic")) {
1267                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1268                         bstr("which_floor"));
1269                 do_graphics_upload(buf);
1270         } else if (!strcasecmp(action, "display_reg")) {
1271                 display_reg(0);
1272         } else if (!strcasecmp(action, "display_changepw")) {
1273                 display_changepw();
1274         } else if (!strcasecmp(action, "changepw")) {
1275                 changepw();
1276         } else if (!strcasecmp(action, "display_edit_node")) {
1277                 display_edit_node();
1278         } else if (!strcasecmp(action, "edit_node")) {
1279                 edit_node();
1280         } else if (!strcasecmp(action, "display_netconf")) {
1281                 display_netconf();
1282         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1283                 display_confirm_delete_node();
1284         } else if (!strcasecmp(action, "delete_node")) {
1285                 delete_node();
1286         } else if (!strcasecmp(action, "display_add_node")) {
1287                 display_add_node();
1288         } else if (!strcasecmp(action, "add_node")) {
1289                 add_node();
1290         } else if (!strcasecmp(action, "terminate_session")) {
1291                 slrp_highest();
1292                 terminate_session();
1293         } else if (!strcasecmp(action, "edit_me")) {
1294                 edit_me();
1295         } else if (!strcasecmp(action, "display_siteconfig")) {
1296                 display_siteconfig();
1297         } else if (!strcasecmp(action, "chat_recv")) {
1298                 chat_recv();
1299         } else if (!strcasecmp(action, "chat_send")) {
1300                 chat_send();
1301         } else if (!strcasecmp(action, "siteconfig")) {
1302                 siteconfig();
1303         } else if (!strcasecmp(action, "display_generic")) {
1304                 display_generic();
1305         } else if (!strcasecmp(action, "do_generic")) {
1306                 do_generic();
1307         } else if (!strcasecmp(action, "display_menubar")) {
1308                 display_menubar(1);
1309         } else if (!strcasecmp(action, "output_mimepart")) {
1310                 output_mimepart();
1311         } else if (!strcasecmp(action, "edit_vcard")) {
1312                 edit_vcard();
1313         } else if (!strcasecmp(action, "submit_vcard")) {
1314                 submit_vcard();
1315         } else if (!strcasecmp(action, "select_user_to_edit")) {
1316                 select_user_to_edit(NULL, NULL);
1317         } else if (!strcasecmp(action, "display_edituser")) {
1318                 display_edituser(NULL, 0);
1319         } else if (!strcasecmp(action, "edituser")) {
1320                 edituser();
1321         } else if (!strcasecmp(action, "create_user")) {
1322                 create_user();
1323         } else if (!strcasecmp(action, "changeview")) {
1324                 change_view();
1325         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1326                 do_stuff_to_msgs();
1327         } else if (!strcasecmp(action, "change_start_page")) {
1328                 change_start_page();
1329         } else if (!strcasecmp(action, "display_floorconfig")) {
1330                 display_floorconfig(NULL);
1331         } else if (!strcasecmp(action, "toggle_self_service")) {
1332                 toggle_self_service();
1333 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1334         } else if (!strcasecmp(action, "display_edit_task")) {
1335                 display_edit_task();
1336         } else if (!strcasecmp(action, "save_task")) {
1337                 save_task();
1338         } else if (!strcasecmp(action, "display_edit_event")) {
1339                 display_edit_event();
1340         } else if (!strcasecmp(action, "save_event")) {
1341                 save_event();
1342         } else if (!strcasecmp(action, "respond_to_request")) {
1343                 respond_to_request();
1344         } else if (!strcasecmp(action, "handle_rsvp")) {
1345                 handle_rsvp();
1346 #endif
1347         } else if (!strcasecmp(action, "summary")) {
1348                 summary();
1349         } else if (!strcasecmp(action, "iconbar")) {
1350                 do_iconbar();
1351         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1352                 display_customize_iconbar();
1353         } else if (!strcasecmp(action, "commit_iconbar")) {
1354                 commit_iconbar();
1355         } else if (!strcasecmp(action, "set_room_policy")) {
1356                 set_room_policy();
1357         } else if (!strcasecmp(action, "display_inetconf")) {
1358                 display_inetconf();
1359         } else if (!strcasecmp(action, "save_inetconf")) {
1360                 save_inetconf();
1361         } else if (!strcasecmp(action, "setup_wizard")) {
1362                 do_setup_wizard();
1363         } else if (!strcasecmp(action, "display_preferences")) {
1364                 display_preferences();
1365         } else if (!strcasecmp(action, "set_preferences")) {
1366                 set_preferences();
1367         } else if (!strcasecmp(action, "diagnostics")) {
1368                 output_headers(1, 1, 1, 0, 0, 0, 0);
1369
1370                 wprintf("You're in session %d<hr />\n", WC->wc_session);
1371                 wprintf("Command: <br /><PRE>\n");
1372                 escputs(cmd);
1373                 wprintf("</PRE><hr />\n");
1374                 wprintf("Variables: <br /><PRE>\n");
1375                 dump_vars();
1376                 wprintf("</PRE><hr />\n");
1377                 wDumpContent(1);
1378         }
1379         /* When all else fais, display the main menu. */
1380         else {
1381                 display_main_menu();
1382         }
1383
1384 SKIP_ALL_THIS_CRAP:
1385         fflush(stdout);
1386         if (content != NULL) {
1387                 free(content);
1388                 content = NULL;
1389         }
1390         free_urls();
1391         if (WC->upload_length > 0) {
1392                 free(WC->upload);
1393                 WC->upload_length = 0;
1394         }
1395
1396 }