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
271 * print_standard_html_head values:
272 * 0 = Nothing. Do not display any leading HTTP or HTML.
273 * 1 = HTTP headers plus the "fake frames" found in most windows.
274 * 2 = HTTP headers required to terminate the session (unset cookies)
275 * 3 = HTTP headers only.
277 void output_headers(int print_standard_html_head)
280 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
283 wprintf("HTTP/1.0 200 OK\n");
285 if (print_standard_html_head > 0) {
286 wprintf("Content-type: text/html\n");
287 wprintf("Server: %s\n", SERVER);
288 wprintf("Connection: close\n");
289 wprintf("Pragma: no-cache\n");
290 wprintf("Cache-Control: no-store\n");
292 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
293 WC->wc_password, WC->wc_roomname);
294 if (print_standard_html_head == 2) {
295 wprintf("X-WebCit-Session: close\n");
296 wprintf("Set-cookie: webcit=%s\n", unset);
298 wprintf("Set-cookie: webcit=%s\n", cookie);
301 if (print_standard_html_head > 0) {
303 wprintf("<HTML><HEAD><TITLE>");
304 escputs(serv_info.serv_humannode);
306 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
308 if (WC->ExpressMessages != NULL) {
309 wprintf("<SCRIPT language=\"javascript\">\n");
310 wprintf("function ExpressMessage() {\n");
311 wprintf(" alert(\"");
312 escputs(WC->ExpressMessages);
314 wprintf(" }\n </SCRIPT>\n");
319 /* JavaScript key-based navigation would go here if it
324 if (WC->ExpressMessages != NULL) {
325 wprintf("onload=\"ExpressMessage()\" ");
326 free(WC->ExpressMessages);
327 WC->ExpressMessages = NULL;
329 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
332 if (print_standard_html_head == 1) {
333 wprintf("<A NAME=\"TheTop\"></A>"
334 "<TABLE border=0 width=100%>"
335 "<TR VALIGN=TOP><TD>");
339 wprintf("</TD><TD VALIGN=TOP>"
340 "<TABLE border=0 width=100%><TR VALIGN=TOP>"
343 embed_room_banner(NULL);
345 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
354 void ExpressMessageCat(char *buf) {
355 if (WC->ExpressMessages == NULL) {
356 WC->ExpressMessages = malloc(strlen(buf) + 4);
357 strcpy(WC->ExpressMessages, "");
359 WC->ExpressMessages = realloc(WC->ExpressMessages,
360 (strlen(WC->ExpressMessages) + strlen(buf) + 4));
362 strcat(WC->ExpressMessages, buf);
363 strcat(WC->ExpressMessages, "\\n");
367 void check_for_express_messages()
374 while (serv_gets(buf), strcmp(buf, "000")) {
375 ExpressMessageCat(buf);
383 void output_static(char *what)
391 sprintf(buf, "static/%s", what);
392 fp = fopen(buf, "rb");
394 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
395 wprintf("Content-Type: text/plain\n");
397 wprintf("Cannot open %s: %s\n", what, strerror(errno));
401 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
402 wprintf("Content-type: image/gif\n");
403 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
404 wprintf("Content-type: text/plain\n");
405 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
406 wprintf("Content-type: image/jpeg\n");
407 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
408 wprintf("Content-type: text/html\n");
410 wprintf("Content-type: application/octet-stream\n");
412 fstat(fileno(fp), &statbuf);
413 bytes = statbuf.st_size;
414 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
415 wprintf("Content-length: %ld\n", (long) bytes);
418 thisblock = sizeof(buf);
419 if (thisblock > bytes) thisblock = bytes;
420 fread(buf, thisblock, 1, fp);
421 write(WC->http_sock, buf, thisblock);
422 bytes = bytes - thisblock;
429 * When the browser requests an image file from the Citadel server,
430 * this function is called to transmit it.
438 off_t accomplished = 0L;
441 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
444 bytes = extract_long(&buf[4], 0);
446 wprintf("Content-type: image/gif\n");
447 wprintf("Content-length: %ld\n", (long) bytes);
450 while (bytes > (off_t) 0) {
451 thisblock = (off_t) sizeof(xferbuf);
452 if (thisblock > bytes)
454 serv_printf("READ %ld|%ld", accomplished, thisblock);
457 thisblock = extract_long(&buf[4], 0);
458 serv_read(xferbuf, (int) thisblock);
459 write(WC->http_sock, xferbuf, thisblock);
460 bytes = bytes - thisblock;
461 accomplished = accomplished + thisblock;
466 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
468 wprintf("Content-Type: text/plain\n");
470 wprintf("Error retrieving image\n");
477 * Convenience functions to display a page containing only a string
479 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
481 wprintf("HTTP/1.0 200 OK\n");
483 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
484 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
485 wprintf("<B>%s</B>\n", titlebarmsg);
486 wprintf("</FONT></TD></TR></TABLE><BR>\n");
487 escputs(messagetext);
494 void display_error(char *errormessage)
496 convenience_page("770000", "Error", errormessage);
499 void display_success(char *successmessage)
501 convenience_page("007700", "OK", successmessage);
506 void extract_action(char *actbuf, char *cmdbuf)
510 strcpy(actbuf, cmdbuf);
511 if (!strncasecmp(actbuf, "GET /", 5))
512 strcpy(actbuf, &actbuf[5]);
513 if (!strncasecmp(actbuf, "PUT /", 5))
514 strcpy(actbuf, &actbuf[5]);
515 if (!strncasecmp(actbuf, "POST /", 6))
516 strcpy(actbuf, &actbuf[6]);
518 for (i = 0; i < strlen(actbuf); ++i) {
519 if (actbuf[i] == ' ') {
523 if (actbuf[i] == '/') {
527 if (actbuf[i] == '?') {
531 if (actbuf[i] == '&') {
539 void upload_handler(char *name, char *filename, char *encoding,
540 void *content, char *cbtype, size_t length)
543 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
544 fprintf(stderr, " name = %s\n", name);
545 fprintf(stderr, "filename = %s\n", filename);
546 fprintf(stderr, "encoding = %s\n", encoding);
547 fprintf(stderr, " type = %s\n", cbtype);
548 fprintf(stderr, " length = %d\n", length);
550 if (strlen(name) > 0) {
551 WC->upload = malloc(length);
552 if (WC->upload != NULL) {
553 WC->upload_length = length;
554 memcpy(WC->upload, content, length);
561 * Entry point for WebCit transaction
563 void session_loop(struct httprequest *req)
569 int ContentLength = 0;
570 char ContentType[512];
572 struct httprequest *hptr;
573 char browser_host[256];
574 char user_agent[256];
576 /* We stuff these with the values coming from the client cookies,
577 * so we can use them to reconnect a timed out session if we have to.
581 char c_username[256];
582 char c_password[256];
583 char c_roomname[256];
586 strcpy(c_host, defaulthost);
587 strcpy(c_port, defaultport);
588 strcpy(c_username, "");
589 strcpy(c_password, "");
590 strcpy(c_roomname, "");
592 WC->upload_length = 0;
596 if (hptr == NULL) return;
598 strcpy(cmd, hptr->line);
600 extract_action(action, cmd);
602 while (hptr != NULL) {
603 strcpy(buf, hptr->line);
606 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
607 strcpy(cookie, &buf[15]);
608 cookie_to_stuff(cookie, NULL,
609 c_username, c_password, c_roomname);
611 else if (!strncasecmp(buf, "Content-length: ", 16)) {
612 ContentLength = atoi(&buf[16]);
614 else if (!strncasecmp(buf, "Content-type: ", 14)) {
615 strcpy(ContentType, &buf[14]);
617 else if (!strncasecmp(buf, "User-agent: ", 12)) {
618 strcpy(user_agent, &buf[12]);
622 if (ContentLength > 0) {
623 content = malloc(ContentLength + 1);
624 read(WC->http_sock, content, ContentLength);
626 content[ContentLength] = 0;
628 if (!strncasecmp(ContentType,
629 "application/x-www-form-urlencoded", 33)) {
631 } else if (!strncasecmp(ContentType, "multipart", 9)) {
632 mime_parser(content, ContentLength, ContentType,
639 /* If there are variables in the URL, we must grab them now */
640 for (a = 0; a < strlen(cmd); ++a)
641 if ((cmd[a] == '?') || (cmd[a] == '&')) {
642 for (b = a; b < strlen(cmd); ++b)
645 addurls(&cmd[a + 1]);
649 * If we're not connected to a Citadel server, try to hook up the
650 * connection now. Preference is given to the host and port specified
651 * by browser cookies, if cookies have been supplied.
653 if (!WC->connected) {
654 if (strlen(bstr("host")) > 0)
655 strcpy(c_host, bstr("host"));
656 if (strlen(bstr("port")) > 0)
657 strcpy(c_port, bstr("port"));
658 WC->serv_sock = connectsock(c_host, c_port, "tcp");
659 if (WC->serv_sock < 0) {
664 serv_gets(buf); /* get the server welcome message */
665 locate_host(browser_host, WC->http_sock);
666 get_serv_info(browser_host, user_agent);
668 check_for_express_messages();
671 * If we're not logged in, but we have username and password cookies
672 * supplied by the browser, try using them to log in.
674 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
675 serv_printf("USER %s", c_username);
678 serv_printf("PASS %s", c_password);
681 become_logged_in(c_username, c_password, buf);
686 * If we don't have a current room, but a cookie specifying the
687 * current room is supplied, make an effort to go there.
689 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
690 serv_printf("GOTO %s", c_roomname);
693 strcpy(WC->wc_roomname, c_roomname);
696 if (!strcasecmp(action, "static")) {
697 strcpy(buf, &cmd[12]);
698 for (a = 0; a < strlen(buf); ++a)
702 } else if (!strcasecmp(action, "image")) {
704 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
706 } else if (!WC->logged_in) {
709 /* Various commands... */
711 else if (!strcasecmp(action, "do_welcome")) {
713 } else if (!strcasecmp(action, "display_main_menu")) {
715 } else if (!strcasecmp(action, "advanced")) {
716 display_advanced_menu();
717 } else if (!strcasecmp(action, "whobbs")) {
719 } else if (!strcasecmp(action, "knrooms")) {
720 list_all_rooms_by_floor();
721 } else if (!strcasecmp(action, "gotonext")) {
724 } else if (!strcasecmp(action, "skip")) {
726 } else if (!strcasecmp(action, "ungoto")) {
728 } else if (!strcasecmp(action, "dotgoto")) {
730 smart_goto(bstr("room"));
731 } else if (!strcasecmp(action, "termquit")) {
733 } else if (!strcasecmp(action, "readnew")) {
735 } else if (!strcasecmp(action, "readold")) {
737 } else if (!strcasecmp(action, "readfwd")) {
739 } else if (!strcasecmp(action, "display_enter")) {
741 } else if (!strcasecmp(action, "post")) {
743 } else if (!strcasecmp(action, "confirm_delete_msg")) {
744 confirm_delete_msg();
745 } else if (!strcasecmp(action, "delete_msg")) {
747 } else if (!strcasecmp(action, "confirm_move_msg")) {
749 } else if (!strcasecmp(action, "move_msg")) {
751 } else if (!strcasecmp(action, "userlist")) {
753 } else if (!strcasecmp(action, "showuser")) {
755 } else if (!strcasecmp(action, "display_page")) {
757 } else if (!strcasecmp(action, "page_user")) {
759 } else if (!strcasecmp(action, "chat")) {
761 } else if (!strcasecmp(action, "display_private")) {
762 display_private("", 0);
763 } else if (!strcasecmp(action, "goto_private")) {
765 } else if (!strcasecmp(action, "zapped_list")) {
767 } else if (!strcasecmp(action, "display_zap")) {
769 } else if (!strcasecmp(action, "zap")) {
771 } else if (!strcasecmp(action, "display_entroom")) {
773 } else if (!strcasecmp(action, "entroom")) {
775 } else if (!strcasecmp(action, "display_editroom")) {
777 } else if (!strcasecmp(action, "editroom")) {
779 } else if (!strcasecmp(action, "display_editinfo")) {
780 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
781 } else if (!strcasecmp(action, "editinfo")) {
782 save_edit("Room info", "EINF 1", 1);
783 } else if (!strcasecmp(action, "display_editbio")) {
784 sprintf(buf, "RBIO %s", WC->wc_username);
785 display_edit("Your bio", "NOOP", buf, "editbio");
786 } else if (!strcasecmp(action, "editbio")) {
787 save_edit("Your bio", "EBIO", 0);
788 } else if (!strcasecmp(action, "confirm_delete_room")) {
789 confirm_delete_room();
790 } else if (!strcasecmp(action, "delete_room")) {
792 } else if (!strcasecmp(action, "validate")) {
794 } else if (!strcasecmp(action, "display_editpic")) {
795 display_graphics_upload("your photo",
798 } else if (!strcasecmp(action, "editpic")) {
799 do_graphics_upload("UIMG 1|_userpic_");
800 } else if (!strcasecmp(action, "display_editroompic")) {
801 display_graphics_upload("the graphic for this room",
804 } else if (!strcasecmp(action, "editroompic")) {
805 do_graphics_upload("UIMG 1|_roompic_");
806 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
807 select_floor_to_edit_pic();
808 } else if (!strcasecmp(action, "display_editfloorpic")) {
809 sprintf(buf, "UIMG 0|_floorpic_|%s",
810 bstr("which_floor"));
811 display_graphics_upload("the graphic for this floor",
814 } else if (!strcasecmp(action, "editfloorpic")) {
815 sprintf(buf, "UIMG 1|_floorpic_|%s",
816 bstr("which_floor"));
817 do_graphics_upload(buf);
818 } else if (!strcasecmp(action, "display_reg")) {
820 } else if (!strcasecmp(action, "register")) {
822 } else if (!strcasecmp(action, "display_changepw")) {
824 } else if (!strcasecmp(action, "changepw")) {
826 } else if (!strcasecmp(action, "display_edit_node")) {
828 } else if (!strcasecmp(action, "display_netconf")) {
830 } else if (!strcasecmp(action, "display_confirm_unshare")) {
831 display_confirm_unshare();
832 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
833 display_confirm_delete_node();
834 } else if (!strcasecmp(action, "delete_node")) {
836 } else if (!strcasecmp(action, "unshare")) {
838 } else if (!strcasecmp(action, "display_add_node")) {
840 } else if (!strcasecmp(action, "add_node")) {
842 } else if (!strcasecmp(action, "display_share")) {
844 } else if (!strcasecmp(action, "share")) {
846 } else if (!strcasecmp(action, "terminate_session")) {
849 } else if (!strcasecmp(action, "edit_me")) {
851 } else if (!strcasecmp(action, "display_siteconfig")) {
852 display_siteconfig();
853 } else if (!strcasecmp(action, "siteconfig")) {
855 } else if (!strcasecmp(action, "display_generic")) {
857 } else if (!strcasecmp(action, "do_generic")) {
859 } else if (!strcasecmp(action, "display_menubar")) {
861 } else if (!strcasecmp(action, "diagnostics")) {
864 wprintf("You're in session %d<HR>\n", WC->wc_session);
865 wprintf("Command: <BR><PRE>\n");
867 wprintf("</PRE><HR>\n");
868 wprintf("Variables: <BR><PRE>\n");
870 wprintf("</PRE><HR>\n");
873 /* When all else fais, display the main menu. */
879 if (content != NULL) {
884 if (WC->upload_length > 0) {
886 WC->upload_length = 0;