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];
29 int TransactionCount = 0;
33 char *ExpressMessages = NULL;
36 struct webcontent *wlist = NULL;
37 struct webcontent *wlast = NULL;
39 struct urlcontent *urlstrings = NULL;
41 static const char *defaulthost = DEFAULT_HOST;
42 static const char *defaultport = DEFAULT_PORT;
44 int upload_length = 0;
48 void unescape_input(char *buf)
53 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
54 buf[strlen(buf)-1] = 0;
56 for (a=0; a<strlen(buf); ++a) {
57 if (buf[a]=='+') buf[a]=' ';
62 sscanf(hex,"%02x",&b);
64 strcpy(&buf[a+1],&buf[a+3]);
71 void addurls(char *url) {
78 while (strlen(up)>0) {
80 /* locate the = sign */
83 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
87 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
90 strcpy(u->url_key, buf);
92 /* now chop that part off */
93 for (a=0; a<=b; ++a) ++up;
95 /* locate the & sign */
98 for (a=0; a<strlen(up); ++a) {
99 if (!strncmp(ptr,"&",1)) {
106 for (a=0; a<b; ++a) ++ptr;
109 u->url_data = malloc(strlen(up)+1);
110 strcpy(u->url_data, up);
112 unescape_input(u->url_data);
118 void free_urls(void) {
119 struct urlcontent *u;
121 while (urlstrings != NULL) {
122 free(urlstrings->url_data);
123 u = urlstrings->next;
130 * Diagnostic function to display the contents of all variables
132 void dump_vars(void) {
133 struct urlcontent *u;
135 for (u = urlstrings; u != NULL; u = u->next) {
136 wprintf("%38s = %s\n", u->url_key, u->url_data);
140 char *bstr(char *key) {
141 struct urlcontent *u;
143 for (u = urlstrings; u != NULL; u = u->next) {
144 if (!strcasecmp(u->url_key, key)) return(u->url_data);
150 void wprintf(const char *format, ...) {
152 struct webcontent *wptr;
154 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
165 va_start(arg_ptr, format);
166 vsprintf(wptr->w_data, format, arg_ptr);
170 int wContentLength(void) {
171 struct webcontent *wptr;
174 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
175 len = len + strlen(wptr->w_data);
181 void wDumpContent(int print_standard_html_footer) {
182 struct webcontent *wptr;
184 if (print_standard_html_footer) {
189 wprintf("</BODY></HTML>\n");
192 printf("Content-type: text/html\n");
193 printf("Content-length: %d\n", wContentLength());
196 while (wlist != NULL) {
197 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
206 void escputs1(char *strbuf, int nbsp)
210 for (a=0; a<strlen(strbuf); ++a) {
211 if (strbuf[a]=='<') wprintf("<");
212 else if (strbuf[a]=='>') wprintf(">");
213 else if (strbuf[a]=='&') wprintf("&");
214 else if (strbuf[a]==34) wprintf(""");
215 else if (strbuf[a]==LB) wprintf("<");
216 else if (strbuf[a]==RB) wprintf(">");
217 else if (strbuf[a]==QU) wprintf("\"");
218 else if ((strbuf[a]==32)&&(nbsp==1)) {
222 wprintf("%c", strbuf[a]);
227 void escputs(char *strbuf)
234 char *urlesc(char *strbuf)
237 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
238 static char outbuf[512];
242 for (a=0; a<strlen(strbuf); ++a) {
244 for (b=0; b<strlen(ec); ++b) {
245 if (strbuf[a]==ec[b]) c=1;
248 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
249 else sprintf(&outbuf[b],"%c",strbuf[a]);
254 void urlescputs(char *strbuf)
256 wprintf("%s",urlesc(strbuf));
260 void getz(char *buf) {
262 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
264 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
265 buf[strlen(buf)-1] = 0;
270 * Output all that important stuff that the browser will want to see
272 * If print_standard_html_head is nonzero, we also get some standard HTML
273 * headers. If it's set to 2, the session is considered to be closing.
275 void output_headers(int print_standard_html_head, char *target) {
277 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
280 printf("Server: %s\n", SERVER);
281 printf("Connection: close\n");
283 if ( (strlen(target)>0) && (noframes == 0) ) {
284 printf("Window-target: %s\n", target);
288 if (print_standard_html_head > 0) {
289 printf("Pragma: no-cache\n");
290 printf("Cache-Control: no-store\n");
293 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
294 wc_roomname, noframes);
295 if (print_standard_html_head==2) {
296 printf("X-WebCit-Session: close\n");
297 printf("Set-cookie: webcit=%s\n", unset);
300 printf("Set-cookie: webcit=%s\n", cookie);
303 if (print_standard_html_head > 0) {
304 wprintf("<HTML><HEAD><TITLE>");
305 escputs("WebCit"); /* FIX -- add BBS name here */
306 wprintf("</TITLE></HEAD>");
307 if (ExpressMessages != NULL) {
308 wprintf("<SCRIPT language=\"javascript\">\n");
309 wprintf("function ExpressMessage() {\n");
310 wprintf(" alert(\"");
311 escputs(ExpressMessages);
313 wprintf(" }\n </SCRIPT>\n");
316 if (ExpressMessages != NULL) {
317 wprintf("onload=\"ExpressMessage()\" ");
318 free(ExpressMessages);
319 ExpressMessages = NULL;
321 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
329 void check_for_express_messages() {
335 while (serv_gets(buf), strcmp(buf, "000")) {
336 if (ExpressMessages == NULL) {
337 ExpressMessages = malloc(strlen(buf) + 4);
338 strcpy(ExpressMessages, "");
341 ExpressMessages = realloc(ExpressMessages,
342 (strlen(ExpressMessages) + strlen(buf) + 4) );
344 strcat(ExpressMessages, buf);
345 strcat(ExpressMessages, "\\n");
353 void output_static(char *what) {
359 sprintf(buf, "static/%s", what);
360 fp = fopen(buf, "rb");
362 printf("HTTP/1.0 404 %s\n", strerror(errno));
363 output_headers(0, "");
364 printf("Content-Type: text/plain\n");
365 sprintf(buf, "%s: %s\n", what, strerror(errno));
366 printf("Content-length: %d\n", strlen(buf));
368 fwrite(buf, strlen(buf), 1, stdout);
371 printf("HTTP/1.0 200 OK\n");
372 output_headers(0, "");
374 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
375 printf("Content-type: image/gif\n");
376 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
377 printf("Content-type: image/jpeg\n");
378 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
379 printf("Content-type: text/html\n");
381 printf("Content-type: application/octet-stream\n");
383 fstat(fileno(fp), &statbuf);
384 bytes = statbuf.st_size;
385 printf("Content-length: %ld\n", (long)bytes);
388 putc(getc(fp), stdout);
395 void output_image() {
400 off_t accomplished = 0L;
403 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
406 bytes = extract_long(&buf[4], 0);
407 printf("HTTP/1.0 200 OK\n");
408 output_headers(0, "");
409 printf("Content-type: image/gif\n");
410 printf("Content-length: %ld\n", bytes);
413 while (bytes > (off_t)0) {
414 thisblock = (off_t)sizeof(xferbuf);
415 if (thisblock > bytes) thisblock = bytes;
416 serv_printf("READ %ld|%ld", accomplished, thisblock);
418 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
419 serv_read(xferbuf, (int)thisblock);
420 fwrite(xferbuf, thisblock, 1, stdout);
421 bytes = bytes - thisblock;
422 accomplished = accomplished + thisblock;
429 printf("HTTP/1.0 404 %s\n", strerror(errno));
430 output_headers(0, "");
431 printf("Content-Type: text/plain\n");
432 sprintf(buf, "Error retrieving image\n");
433 printf("Content-length: %d\n", strlen(buf));
435 fwrite(buf, strlen(buf), 1, stdout);
442 * Convenience functions to display a page containing only a string
444 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext) {
445 printf("HTTP/1.0 200 OK\n");
446 output_headers(1, "bottom");
447 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
448 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
449 wprintf("<B>%s</B>\n", titlebarmsg);
450 wprintf("</FONT></TD></TR></TABLE><BR>\n");
451 escputs(messagetext);
461 void display_error(char *errormessage) {
462 convenience_page("770000", "Error", errormessage);
465 void display_success(char *successmessage) {
466 convenience_page("007700", "OK", successmessage);
471 void extract_action(char *actbuf, char *cmdbuf) {
474 strcpy(actbuf, cmdbuf);
475 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
476 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
477 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
479 for (i=0; i<strlen(actbuf); ++i) {
480 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
481 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
482 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
483 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
488 void upload_handler(char *name, char *filename, char *encoding,
489 void *content, char *cbtype, size_t length) {
491 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
492 fprintf(stderr, " name = %s\n", name);
493 fprintf(stderr, "filename = %s\n", filename);
494 fprintf(stderr, "encoding = %s\n", encoding);
495 fprintf(stderr, " type = %s\n", cbtype);
496 fprintf(stderr, " length = %d\n", length);
498 if (strlen(name)>0) {
499 upload = malloc(length);
500 if (upload != NULL) {
501 upload_length = length;
502 memcpy(upload, content, length);
509 void session_loop(void) {
514 int ContentLength = 0;
515 char ContentType[512];
518 /* We stuff these with the values coming from the client cookies,
519 * so we can use them to reconnect a timed out session if we have to.
523 char c_username[256];
524 char c_password[256];
525 char c_roomname[256];
528 strcpy(c_host, defaulthost);
529 strcpy(c_port, defaultport);
530 strcpy(c_username, "");
531 strcpy(c_password, "");
532 strcpy(c_roomname, "");
538 extract_action(action, cmd);
543 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
544 strcpy(cookie, &buf[15]);
545 cookie_to_stuff(cookie, NULL,
546 c_username, c_password, c_roomname,
550 if (!strncasecmp(buf, "Content-length: ", 16)) {
551 ContentLength = atoi(&buf[16]);
553 if (!strncasecmp(buf, "Content-type: ", 14)) {
554 strcpy(ContentType, &buf[14]);
556 } while(strlen(buf)>0);
560 if (ContentLength > 0) {
561 content = malloc(ContentLength+1);
562 fread(content, ContentLength, 1, stdin);
564 content[ContentLength] = 0;
566 if (!strncasecmp(ContentType,
567 "application/x-www-form-urlencoded", 33)) {
570 else if (!strncasecmp(ContentType, "multipart", 9)) {
571 mime_parser(content, ContentLength, ContentType,
579 /* If there are variables in the URL, we must grab them now */
580 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
581 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
587 * If we're not connected to a Citadel server, try to hook up the
588 * connection now. Preference is given to the host and port specified
589 * by browser cookies, if cookies have been supplied.
592 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
593 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
594 serv_sock = connectsock(c_host, c_port, "tcp");
596 serv_gets(buf); /* get the server welcome message */
600 check_for_express_messages();
603 * If we're not logged in, but we have username and password cookies
604 * supplied by the browser, try using them to log in.
606 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
607 serv_printf("USER %s", c_username);
610 serv_printf("PASS %s", c_password);
613 become_logged_in(c_username, c_password, buf);
619 * If we don't have a current room, but a cookie specifying the
620 * current room is supplied, make an effort to go there.
622 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
623 serv_printf("GOTO %s", c_roomname);
626 strcpy(wc_roomname, c_roomname);
630 if (!strcasecmp(action, "static")) {
631 strcpy(buf, &cmd[12]);
632 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
636 else if (!strcasecmp(action, "image")) {
640 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
644 else if (!logged_in) {
648 /* Various commands... */
650 else if (!strcasecmp(action, "do_welcome")) {
654 else if (!strcasecmp(action, "display_main_menu")) {
658 else if (!strcasecmp(action, "advanced")) {
659 display_advanced_menu();
662 else if (!strcasecmp(action, "whobbs")) {
666 else if (!strcasecmp(action, "knrooms")) {
667 list_all_rooms_by_floor();
670 else if (!strcasecmp(action, "gotonext")) {
675 else if (!strcasecmp(action, "skip")) {
679 else if (!strcasecmp(action, "ungoto")) {
683 else if (!strcasecmp(action, "dotgoto")) {
685 gotoroom(bstr("room"), 1);
688 else if (!strcasecmp(action, "termquit")) {
692 else if (!strcasecmp(action, "readnew")) {
696 else if (!strcasecmp(action, "readold")) {
700 else if (!strcasecmp(action, "readfwd")) {
704 else if (!strcasecmp(action, "display_enter")) {
708 else if (!strcasecmp(action, "post")) {
712 else if (!strcasecmp(action, "confirm_delete_msg")) {
713 confirm_delete_msg();
716 else if (!strcasecmp(action, "delete_msg")) {
720 else if (!strcasecmp(action, "confirm_move_msg")) {
724 else if (!strcasecmp(action, "move_msg")) {
728 else if (!strcasecmp(action, "userlist")) {
732 else if (!strcasecmp(action, "showuser")) {
736 else if (!strcasecmp(action, "display_page")) {
740 else if (!strcasecmp(action, "page_user")) {
744 else if (!strcasecmp(action, "chat")) {
748 else if (!strcasecmp(action, "display_private")) {
749 display_private("", 0);
752 else if (!strcasecmp(action, "goto_private")) {
756 else if (!strcasecmp(action, "zapped_list")) {
760 else if (!strcasecmp(action, "display_zap")) {
764 else if (!strcasecmp(action, "zap")) {
768 else if (!strcasecmp(action, "display_entroom")) {
772 else if (!strcasecmp(action, "entroom")) {
776 else if (!strcasecmp(action, "display_editroom")) {
780 else if (!strcasecmp(action, "editroom")) {
784 else if (!strcasecmp(action, "display_editinfo")) {
785 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
788 else if (!strcasecmp(action, "editinfo")) {
789 save_edit("Room info", "EINF 1", 1);
792 else if (!strcasecmp(action, "display_editbio")) {
793 sprintf(buf, "RBIO %s", wc_username);
794 display_edit("Your bio", "NOOP", buf, "editbio");
797 else if (!strcasecmp(action, "editbio")) {
798 save_edit("Your bio", "EBIO", 0);
801 else if (!strcasecmp(action, "confirm_delete_room")) {
802 confirm_delete_room();
805 else if (!strcasecmp(action, "delete_room")) {
809 else if (!strcasecmp(action, "validate")) {
813 else if (!strcasecmp(action, "display_editpic")) {
814 display_graphics_upload("your photo",
819 else if (!strcasecmp(action, "editpic")) {
820 do_graphics_upload("UIMG 1|_userpic_");
823 else if (!strcasecmp(action, "display_editroompic")) {
824 display_graphics_upload("the graphic for this room",
829 else if (!strcasecmp(action, "editroompic")) {
830 do_graphics_upload("UIMG 1|_roompic_");
833 else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
834 select_floor_to_edit_pic();
837 else if (!strcasecmp(action, "display_editfloorpic")) {
838 sprintf(buf, "UIMG 0|_floorpic_|%s",
839 bstr("which_floor"));
840 display_graphics_upload("the graphic for this floor",
845 else if (!strcasecmp(action, "editfloorpic")) {
846 sprintf(buf, "UIMG 1|_floorpic_|%s",
847 bstr("which_floor"));
848 do_graphics_upload(buf);
851 else if (!strcasecmp(action, "display_reg")) {
855 else if (!strcasecmp(action, "register")) {
859 else if (!strcasecmp(action, "display_changepw")) {
863 else if (!strcasecmp(action, "changepw")) {
867 else if (!strcasecmp(action, "display_edit_node")) {
871 else if (!strcasecmp(action, "display_netconf")) {
875 else if (!strcasecmp(action, "display_confirm_unshare")) {
876 display_confirm_unshare();
879 else if (!strcasecmp(action, "display_confirm_delete_node")) {
880 display_confirm_delete_node();
883 else if (!strcasecmp(action, "delete_node")) {
887 else if (!strcasecmp(action, "unshare")) {
891 else if (!strcasecmp(action, "display_add_node")) {
895 else if (!strcasecmp(action, "add_node")) {
899 else if (!strcasecmp(action, "display_share")) {
903 else if (!strcasecmp(action, "share")) {
907 else if (!strcasecmp(action, "terminate_session")) {
911 else if (!strcasecmp(action, "edit_me")) {
915 else if (!strcasecmp(action, "display_siteconfig")) {
916 display_siteconfig();
919 else if (!strcasecmp(action, "siteconfig")) {
923 else if (!strcasecmp(action, "display_generic")) {
927 else if (!strcasecmp(action, "do_generic")) {
931 /* When all else fails... */
933 printf("HTTP/1.0 200 OK\n");
934 output_headers(1, "");
936 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
937 wprintf("You're in session %d<HR>\n", wc_session);
938 wprintf("Command: <BR><PRE>\n");
940 wprintf("</PRE><HR>\n");
941 wprintf("Variables: <BR><PRE>\n");
943 wprintf("</PRE><HR>\n");
948 if (content != NULL) {
953 if (upload_length > 0) {
959 int main(int argc, char *argv[]) {
961 if (argc < 2 || argc > 4) {
963 "webcit: usage: webcit <session_id> [host [port]]\n");
967 wc_session = atoi(argv[1]);
970 defaulthost = argv[2];
972 defaultport = argv[3];
975 strcpy(wc_username, "");
976 strcpy(wc_password, "");
977 strcpy(wc_roomname, "");