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 if (print_standard_html_head > 0) {
300 wprintf("Content-type: text/html\n");
301 wprintf("Server: %s\n", SERVER);
302 wprintf("Connection: close\n");
303 wprintf("Pragma: no-cache\n");
304 wprintf("Cache-Control: no-store\n");
306 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
307 WC->wc_password, WC->wc_roomname);
308 if (print_standard_html_head == 2) {
309 wprintf("X-WebCit-Session: close\n");
310 wprintf("Set-cookie: webcit=%s\n", unset);
312 wprintf("Set-cookie: webcit=%s\n", cookie);
315 if (print_standard_html_head > 0) {
317 wprintf("<HTML><HEAD><TITLE>");
318 escputs(serv_info.serv_humannode);
320 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
322 if (ExpressMessages != NULL) {
323 wprintf("<SCRIPT language=\"javascript\">\n");
324 wprintf("function ExpressMessage() {\n");
325 wprintf(" alert(\"");
326 escputs(ExpressMessages);
328 wprintf(" }\n </SCRIPT>\n");
333 /* JavaScript key-based navigation would go here if it
338 if (ExpressMessages != NULL) {
339 wprintf("onload=\"ExpressMessage()\" ");
340 free(ExpressMessages);
341 ExpressMessages = NULL;
343 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
346 if (print_standard_html_head == 1) {
347 wprintf("<A NAME=\"TheTop\"></A>"
348 "<TABLE border=0 width=100%>"
349 "<TR VALIGN=TOP><TD>");
353 wprintf("</TD><TD VALIGN=TOP>"
354 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
357 embed_room_banner(NULL);
359 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
368 void ExpressMessageCat(char *buf) {
369 if (ExpressMessages == NULL) {
370 ExpressMessages = malloc(strlen(buf) + 4);
371 strcpy(ExpressMessages, "");
373 ExpressMessages = realloc(ExpressMessages,
374 (strlen(ExpressMessages) + strlen(buf) + 4));
376 strcat(ExpressMessages, buf);
377 strcat(ExpressMessages, "\\n");
381 void check_for_express_messages()
388 while (serv_gets(buf), strcmp(buf, "000")) {
389 ExpressMessageCat(buf);
397 void output_static(char *what)
405 sprintf(buf, "static/%s", what);
406 fp = fopen(buf, "rb");
408 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
409 wprintf("Content-Type: text/plain\n");
411 wprintf("Cannot open %s: %s\n", what, strerror(errno));
413 wprintf("HTTP/1.0 200 OK\n");
416 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
417 wprintf("Content-type: image/gif\n");
418 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
419 wprintf("Content-type: text/plain\n");
420 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
421 wprintf("Content-type: image/jpeg\n");
422 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
423 wprintf("Content-type: text/html\n");
425 wprintf("Content-type: application/octet-stream\n");
427 fstat(fileno(fp), &statbuf);
428 bytes = statbuf.st_size;
429 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
430 wprintf("Content-length: %ld\n", (long) bytes);
433 thisblock = sizeof(buf);
434 if (thisblock > bytes) thisblock = bytes;
435 fread(buf, thisblock, 1, fp);
436 write(WC->http_sock, buf, thisblock);
437 bytes = bytes - thisblock;
444 * When the browser requests an image file from the Citadel server,
445 * this function is called to transmit it.
453 off_t accomplished = 0L;
456 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
459 bytes = extract_long(&buf[4], 0);
460 wprintf("HTTP/1.0 200 OK\n");
462 wprintf("Content-type: image/gif\n");
463 wprintf("Content-length: %ld\n", (long) bytes);
466 while (bytes > (off_t) 0) {
467 thisblock = (off_t) sizeof(xferbuf);
468 if (thisblock > bytes)
470 serv_printf("READ %ld|%ld", accomplished, thisblock);
473 thisblock = extract_long(&buf[4], 0);
474 serv_read(xferbuf, (int) 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);
510 void display_error(char *errormessage)
512 convenience_page("770000", "Error", errormessage);
515 void display_success(char *successmessage)
517 convenience_page("007700", "OK", successmessage);
522 void extract_action(char *actbuf, char *cmdbuf)
526 strcpy(actbuf, cmdbuf);
527 if (!strncasecmp(actbuf, "GET /", 5))
528 strcpy(actbuf, &actbuf[5]);
529 if (!strncasecmp(actbuf, "PUT /", 5))
530 strcpy(actbuf, &actbuf[5]);
531 if (!strncasecmp(actbuf, "POST /", 6))
532 strcpy(actbuf, &actbuf[6]);
534 for (i = 0; i < strlen(actbuf); ++i) {
535 if (actbuf[i] == ' ') {
539 if (actbuf[i] == '/') {
543 if (actbuf[i] == '?') {
547 if (actbuf[i] == '&') {
555 void upload_handler(char *name, char *filename, char *encoding,
556 void *content, char *cbtype, size_t length)
559 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
560 fprintf(stderr, " name = %s\n", name);
561 fprintf(stderr, "filename = %s\n", filename);
562 fprintf(stderr, "encoding = %s\n", encoding);
563 fprintf(stderr, " type = %s\n", cbtype);
564 fprintf(stderr, " length = %d\n", length);
566 if (strlen(name) > 0) {
567 WC->upload = malloc(length);
568 if (WC->upload != NULL) {
569 WC->upload_length = length;
570 memcpy(WC->upload, content, length);
577 * Entry point for WebCit transaction
579 void session_loop(struct httprequest *req)
585 int ContentLength = 0;
586 char ContentType[512];
588 struct httprequest *hptr;
589 char browser_host[256];
590 char user_agent[256];
592 /* We stuff these with the values coming from the client cookies,
593 * so we can use them to reconnect a timed out session if we have to.
597 char c_username[256];
598 char c_password[256];
599 char c_roomname[256];
602 strcpy(c_host, defaulthost);
603 strcpy(c_port, defaultport);
604 strcpy(c_username, "");
605 strcpy(c_password, "");
606 strcpy(c_roomname, "");
608 WC->upload_length = 0;
612 if (hptr == NULL) return;
614 strcpy(cmd, hptr->line);
616 extract_action(action, cmd);
618 while (hptr != NULL) {
619 strcpy(buf, hptr->line);
622 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
623 strcpy(cookie, &buf[15]);
624 cookie_to_stuff(cookie, NULL,
625 c_username, c_password, c_roomname);
627 else if (!strncasecmp(buf, "Content-length: ", 16)) {
628 ContentLength = atoi(&buf[16]);
630 else if (!strncasecmp(buf, "Content-type: ", 14)) {
631 strcpy(ContentType, &buf[14]);
633 else if (!strncasecmp(buf, "User-agent: ", 12)) {
634 strcpy(user_agent, &buf[12]);
638 ++TransactionCount; /* FIX ... make session-specific OR remove */
640 if (ContentLength > 0) {
641 content = malloc(ContentLength + 1);
642 read(WC->http_sock, content, ContentLength);
644 content[ContentLength] = 0;
646 if (!strncasecmp(ContentType,
647 "application/x-www-form-urlencoded", 33)) {
649 } else if (!strncasecmp(ContentType, "multipart", 9)) {
650 mime_parser(content, ContentLength, ContentType,
657 /* If there are variables in the URL, we must grab them now */
658 for (a = 0; a < strlen(cmd); ++a)
659 if ((cmd[a] == '?') || (cmd[a] == '&')) {
660 for (b = a; b < strlen(cmd); ++b)
663 addurls(&cmd[a + 1]);
667 * If we're not connected to a Citadel server, try to hook up the
668 * connection now. Preference is given to the host and port specified
669 * by browser cookies, if cookies have been supplied.
671 if (!WC->connected) {
672 if (strlen(bstr("host")) > 0)
673 strcpy(c_host, bstr("host"));
674 if (strlen(bstr("port")) > 0)
675 strcpy(c_port, bstr("port"));
676 WC->serv_sock = connectsock(c_host, c_port, "tcp");
677 if (WC->serv_sock < 0) {
682 serv_gets(buf); /* get the server welcome message */
683 locate_host(browser_host, WC->http_sock);
684 get_serv_info(browser_host, user_agent);
686 check_for_express_messages();
689 * If we're not logged in, but we have username and password cookies
690 * supplied by the browser, try using them to log in.
692 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
693 serv_printf("USER %s", c_username);
696 serv_printf("PASS %s", c_password);
699 become_logged_in(c_username, c_password, buf);
704 * If we don't have a current room, but a cookie specifying the
705 * current room is supplied, make an effort to go there.
707 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
708 serv_printf("GOTO %s", c_roomname);
711 strcpy(WC->wc_roomname, c_roomname);
714 if (!strcasecmp(action, "static")) {
715 strcpy(buf, &cmd[12]);
716 for (a = 0; a < strlen(buf); ++a)
720 } else if (!strcasecmp(action, "image")) {
722 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
724 } else if (!WC->logged_in) {
727 /* Various commands... */
729 else if (!strcasecmp(action, "do_welcome")) {
731 } else if (!strcasecmp(action, "display_main_menu")) {
733 } else if (!strcasecmp(action, "advanced")) {
734 display_advanced_menu();
735 } else if (!strcasecmp(action, "whobbs")) {
737 } else if (!strcasecmp(action, "knrooms")) {
738 list_all_rooms_by_floor();
739 } else if (!strcasecmp(action, "gotonext")) {
742 } else if (!strcasecmp(action, "skip")) {
744 } else if (!strcasecmp(action, "ungoto")) {
746 } else if (!strcasecmp(action, "dotgoto")) {
748 smart_goto(bstr("room"));
749 } else if (!strcasecmp(action, "termquit")) {
751 } else if (!strcasecmp(action, "readnew")) {
753 } else if (!strcasecmp(action, "readold")) {
755 } else if (!strcasecmp(action, "readfwd")) {
757 } else if (!strcasecmp(action, "display_enter")) {
759 } else if (!strcasecmp(action, "post")) {
761 } else if (!strcasecmp(action, "confirm_delete_msg")) {
762 confirm_delete_msg();
763 } else if (!strcasecmp(action, "delete_msg")) {
765 } else if (!strcasecmp(action, "confirm_move_msg")) {
767 } else if (!strcasecmp(action, "move_msg")) {
769 } else if (!strcasecmp(action, "userlist")) {
771 } else if (!strcasecmp(action, "showuser")) {
773 } else if (!strcasecmp(action, "display_page")) {
775 } else if (!strcasecmp(action, "page_user")) {
777 } else if (!strcasecmp(action, "chat")) {
779 } else if (!strcasecmp(action, "display_private")) {
780 display_private("", 0);
781 } else if (!strcasecmp(action, "goto_private")) {
783 } else if (!strcasecmp(action, "zapped_list")) {
785 } else if (!strcasecmp(action, "display_zap")) {
787 } else if (!strcasecmp(action, "zap")) {
789 } else if (!strcasecmp(action, "display_entroom")) {
791 } else if (!strcasecmp(action, "entroom")) {
793 } else if (!strcasecmp(action, "display_editroom")) {
795 } else if (!strcasecmp(action, "editroom")) {
797 } else if (!strcasecmp(action, "display_editinfo")) {
798 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
799 } else if (!strcasecmp(action, "editinfo")) {
800 save_edit("Room info", "EINF 1", 1);
801 } else if (!strcasecmp(action, "display_editbio")) {
802 sprintf(buf, "RBIO %s", WC->wc_username);
803 display_edit("Your bio", "NOOP", buf, "editbio");
804 } else if (!strcasecmp(action, "editbio")) {
805 save_edit("Your bio", "EBIO", 0);
806 } else if (!strcasecmp(action, "confirm_delete_room")) {
807 confirm_delete_room();
808 } else if (!strcasecmp(action, "delete_room")) {
810 } else if (!strcasecmp(action, "validate")) {
812 } else if (!strcasecmp(action, "display_editpic")) {
813 display_graphics_upload("your photo",
816 } else if (!strcasecmp(action, "editpic")) {
817 do_graphics_upload("UIMG 1|_userpic_");
818 } else if (!strcasecmp(action, "display_editroompic")) {
819 display_graphics_upload("the graphic for this room",
822 } else if (!strcasecmp(action, "editroompic")) {
823 do_graphics_upload("UIMG 1|_roompic_");
824 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
825 select_floor_to_edit_pic();
826 } else if (!strcasecmp(action, "display_editfloorpic")) {
827 sprintf(buf, "UIMG 0|_floorpic_|%s",
828 bstr("which_floor"));
829 display_graphics_upload("the graphic for this floor",
832 } else if (!strcasecmp(action, "editfloorpic")) {
833 sprintf(buf, "UIMG 1|_floorpic_|%s",
834 bstr("which_floor"));
835 do_graphics_upload(buf);
836 } else if (!strcasecmp(action, "display_reg")) {
838 } else if (!strcasecmp(action, "register")) {
840 } else if (!strcasecmp(action, "display_changepw")) {
842 } else if (!strcasecmp(action, "changepw")) {
844 } else if (!strcasecmp(action, "display_edit_node")) {
846 } else if (!strcasecmp(action, "display_netconf")) {
848 } else if (!strcasecmp(action, "display_confirm_unshare")) {
849 display_confirm_unshare();
850 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
851 display_confirm_delete_node();
852 } else if (!strcasecmp(action, "delete_node")) {
854 } else if (!strcasecmp(action, "unshare")) {
856 } else if (!strcasecmp(action, "display_add_node")) {
858 } else if (!strcasecmp(action, "add_node")) {
860 } else if (!strcasecmp(action, "display_share")) {
862 } else if (!strcasecmp(action, "share")) {
864 } else if (!strcasecmp(action, "terminate_session")) {
867 } else if (!strcasecmp(action, "edit_me")) {
869 } else if (!strcasecmp(action, "display_siteconfig")) {
870 display_siteconfig();
871 } else if (!strcasecmp(action, "siteconfig")) {
873 } else if (!strcasecmp(action, "display_generic")) {
875 } else if (!strcasecmp(action, "do_generic")) {
877 } else if (!strcasecmp(action, "display_menubar")) {
879 } else if (!strcasecmp(action, "diagnostics")) {
880 wprintf("HTTP/1.0 200 OK\n");
883 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
884 wprintf("You're in session %d<HR>\n", WC->wc_session);
885 wprintf("Command: <BR><PRE>\n");
887 wprintf("</PRE><HR>\n");
888 wprintf("Variables: <BR><PRE>\n");
890 wprintf("</PRE><HR>\n");
893 /* When all else fais, display the main menu. */
899 if (content != NULL) {
904 if (WC->upload_length > 0) {
906 WC->upload_length = 0;