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