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