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);
109 * \brief free urlstring memory
113 struct urlcontent *u;
115 while (WC->urlstrings != NULL) {
116 free(WC->urlstrings->url_data);
117 u = WC->urlstrings->next;
118 free(WC->urlstrings);
124 * \brief Diagnostic function to display the contents of all variables
128 struct urlcontent *u;
130 for (u = WC->urlstrings; u != NULL; u = u->next) {
131 wprintf("%38s = %s\n", u->url_key, u->url_data);
136 * \brief Return the value of a variable supplied to the current web page (from the url or a form)
137 * \param key The name of the variable we want
139 char *bstr(char *key)
141 struct urlcontent *u;
143 for (u = WC->urlstrings; u != NULL; u = u->next) {
144 if (!strcasecmp(u->url_key, key))
145 return (u->url_data);
151 * \brief web-printing funcion. uses our vsnprintf wrapper
152 * \param format printf format string
153 * \param ... the varargs to put into formatstring
155 void wprintf(const char *format,...)
160 va_start(arg_ptr, format);
161 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
164 client_write(wbuf, strlen(wbuf));
169 * \brief wrap up an HTTP session, closes tags, etc.
170 * \todo multiline params?
171 * \param print_standard_html_footer should be set to 0 to transmit only, 1 to
172 * append the main menu and closing tags, or 2 to
173 * append the closing tags only.
175 void wDumpContent(int print_standard_html_footer)
177 if (print_standard_html_footer) {
178 wprintf("</div>\n"); /* end of "text" div */
179 do_template("trailing");
182 /* If we've been saving it all up for one big output burst,
183 * go ahead and do that now.
190 * \brief Copy a string, escaping characters which have meaning in HTML.
191 * \param target target buffer
192 * \param strbuf source buffer
193 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
194 * \param nolinebreaks if set, linebreaks are removed from the string.
196 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
201 for (a = 0; a < strlen(strbuf); ++a) {
202 if (strbuf[a] == '<')
203 strcat(target, "<");
204 else 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] == LB)
214 else if (strbuf[a] == RB)
216 else if (strbuf[a] == QU)
217 strcat(target, "\"");
218 else if ((strbuf[a] == 32) && (nbsp == 1))
219 strcat(target, " ");
220 else if ((strbuf[a] == '\n') && (nolinebreaks))
221 strcat(target, ""); /* nothing */
222 else if ((strbuf[a] == '\r') && (nolinebreaks))
223 strcat(target, ""); /* nothing */
225 strncat(target, &strbuf[a], 1);
231 * \param strbuf what???
232 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
233 * \param nolinebreaks if set, linebreaks are removed from the string.
235 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
239 if (strbuf == NULL) return;
240 buf = malloc( (3 * strlen(strbuf)) + SIZ );
241 stresc(buf, strbuf, nbsp, nolinebreaks);
247 * \brief static wrapper for ecsputs1
248 * \param strbuf buffer to print escaped to client
250 void escputs(char *strbuf)
252 escputs1(strbuf, 0, 0);
256 * \brief Escape a string for feeding out as a URL.
257 * Returns a pointer to a buffer that must be freed by the caller!
258 * \param outbuf the output buffer
259 * \param strbuf the input buffer
261 void urlesc(char *outbuf, char *strbuf)
264 char *ec = " #&;`'|*?-~<>^()[]{}$\"\\";
268 for (a = 0; a < strlen(strbuf); ++a) {
270 for (b = 0; b < strlen(ec); ++b) {
271 if (strbuf[a] == ec[b])
276 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
278 sprintf(&outbuf[b], "%c", strbuf[a]);
283 * \brief urlescape buffer and print it to the client
284 * \param strbuf buffer to urlescape
286 void urlescputs(char *strbuf)
290 urlesc(outbuf, strbuf);
291 wprintf("%s", outbuf);
296 * \brief Copy a string, escaping characters for JavaScript strings.
297 * \param target output string
298 * \param strbuf input string
300 void jsesc(char *target, char *strbuf)
305 for (a = 0; a < strlen(strbuf); ++a) {
306 if (strbuf[a] == '<')
308 else if (strbuf[a] == '>')
310 else if (strbuf[a] == '\"')
311 strcat(target, """);
312 else if (strbuf[a] == '&')
313 strcat(target, "&;");
314 else if (strbuf[a] == '\'')
315 strcat(target, "\\'");
317 strncat(target, &strbuf[a], 1);
323 * \brief escape and print java script
324 * \param strbuf the js code
326 void jsescputs(char *strbuf)
330 jsesc(outbuf, strbuf);
331 wprintf("%s", outbuf);
335 * \brief Copy a string, escaping characters for message text hold
336 * \param target target buffer
337 * \param strbuf source buffer
339 void msgesc(char *target, char *strbuf)
344 for (a = 0; a < strlen(strbuf); ++a) {
345 if (strbuf[a] == '\n')
347 else if (strbuf[a] == '\r')
349 else if (strbuf[a] == '\'')
350 strcat(target, "'");
352 strncat(target, &strbuf[a], 1);
358 * \brief print a string to the client after cleaning it with msgesc()
359 * \param strbuf string to be printed
361 void msgescputs(char *strbuf) {
364 if (strbuf == NULL) return;
365 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
366 msgesc(outbuf, strbuf);
367 wprintf("%s", outbuf);
375 * \brief Output all that important stuff that the browser will want to see
377 void output_headers( int do_httpheaders, /**< 1 = output HTTP headers */
378 int do_htmlhead, /**< 1 = output HTML <head> section and <body> opener */
380 int do_room_banner, /**< 0=no, 1=yes,
381 * 2 = I'm going to embed my own, so don't open the
382 * <div id="content"> either.
385 int unset_cookies, /**< 1 = session is terminating, so unset the cookies */
386 int suppress_check, /**< 1 = suppress check for instant messages */
387 int cache /**< 1 = allow browser to cache this page */
392 wprintf("HTTP/1.1 200 OK\n");
393 http_datestring(httpnow, sizeof httpnow, time(NULL));
395 if (do_httpheaders) {
396 wprintf("Content-type: text/html; charset=utf-8\r\n"
398 "Connection: close\r\n",
399 SERVER, serv_info.serv_software
404 wprintf("Pragma: public\r\n"
405 "Cache-Control: max-age=3600, must-revalidate\r\n"
406 "Last-modified: %s\r\n",
411 wprintf("Pragma: no-cache\r\n"
412 "Cache-Control: no-store\r\n"
416 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
417 WC->wc_password, WC->wc_roomname);
420 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
422 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
423 if (server_cookie != NULL) {
424 wprintf("%s\n", server_cookie);
437 /** check for ImportantMessages (these display in a div overlaying the main screen) */
438 if (strlen(WC->ImportantMessage) > 0) {
439 wprintf("<div id=\"important_message\">\n");
440 wprintf("<span class=\"imsg\">"
441 "%s</span><br />\n", WC->ImportantMessage);
443 wprintf("<script type=\"text/javascript\">\n"
444 " setTimeout('hide_imsg_popup()', 3000); \n"
446 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
449 if ( (WC->logged_in) && (!unset_cookies) ) {
450 wprintf("<div id=\"iconbar\">");
451 do_selected_iconbar();
452 /** check for instant messages (these display in a new window) */
457 if (do_room_banner == 1) {
458 wprintf("<div id=\"banner\">\n");
459 embed_room_banner(NULL, navbar_default);
464 if (do_room_banner == 1) {
465 wprintf("<div id=\"content\">\n");
471 * \brief Generic function to do an HTTP redirect. Easy and fun.
472 * \param whichpage target url to 302 to
474 void http_redirect(char *whichpage) {
475 wprintf("HTTP/1.1 302 Moved Temporarily\n");
476 wprintf("Location: %s\r\n", whichpage);
477 wprintf("URI: %s\r\n", whichpage);
478 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
479 wprintf("<html><body>");
480 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
481 wprintf("</body></html>\n");
487 * \brief Output a piece of content to the web browser
489 void http_transmit_thing(char *thing, size_t length, char *content_type,
492 output_headers(0, 0, 0, 0, 0, is_static);
494 wprintf("Content-type: %s\r\n"
496 "Connection: close\r\n",
501 /** If we can send the data out compressed, please do so. */
503 char *compressed_data = NULL;
504 uLongf compressed_len;
506 compressed_len = (uLongf) ((length * 101) / 100) + 100;
507 compressed_data = malloc(compressed_len);
509 if (compress_gzip((Bytef *) compressed_data,
512 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
513 wprintf("Content-encoding: gzip\r\n"
514 "Content-length: %ld\r\n"
516 (long) compressed_len
518 client_write(compressed_data, (size_t)compressed_len);
519 free(compressed_data);
525 /** No compression ... just send it out as-is */
526 wprintf("Content-length: %ld\r\n"
530 client_write(thing, (size_t)length);
536 * \brief dump out static pages from disk
537 * \param what the file urs to print
539 void output_static(char *what)
545 char content_type[128];
547 fp = fopen(what, "rb");
549 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
550 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
551 wprintf("Content-Type: text/plain\r\n");
553 wprintf("Cannot open %s: %s\n", what, strerror(errno));
555 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
556 safestrncpy(content_type, "image/gif", sizeof content_type);
557 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
558 safestrncpy(content_type, "text/plain", sizeof content_type);
559 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
560 safestrncpy(content_type, "text/css", sizeof content_type);
561 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
562 safestrncpy(content_type, "image/jpeg", sizeof content_type);
563 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
564 safestrncpy(content_type, "image/png", sizeof content_type);
565 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
566 safestrncpy(content_type, "image/x-icon", sizeof content_type);
567 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
568 safestrncpy(content_type, "text/html", sizeof content_type);
569 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
570 safestrncpy(content_type, "text/html", sizeof content_type);
571 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
572 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
573 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
574 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
575 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
576 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
577 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
578 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
579 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
580 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
581 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
582 safestrncpy(content_type, "text/javascript", sizeof content_type);
584 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
586 fstat(fileno(fp), &statbuf);
587 bytes = statbuf.st_size;
588 bigbuffer = malloc(bytes + 2);
589 fread(bigbuffer, bytes, 1, fp);
592 lprintf(9, "output_static('%s') %s\n", what, content_type);
593 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
596 if (!strcasecmp(bstr("force_close_session"), "yes")) {
597 end_webcit_session();
603 * \brief When the browser requests an image file from the Citadel server,
604 * this function is called to transmit it.
609 char *xferbuf = NULL;
612 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
613 serv_getln(buf, sizeof buf);
615 bytes = extract_long(&buf[4], 0);
616 xferbuf = malloc(bytes + 2);
618 /** Read it from the server */
619 read_server_binary(xferbuf, bytes);
621 serv_getln(buf, sizeof buf);
623 /** Write it to the browser */
624 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
629 * Instead of an ugly 404, send a 1x1 transparent GIF
630 * when there's no such image on the server.
632 output_static("static/blank.gif");
640 * \brief Generic function to output an arbitrary MIME part from an arbitrary
641 * message number on the server.
643 * \param msgnum Number of the item on the citadel server
644 * \param partnum The MIME part to be output
645 * \param force_download Nonzero to force set the Content-Type: header
646 * to "application/octet-stream"
648 void mimepart(char *msgnum, char *partnum, int force_download)
652 char content_type[256];
653 char *content = NULL;
655 serv_printf("OPNA %s|%s", msgnum, partnum);
656 serv_getln(buf, sizeof buf);
658 bytes = extract_long(&buf[4], 0);
659 content = malloc(bytes + 2);
660 if (force_download) {
661 strcpy(content_type, "application/octet-stream");
664 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("DLAT %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 serv_read(content, bytes);
704 content[bytes] = 0; /* null terminate for good measure */
715 * \brief Convenience functions to display a page containing only a string
716 * \param titlebarcolor color of the titlebar of the frame
717 * \param titlebarmsg text to display in the title bar
718 * \param messagetext body of the box
720 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
722 wprintf("HTTP/1.1 200 OK\n");
723 output_headers(1, 1, 2, 0, 0, 0);
724 wprintf("<div id=\"banner\">\n");
725 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
726 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
727 wprintf("</td></tr></table>\n");
728 wprintf("</div>\n<div id=\"content\">\n");
729 escputs(messagetext);
737 * \brief Display a blank page.
739 void blank_page(void) {
740 output_headers(1, 1, 0, 0, 0, 0);
746 * \brief A template has been requested
748 void url_do_template(void) {
749 do_template(bstr("template"));
755 * \brief Offer to make any page the user's "start page."
757 void offer_start_page(void) {
758 wprintf("<a href=\"change_start_page?startpage=");
759 urlescputs(WC->this_page);
760 wprintf("\"><font size=-2 color=\"#AAAAAA\">");
761 wprintf(_("Make this my start page"));
762 wprintf("</font></a>");
764 wprintf("<br/><a href=\"rss?room=");
765 urlescputs(WC->wc_roomname);
766 wprintf("\" title=\"RSS 2.0 feed for ");
767 escputs(WC->wc_roomname);
768 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
774 * \brief Change the user's start page
776 void change_start_page(void) {
778 if (bstr("startpage") == NULL) {
779 safestrncpy(WC->ImportantMessage,
780 _("You no longer have a start page selected."),
781 sizeof WC->ImportantMessage);
786 set_preference("startpage", bstr("startpage"), 1);
788 output_headers(1, 1, 0, 0, 0, 0);
789 do_template("newstartpage");
796 * \brief convenience function to indicate success
797 * \param successmessage the mesage itself
799 void display_success(char *successmessage)
801 convenience_page("007700", "OK", successmessage);
806 * \brief Authorization required page
807 * This is probably temporary and should be revisited
808 * \param message message to put in header
810 void authorization_required(const char *message)
812 wprintf("HTTP/1.1 401 Authorization Required\r\n");
813 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
814 wprintf("Content-Type: text/html\r\n\r\n");
816 wprintf(_("Authorization Required"));
817 wprintf("</h1>\r\n");
818 wprintf(_("The resource you requested requires a valid username and password. "
819 "You could not be logged in: %s\n"), message);
824 * \brief This function is called by the MIME parser to handle data uploaded by
825 * the browser. Form data, uploaded files, and the data from HTTP PUT
826 * operations (such as those found in GroupDAV) all arrive this way.
828 * \param name Name of the item being uploaded
829 * \param filename Filename of the item being uploaded
830 * \param partnum MIME part identifier (not needed)
831 * \param disp MIME content disposition (not needed)
832 * \param content The actual data
833 * \param cbtype MIME content-type
834 * \param cbcharset Character set
835 * \param length Content length
836 * \param encoding MIME encoding type (not needed)
837 * \param userdata Not used here
839 void upload_handler(char *name, char *filename, char *partnum, char *disp,
840 void *content, char *cbtype, char *cbcharset,
841 size_t length, char *encoding, void *userdata)
843 struct urlcontent *u;
845 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
846 name, cbtype, length); */
849 if ( (length > 0) && (strlen(cbtype) == 0) ) {
850 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
851 u->next = WC->urlstrings;
853 safestrncpy(u->url_key, name, sizeof(u->url_key));
854 u->url_data = malloc(length + 1);
855 memcpy(u->url_data, content, length);
856 u->url_data[length] = 0;
859 /** Uploaded files */
860 if ( (length > 0) && (strlen(cbtype) > 0) ) {
861 WC->upload = malloc(length);
862 if (WC->upload != NULL) {
863 WC->upload_length = length;
864 safestrncpy(WC->upload_filename, filename,
865 sizeof(WC->upload_filename));
866 safestrncpy(WC->upload_content_type, cbtype,
867 sizeof(WC->upload_content_type));
868 memcpy(WC->upload, content, length);
871 lprintf(3, "malloc() failed: %s\n", strerror(errno));
878 * \brief Convenience functions to wrap around asynchronous ajax responses
880 void begin_ajax_response(void) {
881 output_headers(0, 0, 0, 0, 0, 0);
883 wprintf("Content-type: text/html; charset=UTF-8\r\n"
885 "Connection: close\r\n"
886 "Pragma: no-cache\r\n"
887 "Cache-Control: no-cache\r\n",
893 * \brief print ajax response footer
895 void end_ajax_response(void) {
901 * \brief Wraps a Citadel server command in an AJAX transaction.
903 void ajax_servcmd(void)
910 begin_ajax_response();
912 serv_printf("%s", bstr("g_cmd"));
913 serv_getln(buf, sizeof buf);
914 wprintf("%s\n", buf);
917 serv_printf("\n\n000");
919 if ((buf[0] == '1') || (buf[0] == '8')) {
920 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
921 wprintf("%s\n", gcontent);
926 text_to_server(bstr("g_input"));
932 serv_read(junk, len);
938 memset(junk, 0, len);
939 serv_write(junk, len);
946 * This is kind of an ugly hack, but this is the only place it can go.
947 * If the command was GEXP, then the instant messenger window must be
948 * running, so reset the "last_pager_check" watchdog timer so
949 * that page_popup() doesn't try to open it a second time.
951 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
952 WC->last_pager_check = time(NULL);
958 * \brief Helper function for the asynchronous check to see if we need
959 * to open the instant messenger window.
961 void seconds_since_last_gexp(void)
965 begin_ajax_response();
966 if ( (time(NULL) - WC->last_pager_check) < 30) {
971 serv_getln(buf, sizeof buf);
986 * \brief Entry point for WebCit transaction
988 void session_loop(struct httprequest *req)
1000 char request_method[128];
1001 char pathname[1024];
1003 int ContentLength = 0;
1005 char ContentType[512];
1006 char *content = NULL;
1007 char *content_end = NULL;
1008 struct httprequest *hptr;
1009 char browser_host[256];
1010 char user_agent[256];
1015 * We stuff these with the values coming from the client cookies,
1016 * so we can use them to reconnect a timed out session if we have to.
1018 char c_username[SIZ];
1019 char c_password[SIZ];
1020 char c_roomname[SIZ];
1021 char c_httpauth_string[SIZ];
1022 char c_httpauth_user[SIZ];
1023 char c_httpauth_pass[SIZ];
1026 safestrncpy(c_username, "", sizeof c_username);
1027 safestrncpy(c_password, "", sizeof c_password);
1028 safestrncpy(c_roomname, "", sizeof c_roomname);
1029 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1030 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1031 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1032 strcpy(browser_host, "");
1034 WC->upload_length = 0;
1040 if (hptr == NULL) return;
1042 safestrncpy(cmd, hptr->line, sizeof cmd);
1044 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1045 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1047 /** Figure out the action */
1048 extract_token(action, pathname, 1, '/', sizeof action);
1049 if (strstr(action, "?")) *strstr(action, "?") = 0;
1050 if (strstr(action, "&")) *strstr(action, "&") = 0;
1051 if (strstr(action, " ")) *strstr(action, " ") = 0;
1053 extract_token(arg1, pathname, 2, '/', sizeof arg1);
1054 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
1055 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
1056 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
1058 extract_token(arg2, pathname, 3, '/', sizeof arg2);
1059 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
1060 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
1061 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
1063 extract_token(arg3, pathname, 4, '/', sizeof arg3);
1064 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
1065 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
1066 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
1068 extract_token(arg4, pathname, 5, '/', sizeof arg4);
1069 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
1070 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
1071 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
1073 extract_token(arg5, pathname, 6, '/', sizeof arg5);
1074 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
1075 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
1076 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
1078 extract_token(arg6, pathname, 7, '/', sizeof arg6);
1079 if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
1080 if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
1081 if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
1083 extract_token(arg7, pathname, 8, '/', sizeof arg7);
1084 if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
1085 if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
1086 if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
1088 while (hptr != NULL) {
1089 safestrncpy(buf, hptr->line, sizeof buf);
1090 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1093 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1094 safestrncpy(cookie, &buf[15], sizeof cookie);
1095 cookie_to_stuff(cookie, NULL,
1096 c_username, sizeof c_username,
1097 c_password, sizeof c_password,
1098 c_roomname, sizeof c_roomname);
1100 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1101 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1102 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1103 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1105 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1106 ContentLength = atoi(&buf[16]);
1108 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1109 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1111 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1112 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1114 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1116 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1119 else if (!strncasecmp(buf, "Host: ", 6)) {
1120 if (strlen(WC->http_host) == 0) {
1121 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1124 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1125 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1126 while (num_tokens(browser_host, ',') > 1) {
1127 remove_token(browser_host, 0, ',');
1129 striplt(browser_host);
1131 /** Only WAP gateways explicitly name this content-type */
1132 else if (strstr(buf, "text/vnd.wap.wml")) {
1137 if (ContentLength > 0) {
1138 content = malloc(ContentLength + SIZ);
1139 memset(content, 0, ContentLength + SIZ);
1140 sprintf(content, "Content-type: %s\n"
1141 "Content-length: %d\n\n",
1142 ContentType, ContentLength);
1143 body_start = strlen(content);
1145 /** Read the entire input data at once. */
1146 client_read(WC->http_sock, &content[BytesRead+body_start],
1149 if (!strncasecmp(ContentType,
1150 "application/x-www-form-urlencoded", 33)) {
1151 addurls(&content[body_start]);
1152 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1153 content_end = content + ContentLength + body_start;
1154 mime_parser(content, content_end, *upload_handler,
1155 NULL, NULL, NULL, 0);
1161 /** make a note of where we are in case the user wants to save it */
1162 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1163 remove_token(WC->this_page, 2, ' ');
1164 remove_token(WC->this_page, 0, ' ');
1166 /** If there are variables in the URL, we must grab them now */
1167 for (a = 0; a < strlen(cmd); ++a) {
1168 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1169 for (b = a; b < strlen(cmd); ++b)
1170 if (isspace(cmd[b]))
1172 addurls(&cmd[a + 1]);
1177 /** If it's a "force 404" situation then display the error and bail. */
1178 if (!strcmp(action, "404")) {
1179 wprintf("HTTP/1.1 404 Not found\r\n");
1180 wprintf("Content-Type: text/plain\r\n");
1182 wprintf("Not found\r\n");
1183 goto SKIP_ALL_THIS_CRAP;
1186 /** Static content can be sent without connecting to Citadel. */
1188 for (a=0; a<ndirs; ++a) {
1189 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1194 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1195 action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1196 for (a=0; a<8; ++a) {
1197 if (buf[strlen(buf)-1] == '/') {
1198 buf[strlen(buf)-1] = 0;
1201 for (a = 0; a < strlen(buf); ++a) {
1202 if (isspace(buf[a])) {
1207 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1211 * If we're not connected to a Citadel server, try to hook up the
1214 if (!WC->connected) {
1215 if (!strcasecmp(ctdlhost, "uds")) {
1216 /* unix domain socket */
1217 sprintf(buf, "%s/citadel.socket", ctdlport);
1218 WC->serv_sock = uds_connectsock(buf);
1222 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1225 if (WC->serv_sock < 0) {
1227 goto SKIP_ALL_THIS_CRAP;
1231 serv_getln(buf, sizeof buf); /** get the server welcome message */
1234 * From what host is our user connecting? Go with
1235 * the host at the other end of the HTTP socket,
1236 * unless we are following X-Forwarded-For: headers
1237 * and such a header has already turned up something.
1239 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1240 locate_host(browser_host, WC->http_sock);
1243 get_serv_info(browser_host, user_agent);
1244 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1245 wprintf(_("You are connected to a Citadel "
1246 "server running Citadel %d.%02d. \n"
1247 "In order to run this version of WebCit "
1248 "you must also have Citadel %d.%02d or"
1250 serv_info.serv_rev_level / 100,
1251 serv_info.serv_rev_level % 100,
1252 MINIMUM_CIT_VERSION / 100,
1253 MINIMUM_CIT_VERSION % 100
1255 end_webcit_session();
1256 goto SKIP_ALL_THIS_CRAP;
1262 * Functions which can be performed without logging in
1264 if (!strcasecmp(action, "listsub")) {
1266 goto SKIP_ALL_THIS_CRAP;
1268 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1269 if (!strcasecmp(action, "freebusy")) {
1271 goto SKIP_ALL_THIS_CRAP;
1276 * If we're not logged in, but we have HTTP Authentication data,
1277 * try logging in to Citadel using that.
1279 if ((!WC->logged_in)
1280 && (strlen(c_httpauth_user) > 0)
1281 && (strlen(c_httpauth_pass) > 0)) {
1282 serv_printf("USER %s", c_httpauth_user);
1283 serv_getln(buf, sizeof buf);
1284 if (buf[0] == '3') {
1285 serv_printf("PASS %s", c_httpauth_pass);
1286 serv_getln(buf, sizeof buf);
1287 if (buf[0] == '2') {
1288 become_logged_in(c_httpauth_user,
1289 c_httpauth_pass, buf);
1290 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1291 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1293 /** Should only display when password is wrong */
1294 authorization_required(&buf[4]);
1295 goto SKIP_ALL_THIS_CRAP;
1300 /** This needs to run early */
1301 if (!strcasecmp(action, "rss")) {
1302 display_rss(bstr("room"), request_method);
1303 goto SKIP_ALL_THIS_CRAP;
1307 * The GroupDAV stuff relies on HTTP authentication instead of
1308 * our session's authentication.
1310 if (!strncasecmp(action, "groupdav", 8)) {
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;
1321 * Automatically send requests with any method other than GET or
1322 * POST to the GroupDAV code as well.
1324 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1325 groupdav_main(req, ContentType, /** do GroupDAV methods */
1326 ContentLength, content+body_start);
1327 if (!WC->logged_in) {
1328 WC->killthis = 1; /** If not logged in, don't */
1329 } /** keep the session active */
1330 goto SKIP_ALL_THIS_CRAP;
1334 * If we're not logged in, but we have username and password cookies
1335 * supplied by the browser, try using them to log in.
1337 if ((!WC->logged_in)
1338 && (strlen(c_username) > 0)
1339 && (strlen(c_password) > 0)) {
1340 serv_printf("USER %s", c_username);
1341 serv_getln(buf, sizeof buf);
1342 if (buf[0] == '3') {
1343 serv_printf("PASS %s", c_password);
1344 serv_getln(buf, sizeof buf);
1345 if (buf[0] == '2') {
1346 become_logged_in(c_username, c_password, buf);
1351 * If we don't have a current room, but a cookie specifying the
1352 * current room is supplied, make an effort to go there.
1354 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1355 serv_printf("GOTO %s", c_roomname);
1356 serv_getln(buf, sizeof buf);
1357 if (buf[0] == '2') {
1358 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1362 if (!strcasecmp(action, "image")) {
1366 * All functions handled below this point ... make sure we log in
1367 * before doing anything else!
1369 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1371 } else if (!WC->logged_in) {
1372 display_login(NULL);
1376 * Various commands...
1379 else if (!strcasecmp(action, "do_welcome")) {
1381 } else if (!strcasecmp(action, "blank")) {
1383 } else if (!strcasecmp(action, "do_template")) {
1385 } else if (!strcasecmp(action, "display_aide_menu")) {
1386 display_aide_menu();
1387 } else if (!strcasecmp(action, "display_main_menu")) {
1388 display_main_menu();
1389 } else if (!strcasecmp(action, "who")) {
1391 } else if (!strcasecmp(action, "sslg")) {
1392 seconds_since_last_gexp();
1393 } else if (!strcasecmp(action, "who_inner_html")) {
1394 begin_ajax_response();
1396 end_ajax_response();
1397 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1398 begin_ajax_response();
1400 end_ajax_response();
1401 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1402 begin_ajax_response();
1403 do_iconbar_roomlist();
1404 end_ajax_response();
1405 } else if (!strcasecmp(action, "knrooms")) {
1407 } else if (!strcasecmp(action, "gotonext")) {
1410 } else if (!strcasecmp(action, "skip")) {
1412 } else if (!strcasecmp(action, "ungoto")) {
1414 } else if (!strcasecmp(action, "dotgoto")) {
1415 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1418 smart_goto(bstr("room"));
1419 } else if (!strcasecmp(action, "dotskip")) {
1420 smart_goto(bstr("room"));
1421 } else if (!strcasecmp(action, "termquit")) {
1423 } else if (!strcasecmp(action, "readnew")) {
1424 readloop("readnew");
1425 } else if (!strcasecmp(action, "readold")) {
1426 readloop("readold");
1427 } else if (!strcasecmp(action, "readfwd")) {
1428 readloop("readfwd");
1429 } else if (!strcasecmp(action, "headers")) {
1430 readloop("headers");
1431 } else if (!strcasecmp(action, "do_search")) {
1432 readloop("do_search");
1433 } else if (!strcasecmp(action, "msg")) {
1434 embed_message(arg1);
1435 } else if (!strcasecmp(action, "printmsg")) {
1436 print_message(arg1);
1437 } else if (!strcasecmp(action, "msgheaders")) {
1438 display_headers(arg1);
1439 } else if (!strcasecmp(action, "wiki")) {
1440 display_wiki_page();
1441 } else if (!strcasecmp(action, "display_enter")) {
1443 } else if (!strcasecmp(action, "post")) {
1445 } else if (!strcasecmp(action, "move_msg")) {
1447 } else if (!strcasecmp(action, "delete_msg")) {
1449 } else if (!strcasecmp(action, "userlist")) {
1451 } else if (!strcasecmp(action, "showuser")) {
1453 } else if (!strcasecmp(action, "display_page")) {
1455 } else if (!strcasecmp(action, "page_user")) {
1457 } else if (!strcasecmp(action, "chat")) {
1459 } else if (!strcasecmp(action, "display_private")) {
1460 display_private("", 0);
1461 } else if (!strcasecmp(action, "goto_private")) {
1463 } else if (!strcasecmp(action, "zapped_list")) {
1465 } else if (!strcasecmp(action, "display_zap")) {
1467 } else if (!strcasecmp(action, "zap")) {
1469 } else if (!strcasecmp(action, "display_entroom")) {
1471 } else if (!strcasecmp(action, "entroom")) {
1473 } else if (!strcasecmp(action, "display_whok")) {
1475 } else if (!strcasecmp(action, "do_invt_kick")) {
1477 } else if (!strcasecmp(action, "display_editroom")) {
1479 } else if (!strcasecmp(action, "netedit")) {
1481 } else if (!strcasecmp(action, "editroom")) {
1483 } else if (!strcasecmp(action, "display_editinfo")) {
1484 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1485 } else if (!strcasecmp(action, "editinfo")) {
1486 save_edit(_("Room info"), "EINF 1", 1);
1487 } else if (!strcasecmp(action, "display_editbio")) {
1488 sprintf(buf, "RBIO %s", WC->wc_fullname);
1489 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1490 } else if (!strcasecmp(action, "editbio")) {
1491 save_edit(_("Your bio"), "EBIO", 0);
1492 } else if (!strcasecmp(action, "confirm_move_msg")) {
1494 } else if (!strcasecmp(action, "delete_room")) {
1496 } else if (!strcasecmp(action, "validate")) {
1498 } else if (!strcasecmp(action, "display_editpic")) {
1499 display_graphics_upload(_("your photo"),
1502 } else if (!strcasecmp(action, "editpic")) {
1503 do_graphics_upload("UIMG 1|_userpic_");
1504 } else if (!strcasecmp(action, "display_editroompic")) {
1505 display_graphics_upload(_("the icon for this room"),
1508 } else if (!strcasecmp(action, "editroompic")) {
1509 do_graphics_upload("UIMG 1|_roompic_");
1510 } else if (!strcasecmp(action, "delete_floor")) {
1512 } else if (!strcasecmp(action, "rename_floor")) {
1514 } else if (!strcasecmp(action, "create_floor")) {
1516 } else if (!strcasecmp(action, "display_editfloorpic")) {
1517 sprintf(buf, "UIMG 0|_floorpic_|%s",
1518 bstr("which_floor"));
1519 display_graphics_upload(_("the icon for this floor"),
1522 } else if (!strcasecmp(action, "editfloorpic")) {
1523 sprintf(buf, "UIMG 1|_floorpic_|%s",
1524 bstr("which_floor"));
1525 do_graphics_upload(buf);
1526 } else if (!strcasecmp(action, "display_reg")) {
1528 } else if (!strcasecmp(action, "display_changepw")) {
1530 } else if (!strcasecmp(action, "changepw")) {
1532 } else if (!strcasecmp(action, "display_edit_node")) {
1533 display_edit_node();
1534 } else if (!strcasecmp(action, "edit_node")) {
1536 } else if (!strcasecmp(action, "display_netconf")) {
1538 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1539 display_confirm_delete_node();
1540 } else if (!strcasecmp(action, "delete_node")) {
1542 } else if (!strcasecmp(action, "display_add_node")) {
1544 } else if (!strcasecmp(action, "add_node")) {
1546 } else if (!strcasecmp(action, "terminate_session")) {
1548 terminate_session();
1549 } else if (!strcasecmp(action, "edit_me")) {
1551 } else if (!strcasecmp(action, "display_siteconfig")) {
1552 display_siteconfig();
1553 } else if (!strcasecmp(action, "chat_recv")) {
1555 } else if (!strcasecmp(action, "chat_send")) {
1557 } else if (!strcasecmp(action, "siteconfig")) {
1559 } else if (!strcasecmp(action, "display_generic")) {
1561 } else if (!strcasecmp(action, "do_generic")) {
1563 } else if (!strcasecmp(action, "ajax_servcmd")) {
1565 } else if (!strcasecmp(action, "display_menubar")) {
1567 } else if (!strcasecmp(action, "mimepart")) {
1568 mimepart(arg1, arg2, 0);
1569 } else if (!strcasecmp(action, "mimepart_download")) {
1570 mimepart(arg1, arg2, 1);
1571 } else if (!strcasecmp(action, "edit_vcard")) {
1573 } else if (!strcasecmp(action, "submit_vcard")) {
1575 } else if (!strcasecmp(action, "select_user_to_edit")) {
1576 select_user_to_edit(NULL, NULL);
1577 } else if (!strcasecmp(action, "display_edituser")) {
1578 display_edituser(NULL, 0);
1579 } else if (!strcasecmp(action, "edituser")) {
1581 } else if (!strcasecmp(action, "create_user")) {
1583 } else if (!strcasecmp(action, "changeview")) {
1585 } else if (!strcasecmp(action, "change_start_page")) {
1586 change_start_page();
1587 } else if (!strcasecmp(action, "display_floorconfig")) {
1588 display_floorconfig(NULL);
1589 } else if (!strcasecmp(action, "toggle_self_service")) {
1590 toggle_self_service();
1591 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1592 } else if (!strcasecmp(action, "display_edit_task")) {
1593 display_edit_task();
1594 } else if (!strcasecmp(action, "save_task")) {
1596 } else if (!strcasecmp(action, "display_edit_event")) {
1597 display_edit_event();
1598 } else if (!strcasecmp(action, "save_event")) {
1600 } else if (!strcasecmp(action, "respond_to_request")) {
1601 respond_to_request();
1602 } else if (!strcasecmp(action, "handle_rsvp")) {
1605 } else if (!strcasecmp(action, "summary")) {
1607 } else if (!strcasecmp(action, "summary_inner_div")) {
1608 begin_ajax_response();
1609 summary_inner_div();
1610 end_ajax_response();
1611 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1612 display_customize_iconbar();
1613 } else if (!strcasecmp(action, "commit_iconbar")) {
1615 } else if (!strcasecmp(action, "set_room_policy")) {
1617 } else if (!strcasecmp(action, "display_inetconf")) {
1619 } else if (!strcasecmp(action, "save_inetconf")) {
1621 } else if (!strcasecmp(action, "display_smtpqueue")) {
1622 display_smtpqueue();
1623 } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1624 display_smtpqueue_inner_div();
1625 } else if (!strcasecmp(action, "display_sieve")) {
1627 } else if (!strcasecmp(action, "setup_wizard")) {
1629 } else if (!strcasecmp(action, "display_preferences")) {
1630 display_preferences();
1631 } else if (!strcasecmp(action, "set_preferences")) {
1633 } else if (!strcasecmp(action, "recp_autocomplete")) {
1634 recp_autocomplete(bstr("recp"));
1635 } else if (!strcasecmp(action, "cc_autocomplete")) {
1636 recp_autocomplete(bstr("cc"));
1637 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1638 recp_autocomplete(bstr("bcc"));
1639 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1640 set_floordiv_expanded(arg1);
1641 } else if (!strcasecmp(action, "diagnostics")) {
1642 output_headers(1, 1, 1, 0, 0, 0);
1643 wprintf("Session: %d<hr />\n", WC->wc_session);
1644 wprintf("Command: <br /><PRE>\n");
1646 wprintf("</PRE><hr />\n");
1647 wprintf("Variables: <br /><PRE>\n");
1649 wprintf("</PRE><hr />\n");
1651 } else if (!strcasecmp(action, "updatenote")) {
1655 /** When all else fais, display the main menu. */
1657 display_main_menu();
1662 if (content != NULL) {
1667 if (WC->upload_length > 0) {
1669 WC->upload_length = 0;