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 fprintf(stderr, "freeing urlstrings\n");
116 while (WC->urlstrings != NULL) {
117 free(WC->urlstrings->url_data);
118 u = WC->urlstrings->next;
119 free(WC->urlstrings);
125 * Diagnostic function to display the contents of all variables
129 struct urlcontent *u;
131 for (u = WC->urlstrings; u != NULL; u = u->next) {
132 wprintf("%38s = %s\n", u->url_key, u->url_data);
136 char *bstr(char *key)
138 struct urlcontent *u;
140 for (u = WC->urlstrings; u != NULL; u = u->next) {
141 if (!strcasecmp(u->url_key, key))
142 return (u->url_data);
148 void wprintf(const char *format,...)
153 va_start(arg_ptr, format);
154 vsprintf(wbuf, format, arg_ptr);
157 write(WC->http_sock, wbuf, strlen(wbuf));
162 * wDumpContent() wraps up an HTTP session, closes tags, etc.
164 * print_standard_html_footer should be set to 0 to transmit only, 1 to
165 * append the main menu and closing tags, or 2 to
166 * append the closing tags only.
168 void wDumpContent(int print_standard_html_footer)
170 if (WC->fake_frames) {
171 wprintf("<CENTER><FONT SIZE=-1>"
172 "<TABLE border=0 width=100%><TR>"
173 "<TD><A HREF=\"/ungoto\">"
174 "<IMG SRC=\"/static/back.gif\" BORDER=0>"
176 wprintf("<TD><A HREF=\"#TheTop\">"
177 "<IMG SRC=\"/static/up.gif\" BORDER=0>"
178 "Top of page</A></TD>");
179 wprintf("<TD><A HREF=\"/display_enter\">"
180 "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
181 "Enter a message</A></TD>");
182 wprintf("<TD><A HREF=\"/gotonext\">"
184 "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
187 "</TD></TR></TABLE></TABLE>\n");
191 if (print_standard_html_footer) {
192 if (print_standard_html_footer != 2) {
195 wprintf("</BODY></HTML>\n");
202 void escputs1(char *strbuf, int nbsp)
206 for (a = 0; a < strlen(strbuf); ++a) {
207 if (strbuf[a] == '<')
209 else if (strbuf[a] == '>')
211 else if (strbuf[a] == '&')
213 else if (strbuf[a] == '\"')
215 else if (strbuf[a] == '\'')
217 else if (strbuf[a] == LB)
219 else if (strbuf[a] == RB)
221 else if (strbuf[a] == QU)
223 else if ((strbuf[a] == 32) && (nbsp == 1)) {
226 wprintf("%c", strbuf[a]);
231 void escputs(char *strbuf)
238 char *urlesc(char *strbuf)
241 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
242 static char outbuf[512];
246 for (a = 0; a < strlen(strbuf); ++a) {
248 for (b = 0; b < strlen(ec); ++b) {
249 if (strbuf[a] == ec[b])
254 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
256 sprintf(&outbuf[b], "%c", strbuf[a]);
261 void urlescputs(char *strbuf)
263 wprintf("%s", urlesc(strbuf));
270 * Output all that important stuff that the browser will want to see
272 * print_standard_html_head values:
273 * 0 = Nothing. Do not display any leading HTTP or HTML.
274 * 1 = HTTP headers plus the "fake frames" found in most windows.
275 * 2 = HTTP headers required to terminate the session (unset cookies)
276 * 3 = HTTP headers only.
278 void output_headers(int print_standard_html_head)
281 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
284 wprintf("HTTP/1.0 200 OK\n");
286 if (print_standard_html_head > 0) {
287 wprintf("Content-type: text/html\n");
288 wprintf("Server: %s\n", SERVER);
289 wprintf("Connection: close\n");
290 wprintf("Pragma: no-cache\n");
291 wprintf("Cache-Control: no-store\n");
293 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
294 WC->wc_password, WC->wc_roomname);
295 if (print_standard_html_head == 2) {
296 wprintf("X-WebCit-Session: close\n");
297 wprintf("Set-cookie: webcit=%s\n", unset);
299 wprintf("Set-cookie: webcit=%s\n", cookie);
302 if (print_standard_html_head > 0) {
304 wprintf("<HTML><HEAD><TITLE>");
305 escputs(serv_info.serv_humannode);
307 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
309 if (WC->ExpressMessages != NULL) {
310 wprintf("<SCRIPT language=\"javascript\">\n");
311 wprintf("function ExpressMessage() {\n");
312 wprintf(" alert(\"");
313 escputs(WC->ExpressMessages);
315 wprintf(" }\n </SCRIPT>\n");
320 /* JavaScript key-based navigation would go here if it
325 if (WC->ExpressMessages != NULL) {
326 wprintf("onload=\"ExpressMessage()\" ");
327 free(WC->ExpressMessages);
328 WC->ExpressMessages = NULL;
330 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
333 if (print_standard_html_head == 1) {
334 wprintf("<A NAME=\"TheTop\"></A>"
335 "<TABLE border=0 width=100%>"
336 "<TR VALIGN=TOP><TD>");
340 wprintf("</TD><TD VALIGN=TOP>"
341 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
344 embed_room_banner(NULL);
346 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
355 void ExpressMessageCat(char *buf) {
356 if (WC->ExpressMessages == NULL) {
357 WC->ExpressMessages = malloc(strlen(buf) + 4);
358 strcpy(WC->ExpressMessages, "");
360 WC->ExpressMessages = realloc(WC->ExpressMessages,
361 (strlen(WC->ExpressMessages) + strlen(buf) + 4));
363 strcat(WC->ExpressMessages, buf);
364 strcat(WC->ExpressMessages, "\\n");
368 void check_for_express_messages()
375 while (serv_gets(buf), strcmp(buf, "000")) {
376 ExpressMessageCat(buf);
384 void output_static(char *what)
392 sprintf(buf, "static/%s", what);
393 fp = fopen(buf, "rb");
395 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
396 wprintf("Content-Type: text/plain\n");
398 wprintf("Cannot open %s: %s\n", what, strerror(errno));
402 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
403 wprintf("Content-type: image/gif\n");
404 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
405 wprintf("Content-type: text/plain\n");
406 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
407 wprintf("Content-type: image/jpeg\n");
408 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
409 wprintf("Content-type: text/html\n");
411 wprintf("Content-type: application/octet-stream\n");
413 fstat(fileno(fp), &statbuf);
414 bytes = statbuf.st_size;
415 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
416 wprintf("Content-length: %ld\n", (long) bytes);
419 thisblock = sizeof(buf);
420 if (thisblock > bytes) thisblock = bytes;
421 fread(buf, thisblock, 1, fp);
422 write(WC->http_sock, buf, thisblock);
423 bytes = bytes - thisblock;
430 * When the browser requests an image file from the Citadel server,
431 * this function is called to transmit it.
439 off_t accomplished = 0L;
442 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
445 bytes = extract_long(&buf[4], 0);
447 wprintf("Content-type: image/gif\n");
448 wprintf("Content-length: %ld\n", (long) bytes);
451 while (bytes > (off_t) 0) {
452 thisblock = (off_t) sizeof(xferbuf);
453 if (thisblock > bytes)
455 serv_printf("READ %ld|%ld", accomplished, thisblock);
458 thisblock = extract_long(&buf[4], 0);
459 serv_read(xferbuf, (int) thisblock);
460 write(WC->http_sock, xferbuf, thisblock);
461 bytes = bytes - thisblock;
462 accomplished = accomplished + thisblock;
467 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
469 wprintf("Content-Type: text/plain\n");
471 wprintf("Error retrieving image\n");
478 * Convenience functions to display a page containing only a string
480 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
482 wprintf("HTTP/1.0 200 OK\n");
484 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
485 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
486 wprintf("<B>%s</B>\n", titlebarmsg);
487 wprintf("</FONT></TD></TR></TABLE><BR>\n");
488 escputs(messagetext);
495 void display_error(char *errormessage)
497 convenience_page("770000", "Error", errormessage);
500 void display_success(char *successmessage)
502 convenience_page("007700", "OK", successmessage);
507 void extract_action(char *actbuf, char *cmdbuf)
511 strcpy(actbuf, cmdbuf);
512 if (!strncasecmp(actbuf, "GET /", 5))
513 strcpy(actbuf, &actbuf[5]);
514 if (!strncasecmp(actbuf, "PUT /", 5))
515 strcpy(actbuf, &actbuf[5]);
516 if (!strncasecmp(actbuf, "POST /", 6))
517 strcpy(actbuf, &actbuf[6]);
519 for (i = 0; i < strlen(actbuf); ++i) {
520 if (actbuf[i] == ' ') {
524 if (actbuf[i] == '/') {
528 if (actbuf[i] == '?') {
532 if (actbuf[i] == '&') {
540 void upload_handler(char *name, char *filename, char *encoding,
541 void *content, char *cbtype, size_t length)
544 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
545 fprintf(stderr, " name = %s\n", name);
546 fprintf(stderr, "filename = %s\n", filename);
547 fprintf(stderr, "encoding = %s\n", encoding);
548 fprintf(stderr, " type = %s\n", cbtype);
549 fprintf(stderr, " length = %d\n", length);
551 if (strlen(name) > 0) {
552 WC->upload = malloc(length);
553 if (WC->upload != NULL) {
554 WC->upload_length = length;
555 memcpy(WC->upload, content, length);
562 * Entry point for WebCit transaction
564 void session_loop(struct httprequest *req)
570 int ContentLength = 0;
571 char ContentType[512];
573 struct httprequest *hptr;
574 char browser_host[256];
575 char user_agent[256];
577 /* We stuff these with the values coming from the client cookies,
578 * so we can use them to reconnect a timed out session if we have to.
582 char c_username[256];
583 char c_password[256];
584 char c_roomname[256];
587 strcpy(c_host, defaulthost);
588 strcpy(c_port, defaultport);
589 strcpy(c_username, "");
590 strcpy(c_password, "");
591 strcpy(c_roomname, "");
593 WC->upload_length = 0;
597 if (hptr == NULL) return;
599 strcpy(cmd, hptr->line);
601 extract_action(action, cmd);
603 while (hptr != NULL) {
604 strcpy(buf, hptr->line);
607 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
608 strcpy(cookie, &buf[15]);
609 cookie_to_stuff(cookie, NULL,
610 c_username, c_password, c_roomname);
612 else if (!strncasecmp(buf, "Content-length: ", 16)) {
613 ContentLength = atoi(&buf[16]);
615 else if (!strncasecmp(buf, "Content-type: ", 14)) {
616 strcpy(ContentType, &buf[14]);
618 else if (!strncasecmp(buf, "User-agent: ", 12)) {
619 strcpy(user_agent, &buf[12]);
623 if (ContentLength > 0) {
624 content = malloc(ContentLength + 1);
625 read(WC->http_sock, content, ContentLength);
627 content[ContentLength] = 0;
629 if (!strncasecmp(ContentType,
630 "application/x-www-form-urlencoded", 33)) {
632 } else if (!strncasecmp(ContentType, "multipart", 9)) {
633 mime_parser(content, ContentLength, ContentType,
640 /* If there are variables in the URL, we must grab them now */
641 for (a = 0; a < strlen(cmd); ++a)
642 if ((cmd[a] == '?') || (cmd[a] == '&')) {
643 for (b = a; b < strlen(cmd); ++b)
646 addurls(&cmd[a + 1]);
650 * If we're not connected to a Citadel server, try to hook up the
651 * connection now. Preference is given to the host and port specified
652 * by browser cookies, if cookies have been supplied.
654 if (!WC->connected) {
655 if (strlen(bstr("host")) > 0)
656 strcpy(c_host, bstr("host"));
657 if (strlen(bstr("port")) > 0)
658 strcpy(c_port, bstr("port"));
659 WC->serv_sock = connectsock(c_host, c_port, "tcp");
660 if (WC->serv_sock < 0) {
665 serv_gets(buf); /* get the server welcome message */
666 locate_host(browser_host, WC->http_sock);
667 get_serv_info(browser_host, user_agent);
669 check_for_express_messages();
672 * If we're not logged in, but we have username and password cookies
673 * supplied by the browser, try using them to log in.
675 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
676 serv_printf("USER %s", c_username);
679 serv_printf("PASS %s", c_password);
682 become_logged_in(c_username, c_password, buf);
687 * If we don't have a current room, but a cookie specifying the
688 * current room is supplied, make an effort to go there.
690 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
691 serv_printf("GOTO %s", c_roomname);
694 strcpy(WC->wc_roomname, c_roomname);
697 if (!strcasecmp(action, "static")) {
698 strcpy(buf, &cmd[12]);
699 for (a = 0; a < strlen(buf); ++a)
703 } else if (!strcasecmp(action, "image")) {
705 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
707 } else if (!WC->logged_in) {
710 /* Various commands... */
712 else if (!strcasecmp(action, "do_welcome")) {
714 } else if (!strcasecmp(action, "display_main_menu")) {
716 } else if (!strcasecmp(action, "advanced")) {
717 display_advanced_menu();
718 } else if (!strcasecmp(action, "whobbs")) {
720 } else if (!strcasecmp(action, "knrooms")) {
721 list_all_rooms_by_floor();
722 } else if (!strcasecmp(action, "gotonext")) {
725 } else if (!strcasecmp(action, "skip")) {
727 } else if (!strcasecmp(action, "ungoto")) {
729 } else if (!strcasecmp(action, "dotgoto")) {
731 smart_goto(bstr("room"));
732 } else if (!strcasecmp(action, "termquit")) {
734 } else if (!strcasecmp(action, "readnew")) {
736 } else if (!strcasecmp(action, "readold")) {
738 } else if (!strcasecmp(action, "readfwd")) {
740 } else if (!strcasecmp(action, "display_enter")) {
742 } else if (!strcasecmp(action, "post")) {
744 } else if (!strcasecmp(action, "confirm_delete_msg")) {
745 confirm_delete_msg();
746 } else if (!strcasecmp(action, "delete_msg")) {
748 } else if (!strcasecmp(action, "confirm_move_msg")) {
750 } else if (!strcasecmp(action, "move_msg")) {
752 } else if (!strcasecmp(action, "userlist")) {
754 } else if (!strcasecmp(action, "showuser")) {
756 } else if (!strcasecmp(action, "display_page")) {
758 } else if (!strcasecmp(action, "page_user")) {
760 } else if (!strcasecmp(action, "chat")) {
762 } else if (!strcasecmp(action, "display_private")) {
763 display_private("", 0);
764 } else if (!strcasecmp(action, "goto_private")) {
766 } else if (!strcasecmp(action, "zapped_list")) {
768 } else if (!strcasecmp(action, "display_zap")) {
770 } else if (!strcasecmp(action, "zap")) {
772 } else if (!strcasecmp(action, "display_entroom")) {
774 } else if (!strcasecmp(action, "entroom")) {
776 } else if (!strcasecmp(action, "display_editroom")) {
778 } else if (!strcasecmp(action, "editroom")) {
780 } else if (!strcasecmp(action, "display_editinfo")) {
781 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
782 } else if (!strcasecmp(action, "editinfo")) {
783 save_edit("Room info", "EINF 1", 1);
784 } else if (!strcasecmp(action, "display_editbio")) {
785 sprintf(buf, "RBIO %s", WC->wc_username);
786 display_edit("Your bio", "NOOP", buf, "editbio");
787 } else if (!strcasecmp(action, "editbio")) {
788 save_edit("Your bio", "EBIO", 0);
789 } else if (!strcasecmp(action, "confirm_delete_room")) {
790 confirm_delete_room();
791 } else if (!strcasecmp(action, "delete_room")) {
793 } else if (!strcasecmp(action, "validate")) {
795 } else if (!strcasecmp(action, "display_editpic")) {
796 display_graphics_upload("your photo",
799 } else if (!strcasecmp(action, "editpic")) {
800 do_graphics_upload("UIMG 1|_userpic_");
801 } else if (!strcasecmp(action, "display_editroompic")) {
802 display_graphics_upload("the graphic for this room",
805 } else if (!strcasecmp(action, "editroompic")) {
806 do_graphics_upload("UIMG 1|_roompic_");
807 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
808 select_floor_to_edit_pic();
809 } else if (!strcasecmp(action, "display_editfloorpic")) {
810 sprintf(buf, "UIMG 0|_floorpic_|%s",
811 bstr("which_floor"));
812 display_graphics_upload("the graphic for this floor",
815 } else if (!strcasecmp(action, "editfloorpic")) {
816 sprintf(buf, "UIMG 1|_floorpic_|%s",
817 bstr("which_floor"));
818 do_graphics_upload(buf);
819 } else if (!strcasecmp(action, "display_reg")) {
821 } else if (!strcasecmp(action, "register")) {
823 } else if (!strcasecmp(action, "display_changepw")) {
825 } else if (!strcasecmp(action, "changepw")) {
827 } else if (!strcasecmp(action, "display_edit_node")) {
829 } else if (!strcasecmp(action, "display_netconf")) {
831 } else if (!strcasecmp(action, "display_confirm_unshare")) {
832 display_confirm_unshare();
833 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
834 display_confirm_delete_node();
835 } else if (!strcasecmp(action, "delete_node")) {
837 } else if (!strcasecmp(action, "unshare")) {
839 } else if (!strcasecmp(action, "display_add_node")) {
841 } else if (!strcasecmp(action, "add_node")) {
843 } else if (!strcasecmp(action, "display_share")) {
845 } else if (!strcasecmp(action, "share")) {
847 } else if (!strcasecmp(action, "terminate_session")) {
850 } else if (!strcasecmp(action, "edit_me")) {
852 } else if (!strcasecmp(action, "display_siteconfig")) {
853 display_siteconfig();
854 } else if (!strcasecmp(action, "siteconfig")) {
856 } else if (!strcasecmp(action, "display_generic")) {
858 } else if (!strcasecmp(action, "do_generic")) {
860 } else if (!strcasecmp(action, "display_menubar")) {
862 } else if (!strcasecmp(action, "diagnostics")) {
865 wprintf("You're in session %d<HR>\n", WC->wc_session);
866 wprintf("Command: <BR><PRE>\n");
868 wprintf("</PRE><HR>\n");
869 wprintf("Variables: <BR><PRE>\n");
871 wprintf("</PRE><HR>\n");
874 /* When all else fais, display the main menu. */
880 if (content != NULL) {
885 if (WC->upload_length > 0) {
887 WC->upload_length = 0;