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.
23 #include "mime_parser.h"
25 int TransactionCount = 0;
26 char *ExpressMessages = NULL;
28 /* This variable is set to 1 if the room banner and menubar have been
29 * displayed, and we need to close the <TABLE> tags.
33 struct webcontent *wlist = NULL;
34 struct webcontent *wlast = NULL;
36 struct urlcontent *urlstrings = NULL;
38 static const char *defaulthost = DEFAULT_HOST;
39 static const char *defaultport = DEFAULT_PORT;
43 struct wcsession *WC = NULL; /* FIX take this out when multithreaded */
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,...)
161 struct webcontent *wptr;
163 wptr = (struct webcontent *) malloc(sizeof(struct webcontent));
173 va_start(arg_ptr, format);
174 vsprintf(wptr->w_data, format, arg_ptr);
178 int wContentLength(void)
180 struct webcontent *wptr;
183 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
184 len = len + strlen(wptr->w_data);
191 * wDumpContent() takes all the stuff that's been queued up using
192 * the wprintf() and escputs() functions, and sends it out to the browser.
193 * By queuing instead of transmitting as it's generated, we're able to
194 * calculate a Content-length: header.
196 * print_standard_html_footer should be set to 0 to transmit only, 1 to
197 * append the main menu and closing tags, or 2 to
198 * append the closing tags only.
200 void wDumpContent(int print_standard_html_footer)
202 struct webcontent *wptr;
205 wprintf("<CENTER><FONT SIZE=-1>"
206 "<TABLE border=0 width=100%><TR>"
207 "<TD><A HREF=\"/ungoto\">"
208 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
210 wprintf("<TD><A HREF=\"#TheTop\">"
211 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
212 "Top of page</A></TD>");
213 wprintf("<TD><A HREF=\"/display_enter\">"
214 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
215 "Enter a message</A></TD>");
216 wprintf("<TD><A HREF=\"/gotonext\">"
218 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
221 "</TD></TR></TABLE></TABLE>\n");
225 if (print_standard_html_footer) {
226 if (print_standard_html_footer != 2) {
229 wprintf("</BODY></HTML>\n");
231 printf("Content-type: text/html\n");
232 printf("Content-length: %d\n", wContentLength());
235 while (wlist != NULL) {
236 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
245 void escputs1(char *strbuf, int nbsp)
249 for (a = 0; a < strlen(strbuf); ++a) {
250 if (strbuf[a] == '<')
252 else if (strbuf[a] == '>')
254 else if (strbuf[a] == '&')
256 else if (strbuf[a] == '\"')
258 else if (strbuf[a] == '\'')
260 else if (strbuf[a] == LB)
262 else if (strbuf[a] == RB)
264 else if (strbuf[a] == QU)
266 else if ((strbuf[a] == 32) && (nbsp == 1)) {
269 wprintf("%c", strbuf[a]);
274 void escputs(char *strbuf)
281 char *urlesc(char *strbuf)
284 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
285 static char outbuf[512];
289 for (a = 0; a < strlen(strbuf); ++a) {
291 for (b = 0; b < strlen(ec); ++b) {
292 if (strbuf[a] == ec[b])
297 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
299 sprintf(&outbuf[b], "%c", strbuf[a]);
304 void urlescputs(char *strbuf)
306 wprintf("%s", urlesc(strbuf));
313 * Get a line of text from the webserver (which originally came from the
314 * user's browser), checking for sanity etc.
316 char *getz(char *buf)
322 /* If fgets() fails, it's because the webserver crashed, so kill off
325 if (fgets(buf, 256, stdin) == NULL) {
327 fprintf(stderr, "webcit: exit code %d (%s)\n",
332 /* Otherwise, strip out nonprintables and resume our happy day.
335 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
336 buf[strlen(buf) - 1] = 0;
344 * Output all that important stuff that the browser will want to see
346 * print_standard_html_head values:
347 * 0 = Nothing. Do not display any leading HTTP or HTML.
348 * 1 = HTTP headers plus the "fake frames" found in most windows.
349 * 2 = HTTP headers required to terminate the session (unset cookies)
350 * 3 = HTTP headers only.
352 void output_headers(int print_standard_html_head)
355 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
358 printf("Server: %s\n", SERVER);
359 printf("Connection: close\n");
361 if (print_standard_html_head > 0) {
362 printf("Pragma: no-cache\n");
363 printf("Cache-Control: no-store\n");
365 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
366 WC->wc_password, WC->wc_roomname);
367 if (print_standard_html_head == 2) {
368 printf("X-WebCit-Session: close\n");
369 printf("Set-cookie: webcit=%s\n", unset);
371 printf("Set-cookie: webcit=%s\n", cookie);
374 if (print_standard_html_head > 0) {
375 wprintf("<HTML><HEAD><TITLE>");
376 escputs(serv_info.serv_humannode);
378 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
380 if (ExpressMessages != NULL) {
381 wprintf("<SCRIPT language=\"javascript\">\n");
382 wprintf("function ExpressMessage() {\n");
383 wprintf(" alert(\"");
384 escputs(ExpressMessages);
386 wprintf(" }\n </SCRIPT>\n");
391 /* JavaScript key-based navigation would go here if it
396 if (ExpressMessages != NULL) {
397 wprintf("onload=\"ExpressMessage()\" ");
398 free(ExpressMessages);
399 ExpressMessages = NULL;
401 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
404 if (print_standard_html_head == 1) {
405 wprintf("<A NAME=\"TheTop\"></A>"
406 "<TABLE border=0 width=100%>"
407 "<TR VALIGN=TOP><TD>");
411 wprintf("</TD><TD VALIGN=TOP>"
412 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
415 embed_room_banner(NULL);
417 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
426 void ExpressMessageCat(char *buf) {
427 if (ExpressMessages == NULL) {
428 ExpressMessages = malloc(strlen(buf) + 4);
429 strcpy(ExpressMessages, "");
431 ExpressMessages = realloc(ExpressMessages,
432 (strlen(ExpressMessages) + strlen(buf) + 4));
434 strcat(ExpressMessages, buf);
435 strcat(ExpressMessages, "\\n");
439 void check_for_express_messages()
446 while (serv_gets(buf), strcmp(buf, "000")) {
447 ExpressMessageCat(buf);
455 void output_static(char *what)
462 sprintf(buf, "static/%s", what);
463 fp = fopen(buf, "rb");
465 printf("HTTP/1.0 404 %s\n", strerror(errno));
467 printf("Content-Type: text/plain\n");
468 sprintf(buf, "%s: %s\n", what, strerror(errno));
469 printf("Content-length: %d\n", strlen(buf));
471 fwrite(buf, strlen(buf), 1, stdout);
473 printf("HTTP/1.0 200 OK\n");
476 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
477 printf("Content-type: image/gif\n");
478 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
479 printf("Content-type: text/plain\n");
480 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
481 printf("Content-type: image/jpeg\n");
482 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
483 printf("Content-type: text/html\n");
485 printf("Content-type: application/octet-stream\n");
487 fstat(fileno(fp), &statbuf);
488 bytes = statbuf.st_size;
489 printf("Content-length: %ld\n", (long) bytes);
492 putc(getc(fp), stdout);
505 off_t accomplished = 0L;
508 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
511 bytes = extract_long(&buf[4], 0);
512 printf("HTTP/1.0 200 OK\n");
514 printf("Content-type: image/gif\n");
515 printf("Content-length: %ld\n", (long) bytes);
518 while (bytes > (off_t) 0) {
519 thisblock = (off_t) sizeof(xferbuf);
520 if (thisblock > bytes)
522 serv_printf("READ %ld|%ld", accomplished, thisblock);
525 thisblock = extract_long(&buf[4], 0);
526 serv_read(xferbuf, (int) thisblock);
527 fwrite(xferbuf, thisblock, 1, stdout);
528 bytes = bytes - thisblock;
529 accomplished = accomplished + thisblock;
535 printf("HTTP/1.0 404 %s\n", strerror(errno));
537 printf("Content-Type: text/plain\n");
538 sprintf(buf, "Error retrieving image\n");
539 printf("Content-length: %d\n", strlen(buf));
541 fwrite(buf, strlen(buf), 1, stdout);
548 * Convenience functions to display a page containing only a string
550 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
552 printf("HTTP/1.0 200 OK\n");
554 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
555 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
556 wprintf("<B>%s</B>\n", titlebarmsg);
557 wprintf("</FONT></TD></TR></TABLE><BR>\n");
558 escputs(messagetext);
565 void display_error(char *errormessage)
567 convenience_page("770000", "Error", errormessage);
570 void display_success(char *successmessage)
572 convenience_page("007700", "OK", successmessage);
577 void extract_action(char *actbuf, char *cmdbuf)
581 strcpy(actbuf, cmdbuf);
582 if (!strncasecmp(actbuf, "GET /", 5))
583 strcpy(actbuf, &actbuf[5]);
584 if (!strncasecmp(actbuf, "PUT /", 5))
585 strcpy(actbuf, &actbuf[5]);
586 if (!strncasecmp(actbuf, "POST /", 6))
587 strcpy(actbuf, &actbuf[6]);
589 for (i = 0; i < strlen(actbuf); ++i) {
590 if (actbuf[i] == ' ') {
594 if (actbuf[i] == '/') {
598 if (actbuf[i] == '?') {
602 if (actbuf[i] == '&') {
610 void upload_handler(char *name, char *filename, char *encoding,
611 void *content, char *cbtype, size_t length)
614 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
615 fprintf(stderr, " name = %s\n", name);
616 fprintf(stderr, "filename = %s\n", filename);
617 fprintf(stderr, "encoding = %s\n", encoding);
618 fprintf(stderr, " type = %s\n", cbtype);
619 fprintf(stderr, " length = %d\n", length);
621 if (strlen(name) > 0) {
622 WC->upload = malloc(length);
623 if (WC->upload != NULL) {
624 WC->upload_length = length;
625 memcpy(WC->upload, content, length);
631 void session_loop(char *browser_host, char *user_agent)
637 int ContentLength = 0;
638 char ContentType[512];
641 /* We stuff these with the values coming from the client cookies,
642 * so we can use them to reconnect a timed out session if we have to.
646 char c_username[256];
647 char c_password[256];
648 char c_roomname[256];
651 strcpy(c_host, defaulthost);
652 strcpy(c_port, defaultport);
653 strcpy(c_username, "");
654 strcpy(c_password, "");
655 strcpy(c_roomname, "");
657 WC->upload_length = 0;
660 if (getz(cmd) == NULL)
662 extract_action(action, cmd);
665 if (getz(buf) == NULL)
668 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
669 strcpy(cookie, &buf[15]);
670 cookie_to_stuff(cookie, NULL,
671 c_username, c_password, c_roomname);
673 if (!strncasecmp(buf, "Content-length: ", 16)) {
674 ContentLength = atoi(&buf[16]);
676 if (!strncasecmp(buf, "Content-type: ", 14)) {
677 strcpy(ContentType, &buf[14]);
679 } while (strlen(buf) > 0);
683 if (ContentLength > 0) {
684 content = malloc(ContentLength + 1);
685 fread(content, ContentLength, 1, stdin);
687 content[ContentLength] = 0;
689 if (!strncasecmp(ContentType,
690 "application/x-www-form-urlencoded", 33)) {
692 } else if (!strncasecmp(ContentType, "multipart", 9)) {
693 mime_parser(content, ContentLength, ContentType,
700 /* If there are variables in the URL, we must grab them now */
701 for (a = 0; a < strlen(cmd); ++a)
702 if ((cmd[a] == '?') || (cmd[a] == '&')) {
703 for (b = a; b < strlen(cmd); ++b)
706 addurls(&cmd[a + 1]);
710 * If we're not WC->connected to a Citadel server, try to hook up the
711 * connection now. Preference is given to the host and port specified
712 * by browser cookies, if cookies have been supplied.
714 if (!WC->connected) {
715 if (strlen(bstr("host")) > 0)
716 strcpy(c_host, bstr("host"));
717 if (strlen(bstr("port")) > 0)
718 strcpy(c_port, bstr("port"));
719 WC->serv_sock = connectsock(c_host, c_port, "tcp");
720 if (WC->serv_sock < 0) {
725 serv_gets(buf); /* get the server welcome message */
726 get_serv_info(browser_host, user_agent);
728 check_for_express_messages();
731 * If we're not logged in, but we have username and password cookies
732 * supplied by the browser, try using them to log in.
734 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
735 serv_printf("USER %s", c_username);
738 serv_printf("PASS %s", c_password);
741 become_logged_in(c_username, c_password, buf);
746 * If we don't have a current room, but a cookie specifying the
747 * current room is supplied, make an effort to go there.
749 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
750 serv_printf("GOTO %s", c_roomname);
753 strcpy(WC->wc_roomname, c_roomname);
756 if (!strcasecmp(action, "static")) {
757 strcpy(buf, &cmd[12]);
758 for (a = 0; a < strlen(buf); ++a)
762 } else if (!strcasecmp(action, "image")) {
764 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
766 } else if (!WC->logged_in) {
769 /* Various commands... */
771 else if (!strcasecmp(action, "do_welcome")) {
773 } else if (!strcasecmp(action, "display_main_menu")) {
775 } else if (!strcasecmp(action, "advanced")) {
776 display_advanced_menu();
777 } else if (!strcasecmp(action, "whobbs")) {
779 } else if (!strcasecmp(action, "knrooms")) {
780 list_all_rooms_by_floor();
781 } else if (!strcasecmp(action, "gotonext")) {
784 } else if (!strcasecmp(action, "skip")) {
786 } else if (!strcasecmp(action, "ungoto")) {
788 } else if (!strcasecmp(action, "dotgoto")) {
790 smart_goto(bstr("room"));
791 } else if (!strcasecmp(action, "termquit")) {
793 } else if (!strcasecmp(action, "readnew")) {
795 } else if (!strcasecmp(action, "readold")) {
797 } else if (!strcasecmp(action, "readfwd")) {
799 } else if (!strcasecmp(action, "display_enter")) {
801 } else if (!strcasecmp(action, "post")) {
803 } else if (!strcasecmp(action, "confirm_delete_msg")) {
804 confirm_delete_msg();
805 } else if (!strcasecmp(action, "delete_msg")) {
807 } else if (!strcasecmp(action, "confirm_move_msg")) {
809 } else if (!strcasecmp(action, "move_msg")) {
811 } else if (!strcasecmp(action, "userlist")) {
813 } else if (!strcasecmp(action, "showuser")) {
815 } else if (!strcasecmp(action, "display_page")) {
817 } else if (!strcasecmp(action, "page_user")) {
819 } else if (!strcasecmp(action, "chat")) {
821 } else if (!strcasecmp(action, "display_private")) {
822 display_private("", 0);
823 } else if (!strcasecmp(action, "goto_private")) {
825 } else if (!strcasecmp(action, "zapped_list")) {
827 } else if (!strcasecmp(action, "display_zap")) {
829 } else if (!strcasecmp(action, "zap")) {
831 } else if (!strcasecmp(action, "display_entroom")) {
833 } else if (!strcasecmp(action, "entroom")) {
835 } else if (!strcasecmp(action, "display_editroom")) {
837 } else if (!strcasecmp(action, "editroom")) {
839 } else if (!strcasecmp(action, "display_editinfo")) {
840 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
841 } else if (!strcasecmp(action, "editinfo")) {
842 save_edit("Room info", "EINF 1", 1);
843 } else if (!strcasecmp(action, "display_editbio")) {
844 sprintf(buf, "RBIO %s", WC->wc_username);
845 display_edit("Your bio", "NOOP", buf, "editbio");
846 } else if (!strcasecmp(action, "editbio")) {
847 save_edit("Your bio", "EBIO", 0);
848 } else if (!strcasecmp(action, "confirm_delete_room")) {
849 confirm_delete_room();
850 } else if (!strcasecmp(action, "delete_room")) {
852 } else if (!strcasecmp(action, "validate")) {
854 } else if (!strcasecmp(action, "display_editpic")) {
855 display_graphics_upload("your photo",
858 } else if (!strcasecmp(action, "editpic")) {
859 do_graphics_upload("UIMG 1|_userpic_");
860 } else if (!strcasecmp(action, "display_editroompic")) {
861 display_graphics_upload("the graphic for this room",
864 } else if (!strcasecmp(action, "editroompic")) {
865 do_graphics_upload("UIMG 1|_roompic_");
866 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
867 select_floor_to_edit_pic();
868 } else if (!strcasecmp(action, "display_editfloorpic")) {
869 sprintf(buf, "UIMG 0|_floorpic_|%s",
870 bstr("which_floor"));
871 display_graphics_upload("the graphic for this floor",
874 } else if (!strcasecmp(action, "editfloorpic")) {
875 sprintf(buf, "UIMG 1|_floorpic_|%s",
876 bstr("which_floor"));
877 do_graphics_upload(buf);
878 } else if (!strcasecmp(action, "display_reg")) {
880 } else if (!strcasecmp(action, "register")) {
882 } else if (!strcasecmp(action, "display_changepw")) {
884 } else if (!strcasecmp(action, "changepw")) {
886 } else if (!strcasecmp(action, "display_edit_node")) {
888 } else if (!strcasecmp(action, "display_netconf")) {
890 } else if (!strcasecmp(action, "display_confirm_unshare")) {
891 display_confirm_unshare();
892 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
893 display_confirm_delete_node();
894 } else if (!strcasecmp(action, "delete_node")) {
896 } else if (!strcasecmp(action, "unshare")) {
898 } else if (!strcasecmp(action, "display_add_node")) {
900 } else if (!strcasecmp(action, "add_node")) {
902 } else if (!strcasecmp(action, "display_share")) {
904 } else if (!strcasecmp(action, "share")) {
906 } else if (!strcasecmp(action, "terminate_session")) {
909 } else if (!strcasecmp(action, "edit_me")) {
911 } else if (!strcasecmp(action, "display_siteconfig")) {
912 display_siteconfig();
913 } else if (!strcasecmp(action, "siteconfig")) {
915 } else if (!strcasecmp(action, "display_generic")) {
917 } else if (!strcasecmp(action, "do_generic")) {
919 } else if (!strcasecmp(action, "display_menubar")) {
921 } else if (!strcasecmp(action, "diagnostics")) {
922 printf("HTTP/1.0 200 OK\n");
925 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
926 wprintf("You're in session %d<HR>\n", WC->wc_session);
927 wprintf("Command: <BR><PRE>\n");
929 wprintf("</PRE><HR>\n");
930 wprintf("Variables: <BR><PRE>\n");
932 wprintf("</PRE><HR>\n");
935 /* When all else fais, display the main menu. */
941 if (content != NULL) {
946 if (WC->upload_length > 0) {
948 WC->upload_length = 0;
952 int main(int argc, char *argv[])
960 "webcit: usage error (argc must be 6, not %d)\n",
965 WC = (struct wcsession *) malloc(sizeof(struct wcsession));
966 memset(WC, 0, sizeof(struct wcsession));
970 WC->wc_session = atoi(argv[1]);
971 defaulthost = argv[2];
972 defaultport = argv[3];
974 strcpy(WC->wc_username, "");
975 strcpy(WC->wc_password, "");
976 strcpy(WC->wc_roomname, "");
978 /* Clear out serv_info and temporarily set the value of serv_humannode
979 * to a default value, because it'll be used in HTML page titles
981 memset(&serv_info, 0, sizeof(serv_info));
982 strcpy(serv_info.serv_humannode, "WebCit");
984 strcpy(browser, argv[5]);
985 bd = browser_braindamage_check(browser);
988 session_loop(argv[4], browser);