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