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>
34 * String to unset the cookie.
35 * Any date "in the past" will work, so I chose my birthday, right down to
36 * the exact minute. :)
38 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
40 void unescape_input(char *buf)
45 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
46 buf[strlen(buf) - 1] = 0;
48 for (a = 0; a < strlen(buf); ++a) {
55 sscanf(hex, "%02x", &b);
57 strcpy(&buf[a + 1], &buf[a + 3]);
64 void addurls(char *url)
72 while (strlen(up) > 0) {
74 /* locate the = sign */
75 strncpy(buf, up, 255);
77 for (a = 255; a >= 0; --a)
84 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
85 u->next = WC->urlstrings;
87 strcpy(u->url_key, buf);
89 /* now chop that part off */
90 for (a = 0; a <= b; ++a)
93 /* locate the & sign */
96 for (a = 0; a < strlen(up); ++a) {
97 if (!strncmp(ptr, "&", 1)) {
104 for (a = 0; a < b; ++a)
108 u->url_data = malloc(strlen(up) + 1);
109 strcpy(u->url_data, up);
111 unescape_input(u->url_data);
119 struct urlcontent *u;
121 while (WC->urlstrings != NULL) {
122 free(WC->urlstrings->url_data);
123 u = WC->urlstrings->next;
124 free(WC->urlstrings);
130 * Diagnostic function to display the contents of all variables
134 struct urlcontent *u;
136 for (u = WC->urlstrings; u != NULL; u = u->next) {
137 wprintf("%38s = %s\n", u->url_key, u->url_data);
141 char *bstr(char *key)
143 struct urlcontent *u;
145 for (u = WC->urlstrings; u != NULL; u = u->next) {
146 if (!strcasecmp(u->url_key, key))
147 return (u->url_data);
153 void wprintf(const char *format,...)
158 va_start(arg_ptr, format);
159 vsprintf(wbuf, format, arg_ptr);
162 write(WC->http_sock, wbuf, strlen(wbuf));
167 * wDumpContent() wraps up an HTTP session, closes tags, etc.
169 * print_standard_html_footer should be set to 0 to transmit only, 1 to
170 * append the main menu and closing tags, or 2 to
171 * append the closing tags only.
173 void wDumpContent(int print_standard_html_footer)
175 if (WC->fake_frames) {
176 wprintf("</TABLE>\n");
180 if (print_standard_html_footer) {
181 if (print_standard_html_footer != 2) {
184 wprintf("</BODY></HTML>\n");
192 * Copy a string, escaping characters which have meaning in HTML. If
193 * nbsp is nonzero, spaces are converted to non-breaking spaces.
195 void stresc(char *target, char *strbuf, int nbsp)
200 for (a = 0; a < strlen(strbuf); ++a) {
201 if (strbuf[a] == '<')
202 strcat(target, "<");
203 else if (strbuf[a] == '>')
204 strcat(target, ">");
205 else if (strbuf[a] == '&')
206 strcat(target, "&");
207 else if (strbuf[a] == '\"')
208 strcat(target, """);
209 else if (strbuf[a] == '\'')
210 strcat(target, "'");
211 else if (strbuf[a] == LB)
213 else if (strbuf[a] == RB)
215 else if (strbuf[a] == QU)
216 strcat(target, "\"");
217 else if ((strbuf[a] == 32) && (nbsp == 1)) {
218 strcat(target, " ");
220 strncat(target, &strbuf[a], 1);
225 void escputs1(char *strbuf, int nbsp)
228 stresc(buf, strbuf, nbsp);
232 void escputs(char *strbuf)
238 * Escape a string for feeding out as a URL.
239 * FIXME this is not threadsafe!
241 char *urlesc(char *strbuf)
244 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
245 static char outbuf[1024];
249 for (a = 0; a < strlen(strbuf); ++a) {
251 for (b = 0; b < strlen(ec); ++b) {
252 if (strbuf[a] == ec[b])
257 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
259 sprintf(&outbuf[b], "%c", strbuf[a]);
264 void urlescputs(char *strbuf)
266 wprintf("%s", urlesc(strbuf));
273 * Output all that important stuff that the browser will want to see
278 * 0 = Nothing. Do not display any leading HTTP or HTML.
279 * 1 = HTTP headers plus the room banner
280 * 2 = HTTP headers required to terminate the session (unset cookies)
281 * 3 = HTTP and HTML headers, but no room banner
283 * Bit 2: Set to 1 to auto-refresh page every 30 seconds
285 * Bit 3: suppress check for express messages
287 void output_headers(int controlcode)
290 int print_standard_html_head = 0;
292 int suppress_check = 0;
294 static int pageseq = 0;
296 print_standard_html_head = controlcode & 0x03;
297 refresh30 = ((controlcode & 0x04) >> 2);
298 suppress_check = ((controlcode & 0x08) >> 3);
300 wprintf("HTTP/1.0 200 OK\n");
302 httpdate(httpnow, time(NULL));
304 if (print_standard_html_head > 0) {
305 wprintf("Content-type: text/html\n");
306 wprintf("Server: %s\n", SERVER);
307 wprintf("Connection: close\n");
308 wprintf("Pragma: no-cache\n");
309 wprintf("Cache-Control: no-store\n");
311 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
312 WC->wc_password, WC->wc_roomname);
313 if (print_standard_html_head == 2) {
314 wprintf("Set-cookie: webcit=%s\n", unset);
316 wprintf("Set-cookie: webcit=%s\n", cookie);
319 if (print_standard_html_head > 0) {
321 wprintf("<HTML><HEAD><TITLE>");
322 escputs(serv_info.serv_humannode);
324 "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n"
325 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
326 if (refresh30) wprintf(
327 "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
328 wprintf("</HEAD>\n");
330 /* script for checking for express msgs (not always launch) */
331 wprintf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
332 wprintf("function launch_page_popup() {\n");
333 wprintf("pwin = window.open('/page_popup', 'CitaPage%d', 'toolbar=no,location=no,copyhistory=no,status=no,scrollbars=yes,resizable=no,height=150,width=400');\n");
335 wprintf("</SCRIPT>\n", ++pageseq);
338 /* JavaScript keyboard-based navigation would go here if it
342 if (!suppress_check) if (WC->HaveExpressMessages) {
343 svprintf("extrabodyparms", WCS_STRING, "%s",
344 "onload=\"launch_page_popup()\" ");
345 WC->HaveExpressMessages = 0;
347 do_template("background.html");
348 clear_local_substs();
350 if (print_standard_html_head == 1) {
351 wprintf("<A NAME=\"TheTop\"></A>");
353 wprintf("<TABLE border=0 width=100%%><TR VALIGN=TOP>"
356 embed_room_banner(NULL);
358 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
369 void http_redirect(char *whichpage) {
370 wprintf("HTTP/1.0 302 Moved Temporarily\n");
371 wprintf("Location: %s\n", whichpage);
372 wprintf("URI: %s\n", whichpage);
373 wprintf("Content-type: text/html\n\n");
374 wprintf("<html><body>\n");
375 wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
377 wprintf("</body></html>\n");
382 void check_for_express_messages()
388 if (buf[3] == '*') WC->HaveExpressMessages = 1;
394 void output_static(char *what)
402 sprintf(buf, "static/%s", what);
403 fp = fopen(buf, "rb");
405 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
406 wprintf("Content-Type: text/plain\n");
408 wprintf("Cannot open %s: %s\n", what, strerror(errno));
412 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
413 wprintf("Content-type: image/gif\n");
414 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
415 wprintf("Content-type: text/plain\n");
416 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
417 wprintf("Content-type: image/jpeg\n");
418 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
419 wprintf("Content-type: text/html\n");
421 wprintf("Content-type: application/octet-stream\n");
423 fstat(fileno(fp), &statbuf);
424 bytes = statbuf.st_size;
425 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
426 wprintf("Content-length: %ld\n", (long) bytes);
429 thisblock = sizeof(buf);
430 if (thisblock > bytes) thisblock = bytes;
431 fread(buf, thisblock, 1, fp);
432 write(WC->http_sock, buf, thisblock);
433 bytes = bytes - thisblock;
437 if (!strcasecmp(bstr("force_close_session"), "yes")) {
438 end_webcit_session();
443 * When the browser requests an image file from the Citadel server,
444 * this function is called to transmit it.
452 off_t accomplished = 0L;
455 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
458 bytes = extract_long(&buf[4], 0);
460 wprintf("Content-type: image/gif\n");
461 wprintf("Content-length: %ld\n", (long) bytes);
464 while (bytes > (off_t) 0) {
465 thisblock = (off_t) sizeof(xferbuf);
466 if (thisblock > bytes)
468 serv_printf("READ %ld|%ld", accomplished, thisblock);
471 thisblock = extract_long(&buf[4], 0);
472 serv_read(xferbuf, (int) thisblock);
473 write(WC->http_sock, xferbuf, thisblock);
474 bytes = bytes - thisblock;
475 accomplished = accomplished + thisblock;
480 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
482 wprintf("Content-Type: text/plain\n");
484 wprintf("Error retrieving image\n");
491 * Convenience functions to display a page containing only a string
493 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
495 wprintf("HTTP/1.0 200 OK\n");
497 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
498 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
499 wprintf("<B>%s</B>\n", titlebarmsg);
500 wprintf("</FONT></TD></TR></TABLE><BR>\n");
501 escputs(messagetext);
510 * Display a blank page.
512 void blank_page(void) {
518 void display_error(char *errormessage)
520 convenience_page("770000", "Error", errormessage);
523 void display_success(char *successmessage)
525 convenience_page("007700", "OK", successmessage);
530 void extract_action(char *actbuf, char *cmdbuf)
534 strcpy(actbuf, cmdbuf);
535 if (!strncasecmp(actbuf, "GET /", 5))
536 strcpy(actbuf, &actbuf[5]);
537 if (!strncasecmp(actbuf, "PUT /", 5))
538 strcpy(actbuf, &actbuf[5]);
539 if (!strncasecmp(actbuf, "POST /", 6))
540 strcpy(actbuf, &actbuf[6]);
542 for (i = 0; i < strlen(actbuf); ++i) {
543 if (actbuf[i] == ' ') {
547 if (actbuf[i] == '/') {
551 if (actbuf[i] == '?') {
555 if (actbuf[i] == '&') {
563 void upload_handler(char *name, char *filename, char *encoding,
564 void *content, char *cbtype, size_t length)
567 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
568 fprintf(stderr, " name = %s\n", name);
569 fprintf(stderr, "filename = %s\n", filename);
570 fprintf(stderr, "encoding = %s\n", encoding);
571 fprintf(stderr, " type = %s\n", cbtype);
572 fprintf(stderr, " length = %ld\n", (long)length);
574 if (strlen(name) > 0) {
575 WC->upload = malloc(length);
576 if (WC->upload != NULL) {
577 WC->upload_length = length;
578 memcpy(WC->upload, content, length);
585 * Entry point for WebCit transaction
587 void session_loop(struct httprequest *req)
593 int ContentLength = 0;
595 char ContentType[512];
597 struct httprequest *hptr;
598 char browser_host[256];
599 char user_agent[256];
601 /* We stuff these with the values coming from the client cookies,
602 * so we can use them to reconnect a timed out session if we have to.
606 char c_username[256];
607 char c_password[256];
608 char c_roomname[256];
611 strcpy(c_host, defaulthost);
612 strcpy(c_port, defaultport);
613 strcpy(c_username, "");
614 strcpy(c_password, "");
615 strcpy(c_roomname, "");
617 WC->upload_length = 0;
621 if (hptr == NULL) return;
623 strcpy(cmd, hptr->line);
625 extract_action(action, cmd);
627 while (hptr != NULL) {
628 strcpy(buf, hptr->line);
631 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
632 strcpy(cookie, &buf[15]);
633 cookie_to_stuff(cookie, NULL,
634 c_username, c_password, c_roomname);
636 else if (!strncasecmp(buf, "Content-length: ", 16)) {
637 ContentLength = atoi(&buf[16]);
639 else if (!strncasecmp(buf, "Content-type: ", 14)) {
640 strcpy(ContentType, &buf[14]);
642 else if (!strncasecmp(buf, "User-agent: ", 12)) {
643 strcpy(user_agent, &buf[12]);
647 if (ContentLength > 0) {
648 fprintf(stderr, "Content length: %d\n", ContentLength);
649 content = malloc(ContentLength + 1);
650 memset(content, 0, ContentLength+1);
653 while (BytesRead < ContentLength) {
654 a=read(WC->http_sock, &content[BytesRead],
655 ContentLength - BytesRead);
656 if (a <= 0) BytesRead = ContentLength;
660 if (!strncasecmp(ContentType,
661 "application/x-www-form-urlencoded", 33)) {
663 } else if (!strncasecmp(ContentType, "multipart", 9)) {
664 mime_parser(content, ContentLength, ContentType,
671 /* If there are variables in the URL, we must grab them now */
672 for (a = 0; a < strlen(cmd); ++a)
673 if ((cmd[a] == '?') || (cmd[a] == '&')) {
674 for (b = a; b < strlen(cmd); ++b)
677 addurls(&cmd[a + 1]);
681 * If we're not connected to a Citadel server, try to hook up the
682 * connection now. Preference is given to the host and port specified
683 * by browser cookies, if cookies have been supplied.
685 if (!WC->connected) {
686 if (strlen(bstr("host")) > 0)
687 strcpy(c_host, bstr("host"));
688 if (strlen(bstr("port")) > 0)
689 strcpy(c_port, bstr("port"));
691 if (!strcasecmp(c_host, "uds")) {
692 /* unix domain socket */
693 sprintf(buf, "%s/citadel.socket", c_port);
694 WC->serv_sock = uds_connectsock(buf);
698 WC->serv_sock = tcp_connectsock(c_host, c_port);
701 if (WC->serv_sock < 0) {
706 serv_gets(buf); /* get the server welcome message */
707 locate_host(browser_host, WC->http_sock);
708 get_serv_info(browser_host, user_agent);
711 check_for_express_messages();
714 * If we're not logged in, but we have username and password cookies
715 * supplied by the browser, try using them to log in.
717 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
718 serv_printf("USER %s", c_username);
721 serv_printf("PASS %s", c_password);
724 become_logged_in(c_username, c_password, buf);
729 * If we don't have a current room, but a cookie specifying the
730 * current room is supplied, make an effort to go there.
732 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
733 serv_printf("GOTO %s", c_roomname);
736 strcpy(WC->wc_roomname, c_roomname);
739 if (!strcasecmp(action, "static")) {
740 strcpy(buf, &cmd[12]);
741 for (a = 0; a < strlen(buf); ++a)
745 } else if (!strcasecmp(action, "image")) {
747 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
749 } else if (!WC->logged_in) {
752 /* Various commands... */
754 else if (!strcasecmp(action, "do_welcome")) {
756 } else if (!strcasecmp(action, "blank")) {
758 } else if (!strcasecmp(action, "display_main_menu")) {
760 } else if (!strcasecmp(action, "advanced")) {
761 display_advanced_menu();
762 } else if (!strcasecmp(action, "whobbs")) {
764 } else if (!strcasecmp(action, "knrooms")) {
765 list_all_rooms_by_floor();
766 } else if (!strcasecmp(action, "gotonext")) {
769 } else if (!strcasecmp(action, "skip")) {
771 } else if (!strcasecmp(action, "ungoto")) {
773 } else if (!strcasecmp(action, "dotgoto")) {
775 smart_goto(bstr("room"));
776 } else if (!strcasecmp(action, "termquit")) {
778 } else if (!strcasecmp(action, "readnew")) {
780 } else if (!strcasecmp(action, "readold")) {
782 } else if (!strcasecmp(action, "readfwd")) {
784 } else if (!strcasecmp(action, "headers")) {
786 } else if (!strcasecmp(action, "display_enter")) {
788 } else if (!strcasecmp(action, "post")) {
790 } else if (!strcasecmp(action, "delete_msg")) {
792 } else if (!strcasecmp(action, "confirm_move_msg")) {
794 } else if (!strcasecmp(action, "move_msg")) {
796 } else if (!strcasecmp(action, "userlist")) {
798 } else if (!strcasecmp(action, "showuser")) {
800 } else if (!strcasecmp(action, "display_page")) {
802 } else if (!strcasecmp(action, "page_user")) {
804 } else if (!strcasecmp(action, "chat")) {
806 } else if (!strcasecmp(action, "display_private")) {
807 display_private("", 0);
808 } else if (!strcasecmp(action, "goto_private")) {
810 } else if (!strcasecmp(action, "zapped_list")) {
812 } else if (!strcasecmp(action, "display_zap")) {
814 } else if (!strcasecmp(action, "zap")) {
816 } else if (!strcasecmp(action, "display_entroom")) {
818 } else if (!strcasecmp(action, "entroom")) {
820 } else if (!strcasecmp(action, "display_editroom")) {
822 } else if (!strcasecmp(action, "editroom")) {
824 } else if (!strcasecmp(action, "display_whok")) {
826 } else if (!strcasecmp(action, "display_editinfo")) {
827 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
828 } else if (!strcasecmp(action, "editinfo")) {
829 save_edit("Room info", "EINF 1", 1);
830 } else if (!strcasecmp(action, "display_editbio")) {
831 sprintf(buf, "RBIO %s", WC->wc_username);
832 display_edit("Your bio", "NOOP", buf, "editbio");
833 } else if (!strcasecmp(action, "editbio")) {
834 save_edit("Your bio", "EBIO", 0);
835 } else if (!strcasecmp(action, "confirm_delete_room")) {
836 confirm_delete_room();
837 } else if (!strcasecmp(action, "delete_room")) {
839 } else if (!strcasecmp(action, "validate")) {
841 } else if (!strcasecmp(action, "display_editpic")) {
842 display_graphics_upload("your photo",
845 } else if (!strcasecmp(action, "editpic")) {
846 do_graphics_upload("UIMG 1|_userpic_");
847 } else if (!strcasecmp(action, "display_editroompic")) {
848 display_graphics_upload("the graphic for this room",
851 } else if (!strcasecmp(action, "editroompic")) {
852 do_graphics_upload("UIMG 1|_roompic_");
853 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
854 select_floor_to_edit_pic();
855 } else if (!strcasecmp(action, "display_editfloorpic")) {
856 sprintf(buf, "UIMG 0|_floorpic_|%s",
857 bstr("which_floor"));
858 display_graphics_upload("the graphic for this floor",
861 } else if (!strcasecmp(action, "editfloorpic")) {
862 sprintf(buf, "UIMG 1|_floorpic_|%s",
863 bstr("which_floor"));
864 do_graphics_upload(buf);
865 } else if (!strcasecmp(action, "display_reg")) {
867 } else if (!strcasecmp(action, "register")) {
869 } else if (!strcasecmp(action, "display_changepw")) {
871 } else if (!strcasecmp(action, "changepw")) {
873 } else if (!strcasecmp(action, "display_edit_node")) {
875 } else if (!strcasecmp(action, "display_netconf")) {
877 } else if (!strcasecmp(action, "display_confirm_unshare")) {
878 display_confirm_unshare();
879 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
880 display_confirm_delete_node();
881 } else if (!strcasecmp(action, "delete_node")) {
883 } else if (!strcasecmp(action, "unshare")) {
885 } else if (!strcasecmp(action, "display_add_node")) {
887 } else if (!strcasecmp(action, "add_node")) {
889 } else if (!strcasecmp(action, "display_share")) {
891 } else if (!strcasecmp(action, "share")) {
893 } else if (!strcasecmp(action, "terminate_session")) {
896 } else if (!strcasecmp(action, "edit_me")) {
898 } else if (!strcasecmp(action, "display_siteconfig")) {
899 display_siteconfig();
900 } else if (!strcasecmp(action, "page_popup")) {
902 } else if (!strcasecmp(action, "siteconfig")) {
904 } else if (!strcasecmp(action, "display_generic")) {
906 } else if (!strcasecmp(action, "do_generic")) {
908 } else if (!strcasecmp(action, "display_menubar")) {
910 } else if (!strcasecmp(action, "diagnostics")) {
913 wprintf("You're in session %d<HR>\n", WC->wc_session);
914 wprintf("Command: <BR><PRE>\n");
916 wprintf("</PRE><HR>\n");
917 wprintf("Variables: <BR><PRE>\n");
919 wprintf("</PRE><HR>\n");
922 /* When all else fais, display the main menu. */
928 if (content != NULL) {
933 if (WC->upload_length > 0) {
935 WC->upload_length = 0;