]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Smoothed out some of the login/logout code. Failed authentication
[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(void) {
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 int wContentLength(void) {
164         struct webcontent *wptr;
165         int len = 0;
166
167         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
168                 len = len + strlen(wptr->w_data);
169                 }
170
171         return(len);
172         }
173
174 void wDumpContent(void) {
175         struct webcontent *wptr;
176
177         printf("Content-type: text/html\n");
178         printf("Content-length: %d\n", wContentLength());
179         printf("\n");
180
181         while (wlist != NULL) {
182                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
183                 wptr = wlist->next;
184                 free(wlist);
185                 wlist = wptr;
186                 }
187         wlast = NULL;
188         }
189
190
191 void escputs1(char *strbuf, int nbsp)
192 {
193         int a;
194
195         for (a=0; a<strlen(strbuf); ++a) {
196                 if (strbuf[a]=='<') wprintf("&lt;");
197                 else if (strbuf[a]=='>') wprintf("&gt;");
198                 else if (strbuf[a]=='&') wprintf("&amp;");
199                 else if (strbuf[a]==34) wprintf("&quot;");
200                 else if (strbuf[a]==LB) wprintf("<");
201                 else if (strbuf[a]==RB) wprintf(">");
202                 else if (strbuf[a]==QU) wprintf("\"");
203                 else if ((strbuf[a]==32)&&(nbsp==1)) {
204                         wprintf("&nbsp;");
205                         }
206                 else {
207                         wprintf("%c", strbuf[a]);
208                         }
209                 }
210         }
211
212 void escputs(char *strbuf)
213 {
214         escputs1(strbuf,0);
215         }
216
217
218
219 char *urlesc(char *strbuf)
220 {
221         int a,b,c;
222         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
223         static char outbuf[512];
224         
225         strcpy(outbuf,"");
226
227         for (a=0; a<strlen(strbuf); ++a) {
228                 c = 0;
229                 for (b=0; b<strlen(ec); ++b) {
230                         if (strbuf[a]==ec[b]) c=1;
231                         }
232                 b = strlen(outbuf);
233                 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
234                 else sprintf(&outbuf[b],"%c",strbuf[a]);
235                 }
236         return(outbuf);
237         }
238
239 void urlescputs(char *strbuf)
240 {
241         wprintf("%s",urlesc(strbuf));
242         }
243
244
245 /*
246  * Look for URL's embedded in a buffer and make them linkable.  We use a
247  * target window in order to keep the BBS session in its own window.
248  */
249 void url(char *buf)
250 {
251
252         int pos;
253         int start,end;
254         char ench;
255         char urlbuf[256];
256         char outbuf[256];
257
258         start = (-1);
259         end = strlen(buf);
260         ench = 0;
261
262         for (pos=0; pos<strlen(buf); ++pos) {
263                 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
264                 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
265                 }
266
267         if (start<0) return;
268
269         if ((start>0)&&(buf[start-1]=='<')) ench = '>';
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
274         for (pos=strlen(buf); pos>start; --pos) {
275                 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
276                 }
277
278         strncpy(urlbuf,&buf[start],end-start);
279         urlbuf[end-start] = 0;
280
281
282         strncpy(outbuf,buf,start);
283         sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c", 
284                 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
285         strcat(outbuf,&buf[end]);
286         strcpy(buf,outbuf);
287         }
288
289
290
291
292 void getz(char *buf) {
293         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
294         else {
295                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
296                         buf[strlen(buf)-1] = 0;
297                 }
298         }
299
300 /*
301  * Output all that important stuff that the browser will want to see
302  */
303 void output_headers(void) {
304
305         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
306
307         printf("Server: %s\n", SERVER);
308         printf("Connection: close\n");
309         printf("Set-cookie: wc_session=%d\n", wc_session);
310
311         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
312         else printf("Set-cookie: wc_host=%s\n", unset);
313
314         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
315         else printf("Set-cookie: wc_port=%s\n", unset);
316
317         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
318                 wc_username);
319         else printf("Set-cookie: wc_username=%s\n", unset);
320
321         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
322                 wc_password);
323         else printf("Set-cookie: wc_password=%s\n", unset);
324
325         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
326                 wc_roomname);
327         else printf("Set-cookie: wc_roomname=%s\n", unset);
328         }
329
330 void output_static(char *what) {
331         char buf[256];
332         FILE *fp;
333         struct stat statbuf;
334         off_t bytes;
335
336         fprintf(stderr, "output_static(%s)\n", what);
337         sprintf(buf, "static/%s", what);
338         fp = fopen(buf, "rb");
339         if (fp == NULL) {
340                 printf("HTTP/1.0 404 %s\n", strerror(errno));
341                 output_headers();
342                 printf("Content-Type: text/plain\n");
343                 sprintf(buf, "%s: %s\n", what, strerror(errno));
344                 printf("Content-length: %d\n", strlen(buf));
345                 printf("\n");
346                 fwrite(buf, strlen(buf), 1, stdout);
347                 }
348         else {
349                 fprintf(stderr, "opened it ok\n");
350                 printf("HTTP/1.0 200 OK\n");
351                 output_headers();
352
353                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
354                         printf("Content-type: image/gif\n");
355                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
356                         printf("Content-type: text/html\n");
357                 else
358                         printf("Content-type: application/octet-stream\n");
359
360                 fstat(fileno(fp), &statbuf);
361                 bytes = statbuf.st_size;
362                 printf("Content-length: %ld\n", (long)bytes);
363                 printf("\n");
364                 while (bytes--) {
365                         putc(getc(fp), stdout);
366                         }
367                 fflush(stdout);
368                 fclose(fp);
369                 }
370         }
371
372 static const char *defaulthost = DEFAULT_HOST;
373 static const char *defaultport = DEFAULT_PORT;
374
375
376 void extract_action(char *actbuf, char *cmdbuf) {
377         int i;
378
379         strcpy(actbuf, cmdbuf);
380         if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
381         if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
382         if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
383
384         for (i=0; i<strlen(actbuf); ++i) {
385                 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
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                 }
390         }
391
392
393 void session_loop(void) {
394         char cmd[256];
395         char action[256];
396         char buf[256];
397         int a, b;
398         int ContentLength = 0;
399         char *content;
400
401         /* We stuff these with the values coming from the client cookies,
402          * so we can use them to reconnect a timed out session if we have to.
403          */
404         char c_host[256];
405         char c_port[256];
406         char c_username[256];
407         char c_password[256];
408         char c_roomname[256];
409
410         strcpy(c_host, defaulthost);
411         strcpy(c_port, defaultport);
412         strcpy(c_username, "");
413         strcpy(c_password, "");
414         strcpy(c_roomname, "");
415
416         getz(cmd);
417         extract_action(action, cmd);
418
419         do {
420                 getz(buf);
421
422                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
423                         strcpy(c_host, &buf[16]);
424                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
425                         strcpy(c_port, &buf[16]);
426                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
427                         strcpy(c_username, &buf[20]);
428                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
429                         strcpy(c_password, &buf[20]);
430                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
431                         strcpy(c_roomname, &buf[20]);
432
433                 if (!strncasecmp(buf, "Content-length: ", 16)) {
434                         ContentLength = atoi(&buf[16]);
435                         }
436
437                 } while(strlen(buf)>0);
438
439         ++TransactionCount;
440
441         if (ContentLength > 0) {
442                 content = malloc(ContentLength+1);
443                 fread(content, ContentLength, 1, stdin);
444                 content[ContentLength] = 0;
445                 addurls(content);
446                 }
447         else {
448                 content = NULL;
449                 }
450
451         /*
452          * If we're not connected to a Citadel server, try to hook up the
453          * connection now.  Preference is given to the host and port specified
454          * by browser cookies, if cookies have been supplied.
455          */
456         if (!connected) {
457                 serv_sock = connectsock(c_host, c_port, "tcp");
458                 connected = 1;
459                 serv_gets(buf); /* get the server welcome message */
460                 strcpy(wc_host, c_host);
461                 strcpy(wc_port, c_port);
462                 get_serv_info();
463                 }
464
465
466         /*
467          * If we're not logged in, but we have username and password cookies
468          * supplied by the browser, try using them to log in.
469          */
470         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
471                 serv_printf("USER %s", c_username);
472                 serv_gets(buf);
473                 if (buf[0]=='3') {
474                         serv_printf("PASS %s", c_password);
475                         serv_gets(buf);
476                         if (buf[0]=='2') {
477                                 become_logged_in(c_username, c_password, buf);
478                                 }
479                         }
480                 }
481
482         /*
483          * If we don't have a current room, but a cookie specifying the
484          * current room is supplied, make an effort to go there.
485          */
486         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
487                 serv_printf("GOTO %s", c_roomname);
488                 serv_gets(buf);
489                 if (buf[0]=='2') {
490                         strcpy(wc_roomname, c_roomname);
491                         }
492                 }
493
494         /* If there are variables in the URL, we must grab them now */  
495         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
496                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
497                 addurls(&cmd[a+1]);
498                 cmd[a] = 0;
499                 }
500
501         if (!strcasecmp(action, "static")) {
502                 strcpy(buf, &cmd[12]);
503                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
504                 output_static(buf);
505                 }
506
507         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
508                 do_login();
509                 }
510
511         else if (!logged_in) {
512                 display_login(NULL);
513                 }
514
515         /* Various commands... */
516         
517         else if (!strcasecmp(action, "do_welcome")) {
518                 do_welcome();
519                 }
520
521         else if (!strcasecmp(action, "display_main_menu")) {
522                 display_main_menu();
523                 }
524
525         else if (!strcasecmp(action, "advanced")) {
526                 display_advanced_menu();
527                 }
528
529         else if (!strcasecmp(action, "whobbs")) {
530                 whobbs();
531                 }
532
533         else if (!strcasecmp(action, "knrooms")) {
534                 list_all_rooms_by_floor();
535                 }
536
537         else if (!strcasecmp(action, "gotonext")) {
538                 slrp_highest();
539                 gotonext();
540                 }
541
542         else if (!strcasecmp(action, "skip")) {
543                 gotonext();
544                 }
545
546         else if (!strcasecmp(action, "ungoto")) {
547                 ungoto();
548                 }
549
550         else if (!strcasecmp(action, "dotgoto")) {
551                 slrp_highest();
552                 dotgoto();
553                 }
554
555         else if (!strcasecmp(action, "termquit")) {
556                 do_logout();
557                 }
558
559         /* When all else fails... */
560         else {
561                 printf("HTTP/1.0 200 OK\n");
562                 output_headers();
563         
564                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
565                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
566                 wprintf("You're in session %d<HR>\n", wc_session);
567                 wprintf("Command: <BR><PRE>\n");
568                 escputs(cmd);
569                 wprintf("</PRE><HR>\n");
570                 wprintf("Variables: <BR><PRE>\n");
571                 dump_vars();
572                 wprintf("</PRE><HR>\n");
573                 wprintf("</BODY></HTML>\n");
574                 wDumpContent();
575                 }
576
577         fflush(stdout);
578         if (content != NULL) {
579                 free(content);
580                 content = NULL;
581                 }
582         free_urls();
583         }
584
585 int main(int argc, char *argv[]) {
586
587         if (argc < 2 || argc > 4) {
588                 fprintf(stderr,
589                         "webcit: usage: webcit <session_id> [host [port]]\n");
590                 return 1;
591                 }
592
593         wc_session = atoi(argv[1]);
594
595         if (argc > 2) {
596                 defaulthost = argv[2];
597                 if (argc > 3)
598                         defaultport = argv[3];
599                 }
600
601         strcpy(wc_host, "");
602         strcpy(wc_port, "");
603         strcpy(wc_username, "");
604         strcpy(wc_password, "");
605         strcpy(wc_roomname, "");
606
607         while (1) {
608                 session_loop();
609                 }
610         }