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>
35 int TransactionCount = 0;
36 char *ExpressMessages = NULL;
38 /* This variable is set to 1 if the room banner and menubar have been
39 * displayed, and we need to close the <TABLE> tags.
43 struct urlcontent *urlstrings = NULL;
45 static const char *defaulthost = DEFAULT_HOST;
46 static const char *defaultport = DEFAULT_PORT;
50 void unescape_input(char *buf)
55 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
56 buf[strlen(buf) - 1] = 0;
58 for (a = 0; a < strlen(buf); ++a) {
65 sscanf(hex, "%02x", &b);
67 strcpy(&buf[a + 1], &buf[a + 3]);
74 void addurls(char *url)
82 while (strlen(up) > 0) {
84 /* locate the = sign */
85 strncpy(buf, up, 255);
87 for (a = 255; a >= 0; --a)
94 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
97 strcpy(u->url_key, buf);
99 /* now chop that part off */
100 for (a = 0; a <= b; ++a)
103 /* locate the & sign */
106 for (a = 0; a < strlen(up); ++a) {
107 if (!strncmp(ptr, "&", 1)) {
114 for (a = 0; a < b; ++a)
118 u->url_data = malloc(strlen(up) + 1);
119 strcpy(u->url_data, up);
121 unescape_input(u->url_data);
129 struct urlcontent *u;
131 while (urlstrings != NULL) {
132 free(urlstrings->url_data);
133 u = urlstrings->next;
140 * Diagnostic function to display the contents of all variables
144 struct urlcontent *u;
146 for (u = urlstrings; u != NULL; u = u->next) {
147 wprintf("%38s = %s\n", u->url_key, u->url_data);
151 char *bstr(char *key)
153 struct urlcontent *u;
155 for (u = urlstrings; u != NULL; u = u->next) {
156 if (!strcasecmp(u->url_key, key))
157 return (u->url_data);
163 void wprintf(const char *format,...)
168 va_start(arg_ptr, format);
169 vsprintf(wbuf, format, arg_ptr);
172 write(WC->http_sock, wbuf, strlen(wbuf));
177 * wDumpContent() wraps up an HTTP session, closes tags, etc.
179 * print_standard_html_footer should be set to 0 to transmit only, 1 to
180 * append the main menu and closing tags, or 2 to
181 * append the closing tags only.
183 void wDumpContent(int print_standard_html_footer)
186 wprintf("<CENTER><FONT SIZE=-1>"
187 "<TABLE border=0 width=100%><TR>"
188 "<TD><A HREF=\"/ungoto\">"
189 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
191 wprintf("<TD><A HREF=\"#TheTop\">"
192 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
193 "Top of page</A></TD>");
194 wprintf("<TD><A HREF=\"/display_enter\">"
195 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
196 "Enter a message</A></TD>");
197 wprintf("<TD><A HREF=\"/gotonext\">"
199 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
202 "</TD></TR></TABLE></TABLE>\n");
206 if (print_standard_html_footer) {
207 if (print_standard_html_footer != 2) {
210 wprintf("</BODY></HTML>\n");
217 void escputs1(char *strbuf, int nbsp)
221 for (a = 0; a < strlen(strbuf); ++a) {
222 if (strbuf[a] == '<')
224 else if (strbuf[a] == '>')
226 else if (strbuf[a] == '&')
228 else if (strbuf[a] == '\"')
230 else if (strbuf[a] == '\'')
232 else if (strbuf[a] == LB)
234 else if (strbuf[a] == RB)
236 else if (strbuf[a] == QU)
238 else if ((strbuf[a] == 32) && (nbsp == 1)) {
241 wprintf("%c", strbuf[a]);
246 void escputs(char *strbuf)
253 char *urlesc(char *strbuf)
256 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
257 static char outbuf[512];
261 for (a = 0; a < strlen(strbuf); ++a) {
263 for (b = 0; b < strlen(ec); ++b) {
264 if (strbuf[a] == ec[b])
269 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
271 sprintf(&outbuf[b], "%c", strbuf[a]);
276 void urlescputs(char *strbuf)
278 wprintf("%s", urlesc(strbuf));
285 * Output all that important stuff that the browser will want to see
287 * print_standard_html_head values:
288 * 0 = Nothing. Do not display any leading HTTP or HTML.
289 * 1 = HTTP headers plus the "fake frames" found in most windows.
290 * 2 = HTTP headers required to terminate the session (unset cookies)
291 * 3 = HTTP headers only.
293 void output_headers(int print_standard_html_head)
296 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
299 wprintf("Content-type: text/html\n");
300 wprintf("Server: %s\n", SERVER);
301 wprintf("Connection: close\n");
303 if (print_standard_html_head > 0) {
304 wprintf("Pragma: no-cache\n");
305 wprintf("Cache-Control: no-store\n");
307 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
308 WC->wc_password, WC->wc_roomname);
309 if (print_standard_html_head == 2) {
310 wprintf("X-WebCit-Session: close\n");
311 wprintf("Set-cookie: webcit=%s\n", unset);
313 wprintf("Set-cookie: webcit=%s\n", cookie);
318 if (print_standard_html_head > 0) {
319 wprintf("<HTML><HEAD><TITLE>");
320 escputs(serv_info.serv_humannode);
322 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
324 if (ExpressMessages != NULL) {
325 wprintf("<SCRIPT language=\"javascript\">\n");
326 wprintf("function ExpressMessage() {\n");
327 wprintf(" alert(\"");
328 escputs(ExpressMessages);
330 wprintf(" }\n </SCRIPT>\n");
335 /* JavaScript key-based navigation would go here if it
340 if (ExpressMessages != NULL) {
341 wprintf("onload=\"ExpressMessage()\" ");
342 free(ExpressMessages);
343 ExpressMessages = NULL;
345 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
348 if (print_standard_html_head == 1) {
349 wprintf("<A NAME=\"TheTop\"></A>"
350 "<TABLE border=0 width=100%>"
351 "<TR VALIGN=TOP><TD>");
355 wprintf("</TD><TD VALIGN=TOP>"
356 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
359 embed_room_banner(NULL);
361 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
370 void ExpressMessageCat(char *buf) {
371 if (ExpressMessages == NULL) {
372 ExpressMessages = malloc(strlen(buf) + 4);
373 strcpy(ExpressMessages, "");
375 ExpressMessages = realloc(ExpressMessages,
376 (strlen(ExpressMessages) + strlen(buf) + 4));
378 strcat(ExpressMessages, buf);
379 strcat(ExpressMessages, "\\n");
383 void check_for_express_messages()
390 while (serv_gets(buf), strcmp(buf, "000")) {
391 ExpressMessageCat(buf);
399 void output_static(char *what)
406 sprintf(buf, "static/%s", what);
407 fp = fopen(buf, "rb");
409 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
410 wprintf("Content-Type: text/plain\n");
412 wprintf("Cannot open %s: %s\n", what, strerror(errno));
414 wprintf("HTTP/1.0 200 OK\n");
417 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
418 wprintf("Content-type: image/gif\n");
419 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
420 wprintf("Content-type: text/plain\n");
421 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
422 wprintf("Content-type: image/jpeg\n");
423 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
424 wprintf("Content-type: text/html\n");
426 wprintf("Content-type: application/octet-stream\n");
428 fstat(fileno(fp), &statbuf);
429 bytes = statbuf.st_size;
430 wprintf("Content-length: %ld\n", (long) bytes);
433 wprintf("%c", getc(fp) );
445 off_t accomplished = 0L;
448 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
451 bytes = extract_long(&buf[4], 0);
452 wprintf("HTTP/1.0 200 OK\n");
454 wprintf("Content-type: image/gif\n");
455 wprintf("Content-length: %ld\n", (long) bytes);
458 while (bytes > (off_t) 0) {
459 thisblock = (off_t) sizeof(xferbuf);
460 if (thisblock > bytes)
462 serv_printf("READ %ld|%ld", accomplished, thisblock);
465 thisblock = extract_long(&buf[4], 0);
466 serv_read(xferbuf, (int) thisblock);
467 write(WC->http_sock, xferbuf, thisblock);
468 bytes = bytes - thisblock;
469 accomplished = accomplished + thisblock;
475 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
477 wprintf("Content-Type: text/plain\n");
479 wprintf("Error retrieving image\n");
486 * Convenience functions to display a page containing only a string
488 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
490 wprintf("HTTP/1.0 200 OK\n");
492 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
493 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
494 wprintf("<B>%s</B>\n", titlebarmsg);
495 wprintf("</FONT></TD></TR></TABLE><BR>\n");
496 escputs(messagetext);
503 void display_error(char *errormessage)
505 convenience_page("770000", "Error", errormessage);
508 void display_success(char *successmessage)
510 convenience_page("007700", "OK", successmessage);
515 void extract_action(char *actbuf, char *cmdbuf)
519 strcpy(actbuf, cmdbuf);
520 if (!strncasecmp(actbuf, "GET /", 5))
521 strcpy(actbuf, &actbuf[5]);
522 if (!strncasecmp(actbuf, "PUT /", 5))
523 strcpy(actbuf, &actbuf[5]);
524 if (!strncasecmp(actbuf, "POST /", 6))
525 strcpy(actbuf, &actbuf[6]);
527 for (i = 0; i < strlen(actbuf); ++i) {
528 if (actbuf[i] == ' ') {
532 if (actbuf[i] == '/') {
536 if (actbuf[i] == '?') {
540 if (actbuf[i] == '&') {
548 void upload_handler(char *name, char *filename, char *encoding,
549 void *content, char *cbtype, size_t length)
552 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
553 fprintf(stderr, " name = %s\n", name);
554 fprintf(stderr, "filename = %s\n", filename);
555 fprintf(stderr, "encoding = %s\n", encoding);
556 fprintf(stderr, " type = %s\n", cbtype);
557 fprintf(stderr, " length = %d\n", length);
559 if (strlen(name) > 0) {
560 WC->upload = malloc(length);
561 if (WC->upload != NULL) {
562 WC->upload_length = length;
563 memcpy(WC->upload, content, length);
570 * Entry point for WebCit transaction
572 void session_loop(struct httprequest *req)
578 int ContentLength = 0;
579 char ContentType[512];
581 struct httprequest *hptr;
582 char browser_host[256];
583 char user_agent[256];
585 /* We stuff these with the values coming from the client cookies,
586 * so we can use them to reconnect a timed out session if we have to.
590 char c_username[256];
591 char c_password[256];
592 char c_roomname[256];
595 strcpy(c_host, defaulthost);
596 strcpy(c_port, defaultport);
597 strcpy(c_username, "");
598 strcpy(c_password, "");
599 strcpy(c_roomname, "");
601 WC->upload_length = 0;
605 if (hptr == NULL) return;
607 strcpy(cmd, hptr->line);
609 extract_action(action, cmd);
611 while (hptr != NULL) {
612 strcpy(buf, hptr->line);
615 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
616 strcpy(cookie, &buf[15]);
617 cookie_to_stuff(cookie, NULL,
618 c_username, c_password, c_roomname);
620 else if (!strncasecmp(buf, "Content-length: ", 16)) {
621 ContentLength = atoi(&buf[16]);
623 else if (!strncasecmp(buf, "Content-type: ", 14)) {
624 strcpy(ContentType, &buf[14]);
626 else if (!strncasecmp(buf, "User-agent: ", 12)) {
627 strcpy(user_agent, &buf[12]);
633 if (ContentLength > 0) {
634 content = malloc(ContentLength + 1);
635 read(WC->http_sock, content, ContentLength);
637 content[ContentLength] = 0;
639 if (!strncasecmp(ContentType,
640 "application/x-www-form-urlencoded", 33)) {
642 } else if (!strncasecmp(ContentType, "multipart", 9)) {
643 mime_parser(content, ContentLength, ContentType,
650 /* If there are variables in the URL, we must grab them now */
651 for (a = 0; a < strlen(cmd); ++a)
652 if ((cmd[a] == '?') || (cmd[a] == '&')) {
653 for (b = a; b < strlen(cmd); ++b)
656 addurls(&cmd[a + 1]);
660 * If we're not connected to a Citadel server, try to hook up the
661 * connection now. Preference is given to the host and port specified
662 * by browser cookies, if cookies have been supplied.
664 if (!WC->connected) {
665 if (strlen(bstr("host")) > 0)
666 strcpy(c_host, bstr("host"));
667 if (strlen(bstr("port")) > 0)
668 strcpy(c_port, bstr("port"));
669 WC->serv_sock = connectsock(c_host, c_port, "tcp");
670 if (WC->serv_sock < 0) {
675 serv_gets(buf); /* get the server welcome message */
676 locate_host(browser_host, WC->http_sock);
677 get_serv_info(browser_host, user_agent);
679 check_for_express_messages();
682 * If we're not logged in, but we have username and password cookies
683 * supplied by the browser, try using them to log in.
685 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
686 serv_printf("USER %s", c_username);
689 serv_printf("PASS %s", c_password);
692 become_logged_in(c_username, c_password, buf);
697 * If we don't have a current room, but a cookie specifying the
698 * current room is supplied, make an effort to go there.
700 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
701 serv_printf("GOTO %s", c_roomname);
704 strcpy(WC->wc_roomname, c_roomname);
707 if (!strcasecmp(action, "static")) {
708 strcpy(buf, &cmd[12]);
709 for (a = 0; a < strlen(buf); ++a)
713 } else if (!strcasecmp(action, "image")) {
715 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
717 } else if (!WC->logged_in) {
720 /* Various commands... */
722 else if (!strcasecmp(action, "do_welcome")) {
724 } else if (!strcasecmp(action, "display_main_menu")) {
726 } else if (!strcasecmp(action, "advanced")) {
727 display_advanced_menu();
728 } else if (!strcasecmp(action, "whobbs")) {
730 } else if (!strcasecmp(action, "knrooms")) {
731 list_all_rooms_by_floor();
732 } else if (!strcasecmp(action, "gotonext")) {
735 } else if (!strcasecmp(action, "skip")) {
737 } else if (!strcasecmp(action, "ungoto")) {
739 } else if (!strcasecmp(action, "dotgoto")) {
741 smart_goto(bstr("room"));
742 } else if (!strcasecmp(action, "termquit")) {
744 } else if (!strcasecmp(action, "readnew")) {
746 } else if (!strcasecmp(action, "readold")) {
748 } else if (!strcasecmp(action, "readfwd")) {
750 } else if (!strcasecmp(action, "display_enter")) {
752 } else if (!strcasecmp(action, "post")) {
754 } else if (!strcasecmp(action, "confirm_delete_msg")) {
755 confirm_delete_msg();
756 } else if (!strcasecmp(action, "delete_msg")) {
758 } else if (!strcasecmp(action, "confirm_move_msg")) {
760 } else if (!strcasecmp(action, "move_msg")) {
762 } else if (!strcasecmp(action, "userlist")) {
764 } else if (!strcasecmp(action, "showuser")) {
766 } else if (!strcasecmp(action, "display_page")) {
768 } else if (!strcasecmp(action, "page_user")) {
770 } else if (!strcasecmp(action, "chat")) {
772 } else if (!strcasecmp(action, "display_private")) {
773 display_private("", 0);
774 } else if (!strcasecmp(action, "goto_private")) {
776 } else if (!strcasecmp(action, "zapped_list")) {
778 } else if (!strcasecmp(action, "display_zap")) {
780 } else if (!strcasecmp(action, "zap")) {
782 } else if (!strcasecmp(action, "display_entroom")) {
784 } else if (!strcasecmp(action, "entroom")) {
786 } else if (!strcasecmp(action, "display_editroom")) {
788 } else if (!strcasecmp(action, "editroom")) {
790 } else if (!strcasecmp(action, "display_editinfo")) {
791 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
792 } else if (!strcasecmp(action, "editinfo")) {
793 save_edit("Room info", "EINF 1", 1);
794 } else if (!strcasecmp(action, "display_editbio")) {
795 sprintf(buf, "RBIO %s", WC->wc_username);
796 display_edit("Your bio", "NOOP", buf, "editbio");
797 } else if (!strcasecmp(action, "editbio")) {
798 save_edit("Your bio", "EBIO", 0);
799 } else if (!strcasecmp(action, "confirm_delete_room")) {
800 confirm_delete_room();
801 } else if (!strcasecmp(action, "delete_room")) {
803 } else if (!strcasecmp(action, "validate")) {
805 } else if (!strcasecmp(action, "display_editpic")) {
806 display_graphics_upload("your photo",
809 } else if (!strcasecmp(action, "editpic")) {
810 do_graphics_upload("UIMG 1|_userpic_");
811 } else if (!strcasecmp(action, "display_editroompic")) {
812 display_graphics_upload("the graphic for this room",
815 } else if (!strcasecmp(action, "editroompic")) {
816 do_graphics_upload("UIMG 1|_roompic_");
817 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
818 select_floor_to_edit_pic();
819 } else if (!strcasecmp(action, "display_editfloorpic")) {
820 sprintf(buf, "UIMG 0|_floorpic_|%s",
821 bstr("which_floor"));
822 display_graphics_upload("the graphic for this floor",
825 } else if (!strcasecmp(action, "editfloorpic")) {
826 sprintf(buf, "UIMG 1|_floorpic_|%s",
827 bstr("which_floor"));
828 do_graphics_upload(buf);
829 } else if (!strcasecmp(action, "display_reg")) {
831 } else if (!strcasecmp(action, "register")) {
833 } else if (!strcasecmp(action, "display_changepw")) {
835 } else if (!strcasecmp(action, "changepw")) {
837 } else if (!strcasecmp(action, "display_edit_node")) {
839 } else if (!strcasecmp(action, "display_netconf")) {
841 } else if (!strcasecmp(action, "display_confirm_unshare")) {
842 display_confirm_unshare();
843 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
844 display_confirm_delete_node();
845 } else if (!strcasecmp(action, "delete_node")) {
847 } else if (!strcasecmp(action, "unshare")) {
849 } else if (!strcasecmp(action, "display_add_node")) {
851 } else if (!strcasecmp(action, "add_node")) {
853 } else if (!strcasecmp(action, "display_share")) {
855 } else if (!strcasecmp(action, "share")) {
857 } else if (!strcasecmp(action, "terminate_session")) {
860 } else if (!strcasecmp(action, "edit_me")) {
862 } else if (!strcasecmp(action, "display_siteconfig")) {
863 display_siteconfig();
864 } else if (!strcasecmp(action, "siteconfig")) {
866 } else if (!strcasecmp(action, "display_generic")) {
868 } else if (!strcasecmp(action, "do_generic")) {
870 } else if (!strcasecmp(action, "display_menubar")) {
872 } else if (!strcasecmp(action, "diagnostics")) {
873 wprintf("HTTP/1.0 200 OK\n");
876 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
877 wprintf("You're in session %d<HR>\n", WC->wc_session);
878 wprintf("Command: <BR><PRE>\n");
880 wprintf("</PRE><HR>\n");
881 wprintf("Variables: <BR><PRE>\n");
883 wprintf("</PRE><HR>\n");
886 /* When all else fais, display the main menu. */
892 if (content != NULL) {
897 if (WC->upload_length > 0) {
899 WC->upload_length = 0;