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, char *target)
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 ((strlen(target) > 0) && (noframes == 0)) {
340 printf("Window-target: %s\n", target);
342 if (print_standard_html_head > 0) {
343 printf("Pragma: no-cache\n");
344 printf("Cache-Control: no-store\n");
346 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
347 wc_roomname, noframes);
348 if (print_standard_html_head == 2) {
349 printf("X-WebCit-Session: close\n");
350 printf("Set-cookie: webcit=%s\n", unset);
352 printf("Set-cookie: webcit=%s\n", cookie);
355 if (print_standard_html_head > 0) {
356 wprintf("<HTML><HEAD><TITLE>");
357 escputs("WebCit"); /* FIX -- add BBS name here */
358 wprintf("</TITLE></HEAD>");
359 if (ExpressMessages != NULL) {
360 wprintf("<SCRIPT language=\"javascript\">\n");
361 wprintf("function ExpressMessage() {\n");
362 wprintf(" alert(\"");
363 escputs(ExpressMessages);
365 wprintf(" }\n </SCRIPT>\n");
368 if (ExpressMessages != NULL) {
369 wprintf("onload=\"ExpressMessage()\" ");
370 free(ExpressMessages);
371 ExpressMessages = NULL;
373 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
380 void check_for_express_messages()
387 while (serv_gets(buf), strcmp(buf, "000")) {
388 if (ExpressMessages == NULL) {
389 ExpressMessages = malloc(strlen(buf) + 4);
390 strcpy(ExpressMessages, "");
392 ExpressMessages = realloc(ExpressMessages,
393 (strlen(ExpressMessages) + strlen(buf) + 4));
395 strcat(ExpressMessages, buf);
396 strcat(ExpressMessages, "\\n");
404 void output_static(char *what)
411 sprintf(buf, "static/%s", what);
412 fp = fopen(buf, "rb");
414 printf("HTTP/1.0 404 %s\n", strerror(errno));
415 output_headers(0, "");
416 printf("Content-Type: text/plain\n");
417 sprintf(buf, "%s: %s\n", what, strerror(errno));
418 printf("Content-length: %d\n", strlen(buf));
420 fwrite(buf, strlen(buf), 1, stdout);
422 printf("HTTP/1.0 200 OK\n");
423 output_headers(0, "");
425 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
426 printf("Content-type: image/gif\n");
427 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
428 printf("Content-type: image/jpeg\n");
429 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
430 printf("Content-type: text/html\n");
432 printf("Content-type: application/octet-stream\n");
434 fstat(fileno(fp), &statbuf);
435 bytes = statbuf.st_size;
436 printf("Content-length: %ld\n", (long) bytes);
439 putc(getc(fp), stdout);
452 off_t accomplished = 0L;
455 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
458 bytes = extract_long(&buf[4], 0);
459 printf("HTTP/1.0 200 OK\n");
460 output_headers(0, "");
461 printf("Content-type: image/gif\n");
462 printf("Content-length: %ld\n", (long) bytes);
465 while (bytes > (off_t) 0) {
466 thisblock = (off_t) sizeof(xferbuf);
467 if (thisblock > bytes)
469 serv_printf("READ %ld|%ld", accomplished, thisblock);
472 thisblock = extract_long(&buf[4], 0);
473 serv_read(xferbuf, (int) thisblock);
474 fwrite(xferbuf, thisblock, 1, stdout);
475 bytes = bytes - thisblock;
476 accomplished = accomplished + thisblock;
482 printf("HTTP/1.0 404 %s\n", strerror(errno));
483 output_headers(0, "");
484 printf("Content-Type: text/plain\n");
485 sprintf(buf, "Error retrieving image\n");
486 printf("Content-length: %d\n", strlen(buf));
488 fwrite(buf, strlen(buf), 1, stdout);
495 * Convenience functions to display a page containing only a string
497 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
499 printf("HTTP/1.0 200 OK\n");
500 output_headers(1, "bottom");
501 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
502 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
503 wprintf("<B>%s</B>\n", titlebarmsg);
504 wprintf("</FONT></TD></TR></TABLE><BR>\n");
505 escputs(messagetext);
514 void display_error(char *errormessage)
516 convenience_page("770000", "Error", errormessage);
519 void display_success(char *successmessage)
521 convenience_page("007700", "OK", successmessage);
526 void extract_action(char *actbuf, char *cmdbuf)
530 strcpy(actbuf, cmdbuf);
531 if (!strncasecmp(actbuf, "GET /", 5))
532 strcpy(actbuf, &actbuf[5]);
533 if (!strncasecmp(actbuf, "PUT /", 5))
534 strcpy(actbuf, &actbuf[5]);
535 if (!strncasecmp(actbuf, "POST /", 6))
536 strcpy(actbuf, &actbuf[6]);
538 for (i = 0; i < strlen(actbuf); ++i) {
539 if (actbuf[i] == ' ') {
543 if (actbuf[i] == '/') {
547 if (actbuf[i] == '?') {
551 if (actbuf[i] == '&') {
559 void upload_handler(char *name, char *filename, char *encoding,
560 void *content, char *cbtype, size_t length)
563 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
564 fprintf(stderr, " name = %s\n", name);
565 fprintf(stderr, "filename = %s\n", filename);
566 fprintf(stderr, "encoding = %s\n", encoding);
567 fprintf(stderr, " type = %s\n", cbtype);
568 fprintf(stderr, " length = %d\n", length);
570 if (strlen(name) > 0) {
571 upload = malloc(length);
572 if (upload != NULL) {
573 upload_length = length;
574 memcpy(upload, content, length);
580 void session_loop(char *browser_host)
586 int ContentLength = 0;
587 char ContentType[512];
590 /* We stuff these with the values coming from the client cookies,
591 * so we can use them to reconnect a timed out session if we have to.
595 char c_username[256];
596 char c_password[256];
597 char c_roomname[256];
600 strcpy(c_host, defaulthost);
601 strcpy(c_port, defaultport);
602 strcpy(c_username, "");
603 strcpy(c_password, "");
604 strcpy(c_roomname, "");
609 if (getz(cmd) == NULL)
611 extract_action(action, cmd);
614 if (getz(buf) == NULL)
617 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
618 strcpy(cookie, &buf[15]);
619 cookie_to_stuff(cookie, NULL,
620 c_username, c_password, c_roomname,
623 if (!strncasecmp(buf, "Content-length: ", 16)) {
624 ContentLength = atoi(&buf[16]);
626 if (!strncasecmp(buf, "Content-type: ", 14)) {
627 strcpy(ContentType, &buf[14]);
629 } while (strlen(buf) > 0);
633 if (ContentLength > 0) {
634 content = malloc(ContentLength + 1);
635 fread(content, ContentLength, 1, stdin);
637 content[ContentLength] = 0;
639 if (!strncasecmp(ContentType,
640 "application/x-www-form-urlencoded", 33)) {
642 } else if (!strncasecmp(ContentType, "multipart", 9)) {
643 mime_parser(content, ContentLength, ContentType,
650 /* If there are variables in the URL, we must grab them now */
651 for (a = 0; a < strlen(cmd); ++a)
652 if ((cmd[a] == '?') || (cmd[a] == '&')) {
653 for (b = a; b < strlen(cmd); ++b)
656 addurls(&cmd[a + 1]);
660 * If we're not connected to a Citadel server, try to hook up the
661 * connection now. Preference is given to the host and port specified
662 * by browser cookies, if cookies have been supplied.
665 if (strlen(bstr("host")) > 0)
666 strcpy(c_host, bstr("host"));
667 if (strlen(bstr("port")) > 0)
668 strcpy(c_port, bstr("port"));
669 serv_sock = connectsock(c_host, c_port, "tcp");
671 serv_gets(buf); /* get the server welcome message */
672 get_serv_info(browser_host);
674 check_for_express_messages();
677 * If we're not logged in, but we have username and password cookies
678 * supplied by the browser, try using them to log in.
680 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
681 serv_printf("USER %s", c_username);
684 serv_printf("PASS %s", c_password);
687 become_logged_in(c_username, c_password, buf);
692 * If we don't have a current room, but a cookie specifying the
693 * current room is supplied, make an effort to go there.
695 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
696 serv_printf("GOTO %s", c_roomname);
699 strcpy(wc_roomname, c_roomname);
702 if (!strcasecmp(action, "static")) {
703 strcpy(buf, &cmd[12]);
704 for (a = 0; a < strlen(buf); ++a)
708 } else if (!strcasecmp(action, "image")) {
710 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
712 } else if (!logged_in) {
715 /* Various commands... */
717 else if (!strcasecmp(action, "do_welcome")) {
719 } else if (!strcasecmp(action, "display_main_menu")) {
721 } else if (!strcasecmp(action, "advanced")) {
722 display_advanced_menu();
723 } else if (!strcasecmp(action, "whobbs")) {
725 } else if (!strcasecmp(action, "knrooms")) {
726 list_all_rooms_by_floor();
727 } else if (!strcasecmp(action, "gotonext")) {
730 } else if (!strcasecmp(action, "skip")) {
732 } else if (!strcasecmp(action, "ungoto")) {
734 } else if (!strcasecmp(action, "dotgoto")) {
736 gotoroom(bstr("room"), 1);
737 } else if (!strcasecmp(action, "termquit")) {
739 } else if (!strcasecmp(action, "readnew")) {
741 } else if (!strcasecmp(action, "readold")) {
743 } else if (!strcasecmp(action, "readfwd")) {
745 } else if (!strcasecmp(action, "display_enter")) {
747 } else if (!strcasecmp(action, "post")) {
749 } else if (!strcasecmp(action, "confirm_delete_msg")) {
750 confirm_delete_msg();
751 } else if (!strcasecmp(action, "delete_msg")) {
753 } else if (!strcasecmp(action, "confirm_move_msg")) {
755 } else if (!strcasecmp(action, "move_msg")) {
757 } else if (!strcasecmp(action, "userlist")) {
759 } else if (!strcasecmp(action, "showuser")) {
761 } else if (!strcasecmp(action, "display_page")) {
763 } else if (!strcasecmp(action, "page_user")) {
765 } else if (!strcasecmp(action, "chat")) {
767 } else if (!strcasecmp(action, "display_private")) {
768 display_private("", 0);
769 } else if (!strcasecmp(action, "goto_private")) {
771 } else if (!strcasecmp(action, "zapped_list")) {
773 } else if (!strcasecmp(action, "display_zap")) {
775 } else if (!strcasecmp(action, "zap")) {
777 } else if (!strcasecmp(action, "display_entroom")) {
779 } else if (!strcasecmp(action, "entroom")) {
781 } else if (!strcasecmp(action, "display_editroom")) {
783 } else if (!strcasecmp(action, "editroom")) {
785 } else if (!strcasecmp(action, "display_editinfo")) {
786 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
787 } else if (!strcasecmp(action, "editinfo")) {
788 save_edit("Room info", "EINF 1", 1);
789 } else if (!strcasecmp(action, "display_editbio")) {
790 sprintf(buf, "RBIO %s", wc_username);
791 display_edit("Your bio", "NOOP", buf, "editbio");
792 } else if (!strcasecmp(action, "editbio")) {
793 save_edit("Your bio", "EBIO", 0);
794 } else if (!strcasecmp(action, "confirm_delete_room")) {
795 confirm_delete_room();
796 } else if (!strcasecmp(action, "delete_room")) {
798 } else if (!strcasecmp(action, "validate")) {
800 } else if (!strcasecmp(action, "display_editpic")) {
801 display_graphics_upload("your photo",
804 } else if (!strcasecmp(action, "editpic")) {
805 do_graphics_upload("UIMG 1|_userpic_");
806 } else if (!strcasecmp(action, "display_editroompic")) {
807 display_graphics_upload("the graphic for this room",
810 } else if (!strcasecmp(action, "editroompic")) {
811 do_graphics_upload("UIMG 1|_roompic_");
812 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
813 select_floor_to_edit_pic();
814 } else if (!strcasecmp(action, "display_editfloorpic")) {
815 sprintf(buf, "UIMG 0|_floorpic_|%s",
816 bstr("which_floor"));
817 display_graphics_upload("the graphic for this floor",
820 } else if (!strcasecmp(action, "editfloorpic")) {
821 sprintf(buf, "UIMG 1|_floorpic_|%s",
822 bstr("which_floor"));
823 do_graphics_upload(buf);
824 } else if (!strcasecmp(action, "display_reg")) {
826 } else if (!strcasecmp(action, "register")) {
828 } else if (!strcasecmp(action, "display_changepw")) {
830 } else if (!strcasecmp(action, "changepw")) {
832 } else if (!strcasecmp(action, "display_edit_node")) {
834 } else if (!strcasecmp(action, "display_netconf")) {
836 } else if (!strcasecmp(action, "display_confirm_unshare")) {
837 display_confirm_unshare();
838 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
839 display_confirm_delete_node();
840 } else if (!strcasecmp(action, "delete_node")) {
842 } else if (!strcasecmp(action, "unshare")) {
844 } else if (!strcasecmp(action, "display_add_node")) {
846 } else if (!strcasecmp(action, "add_node")) {
848 } else if (!strcasecmp(action, "display_share")) {
850 } else if (!strcasecmp(action, "share")) {
852 } else if (!strcasecmp(action, "terminate_session")) {
854 } else if (!strcasecmp(action, "edit_me")) {
856 } else if (!strcasecmp(action, "display_siteconfig")) {
857 display_siteconfig();
858 } else if (!strcasecmp(action, "siteconfig")) {
860 } else if (!strcasecmp(action, "display_generic")) {
862 } else if (!strcasecmp(action, "do_generic")) {
865 /* When all else fails... */
867 printf("HTTP/1.0 200 OK\n");
868 output_headers(1, "");
870 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
871 wprintf("You're in session %d<HR>\n", wc_session);
872 wprintf("Command: <BR><PRE>\n");
874 wprintf("</PRE><HR>\n");
875 wprintf("Variables: <BR><PRE>\n");
877 wprintf("</PRE><HR>\n");
882 if (content != NULL) {
887 if (upload_length > 0) {
893 int main(int argc, char *argv[])
901 "webcit: usage error (argc must be 6, not %d)\n",
905 wc_session = atoi(argv[1]);
906 defaulthost = argv[2];
907 defaultport = argv[3];
909 strcpy(wc_username, "");
910 strcpy(wc_password, "");
911 strcpy(wc_roomname, "");
913 strcpy(browser, argv[5]);
914 bd = browser_braindamage_check(browser);
921 session_loop(argv[4]);