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("<CENTER><FONT SIZE=-1>"
177 "<TABLE border=0 width=100%%><TR>"
178 "<TD><A HREF=\"/ungoto\">"
179 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
181 wprintf("<TD><A HREF=\"#TheTop\">"
182 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
183 "Top of page</A></TD>");
184 wprintf("<TD><A HREF=\"/display_enter\">"
185 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
186 "Enter a message</A></TD>");
187 wprintf("<TD><A HREF=\"/gotonext\">"
189 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
192 "</TD></TR></TABLE></TABLE>\n");
196 if (print_standard_html_footer) {
197 if (print_standard_html_footer != 2) {
200 wprintf("</BODY></HTML>\n");
208 * Copy a string, escaping characters which have meaning in HTML. If
209 * nbsp is nonzero, spaces are converted to non-breaking spaces.
211 void stresc(char *target, char *strbuf, int nbsp)
216 for (a = 0; a < strlen(strbuf); ++a) {
217 if (strbuf[a] == '<')
218 strcat(target, "<");
219 else if (strbuf[a] == '>')
220 strcat(target, ">");
221 else if (strbuf[a] == '&')
222 strcat(target, "&");
223 else if (strbuf[a] == '\"')
224 strcat(target, """);
225 else if (strbuf[a] == '\'')
226 strcat(target, "'");
227 else if (strbuf[a] == LB)
229 else if (strbuf[a] == RB)
231 else if (strbuf[a] == QU)
232 strcat(target, "\"");
233 else if ((strbuf[a] == 32) && (nbsp == 1)) {
234 strcat(target, " ");
236 strncat(target, &strbuf[a], 1);
241 void escputs1(char *strbuf, int nbsp)
244 stresc(buf, strbuf, nbsp);
248 void escputs(char *strbuf)
254 * Escape a string for feeding out as a URL.
255 * FIXME this is not threadsafe!
257 char *urlesc(char *strbuf)
260 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
261 static char outbuf[1024];
265 for (a = 0; a < strlen(strbuf); ++a) {
267 for (b = 0; b < strlen(ec); ++b) {
268 if (strbuf[a] == ec[b])
273 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
275 sprintf(&outbuf[b], "%c", strbuf[a]);
280 void urlescputs(char *strbuf)
282 wprintf("%s", urlesc(strbuf));
289 * Output all that important stuff that the browser will want to see
294 * 0 = Nothing. Do not display any leading HTTP or HTML.
295 * 1 = HTTP headers plus the "fake frames" found in most windows.
296 * 2 = HTTP headers required to terminate the session (unset cookies)
297 * 3 = HTTP and HTML headers, but no 'fake frames'
299 * Bit 2: Set to 1 to auto-refresh page every 30 seconds
301 * Bit 3: suppress check for express messages
303 void output_headers(int controlcode)
306 int print_standard_html_head = 0;
308 int suppress_check = 0;
310 static int pageseq = 0;
312 print_standard_html_head = controlcode & 0x03;
313 refresh30 = ((controlcode & 0x04) >> 2);
314 suppress_check = ((controlcode & 0x08) >> 3);
316 wprintf("HTTP/1.0 200 OK\n");
318 httpdate(httpnow, time(NULL));
320 if (print_standard_html_head > 0) {
321 wprintf("Content-type: text/html\n");
322 wprintf("Server: %s\n", SERVER);
323 wprintf("Connection: close\n");
324 wprintf("Pragma: no-cache\n");
325 wprintf("Cache-Control: no-store\n");
327 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
328 WC->wc_password, WC->wc_roomname);
329 if (print_standard_html_head == 2) {
330 wprintf("Set-cookie: webcit=%s\n", unset);
332 wprintf("Set-cookie: webcit=%s\n", cookie);
335 if (print_standard_html_head > 0) {
337 wprintf("<HTML><HEAD><TITLE>");
338 escputs(serv_info.serv_humannode);
340 "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n"
341 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
342 if (refresh30) wprintf(
343 "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
344 wprintf("</HEAD>\n");
346 /* script for checking for express msgs (not always launch) */
347 wprintf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
348 wprintf("function launch_page_popup() {\n");
349 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");
351 wprintf("</SCRIPT>\n", ++pageseq);
354 /* JavaScript keyboard-based navigation would go here if it
358 if (!suppress_check) if (WC->HaveExpressMessages) {
359 svprintf("extrabodyparms", WCS_STRING, "%s",
360 "onload=\"launch_page_popup()\" ");
361 WC->HaveExpressMessages = 0;
363 do_template("background.html");
364 clear_local_substs();
366 if (print_standard_html_head == 1) {
367 wprintf("<A NAME=\"TheTop\"></A>"
368 "<TABLE border=0 width=100%%>"
369 "<TR VALIGN=TOP><TD VALIGN=LEFT CELLPADDING=0>");
373 wprintf("</TD><TD VALIGN=TOP>"
374 "<TABLE border=0 width=100%%><TR VALIGN=TOP>"
377 embed_room_banner(NULL);
379 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
390 void http_redirect(char *whichpage) {
391 wprintf("HTTP/1.0 302 Moved Temporarily\n");
392 wprintf("Location: %s\n", whichpage);
393 wprintf("URI: %s\n", whichpage);
394 wprintf("Content-type: text/html\n\n");
395 wprintf("<html><body>\n");
396 wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
398 wprintf("</body></html>\n");
403 void check_for_express_messages()
409 if (buf[3] == '*') WC->HaveExpressMessages = 1;
415 void output_static(char *what)
423 sprintf(buf, "static/%s", what);
424 fp = fopen(buf, "rb");
426 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
427 wprintf("Content-Type: text/plain\n");
429 wprintf("Cannot open %s: %s\n", what, strerror(errno));
433 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
434 wprintf("Content-type: image/gif\n");
435 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
436 wprintf("Content-type: text/plain\n");
437 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
438 wprintf("Content-type: image/jpeg\n");
439 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
440 wprintf("Content-type: text/html\n");
442 wprintf("Content-type: application/octet-stream\n");
444 fstat(fileno(fp), &statbuf);
445 bytes = statbuf.st_size;
446 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
447 wprintf("Content-length: %ld\n", (long) bytes);
450 thisblock = sizeof(buf);
451 if (thisblock > bytes) thisblock = bytes;
452 fread(buf, thisblock, 1, fp);
453 write(WC->http_sock, buf, thisblock);
454 bytes = bytes - thisblock;
458 if (!strcasecmp(bstr("force_close_session"), "yes")) {
459 end_webcit_session();
464 * When the browser requests an image file from the Citadel server,
465 * this function is called to transmit it.
473 off_t accomplished = 0L;
476 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
479 bytes = extract_long(&buf[4], 0);
481 wprintf("Content-type: image/gif\n");
482 wprintf("Content-length: %ld\n", (long) bytes);
485 while (bytes > (off_t) 0) {
486 thisblock = (off_t) sizeof(xferbuf);
487 if (thisblock > bytes)
489 serv_printf("READ %ld|%ld", accomplished, thisblock);
492 thisblock = extract_long(&buf[4], 0);
493 serv_read(xferbuf, (int) thisblock);
494 write(WC->http_sock, xferbuf, thisblock);
495 bytes = bytes - thisblock;
496 accomplished = accomplished + thisblock;
501 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
503 wprintf("Content-Type: text/plain\n");
505 wprintf("Error retrieving image\n");
512 * Convenience functions to display a page containing only a string
514 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
516 wprintf("HTTP/1.0 200 OK\n");
518 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
519 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
520 wprintf("<B>%s</B>\n", titlebarmsg);
521 wprintf("</FONT></TD></TR></TABLE><BR>\n");
522 escputs(messagetext);
529 void display_error(char *errormessage)
531 convenience_page("770000", "Error", errormessage);
534 void display_success(char *successmessage)
536 convenience_page("007700", "OK", successmessage);
541 void extract_action(char *actbuf, char *cmdbuf)
545 strcpy(actbuf, cmdbuf);
546 if (!strncasecmp(actbuf, "GET /", 5))
547 strcpy(actbuf, &actbuf[5]);
548 if (!strncasecmp(actbuf, "PUT /", 5))
549 strcpy(actbuf, &actbuf[5]);
550 if (!strncasecmp(actbuf, "POST /", 6))
551 strcpy(actbuf, &actbuf[6]);
553 for (i = 0; i < strlen(actbuf); ++i) {
554 if (actbuf[i] == ' ') {
558 if (actbuf[i] == '/') {
562 if (actbuf[i] == '?') {
566 if (actbuf[i] == '&') {
574 void upload_handler(char *name, char *filename, char *encoding,
575 void *content, char *cbtype, size_t length)
578 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
579 fprintf(stderr, " name = %s\n", name);
580 fprintf(stderr, "filename = %s\n", filename);
581 fprintf(stderr, "encoding = %s\n", encoding);
582 fprintf(stderr, " type = %s\n", cbtype);
583 fprintf(stderr, " length = %ld\n", (long)length);
585 if (strlen(name) > 0) {
586 WC->upload = malloc(length);
587 if (WC->upload != NULL) {
588 WC->upload_length = length;
589 memcpy(WC->upload, content, length);
596 * Entry point for WebCit transaction
598 void session_loop(struct httprequest *req)
604 int ContentLength = 0;
606 char ContentType[512];
608 struct httprequest *hptr;
609 char browser_host[256];
610 char user_agent[256];
612 /* We stuff these with the values coming from the client cookies,
613 * so we can use them to reconnect a timed out session if we have to.
617 char c_username[256];
618 char c_password[256];
619 char c_roomname[256];
622 strcpy(c_host, defaulthost);
623 strcpy(c_port, defaultport);
624 strcpy(c_username, "");
625 strcpy(c_password, "");
626 strcpy(c_roomname, "");
628 WC->upload_length = 0;
632 if (hptr == NULL) return;
634 strcpy(cmd, hptr->line);
636 extract_action(action, cmd);
638 while (hptr != NULL) {
639 strcpy(buf, hptr->line);
642 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
643 strcpy(cookie, &buf[15]);
644 cookie_to_stuff(cookie, NULL,
645 c_username, c_password, c_roomname);
647 else if (!strncasecmp(buf, "Content-length: ", 16)) {
648 ContentLength = atoi(&buf[16]);
650 else if (!strncasecmp(buf, "Content-type: ", 14)) {
651 strcpy(ContentType, &buf[14]);
653 else if (!strncasecmp(buf, "User-agent: ", 12)) {
654 strcpy(user_agent, &buf[12]);
658 if (ContentLength > 0) {
659 fprintf(stderr, "Content length: %d\n", ContentLength);
660 content = malloc(ContentLength + 1);
661 memset(content, 0, ContentLength+1);
664 while (BytesRead < ContentLength) {
665 a=read(WC->http_sock, &content[BytesRead],
666 ContentLength - BytesRead);
667 if (a <= 0) BytesRead = ContentLength;
671 if (!strncasecmp(ContentType,
672 "application/x-www-form-urlencoded", 33)) {
674 } else if (!strncasecmp(ContentType, "multipart", 9)) {
675 mime_parser(content, ContentLength, ContentType,
682 /* If there are variables in the URL, we must grab them now */
683 for (a = 0; a < strlen(cmd); ++a)
684 if ((cmd[a] == '?') || (cmd[a] == '&')) {
685 for (b = a; b < strlen(cmd); ++b)
688 addurls(&cmd[a + 1]);
692 * If we're not connected to a Citadel server, try to hook up the
693 * connection now. Preference is given to the host and port specified
694 * by browser cookies, if cookies have been supplied.
696 if (!WC->connected) {
697 if (strlen(bstr("host")) > 0)
698 strcpy(c_host, bstr("host"));
699 if (strlen(bstr("port")) > 0)
700 strcpy(c_port, bstr("port"));
702 if (!strcasecmp(c_host, "uds")) {
703 /* unix domain socket */
704 sprintf(buf, "%s/citadel.socket", c_port);
705 WC->serv_sock = uds_connectsock(buf);
709 WC->serv_sock = tcp_connectsock(c_host, c_port);
712 if (WC->serv_sock < 0) {
717 serv_gets(buf); /* get the server welcome message */
718 locate_host(browser_host, WC->http_sock);
719 get_serv_info(browser_host, user_agent);
722 check_for_express_messages();
725 * If we're not logged in, but we have username and password cookies
726 * supplied by the browser, try using them to log in.
728 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
729 serv_printf("USER %s", c_username);
732 serv_printf("PASS %s", c_password);
735 become_logged_in(c_username, c_password, buf);
740 * If we don't have a current room, but a cookie specifying the
741 * current room is supplied, make an effort to go there.
743 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
744 serv_printf("GOTO %s", c_roomname);
747 strcpy(WC->wc_roomname, c_roomname);
750 if (!strcasecmp(action, "static")) {
751 strcpy(buf, &cmd[12]);
752 for (a = 0; a < strlen(buf); ++a)
756 } else if (!strcasecmp(action, "image")) {
758 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
760 } else if (!WC->logged_in) {
763 /* Various commands... */
765 else if (!strcasecmp(action, "do_welcome")) {
767 } else if (!strcasecmp(action, "display_main_menu")) {
769 } else if (!strcasecmp(action, "advanced")) {
770 display_advanced_menu();
771 } else if (!strcasecmp(action, "whobbs")) {
773 } else if (!strcasecmp(action, "knrooms")) {
774 list_all_rooms_by_floor();
775 } else if (!strcasecmp(action, "gotonext")) {
778 } else if (!strcasecmp(action, "skip")) {
780 } else if (!strcasecmp(action, "ungoto")) {
782 } else if (!strcasecmp(action, "dotgoto")) {
784 smart_goto(bstr("room"));
785 } else if (!strcasecmp(action, "termquit")) {
787 } else if (!strcasecmp(action, "readnew")) {
789 } else if (!strcasecmp(action, "readold")) {
791 } else if (!strcasecmp(action, "readfwd")) {
793 } else if (!strcasecmp(action, "headers")) {
795 } else if (!strcasecmp(action, "display_enter")) {
797 } else if (!strcasecmp(action, "post")) {
799 } else if (!strcasecmp(action, "delete_msg")) {
801 } else if (!strcasecmp(action, "confirm_move_msg")) {
803 } else if (!strcasecmp(action, "move_msg")) {
805 } else if (!strcasecmp(action, "userlist")) {
807 } else if (!strcasecmp(action, "showuser")) {
809 } else if (!strcasecmp(action, "display_page")) {
811 } else if (!strcasecmp(action, "page_user")) {
813 } else if (!strcasecmp(action, "chat")) {
815 } else if (!strcasecmp(action, "display_private")) {
816 display_private("", 0);
817 } else if (!strcasecmp(action, "goto_private")) {
819 } else if (!strcasecmp(action, "zapped_list")) {
821 } else if (!strcasecmp(action, "display_zap")) {
823 } else if (!strcasecmp(action, "zap")) {
825 } else if (!strcasecmp(action, "display_entroom")) {
827 } else if (!strcasecmp(action, "entroom")) {
829 } else if (!strcasecmp(action, "display_editroom")) {
831 } else if (!strcasecmp(action, "editroom")) {
833 } else if (!strcasecmp(action, "display_whok")) {
835 } else if (!strcasecmp(action, "display_editinfo")) {
836 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
837 } else if (!strcasecmp(action, "editinfo")) {
838 save_edit("Room info", "EINF 1", 1);
839 } else if (!strcasecmp(action, "display_editbio")) {
840 sprintf(buf, "RBIO %s", WC->wc_username);
841 display_edit("Your bio", "NOOP", buf, "editbio");
842 } else if (!strcasecmp(action, "editbio")) {
843 save_edit("Your bio", "EBIO", 0);
844 } else if (!strcasecmp(action, "confirm_delete_room")) {
845 confirm_delete_room();
846 } else if (!strcasecmp(action, "delete_room")) {
848 } else if (!strcasecmp(action, "validate")) {
850 } else if (!strcasecmp(action, "display_editpic")) {
851 display_graphics_upload("your photo",
854 } else if (!strcasecmp(action, "editpic")) {
855 do_graphics_upload("UIMG 1|_userpic_");
856 } else if (!strcasecmp(action, "display_editroompic")) {
857 display_graphics_upload("the graphic for this room",
860 } else if (!strcasecmp(action, "editroompic")) {
861 do_graphics_upload("UIMG 1|_roompic_");
862 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
863 select_floor_to_edit_pic();
864 } else if (!strcasecmp(action, "display_editfloorpic")) {
865 sprintf(buf, "UIMG 0|_floorpic_|%s",
866 bstr("which_floor"));
867 display_graphics_upload("the graphic for this floor",
870 } else if (!strcasecmp(action, "editfloorpic")) {
871 sprintf(buf, "UIMG 1|_floorpic_|%s",
872 bstr("which_floor"));
873 do_graphics_upload(buf);
874 } else if (!strcasecmp(action, "display_reg")) {
876 } else if (!strcasecmp(action, "register")) {
878 } else if (!strcasecmp(action, "display_changepw")) {
880 } else if (!strcasecmp(action, "changepw")) {
882 } else if (!strcasecmp(action, "display_edit_node")) {
884 } else if (!strcasecmp(action, "display_netconf")) {
886 } else if (!strcasecmp(action, "display_confirm_unshare")) {
887 display_confirm_unshare();
888 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
889 display_confirm_delete_node();
890 } else if (!strcasecmp(action, "delete_node")) {
892 } else if (!strcasecmp(action, "unshare")) {
894 } else if (!strcasecmp(action, "display_add_node")) {
896 } else if (!strcasecmp(action, "add_node")) {
898 } else if (!strcasecmp(action, "display_share")) {
900 } else if (!strcasecmp(action, "share")) {
902 } else if (!strcasecmp(action, "terminate_session")) {
905 } else if (!strcasecmp(action, "edit_me")) {
907 } else if (!strcasecmp(action, "display_siteconfig")) {
908 display_siteconfig();
909 } else if (!strcasecmp(action, "page_popup")) {
911 } else if (!strcasecmp(action, "siteconfig")) {
913 } else if (!strcasecmp(action, "display_generic")) {
915 } else if (!strcasecmp(action, "do_generic")) {
917 } else if (!strcasecmp(action, "display_menubar")) {
919 } else if (!strcasecmp(action, "diagnostics")) {
922 wprintf("You're in session %d<HR>\n", WC->wc_session);
923 wprintf("Command: <BR><PRE>\n");
925 wprintf("</PRE><HR>\n");
926 wprintf("Variables: <BR><PRE>\n");
928 wprintf("</PRE><HR>\n");
931 /* When all else fais, display the main menu. */
937 if (content != NULL) {
942 if (WC->upload_length > 0) {
944 WC->upload_length = 0;