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] == 34)
265 else if (strbuf[a] == LB)
267 else if (strbuf[a] == RB)
269 else if (strbuf[a] == QU)
271 else if ((strbuf[a] == 32) && (nbsp == 1)) {
274 wprintf("%c", strbuf[a]);
279 void escputs(char *strbuf)
286 char *urlesc(char *strbuf)
289 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
290 static char outbuf[512];
294 for (a = 0; a < strlen(strbuf); ++a) {
296 for (b = 0; b < strlen(ec); ++b) {
297 if (strbuf[a] == ec[b])
302 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
304 sprintf(&outbuf[b], "%c", strbuf[a]);
309 void urlescputs(char *strbuf)
311 wprintf("%s", urlesc(strbuf));
318 * Get a line of text from the webserver (which originally came from the
319 * user's browser), checking for sanity etc.
321 char *getz(char *buf)
327 /* If fgets() fails, it's because the webserver crashed, so kill off
330 if (fgets(buf, 256, stdin) == NULL) {
332 fprintf(stderr, "webcit: exit code %d (%s)\n",
337 /* Otherwise, strip out nonprintables and resume our happy day.
340 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
341 buf[strlen(buf) - 1] = 0;
349 * Output all that important stuff that the browser will want to see
351 * print_standard_html_head values:
352 * 0 = Nothing. Do not display any leading HTTP or HTML.
353 * 1 = HTTP headers plus the "fake frames" found in most windows.
354 * 2 = HTTP headers required to terminate the session (unset cookies)
355 * 3 = HTTP headers only.
357 void output_headers(int print_standard_html_head)
360 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
363 printf("Server: %s\n", SERVER);
364 printf("Connection: close\n");
366 if (print_standard_html_head > 0) {
367 printf("Pragma: no-cache\n");
368 printf("Cache-Control: no-store\n");
370 stuff_to_cookie(cookie, wc_session, wc_username,
371 wc_password, wc_roomname);
372 if (print_standard_html_head == 2) {
373 printf("X-WebCit-Session: close\n");
374 printf("Set-cookie: webcit=%s\n", unset);
376 printf("Set-cookie: webcit=%s\n", cookie);
379 if (print_standard_html_head > 0) {
380 wprintf("<HTML><HEAD><TITLE>");
381 escputs(serv_info.serv_humannode);
382 wprintf("</TITLE></HEAD>");
383 if (ExpressMessages != NULL) {
384 wprintf("<SCRIPT language=\"javascript\">\n");
385 wprintf("function ExpressMessage() {\n");
386 wprintf(" alert(\"");
387 escputs(ExpressMessages);
389 wprintf(" }\n </SCRIPT>\n");
392 if (ExpressMessages != NULL) {
393 wprintf("onload=\"ExpressMessage()\" ");
394 free(ExpressMessages);
395 ExpressMessages = NULL;
397 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
400 if (print_standard_html_head == 1) {
401 wprintf("<A NAME=\"TheTop\"></A>"
402 "<TABLE border=0 width=100%>"
403 "<TR VALIGN=TOP><TD>");
407 wprintf("</TD><TD VALIGN=TOP>"
408 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
411 embed_room_banner(NULL);
413 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
422 void ExpressMessageCat(char *buf) {
423 if (ExpressMessages == NULL) {
424 ExpressMessages = malloc(strlen(buf) + 4);
425 strcpy(ExpressMessages, "");
427 ExpressMessages = realloc(ExpressMessages,
428 (strlen(ExpressMessages) + strlen(buf) + 4));
430 strcat(ExpressMessages, buf);
431 strcat(ExpressMessages, "\\n");
435 void check_for_express_messages()
442 while (serv_gets(buf), strcmp(buf, "000")) {
443 ExpressMessageCat(buf);
451 void output_static(char *what)
458 sprintf(buf, "static/%s", what);
459 fp = fopen(buf, "rb");
461 printf("HTTP/1.0 404 %s\n", strerror(errno));
463 printf("Content-Type: text/plain\n");
464 sprintf(buf, "%s: %s\n", what, strerror(errno));
465 printf("Content-length: %d\n", strlen(buf));
467 fwrite(buf, strlen(buf), 1, stdout);
469 printf("HTTP/1.0 200 OK\n");
472 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
473 printf("Content-type: image/gif\n");
474 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
475 printf("Content-type: text/plain\n");
476 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
477 printf("Content-type: image/jpeg\n");
478 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
479 printf("Content-type: text/html\n");
481 printf("Content-type: application/octet-stream\n");
483 fstat(fileno(fp), &statbuf);
484 bytes = statbuf.st_size;
485 printf("Content-length: %ld\n", (long) bytes);
488 putc(getc(fp), stdout);
501 off_t accomplished = 0L;
504 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
507 bytes = extract_long(&buf[4], 0);
508 printf("HTTP/1.0 200 OK\n");
510 printf("Content-type: image/gif\n");
511 printf("Content-length: %ld\n", (long) bytes);
514 while (bytes > (off_t) 0) {
515 thisblock = (off_t) sizeof(xferbuf);
516 if (thisblock > bytes)
518 serv_printf("READ %ld|%ld", accomplished, thisblock);
521 thisblock = extract_long(&buf[4], 0);
522 serv_read(xferbuf, (int) thisblock);
523 fwrite(xferbuf, thisblock, 1, stdout);
524 bytes = bytes - thisblock;
525 accomplished = accomplished + thisblock;
531 printf("HTTP/1.0 404 %s\n", strerror(errno));
533 printf("Content-Type: text/plain\n");
534 sprintf(buf, "Error retrieving image\n");
535 printf("Content-length: %d\n", strlen(buf));
537 fwrite(buf, strlen(buf), 1, stdout);
544 * Convenience functions to display a page containing only a string
546 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
548 printf("HTTP/1.0 200 OK\n");
550 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
551 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
552 wprintf("<B>%s</B>\n", titlebarmsg);
553 wprintf("</FONT></TD></TR></TABLE><BR>\n");
554 escputs(messagetext);
561 void display_error(char *errormessage)
563 convenience_page("770000", "Error", errormessage);
566 void display_success(char *successmessage)
568 convenience_page("007700", "OK", successmessage);
573 void extract_action(char *actbuf, char *cmdbuf)
577 strcpy(actbuf, cmdbuf);
578 if (!strncasecmp(actbuf, "GET /", 5))
579 strcpy(actbuf, &actbuf[5]);
580 if (!strncasecmp(actbuf, "PUT /", 5))
581 strcpy(actbuf, &actbuf[5]);
582 if (!strncasecmp(actbuf, "POST /", 6))
583 strcpy(actbuf, &actbuf[6]);
585 for (i = 0; i < strlen(actbuf); ++i) {
586 if (actbuf[i] == ' ') {
590 if (actbuf[i] == '/') {
594 if (actbuf[i] == '?') {
598 if (actbuf[i] == '&') {
606 void upload_handler(char *name, char *filename, char *encoding,
607 void *content, char *cbtype, size_t length)
610 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
611 fprintf(stderr, " name = %s\n", name);
612 fprintf(stderr, "filename = %s\n", filename);
613 fprintf(stderr, "encoding = %s\n", encoding);
614 fprintf(stderr, " type = %s\n", cbtype);
615 fprintf(stderr, " length = %d\n", length);
617 if (strlen(name) > 0) {
618 upload = malloc(length);
619 if (upload != NULL) {
620 upload_length = length;
621 memcpy(upload, content, length);
627 void session_loop(char *browser_host, char *user_agent)
633 int ContentLength = 0;
634 char ContentType[512];
637 /* We stuff these with the values coming from the client cookies,
638 * so we can use them to reconnect a timed out session if we have to.
642 char c_username[256];
643 char c_password[256];
644 char c_roomname[256];
647 strcpy(c_host, defaulthost);
648 strcpy(c_port, defaultport);
649 strcpy(c_username, "");
650 strcpy(c_password, "");
651 strcpy(c_roomname, "");
656 if (getz(cmd) == NULL)
658 extract_action(action, cmd);
661 if (getz(buf) == NULL)
664 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
665 strcpy(cookie, &buf[15]);
666 cookie_to_stuff(cookie, NULL,
667 c_username, c_password, c_roomname);
669 if (!strncasecmp(buf, "Content-length: ", 16)) {
670 ContentLength = atoi(&buf[16]);
672 if (!strncasecmp(buf, "Content-type: ", 14)) {
673 strcpy(ContentType, &buf[14]);
675 } while (strlen(buf) > 0);
679 if (ContentLength > 0) {
680 content = malloc(ContentLength + 1);
681 fread(content, ContentLength, 1, stdin);
683 content[ContentLength] = 0;
685 if (!strncasecmp(ContentType,
686 "application/x-www-form-urlencoded", 33)) {
688 } else if (!strncasecmp(ContentType, "multipart", 9)) {
689 mime_parser(content, ContentLength, ContentType,
696 /* If there are variables in the URL, we must grab them now */
697 for (a = 0; a < strlen(cmd); ++a)
698 if ((cmd[a] == '?') || (cmd[a] == '&')) {
699 for (b = a; b < strlen(cmd); ++b)
702 addurls(&cmd[a + 1]);
706 * If we're not connected to a Citadel server, try to hook up the
707 * connection now. Preference is given to the host and port specified
708 * by browser cookies, if cookies have been supplied.
711 if (strlen(bstr("host")) > 0)
712 strcpy(c_host, bstr("host"));
713 if (strlen(bstr("port")) > 0)
714 strcpy(c_port, bstr("port"));
715 serv_sock = connectsock(c_host, c_port, "tcp");
721 serv_gets(buf); /* get the server welcome message */
722 get_serv_info(browser_host, user_agent);
724 check_for_express_messages();
727 * If we're not logged in, but we have username and password cookies
728 * supplied by the browser, try using them to log in.
730 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
731 serv_printf("USER %s", c_username);
734 serv_printf("PASS %s", c_password);
737 become_logged_in(c_username, c_password, buf);
742 * If we don't have a current room, but a cookie specifying the
743 * current room is supplied, make an effort to go there.
745 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
746 serv_printf("GOTO %s", c_roomname);
749 strcpy(wc_roomname, c_roomname);
752 if (!strcasecmp(action, "static")) {
753 strcpy(buf, &cmd[12]);
754 for (a = 0; a < strlen(buf); ++a)
758 } else if (!strcasecmp(action, "image")) {
760 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
762 } else if (!logged_in) {
765 /* Various commands... */
767 else if (!strcasecmp(action, "do_welcome")) {
769 } else if (!strcasecmp(action, "display_main_menu")) {
771 } else if (!strcasecmp(action, "advanced")) {
772 display_advanced_menu();
773 } else if (!strcasecmp(action, "whobbs")) {
775 } else if (!strcasecmp(action, "knrooms")) {
776 list_all_rooms_by_floor();
777 } else if (!strcasecmp(action, "gotonext")) {
780 } else if (!strcasecmp(action, "skip")) {
782 } else if (!strcasecmp(action, "ungoto")) {
784 } else if (!strcasecmp(action, "dotgoto")) {
786 smart_goto(bstr("room"));
787 } else if (!strcasecmp(action, "termquit")) {
789 } else if (!strcasecmp(action, "readnew")) {
791 } else if (!strcasecmp(action, "readold")) {
793 } else if (!strcasecmp(action, "readfwd")) {
795 } else if (!strcasecmp(action, "display_enter")) {
797 } else if (!strcasecmp(action, "post")) {
799 } else if (!strcasecmp(action, "confirm_delete_msg")) {
800 confirm_delete_msg();
801 } else if (!strcasecmp(action, "delete_msg")) {
803 } else if (!strcasecmp(action, "confirm_move_msg")) {
805 } else if (!strcasecmp(action, "move_msg")) {
807 } else if (!strcasecmp(action, "userlist")) {
809 } else if (!strcasecmp(action, "showuser")) {
811 } else if (!strcasecmp(action, "display_page")) {
813 } else if (!strcasecmp(action, "page_user")) {
815 } else if (!strcasecmp(action, "chat")) {
817 } else if (!strcasecmp(action, "display_private")) {
818 display_private("", 0);
819 } else if (!strcasecmp(action, "goto_private")) {
821 } else if (!strcasecmp(action, "zapped_list")) {
823 } else if (!strcasecmp(action, "display_zap")) {
825 } else if (!strcasecmp(action, "zap")) {
827 } else if (!strcasecmp(action, "display_entroom")) {
829 } else if (!strcasecmp(action, "entroom")) {
831 } else if (!strcasecmp(action, "display_editroom")) {
833 } else if (!strcasecmp(action, "editroom")) {
835 } else if (!strcasecmp(action, "display_editinfo")) {
836 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
837 } else if (!strcasecmp(action, "editinfo")) {
838 save_edit("Room info", "EINF 1", 1);
839 } else if (!strcasecmp(action, "display_editbio")) {
840 sprintf(buf, "RBIO %s", wc_username);
841 display_edit("Your bio", "NOOP", buf, "editbio");
842 } else if (!strcasecmp(action, "editbio")) {
843 save_edit("Your bio", "EBIO", 0);
844 } else if (!strcasecmp(action, "confirm_delete_room")) {
845 confirm_delete_room();
846 } else if (!strcasecmp(action, "delete_room")) {
848 } else if (!strcasecmp(action, "validate")) {
850 } else if (!strcasecmp(action, "display_editpic")) {
851 display_graphics_upload("your photo",
854 } else if (!strcasecmp(action, "editpic")) {
855 do_graphics_upload("UIMG 1|_userpic_");
856 } else if (!strcasecmp(action, "display_editroompic")) {
857 display_graphics_upload("the graphic for this room",
860 } else if (!strcasecmp(action, "editroompic")) {
861 do_graphics_upload("UIMG 1|_roompic_");
862 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
863 select_floor_to_edit_pic();
864 } else if (!strcasecmp(action, "display_editfloorpic")) {
865 sprintf(buf, "UIMG 0|_floorpic_|%s",
866 bstr("which_floor"));
867 display_graphics_upload("the graphic for this floor",
870 } else if (!strcasecmp(action, "editfloorpic")) {
871 sprintf(buf, "UIMG 1|_floorpic_|%s",
872 bstr("which_floor"));
873 do_graphics_upload(buf);
874 } else if (!strcasecmp(action, "display_reg")) {
876 } else if (!strcasecmp(action, "register")) {
878 } else if (!strcasecmp(action, "display_changepw")) {
880 } else if (!strcasecmp(action, "changepw")) {
882 } else if (!strcasecmp(action, "display_edit_node")) {
884 } else if (!strcasecmp(action, "display_netconf")) {
886 } else if (!strcasecmp(action, "display_confirm_unshare")) {
887 display_confirm_unshare();
888 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
889 display_confirm_delete_node();
890 } else if (!strcasecmp(action, "delete_node")) {
892 } else if (!strcasecmp(action, "unshare")) {
894 } else if (!strcasecmp(action, "display_add_node")) {
896 } else if (!strcasecmp(action, "add_node")) {
898 } else if (!strcasecmp(action, "display_share")) {
900 } else if (!strcasecmp(action, "share")) {
902 } else if (!strcasecmp(action, "terminate_session")) {
905 } else if (!strcasecmp(action, "edit_me")) {
907 } else if (!strcasecmp(action, "display_siteconfig")) {
908 display_siteconfig();
909 } else if (!strcasecmp(action, "siteconfig")) {
911 } else if (!strcasecmp(action, "display_generic")) {
913 } else if (!strcasecmp(action, "do_generic")) {
915 } else if (!strcasecmp(action, "display_menubar")) {
917 } else if (!strcasecmp(action, "diagnostics")) {
918 printf("HTTP/1.0 200 OK\n");
921 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
922 wprintf("You're in session %d<HR>\n", wc_session);
923 wprintf("Command: <BR><PRE>\n");
925 wprintf("</PRE><HR>\n");
926 wprintf("Variables: <BR><PRE>\n");
928 wprintf("</PRE><HR>\n");
931 /* When all else fais, display the main menu. */
937 if (content != NULL) {
942 if (upload_length > 0) {
948 int main(int argc, char *argv[])
956 "webcit: usage error (argc must be 6, not %d)\n",
961 wc_session = atoi(argv[1]);
962 defaulthost = argv[2];
963 defaultport = argv[3];
965 strcpy(wc_username, "");
966 strcpy(wc_password, "");
967 strcpy(wc_roomname, "");
969 /* Clear out serv_info and temporarily set the value of serv_humannode
970 * to a default value, because it'll be used in HTML page titles
972 memset(&serv_info, 0, sizeof(serv_info));
973 strcpy(serv_info.serv_humannode, "WebCit");
975 strcpy(browser, argv[5]);
976 bd = browser_braindamage_check(browser);
979 session_loop(argv[4], browser);