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 BackendWebServer
13 #include "webserver.h"
14 #include "mime_parser.h"
17 * Subdirectories from which the client may request static content
19 char *static_content_dirs[] = {
20 "static", /** static templates */
21 "tiny_mce" /** the JS editor */
25 * String to unset the cookie.
26 * Any date "in the past" will work, so I chose my birthday, right down to
27 * the exact minute. :)
29 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
32 * \brief remove escaped strings from i.e. the url string (like %20 for blanks)
33 * \param buf the buffer to examine
35 void unescape_input(char *buf)
40 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
41 buf[strlen(buf) - 1] = 0;
43 for (a = 0; a < strlen(buf); ++a) {
51 sscanf(hex, "%02x", &b);
53 strcpy(&buf[a + 1], &buf[a + 3]);
60 * \brief Extract variables from the URL.
61 * \param url URL supplied by the HTTP parser
63 void addurls(char *url)
71 while (strlen(up) > 0) {
73 /** locate the = sign */
74 safestrncpy(buf, up, sizeof buf);
76 for (a = 255; a >= 0; --a)
83 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
84 u->next = WC->urlstrings;
86 safestrncpy(u->url_key, buf, sizeof u->url_key);
88 /** now chop that part off */
89 for (a = 0; a <= b; ++a)
92 /** locate "&" and "?" delimiters */
95 for (a = 0; a < strlen(up); ++a) {
96 if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
103 for (a = 0; a < b; ++a)
107 u->url_data = malloc(strlen(up) + 2);
108 safestrncpy(u->url_data, up, strlen(up) + 1);
110 unescape_input(u->url_data);
117 * \brief free urlstring memory
121 struct urlcontent *u;
123 while (WC->urlstrings != NULL) {
124 free(WC->urlstrings->url_data);
125 u = WC->urlstrings->next;
126 free(WC->urlstrings);
132 * \brief Diagnostic function to display the contents of all variables
136 struct urlcontent *u;
138 for (u = WC->urlstrings; u != NULL; u = u->next) {
139 wprintf("%38s = %s\n", u->url_key, u->url_data);
144 * \brief Return the value of a variable supplied to the current web page (from the url or a form)
145 * \param key The name of the variable we want
147 char *bstr(char *key)
149 struct urlcontent *u;
151 for (u = WC->urlstrings; u != NULL; u = u->next) {
152 if (!strcasecmp(u->url_key, key))
153 return (u->url_data);
159 * \brief web-printing funcion. uses our vsnprintf wrapper
160 * \param format printf format string
161 * \param ... the varargs to put into formatstring
163 void wprintf(const char *format,...)
168 va_start(arg_ptr, format);
169 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
172 client_write(wbuf, strlen(wbuf));
177 * \brief wrap up an HTTP session, closes tags, etc.
178 * \todo multiline params?
179 * \param print_standard_html_footer should be set to 0 to transmit only, 1 to
180 * append the main menu and closing tags, or 2 to
181 * append the closing tags only.
183 void wDumpContent(int print_standard_html_footer)
185 if (print_standard_html_footer) {
186 wprintf("</div>\n"); /* end of "text" div */
187 do_template("trailing");
190 /* If we've been saving it all up for one big output burst,
191 * go ahead and do that now.
198 * \brief Copy a string, escaping characters which have meaning in HTML.
199 * \param target target buffer
200 * \param strbuf source buffer
201 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
202 * \param nolinebreaks if set, linebreaks are removed from the string.
204 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
209 for (a = 0; a < strlen(strbuf); ++a) {
210 if (strbuf[a] == '<')
211 strcat(target, "<");
212 else if (strbuf[a] == '>')
213 strcat(target, ">");
214 else if (strbuf[a] == '&')
215 strcat(target, "&");
216 else if (strbuf[a] == '\"')
217 strcat(target, """);
218 else if (strbuf[a] == '\'')
219 strcat(target, "'");
220 else if (strbuf[a] == LB)
222 else if (strbuf[a] == RB)
224 else if (strbuf[a] == QU)
225 strcat(target, "\"");
226 else if ((strbuf[a] == 32) && (nbsp == 1))
227 strcat(target, " ");
228 else if ((strbuf[a] == '\n') && (nolinebreaks))
229 strcat(target, ""); /* nothing */
230 else if ((strbuf[a] == '\r') && (nolinebreaks))
231 strcat(target, ""); /* nothing */
233 strncat(target, &strbuf[a], 1);
239 * \param strbuf what???
240 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
241 * \param nolinebreaks if set, linebreaks are removed from the string.
243 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
247 if (strbuf == NULL) return;
248 buf = malloc( (3 * strlen(strbuf)) + SIZ );
249 stresc(buf, strbuf, nbsp, nolinebreaks);
255 * \brief static wrapper for ecsputs1
256 * \param strbuf buffer to print escaped to client
258 void escputs(char *strbuf)
260 escputs1(strbuf, 0, 0);
264 * \brief Escape a string for feeding out as a URL.
265 * Returns a pointer to a buffer that must be freed by the caller!
266 * \param outbuf the output buffer
267 * \param strbuf the input buffer
269 void urlesc(char *outbuf, char *strbuf)
272 char *ec = " #&;`'|*?-~<>^()[]{}$\"\\";
276 for (a = 0; a < strlen(strbuf); ++a) {
278 for (b = 0; b < strlen(ec); ++b) {
279 if (strbuf[a] == ec[b])
284 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
286 sprintf(&outbuf[b], "%c", strbuf[a]);
291 * \brief urlescape buffer and print it to the client
292 * \param strbuf buffer to urlescape
294 void urlescputs(char *strbuf)
298 urlesc(outbuf, strbuf);
299 wprintf("%s", outbuf);
304 * \brief Copy a string, escaping characters for JavaScript strings.
305 * \param target output string
306 * \param strbuf input string
308 void jsesc(char *target, char *strbuf)
313 for (a = 0; a < strlen(strbuf); ++a) {
314 if (strbuf[a] == '<')
316 else if (strbuf[a] == '>')
318 else if (strbuf[a] == '\"')
319 strcat(target, """);
320 else if (strbuf[a] == '&')
321 strcat(target, "&;");
322 else if (strbuf[a] == '\'')
323 strcat(target, "\\'");
325 strncat(target, &strbuf[a], 1);
331 * \brief escape and print java script
332 * \param strbuf the js code
334 void jsescputs(char *strbuf)
338 jsesc(outbuf, strbuf);
339 wprintf("%s", outbuf);
343 * \brief Copy a string, escaping characters for message text hold
344 * \param target target buffer
345 * \param strbuf source buffer
347 void msgesc(char *target, char *strbuf)
352 for (a = 0; a < strlen(strbuf); ++a) {
353 if (strbuf[a] == '\n')
355 else if (strbuf[a] == '\r')
357 else if (strbuf[a] == '\'')
358 strcat(target, "'");
360 strncat(target, &strbuf[a], 1);
366 * \brief print a string to the client after cleaning it with msgesc()
367 * \param strbuf string to be printed
369 void msgescputs(char *strbuf) {
372 if (strbuf == NULL) return;
373 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
374 msgesc(outbuf, strbuf);
375 wprintf("%s", outbuf);
383 * \brief Output all that important stuff that the browser will want to see
385 void output_headers( int do_httpheaders, /**< 1 = output HTTP headers */
386 int do_htmlhead, /**< 1 = output HTML <head> section and <body> opener */
388 int do_room_banner, /**< 0=no, 1=yes,
389 * 2 = I'm going to embed my own, so don't open the
390 * <div id="content"> either.
393 int unset_cookies, /**< 1 = session is terminating, so unset the cookies */
394 int suppress_check, /**< 1 = suppress check for instant messages */
395 int cache /**< 1 = allow browser to cache this page */
400 wprintf("HTTP/1.1 200 OK\n");
401 http_datestring(httpnow, sizeof httpnow, time(NULL));
403 if (do_httpheaders) {
404 wprintf("Content-type: text/html; charset=utf-8\r\n"
406 "Connection: close\r\n",
407 SERVER, serv_info.serv_software
412 wprintf("Pragma: public\r\n"
413 "Cache-Control: max-age=3600, must-revalidate\r\n"
414 "Last-modified: %s\r\n",
419 wprintf("Pragma: no-cache\r\n"
420 "Cache-Control: no-store\r\n"
424 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
425 WC->wc_password, WC->wc_roomname);
428 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
430 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
431 if (server_cookie != NULL) {
432 wprintf("%s\n", server_cookie);
445 /** check for ImportantMessages (these display in a div overlaying the main screen) */
446 if (strlen(WC->ImportantMessage) > 0) {
447 wprintf("<div id=\"important_message\">\n");
448 wprintf("<SPAN CLASS=\"imsg\">"
449 "%s</SPAN><br />\n", WC->ImportantMessage);
451 wprintf("<script type=\"text/javascript\">\n"
452 " setTimeout('hide_imsg_popup()', 3000); \n"
454 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
457 if ( (WC->logged_in) && (!unset_cookies) ) {
458 wprintf("<div id=\"iconbar\">");
459 do_selected_iconbar();
460 /** check for instant messages (these display in a new window) */
465 if (do_room_banner == 1) {
466 wprintf("<div id=\"banner\">\n");
467 embed_room_banner(NULL, navbar_default);
472 if (do_room_banner == 1) {
473 wprintf("<div id=\"content\">\n");
479 * \brief Generic function to do an HTTP redirect. Easy and fun.
480 * \param whichpage target url to 302 to
482 void http_redirect(char *whichpage) {
483 wprintf("HTTP/1.1 302 Moved Temporarily\n");
484 wprintf("Location: %s\r\n", whichpage);
485 wprintf("URI: %s\r\n", whichpage);
486 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
487 wprintf("<html><body>");
488 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
489 wprintf("</body></html>\n");
495 * \brief Output a piece of content to the web browser
497 void http_transmit_thing(char *thing, size_t length, char *content_type,
500 output_headers(0, 0, 0, 0, 0, is_static);
502 wprintf("Content-type: %s\r\n"
504 "Connection: close\r\n",
509 /** If we can send the data out compressed, please do so. */
511 char *compressed_data = NULL;
512 uLongf compressed_len;
514 compressed_len = (uLongf) ((length * 101) / 100) + 100;
515 compressed_data = malloc(compressed_len);
517 if (compress_gzip((Bytef *) compressed_data,
520 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
521 wprintf("Content-encoding: gzip\r\n"
522 "Content-length: %ld\r\n"
524 (long) compressed_len
526 client_write(compressed_data, (size_t)compressed_len);
527 free(compressed_data);
533 /** No compression ... just send it out as-is */
534 wprintf("Content-length: %ld\r\n"
538 client_write(thing, (size_t)length);
544 * \brief dump out static pages from disk
545 * \param what the file urs to print
547 void output_static(char *what)
553 char content_type[128];
555 fp = fopen(what, "rb");
557 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
558 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
559 wprintf("Content-Type: text/plain\r\n");
561 wprintf("Cannot open %s: %s\n", what, strerror(errno));
563 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
564 safestrncpy(content_type, "image/gif", sizeof content_type);
565 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
566 safestrncpy(content_type, "text/plain", sizeof content_type);
567 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
568 safestrncpy(content_type, "text/css", sizeof content_type);
569 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
570 safestrncpy(content_type, "image/jpeg", sizeof content_type);
571 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
572 safestrncpy(content_type, "image/png", sizeof content_type);
573 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
574 safestrncpy(content_type, "image/x-icon", sizeof content_type);
575 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
576 safestrncpy(content_type, "text/html", sizeof content_type);
577 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
578 safestrncpy(content_type, "text/html", sizeof content_type);
579 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
580 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
581 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
582 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
583 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
584 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
585 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
586 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
587 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
588 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
589 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
590 safestrncpy(content_type, "text/javascript", sizeof content_type);
592 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
594 fstat(fileno(fp), &statbuf);
595 bytes = statbuf.st_size;
596 bigbuffer = malloc(bytes + 2);
597 fread(bigbuffer, bytes, 1, fp);
600 lprintf(9, "output_static('%s') %s\n", what, content_type);
601 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
604 if (!strcasecmp(bstr("force_close_session"), "yes")) {
605 end_webcit_session();
611 * \brief When the browser requests an image file from the Citadel server,
612 * this function is called to transmit it.
617 char *xferbuf = NULL;
620 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
621 serv_getln(buf, sizeof buf);
623 bytes = extract_long(&buf[4], 0);
624 xferbuf = malloc(bytes + 2);
626 /** Read it from the server */
627 read_server_binary(xferbuf, bytes);
629 serv_getln(buf, sizeof buf);
631 /** Write it to the browser */
632 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
637 * Instead of an ugly 404, send a 1x1 transparent GIF
638 * when there's no such image on the server.
640 output_static("static/blank.gif");
648 * \brief Generic function to output an arbitrary MIME part from an arbitrary
649 * message number on the server.
650 * \param msgnum number of the item on the citadel server
651 * \param partnum the MIME part to be output
653 void mimepart(char *msgnum, char *partnum)
657 char content_type[SIZ];
658 char *content = NULL;
660 serv_printf("OPNA %s|%s", msgnum, partnum);
661 serv_getln(buf, sizeof buf);
663 bytes = extract_long(&buf[4], 0);
664 content = malloc(bytes + 2);
665 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
666 output_headers(0, 0, 0, 0, 0, 0);
667 read_server_binary(content, bytes);
669 serv_getln(buf, sizeof buf);
670 http_transmit_thing(content, bytes, content_type, 0);
673 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
674 output_headers(0, 0, 0, 0, 0, 0);
675 wprintf("Content-Type: text/plain\r\n");
677 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
684 * \brief Read any MIME part of a message, from the server, into memory.
685 * \param msgnum number of the message on the citadel server
686 * \param partnum the MIME part to be loaded
688 char *load_mimepart(long msgnum, char *partnum)
692 char content_type[SIZ];
695 serv_printf("OPNA %ld|%s", msgnum, partnum);
696 serv_getln(buf, sizeof buf);
698 bytes = extract_long(&buf[4], 0);
699 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
701 content = malloc(bytes + 2);
702 read_server_binary(content, bytes);
705 serv_getln(buf, sizeof buf);
706 content[bytes] = 0; /* null terminate for good measure */
717 * \brief Convenience functions to display a page containing only a string
718 * \param titlebarcolor color of the titlebar of the frame
719 * \param titlebarmsg text to display in the title bar
720 * \param messagetext body of the box
722 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
724 wprintf("HTTP/1.1 200 OK\n");
725 output_headers(1, 1, 2, 0, 0, 0);
726 wprintf("<div id=\"banner\">\n");
727 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
728 wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
729 wprintf("</TD></TR></TABLE>\n");
730 wprintf("</div>\n<div id=\"content\">\n");
731 escputs(messagetext);
739 * \brief Display a blank page.
741 void blank_page(void) {
742 output_headers(1, 1, 0, 0, 0, 0);
748 * \brief A template has been requested
750 void url_do_template(void) {
751 do_template(bstr("template"));
757 * \brief Offer to make any page the user's "start page."
759 void offer_start_page(void) {
760 wprintf("<a href=\"change_start_page?startpage=");
761 urlescputs(WC->this_page);
762 wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
763 wprintf(_("Make this my start page"));
764 wprintf("</FONT></A>");
766 wprintf("<br/><a href=\"rss?room=");
767 urlescputs(WC->wc_roomname);
768 wprintf("\" title=\"RSS 2.0 feed for ");
769 escputs(WC->wc_roomname);
770 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
776 * \brief Change the user's start page
778 void change_start_page(void) {
780 if (bstr("startpage") == NULL) {
781 safestrncpy(WC->ImportantMessage,
782 _("You no longer have a start page selected."),
783 sizeof WC->ImportantMessage);
788 set_preference("startpage", bstr("startpage"), 1);
790 output_headers(1, 1, 0, 0, 0, 0);
791 do_template("newstartpage");
798 * \brief convenience function to indicate success
799 * \param successmessage the mesage itself
801 void display_success(char *successmessage)
803 convenience_page("007700", "OK", successmessage);
808 * \brief Authorization required page
809 * This is probably temporary and should be revisited
810 * \param message message to put in header
812 void authorization_required(const char *message)
814 wprintf("HTTP/1.1 401 Authorization Required\r\n");
815 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
816 wprintf("Content-Type: text/html\r\n\r\n");
818 wprintf(_("Authorization Required"));
819 wprintf("</h1>\r\n");
820 wprintf(_("The resource you requested requires a valid username and password. "
821 "You could not be logged in: %s\n"), message);
826 * \brief This function is called by the MIME parser to handle data uploaded by
827 * the browser. Form data, uploaded files, and the data from HTTP PUT
828 * operations (such as those found in GroupDAV) all arrive this way.
830 * \param name Name of the item being uploaded
831 * \param filename Filename of the item being uploaded
832 * \param partnum MIME part identifier (not needed)
833 * \param disp MIME content disposition (not needed)
834 * \param content The actual data
835 * \param cbtype MIME content-type
836 * \param cbcharset Character set
837 * \param length Content length
838 * \param encoding MIME encoding type (not needed)
839 * \param userdata Not used here
841 void upload_handler(char *name, char *filename, char *partnum, char *disp,
842 void *content, char *cbtype, char *cbcharset,
843 size_t length, char *encoding, void *userdata)
845 struct urlcontent *u;
847 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
848 name, cbtype, length); */
851 if ( (length > 0) && (strlen(cbtype) == 0) ) {
852 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
853 u->next = WC->urlstrings;
855 safestrncpy(u->url_key, name, sizeof(u->url_key));
856 u->url_data = malloc(length + 1);
857 memcpy(u->url_data, content, length);
858 u->url_data[length] = 0;
861 /** Uploaded files */
862 if ( (length > 0) && (strlen(cbtype) > 0) ) {
863 WC->upload = malloc(length);
864 if (WC->upload != NULL) {
865 WC->upload_length = length;
866 safestrncpy(WC->upload_filename, filename,
867 sizeof(WC->upload_filename));
868 safestrncpy(WC->upload_content_type, cbtype,
869 sizeof(WC->upload_content_type));
870 memcpy(WC->upload, content, length);
873 lprintf(3, "malloc() failed: %s\n", strerror(errno));
880 * \brief Convenience functions to wrap around asynchronous ajax responses
882 void begin_ajax_response(void) {
883 output_headers(0, 0, 0, 0, 0, 0);
885 wprintf("Content-type: text/html; charset=UTF-8\r\n"
887 "Connection: close\r\n"
888 "Pragma: no-cache\r\n"
889 "Cache-Control: no-cache\r\n",
895 * \brief print ajax response footer
897 void end_ajax_response(void) {
903 * \brief Wraps a Citadel server command in an AJAX transaction.
905 void ajax_servcmd(void)
912 begin_ajax_response();
914 serv_printf("%s", bstr("g_cmd"));
915 serv_getln(buf, sizeof buf);
916 wprintf("%s\n", buf);
919 serv_printf("\n\n000");
921 if ((buf[0] == '1') || (buf[0] == '8')) {
922 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
923 wprintf("%s\n", gcontent);
928 text_to_server(bstr("g_input"), 0);
934 serv_read(junk, len);
940 memset(junk, 0, len);
941 serv_write(junk, len);
948 * This is kind of an ugly hack, but this is the only place it can go.
949 * If the command was GEXP, then the instant messenger window must be
950 * running, so reset the "last_pager_check" watchdog timer so
951 * that page_popup() doesn't try to open it a second time.
953 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
954 WC->last_pager_check = time(NULL);
960 * \brief Helper function for the asynchronous check to see if we need
961 * to open the instant messenger window.
963 void seconds_since_last_gexp(void)
967 begin_ajax_response();
968 if ( (time(NULL) - WC->last_pager_check) < 30) {
973 serv_getln(buf, sizeof buf);
988 * \brief Entry point for WebCit transaction
990 void session_loop(struct httprequest *req)
1002 char request_method[128];
1003 char pathname[1024];
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 */
1050 extract_token(action, pathname, 1, '/', sizeof action);
1051 if (strstr(action, "?")) *strstr(action, "?") = 0;
1052 if (strstr(action, "&")) *strstr(action, "&") = 0;
1053 if (strstr(action, " ")) *strstr(action, " ") = 0;
1055 extract_token(arg1, pathname, 2, '/', sizeof arg1);
1056 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
1057 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
1058 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
1060 extract_token(arg2, pathname, 3, '/', sizeof arg2);
1061 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
1062 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
1063 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
1065 extract_token(arg3, pathname, 4, '/', sizeof arg3);
1066 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
1067 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
1068 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
1070 extract_token(arg4, pathname, 5, '/', sizeof arg4);
1071 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
1072 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
1073 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
1075 extract_token(arg5, pathname, 6, '/', sizeof arg5);
1076 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
1077 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
1078 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
1080 extract_token(arg6, pathname, 7, '/', sizeof arg6);
1081 if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
1082 if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
1083 if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
1085 extract_token(arg7, pathname, 8, '/', sizeof arg7);
1086 if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
1087 if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
1088 if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
1090 while (hptr != NULL) {
1091 safestrncpy(buf, hptr->line, sizeof buf);
1094 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1095 safestrncpy(cookie, &buf[15], sizeof cookie);
1096 cookie_to_stuff(cookie, NULL,
1097 c_username, sizeof c_username,
1098 c_password, sizeof c_password,
1099 c_roomname, sizeof c_roomname);
1101 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1102 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1103 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1104 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1106 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1107 ContentLength = atoi(&buf[16]);
1109 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1110 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1112 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1113 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1115 else if (!strncasecmp(buf, "Host: ", 6)) {
1116 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1118 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1119 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1120 while (num_tokens(browser_host, ',') > 1) {
1121 remove_token(browser_host, 0, ',');
1123 striplt(browser_host);
1125 /** Only WAP gateways explicitly name this content-type */
1126 else if (strstr(buf, "text/vnd.wap.wml")) {
1131 if (ContentLength > 0) {
1132 content = malloc(ContentLength + SIZ);
1133 memset(content, 0, ContentLength + SIZ);
1134 sprintf(content, "Content-type: %s\n"
1135 "Content-length: %d\n\n",
1136 ContentType, ContentLength);
1137 body_start = strlen(content);
1139 /** Read the entire input data at once. */
1140 client_read(WC->http_sock, &content[BytesRead+body_start],
1143 if (!strncasecmp(ContentType,
1144 "application/x-www-form-urlencoded", 33)) {
1145 addurls(&content[body_start]);
1146 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1147 content_end = content + ContentLength + body_start;
1148 mime_parser(content, content_end, *upload_handler,
1149 NULL, NULL, NULL, 0);
1155 /** make a note of where we are in case the user wants to save it */
1156 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1157 remove_token(WC->this_page, 2, ' ');
1158 remove_token(WC->this_page, 0, ' ');
1160 /** If there are variables in the URL, we must grab them now */
1161 for (a = 0; a < strlen(cmd); ++a) {
1162 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1163 for (b = a; b < strlen(cmd); ++b)
1164 if (isspace(cmd[b]))
1166 addurls(&cmd[a + 1]);
1172 /** Static content can be sent without connecting to Citadel. */
1174 for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
1175 if (!strcasecmp(action, static_content_dirs[a])) {
1180 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1181 action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1182 for (a=0; a<8; ++a) {
1183 if (buf[strlen(buf)-1] == '/') {
1184 buf[strlen(buf)-1] = 0;
1187 for (a = 0; a < strlen(buf); ++a) {
1188 if (isspace(buf[a])) {
1193 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1197 * If we're not connected to a Citadel server, try to hook up the
1200 if (!WC->connected) {
1201 if (!strcasecmp(ctdlhost, "uds")) {
1202 /* unix domain socket */
1203 sprintf(buf, "%s/citadel.socket", ctdlport);
1204 WC->serv_sock = uds_connectsock(buf);
1208 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1211 if (WC->serv_sock < 0) {
1213 goto SKIP_ALL_THIS_CRAP;
1217 serv_getln(buf, sizeof buf); /** get the server welcome message */
1220 * From what host is our user connecting? Go with
1221 * the host at the other end of the HTTP socket,
1222 * unless we are following X-Forwarded-For: headers
1223 * and such a header has already turned up something.
1225 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1226 locate_host(browser_host, WC->http_sock);
1229 get_serv_info(browser_host, user_agent);
1230 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1231 wprintf(_("You are connected to a Citadel "
1232 "server running Citadel %d.%02d. \n"
1233 "In order to run this version of WebCit "
1234 "you must also have Citadel %d.%02d or"
1236 serv_info.serv_rev_level / 100,
1237 serv_info.serv_rev_level % 100,
1238 MINIMUM_CIT_VERSION / 100,
1239 MINIMUM_CIT_VERSION % 100
1241 end_webcit_session();
1242 goto SKIP_ALL_THIS_CRAP;
1248 * Functions which can be performed without logging in
1250 if (!strcasecmp(action, "listsub")) {
1252 goto SKIP_ALL_THIS_CRAP;
1254 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1255 if (!strcasecmp(action, "freebusy")) {
1257 goto SKIP_ALL_THIS_CRAP;
1262 * If we're not logged in, but we have HTTP Authentication data,
1263 * try logging in to Citadel using that.
1265 if ((!WC->logged_in)
1266 && (strlen(c_httpauth_user) > 0)
1267 && (strlen(c_httpauth_pass) > 0)) {
1268 serv_printf("USER %s", c_httpauth_user);
1269 serv_getln(buf, sizeof buf);
1270 if (buf[0] == '3') {
1271 serv_printf("PASS %s", c_httpauth_pass);
1272 serv_getln(buf, sizeof buf);
1273 if (buf[0] == '2') {
1274 become_logged_in(c_httpauth_user,
1275 c_httpauth_pass, buf);
1276 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1277 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1279 /** Should only display when password is wrong */
1280 authorization_required(&buf[4]);
1281 goto SKIP_ALL_THIS_CRAP;
1286 /** This needs to run early */
1287 if (!strcasecmp(action, "rss")) {
1288 display_rss(bstr("room"), request_method);
1289 goto SKIP_ALL_THIS_CRAP;
1293 * The GroupDAV stuff relies on HTTP authentication instead of
1294 * our session's authentication.
1296 if (!strncasecmp(action, "groupdav", 8)) {
1297 groupdav_main(req, ContentType, /* do GroupDAV methods */
1298 ContentLength, content+body_start);
1299 if (!WC->logged_in) {
1300 WC->killthis = 1; /* If not logged in, don't */
1301 } /* keep the session active */
1302 goto SKIP_ALL_THIS_CRAP;
1307 * Automatically send requests with any method other than GET or
1308 * POST to the GroupDAV code as well.
1310 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1311 groupdav_main(req, ContentType, /** do GroupDAV methods */
1312 ContentLength, content+body_start);
1313 if (!WC->logged_in) {
1314 WC->killthis = 1; /** If not logged in, don't */
1315 } /** keep the session active */
1316 goto SKIP_ALL_THIS_CRAP;
1320 * If we're not logged in, but we have username and password cookies
1321 * supplied by the browser, try using them to log in.
1323 if ((!WC->logged_in)
1324 && (strlen(c_username) > 0)
1325 && (strlen(c_password) > 0)) {
1326 serv_printf("USER %s", c_username);
1327 serv_getln(buf, sizeof buf);
1328 if (buf[0] == '3') {
1329 serv_printf("PASS %s", c_password);
1330 serv_getln(buf, sizeof buf);
1331 if (buf[0] == '2') {
1332 become_logged_in(c_username, c_password, buf);
1337 * If we don't have a current room, but a cookie specifying the
1338 * current room is supplied, make an effort to go there.
1340 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1341 serv_printf("GOTO %s", c_roomname);
1342 serv_getln(buf, sizeof buf);
1343 if (buf[0] == '2') {
1344 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1348 if (!strcasecmp(action, "image")) {
1352 * All functions handled below this point ... make sure we log in
1353 * before doing anything else!
1355 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1357 } else if (!WC->logged_in) {
1358 display_login(NULL);
1362 * Various commands...
1365 else if (!strcasecmp(action, "do_welcome")) {
1367 } else if (!strcasecmp(action, "blank")) {
1369 } else if (!strcasecmp(action, "do_template")) {
1371 } else if (!strcasecmp(action, "display_aide_menu")) {
1372 display_aide_menu();
1373 } else if (!strcasecmp(action, "display_main_menu")) {
1374 display_main_menu();
1375 } else if (!strcasecmp(action, "who")) {
1377 } else if (!strcasecmp(action, "sslg")) {
1378 seconds_since_last_gexp();
1379 } else if (!strcasecmp(action, "who_inner_html")) {
1380 begin_ajax_response();
1382 end_ajax_response();
1383 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1384 begin_ajax_response();
1386 end_ajax_response();
1387 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1388 begin_ajax_response();
1389 do_iconbar_roomlist();
1390 end_ajax_response();
1391 } else if (!strcasecmp(action, "knrooms")) {
1393 } else if (!strcasecmp(action, "gotonext")) {
1396 } else if (!strcasecmp(action, "skip")) {
1398 } else if (!strcasecmp(action, "ungoto")) {
1400 } else if (!strcasecmp(action, "dotgoto")) {
1401 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1404 smart_goto(bstr("room"));
1405 } else if (!strcasecmp(action, "dotskip")) {
1406 smart_goto(bstr("room"));
1407 } else if (!strcasecmp(action, "termquit")) {
1409 } else if (!strcasecmp(action, "readnew")) {
1410 readloop("readnew");
1411 } else if (!strcasecmp(action, "readold")) {
1412 readloop("readold");
1413 } else if (!strcasecmp(action, "readfwd")) {
1414 readloop("readfwd");
1415 } else if (!strcasecmp(action, "headers")) {
1416 readloop("headers");
1417 } else if (!strcasecmp(action, "msg")) {
1418 embed_message(arg1);
1419 } else if (!strcasecmp(action, "printmsg")) {
1420 print_message(arg1);
1421 } else if (!strcasecmp(action, "msgheaders")) {
1422 display_headers(arg1);
1423 } else if (!strcasecmp(action, "wiki")) {
1424 display_wiki_page();
1425 } else if (!strcasecmp(action, "display_enter")) {
1427 } else if (!strcasecmp(action, "post")) {
1429 } else if (!strcasecmp(action, "move_msg")) {
1431 } else if (!strcasecmp(action, "delete_msg")) {
1433 } else if (!strcasecmp(action, "userlist")) {
1435 } else if (!strcasecmp(action, "showuser")) {
1437 } else if (!strcasecmp(action, "display_page")) {
1439 } else if (!strcasecmp(action, "page_user")) {
1441 } else if (!strcasecmp(action, "chat")) {
1443 } else if (!strcasecmp(action, "display_private")) {
1444 display_private("", 0);
1445 } else if (!strcasecmp(action, "goto_private")) {
1447 } else if (!strcasecmp(action, "zapped_list")) {
1449 } else if (!strcasecmp(action, "display_zap")) {
1451 } else if (!strcasecmp(action, "zap")) {
1453 } else if (!strcasecmp(action, "display_entroom")) {
1455 } else if (!strcasecmp(action, "entroom")) {
1457 } else if (!strcasecmp(action, "display_whok")) {
1459 } else if (!strcasecmp(action, "do_invt_kick")) {
1461 } else if (!strcasecmp(action, "display_editroom")) {
1463 } else if (!strcasecmp(action, "netedit")) {
1465 } else if (!strcasecmp(action, "editroom")) {
1467 } else if (!strcasecmp(action, "display_editinfo")) {
1468 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1469 } else if (!strcasecmp(action, "editinfo")) {
1470 save_edit(_("Room info"), "EINF 1", 1);
1471 } else if (!strcasecmp(action, "display_editbio")) {
1472 sprintf(buf, "RBIO %s", WC->wc_fullname);
1473 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1474 } else if (!strcasecmp(action, "editbio")) {
1475 save_edit(_("Your bio"), "EBIO", 0);
1476 } else if (!strcasecmp(action, "confirm_move_msg")) {
1478 } else if (!strcasecmp(action, "delete_room")) {
1480 } else if (!strcasecmp(action, "validate")) {
1482 } else if (!strcasecmp(action, "display_editpic")) {
1483 display_graphics_upload(_("your photo"),
1486 } else if (!strcasecmp(action, "editpic")) {
1487 do_graphics_upload("UIMG 1|_userpic_");
1488 } else if (!strcasecmp(action, "display_editroompic")) {
1489 display_graphics_upload(_("the icon for this room"),
1492 } else if (!strcasecmp(action, "editroompic")) {
1493 do_graphics_upload("UIMG 1|_roompic_");
1494 } else if (!strcasecmp(action, "delete_floor")) {
1496 } else if (!strcasecmp(action, "rename_floor")) {
1498 } else if (!strcasecmp(action, "create_floor")) {
1500 } else if (!strcasecmp(action, "display_editfloorpic")) {
1501 sprintf(buf, "UIMG 0|_floorpic_|%s",
1502 bstr("which_floor"));
1503 display_graphics_upload(_("the icon for this floor"),
1506 } else if (!strcasecmp(action, "editfloorpic")) {
1507 sprintf(buf, "UIMG 1|_floorpic_|%s",
1508 bstr("which_floor"));
1509 do_graphics_upload(buf);
1510 } else if (!strcasecmp(action, "display_reg")) {
1512 } else if (!strcasecmp(action, "display_changepw")) {
1514 } else if (!strcasecmp(action, "changepw")) {
1516 } else if (!strcasecmp(action, "display_edit_node")) {
1517 display_edit_node();
1518 } else if (!strcasecmp(action, "edit_node")) {
1520 } else if (!strcasecmp(action, "display_netconf")) {
1522 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1523 display_confirm_delete_node();
1524 } else if (!strcasecmp(action, "delete_node")) {
1526 } else if (!strcasecmp(action, "display_add_node")) {
1528 } else if (!strcasecmp(action, "add_node")) {
1530 } else if (!strcasecmp(action, "terminate_session")) {
1532 terminate_session();
1533 } else if (!strcasecmp(action, "edit_me")) {
1535 } else if (!strcasecmp(action, "display_siteconfig")) {
1536 display_siteconfig();
1537 } else if (!strcasecmp(action, "chat_recv")) {
1539 } else if (!strcasecmp(action, "chat_send")) {
1541 } else if (!strcasecmp(action, "siteconfig")) {
1543 } else if (!strcasecmp(action, "display_generic")) {
1545 } else if (!strcasecmp(action, "do_generic")) {
1547 } else if (!strcasecmp(action, "ajax_servcmd")) {
1549 } else if (!strcasecmp(action, "display_menubar")) {
1551 } else if (!strcasecmp(action, "mimepart")) {
1552 mimepart(arg1, arg2);
1553 } else if (!strcasecmp(action, "edit_vcard")) {
1555 } else if (!strcasecmp(action, "submit_vcard")) {
1557 } else if (!strcasecmp(action, "select_user_to_edit")) {
1558 select_user_to_edit(NULL, NULL);
1559 } else if (!strcasecmp(action, "display_edituser")) {
1560 display_edituser(NULL, 0);
1561 } else if (!strcasecmp(action, "edituser")) {
1563 } else if (!strcasecmp(action, "create_user")) {
1565 } else if (!strcasecmp(action, "changeview")) {
1567 } else if (!strcasecmp(action, "change_start_page")) {
1568 change_start_page();
1569 } else if (!strcasecmp(action, "display_floorconfig")) {
1570 display_floorconfig(NULL);
1571 } else if (!strcasecmp(action, "toggle_self_service")) {
1572 toggle_self_service();
1573 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1574 } else if (!strcasecmp(action, "display_edit_task")) {
1575 display_edit_task();
1576 } else if (!strcasecmp(action, "save_task")) {
1578 } else if (!strcasecmp(action, "display_edit_event")) {
1579 display_edit_event();
1580 } else if (!strcasecmp(action, "save_event")) {
1582 } else if (!strcasecmp(action, "respond_to_request")) {
1583 respond_to_request();
1584 } else if (!strcasecmp(action, "handle_rsvp")) {
1587 } else if (!strcasecmp(action, "summary")) {
1589 } else if (!strcasecmp(action, "summary_inner_div")) {
1590 begin_ajax_response();
1591 summary_inner_div();
1592 end_ajax_response();
1593 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1594 display_customize_iconbar();
1595 } else if (!strcasecmp(action, "commit_iconbar")) {
1597 } else if (!strcasecmp(action, "set_room_policy")) {
1599 } else if (!strcasecmp(action, "display_inetconf")) {
1601 } else if (!strcasecmp(action, "save_inetconf")) {
1603 } else if (!strcasecmp(action, "setup_wizard")) {
1605 } else if (!strcasecmp(action, "display_preferences")) {
1606 display_preferences();
1607 } else if (!strcasecmp(action, "set_preferences")) {
1609 } else if (!strcasecmp(action, "recp_autocomplete")) {
1610 recp_autocomplete(bstr("recp"));
1611 } else if (!strcasecmp(action, "cc_autocomplete")) {
1612 recp_autocomplete(bstr("cc"));
1613 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1614 recp_autocomplete(bstr("bcc"));
1615 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1616 set_floordiv_expanded(arg1);
1617 } else if (!strcasecmp(action, "diagnostics")) {
1618 output_headers(1, 1, 1, 0, 0, 0);
1619 wprintf("Session: %d<hr />\n", WC->wc_session);
1620 wprintf("Command: <br /><PRE>\n");
1622 wprintf("</PRE><hr />\n");
1623 wprintf("Variables: <br /><PRE>\n");
1625 wprintf("</PRE><hr />\n");
1627 } else if (!strcasecmp(action, "updatenote")) {
1631 /** When all else fais, display the main menu. */
1633 display_main_menu();
1638 if (content != NULL) {
1643 if (WC->upload_length > 0) {
1645 WC->upload_length = 0;