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], ".txt", 4))
472 printf("Content-type: text/plain\n");
473 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
474 printf("Content-type: image/jpeg\n");
475 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
476 printf("Content-type: text/html\n");
478 printf("Content-type: application/octet-stream\n");
480 fstat(fileno(fp), &statbuf);
481 bytes = statbuf.st_size;
482 printf("Content-length: %ld\n", (long) bytes);
485 putc(getc(fp), stdout);
498 off_t accomplished = 0L;
501 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
504 bytes = extract_long(&buf[4], 0);
505 printf("HTTP/1.0 200 OK\n");
507 printf("Content-type: image/gif\n");
508 printf("Content-length: %ld\n", (long) bytes);
511 while (bytes > (off_t) 0) {
512 thisblock = (off_t) sizeof(xferbuf);
513 if (thisblock > bytes)
515 serv_printf("READ %ld|%ld", accomplished, thisblock);
518 thisblock = extract_long(&buf[4], 0);
519 serv_read(xferbuf, (int) thisblock);
520 fwrite(xferbuf, thisblock, 1, stdout);
521 bytes = bytes - thisblock;
522 accomplished = accomplished + thisblock;
528 printf("HTTP/1.0 404 %s\n", strerror(errno));
530 printf("Content-Type: text/plain\n");
531 sprintf(buf, "Error retrieving image\n");
532 printf("Content-length: %d\n", strlen(buf));
534 fwrite(buf, strlen(buf), 1, stdout);
541 * Convenience functions to display a page containing only a string
543 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
545 printf("HTTP/1.0 200 OK\n");
547 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
548 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
549 wprintf("<B>%s</B>\n", titlebarmsg);
550 wprintf("</FONT></TD></TR></TABLE><BR>\n");
551 escputs(messagetext);
558 void display_error(char *errormessage)
560 convenience_page("770000", "Error", errormessage);
563 void display_success(char *successmessage)
565 convenience_page("007700", "OK", successmessage);
570 void extract_action(char *actbuf, char *cmdbuf)
574 strcpy(actbuf, cmdbuf);
575 if (!strncasecmp(actbuf, "GET /", 5))
576 strcpy(actbuf, &actbuf[5]);
577 if (!strncasecmp(actbuf, "PUT /", 5))
578 strcpy(actbuf, &actbuf[5]);
579 if (!strncasecmp(actbuf, "POST /", 6))
580 strcpy(actbuf, &actbuf[6]);
582 for (i = 0; i < strlen(actbuf); ++i) {
583 if (actbuf[i] == ' ') {
587 if (actbuf[i] == '/') {
591 if (actbuf[i] == '?') {
595 if (actbuf[i] == '&') {
603 void upload_handler(char *name, char *filename, char *encoding,
604 void *content, char *cbtype, size_t length)
607 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
608 fprintf(stderr, " name = %s\n", name);
609 fprintf(stderr, "filename = %s\n", filename);
610 fprintf(stderr, "encoding = %s\n", encoding);
611 fprintf(stderr, " type = %s\n", cbtype);
612 fprintf(stderr, " length = %d\n", length);
614 if (strlen(name) > 0) {
615 upload = malloc(length);
616 if (upload != NULL) {
617 upload_length = length;
618 memcpy(upload, content, length);
624 void session_loop(char *browser_host, char *user_agent)
630 int ContentLength = 0;
631 char ContentType[512];
634 /* We stuff these with the values coming from the client cookies,
635 * so we can use them to reconnect a timed out session if we have to.
639 char c_username[256];
640 char c_password[256];
641 char c_roomname[256];
644 strcpy(c_host, defaulthost);
645 strcpy(c_port, defaultport);
646 strcpy(c_username, "");
647 strcpy(c_password, "");
648 strcpy(c_roomname, "");
653 if (getz(cmd) == NULL)
655 extract_action(action, cmd);
658 if (getz(buf) == NULL)
661 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
662 strcpy(cookie, &buf[15]);
663 cookie_to_stuff(cookie, NULL,
664 c_username, c_password, c_roomname);
666 if (!strncasecmp(buf, "Content-length: ", 16)) {
667 ContentLength = atoi(&buf[16]);
669 if (!strncasecmp(buf, "Content-type: ", 14)) {
670 strcpy(ContentType, &buf[14]);
672 } while (strlen(buf) > 0);
676 if (ContentLength > 0) {
677 content = malloc(ContentLength + 1);
678 fread(content, ContentLength, 1, stdin);
680 content[ContentLength] = 0;
682 if (!strncasecmp(ContentType,
683 "application/x-www-form-urlencoded", 33)) {
685 } else if (!strncasecmp(ContentType, "multipart", 9)) {
686 mime_parser(content, ContentLength, ContentType,
693 /* If there are variables in the URL, we must grab them now */
694 for (a = 0; a < strlen(cmd); ++a)
695 if ((cmd[a] == '?') || (cmd[a] == '&')) {
696 for (b = a; b < strlen(cmd); ++b)
699 addurls(&cmd[a + 1]);
703 * If we're not connected to a Citadel server, try to hook up the
704 * connection now. Preference is given to the host and port specified
705 * by browser cookies, if cookies have been supplied.
708 if (strlen(bstr("host")) > 0)
709 strcpy(c_host, bstr("host"));
710 if (strlen(bstr("port")) > 0)
711 strcpy(c_port, bstr("port"));
712 serv_sock = connectsock(c_host, c_port, "tcp");
718 serv_gets(buf); /* get the server welcome message */
719 get_serv_info(browser_host, user_agent);
721 check_for_express_messages();
724 * If we're not logged in, but we have username and password cookies
725 * supplied by the browser, try using them to log in.
727 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
728 serv_printf("USER %s", c_username);
731 serv_printf("PASS %s", c_password);
734 become_logged_in(c_username, c_password, buf);
739 * If we don't have a current room, but a cookie specifying the
740 * current room is supplied, make an effort to go there.
742 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
743 serv_printf("GOTO %s", c_roomname);
746 strcpy(wc_roomname, c_roomname);
749 if (!strcasecmp(action, "static")) {
750 strcpy(buf, &cmd[12]);
751 for (a = 0; a < strlen(buf); ++a)
755 } else if (!strcasecmp(action, "image")) {
757 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
759 } else if (!logged_in) {
762 /* Various commands... */
764 else if (!strcasecmp(action, "do_welcome")) {
766 } else if (!strcasecmp(action, "display_main_menu")) {
768 } else if (!strcasecmp(action, "advanced")) {
769 display_advanced_menu();
770 } else if (!strcasecmp(action, "whobbs")) {
772 } else if (!strcasecmp(action, "knrooms")) {
773 list_all_rooms_by_floor();
774 } else if (!strcasecmp(action, "gotonext")) {
777 } else if (!strcasecmp(action, "skip")) {
779 } else if (!strcasecmp(action, "ungoto")) {
781 } else if (!strcasecmp(action, "dotgoto")) {
783 smart_goto(bstr("room"));
784 } else if (!strcasecmp(action, "termquit")) {
786 } else if (!strcasecmp(action, "readnew")) {
788 } else if (!strcasecmp(action, "readold")) {
790 } else if (!strcasecmp(action, "readfwd")) {
792 } else if (!strcasecmp(action, "display_enter")) {
794 } else if (!strcasecmp(action, "post")) {
796 } else if (!strcasecmp(action, "confirm_delete_msg")) {
797 confirm_delete_msg();
798 } else if (!strcasecmp(action, "delete_msg")) {
800 } else if (!strcasecmp(action, "confirm_move_msg")) {
802 } else if (!strcasecmp(action, "move_msg")) {
804 } else if (!strcasecmp(action, "userlist")) {
806 } else if (!strcasecmp(action, "showuser")) {
808 } else if (!strcasecmp(action, "display_page")) {
810 } else if (!strcasecmp(action, "page_user")) {
812 } else if (!strcasecmp(action, "chat")) {
814 } else if (!strcasecmp(action, "display_private")) {
815 display_private("", 0);
816 } else if (!strcasecmp(action, "goto_private")) {
818 } else if (!strcasecmp(action, "zapped_list")) {
820 } else if (!strcasecmp(action, "display_zap")) {
822 } else if (!strcasecmp(action, "zap")) {
824 } else if (!strcasecmp(action, "display_entroom")) {
826 } else if (!strcasecmp(action, "entroom")) {
828 } else if (!strcasecmp(action, "display_editroom")) {
830 } else if (!strcasecmp(action, "editroom")) {
832 } else if (!strcasecmp(action, "display_editinfo")) {
833 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
834 } else if (!strcasecmp(action, "editinfo")) {
835 save_edit("Room info", "EINF 1", 1);
836 } else if (!strcasecmp(action, "display_editbio")) {
837 sprintf(buf, "RBIO %s", wc_username);
838 display_edit("Your bio", "NOOP", buf, "editbio");
839 } else if (!strcasecmp(action, "editbio")) {
840 save_edit("Your bio", "EBIO", 0);
841 } else if (!strcasecmp(action, "confirm_delete_room")) {
842 confirm_delete_room();
843 } else if (!strcasecmp(action, "delete_room")) {
845 } else if (!strcasecmp(action, "validate")) {
847 } else if (!strcasecmp(action, "display_editpic")) {
848 display_graphics_upload("your photo",
851 } else if (!strcasecmp(action, "editpic")) {
852 do_graphics_upload("UIMG 1|_userpic_");
853 } else if (!strcasecmp(action, "display_editroompic")) {
854 display_graphics_upload("the graphic for this room",
857 } else if (!strcasecmp(action, "editroompic")) {
858 do_graphics_upload("UIMG 1|_roompic_");
859 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
860 select_floor_to_edit_pic();
861 } else if (!strcasecmp(action, "display_editfloorpic")) {
862 sprintf(buf, "UIMG 0|_floorpic_|%s",
863 bstr("which_floor"));
864 display_graphics_upload("the graphic for this floor",
867 } else if (!strcasecmp(action, "editfloorpic")) {
868 sprintf(buf, "UIMG 1|_floorpic_|%s",
869 bstr("which_floor"));
870 do_graphics_upload(buf);
871 } else if (!strcasecmp(action, "display_reg")) {
873 } else if (!strcasecmp(action, "register")) {
875 } else if (!strcasecmp(action, "display_changepw")) {
877 } else if (!strcasecmp(action, "changepw")) {
879 } else if (!strcasecmp(action, "display_edit_node")) {
881 } else if (!strcasecmp(action, "display_netconf")) {
883 } else if (!strcasecmp(action, "display_confirm_unshare")) {
884 display_confirm_unshare();
885 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
886 display_confirm_delete_node();
887 } else if (!strcasecmp(action, "delete_node")) {
889 } else if (!strcasecmp(action, "unshare")) {
891 } else if (!strcasecmp(action, "display_add_node")) {
893 } else if (!strcasecmp(action, "add_node")) {
895 } else if (!strcasecmp(action, "display_share")) {
897 } else if (!strcasecmp(action, "share")) {
899 } else if (!strcasecmp(action, "terminate_session")) {
902 } else if (!strcasecmp(action, "edit_me")) {
904 } else if (!strcasecmp(action, "display_siteconfig")) {
905 display_siteconfig();
906 } else if (!strcasecmp(action, "siteconfig")) {
908 } else if (!strcasecmp(action, "display_generic")) {
910 } else if (!strcasecmp(action, "do_generic")) {
912 } else if (!strcasecmp(action, "display_menubar")) {
914 } else if (!strcasecmp(action, "diagnostics")) {
915 printf("HTTP/1.0 200 OK\n");
918 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
919 wprintf("You're in session %d<HR>\n", wc_session);
920 wprintf("Command: <BR><PRE>\n");
922 wprintf("</PRE><HR>\n");
923 wprintf("Variables: <BR><PRE>\n");
925 wprintf("</PRE><HR>\n");
928 /* When all else fais, display the main menu. */
934 if (content != NULL) {
939 if (upload_length > 0) {
945 int main(int argc, char *argv[])
953 "webcit: usage error (argc must be 6, not %d)\n",
958 wc_session = atoi(argv[1]);
959 defaulthost = argv[2];
960 defaultport = argv[3];
962 strcpy(wc_username, "");
963 strcpy(wc_password, "");
964 strcpy(wc_roomname, "");
966 /* Clear out serv_info and temporarily set the value of serv_humannode
967 * to a default value, because it'll be used in HTML page titles
969 memset(&serv_info, 0, sizeof(serv_info));
970 strcpy(serv_info.serv_humannode, "WebCit");
972 strcpy(browser, argv[5]);
973 bd = browser_braindamage_check(browser);
976 session_loop(argv[4], browser);