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.
17 #include <sys/types.h>
19 #include <sys/socket.h>
23 #include <netinet/in.h>
35 char *ExpressMessages = NULL; /* FIX move to session context */
37 /* This variable is set to 1 if the room banner and menubar have been
38 * displayed, and we need to close the <TABLE> tags.
40 int fake_frames = 0; /* FIX move to session context */
42 struct urlcontent *urlstrings = NULL;
45 void unescape_input(char *buf)
50 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
51 buf[strlen(buf) - 1] = 0;
53 for (a = 0; a < strlen(buf); ++a) {
60 sscanf(hex, "%02x", &b);
62 strcpy(&buf[a + 1], &buf[a + 3]);
69 void addurls(char *url)
77 while (strlen(up) > 0) {
79 /* locate the = sign */
80 strncpy(buf, up, 255);
82 for (a = 255; a >= 0; --a)
89 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
92 strcpy(u->url_key, buf);
94 /* now chop that part off */
95 for (a = 0; a <= b; ++a)
98 /* locate the & sign */
101 for (a = 0; a < strlen(up); ++a) {
102 if (!strncmp(ptr, "&", 1)) {
109 for (a = 0; a < b; ++a)
113 u->url_data = malloc(strlen(up) + 1);
114 strcpy(u->url_data, up);
116 unescape_input(u->url_data);
124 struct urlcontent *u;
126 while (urlstrings != NULL) {
127 free(urlstrings->url_data);
128 u = urlstrings->next;
135 * Diagnostic function to display the contents of all variables
139 struct urlcontent *u;
141 for (u = urlstrings; u != NULL; u = u->next) {
142 wprintf("%38s = %s\n", u->url_key, u->url_data);
146 char *bstr(char *key)
148 struct urlcontent *u;
150 for (u = urlstrings; u != NULL; u = u->next) {
151 if (!strcasecmp(u->url_key, key))
152 return (u->url_data);
158 void wprintf(const char *format,...)
163 va_start(arg_ptr, format);
164 vsprintf(wbuf, format, arg_ptr);
167 write(WC->http_sock, wbuf, strlen(wbuf));
172 * wDumpContent() wraps up an HTTP session, closes tags, etc.
174 * print_standard_html_footer should be set to 0 to transmit only, 1 to
175 * append the main menu and closing tags, or 2 to
176 * append the closing tags only.
178 void wDumpContent(int print_standard_html_footer)
181 wprintf("<CENTER><FONT SIZE=-1>"
182 "<TABLE border=0 width=100%><TR>"
183 "<TD><A HREF=\"/ungoto\">"
184 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
186 wprintf("<TD><A HREF=\"#TheTop\">"
187 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
188 "Top of page</A></TD>");
189 wprintf("<TD><A HREF=\"/display_enter\">"
190 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
191 "Enter a message</A></TD>");
192 wprintf("<TD><A HREF=\"/gotonext\">"
194 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
197 "</TD></TR></TABLE></TABLE>\n");
201 if (print_standard_html_footer) {
202 if (print_standard_html_footer != 2) {
205 wprintf("</BODY></HTML>\n");
212 void escputs1(char *strbuf, int nbsp)
216 for (a = 0; a < strlen(strbuf); ++a) {
217 if (strbuf[a] == '<')
219 else if (strbuf[a] == '>')
221 else if (strbuf[a] == '&')
223 else if (strbuf[a] == '\"')
225 else if (strbuf[a] == '\'')
227 else if (strbuf[a] == LB)
229 else if (strbuf[a] == RB)
231 else if (strbuf[a] == QU)
233 else if ((strbuf[a] == 32) && (nbsp == 1)) {
236 wprintf("%c", strbuf[a]);
241 void escputs(char *strbuf)
248 char *urlesc(char *strbuf)
251 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
252 static char outbuf[512];
256 for (a = 0; a < strlen(strbuf); ++a) {
258 for (b = 0; b < strlen(ec); ++b) {
259 if (strbuf[a] == ec[b])
264 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
266 sprintf(&outbuf[b], "%c", strbuf[a]);
271 void urlescputs(char *strbuf)
273 wprintf("%s", urlesc(strbuf));
280 * Output all that important stuff that the browser will want to see
282 * print_standard_html_head values:
283 * 0 = Nothing. Do not display any leading HTTP or HTML.
284 * 1 = HTTP headers plus the "fake frames" found in most windows.
285 * 2 = HTTP headers required to terminate the session (unset cookies)
286 * 3 = HTTP headers only.
288 void output_headers(int print_standard_html_head)
291 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
294 wprintf("HTTP/1.0 200 OK\n");
296 if (print_standard_html_head > 0) {
297 wprintf("Content-type: text/html\n");
298 wprintf("Server: %s\n", SERVER);
299 wprintf("Connection: close\n");
300 wprintf("Pragma: no-cache\n");
301 wprintf("Cache-Control: no-store\n");
303 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
304 WC->wc_password, WC->wc_roomname);
305 if (print_standard_html_head == 2) {
306 wprintf("X-WebCit-Session: close\n");
307 wprintf("Set-cookie: webcit=%s\n", unset);
309 wprintf("Set-cookie: webcit=%s\n", cookie);
312 if (print_standard_html_head > 0) {
314 wprintf("<HTML><HEAD><TITLE>");
315 escputs(serv_info.serv_humannode);
317 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
319 if (ExpressMessages != NULL) {
320 wprintf("<SCRIPT language=\"javascript\">\n");
321 wprintf("function ExpressMessage() {\n");
322 wprintf(" alert(\"");
323 escputs(ExpressMessages);
325 wprintf(" }\n </SCRIPT>\n");
330 /* JavaScript key-based navigation would go here if it
335 if (ExpressMessages != NULL) {
336 wprintf("onload=\"ExpressMessage()\" ");
337 free(ExpressMessages);
338 ExpressMessages = NULL;
340 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
343 if (print_standard_html_head == 1) {
344 wprintf("<A NAME=\"TheTop\"></A>"
345 "<TABLE border=0 width=100%>"
346 "<TR VALIGN=TOP><TD>");
350 wprintf("</TD><TD VALIGN=TOP>"
351 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
354 embed_room_banner(NULL);
356 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
365 void ExpressMessageCat(char *buf) {
366 if (ExpressMessages == NULL) {
367 ExpressMessages = malloc(strlen(buf) + 4);
368 strcpy(ExpressMessages, "");
370 ExpressMessages = realloc(ExpressMessages,
371 (strlen(ExpressMessages) + strlen(buf) + 4));
373 strcat(ExpressMessages, buf);
374 strcat(ExpressMessages, "\\n");
378 void check_for_express_messages()
385 while (serv_gets(buf), strcmp(buf, "000")) {
386 ExpressMessageCat(buf);
394 void output_static(char *what)
402 sprintf(buf, "static/%s", what);
403 fp = fopen(buf, "rb");
405 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
406 wprintf("Content-Type: text/plain\n");
408 wprintf("Cannot open %s: %s\n", what, strerror(errno));
412 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
413 wprintf("Content-type: image/gif\n");
414 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
415 wprintf("Content-type: text/plain\n");
416 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
417 wprintf("Content-type: image/jpeg\n");
418 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
419 wprintf("Content-type: text/html\n");
421 wprintf("Content-type: application/octet-stream\n");
423 fstat(fileno(fp), &statbuf);
424 bytes = statbuf.st_size;
425 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
426 wprintf("Content-length: %ld\n", (long) bytes);
429 thisblock = sizeof(buf);
430 if (thisblock > bytes) thisblock = bytes;
431 fread(buf, thisblock, 1, fp);
432 write(WC->http_sock, buf, thisblock);
433 bytes = bytes - thisblock;
440 * When the browser requests an image file from the Citadel server,
441 * this function is called to transmit it.
449 off_t accomplished = 0L;
452 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
455 bytes = extract_long(&buf[4], 0);
457 wprintf("Content-type: image/gif\n");
458 wprintf("Content-length: %ld\n", (long) bytes);
461 while (bytes > (off_t) 0) {
462 thisblock = (off_t) sizeof(xferbuf);
463 if (thisblock > bytes)
465 serv_printf("READ %ld|%ld", accomplished, thisblock);
468 thisblock = extract_long(&buf[4], 0);
469 serv_read(xferbuf, (int) thisblock);
470 write(WC->http_sock, xferbuf, thisblock);
471 bytes = bytes - thisblock;
472 accomplished = accomplished + thisblock;
477 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
479 wprintf("Content-Type: text/plain\n");
481 wprintf("Error retrieving image\n");
488 * Convenience functions to display a page containing only a string
490 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
492 wprintf("HTTP/1.0 200 OK\n");
494 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
495 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
496 wprintf("<B>%s</B>\n", titlebarmsg);
497 wprintf("</FONT></TD></TR></TABLE><BR>\n");
498 escputs(messagetext);
505 void display_error(char *errormessage)
507 convenience_page("770000", "Error", errormessage);
510 void display_success(char *successmessage)
512 convenience_page("007700", "OK", successmessage);
517 void extract_action(char *actbuf, char *cmdbuf)
521 strcpy(actbuf, cmdbuf);
522 if (!strncasecmp(actbuf, "GET /", 5))
523 strcpy(actbuf, &actbuf[5]);
524 if (!strncasecmp(actbuf, "PUT /", 5))
525 strcpy(actbuf, &actbuf[5]);
526 if (!strncasecmp(actbuf, "POST /", 6))
527 strcpy(actbuf, &actbuf[6]);
529 for (i = 0; i < strlen(actbuf); ++i) {
530 if (actbuf[i] == ' ') {
534 if (actbuf[i] == '/') {
538 if (actbuf[i] == '?') {
542 if (actbuf[i] == '&') {
550 void upload_handler(char *name, char *filename, char *encoding,
551 void *content, char *cbtype, size_t length)
554 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
555 fprintf(stderr, " name = %s\n", name);
556 fprintf(stderr, "filename = %s\n", filename);
557 fprintf(stderr, "encoding = %s\n", encoding);
558 fprintf(stderr, " type = %s\n", cbtype);
559 fprintf(stderr, " length = %d\n", length);
561 if (strlen(name) > 0) {
562 WC->upload = malloc(length);
563 if (WC->upload != NULL) {
564 WC->upload_length = length;
565 memcpy(WC->upload, content, length);
572 * Entry point for WebCit transaction
574 void session_loop(struct httprequest *req)
580 int ContentLength = 0;
581 char ContentType[512];
583 struct httprequest *hptr;
584 char browser_host[256];
585 char user_agent[256];
587 /* We stuff these with the values coming from the client cookies,
588 * so we can use them to reconnect a timed out session if we have to.
592 char c_username[256];
593 char c_password[256];
594 char c_roomname[256];
597 strcpy(c_host, defaulthost);
598 strcpy(c_port, defaultport);
599 strcpy(c_username, "");
600 strcpy(c_password, "");
601 strcpy(c_roomname, "");
603 WC->upload_length = 0;
607 if (hptr == NULL) return;
609 strcpy(cmd, hptr->line);
611 extract_action(action, cmd);
613 while (hptr != NULL) {
614 strcpy(buf, hptr->line);
617 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
618 strcpy(cookie, &buf[15]);
619 cookie_to_stuff(cookie, NULL,
620 c_username, c_password, c_roomname);
622 else if (!strncasecmp(buf, "Content-length: ", 16)) {
623 ContentLength = atoi(&buf[16]);
625 else if (!strncasecmp(buf, "Content-type: ", 14)) {
626 strcpy(ContentType, &buf[14]);
628 else if (!strncasecmp(buf, "User-agent: ", 12)) {
629 strcpy(user_agent, &buf[12]);
633 if (ContentLength > 0) {
634 content = malloc(ContentLength + 1);
635 read(WC->http_sock, content, ContentLength);
637 content[ContentLength] = 0;
639 if (!strncasecmp(ContentType,
640 "application/x-www-form-urlencoded", 33)) {
642 } else if (!strncasecmp(ContentType, "multipart", 9)) {
643 mime_parser(content, ContentLength, ContentType,
650 /* If there are variables in the URL, we must grab them now */
651 for (a = 0; a < strlen(cmd); ++a)
652 if ((cmd[a] == '?') || (cmd[a] == '&')) {
653 for (b = a; b < strlen(cmd); ++b)
656 addurls(&cmd[a + 1]);
660 * If we're not connected to a Citadel server, try to hook up the
661 * connection now. Preference is given to the host and port specified
662 * by browser cookies, if cookies have been supplied.
664 if (!WC->connected) {
665 if (strlen(bstr("host")) > 0)
666 strcpy(c_host, bstr("host"));
667 if (strlen(bstr("port")) > 0)
668 strcpy(c_port, bstr("port"));
669 WC->serv_sock = connectsock(c_host, c_port, "tcp");
670 if (WC->serv_sock < 0) {
675 serv_gets(buf); /* get the server welcome message */
676 locate_host(browser_host, WC->http_sock);
677 get_serv_info(browser_host, user_agent);
679 check_for_express_messages();
682 * If we're not logged in, but we have username and password cookies
683 * supplied by the browser, try using them to log in.
685 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
686 serv_printf("USER %s", c_username);
689 serv_printf("PASS %s", c_password);
692 become_logged_in(c_username, c_password, buf);
697 * If we don't have a current room, but a cookie specifying the
698 * current room is supplied, make an effort to go there.
700 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
701 serv_printf("GOTO %s", c_roomname);
704 strcpy(WC->wc_roomname, c_roomname);
707 if (!strcasecmp(action, "static")) {
708 strcpy(buf, &cmd[12]);
709 for (a = 0; a < strlen(buf); ++a)
713 } else if (!strcasecmp(action, "image")) {
715 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
717 } else if (!WC->logged_in) {
720 /* Various commands... */
722 else if (!strcasecmp(action, "do_welcome")) {
724 } else if (!strcasecmp(action, "display_main_menu")) {
726 } else if (!strcasecmp(action, "advanced")) {
727 display_advanced_menu();
728 } else if (!strcasecmp(action, "whobbs")) {
730 } else if (!strcasecmp(action, "knrooms")) {
731 list_all_rooms_by_floor();
732 } else if (!strcasecmp(action, "gotonext")) {
735 } else if (!strcasecmp(action, "skip")) {
737 } else if (!strcasecmp(action, "ungoto")) {
739 } else if (!strcasecmp(action, "dotgoto")) {
741 smart_goto(bstr("room"));
742 } else if (!strcasecmp(action, "termquit")) {
744 } else if (!strcasecmp(action, "readnew")) {
746 } else if (!strcasecmp(action, "readold")) {
748 } else if (!strcasecmp(action, "readfwd")) {
750 } else if (!strcasecmp(action, "display_enter")) {
752 } else if (!strcasecmp(action, "post")) {
754 } else if (!strcasecmp(action, "confirm_delete_msg")) {
755 confirm_delete_msg();
756 } else if (!strcasecmp(action, "delete_msg")) {
758 } else if (!strcasecmp(action, "confirm_move_msg")) {
760 } else if (!strcasecmp(action, "move_msg")) {
762 } else if (!strcasecmp(action, "userlist")) {
764 } else if (!strcasecmp(action, "showuser")) {
766 } else if (!strcasecmp(action, "display_page")) {
768 } else if (!strcasecmp(action, "page_user")) {
770 } else if (!strcasecmp(action, "chat")) {
772 } else if (!strcasecmp(action, "display_private")) {
773 display_private("", 0);
774 } else if (!strcasecmp(action, "goto_private")) {
776 } else if (!strcasecmp(action, "zapped_list")) {
778 } else if (!strcasecmp(action, "display_zap")) {
780 } else if (!strcasecmp(action, "zap")) {
782 } else if (!strcasecmp(action, "display_entroom")) {
784 } else if (!strcasecmp(action, "entroom")) {
786 } else if (!strcasecmp(action, "display_editroom")) {
788 } else if (!strcasecmp(action, "editroom")) {
790 } else if (!strcasecmp(action, "display_editinfo")) {
791 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
792 } else if (!strcasecmp(action, "editinfo")) {
793 save_edit("Room info", "EINF 1", 1);
794 } else if (!strcasecmp(action, "display_editbio")) {
795 sprintf(buf, "RBIO %s", WC->wc_username);
796 display_edit("Your bio", "NOOP", buf, "editbio");
797 } else if (!strcasecmp(action, "editbio")) {
798 save_edit("Your bio", "EBIO", 0);
799 } else if (!strcasecmp(action, "confirm_delete_room")) {
800 confirm_delete_room();
801 } else if (!strcasecmp(action, "delete_room")) {
803 } else if (!strcasecmp(action, "validate")) {
805 } else if (!strcasecmp(action, "display_editpic")) {
806 display_graphics_upload("your photo",
809 } else if (!strcasecmp(action, "editpic")) {
810 do_graphics_upload("UIMG 1|_userpic_");
811 } else if (!strcasecmp(action, "display_editroompic")) {
812 display_graphics_upload("the graphic for this room",
815 } else if (!strcasecmp(action, "editroompic")) {
816 do_graphics_upload("UIMG 1|_roompic_");
817 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
818 select_floor_to_edit_pic();
819 } else if (!strcasecmp(action, "display_editfloorpic")) {
820 sprintf(buf, "UIMG 0|_floorpic_|%s",
821 bstr("which_floor"));
822 display_graphics_upload("the graphic for this floor",
825 } else if (!strcasecmp(action, "editfloorpic")) {
826 sprintf(buf, "UIMG 1|_floorpic_|%s",
827 bstr("which_floor"));
828 do_graphics_upload(buf);
829 } else if (!strcasecmp(action, "display_reg")) {
831 } else if (!strcasecmp(action, "register")) {
833 } else if (!strcasecmp(action, "display_changepw")) {
835 } else if (!strcasecmp(action, "changepw")) {
837 } else if (!strcasecmp(action, "display_edit_node")) {
839 } else if (!strcasecmp(action, "display_netconf")) {
841 } else if (!strcasecmp(action, "display_confirm_unshare")) {
842 display_confirm_unshare();
843 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
844 display_confirm_delete_node();
845 } else if (!strcasecmp(action, "delete_node")) {
847 } else if (!strcasecmp(action, "unshare")) {
849 } else if (!strcasecmp(action, "display_add_node")) {
851 } else if (!strcasecmp(action, "add_node")) {
853 } else if (!strcasecmp(action, "display_share")) {
855 } else if (!strcasecmp(action, "share")) {
857 } else if (!strcasecmp(action, "terminate_session")) {
860 } else if (!strcasecmp(action, "edit_me")) {
862 } else if (!strcasecmp(action, "display_siteconfig")) {
863 display_siteconfig();
864 } else if (!strcasecmp(action, "siteconfig")) {
866 } else if (!strcasecmp(action, "display_generic")) {
868 } else if (!strcasecmp(action, "do_generic")) {
870 } else if (!strcasecmp(action, "display_menubar")) {
872 } else if (!strcasecmp(action, "diagnostics")) {
875 wprintf("You're in session %d<HR>\n", WC->wc_session);
876 wprintf("Command: <BR><PRE>\n");
878 wprintf("</PRE><HR>\n");
879 wprintf("Variables: <BR><PRE>\n");
881 wprintf("</PRE><HR>\n");
884 /* When all else fais, display the main menu. */
890 if (content != NULL) {
895 if (WC->upload_length > 0) {
897 WC->upload_length = 0;