4 * This is the main transaction loop of the web service. It maintains a
5 * persistent session to the Citadel server, handling HTTP WebCit requests as
6 * they arrive and presenting a user interface.
12 #include "webserver.h"
13 #include "mime_parser.h"
16 * Subdirectories from which the client may request static content
18 char *static_content_dirs[] = {
24 * String to unset the cookie.
25 * Any date "in the past" will work, so I chose my birthday, right down to
26 * the exact minute. :)
28 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
30 void unescape_input(char *buf)
35 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
36 buf[strlen(buf) - 1] = 0;
38 for (a = 0; a < strlen(buf); ++a) {
46 sscanf(hex, "%02x", &b);
48 strcpy(&buf[a + 1], &buf[a + 3]);
55 void addurls(char *url)
63 while (strlen(up) > 0) {
65 /* locate the = sign */
66 safestrncpy(buf, up, sizeof buf);
68 for (a = 255; a >= 0; --a)
75 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
76 u->next = WC->urlstrings;
78 safestrncpy(u->url_key, buf, sizeof u->url_key);
80 /* now chop that part off */
81 for (a = 0; a <= b; ++a)
84 /* locate "&" and "?" delimiters */
87 for (a = 0; a < strlen(up); ++a) {
88 if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
95 for (a = 0; a < b; ++a)
99 u->url_data = malloc(strlen(up) + 2);
100 safestrncpy(u->url_data, up, strlen(up) + 1);
102 unescape_input(u->url_data);
110 struct urlcontent *u;
112 while (WC->urlstrings != NULL) {
113 free(WC->urlstrings->url_data);
114 u = WC->urlstrings->next;
115 free(WC->urlstrings);
121 * Diagnostic function to display the contents of all variables
125 struct urlcontent *u;
127 for (u = WC->urlstrings; u != NULL; u = u->next) {
128 wprintf("%38s = %s\n", u->url_key, u->url_data);
132 char *bstr(char *key)
134 struct urlcontent *u;
136 for (u = WC->urlstrings; u != NULL; u = u->next) {
137 if (!strcasecmp(u->url_key, key))
138 return (u->url_data);
144 void wprintf(const char *format,...)
149 va_start(arg_ptr, format);
150 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
153 client_write(wbuf, strlen(wbuf));
158 * wDumpContent() wraps up an HTTP session, closes tags, etc.
160 * print_standard_html_footer should be set to 0 to transmit only, 1 to
161 * append the main menu and closing tags, or 2 to
162 * append the closing tags only.
164 void wDumpContent(int print_standard_html_footer)
166 if (print_standard_html_footer) {
167 wprintf("</div>\n"); /* end of "text" div */
168 do_template("trailing");
171 /* If we've been saving it all up for one big output burst,
172 * go ahead and do that now.
179 * Copy a string, escaping characters which have meaning in HTML. If
180 * nbsp is nonzero, spaces are converted to non-breaking spaces.
182 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
187 for (a = 0; a < strlen(strbuf); ++a) {
188 if (strbuf[a] == '<')
189 strcat(target, "<");
190 else if (strbuf[a] == '>')
191 strcat(target, ">");
192 else if (strbuf[a] == '&')
193 strcat(target, "&");
194 else if (strbuf[a] == '\"')
195 strcat(target, """);
196 else if (strbuf[a] == '\'')
197 strcat(target, "'");
198 else if (strbuf[a] == LB)
200 else if (strbuf[a] == RB)
202 else if (strbuf[a] == QU)
203 strcat(target, "\"");
204 else if ((strbuf[a] == 32) && (nbsp == 1))
205 strcat(target, " ");
206 else if ((strbuf[a] == '\n') && (nolinebreaks))
207 strcat(target, ""); /* nothing */
208 else if ((strbuf[a] == '\r') && (nolinebreaks))
209 strcat(target, ""); /* nothing */
211 strncat(target, &strbuf[a], 1);
215 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
219 if (strbuf == NULL) return;
220 buf = malloc( (3 * strlen(strbuf)) + SIZ );
221 stresc(buf, strbuf, nbsp, nolinebreaks);
226 void escputs(char *strbuf)
228 escputs1(strbuf, 0, 0);
232 * Escape a string for feeding out as a URL.
233 * Returns a pointer to a buffer that must be freed by the caller!
235 void urlesc(char *outbuf, char *strbuf)
238 char *ec = " #&;`'|*?-~<>^()[]{}$\"\\";
242 for (a = 0; a < strlen(strbuf); ++a) {
244 for (b = 0; b < strlen(ec); ++b) {
245 if (strbuf[a] == ec[b])
250 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
252 sprintf(&outbuf[b], "%c", strbuf[a]);
256 void urlescputs(char *strbuf)
260 urlesc(outbuf, strbuf);
261 wprintf("%s", outbuf);
266 * Copy a string, escaping characters for JavaScript strings.
268 void jsesc(char *target, char *strbuf)
273 for (a = 0; a < strlen(strbuf); ++a) {
274 if (strbuf[a] == '<')
276 else if (strbuf[a] == '>')
278 else if (strbuf[a] == '\"')
279 strcat(target, """);
280 else if (strbuf[a] == '&')
281 strcat(target, "&;");
282 else if (strbuf[a] == '\'')
283 strcat(target, "\\'");
285 strncat(target, &strbuf[a], 1);
290 void jsescputs(char *strbuf)
294 jsesc(outbuf, strbuf);
295 wprintf("%s", outbuf);
299 * Copy a string, escaping characters for message text hold
301 void msgesc(char *target, char *strbuf)
306 for (a = 0; a < strlen(strbuf); ++a) {
307 if (strbuf[a] == '\n')
309 else if (strbuf[a] == '\r')
311 else if (strbuf[a] == '\'')
312 strcat(target, "'");
314 strncat(target, &strbuf[a], 1);
319 void msgescputs(char *strbuf) {
322 if (strbuf == NULL) return;
323 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
324 msgesc(outbuf, strbuf);
325 wprintf("%s", outbuf);
333 * Output all that important stuff that the browser will want to see
335 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
336 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
338 int do_room_banner, /* 0=no, 1=yes, */
339 /* 2 = I'm going to embed my own, so don't open the */
340 /* <div id="content"> either. */
342 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
343 int suppress_check, /* 1 = suppress check for instant messages */
344 int cache /* 1 = allow browser to cache this page */
349 wprintf("HTTP/1.1 200 OK\n");
350 httpdate(httpnow, time(NULL));
352 if (do_httpheaders) {
353 wprintf("Content-type: text/html; charset=utf-8\r\n"
355 "Connection: close\r\n",
356 SERVER, serv_info.serv_software
361 wprintf("Pragma: public\r\n"
362 "Cache-Control: max-age=3600, must-revalidate\r\n"
363 "Last-modified: %s\r\n",
368 wprintf("Pragma: no-cache\r\n"
369 "Cache-Control: no-store\r\n"
373 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
374 WC->wc_password, WC->wc_roomname);
377 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
379 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
380 if (server_cookie != NULL) {
381 wprintf("%s\n", server_cookie);
393 if (WC->HaveInstantMessages) {
394 wprintf("<div id=\"page_popup\">\n");
398 if (strlen(WC->ImportantMessage) > 0) {
399 wprintf("<div id=\"important_message\">\n");
400 wprintf("<SPAN CLASS=\"imsg\">"
401 "%s</SPAN><br />\n", WC->ImportantMessage);
403 wprintf("<script type=\"text/javascript\">\n"
404 " setTimeout('hide_imsg_popup()', 3000); \n"
406 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
408 if ( (WC->logged_in) && (!unset_cookies) ) {
409 wprintf("<div id=\"iconbar\">");
413 if (do_room_banner == 1) {
414 wprintf("<div id=\"banner\">\n");
415 embed_room_banner(NULL, navbar_default);
420 if (do_room_banner == 1) {
421 wprintf("<div id=\"content\">\n");
427 * Generic function to do an HTTP redirect. Easy and fun.
429 void http_redirect(char *whichpage) {
430 wprintf("HTTP/1.1 302 Moved Temporarily\n");
431 wprintf("Location: %s\r\n", whichpage);
432 wprintf("URI: %s\r\n", whichpage);
433 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
434 wprintf("<html><body>");
435 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
436 wprintf("</body></html>\n");
441 void check_for_instant_messages()
446 serv_getln(buf, sizeof buf);
447 if (buf[3] == '*') WC->HaveInstantMessages = 1;
454 * Output a piece of content to the web browser
456 void http_transmit_thing(char *thing, size_t length, char *content_type,
459 output_headers(0, 0, 0, 0, 0, is_static);
461 wprintf("Content-type: %s\r\n"
463 "Connection: close\r\n",
468 /* If we can send the data out compressed, please do so. */
470 char *compressed_data = NULL;
471 uLongf compressed_len;
473 compressed_len = (uLongf) ((length * 101) / 100) + 100;
474 compressed_data = malloc(compressed_len);
476 if (compress_gzip((Bytef *) compressed_data,
479 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
480 wprintf("Content-encoding: gzip\r\n"
481 "Content-length: %ld\r\n"
483 (long) compressed_len
485 client_write(compressed_data, (size_t)compressed_len);
486 free(compressed_data);
492 /* No compression ... just send it out as-is */
493 wprintf("Content-length: %ld\r\n"
497 client_write(thing, (size_t)length);
503 void output_static(char *what)
509 char content_type[128];
511 fp = fopen(what, "rb");
513 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
514 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
515 wprintf("Content-Type: text/plain\r\n");
517 wprintf("Cannot open %s: %s\n", what, strerror(errno));
519 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
520 safestrncpy(content_type, "image/gif", sizeof content_type);
521 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
522 safestrncpy(content_type, "text/plain", sizeof content_type);
523 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
524 safestrncpy(content_type, "text/css", sizeof content_type);
525 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
526 safestrncpy(content_type, "image/jpeg", sizeof content_type);
527 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
528 safestrncpy(content_type, "image/png", sizeof content_type);
529 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
530 safestrncpy(content_type, "image/x-icon", sizeof content_type);
531 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
532 safestrncpy(content_type, "text/html", sizeof content_type);
533 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
534 safestrncpy(content_type, "text/html", sizeof content_type);
535 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
536 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
537 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
538 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
539 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
540 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
541 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
542 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
543 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
544 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
545 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
546 safestrncpy(content_type, "text/javascript", sizeof content_type);
548 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
550 fstat(fileno(fp), &statbuf);
551 bytes = statbuf.st_size;
552 bigbuffer = malloc(bytes + 2);
553 fread(bigbuffer, bytes, 1, fp);
556 lprintf(9, "output_static('%s') %s\n", what, content_type);
557 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
560 if (!strcasecmp(bstr("force_close_session"), "yes")) {
561 end_webcit_session();
567 * When the browser requests an image file from the Citadel server,
568 * this function is called to transmit it.
573 char *xferbuf = NULL;
576 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
577 serv_getln(buf, sizeof buf);
579 bytes = extract_long(&buf[4], 0);
580 xferbuf = malloc(bytes + 2);
582 /* Read it from the server */
583 read_server_binary(xferbuf, bytes);
585 serv_getln(buf, sizeof buf);
587 /* Write it to the browser */
588 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
592 /* Instead of an ugly 404, send a 1x1 transparent GIF
593 * when there's no such image on the server.
595 output_static("static/blank.gif");
603 * Generic function to output an arbitrary MIME part from an arbitrary
604 * message number on the server.
606 void mimepart(char *msgnum, char *partnum)
610 char content_type[SIZ];
611 char *content = NULL;
613 serv_printf("OPNA %s|%s", msgnum, partnum);
614 serv_getln(buf, sizeof buf);
616 bytes = extract_long(&buf[4], 0);
617 content = malloc(bytes + 2);
618 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
619 output_headers(0, 0, 0, 0, 0, 0);
620 read_server_binary(content, bytes);
622 serv_getln(buf, sizeof buf);
623 http_transmit_thing(content, bytes, content_type, 0);
626 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
627 output_headers(0, 0, 0, 0, 0, 0);
628 wprintf("Content-Type: text/plain\r\n");
630 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
637 * Read any MIME part of a message, from the server, into memory.
639 char *load_mimepart(long msgnum, char *partnum)
643 char content_type[SIZ];
646 serv_printf("OPNA %ld|%s", msgnum, partnum);
647 serv_getln(buf, sizeof buf);
649 bytes = extract_long(&buf[4], 0);
650 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
652 content = malloc(bytes + 2);
653 read_server_binary(content, bytes);
656 serv_getln(buf, sizeof buf);
657 content[bytes] = 0; /* null terminate for good measure */
668 * Convenience functions to display a page containing only a string
670 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
672 wprintf("HTTP/1.1 200 OK\n");
673 output_headers(1, 1, 2, 0, 0, 0);
674 wprintf("<div id=\"banner\">\n");
675 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
676 wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
677 wprintf("</TD></TR></TABLE>\n");
678 wprintf("</div>\n<div id=\"content\">\n");
679 escputs(messagetext);
687 * Display a blank page.
689 void blank_page(void) {
690 output_headers(1, 1, 0, 0, 0, 0);
696 * A template has been requested
698 void url_do_template(void) {
699 do_template(bstr("template"));
705 * Offer to make any page the user's "start page."
707 void offer_start_page(void) {
708 wprintf("<a href=\"change_start_page?startpage=");
709 urlescputs(WC->this_page);
710 wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
711 wprintf(_("Make this my start page"));
712 wprintf("</FONT></A>");
714 wprintf("<br/><a href=\"rss?room=");
715 urlescputs(WC->wc_roomname);
716 wprintf("\" title=\"RSS 2.0 feed for ");
717 escputs(WC->wc_roomname);
718 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
724 * Change the user's start page
726 void change_start_page(void) {
728 if (bstr("startpage") == NULL) {
729 safestrncpy(WC->ImportantMessage,
730 _("You no longer have a start page selected."),
731 sizeof WC->ImportantMessage);
736 set_preference("startpage", bstr("startpage"), 1);
738 output_headers(1, 1, 0, 0, 0, 0);
739 do_template("newstartpage");
746 void display_success(char *successmessage)
748 convenience_page("007700", "OK", successmessage);
752 /* Authorization required page */
753 /* This is probably temporary and should be revisited */
754 void authorization_required(const char *message)
756 wprintf("HTTP/1.1 401 Authorization Required\r\n");
757 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
758 wprintf("Content-Type: text/html\r\n\r\n");
760 wprintf(_("Authorization Required"));
761 wprintf("</h1>\r\n");
762 wprintf(_("The resource you requested requires a valid username and password. "
763 "You could not be logged in: %s\n"), message);
768 void upload_handler(char *name, char *filename, char *partnum, char *disp,
769 void *content, char *cbtype, char *cbcharset,
770 size_t length, char *encoding, void *userdata)
772 struct urlcontent *u;
774 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
775 name, cbtype, length); */
778 if ( (length > 0) && (strlen(cbtype) == 0) ) {
779 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
780 u->next = WC->urlstrings;
782 safestrncpy(u->url_key, name, sizeof(u->url_key));
783 u->url_data = malloc(length + 1);
784 memcpy(u->url_data, content, length);
785 u->url_data[length] = 0;
789 if ( (length > 0) && (strlen(cbtype) > 0) ) {
790 WC->upload = malloc(length);
791 if (WC->upload != NULL) {
792 WC->upload_length = length;
793 safestrncpy(WC->upload_filename, filename,
794 sizeof(WC->upload_filename));
795 safestrncpy(WC->upload_content_type, cbtype,
796 sizeof(WC->upload_content_type));
797 memcpy(WC->upload, content, length);
800 lprintf(3, "malloc() failed: %s\n", strerror(errno));
807 * Convenience functions to wrap around asynchronous ajax responses
809 void begin_ajax_response(void) {
810 output_headers(0, 0, 0, 0, 0, 0);
812 wprintf("Content-type: text/html; charset=UTF-8\r\n"
814 "Connection: close\r\n"
815 "Pragma: no-cache\r\n"
816 "Cache-Control: no-cache\r\n",
821 void end_ajax_response(void) {
826 void ajax_servcmd(void)
833 begin_ajax_response();
835 /* lprintf(9, "Sending cmd: %s\n", bstr("g_cmd")); */
836 serv_printf("%s", bstr("g_cmd"));
837 serv_getln(buf, sizeof buf);
838 /* lprintf(9, " Response: %s\n", buf); */
841 serv_printf("\n\n000");
843 if ((buf[0] == '1') || (buf[0] == '8')) {
844 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
845 /* maybe do something with it? */
850 text_to_server(bstr("g_input"), 0);
856 serv_read(junk, len);
862 memset(junk, 0, len);
863 serv_write(junk, len);
876 * Entry point for WebCit transaction
878 void session_loop(struct httprequest *req)
890 char request_method[128];
893 int ContentLength = 0;
895 char ContentType[512];
896 char *content = NULL;
897 char *content_end = NULL;
898 struct httprequest *hptr;
899 char browser_host[256];
900 char user_agent[256];
904 /* We stuff these with the values coming from the client cookies,
905 * so we can use them to reconnect a timed out session if we have to.
907 char c_username[SIZ];
908 char c_password[SIZ];
909 char c_roomname[SIZ];
910 char c_httpauth_string[SIZ];
911 char c_httpauth_user[SIZ];
912 char c_httpauth_pass[SIZ];
915 safestrncpy(c_username, "", sizeof c_username);
916 safestrncpy(c_password, "", sizeof c_password);
917 safestrncpy(c_roomname, "", sizeof c_roomname);
918 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
919 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
920 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
921 strcpy(browser_host, "");
923 WC->upload_length = 0;
930 if (hptr == NULL) return;
932 safestrncpy(cmd, hptr->line, sizeof cmd);
934 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
935 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
937 /* Figure out the action */
938 extract_token(action, pathname, 1, '/', sizeof action);
939 if (strstr(action, "?")) *strstr(action, "?") = 0;
940 if (strstr(action, "&")) *strstr(action, "&") = 0;
941 if (strstr(action, " ")) *strstr(action, " ") = 0;
943 extract_token(arg1, pathname, 2, '/', sizeof arg1);
944 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
945 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
946 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
948 extract_token(arg2, pathname, 3, '/', sizeof arg2);
949 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
950 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
951 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
953 extract_token(arg3, pathname, 4, '/', sizeof arg3);
954 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
955 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
956 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
958 extract_token(arg4, pathname, 5, '/', sizeof arg4);
959 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
960 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
961 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
963 extract_token(arg5, pathname, 6, '/', sizeof arg5);
964 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
965 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
966 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
968 extract_token(arg6, pathname, 7, '/', sizeof arg6);
969 if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
970 if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
971 if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
973 extract_token(arg7, pathname, 8, '/', sizeof arg7);
974 if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
975 if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
976 if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
978 while (hptr != NULL) {
979 safestrncpy(buf, hptr->line, sizeof buf);
982 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
983 safestrncpy(cookie, &buf[15], sizeof cookie);
984 cookie_to_stuff(cookie, NULL,
985 c_username, sizeof c_username,
986 c_password, sizeof c_password,
987 c_roomname, sizeof c_roomname);
989 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
990 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
991 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
992 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
994 else if (!strncasecmp(buf, "Content-length: ", 16)) {
995 ContentLength = atoi(&buf[16]);
997 else if (!strncasecmp(buf, "Content-type: ", 14)) {
998 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1000 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1001 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1003 else if (!strncasecmp(buf, "Host: ", 6)) {
1004 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1006 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1007 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1008 while (num_tokens(browser_host, ',') > 1) {
1009 remove_token(browser_host, 0, ',');
1011 striplt(browser_host);
1013 /* Only WAP gateways explicitly name this content-type */
1014 else if (strstr(buf, "text/vnd.wap.wml")) {
1019 if (ContentLength > 0) {
1020 content = malloc(ContentLength + SIZ);
1021 memset(content, 0, ContentLength + SIZ);
1022 sprintf(content, "Content-type: %s\n"
1023 "Content-length: %d\n\n",
1024 ContentType, ContentLength);
1025 body_start = strlen(content);
1027 /* Read the entire input data at once. */
1028 client_read(WC->http_sock, &content[BytesRead+body_start],
1031 if (!strncasecmp(ContentType,
1032 "application/x-www-form-urlencoded", 33)) {
1033 addurls(&content[body_start]);
1034 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1035 content_end = content + ContentLength + body_start;
1036 mime_parser(content, content_end, *upload_handler,
1037 NULL, NULL, NULL, 0);
1043 /* make a note of where we are in case the user wants to save it */
1044 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1045 remove_token(WC->this_page, 2, ' ');
1046 remove_token(WC->this_page, 0, ' ');
1048 /* If there are variables in the URL, we must grab them now */
1049 for (a = 0; a < strlen(cmd); ++a) {
1050 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1051 for (b = a; b < strlen(cmd); ++b)
1052 if (isspace(cmd[b]))
1054 addurls(&cmd[a + 1]);
1060 /* Static content can be sent without connecting to Citadel. */
1062 for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
1063 if (!strcasecmp(action, static_content_dirs[a])) {
1068 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1069 action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1070 for (a=0; a<8; ++a) {
1071 if (buf[strlen(buf)-1] == '/') {
1072 buf[strlen(buf)-1] = 0;
1075 for (a = 0; a < strlen(buf); ++a) {
1076 if (isspace(buf[a])) {
1081 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1085 * If we're not connected to a Citadel server, try to hook up the
1088 if (!WC->connected) {
1089 if (!strcasecmp(ctdlhost, "uds")) {
1090 /* unix domain socket */
1091 sprintf(buf, "%s/citadel.socket", ctdlport);
1092 WC->serv_sock = uds_connectsock(buf);
1096 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1099 if (WC->serv_sock < 0) {
1101 goto SKIP_ALL_THIS_CRAP;
1105 serv_getln(buf, sizeof buf); /* get the server welcome message */
1107 /* From what host is our user connecting? Go with
1108 * the host at the other end of the HTTP socket,
1109 * unless we are following X-Forwarded-For: headers
1110 * and such a header has already turned up something.
1112 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1113 locate_host(browser_host, WC->http_sock);
1116 get_serv_info(browser_host, user_agent);
1117 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1118 wprintf(_("You are connected to a Citadel "
1119 "server running Citadel %d.%02d. \n"
1120 "In order to run this version of WebCit "
1121 "you must also have Citadel %d.%02d or"
1123 serv_info.serv_rev_level / 100,
1124 serv_info.serv_rev_level % 100,
1125 MINIMUM_CIT_VERSION / 100,
1126 MINIMUM_CIT_VERSION % 100
1128 end_webcit_session();
1129 goto SKIP_ALL_THIS_CRAP;
1135 * Functions which can be performed without logging in
1137 if (!strcasecmp(action, "listsub")) {
1139 goto SKIP_ALL_THIS_CRAP;
1141 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1142 if (!strcasecmp(action, "freebusy")) {
1144 goto SKIP_ALL_THIS_CRAP;
1149 * If we're not logged in, but we have HTTP Authentication data,
1150 * try logging in to Citadel using that.
1152 if ((!WC->logged_in)
1153 && (strlen(c_httpauth_user) > 0)
1154 && (strlen(c_httpauth_pass) > 0)) {
1155 serv_printf("USER %s", c_httpauth_user);
1156 serv_getln(buf, sizeof buf);
1157 if (buf[0] == '3') {
1158 serv_printf("PASS %s", c_httpauth_pass);
1159 serv_getln(buf, sizeof buf);
1160 if (buf[0] == '2') {
1161 become_logged_in(c_httpauth_user,
1162 c_httpauth_pass, buf);
1163 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1164 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1166 /* Should only display when password is wrong */
1167 authorization_required(&buf[4]);
1168 goto SKIP_ALL_THIS_CRAP;
1173 /* This needs to run early */
1174 if (!strcasecmp(action, "rss")) {
1175 display_rss(bstr("room"), request_method);
1176 goto SKIP_ALL_THIS_CRAP;
1180 * The GroupDAV stuff relies on HTTP authentication instead of
1181 * our session's authentication.
1183 if (!strncasecmp(action, "groupdav", 8)) {
1184 groupdav_main(req, ContentType, /* do GroupDAV methods */
1185 ContentLength, content+body_start);
1186 if (!WC->logged_in) {
1187 WC->killthis = 1; /* If not logged in, don't */
1188 } /* keep the session active */
1189 goto SKIP_ALL_THIS_CRAP;
1194 * Automatically send requests with any method other than GET or
1195 * POST to the GroupDAV code as well.
1197 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1198 groupdav_main(req, ContentType, /* do GroupDAV methods */
1199 ContentLength, content+body_start);
1200 if (!WC->logged_in) {
1201 WC->killthis = 1; /* If not logged in, don't */
1202 } /* keep the session active */
1203 goto SKIP_ALL_THIS_CRAP;
1207 * If we're not logged in, but we have username and password cookies
1208 * supplied by the browser, try using them to log in.
1210 if ((!WC->logged_in)
1211 && (strlen(c_username) > 0)
1212 && (strlen(c_password) > 0)) {
1213 serv_printf("USER %s", c_username);
1214 serv_getln(buf, sizeof buf);
1215 if (buf[0] == '3') {
1216 serv_printf("PASS %s", c_password);
1217 serv_getln(buf, sizeof buf);
1218 if (buf[0] == '2') {
1219 become_logged_in(c_username, c_password, buf);
1224 * If we don't have a current room, but a cookie specifying the
1225 * current room is supplied, make an effort to go there.
1227 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1228 serv_printf("GOTO %s", c_roomname);
1229 serv_getln(buf, sizeof buf);
1230 if (buf[0] == '2') {
1231 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1236 * If there are instant messages waiting, retrieve them for display.
1238 check_for_instant_messages();
1240 if (!strcasecmp(action, "image")) {
1244 * All functions handled below this point ... make sure we log in
1245 * before doing anything else!
1247 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1249 } else if (!WC->logged_in) {
1250 display_login(NULL);
1254 * Various commands...
1257 else if (!strcasecmp(action, "do_welcome")) {
1259 } else if (!strcasecmp(action, "blank")) {
1261 } else if (!strcasecmp(action, "do_template")) {
1263 } else if (!strcasecmp(action, "display_aide_menu")) {
1264 display_aide_menu();
1265 } else if (!strcasecmp(action, "display_main_menu")) {
1266 display_main_menu();
1267 } else if (!strcasecmp(action, "who")) {
1269 } else if (!strcasecmp(action, "who_inner_html")) {
1270 begin_ajax_response();
1272 end_ajax_response();
1273 } else if (!strcasecmp(action, "knrooms")) {
1275 } else if (!strcasecmp(action, "gotonext")) {
1278 } else if (!strcasecmp(action, "skip")) {
1280 } else if (!strcasecmp(action, "ungoto")) {
1282 } else if (!strcasecmp(action, "dotgoto")) {
1283 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1286 smart_goto(bstr("room"));
1287 } else if (!strcasecmp(action, "dotskip")) {
1288 smart_goto(bstr("room"));
1289 } else if (!strcasecmp(action, "termquit")) {
1291 } else if (!strcasecmp(action, "readnew")) {
1292 readloop("readnew");
1293 } else if (!strcasecmp(action, "readold")) {
1294 readloop("readold");
1295 } else if (!strcasecmp(action, "readfwd")) {
1296 readloop("readfwd");
1297 } else if (!strcasecmp(action, "headers")) {
1298 readloop("headers");
1299 } else if (!strcasecmp(action, "msg")) {
1300 embed_message(arg1);
1301 } else if (!strcasecmp(action, "printmsg")) {
1302 print_message(arg1);
1303 } else if (!strcasecmp(action, "display_enter")) {
1305 } else if (!strcasecmp(action, "post")) {
1307 } else if (!strcasecmp(action, "move_msg")) {
1309 } else if (!strcasecmp(action, "delete_msg")) {
1311 } else if (!strcasecmp(action, "userlist")) {
1313 } else if (!strcasecmp(action, "showuser")) {
1315 } else if (!strcasecmp(action, "display_page")) {
1317 } else if (!strcasecmp(action, "page_user")) {
1319 } else if (!strcasecmp(action, "chat")) {
1321 } else if (!strcasecmp(action, "display_private")) {
1322 display_private("", 0);
1323 } else if (!strcasecmp(action, "goto_private")) {
1325 } else if (!strcasecmp(action, "zapped_list")) {
1327 } else if (!strcasecmp(action, "display_zap")) {
1329 } else if (!strcasecmp(action, "zap")) {
1331 } else if (!strcasecmp(action, "display_entroom")) {
1333 } else if (!strcasecmp(action, "entroom")) {
1335 } else if (!strcasecmp(action, "display_whok")) {
1337 } else if (!strcasecmp(action, "do_invt_kick")) {
1339 } else if (!strcasecmp(action, "display_editroom")) {
1341 } else if (!strcasecmp(action, "netedit")) {
1343 } else if (!strcasecmp(action, "editroom")) {
1345 } else if (!strcasecmp(action, "display_editinfo")) {
1346 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1347 } else if (!strcasecmp(action, "editinfo")) {
1348 save_edit(_("Room info"), "EINF 1", 1);
1349 } else if (!strcasecmp(action, "display_editbio")) {
1350 sprintf(buf, "RBIO %s", WC->wc_username);
1351 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1352 } else if (!strcasecmp(action, "editbio")) {
1353 save_edit(_("Your bio"), "EBIO", 0);
1354 } else if (!strcasecmp(action, "confirm_move_msg")) {
1356 } else if (!strcasecmp(action, "delete_room")) {
1358 } else if (!strcasecmp(action, "validate")) {
1360 } else if (!strcasecmp(action, "display_editpic")) {
1361 display_graphics_upload(_("your photo"),
1364 } else if (!strcasecmp(action, "editpic")) {
1365 do_graphics_upload("UIMG 1|_userpic_");
1366 } else if (!strcasecmp(action, "display_editroompic")) {
1367 display_graphics_upload(_("the icon for this room"),
1370 } else if (!strcasecmp(action, "editroompic")) {
1371 do_graphics_upload("UIMG 1|_roompic_");
1372 } else if (!strcasecmp(action, "delete_floor")) {
1374 } else if (!strcasecmp(action, "rename_floor")) {
1376 } else if (!strcasecmp(action, "create_floor")) {
1378 } else if (!strcasecmp(action, "display_editfloorpic")) {
1379 sprintf(buf, "UIMG 0|_floorpic_|%s",
1380 bstr("which_floor"));
1381 display_graphics_upload(_("the icon for this floor"),
1384 } else if (!strcasecmp(action, "editfloorpic")) {
1385 sprintf(buf, "UIMG 1|_floorpic_|%s",
1386 bstr("which_floor"));
1387 do_graphics_upload(buf);
1388 } else if (!strcasecmp(action, "display_reg")) {
1390 } else if (!strcasecmp(action, "display_changepw")) {
1392 } else if (!strcasecmp(action, "changepw")) {
1394 } else if (!strcasecmp(action, "display_edit_node")) {
1395 display_edit_node();
1396 } else if (!strcasecmp(action, "edit_node")) {
1398 } else if (!strcasecmp(action, "display_netconf")) {
1400 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1401 display_confirm_delete_node();
1402 } else if (!strcasecmp(action, "delete_node")) {
1404 } else if (!strcasecmp(action, "display_add_node")) {
1406 } else if (!strcasecmp(action, "add_node")) {
1408 } else if (!strcasecmp(action, "terminate_session")) {
1410 terminate_session();
1411 } else if (!strcasecmp(action, "edit_me")) {
1413 } else if (!strcasecmp(action, "display_siteconfig")) {
1414 display_siteconfig();
1415 } else if (!strcasecmp(action, "chat_recv")) {
1417 } else if (!strcasecmp(action, "chat_send")) {
1419 } else if (!strcasecmp(action, "siteconfig")) {
1421 } else if (!strcasecmp(action, "display_generic")) {
1423 } else if (!strcasecmp(action, "do_generic")) {
1425 } else if (!strcasecmp(action, "ajax_servcmd")) {
1427 } else if (!strcasecmp(action, "display_menubar")) {
1429 } else if (!strcasecmp(action, "mimepart")) {
1430 mimepart(arg1, arg2);
1431 } else if (!strcasecmp(action, "edit_vcard")) {
1433 } else if (!strcasecmp(action, "submit_vcard")) {
1435 } else if (!strcasecmp(action, "select_user_to_edit")) {
1436 select_user_to_edit(NULL, NULL);
1437 } else if (!strcasecmp(action, "display_edituser")) {
1438 display_edituser(NULL, 0);
1439 } else if (!strcasecmp(action, "edituser")) {
1441 } else if (!strcasecmp(action, "create_user")) {
1443 } else if (!strcasecmp(action, "changeview")) {
1445 } else if (!strcasecmp(action, "change_start_page")) {
1446 change_start_page();
1447 } else if (!strcasecmp(action, "display_floorconfig")) {
1448 display_floorconfig(NULL);
1449 } else if (!strcasecmp(action, "toggle_self_service")) {
1450 toggle_self_service();
1451 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1452 } else if (!strcasecmp(action, "display_edit_task")) {
1453 display_edit_task();
1454 } else if (!strcasecmp(action, "save_task")) {
1456 } else if (!strcasecmp(action, "display_edit_event")) {
1457 display_edit_event();
1458 } else if (!strcasecmp(action, "save_event")) {
1460 } else if (!strcasecmp(action, "respond_to_request")) {
1461 respond_to_request();
1462 } else if (!strcasecmp(action, "handle_rsvp")) {
1465 } else if (!strcasecmp(action, "summary")) {
1467 } else if (!strcasecmp(action, "summary_inner_div")) {
1468 begin_ajax_response();
1469 summary_inner_div();
1470 end_ajax_response();
1471 } else if (!strcasecmp(action, "iconbar")) {
1473 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1474 display_customize_iconbar();
1475 } else if (!strcasecmp(action, "commit_iconbar")) {
1477 } else if (!strcasecmp(action, "set_room_policy")) {
1479 } else if (!strcasecmp(action, "display_inetconf")) {
1481 } else if (!strcasecmp(action, "save_inetconf")) {
1483 } else if (!strcasecmp(action, "setup_wizard")) {
1485 } else if (!strcasecmp(action, "display_preferences")) {
1486 display_preferences();
1487 } else if (!strcasecmp(action, "set_preferences")) {
1489 } else if (!strcasecmp(action, "recp_autocomplete")) {
1490 recp_autocomplete(bstr("recp"));
1491 } else if (!strcasecmp(action, "cc_autocomplete")) {
1492 recp_autocomplete(bstr("cc"));
1493 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1494 recp_autocomplete(bstr("bcc"));
1495 } else if (!strcasecmp(action, "diagnostics")) {
1496 output_headers(1, 1, 1, 0, 0, 0);
1497 wprintf("Session: %d<hr />\n", WC->wc_session);
1498 wprintf("Command: <br /><PRE>\n");
1500 wprintf("</PRE><hr />\n");
1501 wprintf("Variables: <br /><PRE>\n");
1503 wprintf("</PRE><hr />\n");
1507 /* When all else fais, display the main menu. */
1509 display_main_menu();
1514 if (content != NULL) {
1519 if (WC->upload_length > 0) {
1521 WC->upload_length = 0;