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