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/bleft.gif\" BORDER=0>"
217 "Ungoto</A> ");
218 wprintf("<A HREF=\"#TheTop\">"
219 "<IMG SRC=\"/static/bup.gif\" BORDER=0>"
220 "Top of page</A> ");
221 wprintf("<A HREF=\"/display_enter\">"
222 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
223 "Enter a message</A> ");
224 wprintf("<A HREF=\"/gotonext\">"
226 "<IMG SRC=\"/static/bright.gif\" BORDER=0></A>"
228 "</TD></TR></TABLE></TABLE>\n");
232 if (print_standard_html_footer) {
233 if (print_standard_html_footer != 2) {
236 wprintf("</BODY></HTML>\n");
238 printf("Content-type: text/html\n");
239 printf("Content-length: %d\n", wContentLength());
242 while (wlist != NULL) {
243 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
252 void escputs1(char *strbuf, int nbsp)
256 for (a = 0; a < strlen(strbuf); ++a) {
257 if (strbuf[a] == '<')
259 else if (strbuf[a] == '>')
261 else if (strbuf[a] == '&')
263 else if (strbuf[a] == '\"')
265 else if (strbuf[a] == '\'')
267 else if (strbuf[a] == LB)
269 else if (strbuf[a] == RB)
271 else if (strbuf[a] == QU)
273 else if ((strbuf[a] == 32) && (nbsp == 1)) {
276 wprintf("%c", strbuf[a]);
281 void escputs(char *strbuf)
288 char *urlesc(char *strbuf)
291 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
292 static char outbuf[512];
296 for (a = 0; a < strlen(strbuf); ++a) {
298 for (b = 0; b < strlen(ec); ++b) {
299 if (strbuf[a] == ec[b])
304 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
306 sprintf(&outbuf[b], "%c", strbuf[a]);
311 void urlescputs(char *strbuf)
313 wprintf("%s", urlesc(strbuf));
320 * Get a line of text from the webserver (which originally came from the
321 * user's browser), checking for sanity etc.
323 char *getz(char *buf)
329 /* If fgets() fails, it's because the webserver crashed, so kill off
332 if (fgets(buf, 256, stdin) == NULL) {
334 fprintf(stderr, "webcit: exit code %d (%s)\n",
339 /* Otherwise, strip out nonprintables and resume our happy day.
342 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
343 buf[strlen(buf) - 1] = 0;
351 * Output all that important stuff that the browser will want to see
353 * print_standard_html_head values:
354 * 0 = Nothing. Do not display any leading HTTP or HTML.
355 * 1 = HTTP headers plus the "fake frames" found in most windows.
356 * 2 = HTTP headers required to terminate the session (unset cookies)
357 * 3 = HTTP headers only.
359 void output_headers(int print_standard_html_head)
362 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
365 printf("Server: %s\n", SERVER);
366 printf("Connection: close\n");
368 if (print_standard_html_head > 0) {
369 printf("Pragma: no-cache\n");
370 printf("Cache-Control: no-store\n");
372 stuff_to_cookie(cookie, wc_session, wc_username,
373 wc_password, wc_roomname);
374 if (print_standard_html_head == 2) {
375 printf("X-WebCit-Session: close\n");
376 printf("Set-cookie: webcit=%s\n", unset);
378 printf("Set-cookie: webcit=%s\n", cookie);
381 if (print_standard_html_head > 0) {
382 wprintf("<HTML><HEAD><TITLE>");
383 escputs(serv_info.serv_humannode);
385 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
387 if (ExpressMessages != NULL) {
388 wprintf("<SCRIPT language=\"javascript\">\n");
389 wprintf("function ExpressMessage() {\n");
390 wprintf(" alert(\"");
391 escputs(ExpressMessages);
393 wprintf(" }\n </SCRIPT>\n");
398 /* JavaScript key-based navigation would go here if it
403 if (ExpressMessages != NULL) {
404 wprintf("onload=\"ExpressMessage()\" ");
405 free(ExpressMessages);
406 ExpressMessages = NULL;
408 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
411 if (print_standard_html_head == 1) {
412 wprintf("<A NAME=\"TheTop\"></A>"
413 "<TABLE border=0 width=100%>"
414 "<TR VALIGN=TOP><TD>");
418 wprintf("</TD><TD VALIGN=TOP>"
419 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
422 embed_room_banner(NULL);
424 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
433 void ExpressMessageCat(char *buf) {
434 if (ExpressMessages == NULL) {
435 ExpressMessages = malloc(strlen(buf) + 4);
436 strcpy(ExpressMessages, "");
438 ExpressMessages = realloc(ExpressMessages,
439 (strlen(ExpressMessages) + strlen(buf) + 4));
441 strcat(ExpressMessages, buf);
442 strcat(ExpressMessages, "\\n");
446 void check_for_express_messages()
453 while (serv_gets(buf), strcmp(buf, "000")) {
454 ExpressMessageCat(buf);
462 void output_static(char *what)
469 sprintf(buf, "static/%s", what);
470 fp = fopen(buf, "rb");
472 printf("HTTP/1.0 404 %s\n", strerror(errno));
474 printf("Content-Type: text/plain\n");
475 sprintf(buf, "%s: %s\n", what, strerror(errno));
476 printf("Content-length: %d\n", strlen(buf));
478 fwrite(buf, strlen(buf), 1, stdout);
480 printf("HTTP/1.0 200 OK\n");
483 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
484 printf("Content-type: image/gif\n");
485 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
486 printf("Content-type: text/plain\n");
487 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
488 printf("Content-type: image/jpeg\n");
489 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
490 printf("Content-type: text/html\n");
492 printf("Content-type: application/octet-stream\n");
494 fstat(fileno(fp), &statbuf);
495 bytes = statbuf.st_size;
496 printf("Content-length: %ld\n", (long) bytes);
499 putc(getc(fp), stdout);
512 off_t accomplished = 0L;
515 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
518 bytes = extract_long(&buf[4], 0);
519 printf("HTTP/1.0 200 OK\n");
521 printf("Content-type: image/gif\n");
522 printf("Content-length: %ld\n", (long) bytes);
525 while (bytes > (off_t) 0) {
526 thisblock = (off_t) sizeof(xferbuf);
527 if (thisblock > bytes)
529 serv_printf("READ %ld|%ld", accomplished, thisblock);
532 thisblock = extract_long(&buf[4], 0);
533 serv_read(xferbuf, (int) thisblock);
534 fwrite(xferbuf, thisblock, 1, stdout);
535 bytes = bytes - thisblock;
536 accomplished = accomplished + thisblock;
542 printf("HTTP/1.0 404 %s\n", strerror(errno));
544 printf("Content-Type: text/plain\n");
545 sprintf(buf, "Error retrieving image\n");
546 printf("Content-length: %d\n", strlen(buf));
548 fwrite(buf, strlen(buf), 1, stdout);
555 * Convenience functions to display a page containing only a string
557 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
559 printf("HTTP/1.0 200 OK\n");
561 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
562 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
563 wprintf("<B>%s</B>\n", titlebarmsg);
564 wprintf("</FONT></TD></TR></TABLE><BR>\n");
565 escputs(messagetext);
572 void display_error(char *errormessage)
574 convenience_page("770000", "Error", errormessage);
577 void display_success(char *successmessage)
579 convenience_page("007700", "OK", successmessage);
584 void extract_action(char *actbuf, char *cmdbuf)
588 strcpy(actbuf, cmdbuf);
589 if (!strncasecmp(actbuf, "GET /", 5))
590 strcpy(actbuf, &actbuf[5]);
591 if (!strncasecmp(actbuf, "PUT /", 5))
592 strcpy(actbuf, &actbuf[5]);
593 if (!strncasecmp(actbuf, "POST /", 6))
594 strcpy(actbuf, &actbuf[6]);
596 for (i = 0; i < strlen(actbuf); ++i) {
597 if (actbuf[i] == ' ') {
601 if (actbuf[i] == '/') {
605 if (actbuf[i] == '?') {
609 if (actbuf[i] == '&') {
617 void upload_handler(char *name, char *filename, char *encoding,
618 void *content, char *cbtype, size_t length)
621 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
622 fprintf(stderr, " name = %s\n", name);
623 fprintf(stderr, "filename = %s\n", filename);
624 fprintf(stderr, "encoding = %s\n", encoding);
625 fprintf(stderr, " type = %s\n", cbtype);
626 fprintf(stderr, " length = %d\n", length);
628 if (strlen(name) > 0) {
629 upload = malloc(length);
630 if (upload != NULL) {
631 upload_length = length;
632 memcpy(upload, content, length);
638 void session_loop(char *browser_host, char *user_agent)
644 int ContentLength = 0;
645 char ContentType[512];
648 /* We stuff these with the values coming from the client cookies,
649 * so we can use them to reconnect a timed out session if we have to.
653 char c_username[256];
654 char c_password[256];
655 char c_roomname[256];
658 strcpy(c_host, defaulthost);
659 strcpy(c_port, defaultport);
660 strcpy(c_username, "");
661 strcpy(c_password, "");
662 strcpy(c_roomname, "");
667 if (getz(cmd) == NULL)
669 extract_action(action, cmd);
672 if (getz(buf) == NULL)
675 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
676 strcpy(cookie, &buf[15]);
677 cookie_to_stuff(cookie, NULL,
678 c_username, c_password, c_roomname);
680 if (!strncasecmp(buf, "Content-length: ", 16)) {
681 ContentLength = atoi(&buf[16]);
683 if (!strncasecmp(buf, "Content-type: ", 14)) {
684 strcpy(ContentType, &buf[14]);
686 } while (strlen(buf) > 0);
690 if (ContentLength > 0) {
691 content = malloc(ContentLength + 1);
692 fread(content, ContentLength, 1, stdin);
694 content[ContentLength] = 0;
696 if (!strncasecmp(ContentType,
697 "application/x-www-form-urlencoded", 33)) {
699 } else if (!strncasecmp(ContentType, "multipart", 9)) {
700 mime_parser(content, ContentLength, ContentType,
707 /* If there are variables in the URL, we must grab them now */
708 for (a = 0; a < strlen(cmd); ++a)
709 if ((cmd[a] == '?') || (cmd[a] == '&')) {
710 for (b = a; b < strlen(cmd); ++b)
713 addurls(&cmd[a + 1]);
717 * If we're not connected to a Citadel server, try to hook up the
718 * connection now. Preference is given to the host and port specified
719 * by browser cookies, if cookies have been supplied.
722 if (strlen(bstr("host")) > 0)
723 strcpy(c_host, bstr("host"));
724 if (strlen(bstr("port")) > 0)
725 strcpy(c_port, bstr("port"));
726 serv_sock = connectsock(c_host, c_port, "tcp");
732 serv_gets(buf); /* get the server welcome message */
733 get_serv_info(browser_host, user_agent);
735 check_for_express_messages();
738 * If we're not logged in, but we have username and password cookies
739 * supplied by the browser, try using them to log in.
741 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
742 serv_printf("USER %s", c_username);
745 serv_printf("PASS %s", c_password);
748 become_logged_in(c_username, c_password, buf);
753 * If we don't have a current room, but a cookie specifying the
754 * current room is supplied, make an effort to go there.
756 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
757 serv_printf("GOTO %s", c_roomname);
760 strcpy(wc_roomname, c_roomname);
763 if (!strcasecmp(action, "static")) {
764 strcpy(buf, &cmd[12]);
765 for (a = 0; a < strlen(buf); ++a)
769 } else if (!strcasecmp(action, "image")) {
771 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
773 } else if (!logged_in) {
776 /* Various commands... */
778 else if (!strcasecmp(action, "do_welcome")) {
780 } else if (!strcasecmp(action, "display_main_menu")) {
782 } else if (!strcasecmp(action, "advanced")) {
783 display_advanced_menu();
784 } else if (!strcasecmp(action, "whobbs")) {
786 } else if (!strcasecmp(action, "knrooms")) {
787 list_all_rooms_by_floor();
788 } else if (!strcasecmp(action, "gotonext")) {
791 } else if (!strcasecmp(action, "skip")) {
793 } else if (!strcasecmp(action, "ungoto")) {
795 } else if (!strcasecmp(action, "dotgoto")) {
797 smart_goto(bstr("room"));
798 } else if (!strcasecmp(action, "termquit")) {
800 } else if (!strcasecmp(action, "readnew")) {
802 } else if (!strcasecmp(action, "readold")) {
804 } else if (!strcasecmp(action, "readfwd")) {
806 } else if (!strcasecmp(action, "display_enter")) {
808 } else if (!strcasecmp(action, "post")) {
810 } else if (!strcasecmp(action, "confirm_delete_msg")) {
811 confirm_delete_msg();
812 } else if (!strcasecmp(action, "delete_msg")) {
814 } else if (!strcasecmp(action, "confirm_move_msg")) {
816 } else if (!strcasecmp(action, "move_msg")) {
818 } else if (!strcasecmp(action, "userlist")) {
820 } else if (!strcasecmp(action, "showuser")) {
822 } else if (!strcasecmp(action, "display_page")) {
824 } else if (!strcasecmp(action, "page_user")) {
826 } else if (!strcasecmp(action, "chat")) {
828 } else if (!strcasecmp(action, "display_private")) {
829 display_private("", 0);
830 } else if (!strcasecmp(action, "goto_private")) {
832 } else if (!strcasecmp(action, "zapped_list")) {
834 } else if (!strcasecmp(action, "display_zap")) {
836 } else if (!strcasecmp(action, "zap")) {
838 } else if (!strcasecmp(action, "display_entroom")) {
840 } else if (!strcasecmp(action, "entroom")) {
842 } else if (!strcasecmp(action, "display_editroom")) {
844 } else if (!strcasecmp(action, "editroom")) {
846 } else if (!strcasecmp(action, "display_editinfo")) {
847 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
848 } else if (!strcasecmp(action, "editinfo")) {
849 save_edit("Room info", "EINF 1", 1);
850 } else if (!strcasecmp(action, "display_editbio")) {
851 sprintf(buf, "RBIO %s", wc_username);
852 display_edit("Your bio", "NOOP", buf, "editbio");
853 } else if (!strcasecmp(action, "editbio")) {
854 save_edit("Your bio", "EBIO", 0);
855 } else if (!strcasecmp(action, "confirm_delete_room")) {
856 confirm_delete_room();
857 } else if (!strcasecmp(action, "delete_room")) {
859 } else if (!strcasecmp(action, "validate")) {
861 } else if (!strcasecmp(action, "display_editpic")) {
862 display_graphics_upload("your photo",
865 } else if (!strcasecmp(action, "editpic")) {
866 do_graphics_upload("UIMG 1|_userpic_");
867 } else if (!strcasecmp(action, "display_editroompic")) {
868 display_graphics_upload("the graphic for this room",
871 } else if (!strcasecmp(action, "editroompic")) {
872 do_graphics_upload("UIMG 1|_roompic_");
873 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
874 select_floor_to_edit_pic();
875 } else if (!strcasecmp(action, "display_editfloorpic")) {
876 sprintf(buf, "UIMG 0|_floorpic_|%s",
877 bstr("which_floor"));
878 display_graphics_upload("the graphic for this floor",
881 } else if (!strcasecmp(action, "editfloorpic")) {
882 sprintf(buf, "UIMG 1|_floorpic_|%s",
883 bstr("which_floor"));
884 do_graphics_upload(buf);
885 } else if (!strcasecmp(action, "display_reg")) {
887 } else if (!strcasecmp(action, "register")) {
889 } else if (!strcasecmp(action, "display_changepw")) {
891 } else if (!strcasecmp(action, "changepw")) {
893 } else if (!strcasecmp(action, "display_edit_node")) {
895 } else if (!strcasecmp(action, "display_netconf")) {
897 } else if (!strcasecmp(action, "display_confirm_unshare")) {
898 display_confirm_unshare();
899 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
900 display_confirm_delete_node();
901 } else if (!strcasecmp(action, "delete_node")) {
903 } else if (!strcasecmp(action, "unshare")) {
905 } else if (!strcasecmp(action, "display_add_node")) {
907 } else if (!strcasecmp(action, "add_node")) {
909 } else if (!strcasecmp(action, "display_share")) {
911 } else if (!strcasecmp(action, "share")) {
913 } else if (!strcasecmp(action, "terminate_session")) {
916 } else if (!strcasecmp(action, "edit_me")) {
918 } else if (!strcasecmp(action, "display_siteconfig")) {
919 display_siteconfig();
920 } else if (!strcasecmp(action, "siteconfig")) {
922 } else if (!strcasecmp(action, "display_generic")) {
924 } else if (!strcasecmp(action, "do_generic")) {
926 } else if (!strcasecmp(action, "display_menubar")) {
928 } else if (!strcasecmp(action, "diagnostics")) {
929 printf("HTTP/1.0 200 OK\n");
932 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
933 wprintf("You're in session %d<HR>\n", wc_session);
934 wprintf("Command: <BR><PRE>\n");
936 wprintf("</PRE><HR>\n");
937 wprintf("Variables: <BR><PRE>\n");
939 wprintf("</PRE><HR>\n");
942 /* When all else fais, display the main menu. */
948 if (content != NULL) {
953 if (upload_length > 0) {
959 int main(int argc, char *argv[])
967 "webcit: usage error (argc must be 6, not %d)\n",
972 wc_session = atoi(argv[1]);
973 defaulthost = argv[2];
974 defaultport = argv[3];
976 strcpy(wc_username, "");
977 strcpy(wc_password, "");
978 strcpy(wc_roomname, "");
980 /* Clear out serv_info and temporarily set the value of serv_humannode
981 * to a default value, because it'll be used in HTML page titles
983 memset(&serv_info, 0, sizeof(serv_info));
984 strcpy(serv_info.serv_humannode, "WebCit");
986 strcpy(browser, argv[5]);
987 bd = browser_braindamage_check(browser);
990 session_loop(argv[4], browser);