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