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