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));
291 char *getz(char *buf)
294 if (fgets(buf, 256, stdin) == NULL) {
298 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
299 buf[strlen(buf) - 1] = 0;
305 * Output all that important stuff that the browser will want to see
307 * If print_standard_html_head is nonzero, we also get some standard HTML
308 * headers. If it's set to 2, the session is considered to be closing.
310 void output_headers(int print_standard_html_head, char *target)
313 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
316 printf("Server: %s\n", SERVER);
317 printf("Connection: close\n");
319 if ((strlen(target) > 0) && (noframes == 0)) {
320 printf("Window-target: %s\n", target);
322 if (print_standard_html_head > 0) {
323 printf("Pragma: no-cache\n");
324 printf("Cache-Control: no-store\n");
326 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
327 wc_roomname, noframes);
328 if (print_standard_html_head == 2) {
329 printf("X-WebCit-Session: close\n");
330 printf("Set-cookie: webcit=%s\n", unset);
332 printf("Set-cookie: webcit=%s\n", cookie);
335 if (print_standard_html_head > 0) {
336 wprintf("<HTML><HEAD><TITLE>");
337 escputs("WebCit"); /* FIX -- add BBS name here */
338 wprintf("</TITLE></HEAD>");
339 if (ExpressMessages != NULL) {
340 wprintf("<SCRIPT language=\"javascript\">\n");
341 wprintf("function ExpressMessage() {\n");
342 wprintf(" alert(\"");
343 escputs(ExpressMessages);
345 wprintf(" }\n </SCRIPT>\n");
348 if (ExpressMessages != NULL) {
349 wprintf("onload=\"ExpressMessage()\" ");
350 free(ExpressMessages);
351 ExpressMessages = NULL;
353 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
360 void check_for_express_messages()
367 while (serv_gets(buf), strcmp(buf, "000")) {
368 if (ExpressMessages == NULL) {
369 ExpressMessages = malloc(strlen(buf) + 4);
370 strcpy(ExpressMessages, "");
372 ExpressMessages = realloc(ExpressMessages,
373 (strlen(ExpressMessages) + strlen(buf) + 4));
375 strcat(ExpressMessages, buf);
376 strcat(ExpressMessages, "\\n");
384 void output_static(char *what)
391 sprintf(buf, "static/%s", what);
392 fp = fopen(buf, "rb");
394 printf("HTTP/1.0 404 %s\n", strerror(errno));
395 output_headers(0, "");
396 printf("Content-Type: text/plain\n");
397 sprintf(buf, "%s: %s\n", what, strerror(errno));
398 printf("Content-length: %d\n", strlen(buf));
400 fwrite(buf, strlen(buf), 1, stdout);
402 printf("HTTP/1.0 200 OK\n");
403 output_headers(0, "");
405 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
406 printf("Content-type: image/gif\n");
407 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
408 printf("Content-type: image/jpeg\n");
409 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
410 printf("Content-type: text/html\n");
412 printf("Content-type: application/octet-stream\n");
414 fstat(fileno(fp), &statbuf);
415 bytes = statbuf.st_size;
416 printf("Content-length: %ld\n", (long) bytes);
419 putc(getc(fp), stdout);
432 off_t accomplished = 0L;
435 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
438 bytes = extract_long(&buf[4], 0);
439 printf("HTTP/1.0 200 OK\n");
440 output_headers(0, "");
441 printf("Content-type: image/gif\n");
442 printf("Content-length: %ld\n", (long) bytes);
445 while (bytes > (off_t) 0) {
446 thisblock = (off_t) sizeof(xferbuf);
447 if (thisblock > bytes)
449 serv_printf("READ %ld|%ld", accomplished, thisblock);
452 thisblock = extract_long(&buf[4], 0);
453 serv_read(xferbuf, (int) thisblock);
454 fwrite(xferbuf, thisblock, 1, stdout);
455 bytes = bytes - thisblock;
456 accomplished = accomplished + thisblock;
462 printf("HTTP/1.0 404 %s\n", strerror(errno));
463 output_headers(0, "");
464 printf("Content-Type: text/plain\n");
465 sprintf(buf, "Error retrieving image\n");
466 printf("Content-length: %d\n", strlen(buf));
468 fwrite(buf, strlen(buf), 1, stdout);
475 * Convenience functions to display a page containing only a string
477 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
479 printf("HTTP/1.0 200 OK\n");
480 output_headers(1, "bottom");
481 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
482 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
483 wprintf("<B>%s</B>\n", titlebarmsg);
484 wprintf("</FONT></TD></TR></TABLE><BR>\n");
485 escputs(messagetext);
494 void display_error(char *errormessage)
496 convenience_page("770000", "Error", errormessage);
499 void display_success(char *successmessage)
501 convenience_page("007700", "OK", successmessage);
506 void extract_action(char *actbuf, char *cmdbuf)
510 strcpy(actbuf, cmdbuf);
511 if (!strncasecmp(actbuf, "GET /", 5))
512 strcpy(actbuf, &actbuf[5]);
513 if (!strncasecmp(actbuf, "PUT /", 5))
514 strcpy(actbuf, &actbuf[5]);
515 if (!strncasecmp(actbuf, "POST /", 6))
516 strcpy(actbuf, &actbuf[6]);
518 for (i = 0; i < strlen(actbuf); ++i) {
519 if (actbuf[i] == ' ') {
523 if (actbuf[i] == '/') {
527 if (actbuf[i] == '?') {
531 if (actbuf[i] == '&') {
539 void upload_handler(char *name, char *filename, char *encoding,
540 void *content, char *cbtype, size_t length)
543 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
544 fprintf(stderr, " name = %s\n", name);
545 fprintf(stderr, "filename = %s\n", filename);
546 fprintf(stderr, "encoding = %s\n", encoding);
547 fprintf(stderr, " type = %s\n", cbtype);
548 fprintf(stderr, " length = %d\n", length);
550 if (strlen(name) > 0) {
551 upload = malloc(length);
552 if (upload != NULL) {
553 upload_length = length;
554 memcpy(upload, content, length);
560 void session_loop(char *browser_host, int bd_use_frames)
566 int ContentLength = 0;
567 char ContentType[512];
570 /* We stuff these with the values coming from the client cookies,
571 * so we can use them to reconnect a timed out session if we have to.
575 char c_username[256];
576 char c_password[256];
577 char c_roomname[256];
580 strcpy(c_host, defaulthost);
581 strcpy(c_port, defaultport);
582 strcpy(c_username, "");
583 strcpy(c_password, "");
584 strcpy(c_roomname, "");
589 if (getz(cmd) == NULL)
591 extract_action(action, cmd);
594 if (getz(buf) == NULL)
597 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
598 strcpy(cookie, &buf[15]);
599 cookie_to_stuff(cookie, NULL,
600 c_username, c_password, c_roomname,
603 if (!strncasecmp(buf, "Content-length: ", 16)) {
604 ContentLength = atoi(&buf[16]);
606 if (!strncasecmp(buf, "Content-type: ", 14)) {
607 strcpy(ContentType, &buf[14]);
609 } while (strlen(buf) > 0);
613 if (ContentLength > 0) {
614 content = malloc(ContentLength + 1);
615 fread(content, ContentLength, 1, stdin);
617 content[ContentLength] = 0;
619 if (!strncasecmp(ContentType,
620 "application/x-www-form-urlencoded", 33)) {
622 } else if (!strncasecmp(ContentType, "multipart", 9)) {
623 mime_parser(content, ContentLength, ContentType,
630 /* If there are variables in the URL, we must grab them now */
631 for (a = 0; a < strlen(cmd); ++a)
632 if ((cmd[a] == '?') || (cmd[a] == '&')) {
633 for (b = a; b < strlen(cmd); ++b)
636 addurls(&cmd[a + 1]);
640 * If we're not connected to a Citadel server, try to hook up the
641 * connection now. Preference is given to the host and port specified
642 * by browser cookies, if cookies have been supplied.
645 if (strlen(bstr("host")) > 0)
646 strcpy(c_host, bstr("host"));
647 if (strlen(bstr("port")) > 0)
648 strcpy(c_port, bstr("port"));
649 serv_sock = connectsock(c_host, c_port, "tcp");
651 serv_gets(buf); /* get the server welcome message */
652 get_serv_info(browser_host);
654 check_for_express_messages();
657 * If we're not logged in, but we have username and password cookies
658 * supplied by the browser, try using them to log in.
660 if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
661 serv_printf("USER %s", c_username);
664 serv_printf("PASS %s", c_password);
667 become_logged_in(c_username, c_password, buf);
672 * If we don't have a current room, but a cookie specifying the
673 * current room is supplied, make an effort to go there.
675 if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
676 serv_printf("GOTO %s", c_roomname);
679 strcpy(wc_roomname, c_roomname);
682 if (!strcasecmp(action, "static")) {
683 strcpy(buf, &cmd[12]);
684 for (a = 0; a < strlen(buf); ++a)
688 } else if (!strcasecmp(action, "image")) {
690 } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
692 } else if (!logged_in) {
695 /* Various commands... */
697 else if (!strcasecmp(action, "do_welcome")) {
699 } else if (!strcasecmp(action, "display_main_menu")) {
701 } else if (!strcasecmp(action, "advanced")) {
702 display_advanced_menu();
703 } else if (!strcasecmp(action, "whobbs")) {
705 } else if (!strcasecmp(action, "knrooms")) {
706 list_all_rooms_by_floor();
707 } else if (!strcasecmp(action, "gotonext")) {
710 } else if (!strcasecmp(action, "skip")) {
712 } else if (!strcasecmp(action, "ungoto")) {
714 } else if (!strcasecmp(action, "dotgoto")) {
716 gotoroom(bstr("room"), 1);
717 } else if (!strcasecmp(action, "termquit")) {
719 } else if (!strcasecmp(action, "readnew")) {
721 } else if (!strcasecmp(action, "readold")) {
723 } else if (!strcasecmp(action, "readfwd")) {
725 } else if (!strcasecmp(action, "display_enter")) {
727 } else if (!strcasecmp(action, "post")) {
729 } else if (!strcasecmp(action, "confirm_delete_msg")) {
730 confirm_delete_msg();
731 } else if (!strcasecmp(action, "delete_msg")) {
733 } else if (!strcasecmp(action, "confirm_move_msg")) {
735 } else if (!strcasecmp(action, "move_msg")) {
737 } else if (!strcasecmp(action, "userlist")) {
739 } else if (!strcasecmp(action, "showuser")) {
741 } else if (!strcasecmp(action, "display_page")) {
743 } else if (!strcasecmp(action, "page_user")) {
745 } else if (!strcasecmp(action, "chat")) {
747 } else if (!strcasecmp(action, "display_private")) {
748 display_private("", 0);
749 } else if (!strcasecmp(action, "goto_private")) {
751 } else if (!strcasecmp(action, "zapped_list")) {
753 } else if (!strcasecmp(action, "display_zap")) {
755 } else if (!strcasecmp(action, "zap")) {
757 } else if (!strcasecmp(action, "display_entroom")) {
759 } else if (!strcasecmp(action, "entroom")) {
761 } else if (!strcasecmp(action, "display_editroom")) {
763 } else if (!strcasecmp(action, "editroom")) {
765 } else if (!strcasecmp(action, "display_editinfo")) {
766 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
767 } else if (!strcasecmp(action, "editinfo")) {
768 save_edit("Room info", "EINF 1", 1);
769 } else if (!strcasecmp(action, "display_editbio")) {
770 sprintf(buf, "RBIO %s", wc_username);
771 display_edit("Your bio", "NOOP", buf, "editbio");
772 } else if (!strcasecmp(action, "editbio")) {
773 save_edit("Your bio", "EBIO", 0);
774 } else if (!strcasecmp(action, "confirm_delete_room")) {
775 confirm_delete_room();
776 } else if (!strcasecmp(action, "delete_room")) {
778 } else if (!strcasecmp(action, "validate")) {
780 } else if (!strcasecmp(action, "display_editpic")) {
781 display_graphics_upload("your photo",
784 } else if (!strcasecmp(action, "editpic")) {
785 do_graphics_upload("UIMG 1|_userpic_");
786 } else if (!strcasecmp(action, "display_editroompic")) {
787 display_graphics_upload("the graphic for this room",
790 } else if (!strcasecmp(action, "editroompic")) {
791 do_graphics_upload("UIMG 1|_roompic_");
792 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
793 select_floor_to_edit_pic();
794 } else if (!strcasecmp(action, "display_editfloorpic")) {
795 sprintf(buf, "UIMG 0|_floorpic_|%s",
796 bstr("which_floor"));
797 display_graphics_upload("the graphic for this floor",
800 } else if (!strcasecmp(action, "editfloorpic")) {
801 sprintf(buf, "UIMG 1|_floorpic_|%s",
802 bstr("which_floor"));
803 do_graphics_upload(buf);
804 } else if (!strcasecmp(action, "display_reg")) {
806 } else if (!strcasecmp(action, "register")) {
808 } else if (!strcasecmp(action, "display_changepw")) {
810 } else if (!strcasecmp(action, "changepw")) {
812 } else if (!strcasecmp(action, "display_edit_node")) {
814 } else if (!strcasecmp(action, "display_netconf")) {
816 } else if (!strcasecmp(action, "display_confirm_unshare")) {
817 display_confirm_unshare();
818 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
819 display_confirm_delete_node();
820 } else if (!strcasecmp(action, "delete_node")) {
822 } else if (!strcasecmp(action, "unshare")) {
824 } else if (!strcasecmp(action, "display_add_node")) {
826 } else if (!strcasecmp(action, "add_node")) {
828 } else if (!strcasecmp(action, "display_share")) {
830 } else if (!strcasecmp(action, "share")) {
832 } else if (!strcasecmp(action, "terminate_session")) {
834 } else if (!strcasecmp(action, "edit_me")) {
836 } else if (!strcasecmp(action, "display_siteconfig")) {
837 display_siteconfig();
838 } else if (!strcasecmp(action, "siteconfig")) {
840 } else if (!strcasecmp(action, "display_generic")) {
842 } else if (!strcasecmp(action, "do_generic")) {
845 /* When all else fails... */
847 printf("HTTP/1.0 200 OK\n");
848 output_headers(1, "");
850 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
851 wprintf("You're in session %d<HR>\n", wc_session);
852 wprintf("Command: <BR><PRE>\n");
854 wprintf("</PRE><HR>\n");
855 wprintf("Variables: <BR><PRE>\n");
857 wprintf("</PRE><HR>\n");
862 if (content != NULL) {
867 if (upload_length > 0) {
873 int main(int argc, char *argv[])
881 "webcit: usage error (argc must be 6, not %d)\n",
885 wc_session = atoi(argv[1]);
886 defaulthost = argv[2];
887 defaultport = argv[3];
889 strcpy(wc_username, "");
890 strcpy(wc_password, "");
891 strcpy(wc_roomname, "");
893 strcpy(browser, argv[5]);
894 bd_use_frames = browser_braindamage_check(browser);
897 session_loop(argv[4], bd_use_frames);