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