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 "<TABLE border=0 width=100%><TR>"
216 "<TD><A HREF=\"/ungoto\">"
217 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
219 wprintf("<TD><A HREF=\"#TheTop\">"
220 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
221 "Top of page</A></TD>");
222 wprintf("<TD><A HREF=\"/display_enter\">"
223 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
224 "Enter a message</A></TD>");
225 wprintf("<TD><A HREF=\"/gotonext\">"
227 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
230 "</TD></TR></TABLE></TABLE>\n");
234 if (print_standard_html_footer) {
235 if (print_standard_html_footer != 2) {
238 wprintf("</BODY></HTML>\n");
240 printf("Content-type: text/html\n");
241 printf("Content-length: %d\n", wContentLength());
244 while (wlist != NULL) {
245 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
254 void escputs1(char *strbuf, int nbsp)
258 for (a = 0; a < strlen(strbuf); ++a) {
259 if (strbuf[a] == '<')
261 else if (strbuf[a] == '>')
263 else if (strbuf[a] == '&')
265 else if (strbuf[a] == '\"')
267 else if (strbuf[a] == '\'')
269 else if (strbuf[a] == LB)
271 else if (strbuf[a] == RB)
273 else if (strbuf[a] == QU)
275 else if ((strbuf[a] == 32) && (nbsp == 1)) {
278 wprintf("%c", strbuf[a]);
283 void escputs(char *strbuf)
290 char *urlesc(char *strbuf)
293 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
294 static char outbuf[512];
298 for (a = 0; a < strlen(strbuf); ++a) {
300 for (b = 0; b < strlen(ec); ++b) {
301 if (strbuf[a] == ec[b])
306 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
308 sprintf(&outbuf[b], "%c", strbuf[a]);
313 void urlescputs(char *strbuf)
315 wprintf("%s", urlesc(strbuf));
322 * Get a line of text from the webserver (which originally came from the
323 * user's browser), checking for sanity etc.
325 char *getz(char *buf)
331 /* If fgets() fails, it's because the webserver crashed, so kill off
334 if (fgets(buf, 256, stdin) == NULL) {
336 fprintf(stderr, "webcit: exit code %d (%s)\n",
341 /* Otherwise, strip out nonprintables and resume our happy day.
344 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
345 buf[strlen(buf) - 1] = 0;
353 * Output all that important stuff that the browser will want to see
355 * print_standard_html_head values:
356 * 0 = Nothing. Do not display any leading HTTP or HTML.
357 * 1 = HTTP headers plus the "fake frames" found in most windows.
358 * 2 = HTTP headers required to terminate the session (unset cookies)
359 * 3 = HTTP headers only.
361 void output_headers(int print_standard_html_head)
364 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
367 printf("Server: %s\n", SERVER);
368 printf("Connection: close\n");
370 if (print_standard_html_head > 0) {
371 printf("Pragma: no-cache\n");
372 printf("Cache-Control: no-store\n");
374 stuff_to_cookie(cookie, wc_session, wc_username,
375 wc_password, wc_roomname);
376 if (print_standard_html_head == 2) {
377 printf("X-WebCit-Session: close\n");
378 printf("Set-cookie: webcit=%s\n", unset);
380 printf("Set-cookie: webcit=%s\n", cookie);
383 if (print_standard_html_head > 0) {
384 wprintf("<HTML><HEAD><TITLE>");
385 escputs(serv_info.serv_humannode);
387 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
389 if (ExpressMessages != NULL) {
390 wprintf("<SCRIPT language=\"javascript\">\n");
391 wprintf("function ExpressMessage() {\n");
392 wprintf(" alert(\"");
393 escputs(ExpressMessages);
395 wprintf(" }\n </SCRIPT>\n");
400 /* JavaScript key-based navigation would go here if it
405 if (ExpressMessages != NULL) {
406 wprintf("onload=\"ExpressMessage()\" ");
407 free(ExpressMessages);
408 ExpressMessages = NULL;
410 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
413 if (print_standard_html_head == 1) {
414 wprintf("<A NAME=\"TheTop\"></A>"
415 "<TABLE border=0 width=100%>"
416 "<TR VALIGN=TOP><TD>");
420 wprintf("</TD><TD VALIGN=TOP>"
421 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
424 embed_room_banner(NULL);
426 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
435 void ExpressMessageCat(char *buf) {
436 if (ExpressMessages == NULL) {
437 ExpressMessages = malloc(strlen(buf) + 4);
438 strcpy(ExpressMessages, "");
440 ExpressMessages = realloc(ExpressMessages,
441 (strlen(ExpressMessages) + strlen(buf) + 4));
443 strcat(ExpressMessages, buf);
444 strcat(ExpressMessages, "\\n");
448 void check_for_express_messages()
455 while (serv_gets(buf), strcmp(buf, "000")) {
456 ExpressMessageCat(buf);
464 void output_static(char *what)
471 sprintf(buf, "static/%s", what);
472 fp = fopen(buf, "rb");
474 printf("HTTP/1.0 404 %s\n", strerror(errno));
476 printf("Content-Type: text/plain\n");
477 sprintf(buf, "%s: %s\n", what, strerror(errno));
478 printf("Content-length: %d\n", strlen(buf));
480 fwrite(buf, strlen(buf), 1, stdout);
482 printf("HTTP/1.0 200 OK\n");
485 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
486 printf("Content-type: image/gif\n");
487 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
488 printf("Content-type: text/plain\n");
489 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
490 printf("Content-type: image/jpeg\n");
491 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
492 printf("Content-type: text/html\n");
494 printf("Content-type: application/octet-stream\n");
496 fstat(fileno(fp), &statbuf);
497 bytes = statbuf.st_size;
498 printf("Content-length: %ld\n", (long) bytes);
501 putc(getc(fp), stdout);
514 off_t accomplished = 0L;
517 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
520 bytes = extract_long(&buf[4], 0);
521 printf("HTTP/1.0 200 OK\n");
523 printf("Content-type: image/gif\n");
524 printf("Content-length: %ld\n", (long) bytes);
527 while (bytes > (off_t) 0) {
528 thisblock = (off_t) sizeof(xferbuf);
529 if (thisblock > bytes)
531 serv_printf("READ %ld|%ld", accomplished, thisblock);
534 thisblock = extract_long(&buf[4], 0);
535 serv_read(xferbuf, (int) thisblock);
536 fwrite(xferbuf, thisblock, 1, stdout);
537 bytes = bytes - thisblock;
538 accomplished = accomplished + thisblock;
544 printf("HTTP/1.0 404 %s\n", strerror(errno));
546 printf("Content-Type: text/plain\n");
547 sprintf(buf, "Error retrieving image\n");
548 printf("Content-length: %d\n", strlen(buf));
550 fwrite(buf, strlen(buf), 1, stdout);
557 * Convenience functions to display a page containing only a string
559 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
561 printf("HTTP/1.0 200 OK\n");
563 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
564 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
565 wprintf("<B>%s</B>\n", titlebarmsg);
566 wprintf("</FONT></TD></TR></TABLE><BR>\n");
567 escputs(messagetext);
574 void display_error(char *errormessage)
576 convenience_page("770000", "Error", errormessage);
579 void display_success(char *successmessage)
581 convenience_page("007700", "OK", successmessage);
586 void extract_action(char *actbuf, char *cmdbuf)
590 strcpy(actbuf, cmdbuf);
591 if (!strncasecmp(actbuf, "GET /", 5))
592 strcpy(actbuf, &actbuf[5]);
593 if (!strncasecmp(actbuf, "PUT /", 5))
594 strcpy(actbuf, &actbuf[5]);
595 if (!strncasecmp(actbuf, "POST /", 6))
596 strcpy(actbuf, &actbuf[6]);
598 for (i = 0; i < strlen(actbuf); ++i) {
599 if (actbuf[i] == ' ') {
603 if (actbuf[i] == '/') {
607 if (actbuf[i] == '?') {
611 if (actbuf[i] == '&') {
619 void upload_handler(char *name, char *filename, char *encoding,
620 void *content, char *cbtype, size_t length)
623 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
624 fprintf(stderr, " name = %s\n", name);
625 fprintf(stderr, "filename = %s\n", filename);
626 fprintf(stderr, "encoding = %s\n", encoding);
627 fprintf(stderr, " type = %s\n", cbtype);
628 fprintf(stderr, " length = %d\n", length);
630 if (strlen(name) > 0) {
631 upload = malloc(length);
632 if (upload != NULL) {
633 upload_length = length;
634 memcpy(upload, content, length);
640 void session_loop(char *browser_host, char *user_agent)
646 int ContentLength = 0;
647 char ContentType[512];
650 /* We stuff these with the values coming from the client cookies,
651 * so we can use them to reconnect a timed out session if we have to.
655 char c_username[256];
656 char c_password[256];
657 char c_roomname[256];
660 strcpy(c_host, defaulthost);
661 strcpy(c_port, defaultport);
662 strcpy(c_username, "");
663 strcpy(c_password, "");
664 strcpy(c_roomname, "");
669 if (getz(cmd) == NULL)
671 extract_action(action, cmd);
674 if (getz(buf) == NULL)
677 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
678 strcpy(cookie, &buf[15]);
679 cookie_to_stuff(cookie, NULL,
680 c_username, c_password, c_roomname);
682 if (!strncasecmp(buf, "Content-length: ", 16)) {
683 ContentLength = atoi(&buf[16]);
685 if (!strncasecmp(buf, "Content-type: ", 14)) {
686 strcpy(ContentType, &buf[14]);
688 } while (strlen(buf) > 0);
692 if (ContentLength > 0) {
693 content = malloc(ContentLength + 1);
694 fread(content, ContentLength, 1, stdin);
696 content[ContentLength] = 0;
698 if (!strncasecmp(ContentType,
699 "application/x-www-form-urlencoded", 33)) {
701 } else if (!strncasecmp(ContentType, "multipart", 9)) {
702 mime_parser(content, ContentLength, ContentType,
709 /* If there are variables in the URL, we must grab them now */
710 for (a = 0; a < strlen(cmd); ++a)
711 if ((cmd[a] == '?') || (cmd[a] == '&')) {
712 for (b = a; b < strlen(cmd); ++b)
715 addurls(&cmd[a + 1]);
719 * If we're not connected to a Citadel server, try to hook up the
720 * connection now. Preference is given to the host and port specified
721 * by browser cookies, if cookies have been supplied.
724 if (strlen(bstr("host")) > 0)
725 strcpy(c_host, bstr("host"));
726 if (strlen(bstr("port")) > 0)
727 strcpy(c_port, bstr("port"));
728 serv_sock = connectsock(c_host, c_port, "tcp");
734 serv_gets(buf); /* get the server welcome message */
735 get_serv_info(browser_host, user_agent);
737 check_for_express_messages();
740 * If we're not logged in, but we have username and password cookies
741 * supplied by the browser, try using them to log in.
743 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
744 serv_printf("USER %s", c_username);
747 serv_printf("PASS %s", c_password);
750 become_logged_in(c_username, c_password, buf);
755 * If we don't have a current room, but a cookie specifying the
756 * current room is supplied, make an effort to go there.
758 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
759 serv_printf("GOTO %s", c_roomname);
762 strcpy(wc_roomname, c_roomname);
765 if (!strcasecmp(action, "static")) {
766 strcpy(buf, &cmd[12]);
767 for (a = 0; a < strlen(buf); ++a)
771 } else if (!strcasecmp(action, "image")) {
773 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
775 } else if (!logged_in) {
778 /* Various commands... */
780 else if (!strcasecmp(action, "do_welcome")) {
782 } else if (!strcasecmp(action, "display_main_menu")) {
784 } else if (!strcasecmp(action, "advanced")) {
785 display_advanced_menu();
786 } else if (!strcasecmp(action, "whobbs")) {
788 } else if (!strcasecmp(action, "knrooms")) {
789 list_all_rooms_by_floor();
790 } else if (!strcasecmp(action, "gotonext")) {
793 } else if (!strcasecmp(action, "skip")) {
795 } else if (!strcasecmp(action, "ungoto")) {
797 } else if (!strcasecmp(action, "dotgoto")) {
799 smart_goto(bstr("room"));
800 } else if (!strcasecmp(action, "termquit")) {
802 } else if (!strcasecmp(action, "readnew")) {
804 } else if (!strcasecmp(action, "readold")) {
806 } else if (!strcasecmp(action, "readfwd")) {
808 } else if (!strcasecmp(action, "display_enter")) {
810 } else if (!strcasecmp(action, "post")) {
812 } else if (!strcasecmp(action, "confirm_delete_msg")) {
813 confirm_delete_msg();
814 } else if (!strcasecmp(action, "delete_msg")) {
816 } else if (!strcasecmp(action, "confirm_move_msg")) {
818 } else if (!strcasecmp(action, "move_msg")) {
820 } else if (!strcasecmp(action, "userlist")) {
822 } else if (!strcasecmp(action, "showuser")) {
824 } else if (!strcasecmp(action, "display_page")) {
826 } else if (!strcasecmp(action, "page_user")) {
828 } else if (!strcasecmp(action, "chat")) {
830 } else if (!strcasecmp(action, "display_private")) {
831 display_private("", 0);
832 } else if (!strcasecmp(action, "goto_private")) {
834 } else if (!strcasecmp(action, "zapped_list")) {
836 } else if (!strcasecmp(action, "display_zap")) {
838 } else if (!strcasecmp(action, "zap")) {
840 } else if (!strcasecmp(action, "display_entroom")) {
842 } else if (!strcasecmp(action, "entroom")) {
844 } else if (!strcasecmp(action, "display_editroom")) {
846 } else if (!strcasecmp(action, "editroom")) {
848 } else if (!strcasecmp(action, "display_editinfo")) {
849 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
850 } else if (!strcasecmp(action, "editinfo")) {
851 save_edit("Room info", "EINF 1", 1);
852 } else if (!strcasecmp(action, "display_editbio")) {
853 sprintf(buf, "RBIO %s", wc_username);
854 display_edit("Your bio", "NOOP", buf, "editbio");
855 } else if (!strcasecmp(action, "editbio")) {
856 save_edit("Your bio", "EBIO", 0);
857 } else if (!strcasecmp(action, "confirm_delete_room")) {
858 confirm_delete_room();
859 } else if (!strcasecmp(action, "delete_room")) {
861 } else if (!strcasecmp(action, "validate")) {
863 } else if (!strcasecmp(action, "display_editpic")) {
864 display_graphics_upload("your photo",
867 } else if (!strcasecmp(action, "editpic")) {
868 do_graphics_upload("UIMG 1|_userpic_");
869 } else if (!strcasecmp(action, "display_editroompic")) {
870 display_graphics_upload("the graphic for this room",
873 } else if (!strcasecmp(action, "editroompic")) {
874 do_graphics_upload("UIMG 1|_roompic_");
875 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
876 select_floor_to_edit_pic();
877 } else if (!strcasecmp(action, "display_editfloorpic")) {
878 sprintf(buf, "UIMG 0|_floorpic_|%s",
879 bstr("which_floor"));
880 display_graphics_upload("the graphic for this floor",
883 } else if (!strcasecmp(action, "editfloorpic")) {
884 sprintf(buf, "UIMG 1|_floorpic_|%s",
885 bstr("which_floor"));
886 do_graphics_upload(buf);
887 } else if (!strcasecmp(action, "display_reg")) {
889 } else if (!strcasecmp(action, "register")) {
891 } else if (!strcasecmp(action, "display_changepw")) {
893 } else if (!strcasecmp(action, "changepw")) {
895 } else if (!strcasecmp(action, "display_edit_node")) {
897 } else if (!strcasecmp(action, "display_netconf")) {
899 } else if (!strcasecmp(action, "display_confirm_unshare")) {
900 display_confirm_unshare();
901 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
902 display_confirm_delete_node();
903 } else if (!strcasecmp(action, "delete_node")) {
905 } else if (!strcasecmp(action, "unshare")) {
907 } else if (!strcasecmp(action, "display_add_node")) {
909 } else if (!strcasecmp(action, "add_node")) {
911 } else if (!strcasecmp(action, "display_share")) {
913 } else if (!strcasecmp(action, "share")) {
915 } else if (!strcasecmp(action, "terminate_session")) {
918 } else if (!strcasecmp(action, "edit_me")) {
920 } else if (!strcasecmp(action, "display_siteconfig")) {
921 display_siteconfig();
922 } else if (!strcasecmp(action, "siteconfig")) {
924 } else if (!strcasecmp(action, "display_generic")) {
926 } else if (!strcasecmp(action, "do_generic")) {
928 } else if (!strcasecmp(action, "display_menubar")) {
930 } else if (!strcasecmp(action, "diagnostics")) {
931 printf("HTTP/1.0 200 OK\n");
934 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
935 wprintf("You're in session %d<HR>\n", wc_session);
936 wprintf("Command: <BR><PRE>\n");
938 wprintf("</PRE><HR>\n");
939 wprintf("Variables: <BR><PRE>\n");
941 wprintf("</PRE><HR>\n");
944 /* When all else fais, display the main menu. */
950 if (content != NULL) {
955 if (upload_length > 0) {
961 int main(int argc, char *argv[])
969 "webcit: usage error (argc must be 6, not %d)\n",
974 wc_session = atoi(argv[1]);
975 defaulthost = argv[2];
976 defaultport = argv[3];
978 strcpy(wc_username, "");
979 strcpy(wc_password, "");
980 strcpy(wc_roomname, "");
982 /* Clear out serv_info and temporarily set the value of serv_humannode
983 * to a default value, because it'll be used in HTML page titles
985 memset(&serv_info, 0, sizeof(serv_info));
986 strcpy(serv_info.serv_humannode, "WebCit");
988 strcpy(browser, argv[5]);
989 bd = browser_braindamage_check(browser);
992 session_loop(argv[4], browser);