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 * 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.
651 * \param msgnum Number of the item on the citadel server
652 * \param partnum The MIME part to be output
653 * \param force_download Nonzero to force set the Content-Type: header
654 * to "application/octet-stream"
656 void mimepart(char *msgnum, char *partnum, int force_download)
660 char content_type[256];
661 char *content = NULL;
663 serv_printf("OPNA %s|%s", msgnum, partnum);
664 serv_getln(buf, sizeof buf);
666 bytes = extract_long(&buf[4], 0);
667 content = malloc(bytes + 2);
668 if (force_download) {
669 strcpy(content_type, "application/octet-stream");
672 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
674 output_headers(0, 0, 0, 0, 0, 0);
675 read_server_binary(content, bytes);
677 serv_getln(buf, sizeof buf);
678 http_transmit_thing(content, bytes, content_type, 0);
681 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
682 output_headers(0, 0, 0, 0, 0, 0);
683 wprintf("Content-Type: text/plain\r\n");
685 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
692 * \brief Read any MIME part of a message, from the server, into memory.
693 * \param msgnum number of the message on the citadel server
694 * \param partnum the MIME part to be loaded
696 char *load_mimepart(long msgnum, char *partnum)
700 char content_type[SIZ];
703 serv_printf("OPNA %ld|%s", msgnum, partnum);
704 serv_getln(buf, sizeof buf);
706 bytes = extract_long(&buf[4], 0);
707 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
709 content = malloc(bytes + 2);
710 read_server_binary(content, bytes);
713 serv_getln(buf, sizeof buf);
714 content[bytes] = 0; /* null terminate for good measure */
725 * \brief Convenience functions to display a page containing only a string
726 * \param titlebarcolor color of the titlebar of the frame
727 * \param titlebarmsg text to display in the title bar
728 * \param messagetext body of the box
730 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
732 wprintf("HTTP/1.1 200 OK\n");
733 output_headers(1, 1, 2, 0, 0, 0);
734 wprintf("<div id=\"banner\">\n");
735 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
736 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
737 wprintf("</td></tr></table>\n");
738 wprintf("</div>\n<div id=\"content\">\n");
739 escputs(messagetext);
747 * \brief Display a blank page.
749 void blank_page(void) {
750 output_headers(1, 1, 0, 0, 0, 0);
756 * \brief A template has been requested
758 void url_do_template(void) {
759 do_template(bstr("template"));
765 * \brief Offer to make any page the user's "start page."
767 void offer_start_page(void) {
768 wprintf("<a href=\"change_start_page?startpage=");
769 urlescputs(WC->this_page);
770 wprintf("\"><font size=-2 color=\"#AAAAAA\">");
771 wprintf(_("Make this my start page"));
772 wprintf("</font></a>");
774 wprintf("<br/><a href=\"rss?room=");
775 urlescputs(WC->wc_roomname);
776 wprintf("\" title=\"RSS 2.0 feed for ");
777 escputs(WC->wc_roomname);
778 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
784 * \brief Change the user's start page
786 void change_start_page(void) {
788 if (bstr("startpage") == NULL) {
789 safestrncpy(WC->ImportantMessage,
790 _("You no longer have a start page selected."),
791 sizeof WC->ImportantMessage);
796 set_preference("startpage", bstr("startpage"), 1);
798 output_headers(1, 1, 0, 0, 0, 0);
799 do_template("newstartpage");
806 * \brief convenience function to indicate success
807 * \param successmessage the mesage itself
809 void display_success(char *successmessage)
811 convenience_page("007700", "OK", successmessage);
816 * \brief Authorization required page
817 * This is probably temporary and should be revisited
818 * \param message message to put in header
820 void authorization_required(const char *message)
822 wprintf("HTTP/1.1 401 Authorization Required\r\n");
823 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
824 wprintf("Content-Type: text/html\r\n\r\n");
826 wprintf(_("Authorization Required"));
827 wprintf("</h1>\r\n");
828 wprintf(_("The resource you requested requires a valid username and password. "
829 "You could not be logged in: %s\n"), message);
834 * \brief This function is called by the MIME parser to handle data uploaded by
835 * the browser. Form data, uploaded files, and the data from HTTP PUT
836 * operations (such as those found in GroupDAV) all arrive this way.
838 * \param name Name of the item being uploaded
839 * \param filename Filename of the item being uploaded
840 * \param partnum MIME part identifier (not needed)
841 * \param disp MIME content disposition (not needed)
842 * \param content The actual data
843 * \param cbtype MIME content-type
844 * \param cbcharset Character set
845 * \param length Content length
846 * \param encoding MIME encoding type (not needed)
847 * \param userdata Not used here
849 void upload_handler(char *name, char *filename, char *partnum, char *disp,
850 void *content, char *cbtype, char *cbcharset,
851 size_t length, char *encoding, void *userdata)
853 struct urlcontent *u;
855 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
856 name, cbtype, length); */
859 if ( (length > 0) && (strlen(cbtype) == 0) ) {
860 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
861 u->next = WC->urlstrings;
863 safestrncpy(u->url_key, name, sizeof(u->url_key));
864 u->url_data = malloc(length + 1);
865 memcpy(u->url_data, content, length);
866 u->url_data[length] = 0;
869 /** Uploaded files */
870 if ( (length > 0) && (strlen(cbtype) > 0) ) {
871 WC->upload = malloc(length);
872 if (WC->upload != NULL) {
873 WC->upload_length = length;
874 safestrncpy(WC->upload_filename, filename,
875 sizeof(WC->upload_filename));
876 safestrncpy(WC->upload_content_type, cbtype,
877 sizeof(WC->upload_content_type));
878 memcpy(WC->upload, content, length);
881 lprintf(3, "malloc() failed: %s\n", strerror(errno));
888 * \brief Convenience functions to wrap around asynchronous ajax responses
890 void begin_ajax_response(void) {
891 output_headers(0, 0, 0, 0, 0, 0);
893 wprintf("Content-type: text/html; charset=UTF-8\r\n"
895 "Connection: close\r\n"
896 "Pragma: no-cache\r\n"
897 "Cache-Control: no-cache\r\n",
903 * \brief print ajax response footer
905 void end_ajax_response(void) {
911 * \brief Wraps a Citadel server command in an AJAX transaction.
913 void ajax_servcmd(void)
920 begin_ajax_response();
922 serv_printf("%s", bstr("g_cmd"));
923 serv_getln(buf, sizeof buf);
924 wprintf("%s\n", buf);
927 serv_printf("\n\n000");
929 if ((buf[0] == '1') || (buf[0] == '8')) {
930 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
931 wprintf("%s\n", gcontent);
936 text_to_server(bstr("g_input"));
942 serv_read(junk, len);
948 memset(junk, 0, len);
949 serv_write(junk, len);
956 * This is kind of an ugly hack, but this is the only place it can go.
957 * If the command was GEXP, then the instant messenger window must be
958 * running, so reset the "last_pager_check" watchdog timer so
959 * that page_popup() doesn't try to open it a second time.
961 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
962 WC->last_pager_check = time(NULL);
968 * \brief Helper function for the asynchronous check to see if we need
969 * to open the instant messenger window.
971 void seconds_since_last_gexp(void)
975 begin_ajax_response();
976 if ( (time(NULL) - WC->last_pager_check) < 30) {
981 serv_getln(buf, sizeof buf);
996 * \brief Entry point for WebCit transaction
998 void session_loop(struct httprequest *req)
1010 char request_method[128];
1011 char pathname[1024];
1013 int ContentLength = 0;
1015 char ContentType[512];
1016 char *content = NULL;
1017 char *content_end = NULL;
1018 struct httprequest *hptr;
1019 char browser_host[256];
1020 char user_agent[256];
1025 * We stuff these with the values coming from the client cookies,
1026 * so we can use them to reconnect a timed out session if we have to.
1028 char c_username[SIZ];
1029 char c_password[SIZ];
1030 char c_roomname[SIZ];
1031 char c_httpauth_string[SIZ];
1032 char c_httpauth_user[SIZ];
1033 char c_httpauth_pass[SIZ];
1036 safestrncpy(c_username, "", sizeof c_username);
1037 safestrncpy(c_password, "", sizeof c_password);
1038 safestrncpy(c_roomname, "", sizeof c_roomname);
1039 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1040 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1041 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1042 strcpy(browser_host, "");
1044 WC->upload_length = 0;
1050 if (hptr == NULL) return;
1052 safestrncpy(cmd, hptr->line, sizeof cmd);
1054 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1055 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1057 /** Figure out the action */
1058 extract_token(action, pathname, 1, '/', sizeof action);
1059 if (strstr(action, "?")) *strstr(action, "?") = 0;
1060 if (strstr(action, "&")) *strstr(action, "&") = 0;
1061 if (strstr(action, " ")) *strstr(action, " ") = 0;
1063 extract_token(arg1, pathname, 2, '/', sizeof arg1);
1064 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
1065 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
1066 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
1068 extract_token(arg2, pathname, 3, '/', sizeof arg2);
1069 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
1070 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
1071 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
1073 extract_token(arg3, pathname, 4, '/', sizeof arg3);
1074 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
1075 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
1076 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
1078 extract_token(arg4, pathname, 5, '/', sizeof arg4);
1079 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
1080 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
1081 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
1083 extract_token(arg5, pathname, 6, '/', sizeof arg5);
1084 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
1085 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
1086 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
1088 extract_token(arg6, pathname, 7, '/', sizeof arg6);
1089 if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
1090 if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
1091 if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
1093 extract_token(arg7, pathname, 8, '/', sizeof arg7);
1094 if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
1095 if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
1096 if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
1098 while (hptr != NULL) {
1099 safestrncpy(buf, hptr->line, sizeof buf);
1100 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1103 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1104 safestrncpy(cookie, &buf[15], sizeof cookie);
1105 cookie_to_stuff(cookie, NULL,
1106 c_username, sizeof c_username,
1107 c_password, sizeof c_password,
1108 c_roomname, sizeof c_roomname);
1110 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1111 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1112 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1113 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1115 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1116 ContentLength = atoi(&buf[16]);
1118 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1119 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1121 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1122 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1124 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1126 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1129 else if (!strncasecmp(buf, "Host: ", 6)) {
1130 if (strlen(WC->http_host) == 0) {
1131 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1134 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1135 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1136 while (num_tokens(browser_host, ',') > 1) {
1137 remove_token(browser_host, 0, ',');
1139 striplt(browser_host);
1141 /** Only WAP gateways explicitly name this content-type */
1142 else if (strstr(buf, "text/vnd.wap.wml")) {
1147 if (ContentLength > 0) {
1148 content = malloc(ContentLength + SIZ);
1149 memset(content, 0, ContentLength + SIZ);
1150 sprintf(content, "Content-type: %s\n"
1151 "Content-length: %d\n\n",
1152 ContentType, ContentLength);
1153 body_start = strlen(content);
1155 /** Read the entire input data at once. */
1156 client_read(WC->http_sock, &content[BytesRead+body_start],
1159 if (!strncasecmp(ContentType,
1160 "application/x-www-form-urlencoded", 33)) {
1161 addurls(&content[body_start]);
1162 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1163 content_end = content + ContentLength + body_start;
1164 mime_parser(content, content_end, *upload_handler,
1165 NULL, NULL, NULL, 0);
1171 /** make a note of where we are in case the user wants to save it */
1172 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1173 remove_token(WC->this_page, 2, ' ');
1174 remove_token(WC->this_page, 0, ' ');
1176 /** If there are variables in the URL, we must grab them now */
1177 for (a = 0; a < strlen(cmd); ++a) {
1178 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1179 for (b = a; b < strlen(cmd); ++b)
1180 if (isspace(cmd[b]))
1182 addurls(&cmd[a + 1]);
1187 /** If it's a "force 404" situation then display the error and bail. */
1188 if (!strcmp(action, "404")) {
1189 wprintf("HTTP/1.1 404 Not found\r\n");
1190 wprintf("Content-Type: text/plain\r\n");
1192 wprintf("Not found\r\n");
1193 goto SKIP_ALL_THIS_CRAP;
1196 /** Static content can be sent without connecting to Citadel. */
1198 for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
1199 if (!strcasecmp(action, static_content_dirs[a])) {
1204 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1205 action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1206 for (a=0; a<8; ++a) {
1207 if (buf[strlen(buf)-1] == '/') {
1208 buf[strlen(buf)-1] = 0;
1211 for (a = 0; a < strlen(buf); ++a) {
1212 if (isspace(buf[a])) {
1217 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1221 * If we're not connected to a Citadel server, try to hook up the
1224 if (!WC->connected) {
1225 if (!strcasecmp(ctdlhost, "uds")) {
1226 /* unix domain socket */
1227 sprintf(buf, "%s/citadel.socket", ctdlport);
1228 WC->serv_sock = uds_connectsock(buf);
1232 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1235 if (WC->serv_sock < 0) {
1237 goto SKIP_ALL_THIS_CRAP;
1241 serv_getln(buf, sizeof buf); /** get the server welcome message */
1244 * From what host is our user connecting? Go with
1245 * the host at the other end of the HTTP socket,
1246 * unless we are following X-Forwarded-For: headers
1247 * and such a header has already turned up something.
1249 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1250 locate_host(browser_host, WC->http_sock);
1253 get_serv_info(browser_host, user_agent);
1254 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1255 wprintf(_("You are connected to a Citadel "
1256 "server running Citadel %d.%02d. \n"
1257 "In order to run this version of WebCit "
1258 "you must also have Citadel %d.%02d or"
1260 serv_info.serv_rev_level / 100,
1261 serv_info.serv_rev_level % 100,
1262 MINIMUM_CIT_VERSION / 100,
1263 MINIMUM_CIT_VERSION % 100
1265 end_webcit_session();
1266 goto SKIP_ALL_THIS_CRAP;
1272 * Functions which can be performed without logging in
1274 if (!strcasecmp(action, "listsub")) {
1276 goto SKIP_ALL_THIS_CRAP;
1278 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1279 if (!strcasecmp(action, "freebusy")) {
1281 goto SKIP_ALL_THIS_CRAP;
1286 * If we're not logged in, but we have HTTP Authentication data,
1287 * try logging in to Citadel using that.
1289 if ((!WC->logged_in)
1290 && (strlen(c_httpauth_user) > 0)
1291 && (strlen(c_httpauth_pass) > 0)) {
1292 serv_printf("USER %s", c_httpauth_user);
1293 serv_getln(buf, sizeof buf);
1294 if (buf[0] == '3') {
1295 serv_printf("PASS %s", c_httpauth_pass);
1296 serv_getln(buf, sizeof buf);
1297 if (buf[0] == '2') {
1298 become_logged_in(c_httpauth_user,
1299 c_httpauth_pass, buf);
1300 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1301 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1303 /** Should only display when password is wrong */
1304 authorization_required(&buf[4]);
1305 goto SKIP_ALL_THIS_CRAP;
1310 /** This needs to run early */
1311 if (!strcasecmp(action, "rss")) {
1312 display_rss(bstr("room"), request_method);
1313 goto SKIP_ALL_THIS_CRAP;
1317 * The GroupDAV stuff relies on HTTP authentication instead of
1318 * our session's authentication.
1320 if (!strncasecmp(action, "groupdav", 8)) {
1321 groupdav_main(req, ContentType, /* do GroupDAV methods */
1322 ContentLength, content+body_start);
1323 if (!WC->logged_in) {
1324 WC->killthis = 1; /* If not logged in, don't */
1325 } /* keep the session active */
1326 goto SKIP_ALL_THIS_CRAP;
1331 * Automatically send requests with any method other than GET or
1332 * POST to the GroupDAV code as well.
1334 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1335 groupdav_main(req, ContentType, /** do GroupDAV methods */
1336 ContentLength, content+body_start);
1337 if (!WC->logged_in) {
1338 WC->killthis = 1; /** If not logged in, don't */
1339 } /** keep the session active */
1340 goto SKIP_ALL_THIS_CRAP;
1344 * If we're not logged in, but we have username and password cookies
1345 * supplied by the browser, try using them to log in.
1347 if ((!WC->logged_in)
1348 && (strlen(c_username) > 0)
1349 && (strlen(c_password) > 0)) {
1350 serv_printf("USER %s", c_username);
1351 serv_getln(buf, sizeof buf);
1352 if (buf[0] == '3') {
1353 serv_printf("PASS %s", c_password);
1354 serv_getln(buf, sizeof buf);
1355 if (buf[0] == '2') {
1356 become_logged_in(c_username, c_password, buf);
1361 * If we don't have a current room, but a cookie specifying the
1362 * current room is supplied, make an effort to go there.
1364 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1365 serv_printf("GOTO %s", c_roomname);
1366 serv_getln(buf, sizeof buf);
1367 if (buf[0] == '2') {
1368 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1372 if (!strcasecmp(action, "image")) {
1376 * All functions handled below this point ... make sure we log in
1377 * before doing anything else!
1379 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1381 } else if (!WC->logged_in) {
1382 display_login(NULL);
1386 * Various commands...
1389 else if (!strcasecmp(action, "do_welcome")) {
1391 } else if (!strcasecmp(action, "blank")) {
1393 } else if (!strcasecmp(action, "do_template")) {
1395 } else if (!strcasecmp(action, "display_aide_menu")) {
1396 display_aide_menu();
1397 } else if (!strcasecmp(action, "display_main_menu")) {
1398 display_main_menu();
1399 } else if (!strcasecmp(action, "who")) {
1401 } else if (!strcasecmp(action, "sslg")) {
1402 seconds_since_last_gexp();
1403 } else if (!strcasecmp(action, "who_inner_html")) {
1404 begin_ajax_response();
1406 end_ajax_response();
1407 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1408 begin_ajax_response();
1410 end_ajax_response();
1411 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1412 begin_ajax_response();
1413 do_iconbar_roomlist();
1414 end_ajax_response();
1415 } else if (!strcasecmp(action, "knrooms")) {
1417 } else if (!strcasecmp(action, "gotonext")) {
1420 } else if (!strcasecmp(action, "skip")) {
1422 } else if (!strcasecmp(action, "ungoto")) {
1424 } else if (!strcasecmp(action, "dotgoto")) {
1425 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1428 smart_goto(bstr("room"));
1429 } else if (!strcasecmp(action, "dotskip")) {
1430 smart_goto(bstr("room"));
1431 } else if (!strcasecmp(action, "termquit")) {
1433 } else if (!strcasecmp(action, "readnew")) {
1434 readloop("readnew");
1435 } else if (!strcasecmp(action, "readold")) {
1436 readloop("readold");
1437 } else if (!strcasecmp(action, "readfwd")) {
1438 readloop("readfwd");
1439 } else if (!strcasecmp(action, "headers")) {
1440 readloop("headers");
1441 } else if (!strcasecmp(action, "msg")) {
1442 embed_message(arg1);
1443 } else if (!strcasecmp(action, "printmsg")) {
1444 print_message(arg1);
1445 } else if (!strcasecmp(action, "msgheaders")) {
1446 display_headers(arg1);
1447 } else if (!strcasecmp(action, "wiki")) {
1448 display_wiki_page();
1449 } else if (!strcasecmp(action, "display_enter")) {
1451 } else if (!strcasecmp(action, "post")) {
1453 } else if (!strcasecmp(action, "move_msg")) {
1455 } else if (!strcasecmp(action, "delete_msg")) {
1457 } else if (!strcasecmp(action, "userlist")) {
1459 } else if (!strcasecmp(action, "showuser")) {
1461 } else if (!strcasecmp(action, "display_page")) {
1463 } else if (!strcasecmp(action, "page_user")) {
1465 } else if (!strcasecmp(action, "chat")) {
1467 } else if (!strcasecmp(action, "display_private")) {
1468 display_private("", 0);
1469 } else if (!strcasecmp(action, "goto_private")) {
1471 } else if (!strcasecmp(action, "zapped_list")) {
1473 } else if (!strcasecmp(action, "display_zap")) {
1475 } else if (!strcasecmp(action, "zap")) {
1477 } else if (!strcasecmp(action, "display_entroom")) {
1479 } else if (!strcasecmp(action, "entroom")) {
1481 } else if (!strcasecmp(action, "display_whok")) {
1483 } else if (!strcasecmp(action, "do_invt_kick")) {
1485 } else if (!strcasecmp(action, "display_editroom")) {
1487 } else if (!strcasecmp(action, "netedit")) {
1489 } else if (!strcasecmp(action, "editroom")) {
1491 } else if (!strcasecmp(action, "display_editinfo")) {
1492 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1493 } else if (!strcasecmp(action, "editinfo")) {
1494 save_edit(_("Room info"), "EINF 1", 1);
1495 } else if (!strcasecmp(action, "display_editbio")) {
1496 sprintf(buf, "RBIO %s", WC->wc_fullname);
1497 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1498 } else if (!strcasecmp(action, "editbio")) {
1499 save_edit(_("Your bio"), "EBIO", 0);
1500 } else if (!strcasecmp(action, "confirm_move_msg")) {
1502 } else if (!strcasecmp(action, "delete_room")) {
1504 } else if (!strcasecmp(action, "validate")) {
1506 } else if (!strcasecmp(action, "display_editpic")) {
1507 display_graphics_upload(_("your photo"),
1510 } else if (!strcasecmp(action, "editpic")) {
1511 do_graphics_upload("UIMG 1|_userpic_");
1512 } else if (!strcasecmp(action, "display_editroompic")) {
1513 display_graphics_upload(_("the icon for this room"),
1516 } else if (!strcasecmp(action, "editroompic")) {
1517 do_graphics_upload("UIMG 1|_roompic_");
1518 } else if (!strcasecmp(action, "delete_floor")) {
1520 } else if (!strcasecmp(action, "rename_floor")) {
1522 } else if (!strcasecmp(action, "create_floor")) {
1524 } else if (!strcasecmp(action, "display_editfloorpic")) {
1525 sprintf(buf, "UIMG 0|_floorpic_|%s",
1526 bstr("which_floor"));
1527 display_graphics_upload(_("the icon for this floor"),
1530 } else if (!strcasecmp(action, "editfloorpic")) {
1531 sprintf(buf, "UIMG 1|_floorpic_|%s",
1532 bstr("which_floor"));
1533 do_graphics_upload(buf);
1534 } else if (!strcasecmp(action, "display_reg")) {
1536 } else if (!strcasecmp(action, "display_changepw")) {
1538 } else if (!strcasecmp(action, "changepw")) {
1540 } else if (!strcasecmp(action, "display_edit_node")) {
1541 display_edit_node();
1542 } else if (!strcasecmp(action, "edit_node")) {
1544 } else if (!strcasecmp(action, "display_netconf")) {
1546 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1547 display_confirm_delete_node();
1548 } else if (!strcasecmp(action, "delete_node")) {
1550 } else if (!strcasecmp(action, "display_add_node")) {
1552 } else if (!strcasecmp(action, "add_node")) {
1554 } else if (!strcasecmp(action, "terminate_session")) {
1556 terminate_session();
1557 } else if (!strcasecmp(action, "edit_me")) {
1559 } else if (!strcasecmp(action, "display_siteconfig")) {
1560 display_siteconfig();
1561 } else if (!strcasecmp(action, "chat_recv")) {
1563 } else if (!strcasecmp(action, "chat_send")) {
1565 } else if (!strcasecmp(action, "siteconfig")) {
1567 } else if (!strcasecmp(action, "display_generic")) {
1569 } else if (!strcasecmp(action, "do_generic")) {
1571 } else if (!strcasecmp(action, "ajax_servcmd")) {
1573 } else if (!strcasecmp(action, "display_menubar")) {
1575 } else if (!strcasecmp(action, "mimepart")) {
1576 mimepart(arg1, arg2, 0);
1577 } else if (!strcasecmp(action, "mimepart_download")) {
1578 mimepart(arg1, arg2, 1);
1579 } else if (!strcasecmp(action, "edit_vcard")) {
1581 } else if (!strcasecmp(action, "submit_vcard")) {
1583 } else if (!strcasecmp(action, "select_user_to_edit")) {
1584 select_user_to_edit(NULL, NULL);
1585 } else if (!strcasecmp(action, "display_edituser")) {
1586 display_edituser(NULL, 0);
1587 } else if (!strcasecmp(action, "edituser")) {
1589 } else if (!strcasecmp(action, "create_user")) {
1591 } else if (!strcasecmp(action, "changeview")) {
1593 } else if (!strcasecmp(action, "change_start_page")) {
1594 change_start_page();
1595 } else if (!strcasecmp(action, "display_floorconfig")) {
1596 display_floorconfig(NULL);
1597 } else if (!strcasecmp(action, "toggle_self_service")) {
1598 toggle_self_service();
1599 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1600 } else if (!strcasecmp(action, "display_edit_task")) {
1601 display_edit_task();
1602 } else if (!strcasecmp(action, "save_task")) {
1604 } else if (!strcasecmp(action, "display_edit_event")) {
1605 display_edit_event();
1606 } else if (!strcasecmp(action, "save_event")) {
1608 } else if (!strcasecmp(action, "respond_to_request")) {
1609 respond_to_request();
1610 } else if (!strcasecmp(action, "handle_rsvp")) {
1613 } else if (!strcasecmp(action, "summary")) {
1615 } else if (!strcasecmp(action, "summary_inner_div")) {
1616 begin_ajax_response();
1617 summary_inner_div();
1618 end_ajax_response();
1619 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1620 display_customize_iconbar();
1621 } else if (!strcasecmp(action, "commit_iconbar")) {
1623 } else if (!strcasecmp(action, "set_room_policy")) {
1625 } else if (!strcasecmp(action, "display_inetconf")) {
1627 } else if (!strcasecmp(action, "save_inetconf")) {
1629 } else if (!strcasecmp(action, "setup_wizard")) {
1631 } else if (!strcasecmp(action, "display_preferences")) {
1632 display_preferences();
1633 } else if (!strcasecmp(action, "set_preferences")) {
1635 } else if (!strcasecmp(action, "recp_autocomplete")) {
1636 recp_autocomplete(bstr("recp"));
1637 } else if (!strcasecmp(action, "cc_autocomplete")) {
1638 recp_autocomplete(bstr("cc"));
1639 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1640 recp_autocomplete(bstr("bcc"));
1641 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1642 set_floordiv_expanded(arg1);
1643 } else if (!strcasecmp(action, "diagnostics")) {
1644 output_headers(1, 1, 1, 0, 0, 0);
1645 wprintf("Session: %d<hr />\n", WC->wc_session);
1646 wprintf("Command: <br /><PRE>\n");
1648 wprintf("</PRE><hr />\n");
1649 wprintf("Variables: <br /><PRE>\n");
1651 wprintf("</PRE><hr />\n");
1653 } else if (!strcasecmp(action, "updatenote")) {
1657 /** When all else fais, display the main menu. */
1659 display_main_menu();
1664 if (content != NULL) {
1669 if (WC->upload_length > 0) {
1671 WC->upload_length = 0;