]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
1df6854caf9b19806ec7e9c8f2b3cd09b35e0039
[citadel.git] / webcit / webcit.c
1 /*
2  * webcit.c
3  *
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.
7  *
8  * $Id$
9  */
10
11 #include <stdlib.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include <stdio.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <sys/stat.h>
20 #include <stdarg.h>
21 #include "webcit.h"
22 #include "child.h"
23
24 int wc_session;
25 char wc_host[256];
26 char wc_port[256];
27 char wc_username[256];
28 char wc_password[256];
29 char wc_roomname[256];
30 int TransactionCount = 0;
31 int connected = 0;
32 int logged_in = 0;
33 int axlevel;
34 char *ExpressMessages = NULL;
35
36 struct webcontent *wlist = NULL;
37 struct webcontent *wlast = NULL;
38
39 struct urlcontent *urlstrings = NULL;
40
41 static const char *defaulthost = DEFAULT_HOST;
42 static const char *defaultport = DEFAULT_PORT;
43
44
45 void unescape_input(char *buf)
46 {
47         int a,b;
48         char hex[3];
49
50         while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
51                 buf[strlen(buf)-1] = 0;
52
53         for (a=0; a<strlen(buf); ++a) {
54                 if (buf[a]=='+') buf[a]=' ';    
55                 if (buf[a]=='%') {
56                         hex[0]=buf[a+1];
57                         hex[1]=buf[a+2];
58                         hex[2]=0;
59                         sscanf(hex,"%02x",&b);
60                         buf[a] = (char) b;
61                         strcpy(&buf[a+1],&buf[a+3]);
62                         }
63                 }
64
65         }
66
67
68 void addurls(char *url) {
69         char *up, *ptr;
70         char buf[256];
71         int a,b;
72         struct urlcontent *u;
73
74         up = url;
75         while (strlen(up)>0) {
76                 
77                 /* locate the = sign */
78                 strncpy(buf,up,255);
79                 b = (-1);
80                 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
81                 if (b<0) return;
82                 buf[b]=0;
83         
84                 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
85                 u->next = urlstrings;
86                 urlstrings = u;
87                 strcpy(u->url_key, buf);
88         
89                 /* now chop that part off */
90                 for (a=0; a<=b; ++a) ++up;
91         
92                 /* locate the & sign */
93                 ptr = up;
94                 b = strlen(up);
95                 for (a=0; a<strlen(up); ++a) {
96                         if (!strncmp(ptr,"&",1)) {
97                                 b=a;
98                                 break;
99                                 }
100                         ++ptr;
101                         }
102                 ptr = up;
103                 for (a=0; a<b; ++a) ++ptr;
104                 strcpy(ptr,"");
105                 
106                 u->url_data = malloc(strlen(up)+1);
107                 strcpy(u->url_data, up);
108                 u->url_data[b] = 0;
109                 unescape_input(u->url_data);
110                 up = ptr;
111                 ++up;
112                 }
113         }
114
115 void free_urls(void) {
116         struct urlcontent *u;
117
118         while (urlstrings != NULL) {
119                 free(urlstrings->url_data);
120                 u = urlstrings->next;
121                 free(urlstrings);
122                 urlstrings = u;
123                 }
124         }
125
126 /*
127  * Diagnostic function to display the contents of all variables
128  */
129 void dump_vars(void) {
130         struct urlcontent *u;
131
132         for (u = urlstrings; u != NULL; u = u->next) {
133                 wprintf("%38s = %s\n", u->url_key, u->url_data);
134                 }
135         }
136
137 char *bstr(char *key) {
138         struct urlcontent *u;
139
140         for (u = urlstrings; u != NULL; u = u->next) {
141                 if (!strcasecmp(u->url_key, key)) return(u->url_data);
142                 }
143         return("");
144         }
145
146
147 void wprintf(const char *format, ...) {   
148         va_list arg_ptr;   
149         struct webcontent *wptr;
150
151         wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
152         wptr->next = NULL;
153         if (wlist == NULL) {
154                 wlist = wptr;
155                 wlast = wptr;
156                 }
157         else {
158                 wlast->next = wptr;
159                 wlast = wptr;
160                 }
161   
162         va_start(arg_ptr, format);   
163         vsprintf(wptr->w_data, format, arg_ptr);   
164         va_end(arg_ptr);   
165         }
166
167 int wContentLength(void) {
168         struct webcontent *wptr;
169         int len = 0;
170
171         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
172                 len = len + strlen(wptr->w_data);
173                 }
174
175         return(len);
176         }
177
178 void wDumpContent(void) {
179         struct webcontent *wptr;
180
181         printf("Content-type: text/html\n");
182         printf("Content-length: %d\n", wContentLength());
183         printf("\n");
184
185         while (wlist != NULL) {
186                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
187                 wptr = wlist->next;
188                 free(wlist);
189                 wlist = wptr;
190                 }
191         wlast = NULL;
192         }
193
194
195 void escputs1(char *strbuf, int nbsp)
196 {
197         int a;
198
199         for (a=0; a<strlen(strbuf); ++a) {
200                 if (strbuf[a]=='<') wprintf("&lt;");
201                 else if (strbuf[a]=='>') wprintf("&gt;");
202                 else if (strbuf[a]=='&') wprintf("&amp;");
203                 else if (strbuf[a]==34) wprintf("&quot;");
204                 else if (strbuf[a]==LB) wprintf("<");
205                 else if (strbuf[a]==RB) wprintf(">");
206                 else if (strbuf[a]==QU) wprintf("\"");
207                 else if ((strbuf[a]==32)&&(nbsp==1)) {
208                         wprintf("&nbsp;");
209                         }
210                 else {
211                         wprintf("%c", strbuf[a]);
212                         }
213                 }
214         }
215
216 void escputs(char *strbuf)
217 {
218         escputs1(strbuf,0);
219         }
220
221
222
223 char *urlesc(char *strbuf)
224 {
225         int a,b,c;
226         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
227         static char outbuf[512];
228         
229         strcpy(outbuf,"");
230
231         for (a=0; a<strlen(strbuf); ++a) {
232                 c = 0;
233                 for (b=0; b<strlen(ec); ++b) {
234                         if (strbuf[a]==ec[b]) c=1;
235                         }
236                 b = strlen(outbuf);
237                 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
238                 else sprintf(&outbuf[b],"%c",strbuf[a]);
239                 }
240         return(outbuf);
241         }
242
243 void urlescputs(char *strbuf)
244 {
245         wprintf("%s",urlesc(strbuf));
246         }
247
248
249 void getz(char *buf) {
250         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
251         else {
252                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
253                         buf[strlen(buf)-1] = 0;
254                 }
255         }
256
257 /*
258  * Output all that important stuff that the browser will want to see
259  */
260 void output_headers(int print_standard_html_head) {
261
262         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
263
264         printf("Server: %s\n", SERVER);
265         printf("Connection: close\n");
266         printf("Set-cookie: wc_session=%d\n", wc_session);
267
268         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
269         else printf("Set-cookie: wc_host=%s\n", unset);
270
271         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
272         else printf("Set-cookie: wc_port=%s\n", unset);
273
274         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
275                 wc_username);
276         else printf("Set-cookie: wc_username=%s\n", unset);
277
278         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
279                 wc_password);
280         else printf("Set-cookie: wc_password=%s\n", unset);
281
282         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
283                 wc_roomname);
284         else printf("Set-cookie: wc_roomname=%s\n", unset);
285
286         if (print_standard_html_head) {
287                 wprintf("<HTML><HEAD><TITLE>");
288                 escputs("WebCit");
289                 wprintf("</TITLE></HEAD>");
290                 if (ExpressMessages != NULL) {
291                         wprintf("<SCRIPT language=\"javascript\">\n");
292                         wprintf("function ExpressMessage() {\n");
293                         wprintf(" alert(\"");
294                         escputs(ExpressMessages);
295                         wprintf("\")\n");
296                         wprintf(" }\n </SCRIPT>\n");
297                         }
298                 wprintf("<BODY ");
299                 if (ExpressMessages != NULL) {
300                         wprintf("onload=\"ExpressMessage()\" ");
301                         free(ExpressMessages);
302                         ExpressMessages = NULL;
303                         }
304                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
305                 }
306
307         }
308
309
310
311
312 void check_for_express_messages() {
313         char buf[256];
314
315         serv_puts("PEXP");
316         serv_gets(buf);
317         if (buf[0]=='1') {
318                 while (serv_gets(buf), strcmp(buf, "000")) {
319                         if (ExpressMessages == NULL) {
320                                 ExpressMessages = malloc(strlen(buf) + 4);
321                                 strcpy(ExpressMessages, "");
322                                 }
323                         else {
324                                 ExpressMessages = realloc(ExpressMessages,
325                                  (strlen(ExpressMessages) + strlen(buf) + 4) );
326                                 }
327                         strcat(ExpressMessages, buf);
328                         strcat(ExpressMessages, "\\n");
329                         }
330                 }
331         }
332
333
334
335
336 void output_static(char *what) {
337         char buf[256];
338         FILE *fp;
339         struct stat statbuf;
340         off_t bytes;
341
342         sprintf(buf, "static/%s", what);
343         fp = fopen(buf, "rb");
344         if (fp == NULL) {
345                 printf("HTTP/1.0 404 %s\n", strerror(errno));
346                 output_headers(0);
347                 printf("Content-Type: text/plain\n");
348                 sprintf(buf, "%s: %s\n", what, strerror(errno));
349                 printf("Content-length: %d\n", strlen(buf));
350                 printf("\n");
351                 fwrite(buf, strlen(buf), 1, stdout);
352                 }
353         else {
354                 printf("HTTP/1.0 200 OK\n");
355                 output_headers(0);
356
357                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
358                         printf("Content-type: image/gif\n");
359                 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
360                         printf("Content-type: image/jpeg\n");
361                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
362                         printf("Content-type: text/html\n");
363                 else
364                         printf("Content-type: application/octet-stream\n");
365
366                 fstat(fileno(fp), &statbuf);
367                 bytes = statbuf.st_size;
368                 printf("Content-length: %ld\n", (long)bytes);
369                 printf("\n");
370                 while (bytes--) {
371                         putc(getc(fp), stdout);
372                         }
373                 fflush(stdout);
374                 fclose(fp);
375                 }
376         }
377
378 void output_image() {
379         char buf[256];
380         char xferbuf[4096];
381         off_t bytes;
382         off_t thisblock;
383         off_t accomplished = 0L;
384
385
386         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
387         serv_gets(buf);
388         if (buf[0]=='2') {
389                 bytes = extract_long(&buf[4], 0);
390                 printf("HTTP/1.0 200 OK\n");
391                 output_headers(0);
392                 printf("Content-type: image/gif\n");
393                 printf("Content-length: %ld\n", bytes);
394                 printf("\n");
395
396                 while (bytes > (off_t)0) {
397                         thisblock = (off_t)sizeof(xferbuf);
398                         if (thisblock > bytes) thisblock = bytes;
399                         serv_printf("READ %ld|%ld", accomplished, thisblock);
400                         serv_gets(buf);
401                         if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
402                         serv_read(xferbuf, (int)thisblock);
403                         fwrite(xferbuf, thisblock, 1, stdout);
404                         bytes = bytes - thisblock;
405                         accomplished = accomplished + thisblock;
406                         }
407                 fflush(stdout);
408                 serv_puts("CLOS");
409                 serv_gets(buf);
410                 }
411         else {
412                 printf("HTTP/1.0 404 %s\n", strerror(errno));
413                 output_headers(0);
414                 printf("Content-Type: text/plain\n");
415                 sprintf(buf, "Error retrieving image\n");
416                 printf("Content-length: %d\n", strlen(buf));
417                 printf("\n");
418                 fwrite(buf, strlen(buf), 1, stdout);
419                 }
420
421         }
422
423 void extract_action(char *actbuf, char *cmdbuf) {
424         int i;
425
426         strcpy(actbuf, cmdbuf);
427         if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
428         if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
429         if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
430
431         for (i=0; i<strlen(actbuf); ++i) {
432                 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
433                 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
434                 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
435                 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
436                 }
437         }
438
439
440 void session_loop(void) {
441         char cmd[256];
442         char action[256];
443         char buf[256];
444         int a, b;
445         int ContentLength = 0;
446         char *content;
447 FILE *fp;
448
449         /* We stuff these with the values coming from the client cookies,
450          * so we can use them to reconnect a timed out session if we have to.
451          */
452         char c_host[256];
453         char c_port[256];
454         char c_username[256];
455         char c_password[256];
456         char c_roomname[256];
457
458         strcpy(c_host, defaulthost);
459         strcpy(c_port, defaultport);
460         strcpy(c_username, "");
461         strcpy(c_password, "");
462         strcpy(c_roomname, "");
463
464         getz(cmd);
465         extract_action(action, cmd);
466
467         do {
468                 getz(buf);
469
470                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
471                         strcpy(c_host, &buf[16]);
472                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
473                         strcpy(c_port, &buf[16]);
474                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
475                         strcpy(c_username, &buf[20]);
476                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
477                         strcpy(c_password, &buf[20]);
478                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
479                         strcpy(c_roomname, &buf[20]);
480
481                 if (!strncasecmp(buf, "Content-length: ", 16)) {
482                         ContentLength = atoi(&buf[16]);
483                         }
484
485                 } while(strlen(buf)>0);
486
487         ++TransactionCount;
488
489         if (ContentLength > 0) {
490                 content = malloc(ContentLength+1);
491                 fread(content, ContentLength, 1, stdin);
492 fp = fopen("content", "wb");
493 fwrite(content, ContentLength, 1, fp);
494 fclose(fp);
495                 content[ContentLength] = 0;
496                 addurls(content);
497                 }
498         else {
499                 content = NULL;
500                 }
501
502         /*
503          * If we're not connected to a Citadel server, try to hook up the
504          * connection now.  Preference is given to the host and port specified
505          * by browser cookies, if cookies have been supplied.
506          */
507         if (!connected) {
508                 serv_sock = connectsock(c_host, c_port, "tcp");
509                 connected = 1;
510                 serv_gets(buf); /* get the server welcome message */
511                 strcpy(wc_host, c_host);
512                 strcpy(wc_port, c_port);
513                 get_serv_info();
514                 }
515
516         check_for_express_messages();
517
518         /*
519          * If we're not logged in, but we have username and password cookies
520          * supplied by the browser, try using them to log in.
521          */
522         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
523                 serv_printf("USER %s", c_username);
524                 serv_gets(buf);
525                 if (buf[0]=='3') {
526                         serv_printf("PASS %s", c_password);
527                         serv_gets(buf);
528                         if (buf[0]=='2') {
529                                 become_logged_in(c_username, c_password, buf);
530                                 }
531                         }
532                 }
533
534         /*
535          * If we don't have a current room, but a cookie specifying the
536          * current room is supplied, make an effort to go there.
537          */
538         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
539                 serv_printf("GOTO %s", c_roomname);
540                 serv_gets(buf);
541                 if (buf[0]=='2') {
542                         strcpy(wc_roomname, c_roomname);
543                         }
544                 }
545
546         /* If there are variables in the URL, we must grab them now */  
547         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
548                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
549                 addurls(&cmd[a+1]);
550                 cmd[a] = 0;
551                 }
552
553         if (!strcasecmp(action, "static")) {
554                 strcpy(buf, &cmd[12]);
555                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
556                 output_static(buf);
557                 }
558
559         else if (!strcasecmp(action, "image")) {
560                 output_image();
561                 }
562
563         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
564                 do_login();
565                 }
566
567         else if (!logged_in) {
568                 display_login(NULL);
569                 }
570
571         /* Various commands... */
572         
573         else if (!strcasecmp(action, "do_welcome")) {
574                 do_welcome();
575                 }
576
577         else if (!strcasecmp(action, "display_main_menu")) {
578                 display_main_menu();
579                 }
580
581         else if (!strcasecmp(action, "advanced")) {
582                 display_advanced_menu();
583                 }
584
585         else if (!strcasecmp(action, "whobbs")) {
586                 whobbs();
587                 }
588
589         else if (!strcasecmp(action, "knrooms")) {
590                 list_all_rooms_by_floor();
591                 }
592
593         else if (!strcasecmp(action, "gotonext")) {
594                 slrp_highest();
595                 gotonext();
596                 }
597
598         else if (!strcasecmp(action, "skip")) {
599                 gotonext();
600                 }
601
602         else if (!strcasecmp(action, "ungoto")) {
603                 ungoto();
604                 }
605
606         else if (!strcasecmp(action, "dotgoto")) {
607                 slrp_highest();
608                 dotgoto();
609                 }
610
611         else if (!strcasecmp(action, "termquit")) {
612                 do_logout();
613                 }
614
615         else if (!strcasecmp(action, "readnew")) {
616                 readloop("readnew");
617                 }
618
619         else if (!strcasecmp(action, "readold")) {
620                 readloop("readold");
621                 }
622
623         else if (!strcasecmp(action, "readfwd")) {
624                 readloop("readfwd");
625                 }
626
627         else if (!strcasecmp(action, "display_enter")) {
628                 display_enter();
629                 }
630
631         else if (!strcasecmp(action, "post")) {
632                 post_message();
633                 }
634
635         else if (!strcasecmp(action, "confirm_delete_msg")) {
636                 confirm_delete_msg();
637                 }
638
639         else if (!strcasecmp(action, "delete_msg")) {
640                 delete_msg();
641                 }
642
643         else if (!strcasecmp(action, "confirm_move_msg")) {
644                 confirm_move_msg();
645                 }
646
647         else if (!strcasecmp(action, "move_msg")) {
648                 move_msg();
649                 }
650
651         else if (!strcasecmp(action, "userlist")) {
652                 userlist();
653                 }
654
655         else if (!strcasecmp(action, "showuser")) {
656                 showuser();
657                 }
658
659         /* When all else fails... */
660         else {
661                 printf("HTTP/1.0 200 OK\n");
662                 output_headers(1);
663         
664                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
665                 wprintf("You're in session %d<HR>\n", wc_session);
666                 wprintf("Command: <BR><PRE>\n");
667                 escputs(cmd);
668                 wprintf("</PRE><HR>\n");
669                 wprintf("Variables: <BR><PRE>\n");
670                 dump_vars();
671                 wprintf("</PRE><HR>\n");
672                 wprintf("</BODY></HTML>\n");
673                 wDumpContent();
674                 }
675
676         fflush(stdout);
677         if (content != NULL) {
678                 free(content);
679                 content = NULL;
680                 }
681         free_urls();
682         }
683
684 int main(int argc, char *argv[]) {
685
686         if (argc < 2 || argc > 4) {
687                 fprintf(stderr,
688                         "webcit: usage: webcit <session_id> [host [port]]\n");
689                 return 1;
690                 }
691
692         wc_session = atoi(argv[1]);
693
694         if (argc > 2) {
695                 defaulthost = argv[2];
696                 if (argc > 3)
697                         defaultport = argv[3];
698                 }
699
700         strcpy(wc_host, "");
701         strcpy(wc_port, "");
702         strcpy(wc_username, "");
703         strcpy(wc_password, "");
704         strcpy(wc_roomname, "");
705
706         while (1) {
707                 session_loop();
708                 }
709         }