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 void unescape_input(char *buf)
39 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
40 buf[strlen(buf) - 1] = 0;
42 for (a = 0; a < strlen(buf); ++a) {
49 sscanf(hex, "%02x", &b);
51 strcpy(&buf[a + 1], &buf[a + 3]);
58 void addurls(char *url)
66 while (strlen(up) > 0) {
68 /* locate the = sign */
69 strncpy(buf, up, 255);
71 for (a = 255; a >= 0; --a)
78 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
79 u->next = WC->urlstrings;
81 strcpy(u->url_key, buf);
83 /* now chop that part off */
84 for (a = 0; a <= b; ++a)
87 /* locate the & sign */
90 for (a = 0; a < strlen(up); ++a) {
91 if (!strncmp(ptr, "&", 1)) {
98 for (a = 0; a < b; ++a)
102 u->url_data = malloc(strlen(up) + 1);
103 strcpy(u->url_data, up);
105 unescape_input(u->url_data);
113 struct urlcontent *u;
115 while (WC->urlstrings != NULL) {
116 free(WC->urlstrings->url_data);
117 u = WC->urlstrings->next;
118 free(WC->urlstrings);
124 * Diagnostic function to display the contents of all variables
128 struct urlcontent *u;
130 for (u = WC->urlstrings; u != NULL; u = u->next) {
131 wprintf("%38s = %s\n", u->url_key, u->url_data);
135 char *bstr(char *key)
137 struct urlcontent *u;
139 for (u = WC->urlstrings; u != NULL; u = u->next) {
140 if (!strcasecmp(u->url_key, key))
141 return (u->url_data);
147 void wprintf(const char *format,...)
152 va_start(arg_ptr, format);
153 vsprintf(wbuf, format, arg_ptr);
156 write(WC->http_sock, wbuf, strlen(wbuf));
161 * wDumpContent() wraps up an HTTP session, closes tags, etc.
163 * print_standard_html_footer should be set to 0 to transmit only, 1 to
164 * append the main menu and closing tags, or 2 to
165 * append the closing tags only.
167 void wDumpContent(int print_standard_html_footer)
169 if (WC->fake_frames) {
170 wprintf("<CENTER><FONT SIZE=-1>"
171 "<TABLE border=0 width=100%><TR>"
172 "<TD><A HREF=\"/ungoto\">"
173 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
175 wprintf("<TD><A HREF=\"#TheTop\">"
176 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
177 "Top of page</A></TD>");
178 wprintf("<TD><A HREF=\"/display_enter\">"
179 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
180 "Enter a message</A></TD>");
181 wprintf("<TD><A HREF=\"/gotonext\">"
183 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
186 "</TD></TR></TABLE></TABLE>\n");
190 if (print_standard_html_footer) {
191 if (print_standard_html_footer != 2) {
194 wprintf("</BODY></HTML>\n");
201 void escputs1(char *strbuf, int nbsp)
205 for (a = 0; a < strlen(strbuf); ++a) {
206 if (strbuf[a] == '<')
208 else if (strbuf[a] == '>')
210 else if (strbuf[a] == '&')
212 else if (strbuf[a] == '\"')
214 else if (strbuf[a] == '\'')
216 else if (strbuf[a] == LB)
218 else if (strbuf[a] == RB)
220 else if (strbuf[a] == QU)
222 else if ((strbuf[a] == 32) && (nbsp == 1)) {
225 wprintf("%c", strbuf[a]);
230 void escputs(char *strbuf)
237 char *urlesc(char *strbuf)
240 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
241 static char outbuf[512];
245 for (a = 0; a < strlen(strbuf); ++a) {
247 for (b = 0; b < strlen(ec); ++b) {
248 if (strbuf[a] == ec[b])
253 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
255 sprintf(&outbuf[b], "%c", strbuf[a]);
260 void urlescputs(char *strbuf)
262 wprintf("%s", urlesc(strbuf));
269 * Output all that important stuff that the browser will want to see
274 * 0 = Nothing. Do not display any leading HTTP or HTML.
275 * 1 = HTTP headers plus the "fake frames" found in most windows.
276 * 2 = HTTP headers required to terminate the session (unset cookies)
277 * 3 = HTTP and HTML headers, but no 'fake frames'
279 * Bit 2: Set to 1 to auto-refresh page every 30 seconds
281 void output_headers(int controlcode)
283 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
285 int print_standard_html_head = 0;
288 print_standard_html_head = controlcode & 0x03;
289 refresh30 = ((controlcode & 0x04) >> 2);
291 wprintf("HTTP/1.0 200 OK\n");
293 if (print_standard_html_head > 0) {
294 wprintf("Content-type: text/html\n");
295 wprintf("Server: %s\n", SERVER);
296 wprintf("Connection: close\n");
297 wprintf("Pragma: no-cache\n");
298 wprintf("Cache-Control: no-store\n");
300 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
301 WC->wc_password, WC->wc_roomname);
302 if (print_standard_html_head == 2) {
303 wprintf("X-WebCit-Session: close\n");
304 wprintf("Set-cookie: webcit=%s\n", unset);
306 wprintf("Set-cookie: webcit=%s\n", cookie);
309 if (print_standard_html_head > 0) {
311 wprintf("<HTML><HEAD><TITLE>");
312 escputs(serv_info.serv_humannode);
314 "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n"
315 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
316 if (refresh30) wprintf(
317 "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
318 wprintf("</HEAD>\n");
319 if (WC->ExpressMessages != NULL) {
320 wprintf("<SCRIPT language=\"javascript\">\n");
321 wprintf("function ExpressMessage() {\n");
322 wprintf(" alert(\"");
323 escputs(WC->ExpressMessages);
325 wprintf(" }\n </SCRIPT>\n");
330 /* JavaScript key-based navigation would go here if it
335 if (WC->ExpressMessages != NULL) {
336 wprintf("onload=\"ExpressMessage()\" ");
337 free(WC->ExpressMessages);
338 WC->ExpressMessages = NULL;
340 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
343 if (print_standard_html_head == 1) {
344 wprintf("<A NAME=\"TheTop\"></A>"
345 "<TABLE border=0 width=100%>"
346 "<TR VALIGN=TOP><TD VALIGN=LEFT CELLPADDING=0>");
350 wprintf("</TD><TD VALIGN=TOP>"
351 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
354 embed_room_banner(NULL);
356 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
365 void ExpressMessageCat(char *buf) {
366 if (WC->ExpressMessages == NULL) {
367 WC->ExpressMessages = malloc(strlen(buf) + 4);
368 strcpy(WC->ExpressMessages, "");
370 WC->ExpressMessages = realloc(WC->ExpressMessages,
371 (strlen(WC->ExpressMessages) + strlen(buf) + 4));
373 strcat(WC->ExpressMessages, buf);
374 strcat(WC->ExpressMessages, "\\n");
378 void check_for_express_messages()
385 while (serv_gets(buf), strcmp(buf, "000")) {
386 ExpressMessageCat(buf);
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;
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)
465 serv_printf("READ %ld|%ld", accomplished, thisblock);
468 thisblock = extract_long(&buf[4], 0);
469 serv_read(xferbuf, (int) thisblock);
470 write(WC->http_sock, xferbuf, thisblock);
471 bytes = bytes - thisblock;
472 accomplished = accomplished + thisblock;
477 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
479 wprintf("Content-Type: text/plain\n");
481 wprintf("Error retrieving image\n");
488 * Convenience functions to display a page containing only a string
490 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
492 wprintf("HTTP/1.0 200 OK\n");
494 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
495 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
496 wprintf("<B>%s</B>\n", titlebarmsg);
497 wprintf("</FONT></TD></TR></TABLE><BR>\n");
498 escputs(messagetext);
505 void display_error(char *errormessage)
507 convenience_page("770000", "Error", errormessage);
510 void display_success(char *successmessage)
512 convenience_page("007700", "OK", successmessage);
517 void extract_action(char *actbuf, char *cmdbuf)
521 strcpy(actbuf, cmdbuf);
522 if (!strncasecmp(actbuf, "GET /", 5))
523 strcpy(actbuf, &actbuf[5]);
524 if (!strncasecmp(actbuf, "PUT /", 5))
525 strcpy(actbuf, &actbuf[5]);
526 if (!strncasecmp(actbuf, "POST /", 6))
527 strcpy(actbuf, &actbuf[6]);
529 for (i = 0; i < strlen(actbuf); ++i) {
530 if (actbuf[i] == ' ') {
534 if (actbuf[i] == '/') {
538 if (actbuf[i] == '?') {
542 if (actbuf[i] == '&') {
550 void upload_handler(char *name, char *filename, char *encoding,
551 void *content, char *cbtype, size_t length)
554 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
555 fprintf(stderr, " name = %s\n", name);
556 fprintf(stderr, "filename = %s\n", filename);
557 fprintf(stderr, "encoding = %s\n", encoding);
558 fprintf(stderr, " type = %s\n", cbtype);
559 fprintf(stderr, " length = %d\n", length);
561 if (strlen(name) > 0) {
562 WC->upload = malloc(length);
563 if (WC->upload != NULL) {
564 WC->upload_length = length;
565 memcpy(WC->upload, content, length);
572 * Entry point for WebCit transaction
574 void session_loop(struct httprequest *req)
580 int ContentLength = 0;
582 char ContentType[512];
584 struct httprequest *hptr;
585 char browser_host[256];
586 char user_agent[256];
588 /* We stuff these with the values coming from the client cookies,
589 * so we can use them to reconnect a timed out session if we have to.
593 char c_username[256];
594 char c_password[256];
595 char c_roomname[256];
598 strcpy(c_host, defaulthost);
599 strcpy(c_port, defaultport);
600 strcpy(c_username, "");
601 strcpy(c_password, "");
602 strcpy(c_roomname, "");
604 WC->upload_length = 0;
608 if (hptr == NULL) return;
610 strcpy(cmd, hptr->line);
612 extract_action(action, cmd);
614 while (hptr != NULL) {
615 strcpy(buf, hptr->line);
618 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
619 strcpy(cookie, &buf[15]);
620 cookie_to_stuff(cookie, NULL,
621 c_username, c_password, c_roomname);
623 else if (!strncasecmp(buf, "Content-length: ", 16)) {
624 ContentLength = atoi(&buf[16]);
626 else if (!strncasecmp(buf, "Content-type: ", 14)) {
627 strcpy(ContentType, &buf[14]);
629 else if (!strncasecmp(buf, "User-agent: ", 12)) {
630 strcpy(user_agent, &buf[12]);
634 if (ContentLength > 0) {
635 fprintf(stderr, "Content length: %d\n", ContentLength);
636 content = malloc(ContentLength + 1);
637 memset(content, 0, ContentLength+1);
640 while (BytesRead < ContentLength) {
641 a=read(WC->http_sock, &content[BytesRead],
642 ContentLength - BytesRead);
643 if (a <= 0) BytesRead = ContentLength;
647 if (!strncasecmp(ContentType,
648 "application/x-www-form-urlencoded", 33)) {
650 } else if (!strncasecmp(ContentType, "multipart", 9)) {
651 mime_parser(content, ContentLength, ContentType,
658 /* If there are variables in the URL, we must grab them now */
659 for (a = 0; a < strlen(cmd); ++a)
660 if ((cmd[a] == '?') || (cmd[a] == '&')) {
661 for (b = a; b < strlen(cmd); ++b)
664 addurls(&cmd[a + 1]);
668 * If we're not connected to a Citadel server, try to hook up the
669 * connection now. Preference is given to the host and port specified
670 * by browser cookies, if cookies have been supplied.
672 if (!WC->connected) {
673 if (strlen(bstr("host")) > 0)
674 strcpy(c_host, bstr("host"));
675 if (strlen(bstr("port")) > 0)
676 strcpy(c_port, bstr("port"));
678 if (!strcasecmp(c_host, "uds")) {
679 /* unix domain socket */
680 sprintf(buf, "%s/citadel.socket", c_port);
681 WC->serv_sock = uds_connectsock(buf);
685 WC->serv_sock = tcp_connectsock(c_host, c_port);
688 if (WC->serv_sock < 0) {
693 serv_gets(buf); /* get the server welcome message */
694 locate_host(browser_host, WC->http_sock);
695 get_serv_info(browser_host, user_agent);
697 check_for_express_messages();
700 * If we're not logged in, but we have username and password cookies
701 * supplied by the browser, try using them to log in.
703 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
704 serv_printf("USER %s", c_username);
707 serv_printf("PASS %s", c_password);
710 become_logged_in(c_username, c_password, buf);
715 * If we don't have a current room, but a cookie specifying the
716 * current room is supplied, make an effort to go there.
718 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
719 serv_printf("GOTO %s", c_roomname);
722 strcpy(WC->wc_roomname, c_roomname);
725 if (!strcasecmp(action, "static")) {
726 strcpy(buf, &cmd[12]);
727 for (a = 0; a < strlen(buf); ++a)
731 } else if (!strcasecmp(action, "image")) {
733 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
735 } else if (!WC->logged_in) {
738 /* Various commands... */
740 else if (!strcasecmp(action, "do_welcome")) {
742 } else if (!strcasecmp(action, "display_main_menu")) {
744 } else if (!strcasecmp(action, "advanced")) {
745 display_advanced_menu();
746 } else if (!strcasecmp(action, "whobbs")) {
748 } else if (!strcasecmp(action, "knrooms")) {
749 list_all_rooms_by_floor();
750 } else if (!strcasecmp(action, "gotonext")) {
753 } else if (!strcasecmp(action, "skip")) {
755 } else if (!strcasecmp(action, "ungoto")) {
757 } else if (!strcasecmp(action, "dotgoto")) {
759 smart_goto(bstr("room"));
760 } else if (!strcasecmp(action, "termquit")) {
762 } else if (!strcasecmp(action, "readnew")) {
764 } else if (!strcasecmp(action, "readold")) {
766 } else if (!strcasecmp(action, "readfwd")) {
768 } else if (!strcasecmp(action, "display_enter")) {
770 } else if (!strcasecmp(action, "post")) {
772 } else if (!strcasecmp(action, "confirm_delete_msg")) {
773 confirm_delete_msg();
774 } else if (!strcasecmp(action, "delete_msg")) {
776 } else if (!strcasecmp(action, "confirm_move_msg")) {
778 } else if (!strcasecmp(action, "move_msg")) {
780 } else if (!strcasecmp(action, "userlist")) {
782 } else if (!strcasecmp(action, "showuser")) {
784 } else if (!strcasecmp(action, "display_page")) {
786 } else if (!strcasecmp(action, "page_user")) {
788 } else if (!strcasecmp(action, "chat")) {
790 } else if (!strcasecmp(action, "display_private")) {
791 display_private("", 0);
792 } else if (!strcasecmp(action, "goto_private")) {
794 } else if (!strcasecmp(action, "zapped_list")) {
796 } else if (!strcasecmp(action, "display_zap")) {
798 } else if (!strcasecmp(action, "zap")) {
800 } else if (!strcasecmp(action, "display_entroom")) {
802 } else if (!strcasecmp(action, "entroom")) {
804 } else if (!strcasecmp(action, "display_editroom")) {
806 } else if (!strcasecmp(action, "editroom")) {
808 } else if (!strcasecmp(action, "display_editinfo")) {
809 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
810 } else if (!strcasecmp(action, "editinfo")) {
811 save_edit("Room info", "EINF 1", 1);
812 } else if (!strcasecmp(action, "display_editbio")) {
813 sprintf(buf, "RBIO %s", WC->wc_username);
814 display_edit("Your bio", "NOOP", buf, "editbio");
815 } else if (!strcasecmp(action, "editbio")) {
816 save_edit("Your bio", "EBIO", 0);
817 } else if (!strcasecmp(action, "confirm_delete_room")) {
818 confirm_delete_room();
819 } else if (!strcasecmp(action, "delete_room")) {
821 } else if (!strcasecmp(action, "validate")) {
823 } else if (!strcasecmp(action, "display_editpic")) {
824 display_graphics_upload("your photo",
827 } else if (!strcasecmp(action, "editpic")) {
828 do_graphics_upload("UIMG 1|_userpic_");
829 } else if (!strcasecmp(action, "display_editroompic")) {
830 display_graphics_upload("the graphic for this room",
833 } else if (!strcasecmp(action, "editroompic")) {
834 do_graphics_upload("UIMG 1|_roompic_");
835 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
836 select_floor_to_edit_pic();
837 } else if (!strcasecmp(action, "display_editfloorpic")) {
838 sprintf(buf, "UIMG 0|_floorpic_|%s",
839 bstr("which_floor"));
840 display_graphics_upload("the graphic for this floor",
843 } else if (!strcasecmp(action, "editfloorpic")) {
844 sprintf(buf, "UIMG 1|_floorpic_|%s",
845 bstr("which_floor"));
846 do_graphics_upload(buf);
847 } else if (!strcasecmp(action, "display_reg")) {
849 } else if (!strcasecmp(action, "register")) {
851 } else if (!strcasecmp(action, "display_changepw")) {
853 } else if (!strcasecmp(action, "changepw")) {
855 } else if (!strcasecmp(action, "display_edit_node")) {
857 } else if (!strcasecmp(action, "display_netconf")) {
859 } else if (!strcasecmp(action, "display_confirm_unshare")) {
860 display_confirm_unshare();
861 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
862 display_confirm_delete_node();
863 } else if (!strcasecmp(action, "delete_node")) {
865 } else if (!strcasecmp(action, "unshare")) {
867 } else if (!strcasecmp(action, "display_add_node")) {
869 } else if (!strcasecmp(action, "add_node")) {
871 } else if (!strcasecmp(action, "display_share")) {
873 } else if (!strcasecmp(action, "share")) {
875 } else if (!strcasecmp(action, "terminate_session")) {
878 } else if (!strcasecmp(action, "edit_me")) {
880 } else if (!strcasecmp(action, "display_siteconfig")) {
881 display_siteconfig();
882 } else if (!strcasecmp(action, "siteconfig")) {
884 } else if (!strcasecmp(action, "display_generic")) {
886 } else if (!strcasecmp(action, "do_generic")) {
888 } else if (!strcasecmp(action, "display_menubar")) {
890 } else if (!strcasecmp(action, "diagnostics")) {
893 wprintf("You're in session %d<HR>\n", WC->wc_session);
894 wprintf("Command: <BR><PRE>\n");
896 wprintf("</PRE><HR>\n");
897 wprintf("Variables: <BR><PRE>\n");
899 wprintf("</PRE><HR>\n");
902 /* When all else fais, display the main menu. */
908 if (content != NULL) {
913 if (WC->upload_length > 0) {
915 WC->upload_length = 0;