]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Cleaned up a bunch of unused cruft from the old layout
[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->HaveExpressMessages) {
402                         wprintf("<div style=\"position:absolute; width=600px; height=400px; "
403                                 "background-color: #880000; z-index: 2; >\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_express_messages()
452 {
453         char buf[SIZ];
454
455         serv_puts("NOOP");
456         serv_gets(buf);
457         if (buf[3] == '*') WC->HaveExpressMessages = 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 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 cookie[SIZ];
839
840         strcpy(c_username, "");
841         strcpy(c_password, "");
842         strcpy(c_roomname, "");
843
844         WC->upload_length = 0;
845         WC->upload = NULL;
846         WC->vars = NULL;
847
848         WC->is_wap = 0;
849
850         hptr = req;
851         if (hptr == NULL) return;
852
853         strcpy(cmd, hptr->line);
854         hptr = hptr->next;
855         extract_action(action, cmd);
856
857         while (hptr != NULL) {
858                 strcpy(buf, hptr->line);
859                 hptr = hptr->next;
860
861                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
862                         safestrncpy(cookie, &buf[15], sizeof cookie);
863                         cookie_to_stuff(cookie, NULL,
864                                       c_username, c_password, c_roomname);
865                 }
866                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
867                         ContentLength = atoi(&buf[16]);
868                 }
869                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
870                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
871                 }
872                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
873                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
874                 }
875                 else if (!strncasecmp(buf, "Host: ", 6)) {
876                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
877                 }
878                 /* Only WAP gateways explicitly name this content-type */
879                 else if (strstr(buf, "text/vnd.wap.wml")) {
880                         WC->is_wap = 1;
881                 }
882         }
883
884         if (ContentLength > 0) {
885                 content = malloc(ContentLength + SIZ);
886                 memset(content, 0, ContentLength + SIZ);
887                 sprintf(content, "Content-type: %s\n"
888                                 "Content-length: %d\n\n",
889                                 ContentType, ContentLength);
890                 body_start = strlen(content);
891
892 /***** old version
893                 BytesRead = 0;
894                 while (BytesRead < ContentLength) {
895                         a=read(WC->http_sock, &content[BytesRead+body_start],
896                                 ContentLength - BytesRead);
897                         if (a <= 0) BytesRead = ContentLength;
898                         else BytesRead += a;
899                 }
900 *******/
901
902                 /* Now we're daring and read it all at once. */
903                 client_read(WC->http_sock, &content[BytesRead+body_start], ContentLength);
904
905                 if (!strncasecmp(ContentType,
906                               "application/x-www-form-urlencoded", 33)) {
907                         addurls(&content[body_start]);
908                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
909                         content_end = content + ContentLength + body_start;
910                         mime_parser(content, content_end, *upload_handler,
911                                         NULL, NULL, NULL, 0);
912                 }
913         } else {
914                 content = NULL;
915         }
916
917         /* make a note of where we are in case the user wants to save it */
918         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
919         remove_token(WC->this_page, 2, ' ');
920         remove_token(WC->this_page, 0, ' ');
921
922         /* If there are variables in the URL, we must grab them now */
923         for (a = 0; a < strlen(cmd); ++a) {
924                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
925                         for (b = a; b < strlen(cmd); ++b)
926                                 if (isspace(cmd[b]))
927                                         cmd[b] = 0;
928                         addurls(&cmd[a + 1]);
929                         cmd[a] = 0;
930                 }
931         }
932
933         /* Static content can be sent without connecting to Citadel. */
934         if (!strcasecmp(action, "static")) {
935                 strcpy(buf, &cmd[12]);
936                 for (a = 0; a < strlen(buf); ++a)
937                         if (isspace(buf[a]))
938                                 buf[a] = 0;
939                 output_static(buf);
940                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
941         }
942
943         /*
944          * If we're not connected to a Citadel server, try to hook up the
945          * connection now.
946          */
947         if (!WC->connected) {
948                 if (!strcasecmp(ctdlhost, "uds")) {
949                         /* unix domain socket */
950                         sprintf(buf, "%s/citadel.socket", ctdlport);
951                         WC->serv_sock = uds_connectsock(buf);
952                 }
953                 else {
954                         /* tcp socket */
955                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
956                 }
957
958                 if (WC->serv_sock < 0) {
959                         do_logout();
960                         goto SKIP_ALL_THIS_CRAP;
961                 }
962                 else {
963                         WC->connected = 1;
964                         serv_gets(buf); /* get the server welcome message */
965                         locate_host(browser_host, WC->http_sock);
966                         get_serv_info(browser_host, user_agent);
967                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
968                                 wprintf("You are connected to a Citadel "
969                                         "server running Citadel %d.%02d;\nin "
970                                         "order to run this version of WebCit "
971                                         "you must also have Citadel %d.%02d or"
972                                         " newer.\n\n\n",
973                                                 serv_info.serv_rev_level / 100,
974                                                 serv_info.serv_rev_level % 100,
975                                                 MINIMUM_CIT_VERSION / 100,
976                                                 MINIMUM_CIT_VERSION % 100
977                                         );
978                                 end_webcit_session();
979                                 goto SKIP_ALL_THIS_CRAP;
980                         }
981                 }
982         }
983
984         /*
985          * Functions which can be performed without logging in
986          */
987         if (!strcasecmp(action, "listsub")) {
988                 do_listsub();
989                 goto SKIP_ALL_THIS_CRAP;
990         }
991 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
992         if (!strcasecmp(action, "freebusy")) {
993                 do_freebusy(cmd);
994                 goto SKIP_ALL_THIS_CRAP;
995         }
996 #endif
997
998         check_for_express_messages();
999
1000         /*
1001          * If we're not logged in, but we have username and password cookies
1002          * supplied by the browser, try using them to log in.
1003          */
1004         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
1005                 serv_printf("USER %s", c_username);
1006                 serv_gets(buf);
1007                 if (buf[0] == '3') {
1008                         serv_printf("PASS %s", c_password);
1009                         serv_gets(buf);
1010                         if (buf[0] == '2') {
1011                                 become_logged_in(c_username, c_password, buf);
1012                         }
1013                 }
1014         }
1015         /*
1016          * If we don't have a current room, but a cookie specifying the
1017          * current room is supplied, make an effort to go there.
1018          */
1019         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1020                 serv_printf("GOTO %s", c_roomname);
1021                 serv_gets(buf);
1022                 if (buf[0] == '2') {
1023                         strcpy(WC->wc_roomname, c_roomname);
1024                 }
1025         }
1026
1027         if (!strcasecmp(action, "image")) {
1028                 output_image();
1029
1030         /*
1031          * All functions handled below this point ... make sure we log in
1032          * before doing anything else!
1033          */
1034         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1035                 do_login();
1036         } else if (!WC->logged_in) {
1037                 display_login(NULL);
1038         }
1039
1040         /*
1041          * Various commands...
1042          */
1043
1044         else if (!strcasecmp(action, "do_welcome")) {
1045                 do_welcome();
1046         } else if (!strcasecmp(action, "blank")) {
1047                 blank_page();
1048         } else if (!strcasecmp(action, "do_template")) {
1049                 url_do_template();
1050         } else if (!strcasecmp(action, "display_main_menu")) {
1051                 display_main_menu();
1052         } else if (!strcasecmp(action, "whobbs")) {
1053                 whobbs();
1054         } else if (!strcasecmp(action, "knrooms")) {
1055                 knrooms();
1056         } else if (!strcasecmp(action, "gotonext")) {
1057                 slrp_highest();
1058                 gotonext();
1059         } else if (!strcasecmp(action, "skip")) {
1060                 gotonext();
1061         } else if (!strcasecmp(action, "ungoto")) {
1062                 ungoto();
1063         } else if (!strcasecmp(action, "dotgoto")) {
1064                 slrp_highest();
1065                 smart_goto(bstr("room"));
1066         } else if (!strcasecmp(action, "dotskip")) {
1067                 smart_goto(bstr("room"));
1068         } else if (!strcasecmp(action, "termquit")) {
1069                 do_logout();
1070         } else if (!strcasecmp(action, "readnew")) {
1071                 readloop("readnew");
1072         } else if (!strcasecmp(action, "readold")) {
1073                 readloop("readold");
1074         } else if (!strcasecmp(action, "readfwd")) {
1075                 readloop("readfwd");
1076         } else if (!strcasecmp(action, "headers")) {
1077                 readloop("headers");
1078         } else if (!strcasecmp(action, "display_enter")) {
1079                 display_enter();
1080         } else if (!strcasecmp(action, "post")) {
1081                 post_message();
1082         } else if (!strcasecmp(action, "do_stuff_to_one_msg")) {
1083                 do_stuff_to_one_msg();
1084         } else if (!strcasecmp(action, "move_msg")) {
1085                 move_msg();
1086         } else if (!strcasecmp(action, "userlist")) {
1087                 userlist();
1088         } else if (!strcasecmp(action, "showuser")) {
1089                 showuser();
1090         } else if (!strcasecmp(action, "display_page")) {
1091                 display_page();
1092         } else if (!strcasecmp(action, "page_user")) {
1093                 page_user();
1094         } else if (!strcasecmp(action, "chat")) {
1095                 do_chat();
1096         } else if (!strcasecmp(action, "display_private")) {
1097                 display_private("", 0);
1098         } else if (!strcasecmp(action, "goto_private")) {
1099                 goto_private();
1100         } else if (!strcasecmp(action, "zapped_list")) {
1101                 zapped_list();
1102         } else if (!strcasecmp(action, "display_zap")) {
1103                 display_zap();
1104         } else if (!strcasecmp(action, "zap")) {
1105                 zap();
1106         } else if (!strcasecmp(action, "display_entroom")) {
1107                 display_entroom();
1108         } else if (!strcasecmp(action, "entroom")) {
1109                 entroom();
1110         } else if (!strcasecmp(action, "display_editroom")) {
1111                 display_editroom();
1112         } else if (!strcasecmp(action, "netedit")) {
1113                 netedit();
1114         } else if (!strcasecmp(action, "editroom")) {
1115                 editroom();
1116         } else if (!strcasecmp(action, "display_whok")) {
1117                 display_whok();
1118         } else if (!strcasecmp(action, "display_editinfo")) {
1119                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1120         } else if (!strcasecmp(action, "editinfo")) {
1121                 save_edit("Room info", "EINF 1", 1);
1122         } else if (!strcasecmp(action, "display_editbio")) {
1123                 sprintf(buf, "RBIO %s", WC->wc_username);
1124                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1125         } else if (!strcasecmp(action, "editbio")) {
1126                 save_edit("Your bio", "EBIO", 0);
1127         } else if (!strcasecmp(action, "confirm_delete_room")) {
1128                 confirm_delete_room();
1129         } else if (!strcasecmp(action, "delete_room")) {
1130                 delete_room();
1131         } else if (!strcasecmp(action, "validate")) {
1132                 validate();
1133         } else if (!strcasecmp(action, "display_editpic")) {
1134                 display_graphics_upload("your photo",
1135                                         "UIMG 0|_userpic_",
1136                                         "/editpic");
1137         } else if (!strcasecmp(action, "editpic")) {
1138                 do_graphics_upload("UIMG 1|_userpic_");
1139         } else if (!strcasecmp(action, "display_editroompic")) {
1140                 display_graphics_upload("the graphic for this room",
1141                                         "UIMG 0|_roompic_",
1142                                         "/editroompic");
1143         } else if (!strcasecmp(action, "editroompic")) {
1144                 do_graphics_upload("UIMG 1|_roompic_");
1145         } else if (!strcasecmp(action, "delete_floor")) {
1146                 delete_floor();
1147         } else if (!strcasecmp(action, "rename_floor")) {
1148                 rename_floor();
1149         } else if (!strcasecmp(action, "create_floor")) {
1150                 create_floor();
1151         } else if (!strcasecmp(action, "display_editfloorpic")) {
1152                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1153                         bstr("which_floor"));
1154                 display_graphics_upload("the graphic for this floor",
1155                                         buf,
1156                                         "/editfloorpic");
1157         } else if (!strcasecmp(action, "editfloorpic")) {
1158                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1159                         bstr("which_floor"));
1160                 do_graphics_upload(buf);
1161         } else if (!strcasecmp(action, "display_reg")) {
1162                 display_reg(0);
1163         } else if (!strcasecmp(action, "display_changepw")) {
1164                 display_changepw();
1165         } else if (!strcasecmp(action, "changepw")) {
1166                 changepw();
1167         } else if (!strcasecmp(action, "display_edit_node")) {
1168                 display_edit_node();
1169         } else if (!strcasecmp(action, "edit_node")) {
1170                 edit_node();
1171         } else if (!strcasecmp(action, "display_netconf")) {
1172                 display_netconf();
1173         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1174                 display_confirm_delete_node();
1175         } else if (!strcasecmp(action, "delete_node")) {
1176                 delete_node();
1177         } else if (!strcasecmp(action, "display_add_node")) {
1178                 display_add_node();
1179         } else if (!strcasecmp(action, "add_node")) {
1180                 add_node();
1181         } else if (!strcasecmp(action, "terminate_session")) {
1182                 slrp_highest();
1183                 terminate_session();
1184         } else if (!strcasecmp(action, "edit_me")) {
1185                 edit_me();
1186         } else if (!strcasecmp(action, "display_siteconfig")) {
1187                 display_siteconfig();
1188         } else if (!strcasecmp(action, "chat_recv")) {
1189                 chat_recv();
1190         } else if (!strcasecmp(action, "chat_send")) {
1191                 chat_send();
1192         } else if (!strcasecmp(action, "siteconfig")) {
1193                 siteconfig();
1194         } else if (!strcasecmp(action, "display_generic")) {
1195                 display_generic();
1196         } else if (!strcasecmp(action, "do_generic")) {
1197                 do_generic();
1198         } else if (!strcasecmp(action, "display_menubar")) {
1199                 display_menubar(1);
1200         } else if (!strcasecmp(action, "output_mimepart")) {
1201                 output_mimepart();
1202         } else if (!strcasecmp(action, "edit_vcard")) {
1203                 edit_vcard();
1204         } else if (!strcasecmp(action, "submit_vcard")) {
1205                 submit_vcard();
1206         } else if (!strcasecmp(action, "select_user_to_edit")) {
1207                 select_user_to_edit(NULL, NULL);
1208         } else if (!strcasecmp(action, "display_edituser")) {
1209                 display_edituser(NULL, 0);
1210         } else if (!strcasecmp(action, "edituser")) {
1211                 edituser();
1212         } else if (!strcasecmp(action, "create_user")) {
1213                 create_user();
1214         } else if (!strcasecmp(action, "changeview")) {
1215                 change_view();
1216         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1217                 do_stuff_to_msgs();
1218         } else if (!strcasecmp(action, "change_start_page")) {
1219                 change_start_page();
1220         } else if (!strcasecmp(action, "display_floorconfig")) {
1221                 display_floorconfig(NULL);
1222         } else if (!strcasecmp(action, "toggle_self_service")) {
1223                 toggle_self_service();
1224 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1225         } else if (!strcasecmp(action, "display_edit_task")) {
1226                 display_edit_task();
1227         } else if (!strcasecmp(action, "save_task")) {
1228                 save_task();
1229         } else if (!strcasecmp(action, "display_edit_event")) {
1230                 display_edit_event();
1231         } else if (!strcasecmp(action, "save_event")) {
1232                 save_event();
1233         } else if (!strcasecmp(action, "respond_to_request")) {
1234                 respond_to_request();
1235         } else if (!strcasecmp(action, "handle_rsvp")) {
1236                 handle_rsvp();
1237 #endif
1238         } else if (!strcasecmp(action, "summary")) {
1239                 summary();
1240         } else if (!strcasecmp(action, "iconbar")) {
1241                 do_iconbar();
1242         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1243                 display_customize_iconbar();
1244         } else if (!strcasecmp(action, "commit_iconbar")) {
1245                 commit_iconbar();
1246         } else if (!strcasecmp(action, "set_room_policy")) {
1247                 set_room_policy();
1248         } else if (!strcasecmp(action, "display_inetconf")) {
1249                 display_inetconf();
1250         } else if (!strcasecmp(action, "save_inetconf")) {
1251                 save_inetconf();
1252         } else if (!strcasecmp(action, "diagnostics")) {
1253                 output_headers(1, 1, 1, 0, 0, 0, 0);
1254
1255                 wprintf("You're in session %d<hr />\n", WC->wc_session);
1256                 wprintf("Command: <br /><PRE>\n");
1257                 escputs(cmd);
1258                 wprintf("</PRE><hr />\n");
1259                 wprintf("Variables: <br /><PRE>\n");
1260                 dump_vars();
1261                 wprintf("</PRE><hr />\n");
1262                 wDumpContent(1);
1263         }
1264         /* When all else fais, display the main menu. */
1265         else {
1266                 display_main_menu();
1267         }
1268
1269 SKIP_ALL_THIS_CRAP:
1270         fflush(stdout);
1271         if (content != NULL) {
1272                 free(content);
1273                 content = NULL;
1274         }
1275         free_urls();
1276         if (WC->upload_length > 0) {
1277                 free(WC->upload);
1278                 WC->upload_length = 0;
1279         }
1280
1281 }