4 * This is the main transaction loop of the web service. It maintains a
5 * persistent session to the Citadel server, handling HTTP WebCit requests as
6 * they arrive and presenting a user interface.
12 #include "webserver.h"
13 #include "mime_parser.h"
16 * Subdirectories from which the client may request static content
18 char *static_content_dirs[] = {
24 * String to unset the cookie.
25 * Any date "in the past" will work, so I chose my birthday, right down to
26 * the exact minute. :)
28 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
30 void unescape_input(char *buf)
35 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
36 buf[strlen(buf) - 1] = 0;
38 for (a = 0; a < strlen(buf); ++a) {
46 sscanf(hex, "%02x", &b);
48 strcpy(&buf[a + 1], &buf[a + 3]);
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);
110 struct urlcontent *u;
112 while (WC->urlstrings != NULL) {
113 free(WC->urlstrings->url_data);
114 u = WC->urlstrings->next;
115 free(WC->urlstrings);
121 * Diagnostic function to display the contents of all variables
125 struct urlcontent *u;
127 for (u = WC->urlstrings; u != NULL; u = u->next) {
128 wprintf("%38s = %s\n", u->url_key, u->url_data);
132 char *bstr(char *key)
134 struct urlcontent *u;
136 for (u = WC->urlstrings; u != NULL; u = u->next) {
137 if (!strcasecmp(u->url_key, key))
138 return (u->url_data);
144 void wprintf(const char *format,...)
149 va_start(arg_ptr, format);
150 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
153 client_write(wbuf, strlen(wbuf));
158 * wDumpContent() wraps up an HTTP session, closes tags, etc.
160 * print_standard_html_footer should be set to 0 to transmit only, 1 to
161 * append the main menu and closing tags, or 2 to
162 * append the closing tags only.
164 void wDumpContent(int print_standard_html_footer)
166 if (print_standard_html_footer) {
167 wprintf("</div>\n"); /* end of "text" div */
168 do_template("trailing");
171 /* If we've been saving it all up for one big output burst,
172 * go ahead and do that now.
179 * Copy a string, escaping characters which have meaning in HTML. If
180 * nbsp is nonzero, spaces are converted to non-breaking spaces.
182 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
187 for (a = 0; a < strlen(strbuf); ++a) {
188 if (strbuf[a] == '<')
189 strcat(target, "<");
190 else if (strbuf[a] == '>')
191 strcat(target, ">");
192 else if (strbuf[a] == '&')
193 strcat(target, "&");
194 else if (strbuf[a] == '\"')
195 strcat(target, """);
196 else if (strbuf[a] == '\'')
197 strcat(target, "'");
198 else if (strbuf[a] == LB)
200 else if (strbuf[a] == RB)
202 else if (strbuf[a] == QU)
203 strcat(target, "\"");
204 else if ((strbuf[a] == 32) && (nbsp == 1))
205 strcat(target, " ");
206 else if ((strbuf[a] == '\n') && (nolinebreaks))
207 strcat(target, ""); /* nothing */
208 else if ((strbuf[a] == '\r') && (nolinebreaks))
209 strcat(target, ""); /* nothing */
211 strncat(target, &strbuf[a], 1);
215 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
219 if (strbuf == NULL) return;
220 buf = malloc( (3 * strlen(strbuf)) + SIZ );
221 stresc(buf, strbuf, nbsp, nolinebreaks);
226 void escputs(char *strbuf)
228 escputs1(strbuf, 0, 0);
232 * Escape a string for feeding out as a URL.
233 * Returns a pointer to a buffer that must be freed by the caller!
235 void urlesc(char *outbuf, char *strbuf)
238 char *ec = " #&;`'|*?-~<>^()[]{}$\"\\";
242 for (a = 0; a < strlen(strbuf); ++a) {
244 for (b = 0; b < strlen(ec); ++b) {
245 if (strbuf[a] == ec[b])
250 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
252 sprintf(&outbuf[b], "%c", strbuf[a]);
256 void urlescputs(char *strbuf)
260 urlesc(outbuf, strbuf);
261 wprintf("%s", outbuf);
266 * Copy a string, escaping characters for JavaScript strings.
268 void jsesc(char *target, char *strbuf)
273 for (a = 0; a < strlen(strbuf); ++a) {
274 if (strbuf[a] == '<')
276 else if (strbuf[a] == '>')
278 else if (strbuf[a] == '\"')
279 strcat(target, """);
280 else if (strbuf[a] == '&')
281 strcat(target, "&;");
282 else if (strbuf[a] == '\'')
283 strcat(target, "\\'");
285 strncat(target, &strbuf[a], 1);
290 void jsescputs(char *strbuf)
294 jsesc(outbuf, strbuf);
295 wprintf("%s", outbuf);
299 * Copy a string, escaping characters for message text hold
301 void msgesc(char *target, char *strbuf)
306 for (a = 0; a < strlen(strbuf); ++a) {
307 if (strbuf[a] == '\n')
309 else if (strbuf[a] == '\r')
311 else if (strbuf[a] == '\'')
312 strcat(target, "'");
314 strncat(target, &strbuf[a], 1);
319 void msgescputs(char *strbuf) {
322 if (strbuf == NULL) return;
323 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
324 msgesc(outbuf, strbuf);
325 wprintf("%s", outbuf);
333 * Output all that important stuff that the browser will want to see
335 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
336 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
338 int do_room_banner, /* 0=no, 1=yes, */
339 /* 2 = I'm going to embed my own, so don't open the */
340 /* <div id="content"> either. */
342 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
343 int suppress_check, /* 1 = suppress check for instant messages */
344 int cache /* 1 = allow browser to cache this page */
349 wprintf("HTTP/1.1 200 OK\n");
350 httpdate(httpnow, time(NULL));
352 if (do_httpheaders) {
353 wprintf("Content-type: text/html; charset=utf-8\r\n"
355 "Connection: close\r\n",
356 SERVER, serv_info.serv_software
361 wprintf("Pragma: public\r\n"
362 "Cache-Control: max-age=3600, must-revalidate\r\n"
363 "Last-modified: %s\r\n",
368 wprintf("Pragma: no-cache\r\n"
369 "Cache-Control: no-store\r\n"
373 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
374 WC->wc_password, WC->wc_roomname);
377 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
379 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
380 if (server_cookie != NULL) {
381 wprintf("%s\n", server_cookie);
394 if (WC->HaveInstantMessages) {
395 wprintf("<div id=\"page_popup\">\n");
399 if (strlen(WC->ImportantMessage) > 0) {
400 wprintf("<div id=\"important_message\">\n");
401 wprintf("<SPAN CLASS=\"imsg\">"
402 "%s</SPAN><br />\n", WC->ImportantMessage);
404 wprintf("<script type=\"text/javascript\">\n"
405 " setTimeout('hide_imsg_popup()', 3000); \n"
407 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
409 if ( (WC->logged_in) && (!unset_cookies) ) {
410 wprintf("<div id=\"iconbar\">");
414 if (do_room_banner == 1) {
415 wprintf("<div id=\"banner\">\n");
416 embed_room_banner(NULL, navbar_default);
421 if (do_room_banner == 1) {
422 wprintf("<div id=\"content\">\n");
428 * Generic function to do an HTTP redirect. Easy and fun.
430 void http_redirect(char *whichpage) {
431 wprintf("HTTP/1.1 302 Moved Temporarily\n");
432 wprintf("Location: %s\r\n", whichpage);
433 wprintf("URI: %s\r\n", whichpage);
434 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
435 wprintf("<html><body>");
436 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
437 wprintf("</body></html>\n");
442 void check_for_instant_messages()
447 serv_getln(buf, sizeof buf);
448 if (buf[3] == '*') WC->HaveInstantMessages = 1;
455 * Output a piece of content to the web browser
457 void http_transmit_thing(char *thing, size_t length, char *content_type,
460 output_headers(0, 0, 0, 0, 0, is_static);
462 wprintf("Content-type: %s\r\n"
464 "Connection: close\r\n",
469 /* If we can send the data out compressed, please do so. */
471 char *compressed_data = NULL;
472 uLongf compressed_len;
474 compressed_len = (uLongf) ((length * 101) / 100) + 100;
475 compressed_data = malloc(compressed_len);
477 if (compress_gzip((Bytef *) compressed_data,
480 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
481 wprintf("Content-encoding: gzip\r\n"
482 "Content-length: %ld\r\n"
484 (long) compressed_len
486 client_write(compressed_data, (size_t)compressed_len);
487 free(compressed_data);
493 /* No compression ... just send it out as-is */
494 wprintf("Content-length: %ld\r\n"
498 client_write(thing, (size_t)length);
504 void output_static(char *what)
510 char content_type[128];
512 fp = fopen(what, "rb");
514 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
515 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
516 wprintf("Content-Type: text/plain\r\n");
518 wprintf("Cannot open %s: %s\n", what, strerror(errno));
520 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
521 safestrncpy(content_type, "image/gif", sizeof content_type);
522 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
523 safestrncpy(content_type, "text/plain", sizeof content_type);
524 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
525 safestrncpy(content_type, "text/css", sizeof content_type);
526 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
527 safestrncpy(content_type, "image/jpeg", sizeof content_type);
528 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
529 safestrncpy(content_type, "image/png", sizeof content_type);
530 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
531 safestrncpy(content_type, "image/x-icon", sizeof content_type);
532 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
533 safestrncpy(content_type, "text/html", sizeof content_type);
534 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
535 safestrncpy(content_type, "text/html", sizeof content_type);
536 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
537 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
538 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
539 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
540 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
541 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
542 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
543 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
544 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
545 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
546 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
547 safestrncpy(content_type, "text/javascript", sizeof content_type);
549 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
551 fstat(fileno(fp), &statbuf);
552 bytes = statbuf.st_size;
553 bigbuffer = malloc(bytes + 2);
554 fread(bigbuffer, bytes, 1, fp);
557 lprintf(9, "output_static('%s') %s\n", what, content_type);
558 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
561 if (!strcasecmp(bstr("force_close_session"), "yes")) {
562 end_webcit_session();
568 * When the browser requests an image file from the Citadel server,
569 * this function is called to transmit it.
574 char *xferbuf = NULL;
577 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
578 serv_getln(buf, sizeof buf);
580 bytes = extract_long(&buf[4], 0);
581 xferbuf = malloc(bytes + 2);
583 /* Read it from the server */
584 read_server_binary(xferbuf, bytes);
586 serv_getln(buf, sizeof buf);
588 /* Write it to the browser */
589 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
593 /* Instead of an ugly 404, send a 1x1 transparent GIF
594 * when there's no such image on the server.
596 output_static("static/blank.gif");
604 * Generic function to output an arbitrary MIME part from an arbitrary
605 * message number on the server.
607 void mimepart(char *msgnum, char *partnum)
611 char content_type[SIZ];
612 char *content = NULL;
614 serv_printf("OPNA %s|%s", msgnum, partnum);
615 serv_getln(buf, sizeof buf);
617 bytes = extract_long(&buf[4], 0);
618 content = malloc(bytes + 2);
619 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
620 output_headers(0, 0, 0, 0, 0, 0);
621 read_server_binary(content, bytes);
623 serv_getln(buf, sizeof buf);
624 http_transmit_thing(content, bytes, content_type, 0);
627 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
628 output_headers(0, 0, 0, 0, 0, 0);
629 wprintf("Content-Type: text/plain\r\n");
631 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
638 * Read any MIME part of a message, from the server, into memory.
640 char *load_mimepart(long msgnum, char *partnum)
644 char content_type[SIZ];
647 serv_printf("OPNA %ld|%s", msgnum, partnum);
648 serv_getln(buf, sizeof buf);
650 bytes = extract_long(&buf[4], 0);
651 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
653 content = malloc(bytes + 2);
654 read_server_binary(content, bytes);
657 serv_getln(buf, sizeof buf);
658 content[bytes] = 0; /* null terminate for good measure */
669 * Convenience functions to display a page containing only a string
671 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
673 wprintf("HTTP/1.1 200 OK\n");
674 output_headers(1, 1, 2, 0, 0, 0);
675 wprintf("<div id=\"banner\">\n");
676 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
677 wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
678 wprintf("</TD></TR></TABLE>\n");
679 wprintf("</div>\n<div id=\"content\">\n");
680 escputs(messagetext);
688 * Display a blank page.
690 void blank_page(void) {
691 output_headers(1, 1, 0, 0, 0, 0);
697 * A template has been requested
699 void url_do_template(void) {
700 do_template(bstr("template"));
706 * Offer to make any page the user's "start page."
708 void offer_start_page(void) {
709 wprintf("<a href=\"change_start_page?startpage=");
710 urlescputs(WC->this_page);
711 wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
712 wprintf(_("Make this my start page"));
713 wprintf("</FONT></A>");
715 wprintf("<br/><a href=\"rss?room=");
716 urlescputs(WC->wc_roomname);
717 wprintf("\" title=\"RSS 2.0 feed for ");
718 escputs(WC->wc_roomname);
719 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
725 * Change the user's start page
727 void change_start_page(void) {
729 if (bstr("startpage") == NULL) {
730 safestrncpy(WC->ImportantMessage,
731 _("You no longer have a start page selected."),
732 sizeof WC->ImportantMessage);
737 set_preference("startpage", bstr("startpage"), 1);
739 output_headers(1, 1, 0, 0, 0, 0);
740 do_template("newstartpage");
747 void display_success(char *successmessage)
749 convenience_page("007700", "OK", successmessage);
753 /* Authorization required page */
754 /* This is probably temporary and should be revisited */
755 void authorization_required(const char *message)
757 wprintf("HTTP/1.1 401 Authorization Required\r\n");
758 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
759 wprintf("Content-Type: text/html\r\n\r\n");
761 wprintf(_("Authorization Required"));
762 wprintf("</h1>\r\n");
763 wprintf(_("The resource you requested requires a valid username and password. "
764 "You could not be logged in: %s\n"), message);
769 void upload_handler(char *name, char *filename, char *partnum, char *disp,
770 void *content, char *cbtype, char *cbcharset,
771 size_t length, char *encoding, void *userdata)
773 struct urlcontent *u;
775 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
776 name, cbtype, length); */
779 if ( (length > 0) && (strlen(cbtype) == 0) ) {
780 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
781 u->next = WC->urlstrings;
783 safestrncpy(u->url_key, name, sizeof(u->url_key));
784 u->url_data = malloc(length + 1);
785 memcpy(u->url_data, content, length);
786 u->url_data[length] = 0;
790 if ( (length > 0) && (strlen(cbtype) > 0) ) {
791 WC->upload = malloc(length);
792 if (WC->upload != NULL) {
793 WC->upload_length = length;
794 safestrncpy(WC->upload_filename, filename,
795 sizeof(WC->upload_filename));
796 safestrncpy(WC->upload_content_type, cbtype,
797 sizeof(WC->upload_content_type));
798 memcpy(WC->upload, content, length);
801 lprintf(3, "malloc() failed: %s\n", strerror(errno));
808 * Convenience functions to wrap around asynchronous ajax responses
810 void begin_ajax_response(void) {
811 output_headers(0, 0, 0, 0, 0, 0);
813 wprintf("Content-type: text/html; charset=UTF-8\r\n"
815 "Connection: close\r\n"
816 "Pragma: no-cache\r\n"
817 "Cache-Control: no-cache\r\n",
822 void end_ajax_response(void) {
827 void ajax_servcmd(void)
834 begin_ajax_response();
836 serv_printf("%s", bstr("g_cmd"));
837 serv_getln(buf, sizeof buf);
840 serv_printf("\n\n000");
842 if ((buf[0] == '1') || (buf[0] == '8')) {
843 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
844 /* maybe do something with it? */
849 text_to_server(bstr("g_input"), 0);
855 serv_read(junk, len);
861 memset(junk, 0, len);
862 serv_write(junk, len);
875 * Entry point for WebCit transaction
877 void session_loop(struct httprequest *req)
889 char request_method[128];
892 int ContentLength = 0;
894 char ContentType[512];
895 char *content = NULL;
896 char *content_end = NULL;
897 struct httprequest *hptr;
898 char browser_host[SIZ];
899 char user_agent[SIZ];
903 /* We stuff these with the values coming from the client cookies,
904 * so we can use them to reconnect a timed out session if we have to.
906 char c_username[SIZ];
907 char c_password[SIZ];
908 char c_roomname[SIZ];
909 char c_httpauth_string[SIZ];
910 char c_httpauth_user[SIZ];
911 char c_httpauth_pass[SIZ];
914 safestrncpy(c_username, "", sizeof c_username);
915 safestrncpy(c_password, "", sizeof c_password);
916 safestrncpy(c_roomname, "", sizeof c_roomname);
917 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
918 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
919 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
921 WC->upload_length = 0;
928 if (hptr == NULL) return;
930 safestrncpy(cmd, hptr->line, sizeof cmd);
932 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
933 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
935 /* Figure out the action */
936 extract_token(action, pathname, 1, '/', sizeof action);
937 if (strstr(action, "?")) *strstr(action, "?") = 0;
938 if (strstr(action, "&")) *strstr(action, "&") = 0;
939 if (strstr(action, " ")) *strstr(action, " ") = 0;
941 extract_token(arg1, pathname, 2, '/', sizeof arg1);
942 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
943 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
944 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
946 extract_token(arg2, pathname, 3, '/', sizeof arg2);
947 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
948 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
949 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
951 extract_token(arg3, pathname, 4, '/', sizeof arg3);
952 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
953 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
954 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
956 extract_token(arg4, pathname, 5, '/', sizeof arg4);
957 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
958 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
959 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
961 extract_token(arg5, pathname, 6, '/', sizeof arg5);
962 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
963 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
964 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
966 extract_token(arg6, pathname, 7, '/', sizeof arg6);
967 if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
968 if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
969 if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
971 extract_token(arg7, pathname, 8, '/', sizeof arg7);
972 if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
973 if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
974 if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
976 while (hptr != NULL) {
977 safestrncpy(buf, hptr->line, sizeof buf);
980 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
981 safestrncpy(cookie, &buf[15], sizeof cookie);
982 cookie_to_stuff(cookie, NULL,
983 c_username, sizeof c_username,
984 c_password, sizeof c_password,
985 c_roomname, sizeof c_roomname);
987 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
988 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
989 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
990 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
992 else if (!strncasecmp(buf, "Content-length: ", 16)) {
993 ContentLength = atoi(&buf[16]);
995 else if (!strncasecmp(buf, "Content-type: ", 14)) {
996 safestrncpy(ContentType, &buf[14], sizeof ContentType);
998 else if (!strncasecmp(buf, "User-agent: ", 12)) {
999 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1001 else if (!strncasecmp(buf, "Host: ", 6)) {
1002 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1004 /* Only WAP gateways explicitly name this content-type */
1005 else if (strstr(buf, "text/vnd.wap.wml")) {
1010 if (ContentLength > 0) {
1011 content = malloc(ContentLength + SIZ);
1012 memset(content, 0, ContentLength + SIZ);
1013 sprintf(content, "Content-type: %s\n"
1014 "Content-length: %d\n\n",
1015 ContentType, ContentLength);
1016 body_start = strlen(content);
1018 /* Read the entire input data at once. */
1019 client_read(WC->http_sock, &content[BytesRead+body_start],
1022 if (!strncasecmp(ContentType,
1023 "application/x-www-form-urlencoded", 33)) {
1024 addurls(&content[body_start]);
1025 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1026 content_end = content + ContentLength + body_start;
1027 mime_parser(content, content_end, *upload_handler,
1028 NULL, NULL, NULL, 0);
1034 /* make a note of where we are in case the user wants to save it */
1035 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1036 remove_token(WC->this_page, 2, ' ');
1037 remove_token(WC->this_page, 0, ' ');
1039 /* If there are variables in the URL, we must grab them now */
1040 for (a = 0; a < strlen(cmd); ++a) {
1041 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1042 for (b = a; b < strlen(cmd); ++b)
1043 if (isspace(cmd[b]))
1045 addurls(&cmd[a + 1]);
1051 /* Static content can be sent without connecting to Citadel. */
1053 for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
1054 if (!strcasecmp(action, static_content_dirs[a])) {
1059 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1060 action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1061 for (a=0; a<8; ++a) {
1062 if (buf[strlen(buf)-1] == '/') {
1063 buf[strlen(buf)-1] = 0;
1066 for (a = 0; a < strlen(buf); ++a) {
1067 if (isspace(buf[a])) {
1072 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1076 * If we're not connected to a Citadel server, try to hook up the
1079 if (!WC->connected) {
1080 if (!strcasecmp(ctdlhost, "uds")) {
1081 /* unix domain socket */
1082 sprintf(buf, "%s/citadel.socket", ctdlport);
1083 WC->serv_sock = uds_connectsock(buf);
1087 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1090 if (WC->serv_sock < 0) {
1092 goto SKIP_ALL_THIS_CRAP;
1096 serv_getln(buf, sizeof buf); /* get the server welcome message */
1097 locate_host(browser_host, WC->http_sock);
1098 get_serv_info(browser_host, user_agent);
1099 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1100 wprintf(_("You are connected to a Citadel "
1101 "server running Citadel %d.%02d. \n"
1102 "In order to run this version of WebCit "
1103 "you must also have Citadel %d.%02d or"
1105 serv_info.serv_rev_level / 100,
1106 serv_info.serv_rev_level % 100,
1107 MINIMUM_CIT_VERSION / 100,
1108 MINIMUM_CIT_VERSION % 100
1110 end_webcit_session();
1111 goto SKIP_ALL_THIS_CRAP;
1117 * Functions which can be performed without logging in
1119 if (!strcasecmp(action, "listsub")) {
1121 goto SKIP_ALL_THIS_CRAP;
1123 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1124 if (!strcasecmp(action, "freebusy")) {
1126 goto SKIP_ALL_THIS_CRAP;
1131 * If we're not logged in, but we have HTTP Authentication data,
1132 * try logging in to Citadel using that.
1134 if ((!WC->logged_in)
1135 && (strlen(c_httpauth_user) > 0)
1136 && (strlen(c_httpauth_pass) > 0)) {
1137 serv_printf("USER %s", c_httpauth_user);
1138 serv_getln(buf, sizeof buf);
1139 if (buf[0] == '3') {
1140 serv_printf("PASS %s", c_httpauth_pass);
1141 serv_getln(buf, sizeof buf);
1142 if (buf[0] == '2') {
1143 become_logged_in(c_httpauth_user,
1144 c_httpauth_pass, buf);
1145 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1146 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1148 /* Should only display when password is wrong */
1149 authorization_required(&buf[4]);
1150 goto SKIP_ALL_THIS_CRAP;
1155 /* This needs to run early */
1156 if (!strcasecmp(action, "rss")) {
1157 display_rss(bstr("room"), request_method);
1158 goto SKIP_ALL_THIS_CRAP;
1162 * The GroupDAV stuff relies on HTTP authentication instead of
1163 * our session's authentication.
1165 if (!strncasecmp(action, "groupdav", 8)) {
1166 groupdav_main(req, ContentType, /* do GroupDAV methods */
1167 ContentLength, content+body_start);
1168 if (!WC->logged_in) {
1169 WC->killthis = 1; /* If not logged in, don't */
1170 } /* keep the session active */
1171 goto SKIP_ALL_THIS_CRAP;
1176 * Automatically send requests with any method other than GET or
1177 * POST to the GroupDAV code as well.
1179 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1180 groupdav_main(req, ContentType, /* do GroupDAV methods */
1181 ContentLength, content+body_start);
1182 if (!WC->logged_in) {
1183 WC->killthis = 1; /* If not logged in, don't */
1184 } /* keep the session active */
1185 goto SKIP_ALL_THIS_CRAP;
1189 * If we're not logged in, but we have username and password cookies
1190 * supplied by the browser, try using them to log in.
1192 if ((!WC->logged_in)
1193 && (strlen(c_username) > 0)
1194 && (strlen(c_password) > 0)) {
1195 serv_printf("USER %s", c_username);
1196 serv_getln(buf, sizeof buf);
1197 if (buf[0] == '3') {
1198 serv_printf("PASS %s", c_password);
1199 serv_getln(buf, sizeof buf);
1200 if (buf[0] == '2') {
1201 become_logged_in(c_username, c_password, buf);
1206 * If we don't have a current room, but a cookie specifying the
1207 * current room is supplied, make an effort to go there.
1209 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1210 serv_printf("GOTO %s", c_roomname);
1211 serv_getln(buf, sizeof buf);
1212 if (buf[0] == '2') {
1213 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1218 * If there are instant messages waiting, retrieve them for display.
1220 check_for_instant_messages();
1222 if (!strcasecmp(action, "image")) {
1226 * All functions handled below this point ... make sure we log in
1227 * before doing anything else!
1229 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1231 } else if (!WC->logged_in) {
1232 display_login(NULL);
1236 * Various commands...
1239 else if (!strcasecmp(action, "do_welcome")) {
1241 } else if (!strcasecmp(action, "blank")) {
1243 } else if (!strcasecmp(action, "do_template")) {
1245 } else if (!strcasecmp(action, "display_aide_menu")) {
1246 display_aide_menu();
1247 } else if (!strcasecmp(action, "display_main_menu")) {
1248 display_main_menu();
1249 } else if (!strcasecmp(action, "who")) {
1251 } else if (!strcasecmp(action, "who_inner_html")) {
1252 begin_ajax_response();
1254 end_ajax_response();
1255 } else if (!strcasecmp(action, "knrooms")) {
1257 } else if (!strcasecmp(action, "gotonext")) {
1260 } else if (!strcasecmp(action, "skip")) {
1262 } else if (!strcasecmp(action, "ungoto")) {
1264 } else if (!strcasecmp(action, "dotgoto")) {
1265 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1268 smart_goto(bstr("room"));
1269 } else if (!strcasecmp(action, "dotskip")) {
1270 smart_goto(bstr("room"));
1271 } else if (!strcasecmp(action, "termquit")) {
1273 } else if (!strcasecmp(action, "readnew")) {
1274 readloop("readnew");
1275 } else if (!strcasecmp(action, "readold")) {
1276 readloop("readold");
1277 } else if (!strcasecmp(action, "readfwd")) {
1278 readloop("readfwd");
1279 } else if (!strcasecmp(action, "headers")) {
1280 readloop("headers");
1281 } else if (!strcasecmp(action, "msg")) {
1282 embed_message(arg1);
1283 } else if (!strcasecmp(action, "printmsg")) {
1284 print_message(arg1);
1285 } else if (!strcasecmp(action, "display_enter")) {
1287 } else if (!strcasecmp(action, "post")) {
1289 } else if (!strcasecmp(action, "move_msg")) {
1291 } else if (!strcasecmp(action, "delete_msg")) {
1293 } else if (!strcasecmp(action, "userlist")) {
1295 } else if (!strcasecmp(action, "showuser")) {
1297 } else if (!strcasecmp(action, "display_page")) {
1299 } else if (!strcasecmp(action, "page_user")) {
1301 } else if (!strcasecmp(action, "chat")) {
1303 } else if (!strcasecmp(action, "display_private")) {
1304 display_private("", 0);
1305 } else if (!strcasecmp(action, "goto_private")) {
1307 } else if (!strcasecmp(action, "zapped_list")) {
1309 } else if (!strcasecmp(action, "display_zap")) {
1311 } else if (!strcasecmp(action, "zap")) {
1313 } else if (!strcasecmp(action, "display_entroom")) {
1315 } else if (!strcasecmp(action, "entroom")) {
1317 } else if (!strcasecmp(action, "display_whok")) {
1319 } else if (!strcasecmp(action, "do_invt_kick")) {
1321 } else if (!strcasecmp(action, "display_editroom")) {
1323 } else if (!strcasecmp(action, "netedit")) {
1325 } else if (!strcasecmp(action, "editroom")) {
1327 } else if (!strcasecmp(action, "display_editinfo")) {
1328 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1329 } else if (!strcasecmp(action, "editinfo")) {
1330 save_edit(_("Room info"), "EINF 1", 1);
1331 } else if (!strcasecmp(action, "display_editbio")) {
1332 sprintf(buf, "RBIO %s", WC->wc_username);
1333 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1334 } else if (!strcasecmp(action, "editbio")) {
1335 save_edit(_("Your bio"), "EBIO", 0);
1336 } else if (!strcasecmp(action, "confirm_move_msg")) {
1338 } else if (!strcasecmp(action, "delete_room")) {
1340 } else if (!strcasecmp(action, "validate")) {
1342 } else if (!strcasecmp(action, "display_editpic")) {
1343 display_graphics_upload(_("your photo"),
1346 } else if (!strcasecmp(action, "editpic")) {
1347 do_graphics_upload("UIMG 1|_userpic_");
1348 } else if (!strcasecmp(action, "display_editroompic")) {
1349 display_graphics_upload(_("the icon for this room"),
1352 } else if (!strcasecmp(action, "editroompic")) {
1353 do_graphics_upload("UIMG 1|_roompic_");
1354 } else if (!strcasecmp(action, "delete_floor")) {
1356 } else if (!strcasecmp(action, "rename_floor")) {
1358 } else if (!strcasecmp(action, "create_floor")) {
1360 } else if (!strcasecmp(action, "display_editfloorpic")) {
1361 sprintf(buf, "UIMG 0|_floorpic_|%s",
1362 bstr("which_floor"));
1363 display_graphics_upload(_("the icon for this floor"),
1366 } else if (!strcasecmp(action, "editfloorpic")) {
1367 sprintf(buf, "UIMG 1|_floorpic_|%s",
1368 bstr("which_floor"));
1369 do_graphics_upload(buf);
1370 } else if (!strcasecmp(action, "display_reg")) {
1372 } else if (!strcasecmp(action, "display_changepw")) {
1374 } else if (!strcasecmp(action, "changepw")) {
1376 } else if (!strcasecmp(action, "display_edit_node")) {
1377 display_edit_node();
1378 } else if (!strcasecmp(action, "edit_node")) {
1380 } else if (!strcasecmp(action, "display_netconf")) {
1382 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1383 display_confirm_delete_node();
1384 } else if (!strcasecmp(action, "delete_node")) {
1386 } else if (!strcasecmp(action, "display_add_node")) {
1388 } else if (!strcasecmp(action, "add_node")) {
1390 } else if (!strcasecmp(action, "terminate_session")) {
1392 terminate_session();
1393 } else if (!strcasecmp(action, "edit_me")) {
1395 } else if (!strcasecmp(action, "display_siteconfig")) {
1396 display_siteconfig();
1397 } else if (!strcasecmp(action, "chat_recv")) {
1399 } else if (!strcasecmp(action, "chat_send")) {
1401 } else if (!strcasecmp(action, "siteconfig")) {
1403 } else if (!strcasecmp(action, "display_generic")) {
1405 } else if (!strcasecmp(action, "do_generic")) {
1407 } else if (!strcasecmp(action, "ajax_servcmd")) {
1409 } else if (!strcasecmp(action, "display_menubar")) {
1411 } else if (!strcasecmp(action, "mimepart")) {
1412 mimepart(arg1, arg2);
1413 } else if (!strcasecmp(action, "edit_vcard")) {
1415 } else if (!strcasecmp(action, "submit_vcard")) {
1417 } else if (!strcasecmp(action, "select_user_to_edit")) {
1418 select_user_to_edit(NULL, NULL);
1419 } else if (!strcasecmp(action, "display_edituser")) {
1420 display_edituser(NULL, 0);
1421 } else if (!strcasecmp(action, "edituser")) {
1423 } else if (!strcasecmp(action, "create_user")) {
1425 } else if (!strcasecmp(action, "changeview")) {
1427 } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1429 } else if (!strcasecmp(action, "change_start_page")) {
1430 change_start_page();
1431 } else if (!strcasecmp(action, "display_floorconfig")) {
1432 display_floorconfig(NULL);
1433 } else if (!strcasecmp(action, "toggle_self_service")) {
1434 toggle_self_service();
1435 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1436 } else if (!strcasecmp(action, "display_edit_task")) {
1437 display_edit_task();
1438 } else if (!strcasecmp(action, "save_task")) {
1440 } else if (!strcasecmp(action, "display_edit_event")) {
1441 display_edit_event();
1442 } else if (!strcasecmp(action, "save_event")) {
1444 } else if (!strcasecmp(action, "respond_to_request")) {
1445 respond_to_request();
1446 } else if (!strcasecmp(action, "handle_rsvp")) {
1449 } else if (!strcasecmp(action, "summary")) {
1451 } else if (!strcasecmp(action, "summary_inner_div")) {
1452 begin_ajax_response();
1453 summary_inner_div();
1454 end_ajax_response();
1455 } else if (!strcasecmp(action, "iconbar")) {
1457 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1458 display_customize_iconbar();
1459 } else if (!strcasecmp(action, "commit_iconbar")) {
1461 } else if (!strcasecmp(action, "set_room_policy")) {
1463 } else if (!strcasecmp(action, "display_inetconf")) {
1465 } else if (!strcasecmp(action, "save_inetconf")) {
1467 } else if (!strcasecmp(action, "setup_wizard")) {
1469 } else if (!strcasecmp(action, "display_preferences")) {
1470 display_preferences();
1471 } else if (!strcasecmp(action, "set_preferences")) {
1473 } else if (!strcasecmp(action, "recp_autocomplete")) {
1474 recp_autocomplete(bstr("recp"));
1475 } else if (!strcasecmp(action, "cc_autocomplete")) {
1476 recp_autocomplete(bstr("cc"));
1477 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1478 recp_autocomplete(bstr("bcc"));
1479 } else if (!strcasecmp(action, "diagnostics")) {
1480 output_headers(1, 1, 1, 0, 0, 0);
1481 wprintf("Session: %d<hr />\n", WC->wc_session);
1482 wprintf("Command: <br /><PRE>\n");
1484 wprintf("</PRE><hr />\n");
1485 wprintf("Variables: <br /><PRE>\n");
1487 wprintf("</PRE><hr />\n");
1491 /* When all else fais, display the main menu. */
1493 display_main_menu();
1498 if (content != NULL) {
1503 if (WC->upload_length > 0) {
1505 WC->upload_length = 0;