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)
35 while ((isspace(buf[buflen - 1])) && (buflen > 0)){
40 for (a = 0; a < buflen; ++a) {
48 sscanf(hex, "%02x", &b);
50 memmove(&buf[a + 1], &buf[a + 3], buflen - a - 2);
59 * \brief Extract variables from the URL.
60 * \param url URL supplied by the HTTP parser
62 void addurls(char *url)
70 while (strlen(up) > 0) {
72 /** locate the = sign */
73 safestrncpy(buf, up, sizeof buf);
75 for (a = 255; a >= 0; --a)
82 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
83 u->next = WC->urlstrings;
85 safestrncpy(u->url_key, buf, sizeof u->url_key);
87 /** now chop that part off */
88 for (a = 0; a <= b; ++a)
91 /** locate "&" and "?" delimiters */
94 for (a = 0; a < strlen(up); ++a) {
95 if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
102 for (a = 0; a < b; ++a)
106 u->url_data = malloc(strlen(up) + 2);
107 safestrncpy(u->url_data, up, strlen(up) + 1);
109 unescape_input(u->url_data);
113 /* lprintf(9, "%s = %s\n", u->url_key, u->url_data); */
118 * \brief free urlstring memory
122 struct urlcontent *u;
124 while (WC->urlstrings != NULL) {
125 free(WC->urlstrings->url_data);
126 u = WC->urlstrings->next;
127 free(WC->urlstrings);
133 * \brief Diagnostic function to display the contents of all variables
137 struct urlcontent *u;
139 for (u = WC->urlstrings; u != NULL; u = u->next) {
140 wprintf("%38s = %s\n", u->url_key, u->url_data);
145 * \brief Return the value of a variable supplied to the current web page (from the url or a form)
146 * \param key The name of the variable we want
148 char *bstr(char *key)
150 struct urlcontent *u;
152 for (u = WC->urlstrings; u != NULL; u = u->next) {
153 if (!strcasecmp(u->url_key, key))
154 return (u->url_data);
160 * \brief web-printing funcion. uses our vsnprintf wrapper
161 * \param format printf format string
162 * \param ... the varargs to put into formatstring
164 void wprintf(const char *format,...)
169 va_start(arg_ptr, format);
170 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
173 client_write(wbuf, strlen(wbuf));
178 * \brief wrap up an HTTP session, closes tags, etc.
179 * \todo multiline params?
180 * \param print_standard_html_footer should be set to 0 to transmit only, 1 to
181 * append the main menu and closing tags, or 2 to
182 * append the closing tags only.
184 void wDumpContent(int print_standard_html_footer)
186 if (print_standard_html_footer) {
187 wprintf("</div>\n"); /* end of "text" div */
188 do_template("trailing");
191 /* If we've been saving it all up for one big output burst,
192 * go ahead and do that now.
199 * \brief Copy a string, escaping characters which have meaning in HTML.
200 * \param target target buffer
201 * \param strbuf source buffer
202 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
203 * \param nolinebreaks if set, linebreaks are removed from the string.
205 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
210 for (a = 0; a < strlen(strbuf); ++a) {
211 if (strbuf[a] == '<')
212 strcat(target, "<");
213 else if (strbuf[a] == '>')
214 strcat(target, ">");
215 else if (strbuf[a] == '&')
216 strcat(target, "&");
217 else if (strbuf[a] == '\"')
218 strcat(target, """);
219 else if (strbuf[a] == '\'')
220 strcat(target, "'");
221 else if (strbuf[a] == LB)
223 else if (strbuf[a] == RB)
225 else if (strbuf[a] == QU)
226 strcat(target, "\"");
227 else if ((strbuf[a] == 32) && (nbsp == 1))
228 strcat(target, " ");
229 else if ((strbuf[a] == '\n') && (nolinebreaks))
230 strcat(target, ""); /* nothing */
231 else if ((strbuf[a] == '\r') && (nolinebreaks))
232 strcat(target, ""); /* nothing */
234 strncat(target, &strbuf[a], 1);
240 * \param strbuf what???
241 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
242 * \param nolinebreaks if set, linebreaks are removed from the string.
244 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
248 if (strbuf == NULL) return;
249 buf = malloc( (3 * strlen(strbuf)) + SIZ );
250 stresc(buf, strbuf, nbsp, nolinebreaks);
256 * \brief static wrapper for ecsputs1
257 * \param strbuf buffer to print escaped to client
259 void escputs(char *strbuf)
261 escputs1(strbuf, 0, 0);
265 * \brief Escape a string for feeding out as a URL.
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);
438 if (!access("static.local/webcit.css", R_OK)) {
439 svprintf("CSSLOCAL", WCS_STRING,
440 "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
450 /** check for ImportantMessages (these display in a div overlaying the main screen) */
451 if (strlen(WC->ImportantMessage) > 0) {
452 wprintf("<div id=\"important_message\">\n");
453 wprintf("<span class=\"imsg\">"
454 "%s</span><br />\n", WC->ImportantMessage);
456 wprintf("<script type=\"text/javascript\">\n"
457 " setTimeout('hide_imsg_popup()', 3000); \n"
459 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
462 if ( (WC->logged_in) && (!unset_cookies) ) {
463 wprintf("<div id=\"iconbar\">");
464 do_selected_iconbar();
465 /** check for instant messages (these display in a new window) */
470 if (do_room_banner == 1) {
471 wprintf("<div id=\"banner\">\n");
472 embed_room_banner(NULL, navbar_default);
477 if (do_room_banner == 1) {
478 wprintf("<div id=\"content\">\n");
484 * \brief Generic function to do an HTTP redirect. Easy and fun.
485 * \param whichpage target url to 302 to
487 void http_redirect(char *whichpage) {
488 wprintf("HTTP/1.1 302 Moved Temporarily\n");
489 wprintf("Location: %s\r\n", whichpage);
490 wprintf("URI: %s\r\n", whichpage);
491 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
492 wprintf("<html><body>");
493 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
494 wprintf("</body></html>\n");
500 * \brief Output a piece of content to the web browser
502 void http_transmit_thing(char *thing, size_t length, char *content_type,
505 output_headers(0, 0, 0, 0, 0, is_static);
507 wprintf("Content-type: %s\r\n"
509 "Connection: close\r\n",
514 /** If we can send the data out compressed, please do so. */
516 char *compressed_data = NULL;
517 uLongf compressed_len;
519 compressed_len = (uLongf) ((length * 101) / 100) + 100;
520 compressed_data = malloc(compressed_len);
522 if (compress_gzip((Bytef *) compressed_data,
525 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
526 wprintf("Content-encoding: gzip\r\n"
527 "Content-length: %ld\r\n"
529 (long) compressed_len
531 client_write(compressed_data, (size_t)compressed_len);
532 free(compressed_data);
538 /** No compression ... just send it out as-is */
539 wprintf("Content-length: %ld\r\n"
543 client_write(thing, (size_t)length);
549 * \brief dump out static pages from disk
550 * \param what the file urs to print
552 void output_static(char *what)
558 char content_type[128];
560 fp = fopen(what, "rb");
562 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
563 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
564 wprintf("Content-Type: text/plain\r\n");
566 wprintf("Cannot open %s: %s\n", what, strerror(errno));
568 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
569 safestrncpy(content_type, "image/gif", sizeof content_type);
570 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
571 safestrncpy(content_type, "text/plain", sizeof content_type);
572 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
573 safestrncpy(content_type, "text/css", sizeof content_type);
574 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
575 safestrncpy(content_type, "image/jpeg", sizeof content_type);
576 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
577 safestrncpy(content_type, "image/png", sizeof content_type);
578 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
579 safestrncpy(content_type, "image/x-icon", sizeof content_type);
580 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
581 safestrncpy(content_type, "text/html", sizeof content_type);
582 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
583 safestrncpy(content_type, "text/html", sizeof content_type);
584 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
585 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
586 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
587 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
588 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
589 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
590 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
591 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
592 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
593 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
594 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
595 safestrncpy(content_type, "text/javascript", sizeof content_type);
597 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
599 fstat(fileno(fp), &statbuf);
600 bytes = statbuf.st_size;
601 bigbuffer = malloc(bytes + 2);
602 fread(bigbuffer, bytes, 1, fp);
605 lprintf(9, "output_static('%s') %s\n", what, content_type);
606 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
609 if (!strcasecmp(bstr("force_close_session"), "yes")) {
610 end_webcit_session();
616 * \brief When the browser requests an image file from the Citadel server,
617 * this function is called to transmit it.
622 char *xferbuf = NULL;
625 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
626 serv_getln(buf, sizeof buf);
628 bytes = extract_long(&buf[4], 0);
629 xferbuf = malloc(bytes + 2);
631 /** Read it from the server */
632 read_server_binary(xferbuf, bytes);
634 serv_getln(buf, sizeof buf);
636 /** Write it to the browser */
637 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
642 * Instead of an ugly 404, send a 1x1 transparent GIF
643 * when there's no such image on the server.
645 output_static("static/blank.gif");
653 * \brief Generic function to output an arbitrary MIME part from an arbitrary
654 * message number on the server.
656 * \param msgnum Number of the item on the citadel server
657 * \param partnum The MIME part to be output
658 * \param force_download Nonzero to force set the Content-Type: header
659 * to "application/octet-stream"
661 void mimepart(char *msgnum, char *partnum, int force_download)
665 char content_type[256];
666 char *content = NULL;
668 serv_printf("OPNA %s|%s", msgnum, partnum);
669 serv_getln(buf, sizeof buf);
671 bytes = extract_long(&buf[4], 0);
672 content = malloc(bytes + 2);
673 if (force_download) {
674 strcpy(content_type, "application/octet-stream");
677 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
679 output_headers(0, 0, 0, 0, 0, 0);
680 read_server_binary(content, bytes);
682 serv_getln(buf, sizeof buf);
683 http_transmit_thing(content, bytes, content_type, 0);
686 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
687 output_headers(0, 0, 0, 0, 0, 0);
688 wprintf("Content-Type: text/plain\r\n");
690 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
697 * \brief Read any MIME part of a message, from the server, into memory.
698 * \param msgnum number of the message on the citadel server
699 * \param partnum the MIME part to be loaded
701 char *load_mimepart(long msgnum, char *partnum)
705 char content_type[SIZ];
708 serv_printf("DLAT %ld|%s", msgnum, partnum);
709 serv_getln(buf, sizeof buf);
711 bytes = extract_long(&buf[4], 0);
712 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
714 content = malloc(bytes + 2);
715 serv_read(content, bytes);
717 content[bytes] = 0; /* null terminate for good measure */
728 * \brief Convenience functions to display a page containing only a string
729 * \param titlebarcolor color of the titlebar of the frame
730 * \param titlebarmsg text to display in the title bar
731 * \param messagetext body of the box
733 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
735 wprintf("HTTP/1.1 200 OK\n");
736 output_headers(1, 1, 2, 0, 0, 0);
737 wprintf("<div id=\"banner\">\n");
738 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
739 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
740 wprintf("</td></tr></table>\n");
741 wprintf("</div>\n<div id=\"content\">\n");
742 escputs(messagetext);
750 * \brief Display a blank page.
752 void blank_page(void) {
753 output_headers(1, 1, 0, 0, 0, 0);
759 * \brief A template has been requested
761 void url_do_template(void) {
762 do_template(bstr("template"));
768 * \brief Offer to make any page the user's "start page."
770 void offer_start_page(void) {
771 wprintf("<a href=\"change_start_page?startpage=");
772 urlescputs(WC->this_page);
774 wprintf(_("Make this my start page"));
777 wprintf("<br/><a href=\"rss?room=");
778 urlescputs(WC->wc_roomname);
779 wprintf("\" title=\"RSS 2.0 feed for ");
780 escputs(WC->wc_roomname);
781 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
787 * \brief Change the user's start page
789 void change_start_page(void) {
791 if (bstr("startpage") == NULL) {
792 safestrncpy(WC->ImportantMessage,
793 _("You no longer have a start page selected."),
794 sizeof WC->ImportantMessage);
799 set_preference("startpage", bstr("startpage"), 1);
801 output_headers(1, 1, 0, 0, 0, 0);
802 do_template("newstartpage");
809 * \brief convenience function to indicate success
810 * \param successmessage the mesage itself
812 void display_success(char *successmessage)
814 convenience_page("007700", "OK", successmessage);
819 * \brief Authorization required page
820 * This is probably temporary and should be revisited
821 * \param message message to put in header
823 void authorization_required(const char *message)
825 wprintf("HTTP/1.1 401 Authorization Required\r\n");
826 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
827 wprintf("Content-Type: text/html\r\n\r\n");
829 wprintf(_("Authorization Required"));
830 wprintf("</h1>\r\n");
831 wprintf(_("The resource you requested requires a valid username and password. "
832 "You could not be logged in: %s\n"), message);
837 * \brief This function is called by the MIME parser to handle data uploaded by
838 * the browser. Form data, uploaded files, and the data from HTTP PUT
839 * operations (such as those found in GroupDAV) all arrive this way.
841 * \param name Name of the item being uploaded
842 * \param filename Filename of the item being uploaded
843 * \param partnum MIME part identifier (not needed)
844 * \param disp MIME content disposition (not needed)
845 * \param content The actual data
846 * \param cbtype MIME content-type
847 * \param cbcharset Character set
848 * \param length Content length
849 * \param encoding MIME encoding type (not needed)
850 * \param userdata Not used here
852 void upload_handler(char *name, char *filename, char *partnum, char *disp,
853 void *content, char *cbtype, char *cbcharset,
854 size_t length, char *encoding, void *userdata)
856 struct urlcontent *u;
858 lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n", name, cbtype, length);
861 if ( (length > 0) && (strlen(cbtype) == 0) ) {
862 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
863 u->next = WC->urlstrings;
865 safestrncpy(u->url_key, name, sizeof(u->url_key));
866 u->url_data = malloc(length + 1);
867 memcpy(u->url_data, content, length);
868 u->url_data[length] = 0;
869 /* lprintf(9, "Key: <%s> Data: <%s>\n", u->url_key, u->url_data); */
872 /** Uploaded files */
873 if ( (length > 0) && (strlen(cbtype) > 0) ) {
874 WC->upload = malloc(length);
875 if (WC->upload != NULL) {
876 WC->upload_length = length;
877 safestrncpy(WC->upload_filename, filename,
878 sizeof(WC->upload_filename));
879 safestrncpy(WC->upload_content_type, cbtype,
880 sizeof(WC->upload_content_type));
881 memcpy(WC->upload, content, length);
884 lprintf(3, "malloc() failed: %s\n", strerror(errno));
891 * \brief Convenience functions to wrap around asynchronous ajax responses
893 void begin_ajax_response(void) {
894 output_headers(0, 0, 0, 0, 0, 0);
896 wprintf("Content-type: text/html; charset=UTF-8\r\n"
898 "Connection: close\r\n"
899 "Pragma: no-cache\r\n"
900 "Cache-Control: no-cache\r\n",
906 * \brief print ajax response footer
908 void end_ajax_response(void) {
914 * \brief Wraps a Citadel server command in an AJAX transaction.
916 void ajax_servcmd(void)
923 begin_ajax_response();
925 serv_printf("%s", bstr("g_cmd"));
926 serv_getln(buf, sizeof buf);
927 wprintf("%s\n", buf);
930 serv_printf("\n\n000");
932 if ((buf[0] == '1') || (buf[0] == '8')) {
933 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
934 wprintf("%s\n", gcontent);
939 text_to_server(bstr("g_input"));
945 serv_read(junk, len);
951 memset(junk, 0, len);
952 serv_write(junk, len);
959 * This is kind of an ugly hack, but this is the only place it can go.
960 * If the command was GEXP, then the instant messenger window must be
961 * running, so reset the "last_pager_check" watchdog timer so
962 * that page_popup() doesn't try to open it a second time.
964 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
965 WC->last_pager_check = time(NULL);
971 * \brief Helper function for the asynchronous check to see if we need
972 * to open the instant messenger window.
974 void seconds_since_last_gexp(void)
978 begin_ajax_response();
979 if ( (time(NULL) - WC->last_pager_check) < 30) {
984 serv_getln(buf, sizeof buf);
999 * \brief Entry point for WebCit transaction
1001 void session_loop(struct httprequest *req)
1009 char request_method[128];
1010 char pathname[1024];
1011 int a, b, nBackDots, nEmpty;
1012 int ContentLength = 0;
1014 char ContentType[512];
1015 char *content = NULL;
1016 char *content_end = NULL;
1017 struct httprequest *hptr;
1018 char browser_host[256];
1019 char user_agent[256];
1024 * We stuff these with the values coming from the client cookies,
1025 * so we can use them to reconnect a timed out session if we have to.
1027 char c_username[SIZ];
1028 char c_password[SIZ];
1029 char c_roomname[SIZ];
1030 char c_httpauth_string[SIZ];
1031 char c_httpauth_user[SIZ];
1032 char c_httpauth_pass[SIZ];
1035 safestrncpy(c_username, "", sizeof c_username);
1036 safestrncpy(c_password, "", sizeof c_password);
1037 safestrncpy(c_roomname, "", sizeof c_roomname);
1038 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1039 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1040 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1041 strcpy(browser_host, "");
1043 WC->upload_length = 0;
1049 if (hptr == NULL) return;
1051 safestrncpy(cmd, hptr->line, sizeof cmd);
1053 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1054 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1056 /** Figure out the action */
1058 sizes[0] = sizeof action;
1061 index[a] = arg[a-1];
1062 sizes[a] = sizeof arg[a-1];
1064 //// index[9] = &foo; todo
1067 for ( a = 0; a < 9; ++a)
1069 extract_token(index[a], pathname, a + 1, '/', sizes[a]);
1070 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
1071 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
1072 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
1073 if ((index[a][0] == '.') && (index[a][1] == '.'))
1075 if (index[a][0] == '\0')
1079 while (hptr != NULL) {
1080 safestrncpy(buf, hptr->line, sizeof buf);
1081 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1084 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1085 safestrncpy(cookie, &buf[15], sizeof cookie);
1086 cookie_to_stuff(cookie, NULL,
1087 c_username, sizeof c_username,
1088 c_password, sizeof c_password,
1089 c_roomname, sizeof c_roomname);
1091 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1092 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1093 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1094 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1096 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1097 ContentLength = atoi(&buf[16]);
1099 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1100 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1102 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1103 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1105 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1107 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1110 else if (!strncasecmp(buf, "Host: ", 6)) {
1111 if (strlen(WC->http_host) == 0) {
1112 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], ContentLength);
1139 if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) {
1140 addurls(&content[body_start]);
1141 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1142 content_end = content + ContentLength + body_start;
1143 mime_parser(content, content_end, *upload_handler, NULL, NULL, NULL, 0);
1149 /** make a note of where we are in case the user wants to save it */
1150 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1151 remove_token(WC->this_page, 2, ' ');
1152 remove_token(WC->this_page, 0, ' ');
1154 /** If there are variables in the URL, we must grab them now */
1155 for (a = 0; a < strlen(cmd); ++a) {
1156 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1157 for (b = a; b < strlen(cmd); ++b)
1158 if (isspace(cmd[b]))
1160 addurls(&cmd[a + 1]);
1165 /** If it's a "force 404" situation then display the error and bail. */
1166 if (!strcmp(action, "404")) {
1167 wprintf("HTTP/1.1 404 Not found\r\n");
1168 wprintf("Content-Type: text/plain\r\n");
1170 wprintf("Not found\r\n");
1171 goto SKIP_ALL_THIS_CRAP;
1174 /** Static content can be sent without connecting to Citadel. */
1176 for (a=0; a<ndirs; ++a) {
1177 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1185 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1186 static_dirs[n_static],
1187 index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
1188 for (a=0; a<8; ++a) {
1189 if (buf[strlen(buf)-1] == '/') {
1190 buf[strlen(buf)-1] = 0;
1193 for (a = 0; a < strlen(buf); ++a) {
1194 if (isspace(buf[a])) {
1202 lprintf(9, "Suspicious request. Ignoring.");
1203 wprintf("HTTP/1.1 404 Not found. Don't try to Trick me DUDE!\r\n");
1204 wprintf("Content-Type: text/plain\r\n");
1206 wprintf("Not found. Don't play games on me!\r\n");
1208 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1212 * If we're not connected to a Citadel server, try to hook up the
1215 if (!WC->connected) {
1216 if (!strcasecmp(ctdlhost, "uds")) {
1217 /* unix domain socket */
1218 sprintf(buf, "%s/citadel.socket", ctdlport);
1219 WC->serv_sock = uds_connectsock(buf);
1223 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1226 if (WC->serv_sock < 0) {
1228 goto SKIP_ALL_THIS_CRAP;
1232 serv_getln(buf, sizeof buf); /** get the server welcome message */
1235 * From what host is our user connecting? Go with
1236 * the host at the other end of the HTTP socket,
1237 * unless we are following X-Forwarded-For: headers
1238 * and such a header has already turned up something.
1240 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1241 locate_host(browser_host, WC->http_sock);
1244 get_serv_info(browser_host, user_agent);
1245 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1246 wprintf(_("You are connected to a Citadel "
1247 "server running Citadel %d.%02d. \n"
1248 "In order to run this version of WebCit "
1249 "you must also have Citadel %d.%02d or"
1251 serv_info.serv_rev_level / 100,
1252 serv_info.serv_rev_level % 100,
1253 MINIMUM_CIT_VERSION / 100,
1254 MINIMUM_CIT_VERSION % 100
1256 end_webcit_session();
1257 goto SKIP_ALL_THIS_CRAP;
1263 * Functions which can be performed without logging in
1265 if (!strcasecmp(action, "listsub")) {
1267 goto SKIP_ALL_THIS_CRAP;
1269 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1270 if (!strcasecmp(action, "freebusy")) {
1272 goto SKIP_ALL_THIS_CRAP;
1277 * If we're not logged in, but we have HTTP Authentication data,
1278 * try logging in to Citadel using that.
1280 if ((!WC->logged_in)
1281 && (strlen(c_httpauth_user) > 0)
1282 && (strlen(c_httpauth_pass) > 0)) {
1283 serv_printf("USER %s", c_httpauth_user);
1284 serv_getln(buf, sizeof buf);
1285 if (buf[0] == '3') {
1286 serv_printf("PASS %s", c_httpauth_pass);
1287 serv_getln(buf, sizeof buf);
1288 if (buf[0] == '2') {
1289 become_logged_in(c_httpauth_user,
1290 c_httpauth_pass, buf);
1291 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1292 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1294 /** Should only display when password is wrong */
1295 authorization_required(&buf[4]);
1296 goto SKIP_ALL_THIS_CRAP;
1301 /** This needs to run early */
1302 if (!strcasecmp(action, "rss")) {
1303 display_rss(bstr("room"), request_method);
1304 goto SKIP_ALL_THIS_CRAP;
1308 * The GroupDAV stuff relies on HTTP authentication instead of
1309 * our session's authentication.
1311 if (!strncasecmp(action, "groupdav", 8)) {
1312 groupdav_main(req, ContentType, /* do GroupDAV methods */
1313 ContentLength, content+body_start);
1314 if (!WC->logged_in) {
1315 WC->killthis = 1; /* If not logged in, don't */
1316 } /* keep the session active */
1317 goto SKIP_ALL_THIS_CRAP;
1322 * Automatically send requests with any method other than GET or
1323 * POST to the GroupDAV code as well.
1325 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1326 groupdav_main(req, ContentType, /** do GroupDAV methods */
1327 ContentLength, content+body_start);
1328 if (!WC->logged_in) {
1329 WC->killthis = 1; /** If not logged in, don't */
1330 } /** keep the session active */
1331 goto SKIP_ALL_THIS_CRAP;
1335 * If we're not logged in, but we have username and password cookies
1336 * supplied by the browser, try using them to log in.
1338 if ((!WC->logged_in)
1339 && (strlen(c_username) > 0)
1340 && (strlen(c_password) > 0)) {
1341 serv_printf("USER %s", c_username);
1342 serv_getln(buf, sizeof buf);
1343 if (buf[0] == '3') {
1344 serv_printf("PASS %s", c_password);
1345 serv_getln(buf, sizeof buf);
1346 if (buf[0] == '2') {
1347 become_logged_in(c_username, c_password, buf);
1352 * If we don't have a current room, but a cookie specifying the
1353 * current room is supplied, make an effort to go there.
1355 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1356 serv_printf("GOTO %s", c_roomname);
1357 serv_getln(buf, sizeof buf);
1358 if (buf[0] == '2') {
1359 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1363 if (!strcasecmp(action, "image")) {
1367 * All functions handled below this point ... make sure we log in
1368 * before doing anything else!
1370 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1372 } else if (!WC->logged_in) {
1373 display_login(NULL);
1377 * Various commands...
1380 else if (!strcasecmp(action, "do_welcome")) {
1382 } else if (!strcasecmp(action, "blank")) {
1384 } else if (!strcasecmp(action, "do_template")) {
1386 } else if (!strcasecmp(action, "display_aide_menu")) {
1387 display_aide_menu();
1388 } else if (!strcasecmp(action, "display_main_menu")) {
1389 display_main_menu();
1390 } else if (!strcasecmp(action, "who")) {
1392 } else if (!strcasecmp(action, "sslg")) {
1393 seconds_since_last_gexp();
1394 } else if (!strcasecmp(action, "who_inner_html")) {
1395 begin_ajax_response();
1397 end_ajax_response();
1398 } else if (!strcasecmp(action, "wholist_section")) {
1399 begin_ajax_response();
1401 end_ajax_response();
1402 } else if (!strcasecmp(action, "new_messages_html")) {
1403 begin_ajax_response();
1404 new_messages_section();
1405 end_ajax_response();
1406 } else if (!strcasecmp(action, "tasks_inner_html")) {
1407 begin_ajax_response();
1409 end_ajax_response();
1410 } else if (!strcasecmp(action, "calendar_inner_html")) {
1411 begin_ajax_response();
1413 end_ajax_response();
1414 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1415 begin_ajax_response();
1417 end_ajax_response();
1418 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1419 begin_ajax_response();
1420 do_iconbar_roomlist();
1421 end_ajax_response();
1422 } else if (!strcasecmp(action, "knrooms")) {
1424 } else if (!strcasecmp(action, "gotonext")) {
1427 } else if (!strcasecmp(action, "skip")) {
1429 } else if (!strcasecmp(action, "ungoto")) {
1431 } else if (!strcasecmp(action, "dotgoto")) {
1432 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1435 smart_goto(bstr("room"));
1436 } else if (!strcasecmp(action, "dotskip")) {
1437 smart_goto(bstr("room"));
1438 } else if (!strcasecmp(action, "termquit")) {
1440 } else if (!strcasecmp(action, "readnew")) {
1441 readloop("readnew");
1442 } else if (!strcasecmp(action, "readold")) {
1443 readloop("readold");
1444 } else if (!strcasecmp(action, "readfwd")) {
1445 readloop("readfwd");
1446 } else if (!strcasecmp(action, "headers")) {
1447 readloop("headers");
1448 } else if (!strcasecmp(action, "do_search")) {
1449 readloop("do_search");
1450 } else if (!strcasecmp(action, "msg")) {
1451 embed_message(index[1]);
1452 } else if (!strcasecmp(action, "printmsg")) {
1453 print_message(index[1]);
1454 } else if (!strcasecmp(action, "msgheaders")) {
1455 display_headers(index[1]);
1456 } else if (!strcasecmp(action, "wiki")) {
1457 display_wiki_page();
1458 } else if (!strcasecmp(action, "display_enter")) {
1460 } else if (!strcasecmp(action, "post")) {
1462 } else if (!strcasecmp(action, "move_msg")) {
1464 } else if (!strcasecmp(action, "delete_msg")) {
1466 } else if (!strcasecmp(action, "userlist")) {
1468 } else if (!strcasecmp(action, "showuser")) {
1470 } else if (!strcasecmp(action, "display_page")) {
1472 } else if (!strcasecmp(action, "page_user")) {
1474 } else if (!strcasecmp(action, "chat")) {
1476 } else if (!strcasecmp(action, "display_private")) {
1477 display_private("", 0);
1478 } else if (!strcasecmp(action, "goto_private")) {
1480 } else if (!strcasecmp(action, "zapped_list")) {
1482 } else if (!strcasecmp(action, "display_zap")) {
1484 } else if (!strcasecmp(action, "zap")) {
1486 } else if (!strcasecmp(action, "display_entroom")) {
1488 } else if (!strcasecmp(action, "entroom")) {
1490 } else if (!strcasecmp(action, "display_whok")) {
1492 } else if (!strcasecmp(action, "do_invt_kick")) {
1494 } else if (!strcasecmp(action, "display_editroom")) {
1496 } else if (!strcasecmp(action, "netedit")) {
1498 } else if (!strcasecmp(action, "editroom")) {
1500 } else if (!strcasecmp(action, "display_editinfo")) {
1501 display_edit(_("Room info"), "EINF 0", "RINF", "editinfo", 1);
1502 } else if (!strcasecmp(action, "editinfo")) {
1503 save_edit(_("Room info"), "EINF 1", 1);
1504 } else if (!strcasecmp(action, "display_editbio")) {
1505 sprintf(buf, "RBIO %s", WC->wc_fullname);
1506 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1507 } else if (!strcasecmp(action, "editbio")) {
1508 save_edit(_("Your bio"), "EBIO", 0);
1509 } else if (!strcasecmp(action, "confirm_move_msg")) {
1511 } else if (!strcasecmp(action, "delete_room")) {
1513 } else if (!strcasecmp(action, "validate")) {
1515 } else if (!strcasecmp(action, "display_editpic")) {
1516 display_graphics_upload(_("your photo"),
1519 } else if (!strcasecmp(action, "editpic")) {
1520 do_graphics_upload("UIMG 1|_userpic_");
1521 } else if (!strcasecmp(action, "display_editroompic")) {
1522 display_graphics_upload(_("the icon for this room"),
1525 } else if (!strcasecmp(action, "editroompic")) {
1526 do_graphics_upload("UIMG 1|_roompic_");
1527 } else if (!strcasecmp(action, "delete_floor")) {
1529 } else if (!strcasecmp(action, "rename_floor")) {
1531 } else if (!strcasecmp(action, "create_floor")) {
1533 } else if (!strcasecmp(action, "display_editfloorpic")) {
1534 sprintf(buf, "UIMG 0|_floorpic_|%s",
1535 bstr("which_floor"));
1536 display_graphics_upload(_("the icon for this floor"),
1539 } else if (!strcasecmp(action, "editfloorpic")) {
1540 sprintf(buf, "UIMG 1|_floorpic_|%s",
1541 bstr("which_floor"));
1542 do_graphics_upload(buf);
1543 } else if (!strcasecmp(action, "display_reg")) {
1545 } else if (!strcasecmp(action, "display_changepw")) {
1547 } else if (!strcasecmp(action, "changepw")) {
1549 } else if (!strcasecmp(action, "display_edit_node")) {
1550 display_edit_node();
1551 } else if (!strcasecmp(action, "edit_node")) {
1553 } else if (!strcasecmp(action, "display_netconf")) {
1555 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1556 display_confirm_delete_node();
1557 } else if (!strcasecmp(action, "delete_node")) {
1559 } else if (!strcasecmp(action, "display_add_node")) {
1561 } else if (!strcasecmp(action, "add_node")) {
1563 } else if (!strcasecmp(action, "terminate_session")) {
1565 terminate_session();
1566 } else if (!strcasecmp(action, "edit_me")) {
1568 } else if (!strcasecmp(action, "display_siteconfig")) {
1569 display_siteconfig();
1570 } else if (!strcasecmp(action, "chat_recv")) {
1572 } else if (!strcasecmp(action, "chat_send")) {
1574 } else if (!strcasecmp(action, "siteconfig")) {
1576 } else if (!strcasecmp(action, "display_generic")) {
1578 } else if (!strcasecmp(action, "do_generic")) {
1580 } else if (!strcasecmp(action, "ajax_servcmd")) {
1582 } else if (!strcasecmp(action, "display_menubar")) {
1584 } else if (!strcasecmp(action, "mimepart")) {
1585 mimepart(index[1], index[2], 0);
1586 } else if (!strcasecmp(action, "mimepart_download")) {
1587 mimepart(index[1], index[2], 1);
1588 } else if (!strcasecmp(action, "edit_vcard")) {
1590 } else if (!strcasecmp(action, "submit_vcard")) {
1592 } else if (!strcasecmp(action, "select_user_to_edit")) {
1593 select_user_to_edit(NULL, NULL);
1594 } else if (!strcasecmp(action, "display_edituser")) {
1595 display_edituser(NULL, 0);
1596 } else if (!strcasecmp(action, "edituser")) {
1598 } else if (!strcasecmp(action, "create_user")) {
1600 } else if (!strcasecmp(action, "changeview")) {
1602 } else if (!strcasecmp(action, "change_start_page")) {
1603 change_start_page();
1604 } else if (!strcasecmp(action, "display_floorconfig")) {
1605 display_floorconfig(NULL);
1606 } else if (!strcasecmp(action, "toggle_self_service")) {
1607 toggle_self_service();
1608 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1609 } else if (!strcasecmp(action, "display_edit_task")) {
1610 display_edit_task();
1611 } else if (!strcasecmp(action, "save_task")) {
1613 } else if (!strcasecmp(action, "display_edit_event")) {
1614 display_edit_event();
1615 } else if (!strcasecmp(action, "save_event")) {
1617 } else if (!strcasecmp(action, "respond_to_request")) {
1618 respond_to_request();
1619 } else if (!strcasecmp(action, "handle_rsvp")) {
1622 } else if (!strcasecmp(action, "summary")) {
1624 } else if (!strcasecmp(action, "summary_inner_div")) {
1625 begin_ajax_response();
1626 summary_inner_div();
1627 end_ajax_response();
1628 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1629 display_customize_iconbar();
1630 } else if (!strcasecmp(action, "commit_iconbar")) {
1632 } else if (!strcasecmp(action, "set_room_policy")) {
1634 } else if (!strcasecmp(action, "display_inetconf")) {
1636 } else if (!strcasecmp(action, "save_inetconf")) {
1638 } else if (!strcasecmp(action, "display_smtpqueue")) {
1639 display_smtpqueue();
1640 } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1641 display_smtpqueue_inner_div();
1642 } else if (!strcasecmp(action, "display_sieve")) {
1644 } else if (!strcasecmp(action, "save_sieve")) {
1646 } else if (!strcasecmp(action, "display_add_remove_scripts")) {
1647 display_add_remove_scripts(NULL);
1648 } else if (!strcasecmp(action, "create_script")) {
1650 } else if (!strcasecmp(action, "delete_script")) {
1652 } else if (!strcasecmp(action, "setup_wizard")) {
1654 } else if (!strcasecmp(action, "display_preferences")) {
1655 display_preferences();
1656 } else if (!strcasecmp(action, "set_preferences")) {
1658 } else if (!strcasecmp(action, "recp_autocomplete")) {
1659 recp_autocomplete(bstr("recp"));
1660 } else if (!strcasecmp(action, "cc_autocomplete")) {
1661 recp_autocomplete(bstr("cc"));
1662 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1663 recp_autocomplete(bstr("bcc"));
1664 } else if (!strcasecmp(action, "display_address_book_middle_div")) {
1665 display_address_book_middle_div();
1666 } else if (!strcasecmp(action, "display_address_book_inner_div")) {
1667 display_address_book_inner_div();
1668 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1669 set_floordiv_expanded(index[1]);
1670 } else if (!strcasecmp(action, "diagnostics")) {
1671 output_headers(1, 1, 1, 0, 0, 0);
1672 wprintf("Session: %d<hr />\n", WC->wc_session);
1673 wprintf("Command: <br /><PRE>\n");
1675 wprintf("</PRE><hr />\n");
1676 wprintf("Variables: <br /><PRE>\n");
1678 wprintf("</PRE><hr />\n");
1680 } else if (!strcasecmp(action, "updatenote")) {
1682 } else if (!strcasecmp(action, "display_room_directory")) {
1683 display_room_directory();
1684 } else if (!strcasecmp(action, "download_file")) {
1685 download_file(index[1]);
1686 } else if (!strcasecmp(action, "upload_file")) {
1690 /** When all else fais, display the main menu. */
1692 display_main_menu();
1697 if (content != NULL) {
1702 if (WC->upload_length > 0) {
1704 WC->upload_length = 0;