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;
36 struct webcontent *wlist = NULL;
37 struct webcontent *wlast = NULL;
39 struct urlcontent *urlstrings = NULL;
41 static const char *defaulthost = DEFAULT_HOST;
42 static const char *defaultport = DEFAULT_PORT;
44 int upload_length = 0;
48 void unescape_input(char *buf)
53 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
54 buf[strlen(buf) - 1] = 0;
56 for (a = 0; a < strlen(buf); ++a) {
63 sscanf(hex, "%02x", &b);
65 strcpy(&buf[a + 1], &buf[a + 3]);
72 void addurls(char *url)
80 while (strlen(up) > 0) {
82 /* locate the = sign */
83 strncpy(buf, up, 255);
85 for (a = 255; a >= 0; --a)
92 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
95 strcpy(u->url_key, buf);
97 /* now chop that part off */
98 for (a = 0; a <= b; ++a)
101 /* locate the & sign */
104 for (a = 0; a < strlen(up); ++a) {
105 if (!strncmp(ptr, "&", 1)) {
112 for (a = 0; a < b; ++a)
116 u->url_data = malloc(strlen(up) + 1);
117 strcpy(u->url_data, up);
119 unescape_input(u->url_data);
127 struct urlcontent *u;
129 while (urlstrings != NULL) {
130 free(urlstrings->url_data);
131 u = urlstrings->next;
138 * Diagnostic function to display the contents of all variables
142 struct urlcontent *u;
144 for (u = urlstrings; u != NULL; u = u->next) {
145 wprintf("%38s = %s\n", u->url_key, u->url_data);
149 char *bstr(char *key)
151 struct urlcontent *u;
153 for (u = urlstrings; u != NULL; u = u->next) {
154 if (!strcasecmp(u->url_key, key))
155 return (u->url_data);
161 void wprintf(const char *format,...)
164 struct webcontent *wptr;
166 wptr = (struct webcontent *) malloc(sizeof(struct webcontent));
176 va_start(arg_ptr, format);
177 vsprintf(wptr->w_data, format, arg_ptr);
181 int wContentLength(void)
183 struct webcontent *wptr;
186 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
187 len = len + strlen(wptr->w_data);
194 * wDumpContent() takes all the stuff that's been queued up using
195 * the wprintf() and escputs() functions, and sends it out to the browser.
196 * By queuing instead of transmitting as it's generated, we're able to
197 * calculate a Content-length: header.
199 * print_standard_html_footer should be set to 0 to transmit only, 1 to
200 * append the main menu (if in noframes mode) and closing tags, or 2 to
201 * append the closing tags only.
203 void wDumpContent(int print_standard_html_footer)
205 struct webcontent *wptr;
207 if (print_standard_html_footer) {
208 if ((noframes) && (print_standard_html_footer != 2)) {
212 wprintf("</BODY></HTML>\n");
214 printf("Content-type: text/html\n");
215 printf("Content-length: %d\n", wContentLength());
218 while (wlist != NULL) {
219 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
228 void escputs1(char *strbuf, int nbsp)
232 for (a = 0; a < strlen(strbuf); ++a) {
233 if (strbuf[a] == '<')
235 else if (strbuf[a] == '>')
237 else if (strbuf[a] == '&')
239 else if (strbuf[a] == 34)
241 else if (strbuf[a] == LB)
243 else if (strbuf[a] == RB)
245 else if (strbuf[a] == QU)
247 else if ((strbuf[a] == 32) && (nbsp == 1)) {
250 wprintf("%c", strbuf[a]);
255 void escputs(char *strbuf)
262 char *urlesc(char *strbuf)
265 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
266 static char outbuf[512];
270 for (a = 0; a < strlen(strbuf); ++a) {
272 for (b = 0; b < strlen(ec); ++b) {
273 if (strbuf[a] == ec[b])
278 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
280 sprintf(&outbuf[b], "%c", strbuf[a]);
285 void urlescputs(char *strbuf)
287 wprintf("%s", urlesc(strbuf));
294 * Get a line of text from the webserver (which originally came from the
295 * user's browser), checking for sanity etc.
297 char *getz(char *buf)
303 /* If fgets() fails, it's because the webserver crashed, so kill off
306 if (fgets(buf, 256, stdin) == NULL) {
308 fprintf(stderr, "webcit: exit code %d (%s)\n",
313 /* Otherwise, strip out nonprintables and resume our happy day.
316 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
317 buf[strlen(buf) - 1] = 0;
325 * Output all that important stuff that the browser will want to see
327 * If print_standard_html_head is nonzero, we also get some standard HTML
328 * headers. If it's set to 2, the session is considered to be closing.
330 void output_headers(int print_standard_html_head)
333 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
336 printf("Server: %s\n", SERVER);
337 printf("Connection: close\n");
339 if (print_standard_html_head > 0) {
340 printf("Pragma: no-cache\n");
341 printf("Cache-Control: no-store\n");
343 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
344 wc_roomname, noframes);
345 if (print_standard_html_head == 2) {
346 printf("X-WebCit-Session: close\n");
347 printf("Set-cookie: webcit=%s\n", unset);
349 printf("Set-cookie: webcit=%s\n", cookie);
352 if (print_standard_html_head > 0) {
353 wprintf("<HTML><HEAD><TITLE>");
354 escputs("WebCit"); /* FIX -- add BBS name here */
355 wprintf("</TITLE></HEAD>");
356 if (ExpressMessages != NULL) {
357 wprintf("<SCRIPT language=\"javascript\">\n");
358 wprintf("function ExpressMessage() {\n");
359 wprintf(" alert(\"");
360 escputs(ExpressMessages);
362 wprintf(" }\n </SCRIPT>\n");
365 if (ExpressMessages != NULL) {
366 wprintf("onload=\"ExpressMessage()\" ");
367 free(ExpressMessages);
368 ExpressMessages = NULL;
370 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
377 void check_for_express_messages()
384 while (serv_gets(buf), strcmp(buf, "000")) {
385 if (ExpressMessages == NULL) {
386 ExpressMessages = malloc(strlen(buf) + 4);
387 strcpy(ExpressMessages, "");
389 ExpressMessages = realloc(ExpressMessages,
390 (strlen(ExpressMessages) + strlen(buf) + 4));
392 strcat(ExpressMessages, buf);
393 strcat(ExpressMessages, "\\n");
401 void output_static(char *what)
408 sprintf(buf, "static/%s", what);
409 fp = fopen(buf, "rb");
411 printf("HTTP/1.0 404 %s\n", strerror(errno));
413 printf("Content-Type: text/plain\n");
414 sprintf(buf, "%s: %s\n", what, strerror(errno));
415 printf("Content-length: %d\n", strlen(buf));
417 fwrite(buf, strlen(buf), 1, stdout);
419 printf("HTTP/1.0 200 OK\n");
422 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
423 printf("Content-type: image/gif\n");
424 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
425 printf("Content-type: image/jpeg\n");
426 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
427 printf("Content-type: text/html\n");
429 printf("Content-type: application/octet-stream\n");
431 fstat(fileno(fp), &statbuf);
432 bytes = statbuf.st_size;
433 printf("Content-length: %ld\n", (long) bytes);
436 putc(getc(fp), stdout);
449 off_t accomplished = 0L;
452 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
455 bytes = extract_long(&buf[4], 0);
456 printf("HTTP/1.0 200 OK\n");
458 printf("Content-type: image/gif\n");
459 printf("Content-length: %ld\n", (long) bytes);
462 while (bytes > (off_t) 0) {
463 thisblock = (off_t) sizeof(xferbuf);
464 if (thisblock > bytes)
466 serv_printf("READ %ld|%ld", accomplished, thisblock);
469 thisblock = extract_long(&buf[4], 0);
470 serv_read(xferbuf, (int) thisblock);
471 fwrite(xferbuf, thisblock, 1, stdout);
472 bytes = bytes - thisblock;
473 accomplished = accomplished + thisblock;
479 printf("HTTP/1.0 404 %s\n", strerror(errno));
481 printf("Content-Type: text/plain\n");
482 sprintf(buf, "Error retrieving image\n");
483 printf("Content-length: %d\n", strlen(buf));
485 fwrite(buf, strlen(buf), 1, stdout);
492 * Convenience functions to display a page containing only a string
494 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
496 printf("HTTP/1.0 200 OK\n");
498 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
499 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
500 wprintf("<B>%s</B>\n", titlebarmsg);
501 wprintf("</FONT></TD></TR></TABLE><BR>\n");
502 escputs(messagetext);
511 void display_error(char *errormessage)
513 convenience_page("770000", "Error", errormessage);
516 void display_success(char *successmessage)
518 convenience_page("007700", "OK", successmessage);
523 void extract_action(char *actbuf, char *cmdbuf)
527 strcpy(actbuf, cmdbuf);
528 if (!strncasecmp(actbuf, "GET /", 5))
529 strcpy(actbuf, &actbuf[5]);
530 if (!strncasecmp(actbuf, "PUT /", 5))
531 strcpy(actbuf, &actbuf[5]);
532 if (!strncasecmp(actbuf, "POST /", 6))
533 strcpy(actbuf, &actbuf[6]);
535 for (i = 0; i < strlen(actbuf); ++i) {
536 if (actbuf[i] == ' ') {
540 if (actbuf[i] == '/') {
544 if (actbuf[i] == '?') {
548 if (actbuf[i] == '&') {
556 void upload_handler(char *name, char *filename, char *encoding,
557 void *content, char *cbtype, size_t length)
560 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
561 fprintf(stderr, " name = %s\n", name);
562 fprintf(stderr, "filename = %s\n", filename);
563 fprintf(stderr, "encoding = %s\n", encoding);
564 fprintf(stderr, " type = %s\n", cbtype);
565 fprintf(stderr, " length = %d\n", length);
567 if (strlen(name) > 0) {
568 upload = malloc(length);
569 if (upload != NULL) {
570 upload_length = length;
571 memcpy(upload, content, length);
577 void session_loop(char *browser_host)
583 int ContentLength = 0;
584 char ContentType[512];
587 /* We stuff these with the values coming from the client cookies,
588 * so we can use them to reconnect a timed out session if we have to.
592 char c_username[256];
593 char c_password[256];
594 char c_roomname[256];
597 strcpy(c_host, defaulthost);
598 strcpy(c_port, defaultport);
599 strcpy(c_username, "");
600 strcpy(c_password, "");
601 strcpy(c_roomname, "");
606 if (getz(cmd) == NULL)
608 extract_action(action, cmd);
611 if (getz(buf) == NULL)
614 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
615 strcpy(cookie, &buf[15]);
616 cookie_to_stuff(cookie, NULL,
617 c_username, c_password, c_roomname,
620 if (!strncasecmp(buf, "Content-length: ", 16)) {
621 ContentLength = atoi(&buf[16]);
623 if (!strncasecmp(buf, "Content-type: ", 14)) {
624 strcpy(ContentType, &buf[14]);
626 } while (strlen(buf) > 0);
630 if (ContentLength > 0) {
631 content = malloc(ContentLength + 1);
632 fread(content, ContentLength, 1, stdin);
634 content[ContentLength] = 0;
636 if (!strncasecmp(ContentType,
637 "application/x-www-form-urlencoded", 33)) {
639 } else if (!strncasecmp(ContentType, "multipart", 9)) {
640 mime_parser(content, ContentLength, ContentType,
647 /* If there are variables in the URL, we must grab them now */
648 for (a = 0; a < strlen(cmd); ++a)
649 if ((cmd[a] == '?') || (cmd[a] == '&')) {
650 for (b = a; b < strlen(cmd); ++b)
653 addurls(&cmd[a + 1]);
657 * If we're not connected to a Citadel server, try to hook up the
658 * connection now. Preference is given to the host and port specified
659 * by browser cookies, if cookies have been supplied.
662 if (strlen(bstr("host")) > 0)
663 strcpy(c_host, bstr("host"));
664 if (strlen(bstr("port")) > 0)
665 strcpy(c_port, bstr("port"));
666 serv_sock = connectsock(c_host, c_port, "tcp");
668 serv_gets(buf); /* get the server welcome message */
669 get_serv_info(browser_host);
671 check_for_express_messages();
674 * If we're not logged in, but we have username and password cookies
675 * supplied by the browser, try using them to log in.
677 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
678 serv_printf("USER %s", c_username);
681 serv_printf("PASS %s", c_password);
684 become_logged_in(c_username, c_password, buf);
689 * If we don't have a current room, but a cookie specifying the
690 * current room is supplied, make an effort to go there.
692 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
693 serv_printf("GOTO %s", c_roomname);
696 strcpy(wc_roomname, c_roomname);
699 if (!strcasecmp(action, "static")) {
700 strcpy(buf, &cmd[12]);
701 for (a = 0; a < strlen(buf); ++a)
705 } else if (!strcasecmp(action, "image")) {
707 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
709 } else if (!logged_in) {
712 /* Various commands... */
714 else if (!strcasecmp(action, "do_welcome")) {
716 } else if (!strcasecmp(action, "display_main_menu")) {
718 } else if (!strcasecmp(action, "advanced")) {
719 display_advanced_menu();
720 } else if (!strcasecmp(action, "whobbs")) {
722 } else if (!strcasecmp(action, "knrooms")) {
723 list_all_rooms_by_floor();
724 } else if (!strcasecmp(action, "gotonext")) {
727 } else if (!strcasecmp(action, "skip")) {
729 } else if (!strcasecmp(action, "ungoto")) {
731 } else if (!strcasecmp(action, "dotgoto")) {
733 gotoroom(bstr("room"), 1);
734 } else if (!strcasecmp(action, "termquit")) {
736 } else if (!strcasecmp(action, "readnew")) {
738 } else if (!strcasecmp(action, "readold")) {
740 } else if (!strcasecmp(action, "readfwd")) {
742 } else if (!strcasecmp(action, "display_enter")) {
744 } else if (!strcasecmp(action, "post")) {
746 } else if (!strcasecmp(action, "confirm_delete_msg")) {
747 confirm_delete_msg();
748 } else if (!strcasecmp(action, "delete_msg")) {
750 } else if (!strcasecmp(action, "confirm_move_msg")) {
752 } else if (!strcasecmp(action, "move_msg")) {
754 } else if (!strcasecmp(action, "userlist")) {
756 } else if (!strcasecmp(action, "showuser")) {
758 } else if (!strcasecmp(action, "display_page")) {
760 } else if (!strcasecmp(action, "page_user")) {
762 } else if (!strcasecmp(action, "chat")) {
764 } else if (!strcasecmp(action, "display_private")) {
765 display_private("", 0);
766 } else if (!strcasecmp(action, "goto_private")) {
768 } else if (!strcasecmp(action, "zapped_list")) {
770 } else if (!strcasecmp(action, "display_zap")) {
772 } else if (!strcasecmp(action, "zap")) {
774 } else if (!strcasecmp(action, "display_entroom")) {
776 } else if (!strcasecmp(action, "entroom")) {
778 } else if (!strcasecmp(action, "display_editroom")) {
780 } else if (!strcasecmp(action, "editroom")) {
782 } else if (!strcasecmp(action, "display_editinfo")) {
783 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
784 } else if (!strcasecmp(action, "editinfo")) {
785 save_edit("Room info", "EINF 1", 1);
786 } else if (!strcasecmp(action, "display_editbio")) {
787 sprintf(buf, "RBIO %s", wc_username);
788 display_edit("Your bio", "NOOP", buf, "editbio");
789 } else if (!strcasecmp(action, "editbio")) {
790 save_edit("Your bio", "EBIO", 0);
791 } else if (!strcasecmp(action, "confirm_delete_room")) {
792 confirm_delete_room();
793 } else if (!strcasecmp(action, "delete_room")) {
795 } else if (!strcasecmp(action, "validate")) {
797 } else if (!strcasecmp(action, "display_editpic")) {
798 display_graphics_upload("your photo",
801 } else if (!strcasecmp(action, "editpic")) {
802 do_graphics_upload("UIMG 1|_userpic_");
803 } else if (!strcasecmp(action, "display_editroompic")) {
804 display_graphics_upload("the graphic for this room",
807 } else if (!strcasecmp(action, "editroompic")) {
808 do_graphics_upload("UIMG 1|_roompic_");
809 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
810 select_floor_to_edit_pic();
811 } else if (!strcasecmp(action, "display_editfloorpic")) {
812 sprintf(buf, "UIMG 0|_floorpic_|%s",
813 bstr("which_floor"));
814 display_graphics_upload("the graphic for this floor",
817 } else if (!strcasecmp(action, "editfloorpic")) {
818 sprintf(buf, "UIMG 1|_floorpic_|%s",
819 bstr("which_floor"));
820 do_graphics_upload(buf);
821 } else if (!strcasecmp(action, "display_reg")) {
823 } else if (!strcasecmp(action, "register")) {
825 } else if (!strcasecmp(action, "display_changepw")) {
827 } else if (!strcasecmp(action, "changepw")) {
829 } else if (!strcasecmp(action, "display_edit_node")) {
831 } else if (!strcasecmp(action, "display_netconf")) {
833 } else if (!strcasecmp(action, "display_confirm_unshare")) {
834 display_confirm_unshare();
835 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
836 display_confirm_delete_node();
837 } else if (!strcasecmp(action, "delete_node")) {
839 } else if (!strcasecmp(action, "unshare")) {
841 } else if (!strcasecmp(action, "display_add_node")) {
843 } else if (!strcasecmp(action, "add_node")) {
845 } else if (!strcasecmp(action, "display_share")) {
847 } else if (!strcasecmp(action, "share")) {
849 } else if (!strcasecmp(action, "terminate_session")) {
851 } else if (!strcasecmp(action, "edit_me")) {
853 } else if (!strcasecmp(action, "display_siteconfig")) {
854 display_siteconfig();
855 } else if (!strcasecmp(action, "siteconfig")) {
857 } else if (!strcasecmp(action, "display_generic")) {
859 } else if (!strcasecmp(action, "do_generic")) {
862 /* When all else fails... */
864 printf("HTTP/1.0 200 OK\n");
867 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
868 wprintf("You're in session %d<HR>\n", wc_session);
869 wprintf("Command: <BR><PRE>\n");
871 wprintf("</PRE><HR>\n");
872 wprintf("Variables: <BR><PRE>\n");
874 wprintf("</PRE><HR>\n");
879 if (content != NULL) {
884 if (upload_length > 0) {
890 int main(int argc, char *argv[])
898 "webcit: usage error (argc must be 6, not %d)\n",
902 wc_session = atoi(argv[1]);
903 defaulthost = argv[2];
904 defaultport = argv[3];
906 strcpy(wc_username, "");
907 strcpy(wc_password, "");
908 strcpy(wc_roomname, "");
910 strcpy(browser, argv[5]);
911 bd = browser_braindamage_check(browser);
918 session_loop(argv[4]);