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