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");
207 void escputs1(char *strbuf, int nbsp)
211 for (a = 0; a < strlen(strbuf); ++a) {
212 if (strbuf[a] == '<')
214 else if (strbuf[a] == '>')
216 else if (strbuf[a] == '&')
218 else if (strbuf[a] == '\"')
220 else if (strbuf[a] == '\'')
222 else if (strbuf[a] == LB)
224 else if (strbuf[a] == RB)
226 else if (strbuf[a] == QU)
228 else if ((strbuf[a] == 32) && (nbsp == 1)) {
231 wprintf("%c", strbuf[a]);
236 void escputs(char *strbuf)
243 char *urlesc(char *strbuf)
246 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
247 static char outbuf[512];
251 for (a = 0; a < strlen(strbuf); ++a) {
253 for (b = 0; b < strlen(ec); ++b) {
254 if (strbuf[a] == ec[b])
259 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
261 sprintf(&outbuf[b], "%c", strbuf[a]);
266 void urlescputs(char *strbuf)
268 wprintf("%s", urlesc(strbuf));
275 * Output all that important stuff that the browser will want to see
280 * 0 = Nothing. Do not display any leading HTTP or HTML.
281 * 1 = HTTP headers plus the "fake frames" found in most windows.
282 * 2 = HTTP headers required to terminate the session (unset cookies)
283 * 3 = HTTP and HTML headers, but no 'fake frames'
285 * Bit 2: Set to 1 to auto-refresh page every 30 seconds
287 void output_headers(int controlcode)
290 int print_standard_html_head = 0;
294 print_standard_html_head = controlcode & 0x03;
295 refresh30 = ((controlcode & 0x04) >> 2);
297 wprintf("HTTP/1.0 200 OK\n");
299 httpdate(httpnow, time(NULL));
301 if (print_standard_html_head > 0) {
302 wprintf("Content-type: text/html\n");
303 wprintf("Server: %s\n", SERVER);
304 wprintf("Connection: close\n");
305 wprintf("Pragma: no-cache\n");
306 wprintf("Cache-Control: no-store\n");
308 "Last-modified: %s\n", httpnow, httpnow);
310 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
311 WC->wc_password, WC->wc_roomname);
312 if (print_standard_html_head == 2) {
313 wprintf("Set-cookie: webcit=%s\n", unset);
315 wprintf("Set-cookie: webcit=%s\n", cookie);
318 if (print_standard_html_head > 0) {
320 wprintf("<HTML><HEAD><TITLE>");
321 escputs(serv_info.serv_humannode);
323 "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n"
324 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
325 if (refresh30) wprintf(
326 "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
327 wprintf("</HEAD>\n");
328 if (WC->ExpressMessages != NULL) {
329 wprintf("<SCRIPT language=\"javascript\">\n");
330 wprintf("function ExpressMessage() {\n");
331 wprintf(" alert(\"");
332 escputs(WC->ExpressMessages);
334 wprintf(" }\n </SCRIPT>\n");
339 /* JavaScript key-based navigation would go here if it
344 if (WC->ExpressMessages != NULL) {
345 wprintf("onload=\"ExpressMessage()\" ");
346 free(WC->ExpressMessages);
347 WC->ExpressMessages = NULL;
349 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
352 if (print_standard_html_head == 1) {
353 wprintf("<A NAME=\"TheTop\"></A>"
354 "<TABLE border=0 width=100%>"
355 "<TR VALIGN=TOP><TD VALIGN=LEFT CELLPADDING=0>");
359 wprintf("</TD><TD VALIGN=TOP>"
360 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
363 embed_room_banner(NULL);
365 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
374 void ExpressMessageCat(char *buf) {
375 if (WC->ExpressMessages == NULL) {
376 WC->ExpressMessages = malloc(strlen(buf) + 4);
377 strcpy(WC->ExpressMessages, "");
379 WC->ExpressMessages = realloc(WC->ExpressMessages,
380 (strlen(WC->ExpressMessages) + strlen(buf) + 4));
382 strcat(WC->ExpressMessages, buf);
383 strcat(WC->ExpressMessages, "\\n");
387 void check_for_express_messages()
394 while (serv_gets(buf), strcmp(buf, "000")) {
395 ExpressMessageCat(buf);
403 void output_static(char *what)
412 sprintf(buf, "static/%s", what);
413 fp = fopen(buf, "rb");
415 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
416 wprintf("Content-Type: text/plain\n");
418 wprintf("Cannot open %s: %s\n", what, strerror(errno));
422 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
423 wprintf("Content-type: image/gif\n");
424 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
425 wprintf("Content-type: text/plain\n");
426 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
427 wprintf("Content-type: image/jpeg\n");
428 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
429 wprintf("Content-type: text/html\n");
431 wprintf("Content-type: application/octet-stream\n");
433 fstat(fileno(fp), &statbuf);
434 bytes = statbuf.st_size;
435 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
436 wprintf("Content-length: %ld\n", (long) bytes);
438 httpdate(datebuf, time(NULL));
439 wprintf("Date: %s\n", datebuf);
441 httpdate(datebuf, statbuf.st_mtime);
442 wprintf("Last-modified: %s\n", datebuf);
446 thisblock = sizeof(buf);
447 if (thisblock > bytes) thisblock = bytes;
448 fread(buf, thisblock, 1, fp);
449 write(WC->http_sock, buf, thisblock);
450 bytes = bytes - thisblock;
454 if (!strcasecmp(bstr("force_close_session"), "yes")) {
455 end_webcit_session();
460 * When the browser requests an image file from the Citadel server,
461 * this function is called to transmit it.
469 off_t accomplished = 0L;
472 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
475 bytes = extract_long(&buf[4], 0);
477 wprintf("Content-type: image/gif\n");
478 wprintf("Content-length: %ld\n", (long) bytes);
481 while (bytes > (off_t) 0) {
482 thisblock = (off_t) sizeof(xferbuf);
483 if (thisblock > bytes)
485 serv_printf("READ %ld|%ld", accomplished, thisblock);
488 thisblock = extract_long(&buf[4], 0);
489 serv_read(xferbuf, (int) thisblock);
490 write(WC->http_sock, xferbuf, thisblock);
491 bytes = bytes - thisblock;
492 accomplished = accomplished + thisblock;
497 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
499 wprintf("Content-Type: text/plain\n");
501 wprintf("Error retrieving image\n");
508 * Convenience functions to display a page containing only a string
510 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
512 wprintf("HTTP/1.0 200 OK\n");
514 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
515 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
516 wprintf("<B>%s</B>\n", titlebarmsg);
517 wprintf("</FONT></TD></TR></TABLE><BR>\n");
518 escputs(messagetext);
525 void display_error(char *errormessage)
527 convenience_page("770000", "Error", errormessage);
530 void display_success(char *successmessage)
532 convenience_page("007700", "OK", successmessage);
537 void extract_action(char *actbuf, char *cmdbuf)
541 strcpy(actbuf, cmdbuf);
542 if (!strncasecmp(actbuf, "GET /", 5))
543 strcpy(actbuf, &actbuf[5]);
544 if (!strncasecmp(actbuf, "PUT /", 5))
545 strcpy(actbuf, &actbuf[5]);
546 if (!strncasecmp(actbuf, "POST /", 6))
547 strcpy(actbuf, &actbuf[6]);
549 for (i = 0; i < strlen(actbuf); ++i) {
550 if (actbuf[i] == ' ') {
554 if (actbuf[i] == '/') {
558 if (actbuf[i] == '?') {
562 if (actbuf[i] == '&') {
570 void upload_handler(char *name, char *filename, char *encoding,
571 void *content, char *cbtype, size_t length)
574 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
575 fprintf(stderr, " name = %s\n", name);
576 fprintf(stderr, "filename = %s\n", filename);
577 fprintf(stderr, "encoding = %s\n", encoding);
578 fprintf(stderr, " type = %s\n", cbtype);
579 fprintf(stderr, " length = %ld\n", (long)length);
581 if (strlen(name) > 0) {
582 WC->upload = malloc(length);
583 if (WC->upload != NULL) {
584 WC->upload_length = length;
585 memcpy(WC->upload, content, length);
592 * Entry point for WebCit transaction
594 void session_loop(struct httprequest *req)
600 int ContentLength = 0;
602 char ContentType[512];
604 struct httprequest *hptr;
605 char browser_host[256];
606 char user_agent[256];
608 /* We stuff these with the values coming from the client cookies,
609 * so we can use them to reconnect a timed out session if we have to.
613 char c_username[256];
614 char c_password[256];
615 char c_roomname[256];
618 strcpy(c_host, defaulthost);
619 strcpy(c_port, defaultport);
620 strcpy(c_username, "");
621 strcpy(c_password, "");
622 strcpy(c_roomname, "");
624 WC->upload_length = 0;
628 if (hptr == NULL) return;
630 strcpy(cmd, hptr->line);
632 extract_action(action, cmd);
634 while (hptr != NULL) {
635 strcpy(buf, hptr->line);
638 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
639 strcpy(cookie, &buf[15]);
640 cookie_to_stuff(cookie, NULL,
641 c_username, c_password, c_roomname);
643 else if (!strncasecmp(buf, "Content-length: ", 16)) {
644 ContentLength = atoi(&buf[16]);
646 else if (!strncasecmp(buf, "Content-type: ", 14)) {
647 strcpy(ContentType, &buf[14]);
649 else if (!strncasecmp(buf, "User-agent: ", 12)) {
650 strcpy(user_agent, &buf[12]);
654 if (ContentLength > 0) {
655 fprintf(stderr, "Content length: %d\n", ContentLength);
656 content = malloc(ContentLength + 1);
657 memset(content, 0, ContentLength+1);
660 while (BytesRead < ContentLength) {
661 a=read(WC->http_sock, &content[BytesRead],
662 ContentLength - BytesRead);
663 if (a <= 0) BytesRead = ContentLength;
667 if (!strncasecmp(ContentType,
668 "application/x-www-form-urlencoded", 33)) {
670 } else if (!strncasecmp(ContentType, "multipart", 9)) {
671 mime_parser(content, ContentLength, ContentType,
678 /* If there are variables in the URL, we must grab them now */
679 for (a = 0; a < strlen(cmd); ++a)
680 if ((cmd[a] == '?') || (cmd[a] == '&')) {
681 for (b = a; b < strlen(cmd); ++b)
684 addurls(&cmd[a + 1]);
688 * If we're not connected to a Citadel server, try to hook up the
689 * connection now. Preference is given to the host and port specified
690 * by browser cookies, if cookies have been supplied.
692 if (!WC->connected) {
693 if (strlen(bstr("host")) > 0)
694 strcpy(c_host, bstr("host"));
695 if (strlen(bstr("port")) > 0)
696 strcpy(c_port, bstr("port"));
698 if (!strcasecmp(c_host, "uds")) {
699 /* unix domain socket */
700 sprintf(buf, "%s/citadel.socket", c_port);
701 WC->serv_sock = uds_connectsock(buf);
705 WC->serv_sock = tcp_connectsock(c_host, c_port);
708 if (WC->serv_sock < 0) {
713 serv_gets(buf); /* get the server welcome message */
714 locate_host(browser_host, WC->http_sock);
715 get_serv_info(browser_host, user_agent);
717 check_for_express_messages();
720 * If we're not logged in, but we have username and password cookies
721 * supplied by the browser, try using them to log in.
723 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
724 serv_printf("USER %s", c_username);
727 serv_printf("PASS %s", c_password);
730 become_logged_in(c_username, c_password, buf);
735 * If we don't have a current room, but a cookie specifying the
736 * current room is supplied, make an effort to go there.
738 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
739 serv_printf("GOTO %s", c_roomname);
742 strcpy(WC->wc_roomname, c_roomname);
745 if (!strcasecmp(action, "static")) {
746 strcpy(buf, &cmd[12]);
747 for (a = 0; a < strlen(buf); ++a)
751 } else if (!strcasecmp(action, "image")) {
753 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
755 } else if (!WC->logged_in) {
758 /* Various commands... */
760 else if (!strcasecmp(action, "do_welcome")) {
762 } else if (!strcasecmp(action, "display_main_menu")) {
764 } else if (!strcasecmp(action, "advanced")) {
765 display_advanced_menu();
766 } else if (!strcasecmp(action, "whobbs")) {
768 } else if (!strcasecmp(action, "knrooms")) {
769 list_all_rooms_by_floor();
770 } else if (!strcasecmp(action, "gotonext")) {
773 } else if (!strcasecmp(action, "skip")) {
775 } else if (!strcasecmp(action, "ungoto")) {
777 } else if (!strcasecmp(action, "dotgoto")) {
779 smart_goto(bstr("room"));
780 } else if (!strcasecmp(action, "termquit")) {
782 } else if (!strcasecmp(action, "readnew")) {
784 } else if (!strcasecmp(action, "readold")) {
786 } else if (!strcasecmp(action, "readfwd")) {
788 } else if (!strcasecmp(action, "display_enter")) {
790 } else if (!strcasecmp(action, "post")) {
792 } else if (!strcasecmp(action, "confirm_delete_msg")) {
793 confirm_delete_msg();
794 } else if (!strcasecmp(action, "delete_msg")) {
796 } else if (!strcasecmp(action, "confirm_move_msg")) {
798 } else if (!strcasecmp(action, "move_msg")) {
800 } else if (!strcasecmp(action, "userlist")) {
802 } else if (!strcasecmp(action, "showuser")) {
804 } else if (!strcasecmp(action, "display_page")) {
806 } else if (!strcasecmp(action, "page_user")) {
808 } else if (!strcasecmp(action, "chat")) {
810 } else if (!strcasecmp(action, "display_private")) {
811 display_private("", 0);
812 } else if (!strcasecmp(action, "goto_private")) {
814 } else if (!strcasecmp(action, "zapped_list")) {
816 } else if (!strcasecmp(action, "display_zap")) {
818 } else if (!strcasecmp(action, "zap")) {
820 } else if (!strcasecmp(action, "display_entroom")) {
822 } else if (!strcasecmp(action, "entroom")) {
824 } else if (!strcasecmp(action, "display_editroom")) {
826 } else if (!strcasecmp(action, "editroom")) {
828 } else if (!strcasecmp(action, "display_editinfo")) {
829 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
830 } else if (!strcasecmp(action, "editinfo")) {
831 save_edit("Room info", "EINF 1", 1);
832 } else if (!strcasecmp(action, "display_editbio")) {
833 sprintf(buf, "RBIO %s", WC->wc_username);
834 display_edit("Your bio", "NOOP", buf, "editbio");
835 } else if (!strcasecmp(action, "editbio")) {
836 save_edit("Your bio", "EBIO", 0);
837 } else if (!strcasecmp(action, "confirm_delete_room")) {
838 confirm_delete_room();
839 } else if (!strcasecmp(action, "delete_room")) {
841 } else if (!strcasecmp(action, "validate")) {
843 } else if (!strcasecmp(action, "display_editpic")) {
844 display_graphics_upload("your photo",
847 } else if (!strcasecmp(action, "editpic")) {
848 do_graphics_upload("UIMG 1|_userpic_");
849 } else if (!strcasecmp(action, "display_editroompic")) {
850 display_graphics_upload("the graphic for this room",
853 } else if (!strcasecmp(action, "editroompic")) {
854 do_graphics_upload("UIMG 1|_roompic_");
855 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
856 select_floor_to_edit_pic();
857 } else if (!strcasecmp(action, "display_editfloorpic")) {
858 sprintf(buf, "UIMG 0|_floorpic_|%s",
859 bstr("which_floor"));
860 display_graphics_upload("the graphic for this floor",
863 } else if (!strcasecmp(action, "editfloorpic")) {
864 sprintf(buf, "UIMG 1|_floorpic_|%s",
865 bstr("which_floor"));
866 do_graphics_upload(buf);
867 } else if (!strcasecmp(action, "display_reg")) {
869 } else if (!strcasecmp(action, "register")) {
871 } else if (!strcasecmp(action, "display_changepw")) {
873 } else if (!strcasecmp(action, "changepw")) {
875 } else if (!strcasecmp(action, "display_edit_node")) {
877 } else if (!strcasecmp(action, "display_netconf")) {
879 } else if (!strcasecmp(action, "display_confirm_unshare")) {
880 display_confirm_unshare();
881 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
882 display_confirm_delete_node();
883 } else if (!strcasecmp(action, "delete_node")) {
885 } else if (!strcasecmp(action, "unshare")) {
887 } else if (!strcasecmp(action, "display_add_node")) {
889 } else if (!strcasecmp(action, "add_node")) {
891 } else if (!strcasecmp(action, "display_share")) {
893 } else if (!strcasecmp(action, "share")) {
895 } else if (!strcasecmp(action, "terminate_session")) {
898 } else if (!strcasecmp(action, "edit_me")) {
900 } else if (!strcasecmp(action, "display_siteconfig")) {
901 display_siteconfig();
902 } else if (!strcasecmp(action, "siteconfig")) {
904 } else if (!strcasecmp(action, "display_generic")) {
906 } else if (!strcasecmp(action, "do_generic")) {
908 } else if (!strcasecmp(action, "display_menubar")) {
910 } else if (!strcasecmp(action, "diagnostics")) {
913 wprintf("You're in session %d<HR>\n", WC->wc_session);
914 wprintf("Command: <BR><PRE>\n");
916 wprintf("</PRE><HR>\n");
917 wprintf("Variables: <BR><PRE>\n");
919 wprintf("</PRE><HR>\n");
922 /* When all else fais, display the main menu. */
928 if (content != NULL) {
933 if (WC->upload_length > 0) {
935 WC->upload_length = 0;