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