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]);
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);
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 to copy to
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 outputbuffer
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 message to the client ???
367 * \param strbuf message to print???
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 httpdate(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 partnumber???
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 part number???
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 display the successfull edit or s.th. lisket hat ???
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 handle file uploads
827 * \param name the url the upload is done to
828 * \param filename the name of the file being uploaded
829 * \param partnum item number on the citadel server ???
830 * \param disp what???
831 * \param content the file contents???
832 * \param cbtype what???
833 * \param cbcharset the character set of cb??
834 * \param length the size of the file ???
835 * \param encoding charset encoding of the file??
836 * \param userdata what???
838 void upload_handler(char *name, char *filename, char *partnum, char *disp,
839 void *content, char *cbtype, char *cbcharset,
840 size_t length, char *encoding, void *userdata)
842 struct urlcontent *u;
844 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
845 name, cbtype, length); */
848 if ( (length > 0) && (strlen(cbtype) == 0) ) {
849 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
850 u->next = WC->urlstrings;
852 safestrncpy(u->url_key, name, sizeof(u->url_key));
853 u->url_data = malloc(length + 1);
854 memcpy(u->url_data, content, length);
855 u->url_data[length] = 0;
858 /** Uploaded files */
859 if ( (length > 0) && (strlen(cbtype) > 0) ) {
860 WC->upload = malloc(length);
861 if (WC->upload != NULL) {
862 WC->upload_length = length;
863 safestrncpy(WC->upload_filename, filename,
864 sizeof(WC->upload_filename));
865 safestrncpy(WC->upload_content_type, cbtype,
866 sizeof(WC->upload_content_type));
867 memcpy(WC->upload, content, length);
870 lprintf(3, "malloc() failed: %s\n", strerror(errno));
877 * \brief Convenience functions to wrap around asynchronous ajax responses
879 void begin_ajax_response(void) {
880 output_headers(0, 0, 0, 0, 0, 0);
882 wprintf("Content-type: text/html; charset=UTF-8\r\n"
884 "Connection: close\r\n"
885 "Pragma: no-cache\r\n"
886 "Cache-Control: no-cache\r\n",
892 * \brief print ajax response footer
894 void end_ajax_response(void) {
900 * \brief parse server command to execute on the citadel
902 void ajax_servcmd(void)
909 begin_ajax_response();
911 serv_printf("%s", bstr("g_cmd"));
912 serv_getln(buf, sizeof buf);
913 wprintf("%s\n", buf);
916 serv_printf("\n\n000");
918 if ((buf[0] == '1') || (buf[0] == '8')) {
919 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
920 wprintf("%s\n", gcontent);
925 text_to_server(bstr("g_input"), 0);
931 serv_read(junk, len);
937 memset(junk, 0, len);
938 serv_write(junk, len);
945 * This is kind of an ugly hack, but this is the only place it can go.
946 * If the command was GEXP, then the instant messenger window must be
947 * running, so reset the "last_pager_check" watchdog timer so
948 * that page_popup() doesn't try to open it a second time.
950 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
951 WC->last_pager_check = time(NULL);
957 * \brief Helper function for the asynchronous check to see if we need
958 * to open the instant messenger window.
960 void seconds_since_last_gexp(void)
964 begin_ajax_response();
965 if ( (time(NULL) - WC->last_pager_check) < 30) {
970 serv_getln(buf, sizeof buf);
985 * \brief Entry point for WebCit transaction
987 void session_loop(struct httprequest *req)
999 char request_method[128];
1000 char pathname[1024];
1002 int ContentLength = 0;
1004 char ContentType[512];
1005 char *content = NULL;
1006 char *content_end = NULL;
1007 struct httprequest *hptr;
1008 char browser_host[256];
1009 char user_agent[256];
1014 * We stuff these with the values coming from the client cookies,
1015 * so we can use them to reconnect a timed out session if we have to.
1017 char c_username[SIZ];
1018 char c_password[SIZ];
1019 char c_roomname[SIZ];
1020 char c_httpauth_string[SIZ];
1021 char c_httpauth_user[SIZ];
1022 char c_httpauth_pass[SIZ];
1025 safestrncpy(c_username, "", sizeof c_username);
1026 safestrncpy(c_password, "", sizeof c_password);
1027 safestrncpy(c_roomname, "", sizeof c_roomname);
1028 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1029 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1030 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1031 strcpy(browser_host, "");
1033 WC->upload_length = 0;
1039 if (hptr == NULL) return;
1041 safestrncpy(cmd, hptr->line, sizeof cmd);
1043 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1044 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1046 /** Figure out the action */
1047 extract_token(action, pathname, 1, '/', sizeof action);
1048 if (strstr(action, "?")) *strstr(action, "?") = 0;
1049 if (strstr(action, "&")) *strstr(action, "&") = 0;
1050 if (strstr(action, " ")) *strstr(action, " ") = 0;
1052 extract_token(arg1, pathname, 2, '/', sizeof arg1);
1053 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
1054 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
1055 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
1057 extract_token(arg2, pathname, 3, '/', sizeof arg2);
1058 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
1059 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
1060 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
1062 extract_token(arg3, pathname, 4, '/', sizeof arg3);
1063 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
1064 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
1065 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
1067 extract_token(arg4, pathname, 5, '/', sizeof arg4);
1068 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
1069 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
1070 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
1072 extract_token(arg5, pathname, 6, '/', sizeof arg5);
1073 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
1074 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
1075 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
1077 extract_token(arg6, pathname, 7, '/', sizeof arg6);
1078 if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
1079 if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
1080 if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
1082 extract_token(arg7, pathname, 8, '/', sizeof arg7);
1083 if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
1084 if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
1085 if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
1087 while (hptr != NULL) {
1088 safestrncpy(buf, hptr->line, sizeof buf);
1091 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1092 safestrncpy(cookie, &buf[15], sizeof cookie);
1093 cookie_to_stuff(cookie, NULL,
1094 c_username, sizeof c_username,
1095 c_password, sizeof c_password,
1096 c_roomname, sizeof c_roomname);
1098 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1099 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1100 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1101 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1103 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1104 ContentLength = atoi(&buf[16]);
1106 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1107 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1109 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1110 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1112 else if (!strncasecmp(buf, "Host: ", 6)) {
1113 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1115 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1116 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1117 while (num_tokens(browser_host, ',') > 1) {
1118 remove_token(browser_host, 0, ',');
1120 striplt(browser_host);
1122 /** Only WAP gateways explicitly name this content-type */
1123 else if (strstr(buf, "text/vnd.wap.wml")) {
1128 if (ContentLength > 0) {
1129 content = malloc(ContentLength + SIZ);
1130 memset(content, 0, ContentLength + SIZ);
1131 sprintf(content, "Content-type: %s\n"
1132 "Content-length: %d\n\n",
1133 ContentType, ContentLength);
1134 body_start = strlen(content);
1136 /** Read the entire input data at once. */
1137 client_read(WC->http_sock, &content[BytesRead+body_start],
1140 if (!strncasecmp(ContentType,
1141 "application/x-www-form-urlencoded", 33)) {
1142 addurls(&content[body_start]);
1143 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1144 content_end = content + ContentLength + body_start;
1145 mime_parser(content, content_end, *upload_handler,
1146 NULL, NULL, NULL, 0);
1152 /** make a note of where we are in case the user wants to save it */
1153 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1154 remove_token(WC->this_page, 2, ' ');
1155 remove_token(WC->this_page, 0, ' ');
1157 /** If there are variables in the URL, we must grab them now */
1158 for (a = 0; a < strlen(cmd); ++a) {
1159 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1160 for (b = a; b < strlen(cmd); ++b)
1161 if (isspace(cmd[b]))
1163 addurls(&cmd[a + 1]);
1169 /** Static content can be sent without connecting to Citadel. */
1171 for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
1172 if (!strcasecmp(action, static_content_dirs[a])) {
1177 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1178 action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1179 for (a=0; a<8; ++a) {
1180 if (buf[strlen(buf)-1] == '/') {
1181 buf[strlen(buf)-1] = 0;
1184 for (a = 0; a < strlen(buf); ++a) {
1185 if (isspace(buf[a])) {
1190 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1194 * If we're not connected to a Citadel server, try to hook up the
1197 if (!WC->connected) {
1198 if (!strcasecmp(ctdlhost, "uds")) {
1199 /* unix domain socket */
1200 sprintf(buf, "%s/citadel.socket", ctdlport);
1201 WC->serv_sock = uds_connectsock(buf);
1205 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1208 if (WC->serv_sock < 0) {
1210 goto SKIP_ALL_THIS_CRAP;
1214 serv_getln(buf, sizeof buf); /** get the server welcome message */
1217 * From what host is our user connecting? Go with
1218 * the host at the other end of the HTTP socket,
1219 * unless we are following X-Forwarded-For: headers
1220 * and such a header has already turned up something.
1222 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1223 locate_host(browser_host, WC->http_sock);
1226 get_serv_info(browser_host, user_agent);
1227 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1228 wprintf(_("You are connected to a Citadel "
1229 "server running Citadel %d.%02d. \n"
1230 "In order to run this version of WebCit "
1231 "you must also have Citadel %d.%02d or"
1233 serv_info.serv_rev_level / 100,
1234 serv_info.serv_rev_level % 100,
1235 MINIMUM_CIT_VERSION / 100,
1236 MINIMUM_CIT_VERSION % 100
1238 end_webcit_session();
1239 goto SKIP_ALL_THIS_CRAP;
1245 * Functions which can be performed without logging in
1247 if (!strcasecmp(action, "listsub")) {
1249 goto SKIP_ALL_THIS_CRAP;
1251 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1252 if (!strcasecmp(action, "freebusy")) {
1254 goto SKIP_ALL_THIS_CRAP;
1259 * If we're not logged in, but we have HTTP Authentication data,
1260 * try logging in to Citadel using that.
1262 if ((!WC->logged_in)
1263 && (strlen(c_httpauth_user) > 0)
1264 && (strlen(c_httpauth_pass) > 0)) {
1265 serv_printf("USER %s", c_httpauth_user);
1266 serv_getln(buf, sizeof buf);
1267 if (buf[0] == '3') {
1268 serv_printf("PASS %s", c_httpauth_pass);
1269 serv_getln(buf, sizeof buf);
1270 if (buf[0] == '2') {
1271 become_logged_in(c_httpauth_user,
1272 c_httpauth_pass, buf);
1273 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1274 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1276 /** Should only display when password is wrong */
1277 authorization_required(&buf[4]);
1278 goto SKIP_ALL_THIS_CRAP;
1283 /** This needs to run early */
1284 if (!strcasecmp(action, "rss")) {
1285 display_rss(bstr("room"), request_method);
1286 goto SKIP_ALL_THIS_CRAP;
1290 * The GroupDAV stuff relies on HTTP authentication instead of
1291 * our session's authentication.
1293 if (!strncasecmp(action, "groupdav", 8)) {
1294 groupdav_main(req, ContentType, /* do GroupDAV methods */
1295 ContentLength, content+body_start);
1296 if (!WC->logged_in) {
1297 WC->killthis = 1; /* If not logged in, don't */
1298 } /* keep the session active */
1299 goto SKIP_ALL_THIS_CRAP;
1304 * Automatically send requests with any method other than GET or
1305 * POST to the GroupDAV code as well.
1307 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
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;
1317 * If we're not logged in, but we have username and password cookies
1318 * supplied by the browser, try using them to log in.
1320 if ((!WC->logged_in)
1321 && (strlen(c_username) > 0)
1322 && (strlen(c_password) > 0)) {
1323 serv_printf("USER %s", c_username);
1324 serv_getln(buf, sizeof buf);
1325 if (buf[0] == '3') {
1326 serv_printf("PASS %s", c_password);
1327 serv_getln(buf, sizeof buf);
1328 if (buf[0] == '2') {
1329 become_logged_in(c_username, c_password, buf);
1334 * If we don't have a current room, but a cookie specifying the
1335 * current room is supplied, make an effort to go there.
1337 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1338 serv_printf("GOTO %s", c_roomname);
1339 serv_getln(buf, sizeof buf);
1340 if (buf[0] == '2') {
1341 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1345 if (!strcasecmp(action, "image")) {
1349 * All functions handled below this point ... make sure we log in
1350 * before doing anything else!
1352 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1354 } else if (!WC->logged_in) {
1355 display_login(NULL);
1359 * Various commands...
1362 else if (!strcasecmp(action, "do_welcome")) {
1364 } else if (!strcasecmp(action, "blank")) {
1366 } else if (!strcasecmp(action, "do_template")) {
1368 } else if (!strcasecmp(action, "display_aide_menu")) {
1369 display_aide_menu();
1370 } else if (!strcasecmp(action, "display_main_menu")) {
1371 display_main_menu();
1372 } else if (!strcasecmp(action, "who")) {
1374 } else if (!strcasecmp(action, "sslg")) {
1375 seconds_since_last_gexp();
1376 } else if (!strcasecmp(action, "who_inner_html")) {
1377 begin_ajax_response();
1379 end_ajax_response();
1380 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1381 begin_ajax_response();
1383 end_ajax_response();
1384 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1385 begin_ajax_response();
1386 do_iconbar_roomlist();
1387 end_ajax_response();
1388 } else if (!strcasecmp(action, "knrooms")) {
1390 } else if (!strcasecmp(action, "gotonext")) {
1393 } else if (!strcasecmp(action, "skip")) {
1395 } else if (!strcasecmp(action, "ungoto")) {
1397 } else if (!strcasecmp(action, "dotgoto")) {
1398 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1401 smart_goto(bstr("room"));
1402 } else if (!strcasecmp(action, "dotskip")) {
1403 smart_goto(bstr("room"));
1404 } else if (!strcasecmp(action, "termquit")) {
1406 } else if (!strcasecmp(action, "readnew")) {
1407 readloop("readnew");
1408 } else if (!strcasecmp(action, "readold")) {
1409 readloop("readold");
1410 } else if (!strcasecmp(action, "readfwd")) {
1411 readloop("readfwd");
1412 } else if (!strcasecmp(action, "headers")) {
1413 readloop("headers");
1414 } else if (!strcasecmp(action, "msg")) {
1415 embed_message(arg1);
1416 } else if (!strcasecmp(action, "printmsg")) {
1417 print_message(arg1);
1418 } else if (!strcasecmp(action, "msgheaders")) {
1419 display_headers(arg1);
1420 } else if (!strcasecmp(action, "wiki")) {
1421 display_wiki_page();
1422 } else if (!strcasecmp(action, "display_enter")) {
1424 } else if (!strcasecmp(action, "post")) {
1426 } else if (!strcasecmp(action, "move_msg")) {
1428 } else if (!strcasecmp(action, "delete_msg")) {
1430 } else if (!strcasecmp(action, "userlist")) {
1432 } else if (!strcasecmp(action, "showuser")) {
1434 } else if (!strcasecmp(action, "display_page")) {
1436 } else if (!strcasecmp(action, "page_user")) {
1438 } else if (!strcasecmp(action, "chat")) {
1440 } else if (!strcasecmp(action, "display_private")) {
1441 display_private("", 0);
1442 } else if (!strcasecmp(action, "goto_private")) {
1444 } else if (!strcasecmp(action, "zapped_list")) {
1446 } else if (!strcasecmp(action, "display_zap")) {
1448 } else if (!strcasecmp(action, "zap")) {
1450 } else if (!strcasecmp(action, "display_entroom")) {
1452 } else if (!strcasecmp(action, "entroom")) {
1454 } else if (!strcasecmp(action, "display_whok")) {
1456 } else if (!strcasecmp(action, "do_invt_kick")) {
1458 } else if (!strcasecmp(action, "display_editroom")) {
1460 } else if (!strcasecmp(action, "netedit")) {
1462 } else if (!strcasecmp(action, "editroom")) {
1464 } else if (!strcasecmp(action, "display_editinfo")) {
1465 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1466 } else if (!strcasecmp(action, "editinfo")) {
1467 save_edit(_("Room info"), "EINF 1", 1);
1468 } else if (!strcasecmp(action, "display_editbio")) {
1469 sprintf(buf, "RBIO %s", WC->wc_fullname);
1470 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1471 } else if (!strcasecmp(action, "editbio")) {
1472 save_edit(_("Your bio"), "EBIO", 0);
1473 } else if (!strcasecmp(action, "confirm_move_msg")) {
1475 } else if (!strcasecmp(action, "delete_room")) {
1477 } else if (!strcasecmp(action, "validate")) {
1479 } else if (!strcasecmp(action, "display_editpic")) {
1480 display_graphics_upload(_("your photo"),
1483 } else if (!strcasecmp(action, "editpic")) {
1484 do_graphics_upload("UIMG 1|_userpic_");
1485 } else if (!strcasecmp(action, "display_editroompic")) {
1486 display_graphics_upload(_("the icon for this room"),
1489 } else if (!strcasecmp(action, "editroompic")) {
1490 do_graphics_upload("UIMG 1|_roompic_");
1491 } else if (!strcasecmp(action, "delete_floor")) {
1493 } else if (!strcasecmp(action, "rename_floor")) {
1495 } else if (!strcasecmp(action, "create_floor")) {
1497 } else if (!strcasecmp(action, "display_editfloorpic")) {
1498 sprintf(buf, "UIMG 0|_floorpic_|%s",
1499 bstr("which_floor"));
1500 display_graphics_upload(_("the icon for this floor"),
1503 } else if (!strcasecmp(action, "editfloorpic")) {
1504 sprintf(buf, "UIMG 1|_floorpic_|%s",
1505 bstr("which_floor"));
1506 do_graphics_upload(buf);
1507 } else if (!strcasecmp(action, "display_reg")) {
1509 } else if (!strcasecmp(action, "display_changepw")) {
1511 } else if (!strcasecmp(action, "changepw")) {
1513 } else if (!strcasecmp(action, "display_edit_node")) {
1514 display_edit_node();
1515 } else if (!strcasecmp(action, "edit_node")) {
1517 } else if (!strcasecmp(action, "display_netconf")) {
1519 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1520 display_confirm_delete_node();
1521 } else if (!strcasecmp(action, "delete_node")) {
1523 } else if (!strcasecmp(action, "display_add_node")) {
1525 } else if (!strcasecmp(action, "add_node")) {
1527 } else if (!strcasecmp(action, "terminate_session")) {
1529 terminate_session();
1530 } else if (!strcasecmp(action, "edit_me")) {
1532 } else if (!strcasecmp(action, "display_siteconfig")) {
1533 display_siteconfig();
1534 } else if (!strcasecmp(action, "chat_recv")) {
1536 } else if (!strcasecmp(action, "chat_send")) {
1538 } else if (!strcasecmp(action, "siteconfig")) {
1540 } else if (!strcasecmp(action, "display_generic")) {
1542 } else if (!strcasecmp(action, "do_generic")) {
1544 } else if (!strcasecmp(action, "ajax_servcmd")) {
1546 } else if (!strcasecmp(action, "display_menubar")) {
1548 } else if (!strcasecmp(action, "mimepart")) {
1549 mimepart(arg1, arg2);
1550 } else if (!strcasecmp(action, "edit_vcard")) {
1552 } else if (!strcasecmp(action, "submit_vcard")) {
1554 } else if (!strcasecmp(action, "select_user_to_edit")) {
1555 select_user_to_edit(NULL, NULL);
1556 } else if (!strcasecmp(action, "display_edituser")) {
1557 display_edituser(NULL, 0);
1558 } else if (!strcasecmp(action, "edituser")) {
1560 } else if (!strcasecmp(action, "create_user")) {
1562 } else if (!strcasecmp(action, "changeview")) {
1564 } else if (!strcasecmp(action, "change_start_page")) {
1565 change_start_page();
1566 } else if (!strcasecmp(action, "display_floorconfig")) {
1567 display_floorconfig(NULL);
1568 } else if (!strcasecmp(action, "toggle_self_service")) {
1569 toggle_self_service();
1570 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1571 } else if (!strcasecmp(action, "display_edit_task")) {
1572 display_edit_task();
1573 } else if (!strcasecmp(action, "save_task")) {
1575 } else if (!strcasecmp(action, "display_edit_event")) {
1576 display_edit_event();
1577 } else if (!strcasecmp(action, "save_event")) {
1579 } else if (!strcasecmp(action, "respond_to_request")) {
1580 respond_to_request();
1581 } else if (!strcasecmp(action, "handle_rsvp")) {
1584 } else if (!strcasecmp(action, "summary")) {
1586 } else if (!strcasecmp(action, "summary_inner_div")) {
1587 begin_ajax_response();
1588 summary_inner_div();
1589 end_ajax_response();
1590 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1591 display_customize_iconbar();
1592 } else if (!strcasecmp(action, "commit_iconbar")) {
1594 } else if (!strcasecmp(action, "set_room_policy")) {
1596 } else if (!strcasecmp(action, "display_inetconf")) {
1598 } else if (!strcasecmp(action, "save_inetconf")) {
1600 } else if (!strcasecmp(action, "setup_wizard")) {
1602 } else if (!strcasecmp(action, "display_preferences")) {
1603 display_preferences();
1604 } else if (!strcasecmp(action, "set_preferences")) {
1606 } else if (!strcasecmp(action, "recp_autocomplete")) {
1607 recp_autocomplete(bstr("recp"));
1608 } else if (!strcasecmp(action, "cc_autocomplete")) {
1609 recp_autocomplete(bstr("cc"));
1610 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1611 recp_autocomplete(bstr("bcc"));
1612 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1613 set_floordiv_expanded(arg1);
1614 } else if (!strcasecmp(action, "diagnostics")) {
1615 output_headers(1, 1, 1, 0, 0, 0);
1616 wprintf("Session: %d<hr />\n", WC->wc_session);
1617 wprintf("Command: <br /><PRE>\n");
1619 wprintf("</PRE><hr />\n");
1620 wprintf("Variables: <br /><PRE>\n");
1622 wprintf("</PRE><hr />\n");
1626 /** When all else fais, display the main menu. */
1628 display_main_menu();
1633 if (content != NULL) {
1638 if (WC->upload_length > 0) {
1640 WC->upload_length = 0;