5 * \defgroup MainServer This is the main transaction loop of the web service. It maintains a
6 * persistent session to the Citadel server, handling HTTP WebCit requests as
7 * they arrive and presenting a user interface.
8 * \ingroup WebcitHttpServer
13 #include "webserver.h"
14 #include "mime_parser.h"
17 * String to unset the cookie.
18 * Any date "in the past" will work, so I chose my birthday, right down to
19 * the exact minute. :)
21 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
24 * \brief remove escaped strings from i.e. the url string (like %20 for blanks)
25 * \param buf the buffer to examine
27 void unescape_input(char *buf)
32 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
33 buf[strlen(buf) - 1] = 0;
35 for (a = 0; a < strlen(buf); ++a) {
43 sscanf(hex, "%02x", &b);
45 strcpy(&buf[a + 1], &buf[a + 3]);
52 * \brief Extract variables from the URL.
53 * \param url URL supplied by the HTTP parser
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);
106 /* lprintf(9, "%s = %s\n", u->url_key, u->url_data); */
111 * \brief free urlstring memory
115 struct urlcontent *u;
117 while (WC->urlstrings != NULL) {
118 free(WC->urlstrings->url_data);
119 u = WC->urlstrings->next;
120 free(WC->urlstrings);
126 * \brief Diagnostic function to display the contents of all variables
130 struct urlcontent *u;
132 for (u = WC->urlstrings; u != NULL; u = u->next) {
133 wprintf("%38s = %s\n", u->url_key, u->url_data);
138 * \brief Return the value of a variable supplied to the current web page (from the url or a form)
139 * \param key The name of the variable we want
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 * \brief web-printing funcion. uses our vsnprintf wrapper
154 * \param format printf format string
155 * \param ... the varargs to put into formatstring
157 void wprintf(const char *format,...)
162 va_start(arg_ptr, format);
163 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
166 client_write(wbuf, strlen(wbuf));
171 * \brief wrap up an HTTP session, closes tags, etc.
172 * \todo multiline params?
173 * \param print_standard_html_footer should be set to 0 to transmit only, 1 to
174 * append the main menu and closing tags, or 2 to
175 * append the closing tags only.
177 void wDumpContent(int print_standard_html_footer)
179 if (print_standard_html_footer) {
180 wprintf("</div>\n"); /* end of "text" div */
181 do_template("trailing");
184 /* If we've been saving it all up for one big output burst,
185 * go ahead and do that now.
192 * \brief Copy a string, escaping characters which have meaning in HTML.
193 * \param target target buffer
194 * \param strbuf source buffer
195 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
196 * \param nolinebreaks if set, linebreaks are removed from the string.
198 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
203 for (a = 0; a < strlen(strbuf); ++a) {
204 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] == '\'')
213 strcat(target, "'");
214 else if (strbuf[a] == LB)
216 else if (strbuf[a] == RB)
218 else if (strbuf[a] == QU)
219 strcat(target, "\"");
220 else if ((strbuf[a] == 32) && (nbsp == 1))
221 strcat(target, " ");
222 else if ((strbuf[a] == '\n') && (nolinebreaks))
223 strcat(target, ""); /* nothing */
224 else if ((strbuf[a] == '\r') && (nolinebreaks))
225 strcat(target, ""); /* nothing */
227 strncat(target, &strbuf[a], 1);
233 * \param strbuf what???
234 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
235 * \param nolinebreaks if set, linebreaks are removed from the string.
237 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
241 if (strbuf == NULL) return;
242 buf = malloc( (3 * strlen(strbuf)) + SIZ );
243 stresc(buf, strbuf, nbsp, nolinebreaks);
249 * \brief static wrapper for ecsputs1
250 * \param strbuf buffer to print escaped to client
252 void escputs(char *strbuf)
254 escputs1(strbuf, 0, 0);
258 * \brief Escape a string for feeding out as a URL.
259 * \param outbuf the output buffer
260 * \param strbuf the input buffer
262 void urlesc(char *outbuf, char *strbuf)
265 char *ec = " #&;`'|*?-~<>^()[]{}$\"\\";
269 for (a = 0; a < strlen(strbuf); ++a) {
271 for (b = 0; b < strlen(ec); ++b) {
272 if (strbuf[a] == ec[b])
277 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
279 sprintf(&outbuf[b], "%c", strbuf[a]);
284 * \brief urlescape buffer and print it to the client
285 * \param strbuf buffer to urlescape
287 void urlescputs(char *strbuf)
291 urlesc(outbuf, strbuf);
292 wprintf("%s", outbuf);
297 * \brief Copy a string, escaping characters for JavaScript strings.
298 * \param target output string
299 * \param strbuf input string
301 void jsesc(char *target, char *strbuf)
306 for (a = 0; a < strlen(strbuf); ++a) {
307 if (strbuf[a] == '<')
309 else if (strbuf[a] == '>')
311 else if (strbuf[a] == '\"')
312 strcat(target, """);
313 else if (strbuf[a] == '&')
314 strcat(target, "&;");
315 else if (strbuf[a] == '\'')
316 strcat(target, "\\'");
318 strncat(target, &strbuf[a], 1);
324 * \brief escape and print java script
325 * \param strbuf the js code
327 void jsescputs(char *strbuf)
331 jsesc(outbuf, strbuf);
332 wprintf("%s", outbuf);
336 * \brief Copy a string, escaping characters for message text hold
337 * \param target target buffer
338 * \param strbuf source buffer
340 void msgesc(char *target, char *strbuf)
345 for (a = 0; a < strlen(strbuf); ++a) {
346 if (strbuf[a] == '\n')
348 else if (strbuf[a] == '\r')
350 else if (strbuf[a] == '\'')
351 strcat(target, "'");
353 strncat(target, &strbuf[a], 1);
359 * \brief print a string to the client after cleaning it with msgesc()
360 * \param strbuf string to be printed
362 void msgescputs(char *strbuf) {
365 if (strbuf == NULL) return;
366 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
367 msgesc(outbuf, strbuf);
368 wprintf("%s", outbuf);
376 * \brief Output all that important stuff that the browser will want to see
378 void output_headers( int do_httpheaders, /**< 1 = output HTTP headers */
379 int do_htmlhead, /**< 1 = output HTML <head> section and <body> opener */
381 int do_room_banner, /**< 0=no, 1=yes,
382 * 2 = I'm going to embed my own, so don't open the
383 * <div id="content"> either.
386 int unset_cookies, /**< 1 = session is terminating, so unset the cookies */
387 int suppress_check, /**< 1 = suppress check for instant messages */
388 int cache /**< 1 = allow browser to cache this page */
393 wprintf("HTTP/1.1 200 OK\n");
394 http_datestring(httpnow, sizeof httpnow, time(NULL));
396 if (do_httpheaders) {
397 wprintf("Content-type: text/html; charset=utf-8\r\n"
399 "Connection: close\r\n",
400 SERVER, serv_info.serv_software
405 wprintf("Pragma: public\r\n"
406 "Cache-Control: max-age=3600, must-revalidate\r\n"
407 "Last-modified: %s\r\n",
412 wprintf("Pragma: no-cache\r\n"
413 "Cache-Control: no-store\r\n"
417 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
418 WC->wc_password, WC->wc_roomname);
421 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
423 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
424 if (server_cookie != NULL) {
425 wprintf("%s\n", server_cookie);
431 if (!access("static.local/webcit.css", R_OK)) {
432 svprintf("CSSLOCAL", WCS_STRING,
433 "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
443 /** check for ImportantMessages (these display in a div overlaying the main screen) */
444 if (strlen(WC->ImportantMessage) > 0) {
445 wprintf("<div id=\"important_message\">\n");
446 wprintf("<span class=\"imsg\">"
447 "%s</span><br />\n", WC->ImportantMessage);
449 wprintf("<script type=\"text/javascript\">\n"
450 " setTimeout('hide_imsg_popup()', 3000); \n"
452 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
455 if ( (WC->logged_in) && (!unset_cookies) ) {
456 wprintf("<div id=\"iconbar\">");
457 do_selected_iconbar();
458 /** check for instant messages (these display in a new window) */
463 if (do_room_banner == 1) {
464 wprintf("<div id=\"banner\">\n");
465 embed_room_banner(NULL, navbar_default);
470 if (do_room_banner == 1) {
471 wprintf("<div id=\"content\">\n");
477 * \brief Generic function to do an HTTP redirect. Easy and fun.
478 * \param whichpage target url to 302 to
480 void http_redirect(char *whichpage) {
481 wprintf("HTTP/1.1 302 Moved Temporarily\n");
482 wprintf("Location: %s\r\n", whichpage);
483 wprintf("URI: %s\r\n", whichpage);
484 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
485 wprintf("<html><body>");
486 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
487 wprintf("</body></html>\n");
493 * \brief Output a piece of content to the web browser
495 void http_transmit_thing(char *thing, size_t length, char *content_type,
498 output_headers(0, 0, 0, 0, 0, is_static);
500 wprintf("Content-type: %s\r\n"
502 "Connection: close\r\n",
507 /** If we can send the data out compressed, please do so. */
509 char *compressed_data = NULL;
510 uLongf compressed_len;
512 compressed_len = (uLongf) ((length * 101) / 100) + 100;
513 compressed_data = malloc(compressed_len);
515 if (compress_gzip((Bytef *) compressed_data,
518 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
519 wprintf("Content-encoding: gzip\r\n"
520 "Content-length: %ld\r\n"
522 (long) compressed_len
524 client_write(compressed_data, (size_t)compressed_len);
525 free(compressed_data);
531 /** No compression ... just send it out as-is */
532 wprintf("Content-length: %ld\r\n"
536 client_write(thing, (size_t)length);
542 * \brief dump out static pages from disk
543 * \param what the file urs to print
545 void output_static(char *what)
551 char content_type[128];
553 fp = fopen(what, "rb");
555 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
556 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
557 wprintf("Content-Type: text/plain\r\n");
559 wprintf("Cannot open %s: %s\n", what, strerror(errno));
561 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
562 safestrncpy(content_type, "image/gif", sizeof content_type);
563 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
564 safestrncpy(content_type, "text/plain", sizeof content_type);
565 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
566 safestrncpy(content_type, "text/css", sizeof content_type);
567 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
568 safestrncpy(content_type, "image/jpeg", sizeof content_type);
569 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
570 safestrncpy(content_type, "image/png", sizeof content_type);
571 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
572 safestrncpy(content_type, "image/x-icon", sizeof content_type);
573 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
574 safestrncpy(content_type, "text/html", sizeof content_type);
575 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
576 safestrncpy(content_type, "text/html", sizeof content_type);
577 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
578 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
579 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
580 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
581 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
582 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
583 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
584 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
585 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
586 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
587 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
588 safestrncpy(content_type, "text/javascript", sizeof content_type);
590 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
592 fstat(fileno(fp), &statbuf);
593 bytes = statbuf.st_size;
594 bigbuffer = malloc(bytes + 2);
595 fread(bigbuffer, bytes, 1, fp);
598 lprintf(9, "output_static('%s') %s\n", what, content_type);
599 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
602 if (!strcasecmp(bstr("force_close_session"), "yes")) {
603 end_webcit_session();
609 * \brief When the browser requests an image file from the Citadel server,
610 * this function is called to transmit it.
615 char *xferbuf = NULL;
618 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
619 serv_getln(buf, sizeof buf);
621 bytes = extract_long(&buf[4], 0);
622 xferbuf = malloc(bytes + 2);
624 /** Read it from the server */
625 read_server_binary(xferbuf, bytes);
627 serv_getln(buf, sizeof buf);
629 /** Write it to the browser */
630 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
635 * Instead of an ugly 404, send a 1x1 transparent GIF
636 * when there's no such image on the server.
638 output_static("static/blank.gif");
646 * \brief Generic function to output an arbitrary MIME part from an arbitrary
647 * message number on the server.
649 * \param msgnum Number of the item on the citadel server
650 * \param partnum The MIME part to be output
651 * \param force_download Nonzero to force set the Content-Type: header
652 * to "application/octet-stream"
654 void mimepart(char *msgnum, char *partnum, int force_download)
658 char content_type[256];
659 char *content = NULL;
661 serv_printf("OPNA %s|%s", msgnum, partnum);
662 serv_getln(buf, sizeof buf);
664 bytes = extract_long(&buf[4], 0);
665 content = malloc(bytes + 2);
666 if (force_download) {
667 strcpy(content_type, "application/octet-stream");
670 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
672 output_headers(0, 0, 0, 0, 0, 0);
673 read_server_binary(content, bytes);
675 serv_getln(buf, sizeof buf);
676 http_transmit_thing(content, bytes, content_type, 0);
679 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
680 output_headers(0, 0, 0, 0, 0, 0);
681 wprintf("Content-Type: text/plain\r\n");
683 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
690 * \brief Read any MIME part of a message, from the server, into memory.
691 * \param msgnum number of the message on the citadel server
692 * \param partnum the MIME part to be loaded
694 char *load_mimepart(long msgnum, char *partnum)
698 char content_type[SIZ];
701 serv_printf("DLAT %ld|%s", msgnum, partnum);
702 serv_getln(buf, sizeof buf);
704 bytes = extract_long(&buf[4], 0);
705 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
707 content = malloc(bytes + 2);
708 serv_read(content, bytes);
710 content[bytes] = 0; /* null terminate for good measure */
721 * \brief Convenience functions to display a page containing only a string
722 * \param titlebarcolor color of the titlebar of the frame
723 * \param titlebarmsg text to display in the title bar
724 * \param messagetext body of the box
726 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
728 wprintf("HTTP/1.1 200 OK\n");
729 output_headers(1, 1, 2, 0, 0, 0);
730 wprintf("<div id=\"banner\">\n");
731 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
732 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
733 wprintf("</td></tr></table>\n");
734 wprintf("</div>\n<div id=\"content\">\n");
735 escputs(messagetext);
743 * \brief Display a blank page.
745 void blank_page(void) {
746 output_headers(1, 1, 0, 0, 0, 0);
752 * \brief A template has been requested
754 void url_do_template(void) {
755 do_template(bstr("template"));
761 * \brief Offer to make any page the user's "start page."
763 void offer_start_page(void) {
764 wprintf("<a href=\"change_start_page?startpage=");
765 urlescputs(WC->this_page);
766 wprintf("\"><font size=-2 color=\"#AAAAAA\">");
767 wprintf(_("Make this my start page"));
768 wprintf("</font></a>");
770 wprintf("<br/><a href=\"rss?room=");
771 urlescputs(WC->wc_roomname);
772 wprintf("\" title=\"RSS 2.0 feed for ");
773 escputs(WC->wc_roomname);
774 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
780 * \brief Change the user's start page
782 void change_start_page(void) {
784 if (bstr("startpage") == NULL) {
785 safestrncpy(WC->ImportantMessage,
786 _("You no longer have a start page selected."),
787 sizeof WC->ImportantMessage);
792 set_preference("startpage", bstr("startpage"), 1);
794 output_headers(1, 1, 0, 0, 0, 0);
795 do_template("newstartpage");
802 * \brief convenience function to indicate success
803 * \param successmessage the mesage itself
805 void display_success(char *successmessage)
807 convenience_page("007700", "OK", successmessage);
812 * \brief Authorization required page
813 * This is probably temporary and should be revisited
814 * \param message message to put in header
816 void authorization_required(const char *message)
818 wprintf("HTTP/1.1 401 Authorization Required\r\n");
819 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
820 wprintf("Content-Type: text/html\r\n\r\n");
822 wprintf(_("Authorization Required"));
823 wprintf("</h1>\r\n");
824 wprintf(_("The resource you requested requires a valid username and password. "
825 "You could not be logged in: %s\n"), message);
830 * \brief This function is called by the MIME parser to handle data uploaded by
831 * the browser. Form data, uploaded files, and the data from HTTP PUT
832 * operations (such as those found in GroupDAV) all arrive this way.
834 * \param name Name of the item being uploaded
835 * \param filename Filename of the item being uploaded
836 * \param partnum MIME part identifier (not needed)
837 * \param disp MIME content disposition (not needed)
838 * \param content The actual data
839 * \param cbtype MIME content-type
840 * \param cbcharset Character set
841 * \param length Content length
842 * \param encoding MIME encoding type (not needed)
843 * \param userdata Not used here
845 void upload_handler(char *name, char *filename, char *partnum, char *disp,
846 void *content, char *cbtype, char *cbcharset,
847 size_t length, char *encoding, void *userdata)
849 struct urlcontent *u;
851 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
852 name, cbtype, length); */
855 if ( (length > 0) && (strlen(cbtype) == 0) ) {
856 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
857 u->next = WC->urlstrings;
859 safestrncpy(u->url_key, name, sizeof(u->url_key));
860 u->url_data = malloc(length + 1);
861 memcpy(u->url_data, content, length);
862 u->url_data[length] = 0;
865 /** Uploaded files */
866 if ( (length > 0) && (strlen(cbtype) > 0) ) {
867 WC->upload = malloc(length);
868 if (WC->upload != NULL) {
869 WC->upload_length = length;
870 safestrncpy(WC->upload_filename, filename,
871 sizeof(WC->upload_filename));
872 safestrncpy(WC->upload_content_type, cbtype,
873 sizeof(WC->upload_content_type));
874 memcpy(WC->upload, content, length);
877 lprintf(3, "malloc() failed: %s\n", strerror(errno));
884 * \brief Convenience functions to wrap around asynchronous ajax responses
886 void begin_ajax_response(void) {
887 output_headers(0, 0, 0, 0, 0, 0);
889 wprintf("Content-type: text/html; charset=UTF-8\r\n"
891 "Connection: close\r\n"
892 "Pragma: no-cache\r\n"
893 "Cache-Control: no-cache\r\n",
899 * \brief print ajax response footer
901 void end_ajax_response(void) {
907 * \brief Wraps a Citadel server command in an AJAX transaction.
909 void ajax_servcmd(void)
916 begin_ajax_response();
918 serv_printf("%s", bstr("g_cmd"));
919 serv_getln(buf, sizeof buf);
920 wprintf("%s\n", buf);
923 serv_printf("\n\n000");
925 if ((buf[0] == '1') || (buf[0] == '8')) {
926 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
927 wprintf("%s\n", gcontent);
932 text_to_server(bstr("g_input"));
938 serv_read(junk, len);
944 memset(junk, 0, len);
945 serv_write(junk, len);
952 * This is kind of an ugly hack, but this is the only place it can go.
953 * If the command was GEXP, then the instant messenger window must be
954 * running, so reset the "last_pager_check" watchdog timer so
955 * that page_popup() doesn't try to open it a second time.
957 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
958 WC->last_pager_check = time(NULL);
964 * \brief Helper function for the asynchronous check to see if we need
965 * to open the instant messenger window.
967 void seconds_since_last_gexp(void)
971 begin_ajax_response();
972 if ( (time(NULL) - WC->last_pager_check) < 30) {
977 serv_getln(buf, sizeof buf);
992 * \brief Entry point for WebCit transaction
994 void session_loop(struct httprequest *req)
1002 char request_method[128];
1003 char pathname[1024];
1004 int a, b, nBackDots, nEmpty;
1005 int ContentLength = 0;
1007 char ContentType[512];
1008 char *content = NULL;
1009 char *content_end = NULL;
1010 struct httprequest *hptr;
1011 char browser_host[256];
1012 char user_agent[256];
1017 * We stuff these with the values coming from the client cookies,
1018 * so we can use them to reconnect a timed out session if we have to.
1020 char c_username[SIZ];
1021 char c_password[SIZ];
1022 char c_roomname[SIZ];
1023 char c_httpauth_string[SIZ];
1024 char c_httpauth_user[SIZ];
1025 char c_httpauth_pass[SIZ];
1028 safestrncpy(c_username, "", sizeof c_username);
1029 safestrncpy(c_password, "", sizeof c_password);
1030 safestrncpy(c_roomname, "", sizeof c_roomname);
1031 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1032 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1033 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1034 strcpy(browser_host, "");
1036 WC->upload_length = 0;
1042 if (hptr == NULL) return;
1044 safestrncpy(cmd, hptr->line, sizeof cmd);
1046 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1047 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1049 /** Figure out the action */
1051 sizes[0] = sizeof action;
1054 index[a] = arg[a-1];
1055 sizes[a] = sizeof arg[a-1];
1057 //// index[9] = &foo; todo
1060 for ( a = 0; a < 9; ++a)
1062 extract_token(index[a], pathname, a + 1, '/', sizes[a]);
1063 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
1064 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
1065 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
1066 if ((index[a][0] == '.') && (index[a][1] == '.'))
1068 if (index[a][0] == '\0')
1072 while (hptr != NULL) {
1073 safestrncpy(buf, hptr->line, sizeof buf);
1074 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1077 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1078 safestrncpy(cookie, &buf[15], sizeof cookie);
1079 cookie_to_stuff(cookie, NULL,
1080 c_username, sizeof c_username,
1081 c_password, sizeof c_password,
1082 c_roomname, sizeof c_roomname);
1084 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1085 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1086 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1087 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1089 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1090 ContentLength = atoi(&buf[16]);
1092 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1093 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1095 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1096 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1098 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1100 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1103 else if (!strncasecmp(buf, "Host: ", 6)) {
1104 if (strlen(WC->http_host) == 0) {
1105 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1108 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1109 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1110 while (num_tokens(browser_host, ',') > 1) {
1111 remove_token(browser_host, 0, ',');
1113 striplt(browser_host);
1115 /** Only WAP gateways explicitly name this content-type */
1116 else if (strstr(buf, "text/vnd.wap.wml")) {
1121 if (ContentLength > 0) {
1122 content = malloc(ContentLength + SIZ);
1123 memset(content, 0, ContentLength + SIZ);
1124 sprintf(content, "Content-type: %s\n"
1125 "Content-length: %d\n\n",
1126 ContentType, ContentLength);
1127 body_start = strlen(content);
1129 /** Read the entire input data at once. */
1130 client_read(WC->http_sock, &content[BytesRead+body_start],
1133 if (!strncasecmp(ContentType,
1134 "application/x-www-form-urlencoded", 33)) {
1135 addurls(&content[body_start]);
1136 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1137 content_end = content + ContentLength + body_start;
1138 mime_parser(content, content_end, *upload_handler,
1139 NULL, NULL, NULL, 0);
1145 /** make a note of where we are in case the user wants to save it */
1146 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1147 remove_token(WC->this_page, 2, ' ');
1148 remove_token(WC->this_page, 0, ' ');
1150 /** If there are variables in the URL, we must grab them now */
1151 for (a = 0; a < strlen(cmd); ++a) {
1152 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1153 for (b = a; b < strlen(cmd); ++b)
1154 if (isspace(cmd[b]))
1156 addurls(&cmd[a + 1]);
1161 /** If it's a "force 404" situation then display the error and bail. */
1162 if (!strcmp(action, "404")) {
1163 wprintf("HTTP/1.1 404 Not found\r\n");
1164 wprintf("Content-Type: text/plain\r\n");
1166 wprintf("Not found\r\n");
1167 goto SKIP_ALL_THIS_CRAP;
1170 /** Static content can be sent without connecting to Citadel. */
1172 for (a=0; a<ndirs; ++a) {
1173 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1181 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1182 static_dirs[n_static],
1183 index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
1184 for (a=0; a<8; ++a) {
1185 if (buf[strlen(buf)-1] == '/') {
1186 buf[strlen(buf)-1] = 0;
1189 for (a = 0; a < strlen(buf); ++a) {
1190 if (isspace(buf[a])) {
1198 lprintf(9, "Suspicious request. Ignoring.");
1199 wprintf("HTTP/1.1 404 Not found. Don't try to Trick me DUDE!\r\n");
1200 wprintf("Content-Type: text/plain\r\n");
1202 wprintf("Not found. Don't play games on me!\r\n");
1204 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1208 * If we're not connected to a Citadel server, try to hook up the
1211 if (!WC->connected) {
1212 if (!strcasecmp(ctdlhost, "uds")) {
1213 /* unix domain socket */
1214 sprintf(buf, "%s/citadel.socket", ctdlport);
1215 WC->serv_sock = uds_connectsock(buf);
1219 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1222 if (WC->serv_sock < 0) {
1224 goto SKIP_ALL_THIS_CRAP;
1228 serv_getln(buf, sizeof buf); /** get the server welcome message */
1231 * From what host is our user connecting? Go with
1232 * the host at the other end of the HTTP socket,
1233 * unless we are following X-Forwarded-For: headers
1234 * and such a header has already turned up something.
1236 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1237 locate_host(browser_host, WC->http_sock);
1240 get_serv_info(browser_host, user_agent);
1241 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1242 wprintf(_("You are connected to a Citadel "
1243 "server running Citadel %d.%02d. \n"
1244 "In order to run this version of WebCit "
1245 "you must also have Citadel %d.%02d or"
1247 serv_info.serv_rev_level / 100,
1248 serv_info.serv_rev_level % 100,
1249 MINIMUM_CIT_VERSION / 100,
1250 MINIMUM_CIT_VERSION % 100
1252 end_webcit_session();
1253 goto SKIP_ALL_THIS_CRAP;
1259 * Functions which can be performed without logging in
1261 if (!strcasecmp(action, "listsub")) {
1263 goto SKIP_ALL_THIS_CRAP;
1265 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1266 if (!strcasecmp(action, "freebusy")) {
1268 goto SKIP_ALL_THIS_CRAP;
1273 * If we're not logged in, but we have HTTP Authentication data,
1274 * try logging in to Citadel using that.
1276 if ((!WC->logged_in)
1277 && (strlen(c_httpauth_user) > 0)
1278 && (strlen(c_httpauth_pass) > 0)) {
1279 serv_printf("USER %s", c_httpauth_user);
1280 serv_getln(buf, sizeof buf);
1281 if (buf[0] == '3') {
1282 serv_printf("PASS %s", c_httpauth_pass);
1283 serv_getln(buf, sizeof buf);
1284 if (buf[0] == '2') {
1285 become_logged_in(c_httpauth_user,
1286 c_httpauth_pass, buf);
1287 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1288 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1290 /** Should only display when password is wrong */
1291 authorization_required(&buf[4]);
1292 goto SKIP_ALL_THIS_CRAP;
1297 /** This needs to run early */
1298 if (!strcasecmp(action, "rss")) {
1299 display_rss(bstr("room"), request_method);
1300 goto SKIP_ALL_THIS_CRAP;
1304 * The GroupDAV stuff relies on HTTP authentication instead of
1305 * our session's authentication.
1307 if (!strncasecmp(action, "groupdav", 8)) {
1308 groupdav_main(req, ContentType, /* do GroupDAV methods */
1309 ContentLength, content+body_start);
1310 if (!WC->logged_in) {
1311 WC->killthis = 1; /* If not logged in, don't */
1312 } /* keep the session active */
1313 goto SKIP_ALL_THIS_CRAP;
1318 * Automatically send requests with any method other than GET or
1319 * POST to the GroupDAV code as well.
1321 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1322 groupdav_main(req, ContentType, /** do GroupDAV methods */
1323 ContentLength, content+body_start);
1324 if (!WC->logged_in) {
1325 WC->killthis = 1; /** If not logged in, don't */
1326 } /** keep the session active */
1327 goto SKIP_ALL_THIS_CRAP;
1331 * If we're not logged in, but we have username and password cookies
1332 * supplied by the browser, try using them to log in.
1334 if ((!WC->logged_in)
1335 && (strlen(c_username) > 0)
1336 && (strlen(c_password) > 0)) {
1337 serv_printf("USER %s", c_username);
1338 serv_getln(buf, sizeof buf);
1339 if (buf[0] == '3') {
1340 serv_printf("PASS %s", c_password);
1341 serv_getln(buf, sizeof buf);
1342 if (buf[0] == '2') {
1343 become_logged_in(c_username, c_password, buf);
1348 * If we don't have a current room, but a cookie specifying the
1349 * current room is supplied, make an effort to go there.
1351 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1352 serv_printf("GOTO %s", c_roomname);
1353 serv_getln(buf, sizeof buf);
1354 if (buf[0] == '2') {
1355 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1359 if (!strcasecmp(action, "image")) {
1363 * All functions handled below this point ... make sure we log in
1364 * before doing anything else!
1366 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1368 } else if (!WC->logged_in) {
1369 display_login(NULL);
1373 * Various commands...
1376 else if (!strcasecmp(action, "do_welcome")) {
1378 } else if (!strcasecmp(action, "blank")) {
1380 } else if (!strcasecmp(action, "do_template")) {
1382 } else if (!strcasecmp(action, "display_aide_menu")) {
1383 display_aide_menu();
1384 } else if (!strcasecmp(action, "display_main_menu")) {
1385 display_main_menu();
1386 } else if (!strcasecmp(action, "who")) {
1388 } else if (!strcasecmp(action, "sslg")) {
1389 seconds_since_last_gexp();
1390 } else if (!strcasecmp(action, "who_inner_html")) {
1391 begin_ajax_response();
1393 end_ajax_response();
1394 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1395 begin_ajax_response();
1397 end_ajax_response();
1398 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1399 begin_ajax_response();
1400 do_iconbar_roomlist();
1401 end_ajax_response();
1402 } else if (!strcasecmp(action, "knrooms")) {
1404 } else if (!strcasecmp(action, "gotonext")) {
1407 } else if (!strcasecmp(action, "skip")) {
1409 } else if (!strcasecmp(action, "ungoto")) {
1411 } else if (!strcasecmp(action, "dotgoto")) {
1412 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1415 smart_goto(bstr("room"));
1416 } else if (!strcasecmp(action, "dotskip")) {
1417 smart_goto(bstr("room"));
1418 } else if (!strcasecmp(action, "termquit")) {
1420 } else if (!strcasecmp(action, "readnew")) {
1421 readloop("readnew");
1422 } else if (!strcasecmp(action, "readold")) {
1423 readloop("readold");
1424 } else if (!strcasecmp(action, "readfwd")) {
1425 readloop("readfwd");
1426 } else if (!strcasecmp(action, "headers")) {
1427 readloop("headers");
1428 } else if (!strcasecmp(action, "do_search")) {
1429 readloop("do_search");
1430 } else if (!strcasecmp(action, "msg")) {
1431 embed_message(index[1]);
1432 } else if (!strcasecmp(action, "printmsg")) {
1433 print_message(index[1]);
1434 } else if (!strcasecmp(action, "msgheaders")) {
1435 display_headers(index[1]);
1436 } else if (!strcasecmp(action, "wiki")) {
1437 display_wiki_page();
1438 } else if (!strcasecmp(action, "display_enter")) {
1440 } else if (!strcasecmp(action, "post")) {
1442 } else if (!strcasecmp(action, "move_msg")) {
1444 } else if (!strcasecmp(action, "delete_msg")) {
1446 } else if (!strcasecmp(action, "userlist")) {
1448 } else if (!strcasecmp(action, "showuser")) {
1450 } else if (!strcasecmp(action, "display_page")) {
1452 } else if (!strcasecmp(action, "page_user")) {
1454 } else if (!strcasecmp(action, "chat")) {
1456 } else if (!strcasecmp(action, "display_private")) {
1457 display_private("", 0);
1458 } else if (!strcasecmp(action, "goto_private")) {
1460 } else if (!strcasecmp(action, "zapped_list")) {
1462 } else if (!strcasecmp(action, "display_zap")) {
1464 } else if (!strcasecmp(action, "zap")) {
1466 } else if (!strcasecmp(action, "display_entroom")) {
1468 } else if (!strcasecmp(action, "entroom")) {
1470 } else if (!strcasecmp(action, "display_whok")) {
1472 } else if (!strcasecmp(action, "do_invt_kick")) {
1474 } else if (!strcasecmp(action, "display_editroom")) {
1476 } else if (!strcasecmp(action, "netedit")) {
1478 } else if (!strcasecmp(action, "editroom")) {
1480 } else if (!strcasecmp(action, "display_editinfo")) {
1481 display_edit(_("Room info"), "EINF 0", "RINF", "editinfo", 1);
1482 } else if (!strcasecmp(action, "editinfo")) {
1483 save_edit(_("Room info"), "EINF 1", 1);
1484 } else if (!strcasecmp(action, "display_editbio")) {
1485 sprintf(buf, "RBIO %s", WC->wc_fullname);
1486 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1487 } else if (!strcasecmp(action, "editbio")) {
1488 save_edit(_("Your bio"), "EBIO", 0);
1489 } else if (!strcasecmp(action, "confirm_move_msg")) {
1491 } else if (!strcasecmp(action, "delete_room")) {
1493 } else if (!strcasecmp(action, "validate")) {
1495 } else if (!strcasecmp(action, "display_editpic")) {
1496 display_graphics_upload(_("your photo"),
1499 } else if (!strcasecmp(action, "editpic")) {
1500 do_graphics_upload("UIMG 1|_userpic_");
1501 } else if (!strcasecmp(action, "display_editroompic")) {
1502 display_graphics_upload(_("the icon for this room"),
1505 } else if (!strcasecmp(action, "editroompic")) {
1506 do_graphics_upload("UIMG 1|_roompic_");
1507 } else if (!strcasecmp(action, "delete_floor")) {
1509 } else if (!strcasecmp(action, "rename_floor")) {
1511 } else if (!strcasecmp(action, "create_floor")) {
1513 } else if (!strcasecmp(action, "display_editfloorpic")) {
1514 sprintf(buf, "UIMG 0|_floorpic_|%s",
1515 bstr("which_floor"));
1516 display_graphics_upload(_("the icon for this floor"),
1519 } else if (!strcasecmp(action, "editfloorpic")) {
1520 sprintf(buf, "UIMG 1|_floorpic_|%s",
1521 bstr("which_floor"));
1522 do_graphics_upload(buf);
1523 } else if (!strcasecmp(action, "display_reg")) {
1525 } else if (!strcasecmp(action, "display_changepw")) {
1527 } else if (!strcasecmp(action, "changepw")) {
1529 } else if (!strcasecmp(action, "display_edit_node")) {
1530 display_edit_node();
1531 } else if (!strcasecmp(action, "edit_node")) {
1533 } else if (!strcasecmp(action, "display_netconf")) {
1535 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1536 display_confirm_delete_node();
1537 } else if (!strcasecmp(action, "delete_node")) {
1539 } else if (!strcasecmp(action, "display_add_node")) {
1541 } else if (!strcasecmp(action, "add_node")) {
1543 } else if (!strcasecmp(action, "terminate_session")) {
1545 terminate_session();
1546 } else if (!strcasecmp(action, "edit_me")) {
1548 } else if (!strcasecmp(action, "display_siteconfig")) {
1549 display_siteconfig();
1550 } else if (!strcasecmp(action, "chat_recv")) {
1552 } else if (!strcasecmp(action, "chat_send")) {
1554 } else if (!strcasecmp(action, "siteconfig")) {
1556 } else if (!strcasecmp(action, "display_generic")) {
1558 } else if (!strcasecmp(action, "do_generic")) {
1560 } else if (!strcasecmp(action, "ajax_servcmd")) {
1562 } else if (!strcasecmp(action, "display_menubar")) {
1564 } else if (!strcasecmp(action, "mimepart")) {
1565 mimepart(index[1], index[2], 0);
1566 } else if (!strcasecmp(action, "mimepart_download")) {
1567 mimepart(index[1], index[2], 1);
1568 } else if (!strcasecmp(action, "edit_vcard")) {
1570 } else if (!strcasecmp(action, "submit_vcard")) {
1572 } else if (!strcasecmp(action, "select_user_to_edit")) {
1573 select_user_to_edit(NULL, NULL);
1574 } else if (!strcasecmp(action, "display_edituser")) {
1575 display_edituser(NULL, 0);
1576 } else if (!strcasecmp(action, "edituser")) {
1578 } else if (!strcasecmp(action, "create_user")) {
1580 } else if (!strcasecmp(action, "changeview")) {
1582 } else if (!strcasecmp(action, "change_start_page")) {
1583 change_start_page();
1584 } else if (!strcasecmp(action, "display_floorconfig")) {
1585 display_floorconfig(NULL);
1586 } else if (!strcasecmp(action, "toggle_self_service")) {
1587 toggle_self_service();
1588 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1589 } else if (!strcasecmp(action, "display_edit_task")) {
1590 display_edit_task();
1591 } else if (!strcasecmp(action, "save_task")) {
1593 } else if (!strcasecmp(action, "display_edit_event")) {
1594 display_edit_event();
1595 } else if (!strcasecmp(action, "save_event")) {
1597 } else if (!strcasecmp(action, "respond_to_request")) {
1598 respond_to_request();
1599 } else if (!strcasecmp(action, "handle_rsvp")) {
1602 } else if (!strcasecmp(action, "summary")) {
1604 } else if (!strcasecmp(action, "summary_inner_div")) {
1605 begin_ajax_response();
1606 summary_inner_div();
1607 end_ajax_response();
1608 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1609 display_customize_iconbar();
1610 } else if (!strcasecmp(action, "commit_iconbar")) {
1612 } else if (!strcasecmp(action, "set_room_policy")) {
1614 } else if (!strcasecmp(action, "display_inetconf")) {
1616 } else if (!strcasecmp(action, "save_inetconf")) {
1618 } else if (!strcasecmp(action, "display_smtpqueue")) {
1619 display_smtpqueue();
1620 } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1621 display_smtpqueue_inner_div();
1622 } else if (!strcasecmp(action, "display_sieve")) {
1624 } else if (!strcasecmp(action, "save_sieve")) {
1626 } else if (!strcasecmp(action, "display_add_remove_scripts")) {
1627 display_add_remove_scripts(NULL);
1628 } else if (!strcasecmp(action, "create_script")) {
1630 } else if (!strcasecmp(action, "delete_script")) {
1632 } else if (!strcasecmp(action, "setup_wizard")) {
1634 } else if (!strcasecmp(action, "display_preferences")) {
1635 display_preferences();
1636 } else if (!strcasecmp(action, "set_preferences")) {
1638 } else if (!strcasecmp(action, "recp_autocomplete")) {
1639 recp_autocomplete(bstr("recp"));
1640 } else if (!strcasecmp(action, "cc_autocomplete")) {
1641 recp_autocomplete(bstr("cc"));
1642 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1643 recp_autocomplete(bstr("bcc"));
1644 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1645 set_floordiv_expanded(index[1]);
1646 } else if (!strcasecmp(action, "diagnostics")) {
1647 output_headers(1, 1, 1, 0, 0, 0);
1648 wprintf("Session: %d<hr />\n", WC->wc_session);
1649 wprintf("Command: <br /><PRE>\n");
1651 wprintf("</PRE><hr />\n");
1652 wprintf("Variables: <br /><PRE>\n");
1654 wprintf("</PRE><hr />\n");
1656 } else if (!strcasecmp(action, "updatenote")) {
1658 } else if (!strcasecmp(action, "display_room_directory")) {
1659 display_room_directory();
1660 } else if (!strcasecmp(action, "download_file")) {
1661 download_file(index[1]);
1662 } else if (!strcasecmp(action, "upload_file")) {
1666 /** When all else fais, display the main menu. */
1668 display_main_menu();
1673 if (content != NULL) {
1678 if (WC->upload_length > 0) {
1680 WC->upload_length = 0;