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>
32 #include "mime_parser.h"
35 * String to unset the cookie.
36 * Any date "in the past" will work, so I chose my birthday, right down to
37 * the exact minute. :)
39 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
41 void unescape_input(char *buf)
46 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
47 buf[strlen(buf) - 1] = 0;
49 for (a = 0; a < strlen(buf); ++a) {
56 sscanf(hex, "%02x", &b);
58 strcpy(&buf[a + 1], &buf[a + 3]);
65 void addurls(char *url)
73 while (strlen(up) > 0) {
75 /* locate the = sign */
76 strncpy(buf, up, 255);
78 for (a = 255; a >= 0; --a)
85 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
86 u->next = WC->urlstrings;
88 strcpy(u->url_key, buf);
90 /* now chop that part off */
91 for (a = 0; a <= b; ++a)
94 /* locate the & sign */
97 for (a = 0; a < strlen(up); ++a) {
98 if (!strncmp(ptr, "&", 1)) {
105 for (a = 0; a < b; ++a)
109 u->url_data = malloc(strlen(up) + 1);
110 strcpy(u->url_data, up);
112 unescape_input(u->url_data);
120 struct urlcontent *u;
122 while (WC->urlstrings != NULL) {
123 free(WC->urlstrings->url_data);
124 u = WC->urlstrings->next;
125 free(WC->urlstrings);
131 * Diagnostic function to display the contents of all variables
135 struct urlcontent *u;
137 for (u = WC->urlstrings; u != NULL; u = u->next) {
138 wprintf("%38s = %s\n", u->url_key, u->url_data);
142 char *bstr(char *key)
144 struct urlcontent *u;
146 for (u = WC->urlstrings; u != NULL; u = u->next) {
147 if (!strcasecmp(u->url_key, key))
148 return (u->url_data);
154 void wprintf(const char *format,...)
159 va_start(arg_ptr, format);
160 vsprintf(wbuf, format, arg_ptr);
163 write(WC->http_sock, wbuf, strlen(wbuf));
168 * wDumpContent() wraps up an HTTP session, closes tags, etc.
170 * print_standard_html_footer should be set to 0 to transmit only, 1 to
171 * append the main menu and closing tags, or 2 to
172 * append the closing tags only.
174 void wDumpContent(int print_standard_html_footer)
176 if (WC->fake_frames) {
177 wprintf("</TABLE>\n");
181 if (print_standard_html_footer) {
182 if (print_standard_html_footer != 2) {
185 wprintf("</BODY></HTML>\n");
193 * Copy a string, escaping characters which have meaning in HTML. If
194 * nbsp is nonzero, spaces are converted to non-breaking spaces.
196 void stresc(char *target, char *strbuf, int nbsp)
201 for (a = 0; a < strlen(strbuf); ++a) {
202 if (strbuf[a] == '<')
203 strcat(target, "<");
204 else if (strbuf[a] == '>')
205 strcat(target, ">");
206 else if (strbuf[a] == '&')
207 strcat(target, "&");
208 else if (strbuf[a] == '\"')
209 strcat(target, """);
210 else if (strbuf[a] == '\'')
211 strcat(target, "'");
212 else if (strbuf[a] == LB)
214 else if (strbuf[a] == RB)
216 else if (strbuf[a] == QU)
217 strcat(target, "\"");
218 else if ((strbuf[a] == 32) && (nbsp == 1)) {
219 strcat(target, " ");
221 strncat(target, &strbuf[a], 1);
226 void escputs1(char *strbuf, int nbsp)
229 stresc(buf, strbuf, nbsp);
233 void escputs(char *strbuf)
239 * Escape a string for feeding out as a URL.
240 * FIXME this is not threadsafe!
242 char *urlesc(char *strbuf)
245 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
246 static char outbuf[1024];
250 for (a = 0; a < strlen(strbuf); ++a) {
252 for (b = 0; b < strlen(ec); ++b) {
253 if (strbuf[a] == ec[b])
258 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
260 sprintf(&outbuf[b], "%c", strbuf[a]);
265 void urlescputs(char *strbuf)
267 wprintf("%s", urlesc(strbuf));
274 * Output all that important stuff that the browser will want to see
279 * 0 = Nothing. Do not display any leading HTTP or HTML.
280 * 1 = HTTP headers plus the room banner
281 * 2 = HTTP headers required to terminate the session (unset cookies)
282 * 3 = HTTP and HTML headers, but no room banner
284 * Bit 2: Set to 1 to auto-refresh page every 30 seconds
286 * Bit 3: suppress check for express messages
288 void output_headers(int controlcode)
291 int print_standard_html_head = 0;
293 int suppress_check = 0;
295 static int pageseq = 0;
297 print_standard_html_head = controlcode & 0x03;
298 refresh30 = ((controlcode & 0x04) >> 2);
299 suppress_check = ((controlcode & 0x08) >> 3);
301 wprintf("HTTP/1.0 200 OK\n");
303 httpdate(httpnow, time(NULL));
305 if (print_standard_html_head > 0) {
306 wprintf("Content-type: text/html\n");
307 wprintf("Server: %s\n", SERVER);
308 wprintf("Connection: close\n");
309 wprintf("Pragma: no-cache\n");
310 wprintf("Cache-Control: no-store\n");
312 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
313 WC->wc_password, WC->wc_roomname);
314 if (print_standard_html_head == 2) {
315 wprintf("Set-cookie: webcit=%s\n", unset);
317 wprintf("Set-cookie: webcit=%s\n", cookie);
320 if (print_standard_html_head > 0) {
322 wprintf("<HTML><HEAD><TITLE>");
323 escputs(serv_info.serv_humannode);
325 "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n"
326 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
327 if (refresh30) wprintf(
328 "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
329 wprintf("</HEAD>\n");
331 /* script for checking for pages (not always launched) */
332 wprintf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
333 wprintf("function launch_page_popup() {\n");
334 wprintf("pwin = window.open('/page_popup', 'CitaPage%d', "
335 "'toolbar=no,location=no,copyhistory=no,status=no,"
336 "scrollbars=yes,resizable=no,height=150,width=400');\n",
339 wprintf("</SCRIPT>\n");
342 /* (JavaScript keyboard-based navigation would go here) */
344 if (!suppress_check) if (WC->HaveExpressMessages) {
345 svprintf("extrabodyparms", WCS_STRING, "%s",
346 "onload=\"launch_page_popup()\" ");
347 WC->HaveExpressMessages = 0;
349 do_template("background.html");
350 clear_local_substs();
352 if (print_standard_html_head == 1) {
353 wprintf("<A NAME=\"TheTop\"></A>");
355 wprintf("<TABLE border=0 width=100%%><TR VALIGN=TOP>"
358 embed_room_banner(NULL);
360 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
371 void http_redirect(char *whichpage) {
372 wprintf("HTTP/1.0 302 Moved Temporarily\n");
373 wprintf("Location: %s\n", whichpage);
374 wprintf("URI: %s\n", whichpage);
375 wprintf("Content-type: text/html\n\n");
376 wprintf("<html><body>\n");
377 wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
379 wprintf("</body></html>\n");
384 void check_for_express_messages()
390 if (buf[3] == '*') WC->HaveExpressMessages = 1;
396 void output_static(char *what)
404 sprintf(buf, "static/%s", what);
405 fp = fopen(buf, "rb");
407 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
408 wprintf("Content-Type: text/plain\n");
410 wprintf("Cannot open %s: %s\n", what, strerror(errno));
414 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
415 wprintf("Content-type: image/gif\n");
416 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
417 wprintf("Content-type: text/plain\n");
418 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
419 wprintf("Content-type: image/jpeg\n");
420 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
421 wprintf("Content-type: text/html\n");
423 wprintf("Content-type: application/octet-stream\n");
425 fstat(fileno(fp), &statbuf);
426 bytes = statbuf.st_size;
427 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
428 wprintf("Content-length: %ld\n", (long) bytes);
430 bigbuffer = malloc(bytes);
431 fread(bigbuffer, bytes, 1, fp);
433 write(WC->http_sock, bigbuffer, bytes);
436 if (!strcasecmp(bstr("force_close_session"), "yes")) {
437 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) {
469 serv_printf("READ %ld|%ld", accomplished, thisblock);
472 thisblock = extract_long(&buf[4], 0);
473 serv_read(xferbuf, (int) thisblock);
476 memset(xferbuf, 0, thisblock);
478 write(WC->http_sock, xferbuf, thisblock);
479 bytes = bytes - thisblock;
480 accomplished = accomplished + thisblock;
485 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
487 wprintf("Content-Type: text/plain\n");
489 wprintf("Error retrieving image: %s\n", &buf[4]);
496 void output_mimepart()
502 off_t accomplished = 0L;
503 char content_type[256];
505 serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
508 bytes = extract_long(&buf[4], 0);
509 extract(content_type, &buf[4], 3);
511 wprintf("Content-type: %s\n", content_type);
512 wprintf("Content-length: %ld\n", (long) bytes);
515 while (bytes > (off_t) 0) {
516 thisblock = (off_t) sizeof(xferbuf);
517 if (thisblock > bytes) {
520 serv_printf("READ %ld|%ld", accomplished, thisblock);
523 thisblock = extract_long(&buf[4], 0);
524 serv_read(xferbuf, (int) thisblock);
527 memset(xferbuf, 0, thisblock);
529 write(WC->http_sock, xferbuf, thisblock);
530 bytes = bytes - thisblock;
531 accomplished = accomplished + thisblock;
536 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
538 wprintf("Content-Type: text/plain\n");
540 wprintf("Error retrieving part: %s\n", &buf[4]);
547 * Convenience functions to display a page containing only a string
549 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
551 wprintf("HTTP/1.0 200 OK\n");
553 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
554 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
555 wprintf("<B>%s</B>\n", titlebarmsg);
556 wprintf("</FONT></TD></TR></TABLE><BR>\n");
557 escputs(messagetext);
565 * Display a blank page.
567 void blank_page(void) {
573 void display_error(char *errormessage)
575 convenience_page("770000", "Error", errormessage);
578 void display_success(char *successmessage)
580 convenience_page("007700", "OK", successmessage);
585 void extract_action(char *actbuf, char *cmdbuf)
589 strcpy(actbuf, cmdbuf);
590 if (!strncasecmp(actbuf, "GET /", 5))
591 strcpy(actbuf, &actbuf[5]);
592 if (!strncasecmp(actbuf, "PUT /", 5))
593 strcpy(actbuf, &actbuf[5]);
594 if (!strncasecmp(actbuf, "POST /", 6))
595 strcpy(actbuf, &actbuf[6]);
597 for (i = 0; i < strlen(actbuf); ++i) {
598 if (actbuf[i] == ' ') {
602 if (actbuf[i] == '/') {
606 if (actbuf[i] == '?') {
610 if (actbuf[i] == '&') {
618 void upload_handler(char *name, char *filename, char *partnum, char *disp,
619 void *content, char *cbtype, size_t length,
620 char *encoding, void *userdata)
623 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
624 fprintf(stderr, " name = %s\n", name);
625 fprintf(stderr, "filename = %s\n", filename);
626 fprintf(stderr, "encoding = %s\n", encoding);
627 fprintf(stderr, " type = %s\n", cbtype);
628 fprintf(stderr, " length = %ld\n", (long)length);
630 if ( (strlen(name) > 0) || (strlen(filename) > 0) ) {
631 WC->upload = malloc(length);
632 if (WC->upload != NULL) {
633 WC->upload_length = length;
634 memcpy(WC->upload, content, length);
641 * Entry point for WebCit transaction
643 void session_loop(struct httprequest *req)
649 int ContentLength = 0;
651 char ContentType[512];
654 struct httprequest *hptr;
655 char browser_host[256];
656 char user_agent[256];
658 /* We stuff these with the values coming from the client cookies,
659 * so we can use them to reconnect a timed out session if we have to.
663 char c_username[256];
664 char c_password[256];
665 char c_roomname[256];
668 strcpy(c_host, defaulthost);
669 strcpy(c_port, defaultport);
670 strcpy(c_username, "");
671 strcpy(c_password, "");
672 strcpy(c_roomname, "");
674 WC->upload_length = 0;
678 if (hptr == NULL) return;
680 strcpy(cmd, hptr->line);
682 extract_action(action, cmd);
684 while (hptr != NULL) {
685 strcpy(buf, hptr->line);
688 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
689 strcpy(cookie, &buf[15]);
690 cookie_to_stuff(cookie, NULL,
691 c_username, c_password, c_roomname);
693 else if (!strncasecmp(buf, "Content-length: ", 16)) {
694 ContentLength = atoi(&buf[16]);
696 else if (!strncasecmp(buf, "Content-type: ", 14)) {
697 strcpy(ContentType, &buf[14]);
699 else if (!strncasecmp(buf, "User-agent: ", 12)) {
700 strcpy(user_agent, &buf[12]);
704 if (ContentLength > 0) {
705 fprintf(stderr, "Content length: %d\n", ContentLength);
706 content = malloc(ContentLength + 1);
707 memset(content, 0, ContentLength+1);
710 while (BytesRead < ContentLength) {
711 a=read(WC->http_sock, &content[BytesRead],
712 ContentLength - BytesRead);
713 if (a <= 0) BytesRead = ContentLength;
717 if (!strncasecmp(ContentType,
718 "application/x-www-form-urlencoded", 33)) {
720 } else if (!strncasecmp(ContentType, "multipart", 9)) {
721 content_end = content + ContentLength;
722 mime_parser(content, content_end, *upload_handler,
723 NULL, NULL, NULL, 0);
729 /* If there are variables in the URL, we must grab them now */
730 for (a = 0; a < strlen(cmd); ++a)
731 if ((cmd[a] == '?') || (cmd[a] == '&')) {
732 for (b = a; b < strlen(cmd); ++b)
735 addurls(&cmd[a + 1]);
739 * If we're not connected to a Citadel server, try to hook up the
740 * connection now. Preference is given to the host and port specified
741 * by browser cookies, if cookies have been supplied.
743 if (!WC->connected) {
744 if (strlen(bstr("host")) > 0)
745 strcpy(c_host, bstr("host"));
746 if (strlen(bstr("port")) > 0)
747 strcpy(c_port, bstr("port"));
749 if (!strcasecmp(c_host, "uds")) {
750 /* unix domain socket */
751 sprintf(buf, "%s/citadel.socket", c_port);
752 WC->serv_sock = uds_connectsock(buf);
756 WC->serv_sock = tcp_connectsock(c_host, c_port);
759 if (WC->serv_sock < 0) {
761 goto SKIP_ALL_THIS_CRAP;
765 serv_gets(buf); /* get the server welcome message */
766 locate_host(browser_host, WC->http_sock);
767 get_serv_info(browser_host, user_agent);
771 check_for_express_messages();
774 * If we're not logged in, but we have username and password cookies
775 * supplied by the browser, try using them to log in.
777 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
778 serv_printf("USER %s", c_username);
781 serv_printf("PASS %s", c_password);
784 become_logged_in(c_username, c_password, buf);
789 * If we don't have a current room, but a cookie specifying the
790 * current room is supplied, make an effort to go there.
792 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
793 serv_printf("GOTO %s", c_roomname);
796 strcpy(WC->wc_roomname, c_roomname);
799 if (!strcasecmp(action, "static")) {
800 strcpy(buf, &cmd[12]);
801 for (a = 0; a < strlen(buf); ++a)
805 } else if (!strcasecmp(action, "image")) {
807 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
809 } else if (!WC->logged_in) {
812 /* Various commands... */
814 else if (!strcasecmp(action, "do_welcome")) {
816 } else if (!strcasecmp(action, "blank")) {
818 } else if (!strcasecmp(action, "display_main_menu")) {
820 } else if (!strcasecmp(action, "whobbs")) {
822 } else if (!strcasecmp(action, "knrooms")) {
823 list_all_rooms_by_floor();
824 } else if (!strcasecmp(action, "gotonext")) {
827 } else if (!strcasecmp(action, "skip")) {
829 } else if (!strcasecmp(action, "ungoto")) {
831 } else if (!strcasecmp(action, "dotgoto")) {
833 smart_goto(bstr("room"));
834 } else if (!strcasecmp(action, "dotskip")) {
835 smart_goto(bstr("room"));
836 } else if (!strcasecmp(action, "termquit")) {
838 } else if (!strcasecmp(action, "readnew")) {
840 } else if (!strcasecmp(action, "readold")) {
842 } else if (!strcasecmp(action, "readfwd")) {
844 } else if (!strcasecmp(action, "headers")) {
846 } else if (!strcasecmp(action, "display_enter")) {
848 } else if (!strcasecmp(action, "post")) {
850 } else if (!strcasecmp(action, "delete_msg")) {
852 } else if (!strcasecmp(action, "confirm_move_msg")) {
854 } else if (!strcasecmp(action, "move_msg")) {
856 } else if (!strcasecmp(action, "userlist")) {
858 } else if (!strcasecmp(action, "showuser")) {
860 } else if (!strcasecmp(action, "display_page")) {
862 } else if (!strcasecmp(action, "page_user")) {
864 } else if (!strcasecmp(action, "chat")) {
866 } else if (!strcasecmp(action, "display_private")) {
867 display_private("", 0);
868 } else if (!strcasecmp(action, "goto_private")) {
870 } else if (!strcasecmp(action, "zapped_list")) {
872 } else if (!strcasecmp(action, "display_zap")) {
874 } else if (!strcasecmp(action, "zap")) {
876 } else if (!strcasecmp(action, "display_entroom")) {
878 } else if (!strcasecmp(action, "entroom")) {
880 } else if (!strcasecmp(action, "display_editroom")) {
882 } else if (!strcasecmp(action, "editroom")) {
884 } else if (!strcasecmp(action, "display_whok")) {
886 } else if (!strcasecmp(action, "display_editinfo")) {
887 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
888 } else if (!strcasecmp(action, "editinfo")) {
889 save_edit("Room info", "EINF 1", 1);
890 } else if (!strcasecmp(action, "display_editbio")) {
891 sprintf(buf, "RBIO %s", WC->wc_username);
892 display_edit("Your bio", "NOOP", buf, "editbio");
893 } else if (!strcasecmp(action, "editbio")) {
894 save_edit("Your bio", "EBIO", 0);
895 } else if (!strcasecmp(action, "confirm_delete_room")) {
896 confirm_delete_room();
897 } else if (!strcasecmp(action, "delete_room")) {
899 } else if (!strcasecmp(action, "validate")) {
901 } else if (!strcasecmp(action, "display_editpic")) {
902 display_graphics_upload("your photo",
905 } else if (!strcasecmp(action, "editpic")) {
906 do_graphics_upload("UIMG 1|_userpic_");
907 } else if (!strcasecmp(action, "display_editroompic")) {
908 display_graphics_upload("the graphic for this room",
911 } else if (!strcasecmp(action, "editroompic")) {
912 do_graphics_upload("UIMG 1|_roompic_");
913 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
914 select_floor_to_edit_pic();
915 } else if (!strcasecmp(action, "display_editfloorpic")) {
916 sprintf(buf, "UIMG 0|_floorpic_|%s",
917 bstr("which_floor"));
918 display_graphics_upload("the graphic for this floor",
921 } else if (!strcasecmp(action, "editfloorpic")) {
922 sprintf(buf, "UIMG 1|_floorpic_|%s",
923 bstr("which_floor"));
924 do_graphics_upload(buf);
925 } else if (!strcasecmp(action, "display_reg")) {
927 } else if (!strcasecmp(action, "register")) {
929 } else if (!strcasecmp(action, "display_changepw")) {
931 } else if (!strcasecmp(action, "changepw")) {
933 } else if (!strcasecmp(action, "display_edit_node")) {
935 } else if (!strcasecmp(action, "display_netconf")) {
937 } else if (!strcasecmp(action, "display_confirm_unshare")) {
938 display_confirm_unshare();
939 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
940 display_confirm_delete_node();
941 } else if (!strcasecmp(action, "delete_node")) {
943 } else if (!strcasecmp(action, "unshare")) {
945 } else if (!strcasecmp(action, "display_add_node")) {
947 } else if (!strcasecmp(action, "add_node")) {
949 } else if (!strcasecmp(action, "display_share")) {
951 } else if (!strcasecmp(action, "share")) {
953 } else if (!strcasecmp(action, "terminate_session")) {
956 } else if (!strcasecmp(action, "edit_me")) {
958 } else if (!strcasecmp(action, "display_siteconfig")) {
959 display_siteconfig();
960 } else if (!strcasecmp(action, "page_popup")) {
962 } else if (!strcasecmp(action, "siteconfig")) {
964 } else if (!strcasecmp(action, "display_generic")) {
966 } else if (!strcasecmp(action, "do_generic")) {
968 } else if (!strcasecmp(action, "display_menubar")) {
970 } else if (!strcasecmp(action, "output_mimepart")) {
972 } else if (!strcasecmp(action, "diagnostics")) {
975 wprintf("You're in session %d<HR>\n", WC->wc_session);
976 wprintf("Command: <BR><PRE>\n");
978 wprintf("</PRE><HR>\n");
979 wprintf("Variables: <BR><PRE>\n");
981 wprintf("</PRE><HR>\n");
984 /* When all else fais, display the main menu. */
991 if (content != NULL) {
996 if (WC->upload_length > 0) {
998 WC->upload_length = 0;