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