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.
17 #include <sys/types.h>
19 #include <sys/socket.h>
23 #include <netinet/in.h>
35 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 urlcontent *urlstrings = NULL;
44 static const char *defaulthost = DEFAULT_HOST;
45 static const char *defaultport = DEFAULT_PORT;
49 void unescape_input(char *buf)
54 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
55 buf[strlen(buf) - 1] = 0;
57 for (a = 0; a < strlen(buf); ++a) {
64 sscanf(hex, "%02x", &b);
66 strcpy(&buf[a + 1], &buf[a + 3]);
73 void addurls(char *url)
81 while (strlen(up) > 0) {
83 /* locate the = sign */
84 strncpy(buf, up, 255);
86 for (a = 255; a >= 0; --a)
93 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
96 strcpy(u->url_key, buf);
98 /* now chop that part off */
99 for (a = 0; a <= b; ++a)
102 /* locate the & sign */
105 for (a = 0; a < strlen(up); ++a) {
106 if (!strncmp(ptr, "&", 1)) {
113 for (a = 0; a < b; ++a)
117 u->url_data = malloc(strlen(up) + 1);
118 strcpy(u->url_data, up);
120 unescape_input(u->url_data);
128 struct urlcontent *u;
130 while (urlstrings != NULL) {
131 free(urlstrings->url_data);
132 u = urlstrings->next;
139 * Diagnostic function to display the contents of all variables
143 struct urlcontent *u;
145 for (u = urlstrings; u != NULL; u = u->next) {
146 wprintf("%38s = %s\n", u->url_key, u->url_data);
150 char *bstr(char *key)
152 struct urlcontent *u;
154 for (u = urlstrings; u != NULL; u = u->next) {
155 if (!strcasecmp(u->url_key, key))
156 return (u->url_data);
162 void wprintf(const char *format,...)
167 va_start(arg_ptr, format);
168 vsprintf(wbuf, format, arg_ptr);
171 write(WC->http_sock, wbuf, strlen(wbuf));
176 * wDumpContent() wraps up an HTTP session, closes tags, etc.
178 * print_standard_html_footer should be set to 0 to transmit only, 1 to
179 * append the main menu and closing tags, or 2 to
180 * append the closing tags only.
182 void wDumpContent(int print_standard_html_footer)
185 wprintf("<CENTER><FONT SIZE=-1>"
186 "<TABLE border=0 width=100%><TR>"
187 "<TD><A HREF=\"/ungoto\">"
188 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
190 wprintf("<TD><A HREF=\"#TheTop\">"
191 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
192 "Top of page</A></TD>");
193 wprintf("<TD><A HREF=\"/display_enter\">"
194 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
195 "Enter a message</A></TD>");
196 wprintf("<TD><A HREF=\"/gotonext\">"
198 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
201 "</TD></TR></TABLE></TABLE>\n");
205 if (print_standard_html_footer) {
206 if (print_standard_html_footer != 2) {
209 wprintf("</BODY></HTML>\n");
216 void escputs1(char *strbuf, int nbsp)
220 for (a = 0; a < strlen(strbuf); ++a) {
221 if (strbuf[a] == '<')
223 else if (strbuf[a] == '>')
225 else if (strbuf[a] == '&')
227 else if (strbuf[a] == '\"')
229 else if (strbuf[a] == '\'')
231 else if (strbuf[a] == LB)
233 else if (strbuf[a] == RB)
235 else if (strbuf[a] == QU)
237 else if ((strbuf[a] == 32) && (nbsp == 1)) {
240 wprintf("%c", strbuf[a]);
245 void escputs(char *strbuf)
252 char *urlesc(char *strbuf)
255 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
256 static char outbuf[512];
260 for (a = 0; a < strlen(strbuf); ++a) {
262 for (b = 0; b < strlen(ec); ++b) {
263 if (strbuf[a] == ec[b])
268 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
270 sprintf(&outbuf[b], "%c", strbuf[a]);
275 void urlescputs(char *strbuf)
277 wprintf("%s", urlesc(strbuf));
284 * Output all that important stuff that the browser will want to see
286 * print_standard_html_head values:
287 * 0 = Nothing. Do not display any leading HTTP or HTML.
288 * 1 = HTTP headers plus the "fake frames" found in most windows.
289 * 2 = HTTP headers required to terminate the session (unset cookies)
290 * 3 = HTTP headers only.
292 void output_headers(int print_standard_html_head)
295 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
298 wprintf("HTTP/1.0 200 OK\n");
300 if (print_standard_html_head > 0) {
301 wprintf("Content-type: text/html\n");
302 wprintf("Server: %s\n", SERVER);
303 wprintf("Connection: close\n");
304 wprintf("Pragma: no-cache\n");
305 wprintf("Cache-Control: no-store\n");
307 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
308 WC->wc_password, WC->wc_roomname);
309 if (print_standard_html_head == 2) {
310 wprintf("X-WebCit-Session: close\n");
311 wprintf("Set-cookie: webcit=%s\n", unset);
313 wprintf("Set-cookie: webcit=%s\n", cookie);
316 if (print_standard_html_head > 0) {
318 wprintf("<HTML><HEAD><TITLE>");
319 escputs(serv_info.serv_humannode);
321 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
323 if (ExpressMessages != NULL) {
324 wprintf("<SCRIPT language=\"javascript\">\n");
325 wprintf("function ExpressMessage() {\n");
326 wprintf(" alert(\"");
327 escputs(ExpressMessages);
329 wprintf(" }\n </SCRIPT>\n");
334 /* JavaScript key-based navigation would go here if it
339 if (ExpressMessages != NULL) {
340 wprintf("onload=\"ExpressMessage()\" ");
341 free(ExpressMessages);
342 ExpressMessages = NULL;
344 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
347 if (print_standard_html_head == 1) {
348 wprintf("<A NAME=\"TheTop\"></A>"
349 "<TABLE border=0 width=100%>"
350 "<TR VALIGN=TOP><TD>");
354 wprintf("</TD><TD VALIGN=TOP>"
355 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
358 embed_room_banner(NULL);
360 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
369 void ExpressMessageCat(char *buf) {
370 if (ExpressMessages == NULL) {
371 ExpressMessages = malloc(strlen(buf) + 4);
372 strcpy(ExpressMessages, "");
374 ExpressMessages = realloc(ExpressMessages,
375 (strlen(ExpressMessages) + strlen(buf) + 4));
377 strcat(ExpressMessages, buf);
378 strcat(ExpressMessages, "\\n");
382 void check_for_express_messages()
389 while (serv_gets(buf), strcmp(buf, "000")) {
390 ExpressMessageCat(buf);
398 void output_static(char *what)
406 sprintf(buf, "static/%s", what);
407 fp = fopen(buf, "rb");
409 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
410 wprintf("Content-Type: text/plain\n");
412 wprintf("Cannot open %s: %s\n", what, strerror(errno));
416 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
417 wprintf("Content-type: image/gif\n");
418 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
419 wprintf("Content-type: text/plain\n");
420 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
421 wprintf("Content-type: image/jpeg\n");
422 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
423 wprintf("Content-type: text/html\n");
425 wprintf("Content-type: application/octet-stream\n");
427 fstat(fileno(fp), &statbuf);
428 bytes = statbuf.st_size;
429 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
430 wprintf("Content-length: %ld\n", (long) bytes);
433 thisblock = sizeof(buf);
434 if (thisblock > bytes) thisblock = bytes;
435 fread(buf, thisblock, 1, fp);
436 write(WC->http_sock, buf, thisblock);
437 bytes = bytes - thisblock;
444 * When the browser requests an image file from the Citadel server,
445 * this function is called to transmit it.
453 off_t accomplished = 0L;
456 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
459 bytes = extract_long(&buf[4], 0);
461 wprintf("Content-type: image/gif\n");
462 wprintf("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 write(WC->http_sock, xferbuf, thisblock);
475 bytes = bytes - thisblock;
476 accomplished = accomplished + thisblock;
481 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
483 wprintf("Content-Type: text/plain\n");
485 wprintf("Error retrieving image\n");
492 * Convenience functions to display a page containing only a string
494 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
496 wprintf("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);
509 void display_error(char *errormessage)
511 convenience_page("770000", "Error", errormessage);
514 void display_success(char *successmessage)
516 convenience_page("007700", "OK", successmessage);
521 void extract_action(char *actbuf, char *cmdbuf)
525 strcpy(actbuf, cmdbuf);
526 if (!strncasecmp(actbuf, "GET /", 5))
527 strcpy(actbuf, &actbuf[5]);
528 if (!strncasecmp(actbuf, "PUT /", 5))
529 strcpy(actbuf, &actbuf[5]);
530 if (!strncasecmp(actbuf, "POST /", 6))
531 strcpy(actbuf, &actbuf[6]);
533 for (i = 0; i < strlen(actbuf); ++i) {
534 if (actbuf[i] == ' ') {
538 if (actbuf[i] == '/') {
542 if (actbuf[i] == '?') {
546 if (actbuf[i] == '&') {
554 void upload_handler(char *name, char *filename, char *encoding,
555 void *content, char *cbtype, size_t length)
558 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
559 fprintf(stderr, " name = %s\n", name);
560 fprintf(stderr, "filename = %s\n", filename);
561 fprintf(stderr, "encoding = %s\n", encoding);
562 fprintf(stderr, " type = %s\n", cbtype);
563 fprintf(stderr, " length = %d\n", length);
565 if (strlen(name) > 0) {
566 WC->upload = malloc(length);
567 if (WC->upload != NULL) {
568 WC->upload_length = length;
569 memcpy(WC->upload, content, length);
576 * Entry point for WebCit transaction
578 void session_loop(struct httprequest *req)
584 int ContentLength = 0;
585 char ContentType[512];
587 struct httprequest *hptr;
588 char browser_host[256];
589 char user_agent[256];
591 /* We stuff these with the values coming from the client cookies,
592 * so we can use them to reconnect a timed out session if we have to.
596 char c_username[256];
597 char c_password[256];
598 char c_roomname[256];
601 strcpy(c_host, defaulthost);
602 strcpy(c_port, defaultport);
603 strcpy(c_username, "");
604 strcpy(c_password, "");
605 strcpy(c_roomname, "");
607 WC->upload_length = 0;
611 if (hptr == NULL) return;
613 strcpy(cmd, hptr->line);
615 extract_action(action, cmd);
617 while (hptr != NULL) {
618 strcpy(buf, hptr->line);
621 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
622 strcpy(cookie, &buf[15]);
623 cookie_to_stuff(cookie, NULL,
624 c_username, c_password, c_roomname);
626 else if (!strncasecmp(buf, "Content-length: ", 16)) {
627 ContentLength = atoi(&buf[16]);
629 else if (!strncasecmp(buf, "Content-type: ", 14)) {
630 strcpy(ContentType, &buf[14]);
632 else if (!strncasecmp(buf, "User-agent: ", 12)) {
633 strcpy(user_agent, &buf[12]);
637 if (ContentLength > 0) {
638 content = malloc(ContentLength + 1);
639 read(WC->http_sock, content, ContentLength);
641 content[ContentLength] = 0;
643 if (!strncasecmp(ContentType,
644 "application/x-www-form-urlencoded", 33)) {
646 } else if (!strncasecmp(ContentType, "multipart", 9)) {
647 mime_parser(content, ContentLength, ContentType,
654 /* If there are variables in the URL, we must grab them now */
655 for (a = 0; a < strlen(cmd); ++a)
656 if ((cmd[a] == '?') || (cmd[a] == '&')) {
657 for (b = a; b < strlen(cmd); ++b)
660 addurls(&cmd[a + 1]);
664 * If we're not connected to a Citadel server, try to hook up the
665 * connection now. Preference is given to the host and port specified
666 * by browser cookies, if cookies have been supplied.
668 if (!WC->connected) {
669 if (strlen(bstr("host")) > 0)
670 strcpy(c_host, bstr("host"));
671 if (strlen(bstr("port")) > 0)
672 strcpy(c_port, bstr("port"));
673 WC->serv_sock = connectsock(c_host, c_port, "tcp");
674 if (WC->serv_sock < 0) {
679 serv_gets(buf); /* get the server welcome message */
680 locate_host(browser_host, WC->http_sock);
681 get_serv_info(browser_host, user_agent);
683 check_for_express_messages();
686 * If we're not logged in, but we have username and password cookies
687 * supplied by the browser, try using them to log in.
689 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
690 serv_printf("USER %s", c_username);
693 serv_printf("PASS %s", c_password);
696 become_logged_in(c_username, c_password, buf);
701 * If we don't have a current room, but a cookie specifying the
702 * current room is supplied, make an effort to go there.
704 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
705 serv_printf("GOTO %s", c_roomname);
708 strcpy(WC->wc_roomname, c_roomname);
711 if (!strcasecmp(action, "static")) {
712 strcpy(buf, &cmd[12]);
713 for (a = 0; a < strlen(buf); ++a)
717 } else if (!strcasecmp(action, "image")) {
719 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
721 } else if (!WC->logged_in) {
724 /* Various commands... */
726 else if (!strcasecmp(action, "do_welcome")) {
728 } else if (!strcasecmp(action, "display_main_menu")) {
730 } else if (!strcasecmp(action, "advanced")) {
731 display_advanced_menu();
732 } else if (!strcasecmp(action, "whobbs")) {
734 } else if (!strcasecmp(action, "knrooms")) {
735 list_all_rooms_by_floor();
736 } else if (!strcasecmp(action, "gotonext")) {
739 } else if (!strcasecmp(action, "skip")) {
741 } else if (!strcasecmp(action, "ungoto")) {
743 } else if (!strcasecmp(action, "dotgoto")) {
745 smart_goto(bstr("room"));
746 } else if (!strcasecmp(action, "termquit")) {
748 } else if (!strcasecmp(action, "readnew")) {
750 } else if (!strcasecmp(action, "readold")) {
752 } else if (!strcasecmp(action, "readfwd")) {
754 } else if (!strcasecmp(action, "display_enter")) {
756 } else if (!strcasecmp(action, "post")) {
758 } else if (!strcasecmp(action, "confirm_delete_msg")) {
759 confirm_delete_msg();
760 } else if (!strcasecmp(action, "delete_msg")) {
762 } else if (!strcasecmp(action, "confirm_move_msg")) {
764 } else if (!strcasecmp(action, "move_msg")) {
766 } else if (!strcasecmp(action, "userlist")) {
768 } else if (!strcasecmp(action, "showuser")) {
770 } else if (!strcasecmp(action, "display_page")) {
772 } else if (!strcasecmp(action, "page_user")) {
774 } else if (!strcasecmp(action, "chat")) {
776 } else if (!strcasecmp(action, "display_private")) {
777 display_private("", 0);
778 } else if (!strcasecmp(action, "goto_private")) {
780 } else if (!strcasecmp(action, "zapped_list")) {
782 } else if (!strcasecmp(action, "display_zap")) {
784 } else if (!strcasecmp(action, "zap")) {
786 } else if (!strcasecmp(action, "display_entroom")) {
788 } else if (!strcasecmp(action, "entroom")) {
790 } else if (!strcasecmp(action, "display_editroom")) {
792 } else if (!strcasecmp(action, "editroom")) {
794 } else if (!strcasecmp(action, "display_editinfo")) {
795 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
796 } else if (!strcasecmp(action, "editinfo")) {
797 save_edit("Room info", "EINF 1", 1);
798 } else if (!strcasecmp(action, "display_editbio")) {
799 sprintf(buf, "RBIO %s", WC->wc_username);
800 display_edit("Your bio", "NOOP", buf, "editbio");
801 } else if (!strcasecmp(action, "editbio")) {
802 save_edit("Your bio", "EBIO", 0);
803 } else if (!strcasecmp(action, "confirm_delete_room")) {
804 confirm_delete_room();
805 } else if (!strcasecmp(action, "delete_room")) {
807 } else if (!strcasecmp(action, "validate")) {
809 } else if (!strcasecmp(action, "display_editpic")) {
810 display_graphics_upload("your photo",
813 } else if (!strcasecmp(action, "editpic")) {
814 do_graphics_upload("UIMG 1|_userpic_");
815 } else if (!strcasecmp(action, "display_editroompic")) {
816 display_graphics_upload("the graphic for this room",
819 } else if (!strcasecmp(action, "editroompic")) {
820 do_graphics_upload("UIMG 1|_roompic_");
821 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
822 select_floor_to_edit_pic();
823 } else if (!strcasecmp(action, "display_editfloorpic")) {
824 sprintf(buf, "UIMG 0|_floorpic_|%s",
825 bstr("which_floor"));
826 display_graphics_upload("the graphic for this floor",
829 } else if (!strcasecmp(action, "editfloorpic")) {
830 sprintf(buf, "UIMG 1|_floorpic_|%s",
831 bstr("which_floor"));
832 do_graphics_upload(buf);
833 } else if (!strcasecmp(action, "display_reg")) {
835 } else if (!strcasecmp(action, "register")) {
837 } else if (!strcasecmp(action, "display_changepw")) {
839 } else if (!strcasecmp(action, "changepw")) {
841 } else if (!strcasecmp(action, "display_edit_node")) {
843 } else if (!strcasecmp(action, "display_netconf")) {
845 } else if (!strcasecmp(action, "display_confirm_unshare")) {
846 display_confirm_unshare();
847 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
848 display_confirm_delete_node();
849 } else if (!strcasecmp(action, "delete_node")) {
851 } else if (!strcasecmp(action, "unshare")) {
853 } else if (!strcasecmp(action, "display_add_node")) {
855 } else if (!strcasecmp(action, "add_node")) {
857 } else if (!strcasecmp(action, "display_share")) {
859 } else if (!strcasecmp(action, "share")) {
861 } else if (!strcasecmp(action, "terminate_session")) {
864 } else if (!strcasecmp(action, "edit_me")) {
866 } else if (!strcasecmp(action, "display_siteconfig")) {
867 display_siteconfig();
868 } else if (!strcasecmp(action, "siteconfig")) {
870 } else if (!strcasecmp(action, "display_generic")) {
872 } else if (!strcasecmp(action, "do_generic")) {
874 } else if (!strcasecmp(action, "display_menubar")) {
876 } else if (!strcasecmp(action, "diagnostics")) {
879 wprintf("You're in session %d<HR>\n", WC->wc_session);
880 wprintf("Command: <BR><PRE>\n");
882 wprintf("</PRE><HR>\n");
883 wprintf("Variables: <BR><PRE>\n");
885 wprintf("</PRE><HR>\n");
888 /* When all else fais, display the main menu. */
894 if (content != NULL) {
899 if (WC->upload_length > 0) {
901 WC->upload_length = 0;