clean up directory listing code, only iterate over the set once (by sorting directori...
[project/uhttpd.git] / file.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19
20 #define _BSD_SOURCE
21 #define _DARWIN_C_SOURCE
22 #define _XOPEN_SOURCE 700
23
24 #include <sys/types.h>
25 #include <sys/dir.h>
26 #include <time.h>
27 #include <strings.h>
28 #include <dirent.h>
29
30 #include <libubox/blobmsg.h>
31
32 #include "uhttpd.h"
33 #include "mimetypes.h"
34
35 static LIST_HEAD(index_files);
36 static LIST_HEAD(dispatch_handlers);
37
38 struct index_file {
39         struct list_head list;
40         const char *name;
41 };
42
43 enum file_hdr {
44         HDR_AUTHORIZATION,
45         HDR_IF_MODIFIED_SINCE,
46         HDR_IF_UNMODIFIED_SINCE,
47         HDR_IF_MATCH,
48         HDR_IF_NONE_MATCH,
49         HDR_IF_RANGE,
50         __HDR_MAX
51 };
52
53 void uh_index_add(const char *filename)
54 {
55         struct index_file *idx;
56
57         idx = calloc(1, sizeof(*idx));
58         idx->name = filename;
59         list_add_tail(&idx->list, &index_files);
60 }
61
62 static char * canonpath(const char *path, char *path_resolved)
63 {
64         const char *path_cpy = path;
65         char *path_res = path_resolved;
66
67         if (conf.no_symlinks)
68                 return realpath(path, path_resolved);
69
70         /* normalize */
71         while ((*path_cpy != '\0') && (path_cpy < (path + PATH_MAX - 2))) {
72                 if (*path_cpy != '/')
73                         goto next;
74
75                 /* skip repeating / */
76                 if (path_cpy[1] == '/') {
77                         path_cpy++;
78                         continue;
79                 }
80
81                 /* /./ or /../ */
82                 if (path_cpy[1] == '.') {
83                         /* skip /./ */
84                         if ((path_cpy[2] == '/') || (path_cpy[2] == '\0')) {
85                                 path_cpy += 2;
86                                 continue;
87                         }
88
89                         /* collapse /x/../ */
90                         if ((path_cpy[2] == '.') &&
91                             ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))) {
92                                 while ((path_res > path_resolved) && (*--path_res != '/'));
93
94                                 path_cpy += 3;
95                                 continue;
96                         }
97                 }
98
99 next:
100                 *path_res++ = *path_cpy++;
101         }
102
103         /* remove trailing slash if not root / */
104         if ((path_res > (path_resolved+1)) && (path_res[-1] == '/'))
105                 path_res--;
106         else if (path_res == path_resolved)
107                 *path_res++ = '/';
108
109         *path_res = '\0';
110
111         return path_resolved;
112 }
113
114 /* Returns NULL on error.
115 ** NB: improperly encoded URL should give client 400 [Bad Syntax]; returning
116 ** NULL here causes 404 [Not Found], but that's not too unreasonable. */
117 static struct path_info *
118 uh_path_lookup(struct client *cl, const char *url)
119 {
120         static char path_phys[PATH_MAX];
121         static char path_info[PATH_MAX];
122         static struct path_info p;
123
124         const char *docroot = conf.docroot;
125         int docroot_len = strlen(docroot);
126         char *pathptr = NULL;
127         bool slash;
128
129         int i = 0;
130         int len;
131         struct stat s;
132         struct index_file *idx;
133
134         /* back out early if url is undefined */
135         if (url == NULL)
136                 return NULL;
137
138         memset(&p, 0, sizeof(p));
139         path_phys[0] = 0;
140         path_info[0] = 0;
141
142         strcpy(uh_buf, docroot);
143
144         /* separate query string from url */
145         if ((pathptr = strchr(url, '?')) != NULL) {
146                 p.query = pathptr[1] ? pathptr + 1 : NULL;
147
148                 /* urldecode component w/o query */
149                 if (pathptr > url) {
150                         if (uh_urldecode(&uh_buf[docroot_len],
151                                          sizeof(uh_buf) - docroot_len - 1,
152                                          url, pathptr - url ) < 0)
153                                 return NULL;
154                 }
155         }
156
157         /* no query string, decode all of url */
158         else if (uh_urldecode(&uh_buf[docroot_len],
159                               sizeof(uh_buf) - docroot_len - 1,
160                               url, strlen(url) ) < 0)
161                 return NULL;
162
163         /* create canon path */
164         len = strlen(uh_buf);
165         slash = len && uh_buf[len - 1] == '/';
166         len = min(len, sizeof(path_phys) - 1);
167
168         for (i = len; i >= 0; i--) {
169                 char ch = uh_buf[i];
170                 bool exists;
171
172                 if (ch != 0 && ch != '/')
173                         continue;
174
175                 uh_buf[i] = 0;
176                 exists = !!canonpath(uh_buf, path_phys);
177                 uh_buf[i] = ch;
178
179                 snprintf(path_info, sizeof(path_info), "%s", uh_buf + i);
180                 break;
181         }
182
183         /* check whether found path is within docroot */
184         if (strncmp(path_phys, docroot, docroot_len) != 0 ||
185             (path_phys[docroot_len] != 0 &&
186              path_phys[docroot_len] != '/'))
187                 return NULL;
188
189         /* test current path */
190         if (stat(path_phys, &p.stat))
191                 return NULL;
192
193         /* is a regular file */
194         if (p.stat.st_mode & S_IFREG) {
195                 p.root = docroot;
196                 p.phys = path_phys;
197                 p.name = &path_phys[docroot_len];
198                 p.info = path_info[0] ? path_info : NULL;
199                 return &p;
200         }
201
202         if (!(p.stat.st_mode & S_IFDIR))
203                 return NULL;
204
205         if (path_info[0])
206             return NULL;
207
208         pathptr = path_phys + strlen(path_phys);
209
210         /* ensure trailing slash */
211         if (pathptr[-1] != '/') {
212                 pathptr[0] = '/';
213                 pathptr[1] = 0;
214                 pathptr++;
215         }
216
217         /* if requested url resolves to a directory and a trailing slash
218            is missing in the request url, redirect the client to the same
219            url with trailing slash appended */
220         if (!slash) {
221                 uh_http_header(cl, 302, "Found");
222                 ustream_printf(cl->us, "Location: %s%s%s\r\n\r\n",
223                                 &path_phys[docroot_len],
224                                 p.query ? "?" : "",
225                                 p.query ? p.query : "");
226                 uh_request_done(cl);
227                 p.redirected = 1;
228                 return &p;
229         }
230
231         /* try to locate index file */
232         len = path_phys + sizeof(path_phys) - pathptr - 1;
233         list_for_each_entry(idx, &index_files, list) {
234                 if (strlen(idx->name) > len)
235                         continue;
236
237                 strcpy(pathptr, idx->name);
238                 if (!stat(path_phys, &s) && (s.st_mode & S_IFREG)) {
239                         memcpy(&p.stat, &s, sizeof(p.stat));
240                         break;
241                 }
242
243                 *pathptr = 0;
244         }
245
246         p.root = docroot;
247         p.phys = path_phys;
248         p.name = &path_phys[docroot_len];
249
250         return p.phys ? &p : NULL;
251 }
252
253 static const char * uh_file_mime_lookup(const char *path)
254 {
255         struct mimetype *m = &uh_mime_types[0];
256         const char *e;
257
258         while (m->extn) {
259                 e = &path[strlen(path)-1];
260
261                 while (e >= path) {
262                         if ((*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn))
263                                 return m->mime;
264
265                         e--;
266                 }
267
268                 m++;
269         }
270
271         return "application/octet-stream";
272 }
273
274 static const char * uh_file_mktag(struct stat *s, char *buf)
275 {
276         snprintf(buf, sizeof(buf), "\"%x-%x-%x\"",
277                          (unsigned int) s->st_ino,
278                          (unsigned int) s->st_size,
279                          (unsigned int) s->st_mtime);
280
281         return buf;
282 }
283
284 static time_t uh_file_date2unix(const char *date)
285 {
286         struct tm t;
287
288         memset(&t, 0, sizeof(t));
289
290         if (strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL)
291                 return timegm(&t);
292
293         return 0;
294 }
295
296 static char * uh_file_unix2date(time_t ts, char *buf, int len)
297 {
298         struct tm *t = gmtime(&ts);
299
300         strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", t);
301
302         return buf;
303 }
304
305 static char *uh_file_header(struct client *cl, int idx)
306 {
307         if (!cl->dispatch.file.hdr[idx])
308                 return NULL;
309
310         return (char *) blobmsg_data(cl->dispatch.file.hdr[idx]);
311 }
312
313 static void uh_file_response_ok_hdrs(struct client *cl, struct stat *s)
314 {
315         char buf[128];
316
317         if (s) {
318                 ustream_printf(cl->us, "ETag: %s\r\n", uh_file_mktag(s, buf));
319                 ustream_printf(cl->us, "Last-Modified: %s\r\n",
320                                uh_file_unix2date(s->st_mtime, buf, sizeof(buf)));
321         }
322         ustream_printf(cl->us, "Date: %s\r\n",
323                        uh_file_unix2date(time(NULL), buf, sizeof(buf)));
324 }
325
326 static void uh_file_response_200(struct client *cl, struct stat *s)
327 {
328         uh_http_header(cl, 200, "OK");
329         return uh_file_response_ok_hdrs(cl, s);
330 }
331
332 static void uh_file_response_304(struct client *cl, struct stat *s)
333 {
334         uh_http_header(cl, 304, "Not Modified");
335
336         return uh_file_response_ok_hdrs(cl, s);
337 }
338
339 static void uh_file_response_412(struct client *cl)
340 {
341         uh_http_header(cl, 412, "Precondition Failed");
342 }
343
344 static bool uh_file_if_match(struct client *cl, struct stat *s)
345 {
346         char buf[128];
347         const char *tag = uh_file_mktag(s, buf);
348         char *hdr = uh_file_header(cl, HDR_IF_MATCH);
349         char *p;
350         int i;
351
352         if (!hdr)
353                 return true;
354
355         p = &hdr[0];
356         for (i = 0; i < strlen(hdr); i++)
357         {
358                 if ((hdr[i] == ' ') || (hdr[i] == ',')) {
359                         hdr[i++] = 0;
360                         p = &hdr[i];
361                 } else if (!strcmp(p, "*") || !strcmp(p, tag)) {
362                         return true;
363                 }
364         }
365
366         uh_file_response_412(cl);
367         return false;
368 }
369
370 static int uh_file_if_modified_since(struct client *cl, struct stat *s)
371 {
372         char *hdr = uh_file_header(cl, HDR_IF_MODIFIED_SINCE);
373
374         if (!hdr)
375                 return true;
376
377         if (uh_file_date2unix(hdr) >= s->st_mtime) {
378                 uh_file_response_304(cl, s);
379                 return false;
380         }
381
382         return true;
383 }
384
385 static int uh_file_if_none_match(struct client *cl, struct stat *s)
386 {
387         char buf[128];
388         const char *tag = uh_file_mktag(s, buf);
389         char *hdr = uh_file_header(cl, HDR_IF_NONE_MATCH);
390         char *p;
391         int i;
392
393         if (!hdr)
394                 return true;
395
396         p = &hdr[0];
397         for (i = 0; i < strlen(hdr); i++) {
398                 if ((hdr[i] == ' ') || (hdr[i] == ',')) {
399                         hdr[i++] = 0;
400                         p = &hdr[i];
401                 } else if (!strcmp(p, "*") || !strcmp(p, tag)) {
402                         if ((cl->request.method == UH_HTTP_MSG_GET) ||
403                                 (cl->request.method == UH_HTTP_MSG_HEAD))
404                                 uh_file_response_304(cl, s);
405                         else
406                                 uh_file_response_412(cl);
407
408                         return false;
409                 }
410         }
411
412         return true;
413 }
414
415 static int uh_file_if_range(struct client *cl, struct stat *s)
416 {
417         char *hdr = uh_file_header(cl, HDR_IF_RANGE);
418
419         if (hdr) {
420                 uh_file_response_412(cl);
421                 return false;
422         }
423
424         return true;
425 }
426
427 static int uh_file_if_unmodified_since(struct client *cl, struct stat *s)
428 {
429         char *hdr = uh_file_header(cl, HDR_IF_UNMODIFIED_SINCE);
430
431         if (hdr && uh_file_date2unix(hdr) <= s->st_mtime) {
432                 uh_file_response_412(cl);
433                 return false;
434         }
435
436         return true;
437 }
438
439 static int dirent_cmp(const struct dirent **a, const struct dirent **b)
440 {
441         bool dir_a = !!((*a)->d_type & DT_DIR);
442         bool dir_b = !!((*b)->d_type & DT_DIR);
443
444         /* directories first */
445         if (dir_a != dir_b)
446                 return dir_b - dir_a;
447
448         return alphasort(a, b);
449 }
450
451 static void list_entries(struct client *cl, struct dirent **files, int count,
452                          const char *path, char *local_path)
453 {
454         const char *suffix = "/";
455         const char *type = "directory";
456         unsigned int mode = S_IXOTH;
457         struct stat s;
458         char *file;
459         char buf[128];
460         int i;
461
462         file = local_path + strlen(local_path);
463         for (i = 0; i < count; i++) {
464                 const char *name = files[i]->d_name;
465                 bool dir = !!(files[i]->d_type & DT_DIR);
466
467                 if (name[0] == '.' && name[1] == 0)
468                         continue;
469
470                 sprintf(file, "%s", name);
471                 if (stat(local_path, &s))
472                         continue;
473
474                 if (!dir) {
475                         suffix = "";
476                         mode = S_IROTH;
477                         type = uh_file_mime_lookup(local_path);
478                 }
479
480                 if (!(s.st_mode & mode))
481                         continue;
482
483                 uh_chunk_printf(cl,
484                                 "<li><strong><a href='%s%s%s'>%s</a>%s"
485                                 "</strong><br /><small>modified: %s"
486                                 "<br />%s - %.02f kbyte<br />"
487                                 "<br /></small></li>",
488                                 path, name, suffix,
489                                 name, suffix,
490                                 uh_file_unix2date(s.st_mtime, buf, sizeof(buf)),
491                                 type, s.st_size / 1024.0);
492
493                 *file = 0;
494                 free(files[i]);
495         }
496 }
497
498 static void uh_file_dirlist(struct client *cl, struct path_info *pi)
499 {
500         struct dirent **files = NULL;
501         int count = 0;
502
503         uh_file_response_200(cl, NULL);
504         ustream_printf(cl->us, "Content-Type: text/html\r\n\r\n");
505
506         uh_chunk_printf(cl,
507                 "<html><head><title>Index of %s</title></head>"
508                 "<body><h1>Index of %s</h1><hr /><ol>",
509                 pi->name, pi->name);
510
511         count = scandir(pi->phys, &files, NULL, dirent_cmp);
512         if (count > 0) {
513                 strcpy(uh_buf, pi->phys);
514                 list_entries(cl, files, count, pi->name, uh_buf);
515         }
516         free(files);
517
518         uh_chunk_printf(cl, "</ol><hr /></body></html>");
519         uh_request_done(cl);
520 }
521
522 static void file_write_cb(struct client *cl)
523 {
524         int fd = cl->dispatch.file.fd;
525         int r;
526
527         while (cl->us->w.data_bytes < 256) {
528                 r = read(fd, uh_buf, sizeof(uh_buf));
529                 if (r < 0) {
530                         if (errno == EINTR)
531                                 continue;
532                 }
533
534                 if (!r) {
535                         uh_request_done(cl);
536                         return;
537                 }
538
539                 uh_chunk_write(cl, uh_buf, r);
540         }
541 }
542
543 static void uh_file_free(struct client *cl)
544 {
545         close(cl->dispatch.file.fd);
546 }
547
548 static void uh_file_data(struct client *cl, struct path_info *pi, int fd)
549 {
550         /* test preconditions */
551         if (!uh_file_if_modified_since(cl, &pi->stat) ||
552                 !uh_file_if_match(cl, &pi->stat) ||
553                 !uh_file_if_range(cl, &pi->stat) ||
554                 !uh_file_if_unmodified_since(cl, &pi->stat) ||
555                 !uh_file_if_none_match(cl, &pi->stat)) {
556                 uh_request_done(cl);
557                 close(fd);
558                 return;
559         }
560
561         /* write status */
562         uh_file_response_200(cl, &pi->stat);
563
564         ustream_printf(cl->us, "Content-Type: %s\r\n",
565                            uh_file_mime_lookup(pi->name));
566
567         ustream_printf(cl->us, "Content-Length: %i\r\n\r\n",
568                            pi->stat.st_size);
569
570
571         /* send body */
572         if (cl->request.method == UH_HTTP_MSG_HEAD) {
573                 uh_request_done(cl);
574                 close(fd);
575                 return;
576         }
577
578         cl->dispatch.file.fd = fd;
579         cl->dispatch.write_cb = file_write_cb;
580         cl->dispatch.free = uh_file_free;
581         cl->dispatch.close_fds = uh_file_free;
582         file_write_cb(cl);
583 }
584
585 static void uh_file_request(struct client *cl, const char *url,
586                             struct path_info *pi, struct blob_attr **tb)
587 {
588         int fd;
589
590         if (!(pi->stat.st_mode & S_IROTH))
591                 goto error;
592
593         if (pi->stat.st_mode & S_IFREG) {
594                 fd = open(pi->phys, O_RDONLY);
595                 if (fd < 0)
596                         goto error;
597
598                 cl->dispatch.file.hdr = tb;
599                 uh_file_data(cl, pi, fd);
600                 cl->dispatch.file.hdr = NULL;
601                 return;
602         }
603
604         if ((pi->stat.st_mode & S_IFDIR)) {
605                 if (conf.no_dirlists)
606                         goto error;
607
608                 uh_file_dirlist(cl, pi);
609                 return;
610         }
611
612 error:
613         uh_client_error(cl, 403, "Forbidden",
614                         "You don't have permission to access %s on this server.",
615                         url);
616 }
617
618 void uh_dispatch_add(struct dispatch_handler *d)
619 {
620         list_add_tail(&d->list, &dispatch_handlers);
621 }
622
623 static struct dispatch_handler *
624 dispatch_find(const char *url, struct path_info *pi)
625 {
626         struct dispatch_handler *d;
627
628         list_for_each_entry(d, &dispatch_handlers, list) {
629                 if (pi) {
630                         if (d->check_url)
631                                 continue;
632
633                         if (d->check_path(pi, url))
634                                 return d;
635                 } else {
636                         if (d->check_path)
637                                 continue;
638
639                         if (d->check_url(url))
640                                 return d;
641                 }
642         }
643
644         return NULL;
645 }
646
647 static bool __handle_file_request(struct client *cl, const char *url)
648 {
649         static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
650                 [HDR_AUTHORIZATION] = { "authorization", BLOBMSG_TYPE_STRING },
651                 [HDR_IF_MODIFIED_SINCE] = { "if-modified-since", BLOBMSG_TYPE_STRING },
652                 [HDR_IF_UNMODIFIED_SINCE] = { "if-unmodified-since", BLOBMSG_TYPE_STRING },
653                 [HDR_IF_MATCH] = { "if-match", BLOBMSG_TYPE_STRING },
654                 [HDR_IF_NONE_MATCH] = { "if-none-match", BLOBMSG_TYPE_STRING },
655                 [HDR_IF_RANGE] = { "if-range", BLOBMSG_TYPE_STRING },
656         };
657         struct dispatch_handler *d;
658         struct blob_attr *tb[__HDR_MAX];
659         struct path_info *pi;
660
661         pi = uh_path_lookup(cl, url);
662         if (!pi)
663                 return false;
664
665         if (pi->redirected)
666                 return true;
667
668         blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(cl->hdr.head), blob_len(cl->hdr.head));
669         if (tb[HDR_AUTHORIZATION])
670                 pi->auth = blobmsg_data(tb[HDR_AUTHORIZATION]);
671
672         if (!uh_auth_check(cl, pi))
673                 return true;
674
675         d = dispatch_find(url, pi);
676         if (d)
677                 d->handle_request(cl, url, pi);
678         else
679                 uh_file_request(cl, url, pi, tb);
680
681         return true;
682 }
683
684 void uh_handle_request(struct client *cl)
685 {
686         struct dispatch_handler *d;
687         const char *url = blobmsg_data(blob_data(cl->hdr.head));;
688
689         d = dispatch_find(url, NULL);
690         if (d) {
691                 d->handle_request(cl, url, NULL);
692                 return;
693         }
694
695         if (__handle_file_request(cl, url) ||
696             __handle_file_request(cl, conf.error_handler))
697                 return;
698
699         uh_client_error(cl, 404, "Not Found", "The requested URL %s was not found on this server.", url);
700 }