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);
182 * wDumpContent() takes all the stuff that's been queued up using
183 * the wprintf() and escputs() functions, and sends it out to the browser.
184 * By queuing instead of transmitting as it's generated, we're able to
185 * calculate a Content-length: header.
187 * print_standard_html_footer should be set to 0 to transmit only, 1 to
188 * append the main menu (if in noframes mode) and closing tags, or 2 to
189 * append the closing tags only.
191 void wDumpContent(int print_standard_html_footer) {
192 struct webcontent *wptr;
194 if (print_standard_html_footer) {
195 if ( (noframes) && (print_standard_html_footer != 2) ) {
199 wprintf("</BODY></HTML>\n");
202 printf("Content-type: text/html\n");
203 printf("Content-length: %d\n", wContentLength());
206 while (wlist != NULL) {
207 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
216 void escputs1(char *strbuf, int nbsp)
220 for (a=0; a<strlen(strbuf); ++a) {
221 if (strbuf[a]=='<') wprintf("<");
222 else if (strbuf[a]=='>') wprintf(">");
223 else if (strbuf[a]=='&') wprintf("&");
224 else if (strbuf[a]==34) wprintf(""");
225 else if (strbuf[a]==LB) wprintf("<");
226 else if (strbuf[a]==RB) wprintf(">");
227 else if (strbuf[a]==QU) wprintf("\"");
228 else if ((strbuf[a]==32)&&(nbsp==1)) {
232 wprintf("%c", strbuf[a]);
237 void escputs(char *strbuf)
244 char *urlesc(char *strbuf)
247 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
248 static char outbuf[512];
252 for (a=0; a<strlen(strbuf); ++a) {
254 for (b=0; b<strlen(ec); ++b) {
255 if (strbuf[a]==ec[b]) c=1;
258 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
259 else sprintf(&outbuf[b],"%c",strbuf[a]);
264 void urlescputs(char *strbuf)
266 wprintf("%s",urlesc(strbuf));
270 void getz(char *buf) {
272 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
274 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
275 buf[strlen(buf)-1] = 0;
280 * Output all that important stuff that the browser will want to see
282 * If print_standard_html_head is nonzero, we also get some standard HTML
283 * headers. If it's set to 2, the session is considered to be closing.
285 void output_headers(int print_standard_html_head, char *target) {
287 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
290 printf("Server: %s\n", SERVER);
291 printf("Connection: close\n");
293 if ( (strlen(target)>0) && (noframes == 0) ) {
294 printf("Window-target: %s\n", target);
298 if (print_standard_html_head > 0) {
299 printf("Pragma: no-cache\n");
300 printf("Cache-Control: no-store\n");
303 stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
304 wc_roomname, noframes);
305 if (print_standard_html_head==2) {
306 printf("X-WebCit-Session: close\n");
307 printf("Set-cookie: webcit=%s\n", unset);
310 printf("Set-cookie: webcit=%s\n", cookie);
313 if (print_standard_html_head > 0) {
314 wprintf("<HTML><HEAD><TITLE>");
315 escputs("WebCit"); /* FIX -- add BBS name here */
316 wprintf("</TITLE></HEAD>");
317 if (ExpressMessages != NULL) {
318 wprintf("<SCRIPT language=\"javascript\">\n");
319 wprintf("function ExpressMessage() {\n");
320 wprintf(" alert(\"");
321 escputs(ExpressMessages);
323 wprintf(" }\n </SCRIPT>\n");
326 if (ExpressMessages != NULL) {
327 wprintf("onload=\"ExpressMessage()\" ");
328 free(ExpressMessages);
329 ExpressMessages = NULL;
331 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
339 void check_for_express_messages() {
345 while (serv_gets(buf), strcmp(buf, "000")) {
346 if (ExpressMessages == NULL) {
347 ExpressMessages = malloc(strlen(buf) + 4);
348 strcpy(ExpressMessages, "");
351 ExpressMessages = realloc(ExpressMessages,
352 (strlen(ExpressMessages) + strlen(buf) + 4) );
354 strcat(ExpressMessages, buf);
355 strcat(ExpressMessages, "\\n");
363 void output_static(char *what) {
369 sprintf(buf, "static/%s", what);
370 fp = fopen(buf, "rb");
372 printf("HTTP/1.0 404 %s\n", strerror(errno));
373 output_headers(0, "");
374 printf("Content-Type: text/plain\n");
375 sprintf(buf, "%s: %s\n", what, strerror(errno));
376 printf("Content-length: %d\n", strlen(buf));
378 fwrite(buf, strlen(buf), 1, stdout);
381 printf("HTTP/1.0 200 OK\n");
382 output_headers(0, "");
384 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
385 printf("Content-type: image/gif\n");
386 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
387 printf("Content-type: image/jpeg\n");
388 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
389 printf("Content-type: text/html\n");
391 printf("Content-type: application/octet-stream\n");
393 fstat(fileno(fp), &statbuf);
394 bytes = statbuf.st_size;
395 printf("Content-length: %ld\n", (long)bytes);
398 putc(getc(fp), stdout);
405 void output_image() {
410 off_t accomplished = 0L;
413 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
416 bytes = extract_long(&buf[4], 0);
417 printf("HTTP/1.0 200 OK\n");
418 output_headers(0, "");
419 printf("Content-type: image/gif\n");
420 printf("Content-length: %ld\n", bytes);
423 while (bytes > (off_t)0) {
424 thisblock = (off_t)sizeof(xferbuf);
425 if (thisblock > bytes) thisblock = bytes;
426 serv_printf("READ %ld|%ld", accomplished, thisblock);
428 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
429 serv_read(xferbuf, (int)thisblock);
430 fwrite(xferbuf, thisblock, 1, stdout);
431 bytes = bytes - thisblock;
432 accomplished = accomplished + thisblock;
439 printf("HTTP/1.0 404 %s\n", strerror(errno));
440 output_headers(0, "");
441 printf("Content-Type: text/plain\n");
442 sprintf(buf, "Error retrieving image\n");
443 printf("Content-length: %d\n", strlen(buf));
445 fwrite(buf, strlen(buf), 1, stdout);
452 * Convenience functions to display a page containing only a string
454 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext) {
455 printf("HTTP/1.0 200 OK\n");
456 output_headers(1, "bottom");
457 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
458 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
459 wprintf("<B>%s</B>\n", titlebarmsg);
460 wprintf("</FONT></TD></TR></TABLE><BR>\n");
461 escputs(messagetext);
471 void display_error(char *errormessage) {
472 convenience_page("770000", "Error", errormessage);
475 void display_success(char *successmessage) {
476 convenience_page("007700", "OK", successmessage);
481 void extract_action(char *actbuf, char *cmdbuf) {
484 strcpy(actbuf, cmdbuf);
485 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
486 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
487 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
489 for (i=0; i<strlen(actbuf); ++i) {
490 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
491 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
492 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
493 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
498 void upload_handler(char *name, char *filename, char *encoding,
499 void *content, char *cbtype, size_t length) {
501 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
502 fprintf(stderr, " name = %s\n", name);
503 fprintf(stderr, "filename = %s\n", filename);
504 fprintf(stderr, "encoding = %s\n", encoding);
505 fprintf(stderr, " type = %s\n", cbtype);
506 fprintf(stderr, " length = %d\n", length);
508 if (strlen(name)>0) {
509 upload = malloc(length);
510 if (upload != NULL) {
511 upload_length = length;
512 memcpy(upload, content, length);
519 void session_loop(void) {
524 int ContentLength = 0;
525 char ContentType[512];
528 /* We stuff these with the values coming from the client cookies,
529 * so we can use them to reconnect a timed out session if we have to.
533 char c_username[256];
534 char c_password[256];
535 char c_roomname[256];
538 strcpy(c_host, defaulthost);
539 strcpy(c_port, defaultport);
540 strcpy(c_username, "");
541 strcpy(c_password, "");
542 strcpy(c_roomname, "");
548 extract_action(action, cmd);
553 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
554 strcpy(cookie, &buf[15]);
555 cookie_to_stuff(cookie, NULL,
556 c_username, c_password, c_roomname,
560 if (!strncasecmp(buf, "Content-length: ", 16)) {
561 ContentLength = atoi(&buf[16]);
563 if (!strncasecmp(buf, "Content-type: ", 14)) {
564 strcpy(ContentType, &buf[14]);
566 } while(strlen(buf)>0);
570 if (ContentLength > 0) {
571 content = malloc(ContentLength+1);
572 fread(content, ContentLength, 1, stdin);
574 content[ContentLength] = 0;
576 if (!strncasecmp(ContentType,
577 "application/x-www-form-urlencoded", 33)) {
580 else if (!strncasecmp(ContentType, "multipart", 9)) {
581 mime_parser(content, ContentLength, ContentType,
589 /* If there are variables in the URL, we must grab them now */
590 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
591 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
597 * If we're not connected to a Citadel server, try to hook up the
598 * connection now. Preference is given to the host and port specified
599 * by browser cookies, if cookies have been supplied.
602 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
603 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
604 serv_sock = connectsock(c_host, c_port, "tcp");
606 serv_gets(buf); /* get the server welcome message */
610 check_for_express_messages();
613 * If we're not logged in, but we have username and password cookies
614 * supplied by the browser, try using them to log in.
616 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
617 serv_printf("USER %s", c_username);
620 serv_printf("PASS %s", c_password);
623 become_logged_in(c_username, c_password, buf);
629 * If we don't have a current room, but a cookie specifying the
630 * current room is supplied, make an effort to go there.
632 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
633 serv_printf("GOTO %s", c_roomname);
636 strcpy(wc_roomname, c_roomname);
640 if (!strcasecmp(action, "static")) {
641 strcpy(buf, &cmd[12]);
642 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
646 else if (!strcasecmp(action, "image")) {
650 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
654 else if (!logged_in) {
658 /* Various commands... */
660 else if (!strcasecmp(action, "do_welcome")) {
664 else if (!strcasecmp(action, "display_main_menu")) {
668 else if (!strcasecmp(action, "advanced")) {
669 display_advanced_menu();
672 else if (!strcasecmp(action, "whobbs")) {
676 else if (!strcasecmp(action, "knrooms")) {
677 list_all_rooms_by_floor();
680 else if (!strcasecmp(action, "gotonext")) {
685 else if (!strcasecmp(action, "skip")) {
689 else if (!strcasecmp(action, "ungoto")) {
693 else if (!strcasecmp(action, "dotgoto")) {
695 gotoroom(bstr("room"), 1);
698 else if (!strcasecmp(action, "termquit")) {
702 else if (!strcasecmp(action, "readnew")) {
706 else if (!strcasecmp(action, "readold")) {
710 else if (!strcasecmp(action, "readfwd")) {
714 else if (!strcasecmp(action, "display_enter")) {
718 else if (!strcasecmp(action, "post")) {
722 else if (!strcasecmp(action, "confirm_delete_msg")) {
723 confirm_delete_msg();
726 else if (!strcasecmp(action, "delete_msg")) {
730 else if (!strcasecmp(action, "confirm_move_msg")) {
734 else if (!strcasecmp(action, "move_msg")) {
738 else if (!strcasecmp(action, "userlist")) {
742 else if (!strcasecmp(action, "showuser")) {
746 else if (!strcasecmp(action, "display_page")) {
750 else if (!strcasecmp(action, "page_user")) {
754 else if (!strcasecmp(action, "chat")) {
758 else if (!strcasecmp(action, "display_private")) {
759 display_private("", 0);
762 else if (!strcasecmp(action, "goto_private")) {
766 else if (!strcasecmp(action, "zapped_list")) {
770 else if (!strcasecmp(action, "display_zap")) {
774 else if (!strcasecmp(action, "zap")) {
778 else if (!strcasecmp(action, "display_entroom")) {
782 else if (!strcasecmp(action, "entroom")) {
786 else if (!strcasecmp(action, "display_editroom")) {
790 else if (!strcasecmp(action, "editroom")) {
794 else if (!strcasecmp(action, "display_editinfo")) {
795 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
798 else if (!strcasecmp(action, "editinfo")) {
799 save_edit("Room info", "EINF 1", 1);
802 else if (!strcasecmp(action, "display_editbio")) {
803 sprintf(buf, "RBIO %s", wc_username);
804 display_edit("Your bio", "NOOP", buf, "editbio");
807 else if (!strcasecmp(action, "editbio")) {
808 save_edit("Your bio", "EBIO", 0);
811 else if (!strcasecmp(action, "confirm_delete_room")) {
812 confirm_delete_room();
815 else if (!strcasecmp(action, "delete_room")) {
819 else if (!strcasecmp(action, "validate")) {
823 else if (!strcasecmp(action, "display_editpic")) {
824 display_graphics_upload("your photo",
829 else if (!strcasecmp(action, "editpic")) {
830 do_graphics_upload("UIMG 1|_userpic_");
833 else if (!strcasecmp(action, "display_editroompic")) {
834 display_graphics_upload("the graphic for this room",
839 else if (!strcasecmp(action, "editroompic")) {
840 do_graphics_upload("UIMG 1|_roompic_");
843 else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
844 select_floor_to_edit_pic();
847 else if (!strcasecmp(action, "display_editfloorpic")) {
848 sprintf(buf, "UIMG 0|_floorpic_|%s",
849 bstr("which_floor"));
850 display_graphics_upload("the graphic for this floor",
855 else if (!strcasecmp(action, "editfloorpic")) {
856 sprintf(buf, "UIMG 1|_floorpic_|%s",
857 bstr("which_floor"));
858 do_graphics_upload(buf);
861 else if (!strcasecmp(action, "display_reg")) {
865 else if (!strcasecmp(action, "register")) {
869 else if (!strcasecmp(action, "display_changepw")) {
873 else if (!strcasecmp(action, "changepw")) {
877 else if (!strcasecmp(action, "display_edit_node")) {
881 else if (!strcasecmp(action, "display_netconf")) {
885 else if (!strcasecmp(action, "display_confirm_unshare")) {
886 display_confirm_unshare();
889 else if (!strcasecmp(action, "display_confirm_delete_node")) {
890 display_confirm_delete_node();
893 else if (!strcasecmp(action, "delete_node")) {
897 else if (!strcasecmp(action, "unshare")) {
901 else if (!strcasecmp(action, "display_add_node")) {
905 else if (!strcasecmp(action, "add_node")) {
909 else if (!strcasecmp(action, "display_share")) {
913 else if (!strcasecmp(action, "share")) {
917 else if (!strcasecmp(action, "terminate_session")) {
921 else if (!strcasecmp(action, "edit_me")) {
925 else if (!strcasecmp(action, "display_siteconfig")) {
926 display_siteconfig();
929 else if (!strcasecmp(action, "siteconfig")) {
933 else if (!strcasecmp(action, "display_generic")) {
937 else if (!strcasecmp(action, "do_generic")) {
941 /* When all else fails... */
943 printf("HTTP/1.0 200 OK\n");
944 output_headers(1, "");
946 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
947 wprintf("You're in session %d<HR>\n", wc_session);
948 wprintf("Command: <BR><PRE>\n");
950 wprintf("</PRE><HR>\n");
951 wprintf("Variables: <BR><PRE>\n");
953 wprintf("</PRE><HR>\n");
958 if (content != NULL) {
963 if (upload_length > 0) {
969 int main(int argc, char *argv[]) {
971 if (argc < 2 || argc > 4) {
973 "webcit: usage: webcit <session_id> [host [port]]\n");
977 wc_session = atoi(argv[1]);
980 defaulthost = argv[2];
982 defaultport = argv[3];
985 strcpy(wc_username, "");
986 strcpy(wc_password, "");
987 strcpy(wc_roomname, "");