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"
26 char wc_username[256];
27 char wc_password[256];
28 char wc_roomname[256];
29 int TransactionCount = 0;
33 char *ExpressMessages = NULL;
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.
42 struct webcontent *wlist = NULL;
43 struct webcontent *wlast = NULL;
45 struct urlcontent *urlstrings = NULL;
47 static const char *defaulthost = DEFAULT_HOST;
48 static const char *defaultport = DEFAULT_PORT;
50 int upload_length = 0;
54 void unescape_input(char *buf)
59 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
60 buf[strlen(buf) - 1] = 0;
62 for (a = 0; a < strlen(buf); ++a) {
69 sscanf(hex, "%02x", &b);
71 strcpy(&buf[a + 1], &buf[a + 3]);
78 void addurls(char *url)
86 while (strlen(up) > 0) {
88 /* locate the = sign */
89 strncpy(buf, up, 255);
91 for (a = 255; a >= 0; --a)
98 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
101 strcpy(u->url_key, buf);
103 /* now chop that part off */
104 for (a = 0; a <= b; ++a)
107 /* locate the & sign */
110 for (a = 0; a < strlen(up); ++a) {
111 if (!strncmp(ptr, "&", 1)) {
118 for (a = 0; a < b; ++a)
122 u->url_data = malloc(strlen(up) + 1);
123 strcpy(u->url_data, up);
125 unescape_input(u->url_data);
133 struct urlcontent *u;
135 while (urlstrings != NULL) {
136 free(urlstrings->url_data);
137 u = urlstrings->next;
144 * Diagnostic function to display the contents of all variables
148 struct urlcontent *u;
150 for (u = urlstrings; u != NULL; u = u->next) {
151 wprintf("%38s = %s\n", u->url_key, u->url_data);
155 char *bstr(char *key)
157 struct urlcontent *u;
159 for (u = urlstrings; u != NULL; u = u->next) {
160 if (!strcasecmp(u->url_key, key))
161 return (u->url_data);
167 void wprintf(const char *format,...)
170 struct webcontent *wptr;
172 wptr = (struct webcontent *) malloc(sizeof(struct webcontent));
182 va_start(arg_ptr, format);
183 vsprintf(wptr->w_data, format, arg_ptr);
187 int wContentLength(void)
189 struct webcontent *wptr;
192 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
193 len = len + strlen(wptr->w_data);
200 * wDumpContent() takes all the stuff that's been queued up using
201 * the wprintf() and escputs() functions, and sends it out to the browser.
202 * By queuing instead of transmitting as it's generated, we're able to
203 * calculate a Content-length: header.
205 * print_standard_html_footer should be set to 0 to transmit only, 1 to
206 * append the main menu and closing tags, or 2 to
207 * append the closing tags only.
209 void wDumpContent(int print_standard_html_footer)
211 struct webcontent *wptr;
214 wprintf("<CENTER><FONT SIZE=-1>"
215 "<A HREF=\"/ungoto\">"
216 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
217 "Ungoto</A> ");
218 wprintf("<A HREF=\"#TheTop\">"
219 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
220 "Top of page</A> ");
221 wprintf("<A HREF=\"/gotonext\">"
223 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A>"
225 "</TD></TR></TABLE></TABLE>\n");
229 if (print_standard_html_footer) {
230 if (print_standard_html_footer != 2) {
233 wprintf("</BODY></HTML>\n");
235 printf("Content-type: text/html\n");
236 printf("Content-length: %d\n", wContentLength());
239 while (wlist != NULL) {
240 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
249 void escputs1(char *strbuf, int nbsp)
253 for (a = 0; a < strlen(strbuf); ++a) {
254 if (strbuf[a] == '<')
256 else if (strbuf[a] == '>')
258 else if (strbuf[a] == '&')
260 else if (strbuf[a] == 34)
262 else if (strbuf[a] == LB)
264 else if (strbuf[a] == RB)
266 else if (strbuf[a] == QU)
268 else if ((strbuf[a] == 32) && (nbsp == 1)) {
271 wprintf("%c", strbuf[a]);
276 void escputs(char *strbuf)
283 char *urlesc(char *strbuf)
286 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
287 static char outbuf[512];
291 for (a = 0; a < strlen(strbuf); ++a) {
293 for (b = 0; b < strlen(ec); ++b) {
294 if (strbuf[a] == ec[b])
299 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
301 sprintf(&outbuf[b], "%c", strbuf[a]);
306 void urlescputs(char *strbuf)
308 wprintf("%s", urlesc(strbuf));
315 * Get a line of text from the webserver (which originally came from the
316 * user's browser), checking for sanity etc.
318 char *getz(char *buf)
324 /* If fgets() fails, it's because the webserver crashed, so kill off
327 if (fgets(buf, 256, stdin) == NULL) {
329 fprintf(stderr, "webcit: exit code %d (%s)\n",
334 /* Otherwise, strip out nonprintables and resume our happy day.
337 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
338 buf[strlen(buf) - 1] = 0;
346 * Output all that important stuff that the browser will want to see
348 * print_standard_html_head values:
349 * 0 = Nothing. Do not display any leading HTTP or HTML.
350 * 1 = HTTP headers plus the "fake frames" found in most windows.
351 * 2 = HTTP headers required to terminate the session (unset cookies)
352 * 3 = HTTP headers only.
354 void output_headers(int print_standard_html_head)
357 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
360 printf("Server: %s\n", SERVER);
361 printf("Connection: close\n");
363 if (print_standard_html_head > 0) {
364 printf("Pragma: no-cache\n");
365 printf("Cache-Control: no-store\n");
367 stuff_to_cookie(cookie, wc_session, wc_username,
368 wc_password, wc_roomname);
369 if (print_standard_html_head == 2) {
370 printf("X-WebCit-Session: close\n");
371 printf("Set-cookie: webcit=%s\n", unset);
373 printf("Set-cookie: webcit=%s\n", cookie);
376 if (print_standard_html_head > 0) {
377 wprintf("<HTML><HEAD><TITLE>");
378 escputs(serv_info.serv_humannode);
379 wprintf("</TITLE></HEAD>");
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");
389 if (ExpressMessages != NULL) {
390 wprintf("onload=\"ExpressMessage()\" ");
391 free(ExpressMessages);
392 ExpressMessages = NULL;
394 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
397 if (print_standard_html_head == 1) {
398 wprintf("<A NAME=\"TheTop\"></A>"
399 "<TABLE border=0 width=100%>"
400 "<TR VALIGN=TOP><TD>");
404 wprintf("</TD><TD VALIGN=TOP>"
405 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
408 embed_room_banner(NULL);
410 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
419 void ExpressMessageCat(char *buf) {
420 if (ExpressMessages == NULL) {
421 ExpressMessages = malloc(strlen(buf) + 4);
422 strcpy(ExpressMessages, "");
424 ExpressMessages = realloc(ExpressMessages,
425 (strlen(ExpressMessages) + strlen(buf) + 4));
427 strcat(ExpressMessages, buf);
428 strcat(ExpressMessages, "\\n");
432 void check_for_express_messages()
439 while (serv_gets(buf), strcmp(buf, "000")) {
440 ExpressMessageCat(buf);
448 void output_static(char *what)
455 sprintf(buf, "static/%s", what);
456 fp = fopen(buf, "rb");
458 printf("HTTP/1.0 404 %s\n", strerror(errno));
460 printf("Content-Type: text/plain\n");
461 sprintf(buf, "%s: %s\n", what, strerror(errno));
462 printf("Content-length: %d\n", strlen(buf));
464 fwrite(buf, strlen(buf), 1, stdout);
466 printf("HTTP/1.0 200 OK\n");
469 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
470 printf("Content-type: image/gif\n");
471 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
472 printf("Content-type: image/jpeg\n");
473 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
474 printf("Content-type: text/html\n");
476 printf("Content-type: application/octet-stream\n");
478 fstat(fileno(fp), &statbuf);
479 bytes = statbuf.st_size;
480 printf("Content-length: %ld\n", (long) bytes);
483 putc(getc(fp), stdout);
496 off_t accomplished = 0L;
499 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
502 bytes = extract_long(&buf[4], 0);
503 printf("HTTP/1.0 200 OK\n");
505 printf("Content-type: image/gif\n");
506 printf("Content-length: %ld\n", (long) bytes);
509 while (bytes > (off_t) 0) {
510 thisblock = (off_t) sizeof(xferbuf);
511 if (thisblock > bytes)
513 serv_printf("READ %ld|%ld", accomplished, thisblock);
516 thisblock = extract_long(&buf[4], 0);
517 serv_read(xferbuf, (int) thisblock);
518 fwrite(xferbuf, thisblock, 1, stdout);
519 bytes = bytes - thisblock;
520 accomplished = accomplished + thisblock;
526 printf("HTTP/1.0 404 %s\n", strerror(errno));
528 printf("Content-Type: text/plain\n");
529 sprintf(buf, "Error retrieving image\n");
530 printf("Content-length: %d\n", strlen(buf));
532 fwrite(buf, strlen(buf), 1, stdout);
539 * Convenience functions to display a page containing only a string
541 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
543 printf("HTTP/1.0 200 OK\n");
545 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
546 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
547 wprintf("<B>%s</B>\n", titlebarmsg);
548 wprintf("</FONT></TD></TR></TABLE><BR>\n");
549 escputs(messagetext);
556 void display_error(char *errormessage)
558 convenience_page("770000", "Error", errormessage);
561 void display_success(char *successmessage)
563 convenience_page("007700", "OK", successmessage);
568 void extract_action(char *actbuf, char *cmdbuf)
572 strcpy(actbuf, cmdbuf);
573 if (!strncasecmp(actbuf, "GET /", 5))
574 strcpy(actbuf, &actbuf[5]);
575 if (!strncasecmp(actbuf, "PUT /", 5))
576 strcpy(actbuf, &actbuf[5]);
577 if (!strncasecmp(actbuf, "POST /", 6))
578 strcpy(actbuf, &actbuf[6]);
580 for (i = 0; i < strlen(actbuf); ++i) {
581 if (actbuf[i] == ' ') {
585 if (actbuf[i] == '/') {
589 if (actbuf[i] == '?') {
593 if (actbuf[i] == '&') {
601 void upload_handler(char *name, char *filename, char *encoding,
602 void *content, char *cbtype, size_t length)
605 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
606 fprintf(stderr, " name = %s\n", name);
607 fprintf(stderr, "filename = %s\n", filename);
608 fprintf(stderr, "encoding = %s\n", encoding);
609 fprintf(stderr, " type = %s\n", cbtype);
610 fprintf(stderr, " length = %d\n", length);
612 if (strlen(name) > 0) {
613 upload = malloc(length);
614 if (upload != NULL) {
615 upload_length = length;
616 memcpy(upload, content, length);
622 void session_loop(char *browser_host, char *user_agent)
628 int ContentLength = 0;
629 char ContentType[512];
632 /* We stuff these with the values coming from the client cookies,
633 * so we can use them to reconnect a timed out session if we have to.
637 char c_username[256];
638 char c_password[256];
639 char c_roomname[256];
642 strcpy(c_host, defaulthost);
643 strcpy(c_port, defaultport);
644 strcpy(c_username, "");
645 strcpy(c_password, "");
646 strcpy(c_roomname, "");
651 if (getz(cmd) == NULL)
653 extract_action(action, cmd);
656 if (getz(buf) == NULL)
659 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
660 strcpy(cookie, &buf[15]);
661 cookie_to_stuff(cookie, NULL,
662 c_username, c_password, c_roomname);
664 if (!strncasecmp(buf, "Content-length: ", 16)) {
665 ContentLength = atoi(&buf[16]);
667 if (!strncasecmp(buf, "Content-type: ", 14)) {
668 strcpy(ContentType, &buf[14]);
670 } while (strlen(buf) > 0);
674 if (ContentLength > 0) {
675 content = malloc(ContentLength + 1);
676 fread(content, ContentLength, 1, stdin);
678 content[ContentLength] = 0;
680 if (!strncasecmp(ContentType,
681 "application/x-www-form-urlencoded", 33)) {
683 } else if (!strncasecmp(ContentType, "multipart", 9)) {
684 mime_parser(content, ContentLength, ContentType,
691 /* If there are variables in the URL, we must grab them now */
692 for (a = 0; a < strlen(cmd); ++a)
693 if ((cmd[a] == '?') || (cmd[a] == '&')) {
694 for (b = a; b < strlen(cmd); ++b)
697 addurls(&cmd[a + 1]);
701 * If we're not connected to a Citadel server, try to hook up the
702 * connection now. Preference is given to the host and port specified
703 * by browser cookies, if cookies have been supplied.
706 if (strlen(bstr("host")) > 0)
707 strcpy(c_host, bstr("host"));
708 if (strlen(bstr("port")) > 0)
709 strcpy(c_port, bstr("port"));
710 serv_sock = connectsock(c_host, c_port, "tcp");
712 serv_gets(buf); /* get the server welcome message */
713 get_serv_info(browser_host, user_agent);
715 check_for_express_messages();
718 * If we're not logged in, but we have username and password cookies
719 * supplied by the browser, try using them to log in.
721 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
722 serv_printf("USER %s", c_username);
725 serv_printf("PASS %s", c_password);
728 become_logged_in(c_username, c_password, buf);
733 * If we don't have a current room, but a cookie specifying the
734 * current room is supplied, make an effort to go there.
736 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
737 serv_printf("GOTO %s", c_roomname);
740 strcpy(wc_roomname, c_roomname);
743 if (!strcasecmp(action, "static")) {
744 strcpy(buf, &cmd[12]);
745 for (a = 0; a < strlen(buf); ++a)
749 } else if (!strcasecmp(action, "image")) {
751 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
753 } else if (!logged_in) {
756 /* Various commands... */
758 else if (!strcasecmp(action, "do_welcome")) {
760 } else if (!strcasecmp(action, "display_main_menu")) {
762 } else if (!strcasecmp(action, "advanced")) {
763 display_advanced_menu();
764 } else if (!strcasecmp(action, "whobbs")) {
766 } else if (!strcasecmp(action, "knrooms")) {
767 list_all_rooms_by_floor();
768 } else if (!strcasecmp(action, "gotonext")) {
771 } else if (!strcasecmp(action, "skip")) {
773 } else if (!strcasecmp(action, "ungoto")) {
775 } else if (!strcasecmp(action, "dotgoto")) {
777 smart_goto(bstr("room"));
778 } else if (!strcasecmp(action, "termquit")) {
780 } else if (!strcasecmp(action, "readnew")) {
782 } else if (!strcasecmp(action, "readold")) {
784 } else if (!strcasecmp(action, "readfwd")) {
786 } else if (!strcasecmp(action, "display_enter")) {
788 } else if (!strcasecmp(action, "post")) {
790 } else if (!strcasecmp(action, "confirm_delete_msg")) {
791 confirm_delete_msg();
792 } else if (!strcasecmp(action, "delete_msg")) {
794 } else if (!strcasecmp(action, "confirm_move_msg")) {
796 } else if (!strcasecmp(action, "move_msg")) {
798 } else if (!strcasecmp(action, "userlist")) {
800 } else if (!strcasecmp(action, "showuser")) {
802 } else if (!strcasecmp(action, "display_page")) {
804 } else if (!strcasecmp(action, "page_user")) {
806 } else if (!strcasecmp(action, "chat")) {
808 } else if (!strcasecmp(action, "display_private")) {
809 display_private("", 0);
810 } else if (!strcasecmp(action, "goto_private")) {
812 } else if (!strcasecmp(action, "zapped_list")) {
814 } else if (!strcasecmp(action, "display_zap")) {
816 } else if (!strcasecmp(action, "zap")) {
818 } else if (!strcasecmp(action, "display_entroom")) {
820 } else if (!strcasecmp(action, "entroom")) {
822 } else if (!strcasecmp(action, "display_editroom")) {
824 } else if (!strcasecmp(action, "editroom")) {
826 } else if (!strcasecmp(action, "display_editinfo")) {
827 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
828 } else if (!strcasecmp(action, "editinfo")) {
829 save_edit("Room info", "EINF 1", 1);
830 } else if (!strcasecmp(action, "display_editbio")) {
831 sprintf(buf, "RBIO %s", wc_username);
832 display_edit("Your bio", "NOOP", buf, "editbio");
833 } else if (!strcasecmp(action, "editbio")) {
834 save_edit("Your bio", "EBIO", 0);
835 } else if (!strcasecmp(action, "confirm_delete_room")) {
836 confirm_delete_room();
837 } else if (!strcasecmp(action, "delete_room")) {
839 } else if (!strcasecmp(action, "validate")) {
841 } else if (!strcasecmp(action, "display_editpic")) {
842 display_graphics_upload("your photo",
845 } else if (!strcasecmp(action, "editpic")) {
846 do_graphics_upload("UIMG 1|_userpic_");
847 } else if (!strcasecmp(action, "display_editroompic")) {
848 display_graphics_upload("the graphic for this room",
851 } else if (!strcasecmp(action, "editroompic")) {
852 do_graphics_upload("UIMG 1|_roompic_");
853 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
854 select_floor_to_edit_pic();
855 } else if (!strcasecmp(action, "display_editfloorpic")) {
856 sprintf(buf, "UIMG 0|_floorpic_|%s",
857 bstr("which_floor"));
858 display_graphics_upload("the graphic for this floor",
861 } else if (!strcasecmp(action, "editfloorpic")) {
862 sprintf(buf, "UIMG 1|_floorpic_|%s",
863 bstr("which_floor"));
864 do_graphics_upload(buf);
865 } else if (!strcasecmp(action, "display_reg")) {
867 } else if (!strcasecmp(action, "register")) {
869 } else if (!strcasecmp(action, "display_changepw")) {
871 } else if (!strcasecmp(action, "changepw")) {
873 } else if (!strcasecmp(action, "display_edit_node")) {
875 } else if (!strcasecmp(action, "display_netconf")) {
877 } else if (!strcasecmp(action, "display_confirm_unshare")) {
878 display_confirm_unshare();
879 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
880 display_confirm_delete_node();
881 } else if (!strcasecmp(action, "delete_node")) {
883 } else if (!strcasecmp(action, "unshare")) {
885 } else if (!strcasecmp(action, "display_add_node")) {
887 } else if (!strcasecmp(action, "add_node")) {
889 } else if (!strcasecmp(action, "display_share")) {
891 } else if (!strcasecmp(action, "share")) {
893 } else if (!strcasecmp(action, "terminate_session")) {
895 } else if (!strcasecmp(action, "edit_me")) {
897 } else if (!strcasecmp(action, "display_siteconfig")) {
898 display_siteconfig();
899 } else if (!strcasecmp(action, "siteconfig")) {
901 } else if (!strcasecmp(action, "display_generic")) {
903 } else if (!strcasecmp(action, "do_generic")) {
905 } else if (!strcasecmp(action, "display_menubar")) {
907 } else if (!strcasecmp(action, "diagnostics")) {
908 printf("HTTP/1.0 200 OK\n");
911 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
912 wprintf("You're in session %d<HR>\n", wc_session);
913 wprintf("Command: <BR><PRE>\n");
915 wprintf("</PRE><HR>\n");
916 wprintf("Variables: <BR><PRE>\n");
918 wprintf("</PRE><HR>\n");
921 /* When all else fais, display the main menu. */
927 if (content != NULL) {
932 if (upload_length > 0) {
938 int main(int argc, char *argv[])
946 "webcit: usage error (argc must be 6, not %d)\n",
951 wc_session = atoi(argv[1]);
952 defaulthost = argv[2];
953 defaultport = argv[3];
955 strcpy(wc_username, "");
956 strcpy(wc_password, "");
957 strcpy(wc_roomname, "");
959 /* Clear out serv_info and temporarily set the value of serv_humannode
960 * to a default value, because it'll be used in HTML page titles
962 memset(&serv_info, 0, sizeof(serv_info));
963 strcpy(serv_info.serv_humannode, "WebCit");
965 strcpy(browser, argv[5]);
966 bd = browser_braindamage_check(browser);
969 session_loop(argv[4], browser);