Removed logging subsystem from webcit. It's all syslog now.
[citadel.git] / webcit / static.c
1 /*
2  * This is the main transaction loop of the web service.  It maintains a
3  * persistent session to the Citadel server, handling HTTP WebCit requests as
4  * they arrive and presenting a user interface.
5  */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <dirent.h>
9 #include <errno.h>
10
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 #include <stddef.h>
15
16
17 #include "webcit.h"
18 #include "webserver.h"
19
20
21 HashList *StaticFilemappings[4] = {NULL, NULL, NULL, NULL};
22 /*
23                 {
24                         syslog(9, "Suspicious request. Ignoring.");
25                         hprintf("HTTP/1.1 404 Security check failed\r\n");
26                         hprintf("Content-Type: text/plain\r\n\r\n");
27                         wc_printf("You have sent a malformed or invalid request.\r\n");
28                         end_burst();
29                 }
30 */
31 /*
32  * dump out static pages from disk
33  */
34 void output_static(const char *what)
35 {
36         int fd;
37         struct stat statbuf;
38         off_t bytes;
39         off_t count = 0;
40         const char *content_type;
41         int len;
42         const char *Err;
43
44         fd = open(what, O_RDONLY);
45         if (fd <= 0) {
46                 syslog(9, "output_static('%s') [%s]  -- NOT FOUND --\n", what, ChrPtr(WC->Hdr->this_page));
47                 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
48                 hprintf("Content-Type: text/plain\r\n");
49                 begin_burst();
50                 wc_printf("Cannot open %s: %s\r\n", what, strerror(errno));
51                 end_burst();
52         } else {
53                 len = strlen (what);
54                 content_type = GuessMimeByFilename(what, len);
55
56                 if (fstat(fd, &statbuf) == -1) {
57                         syslog(9, "output_static('%s')  -- FSTAT FAILED --\n", what);
58                         hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
59                         hprintf("Content-Type: text/plain\r\n");
60                         begin_burst();
61                         wc_printf("Cannot fstat %s: %s\n", what, strerror(errno));
62                         end_burst();
63                         if (fd > 0) close(fd);
64                         return;
65                 }
66
67                 count = 0;
68                 bytes = statbuf.st_size;
69
70                 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
71                 {
72                         if (fd > 0) close(fd);
73                         syslog(9, "output_static('%s')  -- FREAD FAILED (%s) --\n", what, strerror(errno));
74                                 hprintf("HTTP/1.1 500 internal server error \r\n");
75                                 hprintf("Content-Type: text/plain\r\n");
76                                 end_burst();
77                                 return;
78                 }
79
80
81                 close(fd);
82                 http_transmit_thing(content_type, 2);
83         }
84         if (yesbstr("force_close_session")) {
85                 end_webcit_session();
86         }
87 }
88
89
90 int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
91 {
92         char dirname[PATH_MAX];
93         char reldir[PATH_MAX];
94         StrBuf *FileName = NULL;
95         StrBuf *Dir = NULL;
96         StrBuf *WebDir = NULL;
97         StrBuf *OneWebName = NULL;
98         DIR *filedir = NULL;
99         struct dirent *d;
100         struct dirent *filedir_entry;
101         int d_type = 0;
102         int d_namelen;
103         int d_without_ext;
104         int istoplevel;
105                 
106         filedir = opendir (DirName);
107         if (filedir == NULL) {
108                 return 0;
109         }
110
111         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
112         if (d == NULL) {
113                 return 0;
114         }
115
116         Dir = NewStrBufPlain(DirName, -1);
117         WebDir = NewStrBufPlain(RelDir, -1);
118         istoplevel = IsEmptyStr(RelDir);
119         OneWebName = NewStrBuf();
120
121         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
122                (filedir_entry != NULL))
123         {
124                 char *PStart;
125 #ifdef _DIRENT_HAVE_D_NAMELEN
126                 d_namelen = filedir_entry->d_namelen;
127                 d_type = filedir_entry->d_type;
128 #else
129
130 #ifndef DT_UNKNOWN
131 #define DT_UNKNOWN     0
132 #define DT_DIR         4
133 #define DT_REG         8
134 #define DT_LNK         10
135
136 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
137 #define DTTOIF(dirtype)        ((dirtype) << 12)
138 #endif
139                 d_namelen = strlen(filedir_entry->d_name);
140                 d_type = DT_UNKNOWN;
141 #endif
142                 d_without_ext = d_namelen;
143
144                 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
145                         continue; /* Ignore backup files... */
146
147                 if ((d_namelen == 1) && 
148                     (filedir_entry->d_name[0] == '.'))
149                         continue;
150
151                 if ((d_namelen == 2) && 
152                     (filedir_entry->d_name[0] == '.') &&
153                     (filedir_entry->d_name[1] == '.'))
154                         continue;
155
156                 if (d_type == DT_UNKNOWN) {
157                         struct stat s;
158                         char path[PATH_MAX];
159                         snprintf(path, PATH_MAX, "%s/%s", 
160                                 DirName, filedir_entry->d_name);
161                         if (stat(path, &s) == 0) {
162                                 d_type = IFTODT(s.st_mode);
163                         }
164                 }
165
166                 switch (d_type)
167                 {
168                 case DT_DIR:
169                         /* Skip directories we are not interested in... */
170                         if ((strcmp(filedir_entry->d_name, ".svn") == 0) ||
171                             (strcmp(filedir_entry->d_name, "t") == 0))
172                                 break;
173                         snprintf(dirname, PATH_MAX, "%s/%s/", 
174                                  DirName, filedir_entry->d_name);
175                         if (istoplevel)
176                                 snprintf(reldir, PATH_MAX, "%s/", 
177                                          filedir_entry->d_name);
178                         else
179                                 snprintf(reldir, PATH_MAX, "%s/%s/", 
180                                          RelDir, filedir_entry->d_name);
181                         StripSlashes(dirname, 1);
182                         StripSlashes(reldir, 1);
183                         LoadStaticDir(dirname, DirList, reldir);                                 
184                         break;
185                 case DT_LNK: /* TODO: check whether its a file or a directory */
186                 case DT_REG:
187                         PStart = filedir_entry->d_name;
188                         FileName = NewStrBufDup(Dir);
189                         if (ChrPtr(FileName) [ StrLength(FileName) - 1] != '/')
190                                 StrBufAppendBufPlain(FileName, "/", 1, 0);
191                         StrBufAppendBufPlain(FileName, filedir_entry->d_name, d_namelen, 0);
192
193                         FlushStrBuf(OneWebName);
194                         StrBufAppendBuf(OneWebName, WebDir, 0);
195                         if ((StrLength(OneWebName) != 0) && 
196                             (ChrPtr(OneWebName) [ StrLength(OneWebName) - 1] != '/'))
197                                 StrBufAppendBufPlain(OneWebName, "/", 1, 0);
198                         StrBufAppendBufPlain(OneWebName, filedir_entry->d_name, d_namelen, 0);
199
200                         Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
201                         /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
202                         break;
203                 default:
204                         break;
205                 }
206
207
208         }
209         free(d);
210         closedir(filedir);
211         FreeStrBuf(&Dir);
212         FreeStrBuf(&WebDir);
213         FreeStrBuf(&OneWebName);
214         return 1;
215 }
216
217
218 void output_flat_static(void)
219 {
220         wcsession *WCC = WC;
221         void *vFile;
222         StrBuf *File;
223
224         if (WCC->Hdr->HR.Handler == NULL)
225                 return;
226         if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
227             (vFile != NULL))
228         {
229                 File = (StrBuf*) vFile;
230                 output_static(ChrPtr(vFile));
231         }
232 }
233
234 extern void do_404(void);
235
236 void output_static_safe(HashList *DirList)
237 {
238         wcsession *WCC = WC;
239         void *vFile;
240         StrBuf *File;
241
242         if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
243             (vFile != NULL))
244         {
245                 File = (StrBuf*) vFile;
246                 output_static(ChrPtr(vFile));
247         }
248         else {
249                 syslog(1, "output_static_safe() file %s not found. \n", 
250                         ChrPtr(WCC->Hdr->HR.ReqLine));
251 ///TODO: detect image & output blank image
252                 do_404();
253         }
254 }
255 void output_static_0(void)
256 {
257         output_static_safe(StaticFilemappings[0]);
258 }
259 void output_static_1(void)
260 {
261         output_static_safe(StaticFilemappings[1]);
262 }
263 void output_static_2(void)
264 {
265         output_static_safe(StaticFilemappings[2]);
266 }
267 void output_static_3(void)
268 {
269         output_static_safe(StaticFilemappings[3]);
270 }
271
272
273 /*
274  * robots.txt
275  */
276 void robots_txt(void) {
277         output_headers(0, 0, 0, 0, 0, 0);
278
279         hprintf("Content-type: text/plain\r\n"
280                 "Server: %s\r\n"
281                 "Connection: close\r\n",
282                 PACKAGE_STRING);
283         begin_burst();
284
285         wc_printf("User-agent: *\r\n"
286                 "Disallow:\r\n"
287                 "Sitemap: %s/sitemap.xml\r\n"
288                 "\r\n"
289                 ,
290                 ChrPtr(site_prefix)
291         );
292
293         wDumpContent(0);
294 }
295
296
297 void 
298 ServerStartModule_STATIC
299 (void)
300 {
301         StaticFilemappings[0] = NewHash(1, NULL);
302         StaticFilemappings[1] = NewHash(1, NULL);
303         StaticFilemappings[2] = NewHash(1, NULL);
304         StaticFilemappings[3] = NewHash(1, NULL);
305 }
306 void 
307 ServerShutdownModule_STATIC
308 (void)
309 {
310         DeleteHash(&StaticFilemappings[0]);
311         DeleteHash(&StaticFilemappings[1]);
312         DeleteHash(&StaticFilemappings[2]);
313         DeleteHash(&StaticFilemappings[3]);
314 }
315
316
317 void 
318 InitModule_STATIC
319 (void)
320 {
321         LoadStaticDir(static_dirs[0], StaticFilemappings[0], "");
322         LoadStaticDir(static_dirs[1], StaticFilemappings[1], "");
323         LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
324         LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
325
326         WebcitAddUrlHandler(HKEY("robots.txt"), "", 0, robots_txt, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
327         WebcitAddUrlHandler(HKEY("favicon.ico"), "", 0, output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
328         WebcitAddUrlHandler(HKEY("static"), "", 0, output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
329         WebcitAddUrlHandler(HKEY("static.local"), "", 0, output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
330         WebcitAddUrlHandler(HKEY("tinymce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
331         WebcitAddUrlHandler(HKEY("tiny_mce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
332 }