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