]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
Fixed the locks, though I'm not sure I did it correctly.
[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 if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
327                         printf("Content-type: text/html\n");
328                 else
329                         printf("Content-type: application/octet-stream\n");
330
331                 fstat(fileno(fp), &statbuf);
332                 bytes = statbuf.st_size;
333                 printf("Content-length: %d\n", bytes);
334                 printf("\n");
335                 while (bytes--) {
336                         putc(getc(fp), stdout);
337                         }
338                 fclose(fp);
339                 }
340         }
341
342
343 void session_loop() {
344         char cmd[256];
345         char buf[256];
346         int a, b;
347         int ContentLength = 0;
348         char *content;
349
350         /* We stuff these with the values coming from the client cookies,
351          * so we can use them to reconnect a timed out session if we have to.
352          */
353         char c_host[256];
354         char c_port[256];
355         char c_username[256];
356         char c_password[256];
357         char c_roomname[256];
358
359         strcpy(c_host, DEFAULT_HOST);
360         strcpy(c_port, DEFAULT_PORT);
361         strcpy(c_username, "");
362         strcpy(c_password, "");
363         strcpy(c_roomname, "");
364
365         getz(cmd);
366
367         do {
368                 getz(buf);
369
370                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
371                         strcpy(c_host, &buf[16]);
372                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
373                         strcpy(c_port, &buf[16]);
374                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
375                         strcpy(c_username, &buf[20]);
376                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
377                         strcpy(c_password, &buf[20]);
378                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
379                         strcpy(c_roomname, &buf[20]);
380
381                 if (!strncasecmp(buf, "Content-length: ", 16)) {
382                         ContentLength = atoi(&buf[16]);
383                         }
384
385                 } while(strlen(buf)>0);
386
387         ++TransactionCount;
388
389         if (ContentLength > 0) {
390                 content = malloc(ContentLength+1);
391                 fread(content, ContentLength, 1, stdin);
392                 content[ContentLength] = 0;
393                 addurls(content);
394                 }
395         else {
396                 content = NULL;
397                 }
398
399         /*
400          * If we're not connected to a Citadel server, try to hook up the
401          * connection now.  Preference is given to the host and port specified
402          * by browser cookies, if cookies have been supplied.
403          */
404         if (!connected) {
405                 serv_sock = connectsock(c_host, c_port, "tcp");
406                 connected = 1;
407                 serv_gets(buf); /* get the server welcome message */
408                 strcpy(wc_host, c_host);
409                 strcpy(wc_port, c_port);
410                 get_serv_info();
411                 }
412
413
414         /*
415          * If we're not logged in, but we have username and password cookies
416          * supplied by the browser, try using them to log in.
417          */
418         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
419                 serv_printf("USER %s", c_username);
420                 serv_gets(buf);
421                 if (buf[0]=='3') {
422                         serv_printf("PASS %s", c_password);
423                         serv_gets(buf);
424                         if (buf[0]=='2') {
425                                 become_logged_in(c_username, c_password, buf);
426                                 }
427                         }
428                 }
429
430         /*
431          * If we don't have a current room, but a cookie specifying the
432          * current room is supplied, make an effort to go there.
433          */
434         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
435                 serv_printf("GOTO %s", c_roomname);
436                 serv_gets(buf);
437                 if (buf[0]=='2') {
438                         strcpy(wc_roomname, c_roomname);
439                         }
440                 }
441
442         /* If there are variables in the URL, we must grab them now */  
443         for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
444                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
445                 addurls(&cmd[a+1]);
446                 cmd[a] = 0;
447                 }
448
449         if (!strncasecmp(cmd, "GET /static/", 12)) {
450                 strcpy(buf, &cmd[12]);
451                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
452                 output_static(buf);
453                 }
454
455
456         else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
457                 do_login();
458                 }
459
460         else if (!logged_in) {
461                 display_login_page();
462                 }
463
464         /* Various commands... */
465         
466         else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
467                 display_main_menu();
468                 }
469
470         else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
471                 whobbs();
472                 }
473
474         else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
475                 list_all_rooms_by_floor();
476                 }
477
478         else if (!strncasecmp(cmd, "GET /test", 9)) {
479                 printf("HTTP/1.0 200 OK\n");
480                 output_headers();
481         
482                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
483                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
484                 wprintf("You're in session %d<BR>\n", wc_session);
485                 wprintf("</BODY></HTML>\n");
486                 wDumpContent();
487                 }
488
489         /* When all else fails... */
490         else {
491                 printf("HTTP/1.0 200 OK\n");
492                 output_headers();
493         
494                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
495                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
496                 wprintf("You're in session %d<BR>\n", wc_session);
497                 wprintf("</BODY></HTML>\n");
498                 wDumpContent();
499                 }
500
501         fflush(stdout);
502         if (content != NULL) {
503                 free(content);
504                 content = NULL;
505                 }
506         free_urls();
507         }
508
509
510
511 int main(int argc, char *argv[]) {
512
513         if (argc != 2) {
514                 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
515                 exit(1);
516                 }
517
518         wc_session = atoi(argv[1]);
519         strcpy(wc_host, "");
520         strcpy(wc_port, "");
521         strcpy(wc_username, "");
522         strcpy(wc_password, "");
523         strcpy(wc_roomname, "");
524
525         while (1) {
526                 session_loop();
527                 }
528
529         exit(0);
530         }