]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
Set up an option in output_headers() to optionally print the most
[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)+1);
106                 strcpy(u->url_data, up);
107                 u->url_data[b] = 0;
108                 unescape_input(u->url_data);
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 void getz(char *buf) {
249         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
250         else {
251                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
252                         buf[strlen(buf)-1] = 0;
253                 }
254         }
255
256 /*
257  * Output all that important stuff that the browser will want to see
258  */
259 void output_headers(int print_standard_html_head) {
260
261         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
262
263         printf("Server: %s\n", SERVER);
264         printf("Connection: close\n");
265         printf("Set-cookie: wc_session=%d\n", wc_session);
266
267         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
268         else printf("Set-cookie: wc_host=%s\n", unset);
269
270         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
271         else printf("Set-cookie: wc_port=%s\n", unset);
272
273         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
274                 wc_username);
275         else printf("Set-cookie: wc_username=%s\n", unset);
276
277         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
278                 wc_password);
279         else printf("Set-cookie: wc_password=%s\n", unset);
280
281         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
282                 wc_roomname);
283         else printf("Set-cookie: wc_roomname=%s\n", unset);
284
285         if (print_standard_html_head) {
286                 wprintf("<HTML><HEAD><TITLE>");
287                 escputs("WebCit");
288                 wprintf("</TITLE></HEAD>");
289                 wprintf("<BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
290                 }
291
292         }
293
294 void output_static(char *what) {
295         char buf[256];
296         FILE *fp;
297         struct stat statbuf;
298         off_t bytes;
299
300         sprintf(buf, "static/%s", what);
301         fp = fopen(buf, "rb");
302         if (fp == NULL) {
303                 printf("HTTP/1.0 404 %s\n", strerror(errno));
304                 output_headers(0);
305                 printf("Content-Type: text/plain\n");
306                 sprintf(buf, "%s: %s\n", what, strerror(errno));
307                 printf("Content-length: %d\n", strlen(buf));
308                 printf("\n");
309                 fwrite(buf, strlen(buf), 1, stdout);
310                 }
311         else {
312                 printf("HTTP/1.0 200 OK\n");
313                 output_headers(0);
314
315                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
316                         printf("Content-type: image/gif\n");
317                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
318                         printf("Content-type: text/html\n");
319                 else
320                         printf("Content-type: application/octet-stream\n");
321
322                 fstat(fileno(fp), &statbuf);
323                 bytes = statbuf.st_size;
324                 printf("Content-length: %ld\n", (long)bytes);
325                 printf("\n");
326                 while (bytes--) {
327                         putc(getc(fp), stdout);
328                         }
329                 fflush(stdout);
330                 fclose(fp);
331                 }
332         }
333
334 void output_image() {
335         char buf[256];
336         char xferbuf[4096];
337         off_t bytes;
338         off_t thisblock;
339         off_t accomplished = 0L;
340
341
342         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
343         serv_gets(buf);
344         if (buf[0]=='2') {
345                 bytes = extract_long(&buf[4], 0);
346                 printf("HTTP/1.0 200 OK\n");
347                 output_headers(0);
348                 printf("Content-type: image/gif\n");
349                 printf("Content-length: %ld\n", bytes);
350                 printf("\n");
351
352                 while (bytes > (off_t)0) {
353                         thisblock = (off_t)sizeof(xferbuf);
354                         if (thisblock > bytes) thisblock = bytes;
355                         serv_printf("READ %ld|%ld", accomplished, thisblock);
356                         serv_gets(buf);
357                         if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
358                         serv_read(xferbuf, (int)thisblock);
359                         fwrite(xferbuf, thisblock, 1, stdout);
360                         bytes = bytes - thisblock;
361                         accomplished = accomplished + thisblock;
362                         }
363                 fflush(stdout);
364                 serv_puts("CLOS");
365                 serv_gets(buf);
366                 }
367         else {
368                 printf("HTTP/1.0 404 %s\n", strerror(errno));
369                 output_headers(0);
370                 printf("Content-Type: text/plain\n");
371                 sprintf(buf, "Error retrieving image\n");
372                 printf("Content-length: %d\n", strlen(buf));
373                 printf("\n");
374                 fwrite(buf, strlen(buf), 1, stdout);
375                 }
376
377         }
378
379 void extract_action(char *actbuf, char *cmdbuf) {
380         int i;
381
382         strcpy(actbuf, cmdbuf);
383         if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
384         if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
385         if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
386
387         for (i=0; i<strlen(actbuf); ++i) {
388                 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
389                 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
390                 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
391                 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
392                 }
393         }
394
395
396 void session_loop(void) {
397         char cmd[256];
398         char action[256];
399         char buf[256];
400         int a, b;
401         int ContentLength = 0;
402         char *content;
403 FILE *fp;
404
405         /* We stuff these with the values coming from the client cookies,
406          * so we can use them to reconnect a timed out session if we have to.
407          */
408         char c_host[256];
409         char c_port[256];
410         char c_username[256];
411         char c_password[256];
412         char c_roomname[256];
413
414         strcpy(c_host, defaulthost);
415         strcpy(c_port, defaultport);
416         strcpy(c_username, "");
417         strcpy(c_password, "");
418         strcpy(c_roomname, "");
419
420         getz(cmd);
421         extract_action(action, cmd);
422
423         do {
424                 getz(buf);
425
426                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
427                         strcpy(c_host, &buf[16]);
428                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
429                         strcpy(c_port, &buf[16]);
430                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
431                         strcpy(c_username, &buf[20]);
432                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
433                         strcpy(c_password, &buf[20]);
434                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
435                         strcpy(c_roomname, &buf[20]);
436
437                 if (!strncasecmp(buf, "Content-length: ", 16)) {
438                         ContentLength = atoi(&buf[16]);
439                         }
440
441                 } while(strlen(buf)>0);
442
443         ++TransactionCount;
444
445         if (ContentLength > 0) {
446                 content = malloc(ContentLength+1);
447                 fread(content, ContentLength, 1, stdin);
448 fp = fopen("content", "wb");
449 fwrite(content, ContentLength, 1, fp);
450 fclose(fp);
451                 content[ContentLength] = 0;
452                 addurls(content);
453                 }
454         else {
455                 content = NULL;
456                 }
457
458         /*
459          * If we're not connected to a Citadel server, try to hook up the
460          * connection now.  Preference is given to the host and port specified
461          * by browser cookies, if cookies have been supplied.
462          */
463         if (!connected) {
464                 serv_sock = connectsock(c_host, c_port, "tcp");
465                 connected = 1;
466                 serv_gets(buf); /* get the server welcome message */
467                 strcpy(wc_host, c_host);
468                 strcpy(wc_port, c_port);
469                 get_serv_info();
470                 }
471
472
473         /*
474          * If we're not logged in, but we have username and password cookies
475          * supplied by the browser, try using them to log in.
476          */
477         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
478                 serv_printf("USER %s", c_username);
479                 serv_gets(buf);
480                 if (buf[0]=='3') {
481                         serv_printf("PASS %s", c_password);
482                         serv_gets(buf);
483                         if (buf[0]=='2') {
484                                 become_logged_in(c_username, c_password, buf);
485                                 }
486                         }
487                 }
488
489         /*
490          * If we don't have a current room, but a cookie specifying the
491          * current room is supplied, make an effort to go there.
492          */
493         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
494                 serv_printf("GOTO %s", c_roomname);
495                 serv_gets(buf);
496                 if (buf[0]=='2') {
497                         strcpy(wc_roomname, c_roomname);
498                         }
499                 }
500
501         /* If there are variables in the URL, we must grab them now */  
502         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
503                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
504                 addurls(&cmd[a+1]);
505                 cmd[a] = 0;
506                 }
507
508         if (!strcasecmp(action, "static")) {
509                 strcpy(buf, &cmd[12]);
510                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
511                 output_static(buf);
512                 }
513
514         else if (!strcasecmp(action, "image")) {
515                 output_image();
516                 }
517
518         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
519                 do_login();
520                 }
521
522         else if (!logged_in) {
523                 display_login(NULL);
524                 }
525
526         /* Various commands... */
527         
528         else if (!strcasecmp(action, "do_welcome")) {
529                 do_welcome();
530                 }
531
532         else if (!strcasecmp(action, "display_main_menu")) {
533                 display_main_menu();
534                 }
535
536         else if (!strcasecmp(action, "advanced")) {
537                 display_advanced_menu();
538                 }
539
540         else if (!strcasecmp(action, "whobbs")) {
541                 whobbs();
542                 }
543
544         else if (!strcasecmp(action, "knrooms")) {
545                 list_all_rooms_by_floor();
546                 }
547
548         else if (!strcasecmp(action, "gotonext")) {
549                 slrp_highest();
550                 gotonext();
551                 }
552
553         else if (!strcasecmp(action, "skip")) {
554                 gotonext();
555                 }
556
557         else if (!strcasecmp(action, "ungoto")) {
558                 ungoto();
559                 }
560
561         else if (!strcasecmp(action, "dotgoto")) {
562                 slrp_highest();
563                 dotgoto();
564                 }
565
566         else if (!strcasecmp(action, "termquit")) {
567                 do_logout();
568                 }
569
570         else if (!strcasecmp(action, "readnew")) {
571                 readloop("readnew");
572                 }
573
574         else if (!strcasecmp(action, "readold")) {
575                 readloop("readold");
576                 }
577
578         else if (!strcasecmp(action, "readfwd")) {
579                 readloop("readfwd");
580                 }
581
582         else if (!strcasecmp(action, "display_enter")) {
583                 display_enter();
584                 }
585
586         else if (!strcasecmp(action, "post")) {
587                 post_message();
588                 }
589
590         else if (!strcasecmp(action, "confirm_delete_msg")) {
591                 confirm_delete_msg();
592                 }
593
594         else if (!strcasecmp(action, "delete_msg")) {
595                 delete_msg();
596                 }
597
598         else if (!strcasecmp(action, "confirm_move_msg")) {
599                 confirm_move_msg();
600                 }
601
602         else if (!strcasecmp(action, "move_msg")) {
603                 move_msg();
604                 }
605
606         else if (!strcasecmp(action, "userlist")) {
607                 userlist();
608                 }
609
610         else if (!strcasecmp(action, "showuser")) {
611                 showuser();
612                 }
613
614         /* When all else fails... */
615         else {
616                 printf("HTTP/1.0 200 OK\n");
617                 output_headers(1);
618         
619                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
620                 wprintf("You're in session %d<HR>\n", wc_session);
621                 wprintf("Command: <BR><PRE>\n");
622                 escputs(cmd);
623                 wprintf("</PRE><HR>\n");
624                 wprintf("Variables: <BR><PRE>\n");
625                 dump_vars();
626                 wprintf("</PRE><HR>\n");
627                 wprintf("</BODY></HTML>\n");
628                 wDumpContent();
629                 }
630
631         fflush(stdout);
632         if (content != NULL) {
633                 free(content);
634                 content = NULL;
635                 }
636         free_urls();
637         }
638
639 int main(int argc, char *argv[]) {
640
641         if (argc < 2 || argc > 4) {
642                 fprintf(stderr,
643                         "webcit: usage: webcit <session_id> [host [port]]\n");
644                 return 1;
645                 }
646
647         wc_session = atoi(argv[1]);
648
649         if (argc > 2) {
650                 defaulthost = argv[2];
651                 if (argc > 3)
652                         defaultport = argv[3];
653                 }
654
655         strcpy(wc_host, "");
656         strcpy(wc_port, "");
657         strcpy(wc_username, "");
658         strcpy(wc_password, "");
659         strcpy(wc_roomname, "");
660
661         while (1) {
662                 session_loop();
663                 }
664         }