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);
428 bigbuffer = malloc(bytes);
429 fread(bigbuffer, bytes, 1, fp);
431 write(WC->http_sock, bigbuffer, bytes);
434 if (!strcasecmp(bstr("force_close_session"), "yes")) {
435 end_webcit_session();
440 * When the browser requests an image file from the Citadel server,
441 * this function is called to transmit it.
449 off_t accomplished = 0L;
452 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
455 bytes = extract_long(&buf[4], 0);
457 wprintf("Content-type: image/gif\n");
458 wprintf("Content-length: %ld\n", (long) bytes);
461 while (bytes > (off_t) 0) {
462 thisblock = (off_t) sizeof(xferbuf);
463 if (thisblock > bytes) {
466 serv_printf("READ %ld|%ld", accomplished, thisblock);
469 thisblock = extract_long(&buf[4], 0);
470 serv_read(xferbuf, (int) thisblock);
473 memset(xferbuf, 0, thisblock);
475 write(WC->http_sock, xferbuf, thisblock);
476 bytes = bytes - thisblock;
477 accomplished = accomplished + thisblock;
482 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
484 wprintf("Content-Type: text/plain\n");
486 wprintf("Error retrieving image\n");
493 * Convenience functions to display a page containing only a string
495 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
497 wprintf("HTTP/1.0 200 OK\n");
499 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
500 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
501 wprintf("<B>%s</B>\n", titlebarmsg);
502 wprintf("</FONT></TD></TR></TABLE><BR>\n");
503 escputs(messagetext);
511 * Display a blank page.
513 void blank_page(void) {
519 void display_error(char *errormessage)
521 convenience_page("770000", "Error", errormessage);
524 void display_success(char *successmessage)
526 convenience_page("007700", "OK", successmessage);
531 void extract_action(char *actbuf, char *cmdbuf)
535 strcpy(actbuf, cmdbuf);
536 if (!strncasecmp(actbuf, "GET /", 5))
537 strcpy(actbuf, &actbuf[5]);
538 if (!strncasecmp(actbuf, "PUT /", 5))
539 strcpy(actbuf, &actbuf[5]);
540 if (!strncasecmp(actbuf, "POST /", 6))
541 strcpy(actbuf, &actbuf[6]);
543 for (i = 0; i < strlen(actbuf); ++i) {
544 if (actbuf[i] == ' ') {
548 if (actbuf[i] == '/') {
552 if (actbuf[i] == '?') {
556 if (actbuf[i] == '&') {
564 void upload_handler(char *name, char *filename, char *encoding,
565 void *content, char *cbtype, size_t length)
568 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
569 fprintf(stderr, " name = %s\n", name);
570 fprintf(stderr, "filename = %s\n", filename);
571 fprintf(stderr, "encoding = %s\n", encoding);
572 fprintf(stderr, " type = %s\n", cbtype);
573 fprintf(stderr, " length = %ld\n", (long)length);
575 if (strlen(name) > 0) {
576 WC->upload = malloc(length);
577 if (WC->upload != NULL) {
578 WC->upload_length = length;
579 memcpy(WC->upload, content, length);
586 * Entry point for WebCit transaction
588 void session_loop(struct httprequest *req)
594 int ContentLength = 0;
596 char ContentType[512];
598 struct httprequest *hptr;
599 char browser_host[256];
600 char user_agent[256];
602 /* We stuff these with the values coming from the client cookies,
603 * so we can use them to reconnect a timed out session if we have to.
607 char c_username[256];
608 char c_password[256];
609 char c_roomname[256];
612 strcpy(c_host, defaulthost);
613 strcpy(c_port, defaultport);
614 strcpy(c_username, "");
615 strcpy(c_password, "");
616 strcpy(c_roomname, "");
618 WC->upload_length = 0;
622 if (hptr == NULL) return;
624 strcpy(cmd, hptr->line);
626 extract_action(action, cmd);
628 while (hptr != NULL) {
629 strcpy(buf, hptr->line);
632 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
633 strcpy(cookie, &buf[15]);
634 cookie_to_stuff(cookie, NULL,
635 c_username, c_password, c_roomname);
637 else if (!strncasecmp(buf, "Content-length: ", 16)) {
638 ContentLength = atoi(&buf[16]);
640 else if (!strncasecmp(buf, "Content-type: ", 14)) {
641 strcpy(ContentType, &buf[14]);
643 else if (!strncasecmp(buf, "User-agent: ", 12)) {
644 strcpy(user_agent, &buf[12]);
648 if (ContentLength > 0) {
649 fprintf(stderr, "Content length: %d\n", ContentLength);
650 content = malloc(ContentLength + 1);
651 memset(content, 0, ContentLength+1);
654 while (BytesRead < ContentLength) {
655 a=read(WC->http_sock, &content[BytesRead],
656 ContentLength - BytesRead);
657 if (a <= 0) BytesRead = ContentLength;
661 if (!strncasecmp(ContentType,
662 "application/x-www-form-urlencoded", 33)) {
664 } else if (!strncasecmp(ContentType, "multipart", 9)) {
665 mime_parser(content, ContentLength, ContentType,
672 /* If there are variables in the URL, we must grab them now */
673 for (a = 0; a < strlen(cmd); ++a)
674 if ((cmd[a] == '?') || (cmd[a] == '&')) {
675 for (b = a; b < strlen(cmd); ++b)
678 addurls(&cmd[a + 1]);
682 * If we're not connected to a Citadel server, try to hook up the
683 * connection now. Preference is given to the host and port specified
684 * by browser cookies, if cookies have been supplied.
686 if (!WC->connected) {
687 if (strlen(bstr("host")) > 0)
688 strcpy(c_host, bstr("host"));
689 if (strlen(bstr("port")) > 0)
690 strcpy(c_port, bstr("port"));
692 if (!strcasecmp(c_host, "uds")) {
693 /* unix domain socket */
694 sprintf(buf, "%s/citadel.socket", c_port);
695 WC->serv_sock = uds_connectsock(buf);
699 WC->serv_sock = tcp_connectsock(c_host, c_port);
702 if (WC->serv_sock < 0) {
707 serv_gets(buf); /* get the server welcome message */
708 locate_host(browser_host, WC->http_sock);
709 get_serv_info(browser_host, user_agent);
712 check_for_express_messages();
715 * If we're not logged in, but we have username and password cookies
716 * supplied by the browser, try using them to log in.
718 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
719 serv_printf("USER %s", c_username);
722 serv_printf("PASS %s", c_password);
725 become_logged_in(c_username, c_password, buf);
730 * If we don't have a current room, but a cookie specifying the
731 * current room is supplied, make an effort to go there.
733 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
734 serv_printf("GOTO %s", c_roomname);
737 strcpy(WC->wc_roomname, c_roomname);
740 if (!strcasecmp(action, "static")) {
741 strcpy(buf, &cmd[12]);
742 for (a = 0; a < strlen(buf); ++a)
746 } else if (!strcasecmp(action, "image")) {
748 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
750 } else if (!WC->logged_in) {
753 /* Various commands... */
755 else if (!strcasecmp(action, "do_welcome")) {
757 } else if (!strcasecmp(action, "blank")) {
759 } else if (!strcasecmp(action, "display_main_menu")) {
761 } else if (!strcasecmp(action, "whobbs")) {
763 } else if (!strcasecmp(action, "knrooms")) {
764 list_all_rooms_by_floor();
765 } else if (!strcasecmp(action, "gotonext")) {
768 } else if (!strcasecmp(action, "skip")) {
770 } else if (!strcasecmp(action, "ungoto")) {
772 } else if (!strcasecmp(action, "dotgoto")) {
774 smart_goto(bstr("room"));
775 } else if (!strcasecmp(action, "termquit")) {
777 } else if (!strcasecmp(action, "readnew")) {
779 } else if (!strcasecmp(action, "readold")) {
781 } else if (!strcasecmp(action, "readfwd")) {
783 } else if (!strcasecmp(action, "headers")) {
785 } else if (!strcasecmp(action, "display_enter")) {
787 } else if (!strcasecmp(action, "post")) {
789 } else if (!strcasecmp(action, "delete_msg")) {
791 } else if (!strcasecmp(action, "confirm_move_msg")) {
793 } else if (!strcasecmp(action, "move_msg")) {
795 } else if (!strcasecmp(action, "userlist")) {
797 } else if (!strcasecmp(action, "showuser")) {
799 } else if (!strcasecmp(action, "display_page")) {
801 } else if (!strcasecmp(action, "page_user")) {
803 } else if (!strcasecmp(action, "chat")) {
805 } else if (!strcasecmp(action, "display_private")) {
806 display_private("", 0);
807 } else if (!strcasecmp(action, "goto_private")) {
809 } else if (!strcasecmp(action, "zapped_list")) {
811 } else if (!strcasecmp(action, "display_zap")) {
813 } else if (!strcasecmp(action, "zap")) {
815 } else if (!strcasecmp(action, "display_entroom")) {
817 } else if (!strcasecmp(action, "entroom")) {
819 } else if (!strcasecmp(action, "display_editroom")) {
821 } else if (!strcasecmp(action, "editroom")) {
823 } else if (!strcasecmp(action, "display_whok")) {
825 } else if (!strcasecmp(action, "display_editinfo")) {
826 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
827 } else if (!strcasecmp(action, "editinfo")) {
828 save_edit("Room info", "EINF 1", 1);
829 } else if (!strcasecmp(action, "display_editbio")) {
830 sprintf(buf, "RBIO %s", WC->wc_username);
831 display_edit("Your bio", "NOOP", buf, "editbio");
832 } else if (!strcasecmp(action, "editbio")) {
833 save_edit("Your bio", "EBIO", 0);
834 } else if (!strcasecmp(action, "confirm_delete_room")) {
835 confirm_delete_room();
836 } else if (!strcasecmp(action, "delete_room")) {
838 } else if (!strcasecmp(action, "validate")) {
840 } else if (!strcasecmp(action, "display_editpic")) {
841 display_graphics_upload("your photo",
844 } else if (!strcasecmp(action, "editpic")) {
845 do_graphics_upload("UIMG 1|_userpic_");
846 } else if (!strcasecmp(action, "display_editroompic")) {
847 display_graphics_upload("the graphic for this room",
850 } else if (!strcasecmp(action, "editroompic")) {
851 do_graphics_upload("UIMG 1|_roompic_");
852 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
853 select_floor_to_edit_pic();
854 } else if (!strcasecmp(action, "display_editfloorpic")) {
855 sprintf(buf, "UIMG 0|_floorpic_|%s",
856 bstr("which_floor"));
857 display_graphics_upload("the graphic for this floor",
860 } else if (!strcasecmp(action, "editfloorpic")) {
861 sprintf(buf, "UIMG 1|_floorpic_|%s",
862 bstr("which_floor"));
863 do_graphics_upload(buf);
864 } else if (!strcasecmp(action, "display_reg")) {
866 } else if (!strcasecmp(action, "register")) {
868 } else if (!strcasecmp(action, "display_changepw")) {
870 } else if (!strcasecmp(action, "changepw")) {
872 } else if (!strcasecmp(action, "display_edit_node")) {
874 } else if (!strcasecmp(action, "display_netconf")) {
876 } else if (!strcasecmp(action, "display_confirm_unshare")) {
877 display_confirm_unshare();
878 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
879 display_confirm_delete_node();
880 } else if (!strcasecmp(action, "delete_node")) {
882 } else if (!strcasecmp(action, "unshare")) {
884 } else if (!strcasecmp(action, "display_add_node")) {
886 } else if (!strcasecmp(action, "add_node")) {
888 } else if (!strcasecmp(action, "display_share")) {
890 } else if (!strcasecmp(action, "share")) {
892 } else if (!strcasecmp(action, "terminate_session")) {
895 } else if (!strcasecmp(action, "edit_me")) {
897 } else if (!strcasecmp(action, "display_siteconfig")) {
898 display_siteconfig();
899 } else if (!strcasecmp(action, "page_popup")) {
901 } else if (!strcasecmp(action, "siteconfig")) {
903 } else if (!strcasecmp(action, "display_generic")) {
905 } else if (!strcasecmp(action, "do_generic")) {
907 } else if (!strcasecmp(action, "display_menubar")) {
909 } else if (!strcasecmp(action, "diagnostics")) {
912 wprintf("You're in session %d<HR>\n", WC->wc_session);
913 wprintf("Command: <BR><PRE>\n");
915 wprintf("</PRE><HR>\n");
916 wprintf("Variables: <BR><PRE>\n");
918 wprintf("</PRE><HR>\n");
921 /* When all else fais, display the main menu. */
927 if (content != NULL) {
932 if (WC->upload_length > 0) {
934 WC->upload_length = 0;