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"
28 char wc_username[256];
29 char wc_password[256];
30 char wc_roomname[256];
31 int TransactionCount = 0;
35 char *ExpressMessages = NULL;
40 struct webcontent *wlist = NULL;
41 struct webcontent *wlast = NULL;
43 struct urlcontent *urlstrings = NULL;
45 static const char *defaulthost = DEFAULT_HOST;
46 static const char *defaultport = DEFAULT_PORT;
48 int upload_length = 0;
52 void unescape_input(char *buf)
57 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
58 buf[strlen(buf) - 1] = 0;
60 for (a = 0; a < strlen(buf); ++a) {
67 sscanf(hex, "%02x", &b);
69 strcpy(&buf[a + 1], &buf[a + 3]);
76 void addurls(char *url)
84 while (strlen(up) > 0) {
86 /* locate the = sign */
87 strncpy(buf, up, 255);
89 for (a = 255; a >= 0; --a)
96 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
99 strcpy(u->url_key, buf);
101 /* now chop that part off */
102 for (a = 0; a <= b; ++a)
105 /* locate the & sign */
108 for (a = 0; a < strlen(up); ++a) {
109 if (!strncmp(ptr, "&", 1)) {
116 for (a = 0; a < b; ++a)
120 u->url_data = malloc(strlen(up) + 1);
121 strcpy(u->url_data, up);
123 unescape_input(u->url_data);
131 struct urlcontent *u;
133 while (urlstrings != NULL) {
134 free(urlstrings->url_data);
135 u = urlstrings->next;
142 * Diagnostic function to display the contents of all variables
146 struct urlcontent *u;
148 for (u = urlstrings; u != NULL; u = u->next) {
149 wprintf("%38s = %s\n", u->url_key, u->url_data);
153 char *bstr(char *key)
155 struct urlcontent *u;
157 for (u = urlstrings; u != NULL; u = u->next) {
158 if (!strcasecmp(u->url_key, key))
159 return (u->url_data);
165 void wprintf(const char *format,...)
168 struct webcontent *wptr;
170 wptr = (struct webcontent *) malloc(sizeof(struct webcontent));
180 va_start(arg_ptr, format);
181 vsprintf(wptr->w_data, format, arg_ptr);
185 int wContentLength(void)
187 struct webcontent *wptr;
190 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
191 len = len + strlen(wptr->w_data);
198 * wDumpContent() takes all the stuff that's been queued up using
199 * the wprintf() and escputs() functions, and sends it out to the browser.
200 * By queuing instead of transmitting as it's generated, we're able to
201 * calculate a Content-length: header.
203 * print_standard_html_footer should be set to 0 to transmit only, 1 to
204 * append the main menu (if in noframes mode) and closing tags, or 2 to
205 * append the closing tags only.
207 void wDumpContent(int print_standard_html_footer)
209 struct webcontent *wptr;
212 wprintf("</TD></TR></TABLE></TABLE>\n");
216 if (print_standard_html_footer) {
217 if ((noframes) && (print_standard_html_footer != 2)) {
219 /* embed_main_menu(); */ /* not any more */
221 wprintf("</BODY></HTML>\n");
223 printf("Content-type: text/html\n");
224 printf("Content-length: %d\n", wContentLength());
227 while (wlist != NULL) {
228 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
237 void escputs1(char *strbuf, int nbsp)
241 for (a = 0; a < strlen(strbuf); ++a) {
242 if (strbuf[a] == '<')
244 else if (strbuf[a] == '>')
246 else if (strbuf[a] == '&')
248 else if (strbuf[a] == 34)
250 else if (strbuf[a] == LB)
252 else if (strbuf[a] == RB)
254 else if (strbuf[a] == QU)
256 else if ((strbuf[a] == 32) && (nbsp == 1)) {
259 wprintf("%c", strbuf[a]);
264 void escputs(char *strbuf)
271 char *urlesc(char *strbuf)
274 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
275 static char outbuf[512];
279 for (a = 0; a < strlen(strbuf); ++a) {
281 for (b = 0; b < strlen(ec); ++b) {
282 if (strbuf[a] == ec[b])
287 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
289 sprintf(&outbuf[b], "%c", strbuf[a]);
294 void urlescputs(char *strbuf)
296 wprintf("%s", urlesc(strbuf));
303 * Get a line of text from the webserver (which originally came from the
304 * user's browser), checking for sanity etc.
306 char *getz(char *buf)
312 /* If fgets() fails, it's because the webserver crashed, so kill off
315 if (fgets(buf, 256, stdin) == NULL) {
317 fprintf(stderr, "webcit: exit code %d (%s)\n",
322 /* Otherwise, strip out nonprintables and resume our happy day.
325 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
326 buf[strlen(buf) - 1] = 0;
334 * Output all that important stuff that the browser will want to see
336 * If print_standard_html_head is nonzero, we also get some standard HTML
337 * headers. If it's set to 2, the session is considered to be closing.
339 void output_headers(int print_standard_html_head)
342 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
345 printf("Server: %s\n", SERVER);
346 printf("Connection: close\n");
348 if (print_standard_html_head > 0) {
349 printf("Pragma: no-cache\n");
350 printf("Cache-Control: no-store\n");
352 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
353 wc_roomname, noframes);
354 if (print_standard_html_head == 2) {
355 printf("X-WebCit-Session: close\n");
356 printf("Set-cookie: webcit=%s\n", unset);
358 printf("Set-cookie: webcit=%s\n", cookie);
361 if (print_standard_html_head > 0) {
362 wprintf("<HTML><HEAD><TITLE>");
363 escputs(serv_info.serv_humannode);
364 wprintf("</TITLE></HEAD>");
365 if (ExpressMessages != NULL) {
366 wprintf("<SCRIPT language=\"javascript\">\n");
367 wprintf("function ExpressMessage() {\n");
368 wprintf(" alert(\"");
369 escputs(ExpressMessages);
371 wprintf(" }\n </SCRIPT>\n");
374 if (ExpressMessages != NULL) {
375 wprintf("onload=\"ExpressMessage()\" ");
376 free(ExpressMessages);
377 ExpressMessages = NULL;
379 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
382 if ((print_standard_html_head == 1) && (noframes == 1)) {
383 wprintf("<TABLE border=0 width=100%>");
384 wprintf("<TR VALIGN=TOP><TD>");
388 wprintf("</TD><TD VALIGN=TOP>"
389 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
392 embed_room_banner(NULL);
394 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
404 void ExpressMessageCat(char *buf) {
405 if (ExpressMessages == NULL) {
406 ExpressMessages = malloc(strlen(buf) + 4);
407 strcpy(ExpressMessages, "");
409 ExpressMessages = realloc(ExpressMessages,
410 (strlen(ExpressMessages) + strlen(buf) + 4));
412 strcat(ExpressMessages, buf);
413 strcat(ExpressMessages, "\\n");
417 void check_for_express_messages()
424 while (serv_gets(buf), strcmp(buf, "000")) {
425 ExpressMessageCat(buf);
433 void output_static(char *what)
440 sprintf(buf, "static/%s", what);
441 fp = fopen(buf, "rb");
443 printf("HTTP/1.0 404 %s\n", strerror(errno));
445 printf("Content-Type: text/plain\n");
446 sprintf(buf, "%s: %s\n", what, strerror(errno));
447 printf("Content-length: %d\n", strlen(buf));
449 fwrite(buf, strlen(buf), 1, stdout);
451 printf("HTTP/1.0 200 OK\n");
454 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
455 printf("Content-type: image/gif\n");
456 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
457 printf("Content-type: image/jpeg\n");
458 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
459 printf("Content-type: text/html\n");
461 printf("Content-type: application/octet-stream\n");
463 fstat(fileno(fp), &statbuf);
464 bytes = statbuf.st_size;
465 printf("Content-length: %ld\n", (long) bytes);
468 putc(getc(fp), stdout);
481 off_t accomplished = 0L;
484 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
487 bytes = extract_long(&buf[4], 0);
488 printf("HTTP/1.0 200 OK\n");
490 printf("Content-type: image/gif\n");
491 printf("Content-length: %ld\n", (long) bytes);
494 while (bytes > (off_t) 0) {
495 thisblock = (off_t) sizeof(xferbuf);
496 if (thisblock > bytes)
498 serv_printf("READ %ld|%ld", accomplished, thisblock);
501 thisblock = extract_long(&buf[4], 0);
502 serv_read(xferbuf, (int) thisblock);
503 fwrite(xferbuf, thisblock, 1, stdout);
504 bytes = bytes - thisblock;
505 accomplished = accomplished + thisblock;
511 printf("HTTP/1.0 404 %s\n", strerror(errno));
513 printf("Content-Type: text/plain\n");
514 sprintf(buf, "Error retrieving image\n");
515 printf("Content-length: %d\n", strlen(buf));
517 fwrite(buf, strlen(buf), 1, stdout);
524 * Convenience functions to display a page containing only a string
526 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
528 printf("HTTP/1.0 200 OK\n");
530 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
531 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
532 wprintf("<B>%s</B>\n", titlebarmsg);
533 wprintf("</FONT></TD></TR></TABLE><BR>\n");
534 escputs(messagetext);
543 void display_error(char *errormessage)
545 convenience_page("770000", "Error", errormessage);
548 void display_success(char *successmessage)
550 convenience_page("007700", "OK", successmessage);
555 void extract_action(char *actbuf, char *cmdbuf)
559 strcpy(actbuf, cmdbuf);
560 if (!strncasecmp(actbuf, "GET /", 5))
561 strcpy(actbuf, &actbuf[5]);
562 if (!strncasecmp(actbuf, "PUT /", 5))
563 strcpy(actbuf, &actbuf[5]);
564 if (!strncasecmp(actbuf, "POST /", 6))
565 strcpy(actbuf, &actbuf[6]);
567 for (i = 0; i < strlen(actbuf); ++i) {
568 if (actbuf[i] == ' ') {
572 if (actbuf[i] == '/') {
576 if (actbuf[i] == '?') {
580 if (actbuf[i] == '&') {
588 void upload_handler(char *name, char *filename, char *encoding,
589 void *content, char *cbtype, size_t length)
592 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
593 fprintf(stderr, " name = %s\n", name);
594 fprintf(stderr, "filename = %s\n", filename);
595 fprintf(stderr, "encoding = %s\n", encoding);
596 fprintf(stderr, " type = %s\n", cbtype);
597 fprintf(stderr, " length = %d\n", length);
599 if (strlen(name) > 0) {
600 upload = malloc(length);
601 if (upload != NULL) {
602 upload_length = length;
603 memcpy(upload, content, length);
609 void session_loop(char *browser_host)
615 int ContentLength = 0;
616 char ContentType[512];
619 /* We stuff these with the values coming from the client cookies,
620 * so we can use them to reconnect a timed out session if we have to.
624 char c_username[256];
625 char c_password[256];
626 char c_roomname[256];
629 strcpy(c_host, defaulthost);
630 strcpy(c_port, defaultport);
631 strcpy(c_username, "");
632 strcpy(c_password, "");
633 strcpy(c_roomname, "");
638 if (getz(cmd) == NULL)
640 extract_action(action, cmd);
643 if (getz(buf) == NULL)
646 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
647 strcpy(cookie, &buf[15]);
648 cookie_to_stuff(cookie, NULL,
649 c_username, c_password, c_roomname,
652 if (!strncasecmp(buf, "Content-length: ", 16)) {
653 ContentLength = atoi(&buf[16]);
655 if (!strncasecmp(buf, "Content-type: ", 14)) {
656 strcpy(ContentType, &buf[14]);
658 } while (strlen(buf) > 0);
662 if (ContentLength > 0) {
663 content = malloc(ContentLength + 1);
664 fread(content, ContentLength, 1, stdin);
666 content[ContentLength] = 0;
668 if (!strncasecmp(ContentType,
669 "application/x-www-form-urlencoded", 33)) {
671 } else if (!strncasecmp(ContentType, "multipart", 9)) {
672 mime_parser(content, ContentLength, ContentType,
679 /* If there are variables in the URL, we must grab them now */
680 for (a = 0; a < strlen(cmd); ++a)
681 if ((cmd[a] == '?') || (cmd[a] == '&')) {
682 for (b = a; b < strlen(cmd); ++b)
685 addurls(&cmd[a + 1]);
689 * If we're not connected to a Citadel server, try to hook up the
690 * connection now. Preference is given to the host and port specified
691 * by browser cookies, if cookies have been supplied.
694 if (strlen(bstr("host")) > 0)
695 strcpy(c_host, bstr("host"));
696 if (strlen(bstr("port")) > 0)
697 strcpy(c_port, bstr("port"));
698 serv_sock = connectsock(c_host, c_port, "tcp");
700 serv_gets(buf); /* get the server welcome message */
701 get_serv_info(browser_host);
703 check_for_express_messages();
706 * If we're not logged in, but we have username and password cookies
707 * supplied by the browser, try using them to log in.
709 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
710 serv_printf("USER %s", c_username);
713 serv_printf("PASS %s", c_password);
716 become_logged_in(c_username, c_password, buf);
721 * If we don't have a current room, but a cookie specifying the
722 * current room is supplied, make an effort to go there.
724 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
725 serv_printf("GOTO %s", c_roomname);
728 strcpy(wc_roomname, c_roomname);
731 if (!strcasecmp(action, "static")) {
732 strcpy(buf, &cmd[12]);
733 for (a = 0; a < strlen(buf); ++a)
737 } else if (!strcasecmp(action, "image")) {
739 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
741 } else if (!logged_in) {
744 /* Various commands... */
746 else if (!strcasecmp(action, "do_welcome")) {
748 } else if (!strcasecmp(action, "display_main_menu")) {
750 } else if (!strcasecmp(action, "advanced")) {
751 display_advanced_menu();
752 } else if (!strcasecmp(action, "whobbs")) {
754 } else if (!strcasecmp(action, "knrooms")) {
755 list_all_rooms_by_floor();
756 } else if (!strcasecmp(action, "gotonext")) {
759 } else if (!strcasecmp(action, "skip")) {
761 } else if (!strcasecmp(action, "ungoto")) {
763 } else if (!strcasecmp(action, "dotgoto")) {
765 smart_goto(bstr("room"));
766 } else if (!strcasecmp(action, "termquit")) {
768 } else if (!strcasecmp(action, "readnew")) {
770 } else if (!strcasecmp(action, "readold")) {
772 } else if (!strcasecmp(action, "readfwd")) {
774 } else if (!strcasecmp(action, "display_enter")) {
776 } else if (!strcasecmp(action, "post")) {
778 } else if (!strcasecmp(action, "confirm_delete_msg")) {
779 confirm_delete_msg();
780 } else if (!strcasecmp(action, "delete_msg")) {
782 } else if (!strcasecmp(action, "confirm_move_msg")) {
784 } else if (!strcasecmp(action, "move_msg")) {
786 } else if (!strcasecmp(action, "userlist")) {
788 } else if (!strcasecmp(action, "showuser")) {
790 } else if (!strcasecmp(action, "display_page")) {
792 } else if (!strcasecmp(action, "page_user")) {
794 } else if (!strcasecmp(action, "chat")) {
796 } else if (!strcasecmp(action, "display_private")) {
797 display_private("", 0);
798 } else if (!strcasecmp(action, "goto_private")) {
800 } else if (!strcasecmp(action, "zapped_list")) {
802 } else if (!strcasecmp(action, "display_zap")) {
804 } else if (!strcasecmp(action, "zap")) {
806 } else if (!strcasecmp(action, "display_entroom")) {
808 } else if (!strcasecmp(action, "entroom")) {
810 } else if (!strcasecmp(action, "display_editroom")) {
812 } else if (!strcasecmp(action, "editroom")) {
814 } else if (!strcasecmp(action, "display_editinfo")) {
815 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
816 } else if (!strcasecmp(action, "editinfo")) {
817 save_edit("Room info", "EINF 1", 1);
818 } else if (!strcasecmp(action, "display_editbio")) {
819 sprintf(buf, "RBIO %s", wc_username);
820 display_edit("Your bio", "NOOP", buf, "editbio");
821 } else if (!strcasecmp(action, "editbio")) {
822 save_edit("Your bio", "EBIO", 0);
823 } else if (!strcasecmp(action, "confirm_delete_room")) {
824 confirm_delete_room();
825 } else if (!strcasecmp(action, "delete_room")) {
827 } else if (!strcasecmp(action, "validate")) {
829 } else if (!strcasecmp(action, "display_editpic")) {
830 display_graphics_upload("your photo",
833 } else if (!strcasecmp(action, "editpic")) {
834 do_graphics_upload("UIMG 1|_userpic_");
835 } else if (!strcasecmp(action, "display_editroompic")) {
836 display_graphics_upload("the graphic for this room",
839 } else if (!strcasecmp(action, "editroompic")) {
840 do_graphics_upload("UIMG 1|_roompic_");
841 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
842 select_floor_to_edit_pic();
843 } else if (!strcasecmp(action, "display_editfloorpic")) {
844 sprintf(buf, "UIMG 0|_floorpic_|%s",
845 bstr("which_floor"));
846 display_graphics_upload("the graphic for this floor",
849 } else if (!strcasecmp(action, "editfloorpic")) {
850 sprintf(buf, "UIMG 1|_floorpic_|%s",
851 bstr("which_floor"));
852 do_graphics_upload(buf);
853 } else if (!strcasecmp(action, "display_reg")) {
855 } else if (!strcasecmp(action, "register")) {
857 } else if (!strcasecmp(action, "display_changepw")) {
859 } else if (!strcasecmp(action, "changepw")) {
861 } else if (!strcasecmp(action, "display_edit_node")) {
863 } else if (!strcasecmp(action, "display_netconf")) {
865 } else if (!strcasecmp(action, "display_confirm_unshare")) {
866 display_confirm_unshare();
867 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
868 display_confirm_delete_node();
869 } else if (!strcasecmp(action, "delete_node")) {
871 } else if (!strcasecmp(action, "unshare")) {
873 } else if (!strcasecmp(action, "display_add_node")) {
875 } else if (!strcasecmp(action, "add_node")) {
877 } else if (!strcasecmp(action, "display_share")) {
879 } else if (!strcasecmp(action, "share")) {
881 } else if (!strcasecmp(action, "terminate_session")) {
883 } else if (!strcasecmp(action, "edit_me")) {
885 } else if (!strcasecmp(action, "display_siteconfig")) {
886 display_siteconfig();
887 } else if (!strcasecmp(action, "siteconfig")) {
889 } else if (!strcasecmp(action, "display_generic")) {
891 } else if (!strcasecmp(action, "do_generic")) {
893 } else if (!strcasecmp(action, "display_menubar")) {
896 /* When all else fails... */
898 printf("HTTP/1.0 200 OK\n");
901 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
902 wprintf("You're in session %d<HR>\n", wc_session);
903 wprintf("Command: <BR><PRE>\n");
905 wprintf("</PRE><HR>\n");
906 wprintf("Variables: <BR><PRE>\n");
908 wprintf("</PRE><HR>\n");
913 if (content != NULL) {
918 if (upload_length > 0) {
924 int main(int argc, char *argv[])
932 "webcit: usage error (argc must be 6, not %d)\n",
937 wc_session = atoi(argv[1]);
938 defaulthost = argv[2];
939 defaultport = argv[3];
941 strcpy(wc_username, "");
942 strcpy(wc_password, "");
943 strcpy(wc_roomname, "");
945 /* Clear out serv_info and temporarily set the value of serv_humannode
946 * to a default value, because it'll be used in HTML page titles
948 memset(&serv_info, 0, sizeof(serv_info));
949 strcpy(serv_info.serv_humannode, "WebCit");
951 strcpy(browser, argv[5]);
952 bd = browser_braindamage_check(browser);
959 session_loop(argv[4]);