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