]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Compress output_image() graphics
[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 void output_static(char *what)
451 {
452         char buf[4096];
453         FILE *fp;
454         struct stat statbuf;
455         off_t bytes;
456         char *bigbuffer;
457
458         sprintf(buf, "static/%s", what);
459         fp = fopen(buf, "rb");
460         if (fp == NULL) {
461                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
462                 wprintf("Content-Type: text/plain\n");
463                 wprintf("\n");
464                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
465         } else {
466                 output_headers(0);
467
468                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
469                         wprintf("Content-type: image/gif\n");
470                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
471                         wprintf("Content-type: text/plain\n");
472                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
473                         wprintf("Content-type: text/css\n");
474                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
475                         wprintf("Content-type: image/jpeg\n");
476                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
477                         wprintf("Content-type: image/png\n");
478                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
479                         wprintf("Content-type: text/html\n");
480                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
481                         wprintf("Content-type: text/vnd.wap.wml\n");
482                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
483                         wprintf("Content-type: text/vnd.wap.wmlscript\n");
484                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
485                         wprintf("Content-type: application/vnd.wap.wmlc\n");
486                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
487                         wprintf("Content-type: application/vnd.wap.wmlscriptc\n");
488                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
489                         wprintf("Content-type: image/vnd.wap.wbmp\n");
490                 else
491                         wprintf("Content-type: application/octet-stream\n");
492
493                 fstat(fileno(fp), &statbuf);
494                 bytes = statbuf.st_size;
495                 lprintf(3, "Static: %s, %ld bytes\n", what, bytes);
496                 wprintf("Content-length: %ld\n", (long) bytes);
497                 wprintf("\n");
498                 bigbuffer = malloc(bytes);
499                 fread(bigbuffer, bytes, 1, fp);
500                 fclose(fp);
501                 http_write(WC->http_sock, bigbuffer, bytes);
502                 free(bigbuffer);
503         }
504         if (!strcasecmp(bstr("force_close_session"), "yes")) {
505                 end_webcit_session();
506         }
507 }
508
509
510 /*
511  * When the browser requests an image file from the Citadel server,
512  * this function is called to transmit it.
513  */
514 void output_image()
515 {
516         char buf[SIZ];
517         char *xferbuf = NULL;
518         char *compressed_xferbuf = NULL;
519         off_t bytes;
520         off_t thisblock;
521         off_t accomplished = 0L;
522 #ifdef WITH_ZLIB
523         uLongf compressed_size = 0;
524 #endif
525         int do_compress = 0;
526
527 #ifdef WITH_ZLIB
528         do_compress = WC->gzcompressed;
529 #endif
530
531         lprintf(5, "output_image() called; do_compress=%d\n", do_compress);
532         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
533         serv_gets(buf);
534         if (buf[0] == '2') {
535                 bytes = extract_long(&buf[4], 0);
536                 xferbuf = malloc(bytes);
537
538                 /* Read it from the server */
539                 while (bytes > (off_t) 0) {
540                         thisblock = (off_t) sizeof(xferbuf);
541                         if (thisblock > bytes) {
542                                 thisblock = bytes;
543                         }
544                         serv_printf("READ %ld|%ld", accomplished, thisblock);
545                         serv_gets(buf);
546                         if (buf[0] == '6') {
547                                 thisblock = extract_long(&buf[4], 0);
548                                 serv_read(&xferbuf[accomplished],
549                                         (int) thisblock);
550                         }
551                         else {
552                                 memset(&xferbuf[accomplished], 0, thisblock);
553                         }
554                         bytes = bytes - thisblock;
555                         accomplished += thisblock;
556                 }
557                 serv_puts("CLOS");
558                 serv_gets(buf);
559
560 #ifdef WITH_ZLIB
561                 if (do_compress) {
562                         /* Write it to the browser -- compressed */
563                         compressed_size = (uLongf)
564                                         ((accomplished * 101) / 100) + 4096;
565                         compressed_xferbuf = malloc(compressed_size);
566                         lprintf(9, "uncompressed size: %d\n", accomplished);
567                         compress2(compressed_xferbuf, &compressed_size,
568                                 xferbuf, (uLong)accomplished, 9);
569                         lprintf(9, "  compressed size: %d\n", compressed_size);
570                         output_headers(0);
571                         wprintf("Content-type: image/gif\n"
572                                 "Content-length: %ld\n"
573                                 "Content-Encoding: gzip\n"
574                                 "\n",
575                                 (long) compressed_size
576                         );
577                         http_write(WC->http_sock, compressed_xferbuf,
578                                                         compressed_size);
579                 }
580 #endif
581                 if (!do_compress) {
582                         /* Write it to the browser -- no compression */
583                         output_headers(0);
584                         wprintf("Content-type: image/gif\n"
585                         wprintf("Content-length: %ld\n"
586                         wprintf("\n",
587                         (long) accomplished
588                         );
589                         http_write(WC->http_sock, xferbuf, accomplished);
590                 }
591
592         } else {
593                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
594                 output_headers(0);
595                 wprintf("Content-Type: text/plain\n");
596                 wprintf("\n");
597                 wprintf("Error retrieving image: %s\n", &buf[4]);
598         }
599
600         if (xferbuf) {
601                 free(xferbuf);
602         }
603         if (compressed_xferbuf) {
604                 free(compressed_xferbuf);
605         }
606
607 }
608
609 /*
610  */
611 void output_mimepart()
612 {
613         char buf[SIZ];
614         char xferbuf[4096];
615         off_t bytes;
616         off_t thisblock;
617         off_t accomplished = 0L;
618         char content_type[SIZ];
619         
620         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
621         serv_gets(buf);
622         if (buf[0] == '2') {
623                 bytes = extract_long(&buf[4], 0);
624                 extract(content_type, &buf[4], 3);
625                 output_headers(0);
626                 wprintf("Content-type: %s\n", content_type);
627                 wprintf("Content-length: %ld\n", (long) bytes);
628                 wprintf("\n");
629
630                 while (bytes > (off_t) 0) {
631                         thisblock = (off_t) sizeof(xferbuf);
632                         if (thisblock > bytes) {
633                                 thisblock = bytes;
634                         }
635                         serv_printf("READ %ld|%ld", accomplished, thisblock);
636                         serv_gets(buf);
637                         if (buf[0] == '6') {
638                                 thisblock = extract_long(&buf[4], 0);
639                                 serv_read(xferbuf, (int) thisblock);
640                         }
641                         else {
642                                 memset(xferbuf, 0, thisblock);
643                         }
644                         http_write(WC->http_sock, xferbuf, thisblock);
645                         bytes = bytes - thisblock;
646                         accomplished = accomplished + thisblock;
647                 }
648                 serv_puts("CLOS");
649                 serv_gets(buf);
650         } else {
651                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
652                 output_headers(0);
653                 wprintf("Content-Type: text/plain\n");
654                 wprintf("\n");
655                 wprintf("Error retrieving part: %s\n", &buf[4]);
656         }
657
658 }
659
660
661 /*
662  */
663 char *load_mimepart(long msgnum, char *partnum)
664 {
665         char buf[SIZ];
666         off_t bytes;
667         off_t thisblock;
668         off_t accomplished = 0L;
669         char content_type[SIZ];
670         char *content;
671         
672         serv_printf("OPNA %ld|%s", msgnum, partnum);
673         serv_gets(buf);
674         if (buf[0] == '2') {
675                 bytes = extract_long(&buf[4], 0);
676                 extract(content_type, &buf[4], 3);
677
678                 content = malloc(bytes + 1);
679
680                 while (bytes > (off_t) 0) {
681                         thisblock = bytes;
682                         if (thisblock > 4096L) {
683                                 thisblock = 4096L;
684                         }
685                         if (thisblock > bytes) {
686                                 thisblock = bytes;
687                         }
688                         serv_printf("READ %ld|%ld", accomplished, thisblock);
689                         serv_gets(buf);
690                         if (buf[0] == '6') {
691                                 thisblock = extract_long(&buf[4], 0);
692                                 serv_read(&content[accomplished], (int) thisblock);
693                         }
694                         else {
695                                 memset(&content[accomplished], 0, thisblock);
696                         }
697                         bytes = bytes - thisblock;
698                         accomplished = accomplished + thisblock;
699                 }
700                 serv_puts("CLOS");
701                 serv_gets(buf);
702                 content[accomplished] = 0;      /* null terminate for good measure */
703                 return(content);
704         }
705         else {
706                 return(NULL);
707         }
708
709 }
710
711
712 /*
713  * Convenience functions to display a page containing only a string
714  */
715 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
716 {
717         wprintf("HTTP/1.0 200 OK\n");
718         output_headers(1);
719         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
720         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
721         wprintf("<B>%s</B>\n", titlebarmsg);
722         wprintf("</FONT></TD></TR></TABLE><BR>\n");
723         escputs(messagetext);
724
725         wprintf("<HR>\n");
726         wDumpContent(1);
727 }
728
729
730 /*
731  * Display a blank page.
732  */
733 void blank_page(void) {
734         output_headers(7);
735         wDumpContent(2);
736 }
737
738
739
740
741 /*
742  * Offer to make any page the user's "start page."
743  */
744 void offer_start_page(void) {
745         wprintf("<A HREF=\"/change_start_page?startpage=");
746         urlescputs(WC->this_page);
747         wprintf("\">"
748                 "<FONT SIZE=-2 COLOR=#AAAAAA>Make this my start page</FONT>"
749                 "</A>"
750         );
751 }
752
753
754 /* 
755  * Change the user's start page
756  */
757 void change_start_page(void) {
758
759         if (bstr("startpage") == NULL) {
760                 display_error("startpage set to null");
761                 return;
762         }
763
764         set_preference("startpage", bstr("startpage"));
765
766         output_headers(3);
767         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=000077><TR><TD>");
768         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
769         wprintf("<B>New start page</B>\n");
770         wprintf("</FONT></TD></TR></TABLE>\n");
771
772         wprintf("<CENTER>"
773                 "<font size=+2>Your start page has been changed.</font>"
774                 "<BR><BR>\n"
775                 "<I>(Note: this does not change your browser's home page. "
776                 "It changes the page you begin on when you log on to ");
777         escputs(serv_info.serv_humannode);
778         wprintf(".)</I><BR><BR>"
779                 "<a href = \"javascript:history.back()\">Back...</a>"
780                 "</CENTER>");
781
782         wDumpContent(1);
783 }
784
785
786
787
788 void display_error(char *errormessage)
789 {
790         convenience_page("770000", "Error", errormessage);
791 }
792
793 void display_success(char *successmessage)
794 {
795         convenience_page("007700", "OK", successmessage);
796 }
797
798
799
800 void extract_action(char *actbuf, char *cmdbuf)
801 {
802         int i;
803
804         strcpy(actbuf, cmdbuf);
805         if (!strncasecmp(actbuf, "GET /", 5))
806                 strcpy(actbuf, &actbuf[5]);
807         if (!strncasecmp(actbuf, "PUT /", 5))
808                 strcpy(actbuf, &actbuf[5]);
809         if (!strncasecmp(actbuf, "POST /", 6))
810                 strcpy(actbuf, &actbuf[6]);
811
812         for (i = 0; i < strlen(actbuf); ++i) {
813                 if (actbuf[i] == ' ') {
814                         actbuf[i] = 0;
815                         i = 0;
816                 }
817                 if (actbuf[i] == '/') {
818                         actbuf[i] = 0;
819                         i = 0;
820                 }
821                 if (actbuf[i] == '?') {
822                         actbuf[i] = 0;
823                         i = 0;
824                 }
825                 if (actbuf[i] == '&') {
826                         actbuf[i] = 0;
827                         i = 0;
828                 }
829         }
830 }
831
832
833 void upload_handler(char *name, char *filename, char *partnum, char *disp,
834                         void *content, char *cbtype, size_t length,
835                         char *encoding, void *userdata)
836 {
837
838         lprintf(5, "UPLOAD HANDLER CALLED\n");
839         lprintf(5, "    name = %s\n", name);
840         lprintf(5, "filename = %s\n", filename);
841         lprintf(5, "encoding = %s\n", encoding);
842         lprintf(5, "    type = %s\n", cbtype);
843         lprintf(5, "  length = %ld\n", (long)length);
844
845         if (length > 0) {
846                 WC->upload = malloc(length);
847                 if (WC->upload != NULL) {
848                         WC->upload_length = length;
849                         memcpy(WC->upload, content, length);
850                 }
851                 else {
852                         lprintf(9, "malloc() failed: %s\n",
853                                 strerror(errno));
854                 }
855         }
856 }
857
858
859
860
861 /*
862  * Entry point for WebCit transaction
863  */
864 void session_loop(struct httprequest *req, int gzip)
865 {
866         char cmd[SIZ];
867         char action[SIZ];
868         char buf[SIZ];
869         int a, b;
870         int ContentLength = 0;
871         int BytesRead;
872         char ContentType[512];
873         char *content;
874         char *content_end;
875         struct httprequest *hptr;
876         char browser_host[SIZ];
877         char user_agent[SIZ];
878
879         /* We stuff these with the values coming from the client cookies,
880          * so we can use them to reconnect a timed out session if we have to.
881          */
882         char c_host[SIZ];
883         char c_port[SIZ];
884         char c_username[SIZ];
885         char c_password[SIZ];
886         char c_roomname[SIZ];
887         char cookie[SIZ];
888
889         strcpy(c_host, defaulthost);
890         strcpy(c_port, defaultport);
891         strcpy(c_username, "");
892         strcpy(c_password, "");
893         strcpy(c_roomname, "");
894
895         WC->upload_length = 0;
896         WC->upload = NULL;
897
898         WC->is_wap = 0;
899
900         if (gzip) {
901                 WC->gzcompressed = 1;
902         }
903         else {
904                 WC->gzcompressed = 0;
905         }
906
907         hptr = req;
908         if (hptr == NULL) return;
909
910         strcpy(cmd, hptr->line);
911         hptr = hptr->next;
912         extract_action(action, cmd);
913
914         while (hptr != NULL) {
915                 strcpy(buf, hptr->line);
916                 hptr = hptr->next;
917
918                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
919                         strcpy(cookie, &buf[15]);
920                         cookie_to_stuff(cookie, NULL,
921                                       c_username, c_password, c_roomname);
922                 }
923                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
924                         ContentLength = atoi(&buf[16]);
925                 }
926                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
927                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
928                 }
929                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
930                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
931                 }
932                 else if (!strncasecmp(buf, "Host: ", 6)) {
933                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
934                 }
935                 /* Only WAP gateways explicitly name this content-type */
936                 else if (strstr(buf, "text/vnd.wap.wml")) {
937                         WC->is_wap = 1;
938                 }
939         }
940
941         if (ContentLength > 0) {
942                 lprintf(5, "Content length: %d\n", ContentLength);
943                 content = malloc(ContentLength + 1);
944                 memset(content, 0, ContentLength+1);
945                 BytesRead = 0;
946
947                 while (BytesRead < ContentLength) {
948                         a=read(WC->http_sock, &content[BytesRead],
949                                 ContentLength - BytesRead);
950                         if (a <= 0) BytesRead = ContentLength;
951                         else BytesRead += a;
952                 }
953
954                 if (!strncasecmp(ContentType,
955                               "application/x-www-form-urlencoded", 33)) {
956                         addurls(content);
957                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
958                         content_end = content + ContentLength;
959                         mime_parser(content, content_end, *upload_handler,
960                                         NULL, NULL, NULL, 0);
961                 }
962         } else {
963                 content = NULL;
964         }
965
966         /* make a note of where we are in case the user wants to save it */
967         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
968         remove_token(WC->this_page, 2, ' ');
969         remove_token(WC->this_page, 0, ' ');
970
971         /* If there are variables in the URL, we must grab them now */
972         for (a = 0; a < strlen(cmd); ++a) {
973                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
974                         for (b = a; b < strlen(cmd); ++b)
975                                 if (isspace(cmd[b]))
976                                         cmd[b] = 0;
977                         addurls(&cmd[a + 1]);
978                         cmd[a] = 0;
979                 }
980         }
981
982         /*
983          * If we're not connected to a Citadel server, try to hook up the
984          * connection now.  Preference is given to the host and port specified
985          * by browser cookies, if cookies have been supplied.
986          */
987         if (!WC->connected) {
988                 if (strlen(bstr("host")) > 0)
989                         strcpy(c_host, bstr("host"));
990                 if (strlen(bstr("port")) > 0)
991                         strcpy(c_port, bstr("port"));
992
993                 if (!strcasecmp(c_host, "uds")) {
994                         /* unix domain socket */
995                         sprintf(buf, "%s/citadel.socket", c_port);
996                         WC->serv_sock = uds_connectsock(buf);
997                 }
998                 else {
999                         /* tcp socket */
1000                         WC->serv_sock = tcp_connectsock(c_host, c_port);
1001                 }
1002
1003                 if (WC->serv_sock < 0) {
1004                         do_logout();
1005                         goto SKIP_ALL_THIS_CRAP;
1006                 }
1007                 else {
1008                         WC->connected = 1;
1009                         serv_gets(buf); /* get the server welcome message */
1010                         locate_host(browser_host, WC->http_sock);
1011                         get_serv_info(browser_host, user_agent);
1012                 }
1013         }
1014
1015         /*
1016          * Functions which can be performed without logging in
1017          */
1018         if (!strcasecmp(action, "listsub")) {
1019                 do_listsub();
1020                 goto SKIP_ALL_THIS_CRAP;
1021         }
1022
1023         check_for_express_messages();
1024
1025         /*
1026          * If we're not logged in, but we have username and password cookies
1027          * supplied by the browser, try using them to log in.
1028          */
1029         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
1030                 serv_printf("USER %s", c_username);
1031                 serv_gets(buf);
1032                 if (buf[0] == '3') {
1033                         serv_printf("PASS %s", c_password);
1034                         serv_gets(buf);
1035                         if (buf[0] == '2') {
1036                                 become_logged_in(c_username, c_password, buf);
1037                         }
1038                 }
1039         }
1040         /*
1041          * If we don't have a current room, but a cookie specifying the
1042          * current room is supplied, make an effort to go there.
1043          */
1044         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1045                 serv_printf("GOTO %s", c_roomname);
1046                 serv_gets(buf);
1047                 if (buf[0] == '2') {
1048                         strcpy(WC->wc_roomname, c_roomname);
1049                 }
1050         }
1051         if (!strcasecmp(action, "static")) {
1052                 strcpy(buf, &cmd[12]);
1053                 for (a = 0; a < strlen(buf); ++a)
1054                         if (isspace(buf[a]))
1055                                 buf[a] = 0;
1056                 output_static(buf);
1057         } else if (!strcasecmp(action, "image")) {
1058                 output_image();
1059
1060         /*
1061          * All functions handled below this point ... make sure we log in
1062          * before doing anything else!
1063          */
1064         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1065                 do_login();
1066         } else if (!WC->logged_in) {
1067                 display_login(NULL);
1068         }
1069
1070         /*
1071          * Various commands...
1072          */
1073
1074         else if (!strcasecmp(action, "do_welcome")) {
1075                 do_welcome();
1076         } else if (!strcasecmp(action, "blank")) {
1077                 blank_page();
1078         } else if (!strcasecmp(action, "display_main_menu")) {
1079                 display_main_menu();
1080         } else if (!strcasecmp(action, "whobbs")) {
1081                 whobbs();
1082         } else if (!strcasecmp(action, "knrooms")) {
1083                 knrooms();
1084         } else if (!strcasecmp(action, "gotonext")) {
1085                 slrp_highest();
1086                 gotonext();
1087         } else if (!strcasecmp(action, "skip")) {
1088                 gotonext();
1089         } else if (!strcasecmp(action, "ungoto")) {
1090                 ungoto();
1091         } else if (!strcasecmp(action, "dotgoto")) {
1092                 slrp_highest();
1093                 smart_goto(bstr("room"));
1094         } else if (!strcasecmp(action, "dotskip")) {
1095                 smart_goto(bstr("room"));
1096         } else if (!strcasecmp(action, "termquit")) {
1097                 do_logout();
1098         } else if (!strcasecmp(action, "readnew")) {
1099                 readloop("readnew");
1100         } else if (!strcasecmp(action, "readold")) {
1101                 readloop("readold");
1102         } else if (!strcasecmp(action, "readfwd")) {
1103                 readloop("readfwd");
1104         } else if (!strcasecmp(action, "headers")) {
1105                 readloop("headers");
1106         } else if (!strcasecmp(action, "display_enter")) {
1107                 display_enter();
1108         } else if (!strcasecmp(action, "post")) {
1109                 post_message();
1110         } else if (!strcasecmp(action, "delete_msg")) {
1111                 delete_msg();
1112         } else if (!strcasecmp(action, "confirm_move_msg")) {
1113                 confirm_move_msg();
1114         } else if (!strcasecmp(action, "move_msg")) {
1115                 move_msg();
1116         } else if (!strcasecmp(action, "userlist")) {
1117                 userlist();
1118         } else if (!strcasecmp(action, "showuser")) {
1119                 showuser();
1120         } else if (!strcasecmp(action, "display_page")) {
1121                 display_page();
1122         } else if (!strcasecmp(action, "page_user")) {
1123                 page_user();
1124         } else if (!strcasecmp(action, "chat")) {
1125                 do_chat();
1126         } else if (!strcasecmp(action, "display_private")) {
1127                 display_private("", 0);
1128         } else if (!strcasecmp(action, "goto_private")) {
1129                 goto_private();
1130         } else if (!strcasecmp(action, "zapped_list")) {
1131                 zapped_list();
1132         } else if (!strcasecmp(action, "display_zap")) {
1133                 display_zap();
1134         } else if (!strcasecmp(action, "zap")) {
1135                 zap();
1136         } else if (!strcasecmp(action, "display_entroom")) {
1137                 display_entroom();
1138         } else if (!strcasecmp(action, "entroom")) {
1139                 entroom();
1140         } else if (!strcasecmp(action, "display_editroom")) {
1141                 display_editroom();
1142         } else if (!strcasecmp(action, "netedit")) {
1143                 netedit();
1144         } else if (!strcasecmp(action, "editroom")) {
1145                 editroom();
1146         } else if (!strcasecmp(action, "display_whok")) {
1147                 display_whok();
1148         } else if (!strcasecmp(action, "display_editinfo")) {
1149                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
1150         } else if (!strcasecmp(action, "editinfo")) {
1151                 save_edit("Room info", "EINF 1", 1);
1152         } else if (!strcasecmp(action, "display_editbio")) {
1153                 sprintf(buf, "RBIO %s", WC->wc_username);
1154                 display_edit("Your bio", "NOOP", buf, "editbio");
1155         } else if (!strcasecmp(action, "editbio")) {
1156                 save_edit("Your bio", "EBIO", 0);
1157         } else if (!strcasecmp(action, "confirm_delete_room")) {
1158                 confirm_delete_room();
1159         } else if (!strcasecmp(action, "delete_room")) {
1160                 delete_room();
1161         } else if (!strcasecmp(action, "validate")) {
1162                 validate();
1163         } else if (!strcasecmp(action, "display_editpic")) {
1164                 display_graphics_upload("your photo",
1165                                         "UIMG 0|_userpic_",
1166                                         "/editpic");
1167         } else if (!strcasecmp(action, "editpic")) {
1168                 do_graphics_upload("UIMG 1|_userpic_");
1169         } else if (!strcasecmp(action, "display_editroompic")) {
1170                 display_graphics_upload("the graphic for this room",
1171                                         "UIMG 0|_roompic_",
1172                                         "/editroompic");
1173         } else if (!strcasecmp(action, "editroompic")) {
1174                 do_graphics_upload("UIMG 1|_roompic_");
1175         } else if (!strcasecmp(action, "delete_floor")) {
1176                 delete_floor();
1177         } else if (!strcasecmp(action, "rename_floor")) {
1178                 rename_floor();
1179         } else if (!strcasecmp(action, "create_floor")) {
1180                 create_floor();
1181         } else if (!strcasecmp(action, "display_editfloorpic")) {
1182                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1183                         bstr("which_floor"));
1184                 display_graphics_upload("the graphic for this floor",
1185                                         buf,
1186                                         "/editfloorpic");
1187         } else if (!strcasecmp(action, "editfloorpic")) {
1188                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1189                         bstr("which_floor"));
1190                 do_graphics_upload(buf);
1191         } else if (!strcasecmp(action, "display_reg")) {
1192                 display_reg(0);
1193         } else if (!strcasecmp(action, "register")) {
1194                 register_user();
1195         } else if (!strcasecmp(action, "display_changepw")) {
1196                 display_changepw();
1197         } else if (!strcasecmp(action, "changepw")) {
1198                 changepw();
1199         } else if (!strcasecmp(action, "display_edit_node")) {
1200                 display_edit_node();
1201         } else if (!strcasecmp(action, "edit_node")) {
1202                 edit_node();
1203         } else if (!strcasecmp(action, "display_netconf")) {
1204                 display_netconf();
1205         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1206                 display_confirm_delete_node();
1207         } else if (!strcasecmp(action, "delete_node")) {
1208                 delete_node();
1209         } else if (!strcasecmp(action, "display_add_node")) {
1210                 display_add_node();
1211         } else if (!strcasecmp(action, "add_node")) {
1212                 add_node();
1213         } else if (!strcasecmp(action, "terminate_session")) {
1214                 slrp_highest();
1215                 terminate_session();
1216         } else if (!strcasecmp(action, "edit_me")) {
1217                 edit_me();
1218         } else if (!strcasecmp(action, "display_siteconfig")) {
1219                 display_siteconfig();
1220         } else if (!strcasecmp(action, "page_popup")) {
1221                 page_popup();
1222         } else if (!strcasecmp(action, "siteconfig")) {
1223                 siteconfig();
1224         } else if (!strcasecmp(action, "display_generic")) {
1225                 display_generic();
1226         } else if (!strcasecmp(action, "do_generic")) {
1227                 do_generic();
1228         } else if (!strcasecmp(action, "display_menubar")) {
1229                 display_menubar(1);
1230         } else if (!strcasecmp(action, "output_mimepart")) {
1231                 output_mimepart();
1232         } else if (!strcasecmp(action, "edit_vcard")) {
1233                 edit_vcard();
1234         } else if (!strcasecmp(action, "submit_vcard")) {
1235                 submit_vcard();
1236         } else if (!strcasecmp(action, "select_user_to_edit")) {
1237                 select_user_to_edit(NULL, NULL);
1238         } else if (!strcasecmp(action, "display_edituser")) {
1239                 display_edituser(NULL);
1240         } else if (!strcasecmp(action, "edituser")) {
1241                 edituser();
1242         } else if (!strcasecmp(action, "create_user")) {
1243                 create_user();
1244         } else if (!strcasecmp(action, "changeview")) {
1245                 change_view();
1246         } else if (!strcasecmp(action, "folders")) {
1247                 folders();
1248         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1249                 do_stuff_to_msgs();
1250         } else if (!strcasecmp(action, "change_start_page")) {
1251                 change_start_page();
1252         } else if (!strcasecmp(action, "display_floorconfig")) {
1253                 display_floorconfig(NULL);
1254         } else if (!strcasecmp(action, "toggle_self_service")) {
1255                 toggle_self_service();
1256         } else if (!strcasecmp(action, "summary")) {
1257                 summary();
1258         } else if (!strcasecmp(action, "diagnostics")) {
1259                 output_headers(1);
1260
1261                 wprintf("You're in session %d<HR>\n", WC->wc_session);
1262                 wprintf("Command: <BR><PRE>\n");
1263                 escputs(cmd);
1264                 wprintf("</PRE><HR>\n");
1265                 wprintf("Variables: <BR><PRE>\n");
1266                 dump_vars();
1267                 wprintf("</PRE><HR>\n");
1268                 wDumpContent(1);
1269         }
1270         /* When all else fais, display the main menu. */
1271         else {
1272                 display_main_menu();
1273         }
1274
1275 SKIP_ALL_THIS_CRAP:
1276         fflush(stdout);
1277         if (content != NULL) {
1278                 free(content);
1279                 content = NULL;
1280         }
1281         free_urls();
1282         if (WC->upload_length > 0) {
1283                 free(WC->upload);
1284                 WC->upload_length = 0;
1285         }
1286
1287 #ifdef WITH_ZLIB
1288         if (WC->gzfd) {
1289                 gzclose(WC->gzfd);
1290                 WC->gzfd = NULL;
1291                 WC->gzcompressed = 0;
1292         }
1293 #endif
1294 }