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;
35 struct webcontent *wlist = NULL;
36 struct webcontent *wlast = NULL;
38 struct urlcontent *urlstrings = NULL;
40 static const char *defaulthost = DEFAULT_HOST;
41 static const char *defaultport = DEFAULT_PORT;
43 int upload_length = 0;
47 void unescape_input(char *buf)
52 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
53 buf[strlen(buf)-1] = 0;
55 for (a=0; a<strlen(buf); ++a) {
56 if (buf[a]=='+') buf[a]=' ';
61 sscanf(hex,"%02x",&b);
63 strcpy(&buf[a+1],&buf[a+3]);
70 void addurls(char *url) {
77 while (strlen(up)>0) {
79 /* locate the = sign */
82 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
86 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
89 strcpy(u->url_key, buf);
91 /* now chop that part off */
92 for (a=0; a<=b; ++a) ++up;
94 /* locate the & sign */
97 for (a=0; a<strlen(up); ++a) {
98 if (!strncmp(ptr,"&",1)) {
105 for (a=0; a<b; ++a) ++ptr;
108 u->url_data = malloc(strlen(up)+1);
109 strcpy(u->url_data, up);
111 unescape_input(u->url_data);
117 void free_urls(void) {
118 struct urlcontent *u;
120 while (urlstrings != NULL) {
121 free(urlstrings->url_data);
122 u = urlstrings->next;
129 * Diagnostic function to display the contents of all variables
131 void dump_vars(void) {
132 struct urlcontent *u;
134 for (u = urlstrings; u != NULL; u = u->next) {
135 wprintf("%38s = %s\n", u->url_key, u->url_data);
139 char *bstr(char *key) {
140 struct urlcontent *u;
142 for (u = urlstrings; u != NULL; u = u->next) {
143 if (!strcasecmp(u->url_key, key)) return(u->url_data);
149 void wprintf(const char *format, ...) {
151 struct webcontent *wptr;
153 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
164 va_start(arg_ptr, format);
165 vsprintf(wptr->w_data, format, arg_ptr);
169 int wContentLength(void) {
170 struct webcontent *wptr;
173 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
174 len = len + strlen(wptr->w_data);
180 void wDumpContent(void) {
181 struct webcontent *wptr;
183 printf("Content-type: text/html\n");
184 printf("Content-length: %d\n", wContentLength());
187 while (wlist != NULL) {
188 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
197 void escputs1(char *strbuf, int nbsp)
201 for (a=0; a<strlen(strbuf); ++a) {
202 if (strbuf[a]=='<') wprintf("<");
203 else if (strbuf[a]=='>') wprintf(">");
204 else if (strbuf[a]=='&') wprintf("&");
205 else if (strbuf[a]==34) wprintf(""");
206 else if (strbuf[a]==LB) wprintf("<");
207 else if (strbuf[a]==RB) wprintf(">");
208 else if (strbuf[a]==QU) wprintf("\"");
209 else if ((strbuf[a]==32)&&(nbsp==1)) {
213 wprintf("%c", strbuf[a]);
218 void escputs(char *strbuf)
225 char *urlesc(char *strbuf)
228 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
229 static char outbuf[512];
233 for (a=0; a<strlen(strbuf); ++a) {
235 for (b=0; b<strlen(ec); ++b) {
236 if (strbuf[a]==ec[b]) c=1;
239 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
240 else sprintf(&outbuf[b],"%c",strbuf[a]);
245 void urlescputs(char *strbuf)
247 wprintf("%s",urlesc(strbuf));
251 void getz(char *buf) {
253 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
255 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
256 buf[strlen(buf)-1] = 0;
261 * Output all that important stuff that the browser will want to see
263 * If print_standard_html_head is nonzero, we also get some standard HTML
264 * headers. If it's set to 2, the session is considered to be closing.
266 void output_headers(int print_standard_html_head) {
268 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
271 printf("Server: %s\n", SERVER);
272 printf("Connection: close\n");
274 stuff_to_cookie(cookie, wc_session, wc_username, wc_password, wc_roomname);
275 if (print_standard_html_head==2) {
276 printf("X-WebCit-Session: close\n");
277 printf("Set-cookie: webcit=%s\n", unset);
280 printf("Set-cookie: webcit=%s\n", cookie);
283 if (print_standard_html_head > 0) {
284 wprintf("<HTML><HEAD><TITLE>");
285 escputs("WebCit"); /* FIX -- add BBS name here */
286 wprintf("</TITLE></HEAD>");
287 if (ExpressMessages != NULL) {
288 wprintf("<SCRIPT language=\"javascript\">\n");
289 wprintf("function ExpressMessage() {\n");
290 wprintf(" alert(\"");
291 escputs(ExpressMessages);
293 wprintf(" }\n </SCRIPT>\n");
296 if (ExpressMessages != NULL) {
297 wprintf("onload=\"ExpressMessage()\" ");
298 free(ExpressMessages);
299 ExpressMessages = NULL;
301 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
309 void check_for_express_messages() {
315 while (serv_gets(buf), strcmp(buf, "000")) {
316 if (ExpressMessages == NULL) {
317 ExpressMessages = malloc(strlen(buf) + 4);
318 strcpy(ExpressMessages, "");
321 ExpressMessages = realloc(ExpressMessages,
322 (strlen(ExpressMessages) + strlen(buf) + 4) );
324 strcat(ExpressMessages, buf);
325 strcat(ExpressMessages, "\\n");
333 void output_static(char *what) {
339 sprintf(buf, "static/%s", what);
340 fp = fopen(buf, "rb");
342 printf("HTTP/1.0 404 %s\n", strerror(errno));
344 printf("Content-Type: text/plain\n");
345 sprintf(buf, "%s: %s\n", what, strerror(errno));
346 printf("Content-length: %d\n", strlen(buf));
348 fwrite(buf, strlen(buf), 1, stdout);
351 printf("HTTP/1.0 200 OK\n");
354 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
355 printf("Content-type: image/gif\n");
356 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
357 printf("Content-type: image/jpeg\n");
358 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
359 printf("Content-type: text/html\n");
361 printf("Content-type: application/octet-stream\n");
363 fstat(fileno(fp), &statbuf);
364 bytes = statbuf.st_size;
365 printf("Content-length: %ld\n", (long)bytes);
368 putc(getc(fp), stdout);
375 void output_image() {
380 off_t accomplished = 0L;
383 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
386 bytes = extract_long(&buf[4], 0);
387 printf("HTTP/1.0 200 OK\n");
389 printf("Content-type: image/gif\n");
390 printf("Content-length: %ld\n", bytes);
393 while (bytes > (off_t)0) {
394 thisblock = (off_t)sizeof(xferbuf);
395 if (thisblock > bytes) thisblock = bytes;
396 serv_printf("READ %ld|%ld", accomplished, thisblock);
398 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
399 serv_read(xferbuf, (int)thisblock);
400 fwrite(xferbuf, thisblock, 1, stdout);
401 bytes = bytes - thisblock;
402 accomplished = accomplished + thisblock;
409 printf("HTTP/1.0 404 %s\n", strerror(errno));
411 printf("Content-Type: text/plain\n");
412 sprintf(buf, "Error retrieving image\n");
413 printf("Content-length: %d\n", strlen(buf));
415 fwrite(buf, strlen(buf), 1, stdout);
422 * Convenience functions to display a page containing only a string
424 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext) {
425 printf("HTTP/1.0 200 OK\n");
427 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
428 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
429 wprintf("<B>%s</B>\n", titlebarmsg);
430 wprintf("</FONT></TD></TR></TABLE><BR>\n");
431 escputs(messagetext);
432 wprintf("</BODY></HTML>\n");
436 void display_error(char *errormessage) {
437 convenience_page("770000", "Error", errormessage);
440 void display_success(char *successmessage) {
441 convenience_page("007700", "OK", successmessage);
446 void extract_action(char *actbuf, char *cmdbuf) {
449 strcpy(actbuf, cmdbuf);
450 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
451 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
452 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
454 for (i=0; i<strlen(actbuf); ++i) {
455 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
456 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
457 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
458 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
463 void upload_handler(char *name, char *filename, char *encoding,
464 void *content, size_t length) {
466 fprintf(stderr, "UPLOAD HANDLER CALLED\n");
467 fprintf(stderr, " name = %s\n", name);
468 fprintf(stderr, "filename = %s\n", filename);
469 fprintf(stderr, "encoding = %s\n", encoding);
470 fprintf(stderr, " length = %d\n", length);
472 if (strlen(name)>0) {
473 upload = malloc(length);
474 if (upload != NULL) {
475 upload_length = length;
476 memcpy(upload, content, length);
483 void session_loop(void) {
488 int ContentLength = 0;
489 char ContentType[512];
492 /* We stuff these with the values coming from the client cookies,
493 * so we can use them to reconnect a timed out session if we have to.
497 char c_username[256];
498 char c_password[256];
499 char c_roomname[256];
502 strcpy(c_host, defaulthost);
503 strcpy(c_port, defaultport);
504 strcpy(c_username, "");
505 strcpy(c_password, "");
506 strcpy(c_roomname, "");
512 extract_action(action, cmd);
517 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
518 strcpy(cookie, &buf[15]);
519 cookie_to_stuff(cookie, NULL,
520 c_username, c_password, c_roomname);
523 if (!strncasecmp(buf, "Content-length: ", 16)) {
524 ContentLength = atoi(&buf[16]);
526 if (!strncasecmp(buf, "Content-type: ", 14)) {
527 strcpy(ContentType, &buf[14]);
529 } while(strlen(buf)>0);
533 if (ContentLength > 0) {
534 content = malloc(ContentLength+1);
535 fread(content, ContentLength, 1, stdin);
537 content[ContentLength] = 0;
539 if (!strncasecmp(ContentType,
540 "application/x-www-form-urlencoded", 33)) {
543 else if (!strncasecmp(ContentType, "multipart", 9)) {
544 mime_parser(content, ContentLength, ContentType,
552 /* If there are variables in the URL, we must grab them now */
553 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
554 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
560 * If we're not connected to a Citadel server, try to hook up the
561 * connection now. Preference is given to the host and port specified
562 * by browser cookies, if cookies have been supplied.
565 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
566 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
567 serv_sock = connectsock(c_host, c_port, "tcp");
569 serv_gets(buf); /* get the server welcome message */
573 check_for_express_messages();
576 * If we're not logged in, but we have username and password cookies
577 * supplied by the browser, try using them to log in.
579 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
580 serv_printf("USER %s", c_username);
583 serv_printf("PASS %s", c_password);
586 become_logged_in(c_username, c_password, buf);
592 * If we don't have a current room, but a cookie specifying the
593 * current room is supplied, make an effort to go there.
595 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
596 serv_printf("GOTO %s", c_roomname);
599 strcpy(wc_roomname, c_roomname);
603 if (!strcasecmp(action, "static")) {
604 strcpy(buf, &cmd[12]);
605 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
609 else if (!strcasecmp(action, "image")) {
613 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
617 else if (!logged_in) {
621 /* Various commands... */
623 else if (!strcasecmp(action, "do_welcome")) {
627 else if (!strcasecmp(action, "display_main_menu")) {
631 else if (!strcasecmp(action, "advanced")) {
632 display_advanced_menu();
635 else if (!strcasecmp(action, "whobbs")) {
639 else if (!strcasecmp(action, "knrooms")) {
640 list_all_rooms_by_floor();
643 else if (!strcasecmp(action, "gotonext")) {
648 else if (!strcasecmp(action, "skip")) {
652 else if (!strcasecmp(action, "ungoto")) {
656 else if (!strcasecmp(action, "dotgoto")) {
658 gotoroom(bstr("room"), 1);
661 else if (!strcasecmp(action, "termquit")) {
665 else if (!strcasecmp(action, "readnew")) {
669 else if (!strcasecmp(action, "readold")) {
673 else if (!strcasecmp(action, "readfwd")) {
677 else if (!strcasecmp(action, "display_enter")) {
681 else if (!strcasecmp(action, "post")) {
685 else if (!strcasecmp(action, "confirm_delete_msg")) {
686 confirm_delete_msg();
689 else if (!strcasecmp(action, "delete_msg")) {
693 else if (!strcasecmp(action, "confirm_move_msg")) {
697 else if (!strcasecmp(action, "move_msg")) {
701 else if (!strcasecmp(action, "userlist")) {
705 else if (!strcasecmp(action, "showuser")) {
709 else if (!strcasecmp(action, "display_page")) {
713 else if (!strcasecmp(action, "page_user")) {
717 else if (!strcasecmp(action, "chat")) {
721 else if (!strcasecmp(action, "display_private")) {
722 display_private("", 0);
725 else if (!strcasecmp(action, "goto_private")) {
729 else if (!strcasecmp(action, "zapped_list")) {
733 else if (!strcasecmp(action, "display_zap")) {
737 else if (!strcasecmp(action, "zap")) {
741 else if (!strcasecmp(action, "display_entroom")) {
745 else if (!strcasecmp(action, "entroom")) {
749 else if (!strcasecmp(action, "display_editroom")) {
753 else if (!strcasecmp(action, "editroom")) {
757 else if (!strcasecmp(action, "display_editinfo")) {
758 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
761 else if (!strcasecmp(action, "editinfo")) {
762 save_edit("Room info", "EINF 1", 1);
765 else if (!strcasecmp(action, "display_editbio")) {
766 sprintf(buf, "RBIO %s", wc_username);
767 display_edit("Your bio", "NOOP", buf, "editbio");
770 else if (!strcasecmp(action, "editbio")) {
771 save_edit("Your bio", "EBIO", 0);
774 else if (!strcasecmp(action, "confirm_delete_room")) {
775 confirm_delete_room();
778 else if (!strcasecmp(action, "delete_room")) {
782 else if (!strcasecmp(action, "validate")) {
786 else if (!strcasecmp(action, "display_editpic")) {
787 display_graphics_upload("your photo",
792 else if (!strcasecmp(action, "editpic")) {
793 do_graphics_upload("UIMG 1|_userpic_");
796 else if (!strcasecmp(action, "display_editroompic")) {
797 display_graphics_upload("the graphic for this room",
802 else if (!strcasecmp(action, "editroompic")) {
803 do_graphics_upload("UIMG 1|_roompic_");
806 else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
807 select_floor_to_edit_pic();
810 else if (!strcasecmp(action, "display_editfloorpic")) {
811 sprintf(buf, "UIMG 0|_floorpic_|%s",
812 bstr("which_floor"));
813 display_graphics_upload("the graphic for this floor",
818 else if (!strcasecmp(action, "editfloorpic")) {
819 sprintf(buf, "UIMG 1|_floorpic_|%s",
820 bstr("which_floor"));
821 do_graphics_upload(buf);
824 else if (!strcasecmp(action, "display_reg")) {
828 else if (!strcasecmp(action, "register")) {
832 else if (!strcasecmp(action, "display_changepw")) {
836 else if (!strcasecmp(action, "changepw")) {
840 else if (!strcasecmp(action, "display_edit_node")) {
844 else if (!strcasecmp(action, "display_netconf")) {
848 else if (!strcasecmp(action, "display_confirm_unshare")) {
849 display_confirm_unshare();
852 else if (!strcasecmp(action, "display_confirm_delete_node")) {
853 display_confirm_delete_node();
856 else if (!strcasecmp(action, "delete_node")) {
860 else if (!strcasecmp(action, "unshare")) {
864 else if (!strcasecmp(action, "display_add_node")) {
868 else if (!strcasecmp(action, "add_node")) {
872 else if (!strcasecmp(action, "display_share")) {
876 else if (!strcasecmp(action, "share")) {
880 else if (!strcasecmp(action, "terminate_session")) {
884 else if (!strcasecmp(action, "edit_me")) {
888 else if (!strcasecmp(action, "display_siteconfig")) {
889 display_siteconfig();
892 else if (!strcasecmp(action, "siteconfig")) {
896 else if (!strcasecmp(action, "display_generic")) {
900 else if (!strcasecmp(action, "do_generic")) {
904 /* When all else fails... */
906 printf("HTTP/1.0 200 OK\n");
909 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
910 wprintf("You're in session %d<HR>\n", wc_session);
911 wprintf("Command: <BR><PRE>\n");
913 wprintf("</PRE><HR>\n");
914 wprintf("Variables: <BR><PRE>\n");
916 wprintf("</PRE><HR>\n");
917 wprintf("</BODY></HTML>\n");
922 if (content != NULL) {
927 if (upload_length > 0) {
933 int main(int argc, char *argv[]) {
935 if (argc < 2 || argc > 4) {
937 "webcit: usage: webcit <session_id> [host [port]]\n");
941 wc_session = atoi(argv[1]);
944 defaulthost = argv[2];
946 defaultport = argv[3];
949 strcpy(wc_username, "");
950 strcpy(wc_password, "");
951 strcpy(wc_roomname, "");