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;
38 struct webcontent *wlist = NULL;
39 struct webcontent *wlast = NULL;
41 struct urlcontent *urlstrings = NULL;
43 static const char *defaulthost = DEFAULT_HOST;
44 static const char *defaultport = DEFAULT_PORT;
46 int upload_length = 0;
50 void unescape_input(char *buf)
55 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
56 buf[strlen(buf) - 1] = 0;
58 for (a = 0; a < strlen(buf); ++a) {
65 sscanf(hex, "%02x", &b);
67 strcpy(&buf[a + 1], &buf[a + 3]);
74 void addurls(char *url)
82 while (strlen(up) > 0) {
84 /* locate the = sign */
85 strncpy(buf, up, 255);
87 for (a = 255; a >= 0; --a)
94 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
97 strcpy(u->url_key, buf);
99 /* now chop that part off */
100 for (a = 0; a <= b; ++a)
103 /* locate the & sign */
106 for (a = 0; a < strlen(up); ++a) {
107 if (!strncmp(ptr, "&", 1)) {
114 for (a = 0; a < b; ++a)
118 u->url_data = malloc(strlen(up) + 1);
119 strcpy(u->url_data, up);
121 unescape_input(u->url_data);
129 struct urlcontent *u;
131 while (urlstrings != NULL) {
132 free(urlstrings->url_data);
133 u = urlstrings->next;
140 * Diagnostic function to display the contents of all variables
144 struct urlcontent *u;
146 for (u = urlstrings; u != NULL; u = u->next) {
147 wprintf("%38s = %s\n", u->url_key, u->url_data);
151 char *bstr(char *key)
153 struct urlcontent *u;
155 for (u = urlstrings; u != NULL; u = u->next) {
156 if (!strcasecmp(u->url_key, key))
157 return (u->url_data);
163 void wprintf(const char *format,...)
166 struct webcontent *wptr;
168 wptr = (struct webcontent *) malloc(sizeof(struct webcontent));
178 va_start(arg_ptr, format);
179 vsprintf(wptr->w_data, format, arg_ptr);
183 int wContentLength(void)
185 struct webcontent *wptr;
188 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
189 len = len + strlen(wptr->w_data);
196 * wDumpContent() takes all the stuff that's been queued up using
197 * the wprintf() and escputs() functions, and sends it out to the browser.
198 * By queuing instead of transmitting as it's generated, we're able to
199 * calculate a Content-length: header.
201 * print_standard_html_footer should be set to 0 to transmit only, 1 to
202 * append the main menu (if in noframes mode) and closing tags, or 2 to
203 * append the closing tags only.
205 void wDumpContent(int print_standard_html_footer)
207 struct webcontent *wptr;
210 wprintf("</TD></TR></TABLE></TABLE>\n");
214 if (print_standard_html_footer) {
215 if ((noframes) && (print_standard_html_footer != 2)) {
217 /* embed_main_menu(); */ /* not any more */
219 wprintf("</BODY></HTML>\n");
221 printf("Content-type: text/html\n");
222 printf("Content-length: %d\n", wContentLength());
225 while (wlist != NULL) {
226 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
235 void escputs1(char *strbuf, int nbsp)
239 for (a = 0; a < strlen(strbuf); ++a) {
240 if (strbuf[a] == '<')
242 else if (strbuf[a] == '>')
244 else if (strbuf[a] == '&')
246 else if (strbuf[a] == 34)
248 else if (strbuf[a] == LB)
250 else if (strbuf[a] == RB)
252 else if (strbuf[a] == QU)
254 else if ((strbuf[a] == 32) && (nbsp == 1)) {
257 wprintf("%c", strbuf[a]);
262 void escputs(char *strbuf)
269 char *urlesc(char *strbuf)
272 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
273 static char outbuf[512];
277 for (a = 0; a < strlen(strbuf); ++a) {
279 for (b = 0; b < strlen(ec); ++b) {
280 if (strbuf[a] == ec[b])
285 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
287 sprintf(&outbuf[b], "%c", strbuf[a]);
292 void urlescputs(char *strbuf)
294 wprintf("%s", urlesc(strbuf));
301 * Get a line of text from the webserver (which originally came from the
302 * user's browser), checking for sanity etc.
304 char *getz(char *buf)
310 /* If fgets() fails, it's because the webserver crashed, so kill off
313 if (fgets(buf, 256, stdin) == NULL) {
315 fprintf(stderr, "webcit: exit code %d (%s)\n",
320 /* Otherwise, strip out nonprintables and resume our happy day.
323 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
324 buf[strlen(buf) - 1] = 0;
332 * Output all that important stuff that the browser will want to see
334 * If print_standard_html_head is nonzero, we also get some standard HTML
335 * headers. If it's set to 2, the session is considered to be closing.
337 void output_headers(int print_standard_html_head)
340 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
343 printf("Server: %s\n", SERVER);
344 printf("Connection: close\n");
346 if (print_standard_html_head > 0) {
347 printf("Pragma: no-cache\n");
348 printf("Cache-Control: no-store\n");
350 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
351 wc_roomname, noframes);
352 if (print_standard_html_head == 2) {
353 printf("X-WebCit-Session: close\n");
354 printf("Set-cookie: webcit=%s\n", unset);
356 printf("Set-cookie: webcit=%s\n", cookie);
359 if (print_standard_html_head > 0) {
360 wprintf("<HTML><HEAD><TITLE>");
361 escputs(serv_info.serv_humannode);
362 wprintf("</TITLE></HEAD>");
363 if (ExpressMessages != NULL) {
364 wprintf("<SCRIPT language=\"javascript\">\n");
365 wprintf("function ExpressMessage() {\n");
366 wprintf(" alert(\"");
367 escputs(ExpressMessages);
369 wprintf(" }\n </SCRIPT>\n");
372 if (ExpressMessages != NULL) {
373 wprintf("onload=\"ExpressMessage()\" ");
374 free(ExpressMessages);
375 ExpressMessages = NULL;
377 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
380 if ((print_standard_html_head == 1) && (noframes == 1)) {
381 wprintf("<TABLE border=0 width=100%>");
382 wprintf("<TR ALIGN=TOP><TD>");
386 wprintf("</TD><TD ALIGN=TOP>"
387 "<TABLE border=0 width=100%><TR ALIGN=TOP>"
390 embed_room_banner(NULL);
392 wprintf("</TD></TR><TR ALIGN=TOP><TD>\n");
404 void check_for_express_messages()
411 while (serv_gets(buf), strcmp(buf, "000")) {
412 if (ExpressMessages == NULL) {
413 ExpressMessages = malloc(strlen(buf) + 4);
414 strcpy(ExpressMessages, "");
416 ExpressMessages = realloc(ExpressMessages,
417 (strlen(ExpressMessages) + strlen(buf) + 4));
419 strcat(ExpressMessages, buf);
420 strcat(ExpressMessages, "\\n");
428 void output_static(char *what)
435 sprintf(buf, "static/%s", what);
436 fp = fopen(buf, "rb");
438 printf("HTTP/1.0 404 %s\n", strerror(errno));
440 printf("Content-Type: text/plain\n");
441 sprintf(buf, "%s: %s\n", what, strerror(errno));
442 printf("Content-length: %d\n", strlen(buf));
444 fwrite(buf, strlen(buf), 1, stdout);
446 printf("HTTP/1.0 200 OK\n");
449 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
450 printf("Content-type: image/gif\n");
451 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
452 printf("Content-type: image/jpeg\n");
453 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
454 printf("Content-type: text/html\n");
456 printf("Content-type: application/octet-stream\n");
458 fstat(fileno(fp), &statbuf);
459 bytes = statbuf.st_size;
460 printf("Content-length: %ld\n", (long) bytes);
463 putc(getc(fp), stdout);
476 off_t accomplished = 0L;
479 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
482 bytes = extract_long(&buf[4], 0);
483 printf("HTTP/1.0 200 OK\n");
485 printf("Content-type: image/gif\n");
486 printf("Content-length: %ld\n", (long) bytes);
489 while (bytes > (off_t) 0) {
490 thisblock = (off_t) sizeof(xferbuf);
491 if (thisblock > bytes)
493 serv_printf("READ %ld|%ld", accomplished, thisblock);
496 thisblock = extract_long(&buf[4], 0);
497 serv_read(xferbuf, (int) thisblock);
498 fwrite(xferbuf, thisblock, 1, stdout);
499 bytes = bytes - thisblock;
500 accomplished = accomplished + thisblock;
506 printf("HTTP/1.0 404 %s\n", strerror(errno));
508 printf("Content-Type: text/plain\n");
509 sprintf(buf, "Error retrieving image\n");
510 printf("Content-length: %d\n", strlen(buf));
512 fwrite(buf, strlen(buf), 1, stdout);
519 * Convenience functions to display a page containing only a string
521 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
523 printf("HTTP/1.0 200 OK\n");
525 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
526 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
527 wprintf("<B>%s</B>\n", titlebarmsg);
528 wprintf("</FONT></TD></TR></TABLE><BR>\n");
529 escputs(messagetext);
538 void display_error(char *errormessage)
540 convenience_page("770000", "Error", errormessage);
543 void display_success(char *successmessage)
545 convenience_page("007700", "OK", successmessage);
550 void extract_action(char *actbuf, char *cmdbuf)
554 strcpy(actbuf, cmdbuf);
555 if (!strncasecmp(actbuf, "GET /", 5))
556 strcpy(actbuf, &actbuf[5]);
557 if (!strncasecmp(actbuf, "PUT /", 5))
558 strcpy(actbuf, &actbuf[5]);
559 if (!strncasecmp(actbuf, "POST /", 6))
560 strcpy(actbuf, &actbuf[6]);
562 for (i = 0; i < strlen(actbuf); ++i) {
563 if (actbuf[i] == ' ') {
567 if (actbuf[i] == '/') {
571 if (actbuf[i] == '?') {
575 if (actbuf[i] == '&') {
583 void upload_handler(char *name, char *filename, char *encoding,
584 void *content, char *cbtype, size_t length)
587 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
588 fprintf(stderr, " name = %s\n", name);
589 fprintf(stderr, "filename = %s\n", filename);
590 fprintf(stderr, "encoding = %s\n", encoding);
591 fprintf(stderr, " type = %s\n", cbtype);
592 fprintf(stderr, " length = %d\n", length);
594 if (strlen(name) > 0) {
595 upload = malloc(length);
596 if (upload != NULL) {
597 upload_length = length;
598 memcpy(upload, content, length);
604 void session_loop(char *browser_host)
610 int ContentLength = 0;
611 char ContentType[512];
614 /* We stuff these with the values coming from the client cookies,
615 * so we can use them to reconnect a timed out session if we have to.
619 char c_username[256];
620 char c_password[256];
621 char c_roomname[256];
624 strcpy(c_host, defaulthost);
625 strcpy(c_port, defaultport);
626 strcpy(c_username, "");
627 strcpy(c_password, "");
628 strcpy(c_roomname, "");
633 if (getz(cmd) == NULL)
635 extract_action(action, cmd);
638 if (getz(buf) == NULL)
641 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
642 strcpy(cookie, &buf[15]);
643 cookie_to_stuff(cookie, NULL,
644 c_username, c_password, c_roomname,
647 if (!strncasecmp(buf, "Content-length: ", 16)) {
648 ContentLength = atoi(&buf[16]);
650 if (!strncasecmp(buf, "Content-type: ", 14)) {
651 strcpy(ContentType, &buf[14]);
653 } while (strlen(buf) > 0);
657 if (ContentLength > 0) {
658 content = malloc(ContentLength + 1);
659 fread(content, ContentLength, 1, stdin);
661 content[ContentLength] = 0;
663 if (!strncasecmp(ContentType,
664 "application/x-www-form-urlencoded", 33)) {
666 } else if (!strncasecmp(ContentType, "multipart", 9)) {
667 mime_parser(content, ContentLength, ContentType,
674 /* If there are variables in the URL, we must grab them now */
675 for (a = 0; a < strlen(cmd); ++a)
676 if ((cmd[a] == '?') || (cmd[a] == '&')) {
677 for (b = a; b < strlen(cmd); ++b)
680 addurls(&cmd[a + 1]);
684 * If we're not connected to a Citadel server, try to hook up the
685 * connection now. Preference is given to the host and port specified
686 * by browser cookies, if cookies have been supplied.
689 if (strlen(bstr("host")) > 0)
690 strcpy(c_host, bstr("host"));
691 if (strlen(bstr("port")) > 0)
692 strcpy(c_port, bstr("port"));
693 serv_sock = connectsock(c_host, c_port, "tcp");
695 serv_gets(buf); /* get the server welcome message */
696 get_serv_info(browser_host);
698 check_for_express_messages();
701 * If we're not logged in, but we have username and password cookies
702 * supplied by the browser, try using them to log in.
704 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
705 serv_printf("USER %s", c_username);
708 serv_printf("PASS %s", c_password);
711 become_logged_in(c_username, c_password, buf);
716 * If we don't have a current room, but a cookie specifying the
717 * current room is supplied, make an effort to go there.
719 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
720 serv_printf("GOTO %s", c_roomname);
723 strcpy(wc_roomname, c_roomname);
726 if (!strcasecmp(action, "static")) {
727 strcpy(buf, &cmd[12]);
728 for (a = 0; a < strlen(buf); ++a)
732 } else if (!strcasecmp(action, "image")) {
734 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
736 } else if (!logged_in) {
739 /* Various commands... */
741 else if (!strcasecmp(action, "do_welcome")) {
743 } else if (!strcasecmp(action, "display_main_menu")) {
745 } else if (!strcasecmp(action, "advanced")) {
746 display_advanced_menu();
747 } else if (!strcasecmp(action, "whobbs")) {
749 } else if (!strcasecmp(action, "knrooms")) {
750 list_all_rooms_by_floor();
751 } else if (!strcasecmp(action, "gotonext")) {
754 } else if (!strcasecmp(action, "skip")) {
756 } else if (!strcasecmp(action, "ungoto")) {
758 } else if (!strcasecmp(action, "dotgoto")) {
760 gotoroom(bstr("room"), 1);
761 } else if (!strcasecmp(action, "termquit")) {
763 } else if (!strcasecmp(action, "readnew")) {
765 } else if (!strcasecmp(action, "readold")) {
767 } else if (!strcasecmp(action, "readfwd")) {
769 } else if (!strcasecmp(action, "display_enter")) {
771 } else if (!strcasecmp(action, "post")) {
773 } else if (!strcasecmp(action, "confirm_delete_msg")) {
774 confirm_delete_msg();
775 } else if (!strcasecmp(action, "delete_msg")) {
777 } else if (!strcasecmp(action, "confirm_move_msg")) {
779 } else if (!strcasecmp(action, "move_msg")) {
781 } else if (!strcasecmp(action, "userlist")) {
783 } else if (!strcasecmp(action, "showuser")) {
785 } else if (!strcasecmp(action, "display_page")) {
787 } else if (!strcasecmp(action, "page_user")) {
789 } else if (!strcasecmp(action, "chat")) {
791 } else if (!strcasecmp(action, "display_private")) {
792 display_private("", 0);
793 } else if (!strcasecmp(action, "goto_private")) {
795 } else if (!strcasecmp(action, "zapped_list")) {
797 } else if (!strcasecmp(action, "display_zap")) {
799 } else if (!strcasecmp(action, "zap")) {
801 } else if (!strcasecmp(action, "display_entroom")) {
803 } else if (!strcasecmp(action, "entroom")) {
805 } else if (!strcasecmp(action, "display_editroom")) {
807 } else if (!strcasecmp(action, "editroom")) {
809 } else if (!strcasecmp(action, "display_editinfo")) {
810 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
811 } else if (!strcasecmp(action, "editinfo")) {
812 save_edit("Room info", "EINF 1", 1);
813 } else if (!strcasecmp(action, "display_editbio")) {
814 sprintf(buf, "RBIO %s", wc_username);
815 display_edit("Your bio", "NOOP", buf, "editbio");
816 } else if (!strcasecmp(action, "editbio")) {
817 save_edit("Your bio", "EBIO", 0);
818 } else if (!strcasecmp(action, "confirm_delete_room")) {
819 confirm_delete_room();
820 } else if (!strcasecmp(action, "delete_room")) {
822 } else if (!strcasecmp(action, "validate")) {
824 } else if (!strcasecmp(action, "display_editpic")) {
825 display_graphics_upload("your photo",
828 } else if (!strcasecmp(action, "editpic")) {
829 do_graphics_upload("UIMG 1|_userpic_");
830 } else if (!strcasecmp(action, "display_editroompic")) {
831 display_graphics_upload("the graphic for this room",
834 } else if (!strcasecmp(action, "editroompic")) {
835 do_graphics_upload("UIMG 1|_roompic_");
836 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
837 select_floor_to_edit_pic();
838 } else if (!strcasecmp(action, "display_editfloorpic")) {
839 sprintf(buf, "UIMG 0|_floorpic_|%s",
840 bstr("which_floor"));
841 display_graphics_upload("the graphic for this floor",
844 } else if (!strcasecmp(action, "editfloorpic")) {
845 sprintf(buf, "UIMG 1|_floorpic_|%s",
846 bstr("which_floor"));
847 do_graphics_upload(buf);
848 } else if (!strcasecmp(action, "display_reg")) {
850 } else if (!strcasecmp(action, "register")) {
852 } else if (!strcasecmp(action, "display_changepw")) {
854 } else if (!strcasecmp(action, "changepw")) {
856 } else if (!strcasecmp(action, "display_edit_node")) {
858 } else if (!strcasecmp(action, "display_netconf")) {
860 } else if (!strcasecmp(action, "display_confirm_unshare")) {
861 display_confirm_unshare();
862 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
863 display_confirm_delete_node();
864 } else if (!strcasecmp(action, "delete_node")) {
866 } else if (!strcasecmp(action, "unshare")) {
868 } else if (!strcasecmp(action, "display_add_node")) {
870 } else if (!strcasecmp(action, "add_node")) {
872 } else if (!strcasecmp(action, "display_share")) {
874 } else if (!strcasecmp(action, "share")) {
876 } else if (!strcasecmp(action, "terminate_session")) {
878 } else if (!strcasecmp(action, "edit_me")) {
880 } else if (!strcasecmp(action, "display_siteconfig")) {
881 display_siteconfig();
882 } else if (!strcasecmp(action, "siteconfig")) {
884 } else if (!strcasecmp(action, "display_generic")) {
886 } else if (!strcasecmp(action, "do_generic")) {
888 } else if (!strcasecmp(action, "display_menubar")) {
891 /* When all else fails... */
893 printf("HTTP/1.0 200 OK\n");
896 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
897 wprintf("You're in session %d<HR>\n", wc_session);
898 wprintf("Command: <BR><PRE>\n");
900 wprintf("</PRE><HR>\n");
901 wprintf("Variables: <BR><PRE>\n");
903 wprintf("</PRE><HR>\n");
908 if (content != NULL) {
913 if (upload_length > 0) {
919 int main(int argc, char *argv[])
927 "webcit: usage error (argc must be 6, not %d)\n",
932 wc_session = atoi(argv[1]);
933 defaulthost = argv[2];
934 defaultport = argv[3];
936 strcpy(wc_username, "");
937 strcpy(wc_password, "");
938 strcpy(wc_roomname, "");
940 /* Clear out serv_info and temporarily set the value of serv_humannode
941 * to a default value, because it'll be used in HTML page titles
943 memset(&serv_info, 0, sizeof(serv_info));
944 strcpy(serv_info.serv_humannode, "WebCit");
946 strcpy(browser, argv[5]);
947 bd = browser_braindamage_check(browser);
954 session_loop(argv[4]);