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