4 * This is the actual program called by the webserver. It maintains a
5 * persistent session to the Citadel server, handling HTTP WebCit requests as
6 * they arrive and presenting a user interface.
23 #include "mime_parser.h"
26 char wc_username[256];
27 char wc_password[256];
28 char wc_roomname[256];
30 int TransactionCount = 0;
34 char *ExpressMessages = NULL;
37 struct webcontent *wlist = NULL;
38 struct webcontent *wlast = NULL;
40 struct urlcontent *urlstrings = NULL;
42 static const char *defaulthost = DEFAULT_HOST;
43 static const char *defaultport = DEFAULT_PORT;
45 int upload_length = 0;
49 void unescape_input(char *buf)
54 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
55 buf[strlen(buf)-1] = 0;
57 for (a=0; a<strlen(buf); ++a) {
58 if (buf[a]=='+') buf[a]=' ';
63 sscanf(hex,"%02x",&b);
65 strcpy(&buf[a+1],&buf[a+3]);
72 void addurls(char *url) {
79 while (strlen(up)>0) {
81 /* locate the = sign */
84 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
88 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
91 strcpy(u->url_key, buf);
93 /* now chop that part off */
94 for (a=0; a<=b; ++a) ++up;
96 /* locate the & sign */
99 for (a=0; a<strlen(up); ++a) {
100 if (!strncmp(ptr,"&",1)) {
107 for (a=0; a<b; ++a) ++ptr;
110 u->url_data = malloc(strlen(up)+1);
111 strcpy(u->url_data, up);
113 unescape_input(u->url_data);
119 void free_urls(void) {
120 struct urlcontent *u;
122 while (urlstrings != NULL) {
123 free(urlstrings->url_data);
124 u = urlstrings->next;
131 * Diagnostic function to display the contents of all variables
133 void dump_vars(void) {
134 struct urlcontent *u;
136 for (u = urlstrings; u != NULL; u = u->next) {
137 wprintf("%38s = %s\n", u->url_key, u->url_data);
141 char *bstr(char *key) {
142 struct urlcontent *u;
144 for (u = urlstrings; u != NULL; u = u->next) {
145 if (!strcasecmp(u->url_key, key)) return(u->url_data);
151 void wprintf(const char *format, ...) {
153 struct webcontent *wptr;
155 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
166 va_start(arg_ptr, format);
167 vsprintf(wptr->w_data, format, arg_ptr);
171 int wContentLength(void) {
172 struct webcontent *wptr;
175 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
176 len = len + strlen(wptr->w_data);
183 * wDumpContent() takes all the stuff that's been queued up using
184 * the wprintf() and escputs() functions, and sends it out to the browser.
185 * By queuing instead of transmitting as it's generated, we're able to
186 * calculate a Content-length: header.
188 * print_standard_html_footer should be set to 0 to transmit only, 1 to
189 * append the main menu (if in noframes mode) and closing tags, or 2 to
190 * append the closing tags only.
192 void wDumpContent(int print_standard_html_footer) {
193 struct webcontent *wptr;
195 if (print_standard_html_footer) {
196 if ( (noframes) && (print_standard_html_footer != 2) ) {
200 wprintf("</BODY></HTML>\n");
203 printf("Content-type: text/html\n");
204 printf("Content-length: %d\n", wContentLength());
207 while (wlist != NULL) {
208 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
217 void escputs1(char *strbuf, int nbsp)
221 for (a=0; a<strlen(strbuf); ++a) {
222 if (strbuf[a]=='<') wprintf("<");
223 else if (strbuf[a]=='>') wprintf(">");
224 else if (strbuf[a]=='&') wprintf("&");
225 else if (strbuf[a]==34) wprintf(""");
226 else if (strbuf[a]==LB) wprintf("<");
227 else if (strbuf[a]==RB) wprintf(">");
228 else if (strbuf[a]==QU) wprintf("\"");
229 else if ((strbuf[a]==32)&&(nbsp==1)) {
233 wprintf("%c", strbuf[a]);
238 void escputs(char *strbuf)
245 char *urlesc(char *strbuf)
248 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
249 static char outbuf[512];
253 for (a=0; a<strlen(strbuf); ++a) {
255 for (b=0; b<strlen(ec); ++b) {
256 if (strbuf[a]==ec[b]) c=1;
259 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
260 else sprintf(&outbuf[b],"%c",strbuf[a]);
265 void urlescputs(char *strbuf)
267 wprintf("%s",urlesc(strbuf));
271 char *getz(char *buf) {
273 if (fgets(buf, 256, stdin) == NULL) {
278 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
279 buf[strlen(buf)-1] = 0;
285 * Output all that important stuff that the browser will want to see
287 * If print_standard_html_head is nonzero, we also get some standard HTML
288 * headers. If it's set to 2, the session is considered to be closing.
290 void output_headers(int print_standard_html_head, char *target) {
292 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
295 printf("Server: %s\n", SERVER);
296 printf("Connection: close\n");
298 if ( (strlen(target)>0) && (noframes == 0) ) {
299 printf("Window-target: %s\n", target);
303 if (print_standard_html_head > 0) {
304 printf("Pragma: no-cache\n");
305 printf("Cache-Control: no-store\n");
308 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
309 wc_roomname, noframes);
310 if (print_standard_html_head==2) {
311 printf("X-WebCit-Session: close\n");
312 printf("Set-cookie: webcit=%s\n", unset);
315 printf("Set-cookie: webcit=%s\n", cookie);
318 if (print_standard_html_head > 0) {
319 wprintf("<HTML><HEAD><TITLE>");
320 escputs("WebCit"); /* FIX -- add BBS name here */
321 wprintf("</TITLE></HEAD>");
322 if (ExpressMessages != NULL) {
323 wprintf("<SCRIPT language=\"javascript\">\n");
324 wprintf("function ExpressMessage() {\n");
325 wprintf(" alert(\"");
326 escputs(ExpressMessages);
328 wprintf(" }\n </SCRIPT>\n");
331 if (ExpressMessages != NULL) {
332 wprintf("onload=\"ExpressMessage()\" ");
333 free(ExpressMessages);
334 ExpressMessages = NULL;
336 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
344 void check_for_express_messages() {
350 while (serv_gets(buf), strcmp(buf, "000")) {
351 if (ExpressMessages == NULL) {
352 ExpressMessages = malloc(strlen(buf) + 4);
353 strcpy(ExpressMessages, "");
356 ExpressMessages = realloc(ExpressMessages,
357 (strlen(ExpressMessages) + strlen(buf) + 4) );
359 strcat(ExpressMessages, buf);
360 strcat(ExpressMessages, "\\n");
368 void output_static(char *what) {
374 sprintf(buf, "static/%s", what);
375 fp = fopen(buf, "rb");
377 printf("HTTP/1.0 404 %s\n", strerror(errno));
378 output_headers(0, "");
379 printf("Content-Type: text/plain\n");
380 sprintf(buf, "%s: %s\n", what, strerror(errno));
381 printf("Content-length: %d\n", strlen(buf));
383 fwrite(buf, strlen(buf), 1, stdout);
386 printf("HTTP/1.0 200 OK\n");
387 output_headers(0, "");
389 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
390 printf("Content-type: image/gif\n");
391 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
392 printf("Content-type: image/jpeg\n");
393 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
394 printf("Content-type: text/html\n");
396 printf("Content-type: application/octet-stream\n");
398 fstat(fileno(fp), &statbuf);
399 bytes = statbuf.st_size;
400 printf("Content-length: %ld\n", (long)bytes);
403 putc(getc(fp), stdout);
410 void output_image() {
415 off_t accomplished = 0L;
418 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
421 bytes = extract_long(&buf[4], 0);
422 printf("HTTP/1.0 200 OK\n");
423 output_headers(0, "");
424 printf("Content-type: image/gif\n");
425 printf("Content-length: %ld\n", (long)bytes);
428 while (bytes > (off_t)0) {
429 thisblock = (off_t)sizeof(xferbuf);
430 if (thisblock > bytes) thisblock = bytes;
431 serv_printf("READ %ld|%ld", accomplished, thisblock);
433 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
434 serv_read(xferbuf, (int)thisblock);
435 fwrite(xferbuf, thisblock, 1, stdout);
436 bytes = bytes - thisblock;
437 accomplished = accomplished + thisblock;
444 printf("HTTP/1.0 404 %s\n", strerror(errno));
445 output_headers(0, "");
446 printf("Content-Type: text/plain\n");
447 sprintf(buf, "Error retrieving image\n");
448 printf("Content-length: %d\n", strlen(buf));
450 fwrite(buf, strlen(buf), 1, stdout);
457 * Convenience functions to display a page containing only a string
459 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext) {
460 printf("HTTP/1.0 200 OK\n");
461 output_headers(1, "bottom");
462 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
463 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
464 wprintf("<B>%s</B>\n", titlebarmsg);
465 wprintf("</FONT></TD></TR></TABLE><BR>\n");
466 escputs(messagetext);
476 void display_error(char *errormessage) {
477 convenience_page("770000", "Error", errormessage);
480 void display_success(char *successmessage) {
481 convenience_page("007700", "OK", successmessage);
486 void extract_action(char *actbuf, char *cmdbuf) {
489 strcpy(actbuf, cmdbuf);
490 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
491 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
492 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
494 for (i=0; i<strlen(actbuf); ++i) {
495 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
496 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
497 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
498 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
503 void upload_handler(char *name, char *filename, char *encoding,
504 void *content, char *cbtype, size_t length) {
506 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
507 fprintf(stderr, " name = %s\n", name);
508 fprintf(stderr, "filename = %s\n", filename);
509 fprintf(stderr, "encoding = %s\n", encoding);
510 fprintf(stderr, " type = %s\n", cbtype);
511 fprintf(stderr, " length = %d\n", length);
513 if (strlen(name)>0) {
514 upload = malloc(length);
515 if (upload != NULL) {
516 upload_length = length;
517 memcpy(upload, content, length);
524 void session_loop(char *browser_host) {
529 int ContentLength = 0;
530 char ContentType[512];
533 /* We stuff these with the values coming from the client cookies,
534 * so we can use them to reconnect a timed out session if we have to.
538 char c_username[256];
539 char c_password[256];
540 char c_roomname[256];
543 strcpy(c_host, defaulthost);
544 strcpy(c_port, defaultport);
545 strcpy(c_username, "");
546 strcpy(c_password, "");
547 strcpy(c_roomname, "");
552 if (getz(cmd)==NULL) return;
553 extract_action(action, cmd);
556 if (getz(buf)==NULL) return;
558 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
559 strcpy(cookie, &buf[15]);
560 cookie_to_stuff(cookie, NULL,
561 c_username, c_password, c_roomname,
565 if (!strncasecmp(buf, "Content-length: ", 16)) {
566 ContentLength = atoi(&buf[16]);
568 if (!strncasecmp(buf, "Content-type: ", 14)) {
569 strcpy(ContentType, &buf[14]);
571 } while(strlen(buf)>0);
575 if (ContentLength > 0) {
576 content = malloc(ContentLength+1);
577 fread(content, ContentLength, 1, stdin);
579 content[ContentLength] = 0;
581 if (!strncasecmp(ContentType,
582 "application/x-www-form-urlencoded", 33)) {
585 else if (!strncasecmp(ContentType, "multipart", 9)) {
586 mime_parser(content, ContentLength, ContentType,
594 /* If there are variables in the URL, we must grab them now */
595 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
596 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
602 * If we're not connected to a Citadel server, try to hook up the
603 * connection now. Preference is given to the host and port specified
604 * by browser cookies, if cookies have been supplied.
607 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
608 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
609 serv_sock = connectsock(c_host, c_port, "tcp");
611 serv_gets(buf); /* get the server welcome message */
612 get_serv_info(browser_host);
615 check_for_express_messages();
618 * If we're not logged in, but we have username and password cookies
619 * supplied by the browser, try using them to log in.
621 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
622 serv_printf("USER %s", c_username);
625 serv_printf("PASS %s", c_password);
628 become_logged_in(c_username, c_password, buf);
634 * If we don't have a current room, but a cookie specifying the
635 * current room is supplied, make an effort to go there.
637 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
638 serv_printf("GOTO %s", c_roomname);
641 strcpy(wc_roomname, c_roomname);
645 if (!strcasecmp(action, "static")) {
646 strcpy(buf, &cmd[12]);
647 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
651 else if (!strcasecmp(action, "image")) {
655 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
659 else if (!logged_in) {
663 /* Various commands... */
665 else if (!strcasecmp(action, "do_welcome")) {
669 else if (!strcasecmp(action, "display_main_menu")) {
673 else if (!strcasecmp(action, "advanced")) {
674 display_advanced_menu();
677 else if (!strcasecmp(action, "whobbs")) {
681 else if (!strcasecmp(action, "knrooms")) {
682 list_all_rooms_by_floor();
685 else if (!strcasecmp(action, "gotonext")) {
690 else if (!strcasecmp(action, "skip")) {
694 else if (!strcasecmp(action, "ungoto")) {
698 else if (!strcasecmp(action, "dotgoto")) {
700 gotoroom(bstr("room"), 1);
703 else if (!strcasecmp(action, "termquit")) {
707 else if (!strcasecmp(action, "readnew")) {
711 else if (!strcasecmp(action, "readold")) {
715 else if (!strcasecmp(action, "readfwd")) {
719 else if (!strcasecmp(action, "display_enter")) {
723 else if (!strcasecmp(action, "post")) {
727 else if (!strcasecmp(action, "confirm_delete_msg")) {
728 confirm_delete_msg();
731 else if (!strcasecmp(action, "delete_msg")) {
735 else if (!strcasecmp(action, "confirm_move_msg")) {
739 else if (!strcasecmp(action, "move_msg")) {
743 else if (!strcasecmp(action, "userlist")) {
747 else if (!strcasecmp(action, "showuser")) {
751 else if (!strcasecmp(action, "display_page")) {
755 else if (!strcasecmp(action, "page_user")) {
759 else if (!strcasecmp(action, "chat")) {
763 else if (!strcasecmp(action, "display_private")) {
764 display_private("", 0);
767 else if (!strcasecmp(action, "goto_private")) {
771 else if (!strcasecmp(action, "zapped_list")) {
775 else if (!strcasecmp(action, "display_zap")) {
779 else if (!strcasecmp(action, "zap")) {
783 else if (!strcasecmp(action, "display_entroom")) {
787 else if (!strcasecmp(action, "entroom")) {
791 else if (!strcasecmp(action, "display_editroom")) {
795 else if (!strcasecmp(action, "editroom")) {
799 else if (!strcasecmp(action, "display_editinfo")) {
800 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
803 else if (!strcasecmp(action, "editinfo")) {
804 save_edit("Room info", "EINF 1", 1);
807 else if (!strcasecmp(action, "display_editbio")) {
808 sprintf(buf, "RBIO %s", wc_username);
809 display_edit("Your bio", "NOOP", buf, "editbio");
812 else if (!strcasecmp(action, "editbio")) {
813 save_edit("Your bio", "EBIO", 0);
816 else if (!strcasecmp(action, "confirm_delete_room")) {
817 confirm_delete_room();
820 else if (!strcasecmp(action, "delete_room")) {
824 else if (!strcasecmp(action, "validate")) {
828 else if (!strcasecmp(action, "display_editpic")) {
829 display_graphics_upload("your photo",
834 else if (!strcasecmp(action, "editpic")) {
835 do_graphics_upload("UIMG 1|_userpic_");
838 else if (!strcasecmp(action, "display_editroompic")) {
839 display_graphics_upload("the graphic for this room",
844 else if (!strcasecmp(action, "editroompic")) {
845 do_graphics_upload("UIMG 1|_roompic_");
848 else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
849 select_floor_to_edit_pic();
852 else if (!strcasecmp(action, "display_editfloorpic")) {
853 sprintf(buf, "UIMG 0|_floorpic_|%s",
854 bstr("which_floor"));
855 display_graphics_upload("the graphic for this floor",
860 else if (!strcasecmp(action, "editfloorpic")) {
861 sprintf(buf, "UIMG 1|_floorpic_|%s",
862 bstr("which_floor"));
863 do_graphics_upload(buf);
866 else if (!strcasecmp(action, "display_reg")) {
870 else if (!strcasecmp(action, "register")) {
874 else if (!strcasecmp(action, "display_changepw")) {
878 else if (!strcasecmp(action, "changepw")) {
882 else if (!strcasecmp(action, "display_edit_node")) {
886 else if (!strcasecmp(action, "display_netconf")) {
890 else if (!strcasecmp(action, "display_confirm_unshare")) {
891 display_confirm_unshare();
894 else if (!strcasecmp(action, "display_confirm_delete_node")) {
895 display_confirm_delete_node();
898 else if (!strcasecmp(action, "delete_node")) {
902 else if (!strcasecmp(action, "unshare")) {
906 else if (!strcasecmp(action, "display_add_node")) {
910 else if (!strcasecmp(action, "add_node")) {
914 else if (!strcasecmp(action, "display_share")) {
918 else if (!strcasecmp(action, "share")) {
922 else if (!strcasecmp(action, "terminate_session")) {
926 else if (!strcasecmp(action, "edit_me")) {
930 else if (!strcasecmp(action, "display_siteconfig")) {
931 display_siteconfig();
934 else if (!strcasecmp(action, "siteconfig")) {
938 else if (!strcasecmp(action, "display_generic")) {
942 else if (!strcasecmp(action, "do_generic")) {
946 /* When all else fails... */
948 printf("HTTP/1.0 200 OK\n");
949 output_headers(1, "");
951 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
952 wprintf("You're in session %d<HR>\n", wc_session);
953 wprintf("Command: <BR><PRE>\n");
955 wprintf("</PRE><HR>\n");
956 wprintf("Variables: <BR><PRE>\n");
958 wprintf("</PRE><HR>\n");
963 if (content != NULL) {
968 if (upload_length > 0) {
974 int main(int argc, char *argv[]) {
978 "webcit: usage error (argc must be 6, not %d)\n",
983 wc_session = atoi(argv[1]);
984 defaulthost = argv[2];
985 defaultport = argv[3];
987 strcpy(wc_username, "");
988 strcpy(wc_password, "");
989 strcpy(wc_roomname, "");
990 strcpy(browser, argv[5]);
993 session_loop(argv[4]);