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("OPNA %ld|%s", msgnum, partnum);
696 serv_getln(buf, sizeof buf);
698 bytes = extract_long(&buf[4], 0);
699 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
701 content = malloc(bytes + 2);
702 read_server_binary(content, bytes);
705 serv_getln(buf, sizeof buf);
706 content[bytes] = 0; /* null terminate for good measure */
717 * \brief Convenience functions to display a page containing only a string
718 * \param titlebarcolor color of the titlebar of the frame
719 * \param titlebarmsg text to display in the title bar
720 * \param messagetext body of the box
722 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
724 wprintf("HTTP/1.1 200 OK\n");
725 output_headers(1, 1, 2, 0, 0, 0);
726 wprintf("<div id=\"banner\">\n");
727 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
728 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
729 wprintf("</td></tr></table>\n");
730 wprintf("</div>\n<div id=\"content\">\n");
731 escputs(messagetext);
739 * \brief Display a blank page.
741 void blank_page(void) {
742 output_headers(1, 1, 0, 0, 0, 0);
748 * \brief A template has been requested
750 void url_do_template(void) {
751 do_template(bstr("template"));
757 * \brief Offer to make any page the user's "start page."
759 void offer_start_page(void) {
760 wprintf("<a href=\"change_start_page?startpage=");
761 urlescputs(WC->this_page);
762 wprintf("\"><font size=-2 color=\"#AAAAAA\">");
763 wprintf(_("Make this my start page"));
764 wprintf("</font></a>");
766 wprintf("<br/><a href=\"rss?room=");
767 urlescputs(WC->wc_roomname);
768 wprintf("\" title=\"RSS 2.0 feed for ");
769 escputs(WC->wc_roomname);
770 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
776 * \brief Change the user's start page
778 void change_start_page(void) {
780 if (bstr("startpage") == NULL) {
781 safestrncpy(WC->ImportantMessage,
782 _("You no longer have a start page selected."),
783 sizeof WC->ImportantMessage);
788 set_preference("startpage", bstr("startpage"), 1);
790 output_headers(1, 1, 0, 0, 0, 0);
791 do_template("newstartpage");
798 * \brief convenience function to indicate success
799 * \param successmessage the mesage itself
801 void display_success(char *successmessage)
803 convenience_page("007700", "OK", successmessage);
808 * \brief Authorization required page
809 * This is probably temporary and should be revisited
810 * \param message message to put in header
812 void authorization_required(const char *message)
814 wprintf("HTTP/1.1 401 Authorization Required\r\n");
815 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
816 wprintf("Content-Type: text/html\r\n\r\n");
818 wprintf(_("Authorization Required"));
819 wprintf("</h1>\r\n");
820 wprintf(_("The resource you requested requires a valid username and password. "
821 "You could not be logged in: %s\n"), message);
826 * \brief This function is called by the MIME parser to handle data uploaded by
827 * the browser. Form data, uploaded files, and the data from HTTP PUT
828 * operations (such as those found in GroupDAV) all arrive this way.
830 * \param name Name of the item being uploaded
831 * \param filename Filename of the item being uploaded
832 * \param partnum MIME part identifier (not needed)
833 * \param disp MIME content disposition (not needed)
834 * \param content The actual data
835 * \param cbtype MIME content-type
836 * \param cbcharset Character set
837 * \param length Content length
838 * \param encoding MIME encoding type (not needed)
839 * \param userdata Not used here
841 void upload_handler(char *name, char *filename, char *partnum, char *disp,
842 void *content, char *cbtype, char *cbcharset,
843 size_t length, char *encoding, void *userdata)
845 struct urlcontent *u;
847 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
848 name, cbtype, length); */
851 if ( (length > 0) && (strlen(cbtype) == 0) ) {
852 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
853 u->next = WC->urlstrings;
855 safestrncpy(u->url_key, name, sizeof(u->url_key));
856 u->url_data = malloc(length + 1);
857 memcpy(u->url_data, content, length);
858 u->url_data[length] = 0;
861 /** Uploaded files */
862 if ( (length > 0) && (strlen(cbtype) > 0) ) {
863 WC->upload = malloc(length);
864 if (WC->upload != NULL) {
865 WC->upload_length = length;
866 safestrncpy(WC->upload_filename, filename,
867 sizeof(WC->upload_filename));
868 safestrncpy(WC->upload_content_type, cbtype,
869 sizeof(WC->upload_content_type));
870 memcpy(WC->upload, content, length);
873 lprintf(3, "malloc() failed: %s\n", strerror(errno));
880 * \brief Convenience functions to wrap around asynchronous ajax responses
882 void begin_ajax_response(void) {
883 output_headers(0, 0, 0, 0, 0, 0);
885 wprintf("Content-type: text/html; charset=UTF-8\r\n"
887 "Connection: close\r\n"
888 "Pragma: no-cache\r\n"
889 "Cache-Control: no-cache\r\n",
895 * \brief print ajax response footer
897 void end_ajax_response(void) {
903 * \brief Wraps a Citadel server command in an AJAX transaction.
905 void ajax_servcmd(void)
912 begin_ajax_response();
914 serv_printf("%s", bstr("g_cmd"));
915 serv_getln(buf, sizeof buf);
916 wprintf("%s\n", buf);
919 serv_printf("\n\n000");
921 if ((buf[0] == '1') || (buf[0] == '8')) {
922 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
923 wprintf("%s\n", gcontent);
928 text_to_server(bstr("g_input"));
934 serv_read(junk, len);
940 memset(junk, 0, len);
941 serv_write(junk, len);
948 * This is kind of an ugly hack, but this is the only place it can go.
949 * If the command was GEXP, then the instant messenger window must be
950 * running, so reset the "last_pager_check" watchdog timer so
951 * that page_popup() doesn't try to open it a second time.
953 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
954 WC->last_pager_check = time(NULL);
960 * \brief Helper function for the asynchronous check to see if we need
961 * to open the instant messenger window.
963 void seconds_since_last_gexp(void)
967 begin_ajax_response();
968 if ( (time(NULL) - WC->last_pager_check) < 30) {
973 serv_getln(buf, sizeof buf);
988 * \brief Entry point for WebCit transaction
990 void session_loop(struct httprequest *req)
1002 char request_method[128];
1003 char pathname[1024];
1005 int ContentLength = 0;
1007 char ContentType[512];
1008 char *content = NULL;
1009 char *content_end = NULL;
1010 struct httprequest *hptr;
1011 char browser_host[256];
1012 char user_agent[256];
1017 * We stuff these with the values coming from the client cookies,
1018 * so we can use them to reconnect a timed out session if we have to.
1020 char c_username[SIZ];
1021 char c_password[SIZ];
1022 char c_roomname[SIZ];
1023 char c_httpauth_string[SIZ];
1024 char c_httpauth_user[SIZ];
1025 char c_httpauth_pass[SIZ];
1028 safestrncpy(c_username, "", sizeof c_username);
1029 safestrncpy(c_password, "", sizeof c_password);
1030 safestrncpy(c_roomname, "", sizeof c_roomname);
1031 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1032 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1033 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1034 strcpy(browser_host, "");
1036 WC->upload_length = 0;
1042 if (hptr == NULL) return;
1044 safestrncpy(cmd, hptr->line, sizeof cmd);
1046 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1047 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1049 /** Figure out the action */
1050 extract_token(action, pathname, 1, '/', sizeof action);
1051 if (strstr(action, "?")) *strstr(action, "?") = 0;
1052 if (strstr(action, "&")) *strstr(action, "&") = 0;
1053 if (strstr(action, " ")) *strstr(action, " ") = 0;
1055 extract_token(arg1, pathname, 2, '/', sizeof arg1);
1056 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
1057 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
1058 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
1060 extract_token(arg2, pathname, 3, '/', sizeof arg2);
1061 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
1062 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
1063 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
1065 extract_token(arg3, pathname, 4, '/', sizeof arg3);
1066 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
1067 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
1068 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
1070 extract_token(arg4, pathname, 5, '/', sizeof arg4);
1071 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
1072 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
1073 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
1075 extract_token(arg5, pathname, 6, '/', sizeof arg5);
1076 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
1077 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
1078 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
1080 extract_token(arg6, pathname, 7, '/', sizeof arg6);
1081 if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
1082 if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
1083 if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
1085 extract_token(arg7, pathname, 8, '/', sizeof arg7);
1086 if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
1087 if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
1088 if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
1090 while (hptr != NULL) {
1091 safestrncpy(buf, hptr->line, sizeof buf);
1092 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1095 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1096 safestrncpy(cookie, &buf[15], sizeof cookie);
1097 cookie_to_stuff(cookie, NULL,
1098 c_username, sizeof c_username,
1099 c_password, sizeof c_password,
1100 c_roomname, sizeof c_roomname);
1102 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1103 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1104 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1105 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1107 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1108 ContentLength = atoi(&buf[16]);
1110 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1111 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1113 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1114 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1116 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1118 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1121 else if (!strncasecmp(buf, "Host: ", 6)) {
1122 if (strlen(WC->http_host) == 0) {
1123 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1126 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1127 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1128 while (num_tokens(browser_host, ',') > 1) {
1129 remove_token(browser_host, 0, ',');
1131 striplt(browser_host);
1133 /** Only WAP gateways explicitly name this content-type */
1134 else if (strstr(buf, "text/vnd.wap.wml")) {
1139 if (ContentLength > 0) {
1140 content = malloc(ContentLength + SIZ);
1141 memset(content, 0, ContentLength + SIZ);
1142 sprintf(content, "Content-type: %s\n"
1143 "Content-length: %d\n\n",
1144 ContentType, ContentLength);
1145 body_start = strlen(content);
1147 /** Read the entire input data at once. */
1148 client_read(WC->http_sock, &content[BytesRead+body_start],
1151 if (!strncasecmp(ContentType,
1152 "application/x-www-form-urlencoded", 33)) {
1153 addurls(&content[body_start]);
1154 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1155 content_end = content + ContentLength + body_start;
1156 mime_parser(content, content_end, *upload_handler,
1157 NULL, NULL, NULL, 0);
1163 /** make a note of where we are in case the user wants to save it */
1164 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1165 remove_token(WC->this_page, 2, ' ');
1166 remove_token(WC->this_page, 0, ' ');
1168 /** If there are variables in the URL, we must grab them now */
1169 for (a = 0; a < strlen(cmd); ++a) {
1170 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1171 for (b = a; b < strlen(cmd); ++b)
1172 if (isspace(cmd[b]))
1174 addurls(&cmd[a + 1]);
1179 /** If it's a "force 404" situation then display the error and bail. */
1180 if (!strcmp(action, "404")) {
1181 wprintf("HTTP/1.1 404 Not found\r\n");
1182 wprintf("Content-Type: text/plain\r\n");
1184 wprintf("Not found\r\n");
1185 goto SKIP_ALL_THIS_CRAP;
1188 /** Static content can be sent without connecting to Citadel. */
1190 for (a=0; a<ndirs; ++a) {
1191 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1196 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1197 action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1198 for (a=0; a<8; ++a) {
1199 if (buf[strlen(buf)-1] == '/') {
1200 buf[strlen(buf)-1] = 0;
1203 for (a = 0; a < strlen(buf); ++a) {
1204 if (isspace(buf[a])) {
1209 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1213 * If we're not connected to a Citadel server, try to hook up the
1216 if (!WC->connected) {
1217 if (!strcasecmp(ctdlhost, "uds")) {
1218 /* unix domain socket */
1219 sprintf(buf, "%s/citadel.socket", ctdlport);
1220 WC->serv_sock = uds_connectsock(buf);
1224 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1227 if (WC->serv_sock < 0) {
1229 goto SKIP_ALL_THIS_CRAP;
1233 serv_getln(buf, sizeof buf); /** get the server welcome message */
1236 * From what host is our user connecting? Go with
1237 * the host at the other end of the HTTP socket,
1238 * unless we are following X-Forwarded-For: headers
1239 * and such a header has already turned up something.
1241 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1242 locate_host(browser_host, WC->http_sock);
1245 get_serv_info(browser_host, user_agent);
1246 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1247 wprintf(_("You are connected to a Citadel "
1248 "server running Citadel %d.%02d. \n"
1249 "In order to run this version of WebCit "
1250 "you must also have Citadel %d.%02d or"
1252 serv_info.serv_rev_level / 100,
1253 serv_info.serv_rev_level % 100,
1254 MINIMUM_CIT_VERSION / 100,
1255 MINIMUM_CIT_VERSION % 100
1257 end_webcit_session();
1258 goto SKIP_ALL_THIS_CRAP;
1264 * Functions which can be performed without logging in
1266 if (!strcasecmp(action, "listsub")) {
1268 goto SKIP_ALL_THIS_CRAP;
1270 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1271 if (!strcasecmp(action, "freebusy")) {
1273 goto SKIP_ALL_THIS_CRAP;
1278 * If we're not logged in, but we have HTTP Authentication data,
1279 * try logging in to Citadel using that.
1281 if ((!WC->logged_in)
1282 && (strlen(c_httpauth_user) > 0)
1283 && (strlen(c_httpauth_pass) > 0)) {
1284 serv_printf("USER %s", c_httpauth_user);
1285 serv_getln(buf, sizeof buf);
1286 if (buf[0] == '3') {
1287 serv_printf("PASS %s", c_httpauth_pass);
1288 serv_getln(buf, sizeof buf);
1289 if (buf[0] == '2') {
1290 become_logged_in(c_httpauth_user,
1291 c_httpauth_pass, buf);
1292 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1293 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1295 /** Should only display when password is wrong */
1296 authorization_required(&buf[4]);
1297 goto SKIP_ALL_THIS_CRAP;
1302 /** This needs to run early */
1303 if (!strcasecmp(action, "rss")) {
1304 display_rss(bstr("room"), request_method);
1305 goto SKIP_ALL_THIS_CRAP;
1309 * The GroupDAV stuff relies on HTTP authentication instead of
1310 * our session's authentication.
1312 if (!strncasecmp(action, "groupdav", 8)) {
1313 groupdav_main(req, ContentType, /* do GroupDAV methods */
1314 ContentLength, content+body_start);
1315 if (!WC->logged_in) {
1316 WC->killthis = 1; /* If not logged in, don't */
1317 } /* keep the session active */
1318 goto SKIP_ALL_THIS_CRAP;
1323 * Automatically send requests with any method other than GET or
1324 * POST to the GroupDAV code as well.
1326 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1327 groupdav_main(req, ContentType, /** do GroupDAV methods */
1328 ContentLength, content+body_start);
1329 if (!WC->logged_in) {
1330 WC->killthis = 1; /** If not logged in, don't */
1331 } /** keep the session active */
1332 goto SKIP_ALL_THIS_CRAP;
1336 * If we're not logged in, but we have username and password cookies
1337 * supplied by the browser, try using them to log in.
1339 if ((!WC->logged_in)
1340 && (strlen(c_username) > 0)
1341 && (strlen(c_password) > 0)) {
1342 serv_printf("USER %s", c_username);
1343 serv_getln(buf, sizeof buf);
1344 if (buf[0] == '3') {
1345 serv_printf("PASS %s", c_password);
1346 serv_getln(buf, sizeof buf);
1347 if (buf[0] == '2') {
1348 become_logged_in(c_username, c_password, buf);
1353 * If we don't have a current room, but a cookie specifying the
1354 * current room is supplied, make an effort to go there.
1356 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1357 serv_printf("GOTO %s", c_roomname);
1358 serv_getln(buf, sizeof buf);
1359 if (buf[0] == '2') {
1360 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1364 if (!strcasecmp(action, "image")) {
1368 * All functions handled below this point ... make sure we log in
1369 * before doing anything else!
1371 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1373 } else if (!WC->logged_in) {
1374 display_login(NULL);
1378 * Various commands...
1381 else if (!strcasecmp(action, "do_welcome")) {
1383 } else if (!strcasecmp(action, "blank")) {
1385 } else if (!strcasecmp(action, "do_template")) {
1387 } else if (!strcasecmp(action, "display_aide_menu")) {
1388 display_aide_menu();
1389 } else if (!strcasecmp(action, "display_main_menu")) {
1390 display_main_menu();
1391 } else if (!strcasecmp(action, "who")) {
1393 } else if (!strcasecmp(action, "sslg")) {
1394 seconds_since_last_gexp();
1395 } else if (!strcasecmp(action, "who_inner_html")) {
1396 begin_ajax_response();
1398 end_ajax_response();
1399 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1400 begin_ajax_response();
1402 end_ajax_response();
1403 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1404 begin_ajax_response();
1405 do_iconbar_roomlist();
1406 end_ajax_response();
1407 } else if (!strcasecmp(action, "knrooms")) {
1409 } else if (!strcasecmp(action, "gotonext")) {
1412 } else if (!strcasecmp(action, "skip")) {
1414 } else if (!strcasecmp(action, "ungoto")) {
1416 } else if (!strcasecmp(action, "dotgoto")) {
1417 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1420 smart_goto(bstr("room"));
1421 } else if (!strcasecmp(action, "dotskip")) {
1422 smart_goto(bstr("room"));
1423 } else if (!strcasecmp(action, "termquit")) {
1425 } else if (!strcasecmp(action, "readnew")) {
1426 readloop("readnew");
1427 } else if (!strcasecmp(action, "readold")) {
1428 readloop("readold");
1429 } else if (!strcasecmp(action, "readfwd")) {
1430 readloop("readfwd");
1431 } else if (!strcasecmp(action, "headers")) {
1432 readloop("headers");
1433 } else if (!strcasecmp(action, "do_search")) {
1434 readloop("do_search");
1435 } else if (!strcasecmp(action, "msg")) {
1436 embed_message(arg1);
1437 } else if (!strcasecmp(action, "printmsg")) {
1438 print_message(arg1);
1439 } else if (!strcasecmp(action, "msgheaders")) {
1440 display_headers(arg1);
1441 } else if (!strcasecmp(action, "wiki")) {
1442 display_wiki_page();
1443 } else if (!strcasecmp(action, "display_enter")) {
1445 } else if (!strcasecmp(action, "post")) {
1447 } else if (!strcasecmp(action, "move_msg")) {
1449 } else if (!strcasecmp(action, "delete_msg")) {
1451 } else if (!strcasecmp(action, "userlist")) {
1453 } else if (!strcasecmp(action, "showuser")) {
1455 } else if (!strcasecmp(action, "display_page")) {
1457 } else if (!strcasecmp(action, "page_user")) {
1459 } else if (!strcasecmp(action, "chat")) {
1461 } else if (!strcasecmp(action, "display_private")) {
1462 display_private("", 0);
1463 } else if (!strcasecmp(action, "goto_private")) {
1465 } else if (!strcasecmp(action, "zapped_list")) {
1467 } else if (!strcasecmp(action, "display_zap")) {
1469 } else if (!strcasecmp(action, "zap")) {
1471 } else if (!strcasecmp(action, "display_entroom")) {
1473 } else if (!strcasecmp(action, "entroom")) {
1475 } else if (!strcasecmp(action, "display_whok")) {
1477 } else if (!strcasecmp(action, "do_invt_kick")) {
1479 } else if (!strcasecmp(action, "display_editroom")) {
1481 } else if (!strcasecmp(action, "netedit")) {
1483 } else if (!strcasecmp(action, "editroom")) {
1485 } else if (!strcasecmp(action, "display_editinfo")) {
1486 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1487 } else if (!strcasecmp(action, "editinfo")) {
1488 save_edit(_("Room info"), "EINF 1", 1);
1489 } else if (!strcasecmp(action, "display_editbio")) {
1490 sprintf(buf, "RBIO %s", WC->wc_fullname);
1491 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1492 } else if (!strcasecmp(action, "editbio")) {
1493 save_edit(_("Your bio"), "EBIO", 0);
1494 } else if (!strcasecmp(action, "confirm_move_msg")) {
1496 } else if (!strcasecmp(action, "delete_room")) {
1498 } else if (!strcasecmp(action, "validate")) {
1500 } else if (!strcasecmp(action, "display_editpic")) {
1501 display_graphics_upload(_("your photo"),
1504 } else if (!strcasecmp(action, "editpic")) {
1505 do_graphics_upload("UIMG 1|_userpic_");
1506 } else if (!strcasecmp(action, "display_editroompic")) {
1507 display_graphics_upload(_("the icon for this room"),
1510 } else if (!strcasecmp(action, "editroompic")) {
1511 do_graphics_upload("UIMG 1|_roompic_");
1512 } else if (!strcasecmp(action, "delete_floor")) {
1514 } else if (!strcasecmp(action, "rename_floor")) {
1516 } else if (!strcasecmp(action, "create_floor")) {
1518 } else if (!strcasecmp(action, "display_editfloorpic")) {
1519 sprintf(buf, "UIMG 0|_floorpic_|%s",
1520 bstr("which_floor"));
1521 display_graphics_upload(_("the icon for this floor"),
1524 } else if (!strcasecmp(action, "editfloorpic")) {
1525 sprintf(buf, "UIMG 1|_floorpic_|%s",
1526 bstr("which_floor"));
1527 do_graphics_upload(buf);
1528 } else if (!strcasecmp(action, "display_reg")) {
1530 } else if (!strcasecmp(action, "display_changepw")) {
1532 } else if (!strcasecmp(action, "changepw")) {
1534 } else if (!strcasecmp(action, "display_edit_node")) {
1535 display_edit_node();
1536 } else if (!strcasecmp(action, "edit_node")) {
1538 } else if (!strcasecmp(action, "display_netconf")) {
1540 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1541 display_confirm_delete_node();
1542 } else if (!strcasecmp(action, "delete_node")) {
1544 } else if (!strcasecmp(action, "display_add_node")) {
1546 } else if (!strcasecmp(action, "add_node")) {
1548 } else if (!strcasecmp(action, "terminate_session")) {
1550 terminate_session();
1551 } else if (!strcasecmp(action, "edit_me")) {
1553 } else if (!strcasecmp(action, "display_siteconfig")) {
1554 display_siteconfig();
1555 } else if (!strcasecmp(action, "chat_recv")) {
1557 } else if (!strcasecmp(action, "chat_send")) {
1559 } else if (!strcasecmp(action, "siteconfig")) {
1561 } else if (!strcasecmp(action, "display_generic")) {
1563 } else if (!strcasecmp(action, "do_generic")) {
1565 } else if (!strcasecmp(action, "ajax_servcmd")) {
1567 } else if (!strcasecmp(action, "display_menubar")) {
1569 } else if (!strcasecmp(action, "mimepart")) {
1570 mimepart(arg1, arg2, 0);
1571 } else if (!strcasecmp(action, "mimepart_download")) {
1572 mimepart(arg1, arg2, 1);
1573 } else if (!strcasecmp(action, "edit_vcard")) {
1575 } else if (!strcasecmp(action, "submit_vcard")) {
1577 } else if (!strcasecmp(action, "select_user_to_edit")) {
1578 select_user_to_edit(NULL, NULL);
1579 } else if (!strcasecmp(action, "display_edituser")) {
1580 display_edituser(NULL, 0);
1581 } else if (!strcasecmp(action, "edituser")) {
1583 } else if (!strcasecmp(action, "create_user")) {
1585 } else if (!strcasecmp(action, "changeview")) {
1587 } else if (!strcasecmp(action, "change_start_page")) {
1588 change_start_page();
1589 } else if (!strcasecmp(action, "display_floorconfig")) {
1590 display_floorconfig(NULL);
1591 } else if (!strcasecmp(action, "toggle_self_service")) {
1592 toggle_self_service();
1593 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1594 } else if (!strcasecmp(action, "display_edit_task")) {
1595 display_edit_task();
1596 } else if (!strcasecmp(action, "save_task")) {
1598 } else if (!strcasecmp(action, "display_edit_event")) {
1599 display_edit_event();
1600 } else if (!strcasecmp(action, "save_event")) {
1602 } else if (!strcasecmp(action, "respond_to_request")) {
1603 respond_to_request();
1604 } else if (!strcasecmp(action, "handle_rsvp")) {
1607 } else if (!strcasecmp(action, "summary")) {
1609 } else if (!strcasecmp(action, "summary_inner_div")) {
1610 begin_ajax_response();
1611 summary_inner_div();
1612 end_ajax_response();
1613 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1614 display_customize_iconbar();
1615 } else if (!strcasecmp(action, "commit_iconbar")) {
1617 } else if (!strcasecmp(action, "set_room_policy")) {
1619 } else if (!strcasecmp(action, "display_inetconf")) {
1621 } else if (!strcasecmp(action, "save_inetconf")) {
1623 } else if (!strcasecmp(action, "display_smtpqueue")) {
1624 display_smtpqueue();
1625 } else if (!strcasecmp(action, "setup_wizard")) {
1627 } else if (!strcasecmp(action, "display_preferences")) {
1628 display_preferences();
1629 } else if (!strcasecmp(action, "set_preferences")) {
1631 } else if (!strcasecmp(action, "recp_autocomplete")) {
1632 recp_autocomplete(bstr("recp"));
1633 } else if (!strcasecmp(action, "cc_autocomplete")) {
1634 recp_autocomplete(bstr("cc"));
1635 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1636 recp_autocomplete(bstr("bcc"));
1637 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1638 set_floordiv_expanded(arg1);
1639 } else if (!strcasecmp(action, "diagnostics")) {
1640 output_headers(1, 1, 1, 0, 0, 0);
1641 wprintf("Session: %d<hr />\n", WC->wc_session);
1642 wprintf("Command: <br /><PRE>\n");
1644 wprintf("</PRE><hr />\n");
1645 wprintf("Variables: <br /><PRE>\n");
1647 wprintf("</PRE><hr />\n");
1649 } else if (!strcasecmp(action, "updatenote")) {
1653 /** When all else fais, display the main menu. */
1655 display_main_menu();
1660 if (content != NULL) {
1665 if (WC->upload_length > 0) {
1667 WC->upload_length = 0;