]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
Fixed broken interpretation of variables specified in the URL
[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                 fprintf(stderr, "%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         printf("Server: %s\n", SERVER);
306         printf("Connection: close\n");
307         printf("Set-cookie: wc_session=%d\n", wc_session);
308         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
309         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
310         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
311                 wc_username);
312         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
313                 wc_password);
314         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
315                 wc_roomname);
316         }
317
318 void output_static(char *what) {
319         char buf[256];
320         FILE *fp;
321         struct stat statbuf;
322         off_t bytes;
323
324         sprintf(buf, "static/%s", what);
325         fp = fopen(buf, "rb");
326         if (fp == NULL) {
327                 printf("HTTP/1.0 404 %s\n", strerror(errno));
328                 output_headers();
329                 printf("Content-Type: text/plain\n");
330                 sprintf(buf, "%s: %s\n", what, strerror(errno));
331                 printf("Content-length: %d\n", strlen(buf));
332                 printf("\n");
333                 fwrite(buf, strlen(buf), 1, stdout);
334                 }
335         else {
336                 printf("HTTP/1.0 200 OK\n");
337                 output_headers();
338
339                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
340                         printf("Content-type: image/gif\n");
341                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
342                         printf("Content-type: text/html\n");
343                 else
344                         printf("Content-type: application/octet-stream\n");
345
346                 fstat(fileno(fp), &statbuf);
347                 bytes = statbuf.st_size;
348                 printf("Content-length: %ld\n", (long)bytes);
349                 printf("\n");
350                 while (bytes--) {
351                         putc(getc(fp), stdout);
352                         }
353                 fclose(fp);
354                 }
355         }
356
357 static const char *defaulthost = DEFAULT_HOST;
358 static const char *defaultport = DEFAULT_PORT;
359
360 void session_loop(void) {
361         char cmd[256];
362         char buf[256];
363         int a, b;
364         int ContentLength = 0;
365         char *content;
366
367         /* We stuff these with the values coming from the client cookies,
368          * so we can use them to reconnect a timed out session if we have to.
369          */
370         char c_host[256];
371         char c_port[256];
372         char c_username[256];
373         char c_password[256];
374         char c_roomname[256];
375
376         strcpy(c_host, defaulthost);
377         strcpy(c_port, defaultport);
378         strcpy(c_username, "");
379         strcpy(c_password, "");
380         strcpy(c_roomname, "");
381
382         getz(cmd);
383
384         do {
385                 getz(buf);
386
387                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
388                         strcpy(c_host, &buf[16]);
389                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
390                         strcpy(c_port, &buf[16]);
391                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
392                         strcpy(c_username, &buf[20]);
393                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
394                         strcpy(c_password, &buf[20]);
395                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
396                         strcpy(c_roomname, &buf[20]);
397
398                 if (!strncasecmp(buf, "Content-length: ", 16)) {
399                         ContentLength = atoi(&buf[16]);
400                         }
401
402                 } while(strlen(buf)>0);
403
404         ++TransactionCount;
405
406         if (ContentLength > 0) {
407                 content = malloc(ContentLength+1);
408                 fread(content, ContentLength, 1, stdin);
409                 content[ContentLength] = 0;
410                 addurls(content);
411                 }
412         else {
413                 content = NULL;
414                 }
415
416         /*
417          * If we're not connected to a Citadel server, try to hook up the
418          * connection now.  Preference is given to the host and port specified
419          * by browser cookies, if cookies have been supplied.
420          */
421         if (!connected) {
422                 serv_sock = connectsock(c_host, c_port, "tcp");
423                 connected = 1;
424                 serv_gets(buf); /* get the server welcome message */
425                 strcpy(wc_host, c_host);
426                 strcpy(wc_port, c_port);
427                 get_serv_info();
428                 }
429
430
431         /*
432          * If we're not logged in, but we have username and password cookies
433          * supplied by the browser, try using them to log in.
434          */
435         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
436                 serv_printf("USER %s", c_username);
437                 serv_gets(buf);
438                 if (buf[0]=='3') {
439                         serv_printf("PASS %s", c_password);
440                         serv_gets(buf);
441                         if (buf[0]=='2') {
442                                 become_logged_in(c_username, c_password, buf);
443                                 }
444                         }
445                 }
446
447         /*
448          * If we don't have a current room, but a cookie specifying the
449          * current room is supplied, make an effort to go there.
450          */
451         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
452                 serv_printf("GOTO %s", c_roomname);
453                 serv_gets(buf);
454                 if (buf[0]=='2') {
455                         strcpy(wc_roomname, c_roomname);
456                         }
457                 }
458
459         /* If there are variables in the URL, we must grab them now */  
460         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
461                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
462                 addurls(&cmd[a+1]);
463                 cmd[a] = 0;
464                 }
465
466         /* Verbose but informative; uncomment if you want to trace variables */
467         /* dump_vars(); */
468
469         if (!strncasecmp(cmd, "GET /static/", 12)) {
470                 strcpy(buf, &cmd[12]);
471                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
472                 output_static(buf);
473                 }
474
475         else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
476                 do_login();
477                 }
478
479         else if (!logged_in) {
480                 display_login();
481                 }
482
483         /* Various commands... */
484         
485         else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
486                 do_welcome();
487                 }
488
489         else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
490                 display_main_menu();
491                 }
492
493         else if (!strncasecmp(cmd, "GET /advanced", 13)) {
494                 display_advanced_menu();
495                 }
496
497         else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
498                 whobbs();
499                 }
500
501         else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
502                 list_all_rooms_by_floor();
503                 }
504
505         else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
506                 slrp_highest();
507                 gotonext();
508                 }
509
510         else if (!strncasecmp(cmd, "GET /skip", 9)) {
511                 gotonext();
512                 }
513
514         else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
515                 ungoto();
516                 }
517
518         else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
519                 slrp_highest();
520                 dotgoto();
521                 }
522
523         else if (!strncasecmp(cmd, "GET /termquit", 13)) {
524                 do_logout();
525                 }
526
527         /* When all else fails... */
528         else {
529                 printf("HTTP/1.0 200 OK\n");
530                 output_headers();
531         
532                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
533                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
534                 wprintf("You're in session %d<BR>\n", wc_session);
535                 wprintf("</BODY></HTML>\n");
536                 wDumpContent();
537                 }
538
539         fflush(stdout);
540         if (content != NULL) {
541                 free(content);
542                 content = NULL;
543                 }
544         free_urls();
545         }
546
547 int main(int argc, char *argv[]) {
548
549         if (argc < 2 || argc > 4) {
550                 fprintf(stderr,
551                         "webcit: usage: webcit <session_id> [host [port]]\n");
552                 return 1;
553                 }
554
555         wc_session = atoi(argv[1]);
556
557         if (argc > 2) {
558                 defaulthost = argv[2];
559                 if (argc > 3)
560                         defaultport = argv[3];
561                 }
562
563         strcpy(wc_host, "");
564         strcpy(wc_port, "");
565         strcpy(wc_username, "");
566         strcpy(wc_password, "");
567         strcpy(wc_roomname, "");
568
569         while (1) {
570                 session_loop();
571                 }
572         }