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.
25 char wc_username[256];
26 char wc_password[256];
27 char wc_roomname[256];
28 int TransactionCount = 0;
32 char *ExpressMessages = NULL;
34 struct webcontent *wlist = NULL;
35 struct webcontent *wlast = NULL;
37 struct urlcontent *urlstrings = NULL;
39 static const char *defaulthost = DEFAULT_HOST;
40 static const char *defaultport = DEFAULT_PORT;
42 int upload_length = 0;
46 void unescape_input(char *buf)
51 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
52 buf[strlen(buf)-1] = 0;
54 for (a=0; a<strlen(buf); ++a) {
55 if (buf[a]=='+') buf[a]=' ';
60 sscanf(hex,"%02x",&b);
62 strcpy(&buf[a+1],&buf[a+3]);
69 void addurls(char *url) {
76 while (strlen(up)>0) {
78 /* locate the = sign */
81 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
85 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
88 strcpy(u->url_key, buf);
90 /* now chop that part off */
91 for (a=0; a<=b; ++a) ++up;
93 /* locate the & sign */
96 for (a=0; a<strlen(up); ++a) {
97 if (!strncmp(ptr,"&",1)) {
104 for (a=0; a<b; ++a) ++ptr;
107 u->url_data = malloc(strlen(up)+1);
108 strcpy(u->url_data, up);
110 unescape_input(u->url_data);
116 void free_urls(void) {
117 struct urlcontent *u;
119 while (urlstrings != NULL) {
120 free(urlstrings->url_data);
121 u = urlstrings->next;
128 * Diagnostic function to display the contents of all variables
130 void dump_vars(void) {
131 struct urlcontent *u;
133 for (u = urlstrings; u != NULL; u = u->next) {
134 wprintf("%38s = %s\n", u->url_key, u->url_data);
138 char *bstr(char *key) {
139 struct urlcontent *u;
141 for (u = urlstrings; u != NULL; u = u->next) {
142 if (!strcasecmp(u->url_key, key)) return(u->url_data);
148 void wprintf(const char *format, ...) {
150 struct webcontent *wptr;
152 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
163 va_start(arg_ptr, format);
164 vsprintf(wptr->w_data, format, arg_ptr);
168 int wContentLength(void) {
169 struct webcontent *wptr;
172 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
173 len = len + strlen(wptr->w_data);
179 void wDumpContent(void) {
180 struct webcontent *wptr;
182 printf("Content-type: text/html\n");
183 printf("Content-length: %d\n", wContentLength());
186 while (wlist != NULL) {
187 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
196 void escputs1(char *strbuf, int nbsp)
200 for (a=0; a<strlen(strbuf); ++a) {
201 if (strbuf[a]=='<') wprintf("<");
202 else if (strbuf[a]=='>') wprintf(">");
203 else if (strbuf[a]=='&') wprintf("&");
204 else if (strbuf[a]==34) wprintf(""");
205 else if (strbuf[a]==LB) wprintf("<");
206 else if (strbuf[a]==RB) wprintf(">");
207 else if (strbuf[a]==QU) wprintf("\"");
208 else if ((strbuf[a]==32)&&(nbsp==1)) {
212 wprintf("%c", strbuf[a]);
217 void escputs(char *strbuf)
224 char *urlesc(char *strbuf)
227 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
228 static char outbuf[512];
232 for (a=0; a<strlen(strbuf); ++a) {
234 for (b=0; b<strlen(ec); ++b) {
235 if (strbuf[a]==ec[b]) c=1;
238 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
239 else sprintf(&outbuf[b],"%c",strbuf[a]);
244 void urlescputs(char *strbuf)
246 wprintf("%s",urlesc(strbuf));
250 void getz(char *buf) {
252 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
254 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
255 buf[strlen(buf)-1] = 0;
260 * Output all that important stuff that the browser will want to see
262 * If print_standard_html_head is nonzero, we also get some standard HTML
263 * headers. If it's set to 2, the session is considered to be closing.
265 void output_headers(int print_standard_html_head) {
267 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
270 printf("Server: %s\n", SERVER);
271 printf("Connection: close\n");
273 stuff_to_cookie(cookie, wc_session, wc_username, wc_password, wc_roomname);
274 if (print_standard_html_head==2) {
275 printf("X-WebCit-Session: close\n");
276 printf("Set-cookie: webcit=%s\n", unset);
279 printf("Set-cookie: webcit=%s\n", cookie);
282 if (print_standard_html_head > 0) {
283 wprintf("<HTML><HEAD><TITLE>");
284 escputs("WebCit"); /* FIX -- add BBS name here */
285 wprintf("</TITLE></HEAD>");
286 if (ExpressMessages != NULL) {
287 wprintf("<SCRIPT language=\"javascript\">\n");
288 wprintf("function ExpressMessage() {\n");
289 wprintf(" alert(\"");
290 escputs(ExpressMessages);
292 wprintf(" }\n </SCRIPT>\n");
295 if (ExpressMessages != NULL) {
296 wprintf("onload=\"ExpressMessage()\" ");
297 free(ExpressMessages);
298 ExpressMessages = NULL;
300 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
308 void check_for_express_messages() {
314 while (serv_gets(buf), strcmp(buf, "000")) {
315 if (ExpressMessages == NULL) {
316 ExpressMessages = malloc(strlen(buf) + 4);
317 strcpy(ExpressMessages, "");
320 ExpressMessages = realloc(ExpressMessages,
321 (strlen(ExpressMessages) + strlen(buf) + 4) );
323 strcat(ExpressMessages, buf);
324 strcat(ExpressMessages, "\\n");
332 void output_static(char *what) {
338 sprintf(buf, "static/%s", what);
339 fp = fopen(buf, "rb");
341 printf("HTTP/1.0 404 %s\n", strerror(errno));
343 printf("Content-Type: text/plain\n");
344 sprintf(buf, "%s: %s\n", what, strerror(errno));
345 printf("Content-length: %d\n", strlen(buf));
347 fwrite(buf, strlen(buf), 1, stdout);
350 printf("HTTP/1.0 200 OK\n");
353 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
354 printf("Content-type: image/gif\n");
355 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
356 printf("Content-type: image/jpeg\n");
357 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
358 printf("Content-type: text/html\n");
360 printf("Content-type: application/octet-stream\n");
362 fstat(fileno(fp), &statbuf);
363 bytes = statbuf.st_size;
364 printf("Content-length: %ld\n", (long)bytes);
367 putc(getc(fp), stdout);
374 void output_image() {
379 off_t accomplished = 0L;
382 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
385 bytes = extract_long(&buf[4], 0);
386 printf("HTTP/1.0 200 OK\n");
388 printf("Content-type: image/gif\n");
389 printf("Content-length: %ld\n", bytes);
392 while (bytes > (off_t)0) {
393 thisblock = (off_t)sizeof(xferbuf);
394 if (thisblock > bytes) thisblock = bytes;
395 serv_printf("READ %ld|%ld", accomplished, thisblock);
397 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
398 serv_read(xferbuf, (int)thisblock);
399 fwrite(xferbuf, thisblock, 1, stdout);
400 bytes = bytes - thisblock;
401 accomplished = accomplished + thisblock;
408 printf("HTTP/1.0 404 %s\n", strerror(errno));
410 printf("Content-Type: text/plain\n");
411 sprintf(buf, "Error retrieving image\n");
412 printf("Content-length: %d\n", strlen(buf));
414 fwrite(buf, strlen(buf), 1, stdout);
421 * Convenience functions to display a page containing only a string
423 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext) {
424 printf("HTTP/1.0 200 OK\n");
426 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
427 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
428 wprintf("<B>%s</B>\n", titlebarmsg);
429 wprintf("</FONT></TD></TR></TABLE><BR>\n");
430 escputs(messagetext);
431 wprintf("</BODY></HTML>\n");
435 void display_error(char *errormessage) {
436 convenience_page("770000", "Error", errormessage);
439 void display_success(char *successmessage) {
440 convenience_page("007700", "OK", successmessage);
445 void extract_action(char *actbuf, char *cmdbuf) {
448 strcpy(actbuf, cmdbuf);
449 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
450 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
451 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
453 for (i=0; i<strlen(actbuf); ++i) {
454 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
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; }
462 void session_loop(void) {
467 int ContentLength = 0;
468 char ContentType[512];
471 /* We stuff these with the values coming from the client cookies,
472 * so we can use them to reconnect a timed out session if we have to.
476 char c_username[256];
477 char c_password[256];
478 char c_roomname[256];
481 strcpy(c_host, defaulthost);
482 strcpy(c_port, defaultport);
483 strcpy(c_username, "");
484 strcpy(c_password, "");
485 strcpy(c_roomname, "");
491 extract_action(action, cmd);
496 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
497 strcpy(cookie, &buf[15]);
498 cookie_to_stuff(cookie, NULL,
499 c_username, c_password, c_roomname);
502 if (!strncasecmp(buf, "Content-length: ", 16)) {
503 ContentLength = atoi(&buf[16]);
505 if (!strncasecmp(buf, "Content-type: ", 14)) {
506 strcpy(ContentType, &buf[14]);
508 } while(strlen(buf)>0);
512 if (ContentLength > 0) {
513 content = malloc(ContentLength+1);
514 fread(content, ContentLength, 1, stdin);
516 content[ContentLength] = 0;
518 if (!strncasecmp(ContentType,
519 "application/x-www-form-urlencoded", 33)) {
522 else if (!strncasecmp(ContentType, "multipart", 9)) {
523 mime_parser(content, ContentLength, ContentType);
530 /* If there are variables in the URL, we must grab them now */
531 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
532 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
538 * If we're not connected to a Citadel server, try to hook up the
539 * connection now. Preference is given to the host and port specified
540 * by browser cookies, if cookies have been supplied.
543 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
544 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
545 serv_sock = connectsock(c_host, c_port, "tcp");
547 serv_gets(buf); /* get the server welcome message */
551 check_for_express_messages();
554 * If we're not logged in, but we have username and password cookies
555 * supplied by the browser, try using them to log in.
557 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
558 serv_printf("USER %s", c_username);
561 serv_printf("PASS %s", c_password);
564 become_logged_in(c_username, c_password, buf);
570 * If we don't have a current room, but a cookie specifying the
571 * current room is supplied, make an effort to go there.
573 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
574 serv_printf("GOTO %s", c_roomname);
577 strcpy(wc_roomname, c_roomname);
581 if (!strcasecmp(action, "static")) {
582 strcpy(buf, &cmd[12]);
583 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
587 else if (!strcasecmp(action, "image")) {
591 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
595 else if (!logged_in) {
599 /* Various commands... */
601 else if (!strcasecmp(action, "do_welcome")) {
605 else if (!strcasecmp(action, "display_main_menu")) {
609 else if (!strcasecmp(action, "advanced")) {
610 display_advanced_menu();
613 else if (!strcasecmp(action, "whobbs")) {
617 else if (!strcasecmp(action, "knrooms")) {
618 list_all_rooms_by_floor();
621 else if (!strcasecmp(action, "gotonext")) {
626 else if (!strcasecmp(action, "skip")) {
630 else if (!strcasecmp(action, "ungoto")) {
634 else if (!strcasecmp(action, "dotgoto")) {
636 gotoroom(bstr("room"), 1);
639 else if (!strcasecmp(action, "termquit")) {
643 else if (!strcasecmp(action, "readnew")) {
647 else if (!strcasecmp(action, "readold")) {
651 else if (!strcasecmp(action, "readfwd")) {
655 else if (!strcasecmp(action, "display_enter")) {
659 else if (!strcasecmp(action, "post")) {
663 else if (!strcasecmp(action, "confirm_delete_msg")) {
664 confirm_delete_msg();
667 else if (!strcasecmp(action, "delete_msg")) {
671 else if (!strcasecmp(action, "confirm_move_msg")) {
675 else if (!strcasecmp(action, "move_msg")) {
679 else if (!strcasecmp(action, "userlist")) {
683 else if (!strcasecmp(action, "showuser")) {
687 else if (!strcasecmp(action, "display_page")) {
691 else if (!strcasecmp(action, "page_user")) {
695 else if (!strcasecmp(action, "chat")) {
699 else if (!strcasecmp(action, "display_private")) {
700 display_private("", 0);
703 else if (!strcasecmp(action, "goto_private")) {
707 else if (!strcasecmp(action, "zapped_list")) {
711 else if (!strcasecmp(action, "display_zap")) {
715 else if (!strcasecmp(action, "zap")) {
719 else if (!strcasecmp(action, "display_entroom")) {
723 else if (!strcasecmp(action, "entroom")) {
727 else if (!strcasecmp(action, "display_editroom")) {
731 else if (!strcasecmp(action, "editroom")) {
735 else if (!strcasecmp(action, "display_editinfo")) {
736 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
739 else if (!strcasecmp(action, "editinfo")) {
740 save_edit("Room info", "EINF 1", 1);
743 else if (!strcasecmp(action, "display_editbio")) {
744 sprintf(buf, "RBIO %s", wc_username);
745 display_edit("Your bio", "NOOP", buf, "editbio");
748 else if (!strcasecmp(action, "editbio")) {
749 save_edit("Your bio", "EBIO", 0);
752 else if (!strcasecmp(action, "confirm_delete_room")) {
753 confirm_delete_room();
756 else if (!strcasecmp(action, "delete_room")) {
760 else if (!strcasecmp(action, "validate")) {
764 else if (!strcasecmp(action, "display_editpic")) {
765 display_graphics_upload("your photo",
770 else if (!strcasecmp(action, "editpic")) {
771 do_graphics_upload("UIMG 1|_userpic_");
774 else if (!strcasecmp(action, "display_editroompic")) {
775 display_graphics_upload("the graphic for this room",
780 else if (!strcasecmp(action, "editroompic")) {
781 do_graphics_upload("UIMG 1|_roompic_");
784 else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
785 select_floor_to_edit_pic();
788 else if (!strcasecmp(action, "display_editfloorpic")) {
789 sprintf(buf, "UIMG 0|_floorpic_|%s",
790 bstr("which_floor"));
791 display_graphics_upload("the graphic for this floor",
796 else if (!strcasecmp(action, "editfloorpic")) {
797 sprintf(buf, "UIMG 1|_floorpic_|%s",
798 bstr("which_floor"));
799 do_graphics_upload(buf);
802 else if (!strcasecmp(action, "display_reg")) {
806 else if (!strcasecmp(action, "register")) {
810 else if (!strcasecmp(action, "display_changepw")) {
814 else if (!strcasecmp(action, "changepw")) {
818 else if (!strcasecmp(action, "display_edit_node")) {
822 else if (!strcasecmp(action, "display_netconf")) {
826 else if (!strcasecmp(action, "display_confirm_unshare")) {
827 display_confirm_unshare();
830 else if (!strcasecmp(action, "display_confirm_delete_node")) {
831 display_confirm_delete_node();
834 else if (!strcasecmp(action, "delete_node")) {
838 else if (!strcasecmp(action, "unshare")) {
842 else if (!strcasecmp(action, "display_add_node")) {
846 else if (!strcasecmp(action, "add_node")) {
850 else if (!strcasecmp(action, "display_share")) {
854 else if (!strcasecmp(action, "share")) {
858 else if (!strcasecmp(action, "terminate_session")) {
862 else if (!strcasecmp(action, "edit_me")) {
866 else if (!strcasecmp(action, "display_siteconfig")) {
867 display_siteconfig();
870 else if (!strcasecmp(action, "siteconfig")) {
874 else if (!strcasecmp(action, "display_generic")) {
878 else if (!strcasecmp(action, "do_generic")) {
882 /* When all else fails... */
884 printf("HTTP/1.0 200 OK\n");
887 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
888 wprintf("You're in session %d<HR>\n", wc_session);
889 wprintf("Command: <BR><PRE>\n");
891 wprintf("</PRE><HR>\n");
892 wprintf("Variables: <BR><PRE>\n");
894 wprintf("</PRE><HR>\n");
895 wprintf("</BODY></HTML>\n");
900 if (content != NULL) {
905 if (upload_length > 0) {
911 int main(int argc, char *argv[]) {
913 if (argc < 2 || argc > 4) {
915 "webcit: usage: webcit <session_id> [host [port]]\n");
919 wc_session = atoi(argv[1]);
922 defaulthost = argv[2];
924 defaultport = argv[3];
927 strcpy(wc_username, "");
928 strcpy(wc_password, "");
929 strcpy(wc_roomname, "");