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