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