]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
fb5fb8b3cf1712c5fb99f0d04e2fee22c614a3a7
[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\n"
367                         "Server: %s / %s\n", SERVER, serv_info.serv_software
368                 );
369                 if (!cache)
370                         wprintf("Connection: close\n"
371                                 "Pragma: no-cache\n"
372                                 "Cache-Control: no-store\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=/\n", unset);
381         } else {
382                 wprintf("Set-cookie: webcit=%s; path=/\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\n", whichpage);
445         wprintf("URI: %s\n", whichpage);
446         wprintf("Content-type: text/html\n\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         lprintf(9, "Checking for instant messages...\n");
460         serv_puts("NOOP");
461         serv_gets(buf);
462         if (buf[3] == '*') WC->HaveInstantMessages = 1;
463         lprintf(9, "...done\n");
464 }
465
466
467
468
469 /* 
470  * Output a piece of content to the web browser
471  */
472 void http_transmit_thing(char *thing, size_t length, char *content_type,
473                          int is_static) {
474         if (is_static) {
475                 output_headers(0, 0, 0, 0, 0, 0, 1);
476         }
477         else {
478                 output_headers(0, 0, 0, 0, 0, 0, 0);
479         }
480         wprintf("Content-type: %s\n"
481                 "Content-length: %ld\n"
482                 "Server: %s\n"
483                 "Connection: close\n"
484                 "\n",
485                 content_type,
486                 (long) length,
487                 SERVER
488         );
489         client_write(thing, (size_t)length);
490 }
491
492
493
494
495 void output_static(char *what)
496 {
497         char buf[4096];
498         FILE *fp;
499         struct stat statbuf;
500         off_t bytes;
501         char *bigbuffer;
502         char content_type[SIZ];
503
504         sprintf(buf, "static/%s", what);
505         fp = fopen(buf, "rb");
506         if (fp == NULL) {
507                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
508                 wprintf("Content-Type: text/plain\n");
509                 wprintf("\n");
510                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
511         } else {
512                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
513                         strcpy(content_type, "image/gif");
514                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
515                         strcpy(content_type, "text/plain");
516                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
517                         strcpy(content_type, "text/css");
518                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
519                         strcpy(content_type, "image/jpeg");
520                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
521                         strcpy(content_type, "image/png");
522                 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
523                         strcpy(content_type, "image/x-icon");
524                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
525                         strcpy(content_type, "text/html");
526                 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
527                         strcpy(content_type, "text/html");
528                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
529                         strcpy(content_type, "text/vnd.wap.wml");
530                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
531                         strcpy(content_type, "text/vnd.wap.wmlscript");
532                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
533                         strcpy(content_type, "application/vnd.wap.wmlc");
534                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
535                         strcpy(content_type, "application/vnd.wap.wmlscriptc");
536                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
537                         strcpy(content_type, "image/vnd.wap.wbmp");
538                 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
539                         strcpy(content_type, "text/javascript");
540                 else
541                         strcpy(content_type, "application/octet-stream");
542
543                 fstat(fileno(fp), &statbuf);
544                 bytes = statbuf.st_size;
545                 /* lprintf(3, "Static: %s, (%s; %ld bytes)\n",
546                         what, content_type, bytes); */
547                 bigbuffer = malloc(bytes + 2);
548                 fread(bigbuffer, bytes, 1, fp);
549                 fclose(fp);
550
551                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
552                 free(bigbuffer);
553         }
554         if (!strcasecmp(bstr("force_close_session"), "yes")) {
555                 end_webcit_session();
556         }
557 }
558
559
560 /*
561  * When the browser requests an image file from the Citadel server,
562  * this function is called to transmit it.
563  */
564 void output_image()
565 {
566         char buf[SIZ];
567         char *xferbuf = NULL;
568         off_t bytes;
569
570         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
571         serv_gets(buf);
572         if (buf[0] == '2') {
573                 bytes = extract_long(&buf[4], 0);
574                 xferbuf = malloc(bytes + 2);
575
576                 /* Read it from the server */
577                 read_server_binary(xferbuf, bytes);
578                 serv_puts("CLOS");
579                 serv_gets(buf);
580
581                 /* Write it to the browser */
582                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
583                 free(xferbuf);
584
585         } else {
586
587                 /* Instead of an ugly 404, send a 1x1 transparent GIF
588                  * when there's no such image on the server.
589                  */
590                 output_static("blank.gif");
591
592                 /*
593                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
594                 output_headers(0, 0, 0, 0, 0, 0, 0);
595                 wprintf("Content-Type: text/plain\n"
596                         "\n"
597                         "Error retrieving image: %s\n",
598                         &buf[4]
599                 );
600                 */
601
602         }
603
604
605
606 }
607
608 /*
609  */
610 void output_mimepart()
611 {
612         char buf[SIZ];
613         off_t bytes;
614         char content_type[SIZ];
615         char *content = NULL;
616         
617         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
618         serv_gets(buf);
619         if (buf[0] == '2') {
620                 bytes = extract_long(&buf[4], 0);
621                 content = malloc(bytes + 2);
622                 extract(content_type, &buf[4], 3);
623                 output_headers(0, 0, 0, 0, 0, 0, 0);
624                 read_server_binary(content, bytes);
625                 serv_puts("CLOS");
626                 serv_gets(buf);
627                 http_transmit_thing(content, bytes, content_type, 0);
628                 free(content);
629         } else {
630                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
631                 output_headers(0, 0, 0, 0, 0, 0, 0);
632                 wprintf("Content-Type: text/plain\n");
633                 wprintf("\n");
634                 wprintf("Error retrieving part: %s\n", &buf[4]);
635         }
636
637 }
638
639
640 /*
641  */
642 char *load_mimepart(long msgnum, char *partnum)
643 {
644         char buf[SIZ];
645         off_t bytes;
646         char content_type[SIZ];
647         char *content;
648         
649         serv_printf("OPNA %ld|%s", msgnum, partnum);
650         serv_gets(buf);
651         if (buf[0] == '2') {
652                 bytes = extract_long(&buf[4], 0);
653                 extract(content_type, &buf[4], 3);
654
655                 content = malloc(bytes + 2);
656                 read_server_binary(content, bytes);
657
658                 serv_puts("CLOS");
659                 serv_gets(buf);
660                 content[bytes] = 0;     /* null terminate for good measure */
661                 return(content);
662         }
663         else {
664                 return(NULL);
665         }
666
667 }
668
669
670 /*
671  * Convenience functions to display a page containing only a string
672  */
673 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
674 {
675         wprintf("HTTP/1.0 200 OK\n");
676         output_headers(1, 1, 2, 0, 0, 0, 0);
677         wprintf("<div id=\"banner\">\n");
678         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
679         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
680         wprintf("</TD></TR></TABLE>\n");
681         wprintf("</div>\n<div id=\"content\">\n");
682         escputs(messagetext);
683
684         wprintf("<hr />\n");
685         wDumpContent(1);
686 }
687
688
689 /*
690  * Display a blank page.
691  */
692 void blank_page(void) {
693         output_headers(1, 1, 0, 0, 1, 0, 0);
694         wDumpContent(2);
695 }
696
697
698 /*
699  * A template has been requested
700  */
701 void url_do_template(void) {
702         do_template(bstr("template"));
703 }
704
705
706
707 /*
708  * Offer to make any page the user's "start page."
709  */
710 void offer_start_page(void) {
711         wprintf("<A HREF=\"/change_start_page?startpage=");
712         urlescputs(WC->this_page);
713         wprintf("\">"
714                 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
715                 "</A>"
716         );
717 }
718
719
720 /* 
721  * Change the user's start page
722  */
723 void change_start_page(void) {
724
725         if (bstr("startpage") == NULL) {
726                 strcpy(WC->ImportantMessage,
727                         "startpage set to null");
728                 display_main_menu();
729                 return;
730         }
731
732         set_preference("startpage", bstr("startpage"));
733
734         output_headers(1, 1, 0, 0, 0, 0, 0);
735         do_template("newstartpage");
736         wDumpContent(1);
737 }
738
739
740
741
742 void display_success(char *successmessage)
743 {
744         convenience_page("007700", "OK", successmessage);
745 }
746
747
748
749 void extract_action(char *actbuf, char *cmdbuf)
750 {
751         int i;
752
753         strcpy(actbuf, cmdbuf);
754
755         /*
756          * First strip out the http method
757          */
758         remove_token(actbuf, 0, ' ');
759         if (actbuf[0] == ' ') strcpy(actbuf, &actbuf[1]);
760         if (actbuf[0] == '/') strcpy(actbuf, &actbuf[1]);
761
762         /*
763          * Now kill invalid (for webcit) characters
764          */
765         for (i = 0; i < strlen(actbuf); ++i) {
766                 if (actbuf[i] == ' ') {
767                         actbuf[i] = 0;
768                         i = 0;
769                 }
770                 if (actbuf[i] == '/') {
771                         actbuf[i] = 0;
772                         i = 0;
773                 }
774                 if (actbuf[i] == '?') {
775                         actbuf[i] = 0;
776                         i = 0;
777                 }
778                 if (actbuf[i] == '&') {
779                         actbuf[i] = 0;
780                         i = 0;
781                 }
782         }
783 }
784
785
786 void upload_handler(char *name, char *filename, char *partnum, char *disp,
787                         void *content, char *cbtype, size_t length,
788                         char *encoding, void *userdata)
789 {
790         struct urlcontent *u;
791
792         /* Form fields */
793         if ( (length > 0) && (strlen(cbtype) == 0) ) {
794                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
795                 u->next = WC->urlstrings;
796                 WC->urlstrings = u;
797                 safestrncpy(u->url_key, name, sizeof(u->url_key));
798                 u->url_data = malloc(length + 1);
799                 memcpy(u->url_data, content, length);
800                 u->url_data[length] = 0;
801         }
802
803         /* Uploaded files */
804         if ( (length > 0) && (strlen(cbtype) > 0) ) {
805                 WC->upload = malloc(length);
806                 if (WC->upload != NULL) {
807                         WC->upload_length = length;
808                         safestrncpy(WC->upload_filename, filename,
809                                         sizeof(WC->upload_filename));
810                         safestrncpy(WC->upload_content_type, cbtype,
811                                         sizeof(WC->upload_content_type));
812                         memcpy(WC->upload, content, length);
813                 }
814                 else {
815                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
816                 }
817         }
818
819 }
820
821
822
823
824 /*
825  * Entry point for WebCit transaction
826  */
827 void session_loop(struct httprequest *req)
828 {
829         char cmd[SIZ];
830         char method[SIZ];
831         char action[SIZ];
832         char buf[SIZ];
833         int a, b;
834         int ContentLength = 0;
835         int BytesRead = 0;
836         char ContentType[512];
837         char *content = NULL;
838         char *content_end = NULL;
839         struct httprequest *hptr;
840         char browser_host[SIZ];
841         char user_agent[SIZ];
842         int body_start = 0;
843
844         /* We stuff these with the values coming from the client cookies,
845          * so we can use them to reconnect a timed out session if we have to.
846          */
847         char c_username[SIZ];
848         char c_password[SIZ];
849         char c_roomname[SIZ];
850         char c_httpauth_string[SIZ];
851         char c_httpauth_user[SIZ];
852         char c_httpauth_pass[SIZ];
853         char cookie[SIZ];
854
855         strcpy(c_username, "");
856         strcpy(c_password, "");
857         strcpy(c_roomname, "");
858         strcpy(c_httpauth_string, "");
859         strcpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER);
860         strcpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS);
861
862         WC->upload_length = 0;
863         WC->upload = NULL;
864         WC->vars = NULL;
865
866         WC->is_wap = 0;
867
868         hptr = req;
869         if (hptr == NULL) return;
870
871         strcpy(cmd, hptr->line);
872         hptr = hptr->next;
873         extract_token(method, cmd, 0, ' ');
874         extract_action(action, cmd);
875
876         while (hptr != NULL) {
877                 strcpy(buf, hptr->line);
878                 hptr = hptr->next;
879
880                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
881                         safestrncpy(cookie, &buf[15], sizeof cookie);
882                         cookie_to_stuff(cookie, NULL,
883                                       c_username, c_password, c_roomname);
884                 }
885                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
886                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
887                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':');
888                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':');
889                 }
890                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
891                         ContentLength = atoi(&buf[16]);
892                 }
893                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
894                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
895                 }
896                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
897                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
898                 }
899                 else if (!strncasecmp(buf, "Host: ", 6)) {
900                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
901                 }
902                 /* Only WAP gateways explicitly name this content-type */
903                 else if (strstr(buf, "text/vnd.wap.wml")) {
904                         WC->is_wap = 1;
905                 }
906         }
907
908         if (ContentLength > 0) {
909                 content = malloc(ContentLength + SIZ);
910                 memset(content, 0, ContentLength + SIZ);
911                 sprintf(content, "Content-type: %s\n"
912                                 "Content-length: %d\n\n",
913                                 ContentType, ContentLength);
914                 body_start = strlen(content);
915
916                 /* Be daring and read it all at once. */
917                 client_read(WC->http_sock, &content[BytesRead+body_start],
918                         ContentLength);
919
920                 if (!strncasecmp(ContentType,
921                               "application/x-www-form-urlencoded", 33)) {
922                         addurls(&content[body_start]);
923                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
924                         content_end = content + ContentLength + body_start;
925                         mime_parser(content, content_end, *upload_handler,
926                                         NULL, NULL, NULL, 0);
927                 }
928         } else {
929                 content = NULL;
930         }
931
932         /* make a note of where we are in case the user wants to save it */
933         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
934         remove_token(WC->this_page, 2, ' ');
935         remove_token(WC->this_page, 0, ' ');
936
937         /* If there are variables in the URL, we must grab them now */
938         for (a = 0; a < strlen(cmd); ++a) {
939                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
940                         for (b = a; b < strlen(cmd); ++b)
941                                 if (isspace(cmd[b]))
942                                         cmd[b] = 0;
943                         addurls(&cmd[a + 1]);
944                         cmd[a] = 0;
945                 }
946         }
947
948         /* Static content can be sent without connecting to Citadel. */
949         if (!strcasecmp(action, "static")) {
950                 strcpy(buf, &cmd[12]);
951                 for (a = 0; a < strlen(buf); ++a)
952                         if (isspace(buf[a]))
953                                 buf[a] = 0;
954                 output_static(buf);
955                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
956         }
957
958         /*
959          * If we're not connected to a Citadel server, try to hook up the
960          * connection now.
961          */
962         if (!WC->connected) {
963                 if (!strcasecmp(ctdlhost, "uds")) {
964                         /* unix domain socket */
965                         sprintf(buf, "%s/citadel.socket", ctdlport);
966                         WC->serv_sock = uds_connectsock(buf);
967                 }
968                 else {
969                         /* tcp socket */
970                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
971                 }
972
973                 if (WC->serv_sock < 0) {
974                         do_logout();
975                         goto SKIP_ALL_THIS_CRAP;
976                 }
977                 else {
978                         WC->connected = 1;
979                         serv_gets(buf); /* get the server welcome message */
980                         locate_host(browser_host, WC->http_sock);
981                         get_serv_info(browser_host, user_agent);
982                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
983                                 wprintf("You are connected to a Citadel "
984                                         "server running Citadel %d.%02d;\nin "
985                                         "order to run this version of WebCit "
986                                         "you must also have Citadel %d.%02d or"
987                                         " newer.\n\n\n",
988                                                 serv_info.serv_rev_level / 100,
989                                                 serv_info.serv_rev_level % 100,
990                                                 MINIMUM_CIT_VERSION / 100,
991                                                 MINIMUM_CIT_VERSION % 100
992                                         );
993                                 end_webcit_session();
994                                 goto SKIP_ALL_THIS_CRAP;
995                         }
996                 }
997         }
998
999         /*
1000          * Functions which can be performed without logging in
1001          */
1002         if (!strcasecmp(action, "listsub")) {
1003                 do_listsub();
1004                 goto SKIP_ALL_THIS_CRAP;
1005         }
1006 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1007         if (!strcasecmp(action, "freebusy")) {
1008                 do_freebusy(cmd);
1009                 goto SKIP_ALL_THIS_CRAP;
1010         }
1011 #endif
1012
1013         /*
1014          * If we're not logged in, but we have HTTP Authentication data,
1015          * try logging in to Citadel using that.
1016          */
1017         if ((!WC->logged_in)
1018            && (strlen(c_httpauth_user) > 0)
1019            && (strlen(c_httpauth_pass) > 0)) {
1020                 serv_printf("USER %s", c_httpauth_user);
1021                 serv_gets(buf);
1022                 if (buf[0] == '3') {
1023                         serv_printf("PASS %s", c_httpauth_pass);
1024                         serv_gets(buf);
1025                         if (buf[0] == '2') {
1026                                 become_logged_in(c_httpauth_user,
1027                                                 c_httpauth_pass, buf);
1028                                 strcpy(WC->httpauth_user, c_httpauth_user);
1029                                 strcpy(WC->httpauth_pass, c_httpauth_pass);
1030                         }
1031                 }
1032         }
1033
1034         /* 
1035          * The GroupDAV stuff relies on HTTP authentication instead of
1036          * our session's authentication.
1037          */
1038         if (!strncasecmp(action, "groupdav", 8)) {
1039                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1040                         ContentLength, content+body_start);
1041                 if (!WC->logged_in) {
1042                         WC->killthis = 1;       /* If not logged in, don't */
1043                 }                               /* keep the session active */
1044                 goto SKIP_ALL_THIS_CRAP;
1045         }
1046
1047
1048         /*
1049          * If this isn't a GroupDAV session, it's an ordinary browser
1050          * connecting to the user interface.  Only allow GET and POST
1051          * methods.
1052          */
1053         if ((strcasecmp(method, "GET")) && (strcasecmp(method, "POST"))) {
1054                 wprintf("HTTP/1.1 405 Method Not Allowed\r\n");
1055                 groupdav_common_headers();
1056                 wprintf("Content-Length: 0\r\n\r\n");
1057                 goto SKIP_ALL_THIS_CRAP;
1058         }
1059
1060         /*
1061          * If we're not logged in, but we have username and password cookies
1062          * supplied by the browser, try using them to log in.
1063          */
1064         if ((!WC->logged_in)
1065            && (strlen(c_username) > 0)
1066            && (strlen(c_password) > 0)) {
1067                 serv_printf("USER %s", c_username);
1068                 serv_gets(buf);
1069                 if (buf[0] == '3') {
1070                         serv_printf("PASS %s", c_password);
1071                         serv_gets(buf);
1072                         if (buf[0] == '2') {
1073                                 become_logged_in(c_username, c_password, buf);
1074                         }
1075                 }
1076         }
1077         /*
1078          * If we don't have a current room, but a cookie specifying the
1079          * current room is supplied, make an effort to go there.
1080          */
1081         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1082                 serv_printf("GOTO %s", c_roomname);
1083                 serv_gets(buf);
1084                 if (buf[0] == '2') {
1085                         strcpy(WC->wc_roomname, c_roomname);
1086                 }
1087         }
1088
1089         /*
1090          * If there are instant messages waiting, retrieve them for display.
1091          */
1092         check_for_instant_messages();
1093
1094         if (!strcasecmp(action, "image")) {
1095                 output_image();
1096
1097         /*
1098          * All functions handled below this point ... make sure we log in
1099          * before doing anything else!
1100          */
1101         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1102                 do_login();
1103         } else if (!WC->logged_in) {
1104                 display_login(NULL);
1105         }
1106
1107         /*
1108          * Various commands...
1109          */
1110
1111         else if (!strcasecmp(action, "do_welcome")) {
1112                 do_welcome();
1113         } else if (!strcasecmp(action, "blank")) {
1114                 blank_page();
1115         } else if (!strcasecmp(action, "do_template")) {
1116                 url_do_template();
1117         } else if (!strcasecmp(action, "display_aide_menu")) {
1118                 display_aide_menu();
1119         } else if (!strcasecmp(action, "display_main_menu")) {
1120                 display_main_menu();
1121         } else if (!strcasecmp(action, "whobbs")) {
1122                 whobbs();
1123         } else if (!strcasecmp(action, "knrooms")) {
1124                 knrooms();
1125         } else if (!strcasecmp(action, "gotonext")) {
1126                 slrp_highest();
1127                 gotonext();
1128         } else if (!strcasecmp(action, "skip")) {
1129                 gotonext();
1130         } else if (!strcasecmp(action, "ungoto")) {
1131                 ungoto();
1132         } else if (!strcasecmp(action, "dotgoto")) {
1133                 slrp_highest();
1134                 smart_goto(bstr("room"));
1135         } else if (!strcasecmp(action, "dotskip")) {
1136                 smart_goto(bstr("room"));
1137         } else if (!strcasecmp(action, "termquit")) {
1138                 do_logout();
1139         } else if (!strcasecmp(action, "readnew")) {
1140                 readloop("readnew");
1141         } else if (!strcasecmp(action, "readold")) {
1142                 readloop("readold");
1143         } else if (!strcasecmp(action, "readfwd")) {
1144                 readloop("readfwd");
1145         } else if (!strcasecmp(action, "headers")) {
1146                 readloop("headers");
1147         } else if (!strcasecmp(action, "display_enter")) {
1148                 display_enter();
1149         } else if (!strcasecmp(action, "post")) {
1150                 post_message();
1151         } else if (!strcasecmp(action, "move_msg")) {
1152                 move_msg();
1153         } else if (!strcasecmp(action, "delete_msg")) {
1154                 delete_msg();
1155         } else if (!strcasecmp(action, "userlist")) {
1156                 userlist();
1157         } else if (!strcasecmp(action, "showuser")) {
1158                 showuser();
1159         } else if (!strcasecmp(action, "display_page")) {
1160                 display_page();
1161         } else if (!strcasecmp(action, "page_user")) {
1162                 page_user();
1163         } else if (!strcasecmp(action, "chat")) {
1164                 do_chat();
1165         } else if (!strcasecmp(action, "display_private")) {
1166                 display_private("", 0);
1167         } else if (!strcasecmp(action, "goto_private")) {
1168                 goto_private();
1169         } else if (!strcasecmp(action, "zapped_list")) {
1170                 zapped_list();
1171         } else if (!strcasecmp(action, "display_zap")) {
1172                 display_zap();
1173         } else if (!strcasecmp(action, "zap")) {
1174                 zap();
1175         } else if (!strcasecmp(action, "display_entroom")) {
1176                 display_entroom();
1177         } else if (!strcasecmp(action, "entroom")) {
1178                 entroom();
1179         } else if (!strcasecmp(action, "display_whok")) {
1180                 display_whok();
1181         } else if (!strcasecmp(action, "do_invt_kick")) {
1182                 do_invt_kick();
1183         } else if (!strcasecmp(action, "display_editroom")) {
1184                 display_editroom();
1185         } else if (!strcasecmp(action, "netedit")) {
1186                 netedit();
1187         } else if (!strcasecmp(action, "editroom")) {
1188                 editroom();
1189         } else if (!strcasecmp(action, "display_editinfo")) {
1190                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1191         } else if (!strcasecmp(action, "editinfo")) {
1192                 save_edit("Room info", "EINF 1", 1);
1193         } else if (!strcasecmp(action, "display_editbio")) {
1194                 sprintf(buf, "RBIO %s", WC->wc_username);
1195                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1196         } else if (!strcasecmp(action, "editbio")) {
1197                 save_edit("Your bio", "EBIO", 0);
1198         } else if (!strcasecmp(action, "confirm_move_msg")) {
1199                 confirm_move_msg();
1200         } else if (!strcasecmp(action, "confirm_delete_room")) {
1201                 confirm_delete_room();
1202         } else if (!strcasecmp(action, "delete_room")) {
1203                 delete_room();
1204         } else if (!strcasecmp(action, "validate")) {
1205                 validate();
1206         } else if (!strcasecmp(action, "display_editpic")) {
1207                 display_graphics_upload("your photo",
1208                                         "UIMG 0|_userpic_",
1209                                         "/editpic");
1210         } else if (!strcasecmp(action, "editpic")) {
1211                 do_graphics_upload("UIMG 1|_userpic_");
1212         } else if (!strcasecmp(action, "display_editroompic")) {
1213                 display_graphics_upload("the graphic for this room",
1214                                         "UIMG 0|_roompic_",
1215                                         "/editroompic");
1216         } else if (!strcasecmp(action, "editroompic")) {
1217                 do_graphics_upload("UIMG 1|_roompic_");
1218         } else if (!strcasecmp(action, "delete_floor")) {
1219                 delete_floor();
1220         } else if (!strcasecmp(action, "rename_floor")) {
1221                 rename_floor();
1222         } else if (!strcasecmp(action, "create_floor")) {
1223                 create_floor();
1224         } else if (!strcasecmp(action, "display_editfloorpic")) {
1225                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1226                         bstr("which_floor"));
1227                 display_graphics_upload("the graphic for this floor",
1228                                         buf,
1229                                         "/editfloorpic");
1230         } else if (!strcasecmp(action, "editfloorpic")) {
1231                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1232                         bstr("which_floor"));
1233                 do_graphics_upload(buf);
1234         } else if (!strcasecmp(action, "display_reg")) {
1235                 display_reg(0);
1236         } else if (!strcasecmp(action, "display_changepw")) {
1237                 display_changepw();
1238         } else if (!strcasecmp(action, "changepw")) {
1239                 changepw();
1240         } else if (!strcasecmp(action, "display_edit_node")) {
1241                 display_edit_node();
1242         } else if (!strcasecmp(action, "edit_node")) {
1243                 edit_node();
1244         } else if (!strcasecmp(action, "display_netconf")) {
1245                 display_netconf();
1246         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1247                 display_confirm_delete_node();
1248         } else if (!strcasecmp(action, "delete_node")) {
1249                 delete_node();
1250         } else if (!strcasecmp(action, "display_add_node")) {
1251                 display_add_node();
1252         } else if (!strcasecmp(action, "add_node")) {
1253                 add_node();
1254         } else if (!strcasecmp(action, "terminate_session")) {
1255                 slrp_highest();
1256                 terminate_session();
1257         } else if (!strcasecmp(action, "edit_me")) {
1258                 edit_me();
1259         } else if (!strcasecmp(action, "display_siteconfig")) {
1260                 display_siteconfig();
1261         } else if (!strcasecmp(action, "chat_recv")) {
1262                 chat_recv();
1263         } else if (!strcasecmp(action, "chat_send")) {
1264                 chat_send();
1265         } else if (!strcasecmp(action, "siteconfig")) {
1266                 siteconfig();
1267         } else if (!strcasecmp(action, "display_generic")) {
1268                 display_generic();
1269         } else if (!strcasecmp(action, "do_generic")) {
1270                 do_generic();
1271         } else if (!strcasecmp(action, "display_menubar")) {
1272                 display_menubar(1);
1273         } else if (!strcasecmp(action, "output_mimepart")) {
1274                 output_mimepart();
1275         } else if (!strcasecmp(action, "edit_vcard")) {
1276                 edit_vcard();
1277         } else if (!strcasecmp(action, "submit_vcard")) {
1278                 submit_vcard();
1279         } else if (!strcasecmp(action, "select_user_to_edit")) {
1280                 select_user_to_edit(NULL, NULL);
1281         } else if (!strcasecmp(action, "display_edituser")) {
1282                 display_edituser(NULL, 0);
1283         } else if (!strcasecmp(action, "edituser")) {
1284                 edituser();
1285         } else if (!strcasecmp(action, "create_user")) {
1286                 create_user();
1287         } else if (!strcasecmp(action, "changeview")) {
1288                 change_view();
1289         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1290                 do_stuff_to_msgs();
1291         } else if (!strcasecmp(action, "change_start_page")) {
1292                 change_start_page();
1293         } else if (!strcasecmp(action, "display_floorconfig")) {
1294                 display_floorconfig(NULL);
1295         } else if (!strcasecmp(action, "toggle_self_service")) {
1296                 toggle_self_service();
1297 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1298         } else if (!strcasecmp(action, "display_edit_task")) {
1299                 display_edit_task();
1300         } else if (!strcasecmp(action, "save_task")) {
1301                 save_task();
1302         } else if (!strcasecmp(action, "display_edit_event")) {
1303                 display_edit_event();
1304         } else if (!strcasecmp(action, "save_event")) {
1305                 save_event();
1306         } else if (!strcasecmp(action, "respond_to_request")) {
1307                 respond_to_request();
1308         } else if (!strcasecmp(action, "handle_rsvp")) {
1309                 handle_rsvp();
1310 #endif
1311         } else if (!strcasecmp(action, "summary")) {
1312                 summary();
1313         } else if (!strcasecmp(action, "iconbar")) {
1314                 do_iconbar();
1315         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1316                 display_customize_iconbar();
1317         } else if (!strcasecmp(action, "commit_iconbar")) {
1318                 commit_iconbar();
1319         } else if (!strcasecmp(action, "set_room_policy")) {
1320                 set_room_policy();
1321         } else if (!strcasecmp(action, "display_inetconf")) {
1322                 display_inetconf();
1323         } else if (!strcasecmp(action, "save_inetconf")) {
1324                 save_inetconf();
1325         } else if (!strcasecmp(action, "diagnostics")) {
1326                 output_headers(1, 1, 1, 0, 0, 0, 0);
1327
1328                 wprintf("You're in session %d<hr />\n", WC->wc_session);
1329                 wprintf("Command: <br /><PRE>\n");
1330                 escputs(cmd);
1331                 wprintf("</PRE><hr />\n");
1332                 wprintf("Variables: <br /><PRE>\n");
1333                 dump_vars();
1334                 wprintf("</PRE><hr />\n");
1335                 wDumpContent(1);
1336         }
1337         /* When all else fais, display the main menu. */
1338         else {
1339                 display_main_menu();
1340         }
1341
1342 SKIP_ALL_THIS_CRAP:
1343         fflush(stdout);
1344         if (content != NULL) {
1345                 free(content);
1346                 content = NULL;
1347         }
1348         free_urls();
1349         if (WC->upload_length > 0) {
1350                 free(WC->upload);
1351                 WC->upload_length = 0;
1352         }
1353
1354 }