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>
32 #include "mime_parser.h"
35 * String to unset the cookie.
36 * Any date "in the past" will work, so I chose my birthday, right down to
37 * the exact minute. :)
39 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
41 void unescape_input(char *buf)
46 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
47 buf[strlen(buf) - 1] = 0;
49 for (a = 0; a < strlen(buf); ++a) {
56 sscanf(hex, "%02x", &b);
58 strcpy(&buf[a + 1], &buf[a + 3]);
65 void addurls(char *url)
73 while (strlen(up) > 0) {
75 /* locate the = sign */
76 strncpy(buf, up, 255);
78 for (a = 255; a >= 0; --a)
85 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
86 u->next = WC->urlstrings;
88 strcpy(u->url_key, buf);
90 /* now chop that part off */
91 for (a = 0; a <= b; ++a)
94 /* locate the & sign */
97 for (a = 0; a < strlen(up); ++a) {
98 if (!strncmp(ptr, "&", 1)) {
105 for (a = 0; a < b; ++a)
109 u->url_data = malloc(strlen(up) + 1);
110 strcpy(u->url_data, up);
112 unescape_input(u->url_data);
120 struct urlcontent *u;
122 while (WC->urlstrings != NULL) {
123 free(WC->urlstrings->url_data);
124 u = WC->urlstrings->next;
125 free(WC->urlstrings);
131 * Diagnostic function to display the contents of all variables
135 struct urlcontent *u;
137 for (u = WC->urlstrings; u != NULL; u = u->next) {
138 wprintf("%38s = %s\n", u->url_key, u->url_data);
142 char *bstr(char *key)
144 struct urlcontent *u;
146 for (u = WC->urlstrings; u != NULL; u = u->next) {
147 if (!strcasecmp(u->url_key, key))
148 return (u->url_data);
154 void wprintf(const char *format,...)
159 va_start(arg_ptr, format);
160 vsprintf(wbuf, format, arg_ptr);
163 write(WC->http_sock, wbuf, strlen(wbuf));
168 * wDumpContent() wraps up an HTTP session, closes tags, etc.
170 * print_standard_html_footer should be set to 0 to transmit only, 1 to
171 * append the main menu and closing tags, or 2 to
172 * append the closing tags only.
174 void wDumpContent(int print_standard_html_footer)
176 if (WC->fake_frames) {
177 wprintf("</TABLE>\n");
181 if (print_standard_html_footer) {
182 if (print_standard_html_footer != 2) {
185 wprintf("</BODY></HTML>\n");
193 * Copy a string, escaping characters which have meaning in HTML. If
194 * nbsp is nonzero, spaces are converted to non-breaking spaces.
196 void stresc(char *target, char *strbuf, int nbsp)
201 for (a = 0; a < strlen(strbuf); ++a) {
202 if (strbuf[a] == '<')
203 strcat(target, "<");
204 else if (strbuf[a] == '>')
205 strcat(target, ">");
206 else if (strbuf[a] == '&')
207 strcat(target, "&");
208 else if (strbuf[a] == '\"')
209 strcat(target, """);
210 else if (strbuf[a] == '\'')
211 strcat(target, "'");
212 else if (strbuf[a] == LB)
214 else if (strbuf[a] == RB)
216 else if (strbuf[a] == QU)
217 strcat(target, "\"");
218 else if ((strbuf[a] == 32) && (nbsp == 1)) {
219 strcat(target, " ");
221 strncat(target, &strbuf[a], 1);
226 void escputs1(char *strbuf, int nbsp)
229 stresc(buf, strbuf, nbsp);
233 void escputs(char *strbuf)
239 * Escape a string for feeding out as a URL.
240 * Returns a pointer to a buffer that must be freed by the caller!
242 void urlesc(char *outbuf, char *strbuf)
245 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
249 for (a = 0; a < strlen(strbuf); ++a) {
251 for (b = 0; b < strlen(ec); ++b) {
252 if (strbuf[a] == ec[b])
257 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
259 sprintf(&outbuf[b], "%c", strbuf[a]);
263 void urlescputs(char *strbuf)
267 urlesc(outbuf, strbuf);
268 wprintf("%s", outbuf);
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 room banner
282 * 2 = HTTP headers required to terminate the session (unset cookies)
283 * 3 = HTTP and HTML headers, but no room banner
285 * Bit 2: Set to 1 to auto-refresh page every 30 seconds
287 * Bit 3: suppress check for express messages
289 void output_headers(int controlcode)
292 int print_standard_html_head = 0;
294 int suppress_check = 0;
296 static int pageseq = 0;
298 print_standard_html_head = controlcode & 0x03;
299 refresh30 = ((controlcode & 0x04) >> 2);
300 suppress_check = ((controlcode & 0x08) >> 3);
302 wprintf("HTTP/1.0 200 OK\n");
304 httpdate(httpnow, time(NULL));
306 if (print_standard_html_head > 0) {
307 wprintf("Content-type: text/html\n");
308 wprintf("Server: %s\n", SERVER);
309 wprintf("Connection: close\n");
310 wprintf("Pragma: no-cache\n");
311 wprintf("Cache-Control: no-store\n");
313 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
314 WC->wc_password, WC->wc_roomname);
315 if (print_standard_html_head == 2) {
316 wprintf("Set-cookie: webcit=%s\n", unset);
318 wprintf("Set-cookie: webcit=%s\n", cookie);
319 if (server_cookie != NULL) {
320 wprintf("%s\n", server_cookie);
324 if (print_standard_html_head > 0) {
326 wprintf("<HTML><HEAD><TITLE>");
327 escputs(serv_info.serv_humannode);
329 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
330 "<META HTTP-EQUIV=\"expired\" CONTENT=\"28-May-1971 18:10:00 GMT\">\n"
331 "<META NAME=\"MSSmartTagsPreventParsing\" CONTENT=\"TRUE\">\n");
332 if (refresh30) wprintf(
333 "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
335 "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n");
336 wprintf("</HEAD>\n");
338 /* script for checking for pages (not always launched) */
339 wprintf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
340 wprintf("function launch_page_popup() {\n");
341 wprintf("pwin = window.open('/page_popup', 'CitaPage%d', "
342 "'toolbar=no,location=no,copyhistory=no,status=no,"
343 "scrollbars=yes,resizable=no,height=250,width=400');\n",
346 wprintf("</SCRIPT>\n");
349 /* (JavaScript keyboard-based navigation would go here) */
351 if (!suppress_check) if (WC->HaveExpressMessages) {
352 svprintf("extrabodyparms", WCS_STRING, "%s",
353 "onload=\"launch_page_popup()\" ");
354 WC->HaveExpressMessages = 0;
356 do_template("background.html");
357 clear_local_substs();
359 if (print_standard_html_head == 1) {
360 wprintf("<A NAME=\"TheTop\"></A>");
362 wprintf("<TABLE border=0 width=100%%><TR VALIGN=TOP>"
365 embed_room_banner(NULL);
367 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
378 void http_redirect(char *whichpage) {
379 wprintf("HTTP/1.0 302 Moved Temporarily\n");
380 wprintf("Location: %s\n", whichpage);
381 wprintf("URI: %s\n", whichpage);
382 wprintf("Content-type: text/html\n\n");
383 wprintf("<html><body>\n");
384 wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
386 wprintf("</body></html>\n");
391 void check_for_express_messages()
397 if (buf[3] == '*') WC->HaveExpressMessages = 1;
403 void output_static(char *what)
411 sprintf(buf, "static/%s", what);
412 fp = fopen(buf, "rb");
414 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
415 wprintf("Content-Type: text/plain\n");
417 wprintf("Cannot open %s: %s\n", what, strerror(errno));
421 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
422 wprintf("Content-type: image/gif\n");
423 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
424 wprintf("Content-type: text/plain\n");
425 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
426 wprintf("Content-type: image/jpeg\n");
427 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
428 wprintf("Content-type: text/html\n");
430 wprintf("Content-type: application/octet-stream\n");
432 fstat(fileno(fp), &statbuf);
433 bytes = statbuf.st_size;
434 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
435 wprintf("Content-length: %ld\n", (long) bytes);
437 bigbuffer = malloc(bytes);
438 fread(bigbuffer, bytes, 1, fp);
440 write(WC->http_sock, bigbuffer, bytes);
443 if (!strcasecmp(bstr("force_close_session"), "yes")) {
444 end_webcit_session();
450 * When the browser requests an image file from the Citadel server,
451 * this function is called to transmit it.
459 off_t accomplished = 0L;
462 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
465 bytes = extract_long(&buf[4], 0);
467 wprintf("Content-type: image/gif\n");
468 wprintf("Content-length: %ld\n", (long) bytes);
471 while (bytes > (off_t) 0) {
472 thisblock = (off_t) sizeof(xferbuf);
473 if (thisblock > bytes) {
476 serv_printf("READ %ld|%ld", accomplished, thisblock);
479 thisblock = extract_long(&buf[4], 0);
480 serv_read(xferbuf, (int) thisblock);
483 memset(xferbuf, 0, thisblock);
485 write(WC->http_sock, xferbuf, thisblock);
486 bytes = bytes - thisblock;
487 accomplished = accomplished + thisblock;
492 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
494 wprintf("Content-Type: text/plain\n");
496 wprintf("Error retrieving image: %s\n", &buf[4]);
503 void output_mimepart()
509 off_t accomplished = 0L;
510 char content_type[SIZ];
512 serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
515 bytes = extract_long(&buf[4], 0);
516 extract(content_type, &buf[4], 3);
518 wprintf("Content-type: %s\n", content_type);
519 wprintf("Content-length: %ld\n", (long) bytes);
522 while (bytes > (off_t) 0) {
523 thisblock = (off_t) sizeof(xferbuf);
524 if (thisblock > bytes) {
527 serv_printf("READ %ld|%ld", accomplished, thisblock);
530 thisblock = extract_long(&buf[4], 0);
531 serv_read(xferbuf, (int) thisblock);
534 memset(xferbuf, 0, thisblock);
536 write(WC->http_sock, xferbuf, thisblock);
537 bytes = bytes - thisblock;
538 accomplished = accomplished + thisblock;
543 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
545 wprintf("Content-Type: text/plain\n");
547 wprintf("Error retrieving part: %s\n", &buf[4]);
554 * Convenience functions to display a page containing only a string
556 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
558 wprintf("HTTP/1.0 200 OK\n");
560 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
561 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
562 wprintf("<B>%s</B>\n", titlebarmsg);
563 wprintf("</FONT></TD></TR></TABLE><BR>\n");
564 escputs(messagetext);
572 * Display a blank page.
574 void blank_page(void) {
580 void display_error(char *errormessage)
582 convenience_page("770000", "Error", errormessage);
585 void display_success(char *successmessage)
587 convenience_page("007700", "OK", successmessage);
592 void extract_action(char *actbuf, char *cmdbuf)
596 strcpy(actbuf, cmdbuf);
597 if (!strncasecmp(actbuf, "GET /", 5))
598 strcpy(actbuf, &actbuf[5]);
599 if (!strncasecmp(actbuf, "PUT /", 5))
600 strcpy(actbuf, &actbuf[5]);
601 if (!strncasecmp(actbuf, "POST /", 6))
602 strcpy(actbuf, &actbuf[6]);
604 for (i = 0; i < strlen(actbuf); ++i) {
605 if (actbuf[i] == ' ') {
609 if (actbuf[i] == '/') {
613 if (actbuf[i] == '?') {
617 if (actbuf[i] == '&') {
625 void upload_handler(char *name, char *filename, char *partnum, char *disp,
626 void *content, char *cbtype, size_t length,
627 char *encoding, void *userdata)
630 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
631 fprintf(stderr, " name = %s\n", name);
632 fprintf(stderr, "filename = %s\n", filename);
633 fprintf(stderr, "encoding = %s\n", encoding);
634 fprintf(stderr, " type = %s\n", cbtype);
635 fprintf(stderr, " length = %ld\n", (long)length);
638 WC->upload = malloc(length);
639 if (WC->upload != NULL) {
640 WC->upload_length = length;
641 memcpy(WC->upload, content, length);
644 fprintf(stderr, "malloc() failed: %s\n",
652 * Entry point for WebCit transaction
654 void session_loop(struct httprequest *req)
660 int ContentLength = 0;
662 char ContentType[512];
665 struct httprequest *hptr;
666 char browser_host[SIZ];
667 char user_agent[SIZ];
669 /* We stuff these with the values coming from the client cookies,
670 * so we can use them to reconnect a timed out session if we have to.
674 char c_username[SIZ];
675 char c_password[SIZ];
676 char c_roomname[SIZ];
679 strcpy(c_host, defaulthost);
680 strcpy(c_port, defaultport);
681 strcpy(c_username, "");
682 strcpy(c_password, "");
683 strcpy(c_roomname, "");
685 WC->upload_length = 0;
689 if (hptr == NULL) return;
691 strcpy(cmd, hptr->line);
693 extract_action(action, cmd);
695 while (hptr != NULL) {
696 strcpy(buf, hptr->line);
699 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
700 strcpy(cookie, &buf[15]);
701 cookie_to_stuff(cookie, NULL,
702 c_username, c_password, c_roomname);
704 else if (!strncasecmp(buf, "Content-length: ", 16)) {
705 ContentLength = atoi(&buf[16]);
707 else if (!strncasecmp(buf, "Content-type: ", 14)) {
708 strcpy(ContentType, &buf[14]);
710 else if (!strncasecmp(buf, "User-agent: ", 12)) {
711 strcpy(user_agent, &buf[12]);
715 if (ContentLength > 0) {
716 fprintf(stderr, "Content length: %d\n", ContentLength);
717 content = malloc(ContentLength + 1);
718 memset(content, 0, ContentLength+1);
721 while (BytesRead < ContentLength) {
722 a=read(WC->http_sock, &content[BytesRead],
723 ContentLength - BytesRead);
724 if (a <= 0) BytesRead = ContentLength;
728 if (!strncasecmp(ContentType,
729 "application/x-www-form-urlencoded", 33)) {
731 } else if (!strncasecmp(ContentType, "multipart", 9)) {
732 content_end = content + ContentLength;
733 mime_parser(content, content_end, *upload_handler,
734 NULL, NULL, NULL, 0);
740 /* If there are variables in the URL, we must grab them now */
741 for (a = 0; a < strlen(cmd); ++a)
742 if ((cmd[a] == '?') || (cmd[a] == '&')) {
743 for (b = a; b < strlen(cmd); ++b)
746 addurls(&cmd[a + 1]);
750 * If we're not connected to a Citadel server, try to hook up the
751 * connection now. Preference is given to the host and port specified
752 * by browser cookies, if cookies have been supplied.
754 if (!WC->connected) {
755 if (strlen(bstr("host")) > 0)
756 strcpy(c_host, bstr("host"));
757 if (strlen(bstr("port")) > 0)
758 strcpy(c_port, bstr("port"));
760 if (!strcasecmp(c_host, "uds")) {
761 /* unix domain socket */
762 sprintf(buf, "%s/citadel.socket", c_port);
763 WC->serv_sock = uds_connectsock(buf);
767 WC->serv_sock = tcp_connectsock(c_host, c_port);
770 if (WC->serv_sock < 0) {
772 goto SKIP_ALL_THIS_CRAP;
776 serv_gets(buf); /* get the server welcome message */
777 locate_host(browser_host, WC->http_sock);
778 get_serv_info(browser_host, user_agent);
782 check_for_express_messages();
785 * If we're not logged in, but we have username and password cookies
786 * supplied by the browser, try using them to log in.
788 if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
789 serv_printf("USER %s", c_username);
792 serv_printf("PASS %s", c_password);
795 become_logged_in(c_username, c_password, buf);
800 * If we don't have a current room, but a cookie specifying the
801 * current room is supplied, make an effort to go there.
803 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
804 serv_printf("GOTO %s", c_roomname);
807 strcpy(WC->wc_roomname, c_roomname);
810 if (!strcasecmp(action, "static")) {
811 strcpy(buf, &cmd[12]);
812 for (a = 0; a < strlen(buf); ++a)
816 } else if (!strcasecmp(action, "image")) {
818 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
820 } else if (!WC->logged_in) {
823 /* Various commands... */
825 else if (!strcasecmp(action, "do_welcome")) {
827 } else if (!strcasecmp(action, "blank")) {
829 } else if (!strcasecmp(action, "display_main_menu")) {
831 } else if (!strcasecmp(action, "whobbs")) {
833 } else if (!strcasecmp(action, "knrooms")) {
834 list_all_rooms_by_floor();
835 } else if (!strcasecmp(action, "gotonext")) {
838 } else if (!strcasecmp(action, "skip")) {
840 } else if (!strcasecmp(action, "ungoto")) {
842 } else if (!strcasecmp(action, "dotgoto")) {
844 smart_goto(bstr("room"));
845 } else if (!strcasecmp(action, "dotskip")) {
846 smart_goto(bstr("room"));
847 } else if (!strcasecmp(action, "termquit")) {
849 } else if (!strcasecmp(action, "readnew")) {
851 } else if (!strcasecmp(action, "readold")) {
853 } else if (!strcasecmp(action, "readfwd")) {
855 } else if (!strcasecmp(action, "headers")) {
857 } else if (!strcasecmp(action, "display_enter")) {
859 } else if (!strcasecmp(action, "post")) {
861 } else if (!strcasecmp(action, "delete_msg")) {
863 } else if (!strcasecmp(action, "confirm_move_msg")) {
865 } else if (!strcasecmp(action, "move_msg")) {
867 } else if (!strcasecmp(action, "userlist")) {
869 } else if (!strcasecmp(action, "showuser")) {
871 } else if (!strcasecmp(action, "display_page")) {
873 } else if (!strcasecmp(action, "page_user")) {
875 } else if (!strcasecmp(action, "chat")) {
877 } else if (!strcasecmp(action, "display_private")) {
878 display_private("", 0);
879 } else if (!strcasecmp(action, "goto_private")) {
881 } else if (!strcasecmp(action, "zapped_list")) {
883 } else if (!strcasecmp(action, "display_zap")) {
885 } else if (!strcasecmp(action, "zap")) {
887 } else if (!strcasecmp(action, "display_entroom")) {
889 } else if (!strcasecmp(action, "entroom")) {
891 } else if (!strcasecmp(action, "display_editroom")) {
893 } else if (!strcasecmp(action, "netedit")) {
895 } else if (!strcasecmp(action, "editroom")) {
897 } else if (!strcasecmp(action, "display_whok")) {
899 } else if (!strcasecmp(action, "display_editinfo")) {
900 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
901 } else if (!strcasecmp(action, "editinfo")) {
902 save_edit("Room info", "EINF 1", 1);
903 } else if (!strcasecmp(action, "display_editbio")) {
904 sprintf(buf, "RBIO %s", WC->wc_username);
905 display_edit("Your bio", "NOOP", buf, "editbio");
906 } else if (!strcasecmp(action, "editbio")) {
907 save_edit("Your bio", "EBIO", 0);
908 } else if (!strcasecmp(action, "confirm_delete_room")) {
909 confirm_delete_room();
910 } else if (!strcasecmp(action, "delete_room")) {
912 } else if (!strcasecmp(action, "validate")) {
914 } else if (!strcasecmp(action, "display_editpic")) {
915 display_graphics_upload("your photo",
918 } else if (!strcasecmp(action, "editpic")) {
919 do_graphics_upload("UIMG 1|_userpic_");
920 } else if (!strcasecmp(action, "display_editroompic")) {
921 display_graphics_upload("the graphic for this room",
924 } else if (!strcasecmp(action, "editroompic")) {
925 do_graphics_upload("UIMG 1|_roompic_");
926 } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
927 select_floor_to_edit_pic();
928 } else if (!strcasecmp(action, "display_editfloorpic")) {
929 sprintf(buf, "UIMG 0|_floorpic_|%s",
930 bstr("which_floor"));
931 display_graphics_upload("the graphic for this floor",
934 } else if (!strcasecmp(action, "editfloorpic")) {
935 sprintf(buf, "UIMG 1|_floorpic_|%s",
936 bstr("which_floor"));
937 do_graphics_upload(buf);
938 } else if (!strcasecmp(action, "display_reg")) {
940 } else if (!strcasecmp(action, "register")) {
942 } else if (!strcasecmp(action, "display_changepw")) {
944 } else if (!strcasecmp(action, "changepw")) {
946 } else if (!strcasecmp(action, "display_edit_node")) {
948 } else if (!strcasecmp(action, "edit_node")) {
950 } else if (!strcasecmp(action, "display_netconf")) {
952 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
953 display_confirm_delete_node();
954 } else if (!strcasecmp(action, "delete_node")) {
956 } else if (!strcasecmp(action, "display_add_node")) {
958 } else if (!strcasecmp(action, "add_node")) {
960 } else if (!strcasecmp(action, "terminate_session")) {
963 } else if (!strcasecmp(action, "edit_me")) {
965 } else if (!strcasecmp(action, "display_siteconfig")) {
966 display_siteconfig();
967 } else if (!strcasecmp(action, "page_popup")) {
969 } else if (!strcasecmp(action, "siteconfig")) {
971 } else if (!strcasecmp(action, "display_generic")) {
973 } else if (!strcasecmp(action, "do_generic")) {
975 } else if (!strcasecmp(action, "display_menubar")) {
977 } else if (!strcasecmp(action, "output_mimepart")) {
979 } else if (!strcasecmp(action, "diagnostics")) {
982 wprintf("You're in session %d<HR>\n", WC->wc_session);
983 wprintf("Command: <BR><PRE>\n");
985 wprintf("</PRE><HR>\n");
986 wprintf("Variables: <BR><PRE>\n");
988 wprintf("</PRE><HR>\n");
991 /* When all else fais, display the main menu. */
998 if (content != NULL) {
1003 if (WC->upload_length > 0) {
1005 WC->upload_length = 0;