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