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);
767 wprintf(_("Make this my start page"));
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", name, cbtype, length);
854 if ( (length > 0) && (strlen(cbtype) == 0) ) {
855 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
856 u->next = WC->urlstrings;
858 safestrncpy(u->url_key, name, sizeof(u->url_key));
859 u->url_data = malloc(length + 1);
860 memcpy(u->url_data, content, length);
861 u->url_data[length] = 0;
864 /** Uploaded files */
865 if ( (length > 0) && (strlen(cbtype) > 0) ) {
866 WC->upload = malloc(length);
867 if (WC->upload != NULL) {
868 WC->upload_length = length;
869 safestrncpy(WC->upload_filename, filename,
870 sizeof(WC->upload_filename));
871 safestrncpy(WC->upload_content_type, cbtype,
872 sizeof(WC->upload_content_type));
873 memcpy(WC->upload, content, length);
876 lprintf(3, "malloc() failed: %s\n", strerror(errno));
883 * \brief Convenience functions to wrap around asynchronous ajax responses
885 void begin_ajax_response(void) {
886 output_headers(0, 0, 0, 0, 0, 0);
888 wprintf("Content-type: text/html; charset=UTF-8\r\n"
890 "Connection: close\r\n"
891 "Pragma: no-cache\r\n"
892 "Cache-Control: no-cache\r\n",
898 * \brief print ajax response footer
900 void end_ajax_response(void) {
906 * \brief Wraps a Citadel server command in an AJAX transaction.
908 void ajax_servcmd(void)
915 begin_ajax_response();
917 serv_printf("%s", bstr("g_cmd"));
918 serv_getln(buf, sizeof buf);
919 wprintf("%s\n", buf);
922 serv_printf("\n\n000");
924 if ((buf[0] == '1') || (buf[0] == '8')) {
925 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
926 wprintf("%s\n", gcontent);
931 text_to_server(bstr("g_input"));
937 serv_read(junk, len);
943 memset(junk, 0, len);
944 serv_write(junk, len);
951 * This is kind of an ugly hack, but this is the only place it can go.
952 * If the command was GEXP, then the instant messenger window must be
953 * running, so reset the "last_pager_check" watchdog timer so
954 * that page_popup() doesn't try to open it a second time.
956 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
957 WC->last_pager_check = time(NULL);
963 * \brief Helper function for the asynchronous check to see if we need
964 * to open the instant messenger window.
966 void seconds_since_last_gexp(void)
970 begin_ajax_response();
971 if ( (time(NULL) - WC->last_pager_check) < 30) {
976 serv_getln(buf, sizeof buf);
991 * \brief Entry point for WebCit transaction
993 void session_loop(struct httprequest *req)
1001 char request_method[128];
1002 char pathname[1024];
1003 int a, b, nBackDots, nEmpty;
1004 int ContentLength = 0;
1006 char ContentType[512];
1007 char *content = NULL;
1008 char *content_end = NULL;
1009 struct httprequest *hptr;
1010 char browser_host[256];
1011 char user_agent[256];
1016 * We stuff these with the values coming from the client cookies,
1017 * so we can use them to reconnect a timed out session if we have to.
1019 char c_username[SIZ];
1020 char c_password[SIZ];
1021 char c_roomname[SIZ];
1022 char c_httpauth_string[SIZ];
1023 char c_httpauth_user[SIZ];
1024 char c_httpauth_pass[SIZ];
1027 safestrncpy(c_username, "", sizeof c_username);
1028 safestrncpy(c_password, "", sizeof c_password);
1029 safestrncpy(c_roomname, "", sizeof c_roomname);
1030 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1031 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1032 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1033 strcpy(browser_host, "");
1035 WC->upload_length = 0;
1041 if (hptr == NULL) return;
1043 safestrncpy(cmd, hptr->line, sizeof cmd);
1045 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1046 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1048 /** Figure out the action */
1050 sizes[0] = sizeof action;
1053 index[a] = arg[a-1];
1054 sizes[a] = sizeof arg[a-1];
1056 //// index[9] = &foo; todo
1059 for ( a = 0; a < 9; ++a)
1061 extract_token(index[a], pathname, a + 1, '/', sizes[a]);
1062 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
1063 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
1064 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
1065 if ((index[a][0] == '.') && (index[a][1] == '.'))
1067 if (index[a][0] == '\0')
1071 while (hptr != NULL) {
1072 safestrncpy(buf, hptr->line, sizeof buf);
1073 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1076 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1077 safestrncpy(cookie, &buf[15], sizeof cookie);
1078 cookie_to_stuff(cookie, NULL,
1079 c_username, sizeof c_username,
1080 c_password, sizeof c_password,
1081 c_roomname, sizeof c_roomname);
1083 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1084 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1085 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1086 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1088 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1089 ContentLength = atoi(&buf[16]);
1091 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1092 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1094 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1095 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1097 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1099 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1102 else if (!strncasecmp(buf, "Host: ", 6)) {
1103 if (strlen(WC->http_host) == 0) {
1104 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1107 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1108 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1109 while (num_tokens(browser_host, ',') > 1) {
1110 remove_token(browser_host, 0, ',');
1112 striplt(browser_host);
1114 /** Only WAP gateways explicitly name this content-type */
1115 else if (strstr(buf, "text/vnd.wap.wml")) {
1120 if (ContentLength > 0) {
1121 content = malloc(ContentLength + SIZ);
1122 memset(content, 0, ContentLength + SIZ);
1123 sprintf(content, "Content-type: %s\n"
1124 "Content-length: %d\n\n",
1125 ContentType, ContentLength);
1126 body_start = strlen(content);
1128 /** Read the entire input data at once. */
1129 client_read(WC->http_sock, &content[BytesRead+body_start], ContentLength);
1131 if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) {
1132 addurls(&content[body_start]);
1133 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1134 content_end = content + ContentLength + body_start;
1135 mime_parser(content, content_end, *upload_handler, NULL, NULL, NULL, 0);
1141 /** make a note of where we are in case the user wants to save it */
1142 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1143 remove_token(WC->this_page, 2, ' ');
1144 remove_token(WC->this_page, 0, ' ');
1146 /** If there are variables in the URL, we must grab them now */
1147 for (a = 0; a < strlen(cmd); ++a) {
1148 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1149 for (b = a; b < strlen(cmd); ++b)
1150 if (isspace(cmd[b]))
1152 addurls(&cmd[a + 1]);
1157 /** If it's a "force 404" situation then display the error and bail. */
1158 if (!strcmp(action, "404")) {
1159 wprintf("HTTP/1.1 404 Not found\r\n");
1160 wprintf("Content-Type: text/plain\r\n");
1162 wprintf("Not found\r\n");
1163 goto SKIP_ALL_THIS_CRAP;
1166 /** Static content can be sent without connecting to Citadel. */
1168 for (a=0; a<ndirs; ++a) {
1169 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1177 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1178 static_dirs[n_static],
1179 index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
1180 for (a=0; a<8; ++a) {
1181 if (buf[strlen(buf)-1] == '/') {
1182 buf[strlen(buf)-1] = 0;
1185 for (a = 0; a < strlen(buf); ++a) {
1186 if (isspace(buf[a])) {
1194 lprintf(9, "Suspicious request. Ignoring.");
1195 wprintf("HTTP/1.1 404 Not found. Don't try to Trick me DUDE!\r\n");
1196 wprintf("Content-Type: text/plain\r\n");
1198 wprintf("Not found. Don't play games on me!\r\n");
1200 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1204 * If we're not connected to a Citadel server, try to hook up the
1207 if (!WC->connected) {
1208 if (!strcasecmp(ctdlhost, "uds")) {
1209 /* unix domain socket */
1210 sprintf(buf, "%s/citadel.socket", ctdlport);
1211 WC->serv_sock = uds_connectsock(buf);
1215 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1218 if (WC->serv_sock < 0) {
1220 goto SKIP_ALL_THIS_CRAP;
1224 serv_getln(buf, sizeof buf); /** get the server welcome message */
1227 * From what host is our user connecting? Go with
1228 * the host at the other end of the HTTP socket,
1229 * unless we are following X-Forwarded-For: headers
1230 * and such a header has already turned up something.
1232 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1233 locate_host(browser_host, WC->http_sock);
1236 get_serv_info(browser_host, user_agent);
1237 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1238 wprintf(_("You are connected to a Citadel "
1239 "server running Citadel %d.%02d. \n"
1240 "In order to run this version of WebCit "
1241 "you must also have Citadel %d.%02d or"
1243 serv_info.serv_rev_level / 100,
1244 serv_info.serv_rev_level % 100,
1245 MINIMUM_CIT_VERSION / 100,
1246 MINIMUM_CIT_VERSION % 100
1248 end_webcit_session();
1249 goto SKIP_ALL_THIS_CRAP;
1255 * Functions which can be performed without logging in
1257 if (!strcasecmp(action, "listsub")) {
1259 goto SKIP_ALL_THIS_CRAP;
1261 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1262 if (!strcasecmp(action, "freebusy")) {
1264 goto SKIP_ALL_THIS_CRAP;
1269 * If we're not logged in, but we have HTTP Authentication data,
1270 * try logging in to Citadel using that.
1272 if ((!WC->logged_in)
1273 && (strlen(c_httpauth_user) > 0)
1274 && (strlen(c_httpauth_pass) > 0)) {
1275 serv_printf("USER %s", c_httpauth_user);
1276 serv_getln(buf, sizeof buf);
1277 if (buf[0] == '3') {
1278 serv_printf("PASS %s", c_httpauth_pass);
1279 serv_getln(buf, sizeof buf);
1280 if (buf[0] == '2') {
1281 become_logged_in(c_httpauth_user,
1282 c_httpauth_pass, buf);
1283 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1284 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1286 /** Should only display when password is wrong */
1287 authorization_required(&buf[4]);
1288 goto SKIP_ALL_THIS_CRAP;
1293 /** This needs to run early */
1294 if (!strcasecmp(action, "rss")) {
1295 display_rss(bstr("room"), request_method);
1296 goto SKIP_ALL_THIS_CRAP;
1300 * The GroupDAV stuff relies on HTTP authentication instead of
1301 * our session's authentication.
1303 if (!strncasecmp(action, "groupdav", 8)) {
1304 groupdav_main(req, ContentType, /* do GroupDAV methods */
1305 ContentLength, content+body_start);
1306 if (!WC->logged_in) {
1307 WC->killthis = 1; /* If not logged in, don't */
1308 } /* keep the session active */
1309 goto SKIP_ALL_THIS_CRAP;
1314 * Automatically send requests with any method other than GET or
1315 * POST to the GroupDAV code as well.
1317 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1318 groupdav_main(req, ContentType, /** do GroupDAV methods */
1319 ContentLength, content+body_start);
1320 if (!WC->logged_in) {
1321 WC->killthis = 1; /** If not logged in, don't */
1322 } /** keep the session active */
1323 goto SKIP_ALL_THIS_CRAP;
1327 * If we're not logged in, but we have username and password cookies
1328 * supplied by the browser, try using them to log in.
1330 if ((!WC->logged_in)
1331 && (strlen(c_username) > 0)
1332 && (strlen(c_password) > 0)) {
1333 serv_printf("USER %s", c_username);
1334 serv_getln(buf, sizeof buf);
1335 if (buf[0] == '3') {
1336 serv_printf("PASS %s", c_password);
1337 serv_getln(buf, sizeof buf);
1338 if (buf[0] == '2') {
1339 become_logged_in(c_username, c_password, buf);
1344 * If we don't have a current room, but a cookie specifying the
1345 * current room is supplied, make an effort to go there.
1347 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1348 serv_printf("GOTO %s", c_roomname);
1349 serv_getln(buf, sizeof buf);
1350 if (buf[0] == '2') {
1351 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1355 if (!strcasecmp(action, "image")) {
1359 * All functions handled below this point ... make sure we log in
1360 * before doing anything else!
1362 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1364 } else if (!WC->logged_in) {
1365 display_login(NULL);
1369 * Various commands...
1372 else if (!strcasecmp(action, "do_welcome")) {
1374 } else if (!strcasecmp(action, "blank")) {
1376 } else if (!strcasecmp(action, "do_template")) {
1378 } else if (!strcasecmp(action, "display_aide_menu")) {
1379 display_aide_menu();
1380 } else if (!strcasecmp(action, "display_main_menu")) {
1381 display_main_menu();
1382 } else if (!strcasecmp(action, "who")) {
1384 } else if (!strcasecmp(action, "sslg")) {
1385 seconds_since_last_gexp();
1386 } else if (!strcasecmp(action, "who_inner_html")) {
1387 begin_ajax_response();
1389 end_ajax_response();
1390 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1391 begin_ajax_response();
1393 end_ajax_response();
1394 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1395 begin_ajax_response();
1396 do_iconbar_roomlist();
1397 end_ajax_response();
1398 } else if (!strcasecmp(action, "knrooms")) {
1400 } else if (!strcasecmp(action, "gotonext")) {
1403 } else if (!strcasecmp(action, "skip")) {
1405 } else if (!strcasecmp(action, "ungoto")) {
1407 } else if (!strcasecmp(action, "dotgoto")) {
1408 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1411 smart_goto(bstr("room"));
1412 } else if (!strcasecmp(action, "dotskip")) {
1413 smart_goto(bstr("room"));
1414 } else if (!strcasecmp(action, "termquit")) {
1416 } else if (!strcasecmp(action, "readnew")) {
1417 readloop("readnew");
1418 } else if (!strcasecmp(action, "readold")) {
1419 readloop("readold");
1420 } else if (!strcasecmp(action, "readfwd")) {
1421 readloop("readfwd");
1422 } else if (!strcasecmp(action, "headers")) {
1423 readloop("headers");
1424 } else if (!strcasecmp(action, "do_search")) {
1425 readloop("do_search");
1426 } else if (!strcasecmp(action, "msg")) {
1427 embed_message(index[1]);
1428 } else if (!strcasecmp(action, "printmsg")) {
1429 print_message(index[1]);
1430 } else if (!strcasecmp(action, "msgheaders")) {
1431 display_headers(index[1]);
1432 } else if (!strcasecmp(action, "wiki")) {
1433 display_wiki_page();
1434 } else if (!strcasecmp(action, "display_enter")) {
1436 } else if (!strcasecmp(action, "post")) {
1438 } else if (!strcasecmp(action, "move_msg")) {
1440 } else if (!strcasecmp(action, "delete_msg")) {
1442 } else if (!strcasecmp(action, "userlist")) {
1444 } else if (!strcasecmp(action, "showuser")) {
1446 } else if (!strcasecmp(action, "display_page")) {
1448 } else if (!strcasecmp(action, "page_user")) {
1450 } else if (!strcasecmp(action, "chat")) {
1452 } else if (!strcasecmp(action, "display_private")) {
1453 display_private("", 0);
1454 } else if (!strcasecmp(action, "goto_private")) {
1456 } else if (!strcasecmp(action, "zapped_list")) {
1458 } else if (!strcasecmp(action, "display_zap")) {
1460 } else if (!strcasecmp(action, "zap")) {
1462 } else if (!strcasecmp(action, "display_entroom")) {
1464 } else if (!strcasecmp(action, "entroom")) {
1466 } else if (!strcasecmp(action, "display_whok")) {
1468 } else if (!strcasecmp(action, "do_invt_kick")) {
1470 } else if (!strcasecmp(action, "display_editroom")) {
1472 } else if (!strcasecmp(action, "netedit")) {
1474 } else if (!strcasecmp(action, "editroom")) {
1476 } else if (!strcasecmp(action, "display_editinfo")) {
1477 display_edit(_("Room info"), "EINF 0", "RINF", "editinfo", 1);
1478 } else if (!strcasecmp(action, "editinfo")) {
1479 save_edit(_("Room info"), "EINF 1", 1);
1480 } else if (!strcasecmp(action, "display_editbio")) {
1481 sprintf(buf, "RBIO %s", WC->wc_fullname);
1482 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1483 } else if (!strcasecmp(action, "editbio")) {
1484 save_edit(_("Your bio"), "EBIO", 0);
1485 } else if (!strcasecmp(action, "confirm_move_msg")) {
1487 } else if (!strcasecmp(action, "delete_room")) {
1489 } else if (!strcasecmp(action, "validate")) {
1491 } else if (!strcasecmp(action, "display_editpic")) {
1492 display_graphics_upload(_("your photo"),
1495 } else if (!strcasecmp(action, "editpic")) {
1496 do_graphics_upload("UIMG 1|_userpic_");
1497 } else if (!strcasecmp(action, "display_editroompic")) {
1498 display_graphics_upload(_("the icon for this room"),
1501 } else if (!strcasecmp(action, "editroompic")) {
1502 do_graphics_upload("UIMG 1|_roompic_");
1503 } else if (!strcasecmp(action, "delete_floor")) {
1505 } else if (!strcasecmp(action, "rename_floor")) {
1507 } else if (!strcasecmp(action, "create_floor")) {
1509 } else if (!strcasecmp(action, "display_editfloorpic")) {
1510 sprintf(buf, "UIMG 0|_floorpic_|%s",
1511 bstr("which_floor"));
1512 display_graphics_upload(_("the icon for this floor"),
1515 } else if (!strcasecmp(action, "editfloorpic")) {
1516 sprintf(buf, "UIMG 1|_floorpic_|%s",
1517 bstr("which_floor"));
1518 do_graphics_upload(buf);
1519 } else if (!strcasecmp(action, "display_reg")) {
1521 } else if (!strcasecmp(action, "display_changepw")) {
1523 } else if (!strcasecmp(action, "changepw")) {
1525 } else if (!strcasecmp(action, "display_edit_node")) {
1526 display_edit_node();
1527 } else if (!strcasecmp(action, "edit_node")) {
1529 } else if (!strcasecmp(action, "display_netconf")) {
1531 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1532 display_confirm_delete_node();
1533 } else if (!strcasecmp(action, "delete_node")) {
1535 } else if (!strcasecmp(action, "display_add_node")) {
1537 } else if (!strcasecmp(action, "add_node")) {
1539 } else if (!strcasecmp(action, "terminate_session")) {
1541 terminate_session();
1542 } else if (!strcasecmp(action, "edit_me")) {
1544 } else if (!strcasecmp(action, "display_siteconfig")) {
1545 display_siteconfig();
1546 } else if (!strcasecmp(action, "chat_recv")) {
1548 } else if (!strcasecmp(action, "chat_send")) {
1550 } else if (!strcasecmp(action, "siteconfig")) {
1552 } else if (!strcasecmp(action, "display_generic")) {
1554 } else if (!strcasecmp(action, "do_generic")) {
1556 } else if (!strcasecmp(action, "ajax_servcmd")) {
1558 } else if (!strcasecmp(action, "display_menubar")) {
1560 } else if (!strcasecmp(action, "mimepart")) {
1561 mimepart(index[1], index[2], 0);
1562 } else if (!strcasecmp(action, "mimepart_download")) {
1563 mimepart(index[1], index[2], 1);
1564 } else if (!strcasecmp(action, "edit_vcard")) {
1566 } else if (!strcasecmp(action, "submit_vcard")) {
1568 } else if (!strcasecmp(action, "select_user_to_edit")) {
1569 select_user_to_edit(NULL, NULL);
1570 } else if (!strcasecmp(action, "display_edituser")) {
1571 display_edituser(NULL, 0);
1572 } else if (!strcasecmp(action, "edituser")) {
1574 } else if (!strcasecmp(action, "create_user")) {
1576 } else if (!strcasecmp(action, "changeview")) {
1578 } else if (!strcasecmp(action, "change_start_page")) {
1579 change_start_page();
1580 } else if (!strcasecmp(action, "display_floorconfig")) {
1581 display_floorconfig(NULL);
1582 } else if (!strcasecmp(action, "toggle_self_service")) {
1583 toggle_self_service();
1584 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1585 } else if (!strcasecmp(action, "display_edit_task")) {
1586 display_edit_task();
1587 } else if (!strcasecmp(action, "save_task")) {
1589 } else if (!strcasecmp(action, "display_edit_event")) {
1590 display_edit_event();
1591 } else if (!strcasecmp(action, "save_event")) {
1593 } else if (!strcasecmp(action, "respond_to_request")) {
1594 respond_to_request();
1595 } else if (!strcasecmp(action, "handle_rsvp")) {
1598 } else if (!strcasecmp(action, "summary")) {
1600 } else if (!strcasecmp(action, "summary_inner_div")) {
1601 begin_ajax_response();
1602 summary_inner_div();
1603 end_ajax_response();
1604 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1605 display_customize_iconbar();
1606 } else if (!strcasecmp(action, "commit_iconbar")) {
1608 } else if (!strcasecmp(action, "set_room_policy")) {
1610 } else if (!strcasecmp(action, "display_inetconf")) {
1612 } else if (!strcasecmp(action, "save_inetconf")) {
1614 } else if (!strcasecmp(action, "display_smtpqueue")) {
1615 display_smtpqueue();
1616 } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1617 display_smtpqueue_inner_div();
1618 } else if (!strcasecmp(action, "display_sieve")) {
1620 } else if (!strcasecmp(action, "save_sieve")) {
1622 } else if (!strcasecmp(action, "display_add_remove_scripts")) {
1623 display_add_remove_scripts(NULL);
1624 } else if (!strcasecmp(action, "create_script")) {
1626 } else if (!strcasecmp(action, "delete_script")) {
1628 } else if (!strcasecmp(action, "setup_wizard")) {
1630 } else if (!strcasecmp(action, "display_preferences")) {
1631 display_preferences();
1632 } else if (!strcasecmp(action, "set_preferences")) {
1634 } else if (!strcasecmp(action, "recp_autocomplete")) {
1635 recp_autocomplete(bstr("recp"));
1636 } else if (!strcasecmp(action, "cc_autocomplete")) {
1637 recp_autocomplete(bstr("cc"));
1638 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1639 recp_autocomplete(bstr("bcc"));
1640 } else if (!strcasecmp(action, "display_address_book_inner_div")) {
1641 display_address_book_inner_div();
1642 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1643 set_floordiv_expanded(index[1]);
1644 } else if (!strcasecmp(action, "diagnostics")) {
1645 output_headers(1, 1, 1, 0, 0, 0);
1646 wprintf("Session: %d<hr />\n", WC->wc_session);
1647 wprintf("Command: <br /><PRE>\n");
1649 wprintf("</PRE><hr />\n");
1650 wprintf("Variables: <br /><PRE>\n");
1652 wprintf("</PRE><hr />\n");
1654 } else if (!strcasecmp(action, "updatenote")) {
1656 } else if (!strcasecmp(action, "display_room_directory")) {
1657 display_room_directory();
1658 } else if (!strcasecmp(action, "download_file")) {
1659 download_file(index[1]);
1660 } else if (!strcasecmp(action, "upload_file")) {
1664 /** When all else fais, display the main menu. */
1666 display_main_menu();
1671 if (content != NULL) {
1676 if (WC->upload_length > 0) {
1678 WC->upload_length = 0;