-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmod_autoindex.c
2358 lines (2161 loc) · 78.2 KB
/
mod_autoindex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* mod_autoindex.c: Handles the on-the-fly html index generation
*
* Rob McCool
* 3/23/93
*
* Adapted to Apache by rst.
*
* Version sort added by Martin Pool <[email protected]>.
*/
#include "apr_strings.h"
#include "apr_fnmatch.h"
#include "apr_strings.h"
#include "apr_lib.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_request.h"
#include "http_protocol.h"
#include "http_log.h"
#include "http_main.h"
#include "util_script.h"
#include "mod_core.h"
module AP_MODULE_DECLARE_DATA autoindex_module;
/****************************************************************
*
* Handling configuration directives...
*/
#define NO_OPTIONS (1 << 0) /* Indexing options */
#define ICONS_ARE_LINKS (1 << 1)
#define SCAN_HTML_TITLES (1 << 2)
#define SUPPRESS_ICON (1 << 3)
#define SUPPRESS_LAST_MOD (1 << 4)
#define SUPPRESS_SIZE (1 << 5)
#define SUPPRESS_DESC (1 << 6)
#define SUPPRESS_PREAMBLE (1 << 7)
#define SUPPRESS_COLSORT (1 << 8)
#define SUPPRESS_RULES (1 << 9)
#define FOLDERS_FIRST (1 << 10)
#define VERSION_SORT (1 << 11)
#define TRACK_MODIFIED (1 << 12)
#define FANCY_INDEXING (1 << 13)
#define TABLE_INDEXING (1 << 14)
#define IGNORE_CLIENT (1 << 15)
#define IGNORE_CASE (1 << 16)
#define EMIT_XHTML (1 << 17)
#define SHOW_FORBIDDEN (1 << 18)
#define ADDALTCLASS (1 << 19)
#define OPTION_UNSET (1 << 20)
#define K_NOADJUST 0
#define K_ADJUST 1
#define K_UNSET 2
/*
* Define keys for sorting.
*/
#define K_NAME 'N' /* Sort by file name (default) */
#define K_LAST_MOD 'M' /* Last modification date */
#define K_SIZE 'S' /* Size (absolute, not as displayed) */
#define K_DESC 'D' /* Description */
#define K_VALID "NMSD" /* String containing _all_ valid K_ opts */
#define D_ASCENDING 'A'
#define D_DESCENDING 'D'
#define D_VALID "AD" /* String containing _all_ valid D_ opts */
/*
* These are the dimensions of the default icons supplied with Apache.
*/
#define DEFAULT_ICON_WIDTH 20
#define DEFAULT_ICON_HEIGHT 22
/*
* Other default dimensions.
*/
#define DEFAULT_NAME_WIDTH 23
#define DEFAULT_DESC_WIDTH 23
struct item {
char *type;
char *apply_to;
char *apply_path;
char *data;
};
typedef struct ai_desc_t {
char *pattern;
char *description;
unsigned int full_path : 1;
unsigned int wildcards : 1;
} ai_desc_t;
typedef struct autoindex_config_struct {
char *default_icon;
char *style_sheet;
char *head_insert;
char *header;
char *readme;
apr_int32_t opts;
apr_int32_t incremented_opts;
apr_int32_t decremented_opts;
int name_width;
int name_adjust;
int desc_width;
int desc_adjust;
int icon_width;
int icon_height;
int not_found;
char default_keyid;
char default_direction;
apr_array_header_t *icon_list;
apr_array_header_t *alt_list;
apr_array_header_t *desc_list;
apr_array_header_t *ign_list;
int ign_noinherit;
char *ctype;
char *charset;
char *datetime_format;
} autoindex_config_rec;
static char c_by_encoding, c_by_type, c_by_path;
#define BY_ENCODING &c_by_encoding
#define BY_TYPE &c_by_type
#define BY_PATH &c_by_path
static APR_INLINE int response_is_html(request_rec *r)
{
char *ctype = ap_field_noparam(r->pool, r->content_type);
return !ap_cstr_casecmp(ctype, "text/html")
|| !ap_cstr_casecmp(ctype, "application/xhtml+xml");
}
/*
* This routine puts the standard HTML header at the top of the index page.
* We include the DOCTYPE because we may be using features therefrom (i.e.,
* HEIGHT and WIDTH attributes on the icons if we're FancyIndexing).
*/
static void emit_preamble(request_rec *r, int xhtml, const char *title)
{
autoindex_config_rec *d;
d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
&autoindex_module);
if (xhtml) {
ap_rvputs(r, DOCTYPE_XHTML_1_0T,
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
" <head>\n <title>Index of ", title,
"</title>\n", NULL);
} else {
ap_rvputs(r, DOCTYPE_HTML_4_01,
"<html>\n <head>\n"
" <title>Index of ", title,
"</title>\n", NULL);
}
if (d->style_sheet != NULL) {
ap_rvputs(r, " <link rel=\"stylesheet\" href=\"", d->style_sheet,
"\" type=\"text/css\"", xhtml ? " />\n" : ">\n", NULL);
}
if (d->head_insert != NULL) {
ap_rputs(d->head_insert, r);
}
ap_rputs(" </head>\n <body>\n", r);
}
static void push_item(apr_array_header_t *arr, char *type, const char *to,
const char *path, const char *data)
{
struct item *p = (struct item *) apr_array_push(arr);
if (!to) {
to = "";
}
if (!path) {
path = "";
}
p->type = type;
p->data = apr_pstrdup(arr->pool, data);
p->apply_path = apr_pstrcat(arr->pool, path, "*", NULL);
if ((type == BY_PATH) && (!ap_is_matchexp(to))) {
p->apply_to = apr_pstrcat(arr->pool, "*", to, NULL);
}
else {
p->apply_to = apr_pstrdup(arr->pool, to);
}
}
static const char *add_alt(cmd_parms *cmd, void *d, const char *alt,
const char *to)
{
if (cmd->info == BY_PATH) {
if (!strcmp(to, "**DIRECTORY**")) {
to = "^^DIRECTORY^^";
}
}
if (cmd->info == BY_ENCODING) {
char *tmp = apr_pstrdup(cmd->temp_pool, to);
ap_str_tolower(tmp);
to = tmp;
}
push_item(((autoindex_config_rec *) d)->alt_list, cmd->info, to,
cmd->path, alt);
return NULL;
}
static const char *add_icon(cmd_parms *cmd, void *d, const char *icon,
const char *to)
{
char *iconbak = apr_pstrdup(cmd->temp_pool, icon);
if (icon[0] == '(') {
char *alt;
char *cl = strchr(iconbak, ')');
if (cl == NULL) {
return "missing closing paren";
}
alt = ap_getword_nc(cmd->temp_pool, &iconbak, ',');
*cl = '\0'; /* Lose closing paren */
add_alt(cmd, d, &alt[1], to);
}
if (cmd->info == BY_PATH) {
if (!strcmp(to, "**DIRECTORY**")) {
to = "^^DIRECTORY^^";
}
}
if (cmd->info == BY_ENCODING) {
char *tmp = apr_pstrdup(cmd->temp_pool, to);
ap_str_tolower(tmp);
to = tmp;
}
push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to,
cmd->path, iconbak);
return NULL;
}
/*
* Add description text for a filename pattern. If the pattern has
* wildcards already (or we need to add them), add leading and
* trailing wildcards to it to ensure substring processing. If the
* pattern contains a '/' anywhere, force wildcard matching mode,
* add a slash to the prefix so that "bar/bletch" won't be matched
* by "foobar/bletch", and make a note that there's a delimiter;
* the matching routine simplifies to just the actual filename
* whenever it can. This allows definitions in parent directories
* to be made for files in subordinate ones using relative paths.
*/
/*
* Absent a strcasestr() function, we have to force wildcards on
* systems for which "AAA" and "aaa" mean the same file.
*/
#ifdef CASE_BLIND_FILESYSTEM
#define WILDCARDS_REQUIRED 1
#else
#define WILDCARDS_REQUIRED 0
#endif
static const char *add_desc(cmd_parms *cmd, void *d, const char *desc,
const char *to)
{
autoindex_config_rec *dcfg = (autoindex_config_rec *) d;
ai_desc_t *desc_entry;
char *prefix = "";
desc_entry = (ai_desc_t *) apr_array_push(dcfg->desc_list);
desc_entry->full_path = (ap_strchr_c(to, '/') == NULL) ? 0 : 1;
desc_entry->wildcards = (WILDCARDS_REQUIRED
|| desc_entry->full_path
|| apr_fnmatch_test(to));
if (desc_entry->wildcards) {
prefix = desc_entry->full_path ? "*/" : "*";
desc_entry->pattern = apr_pstrcat(dcfg->desc_list->pool,
prefix, to, "*", NULL);
}
else {
desc_entry->pattern = apr_pstrdup(dcfg->desc_list->pool, to);
}
desc_entry->description = apr_pstrdup(dcfg->desc_list->pool, desc);
return NULL;
}
static const char *add_ignore(cmd_parms *cmd, void *d, const char *ext)
{
push_item(((autoindex_config_rec *) d)->ign_list, 0, ext, cmd->path, NULL);
return NULL;
}
static const char *add_opts(cmd_parms *cmd, void *d, int argc, char *const argv[])
{
int i;
char *w;
apr_int32_t opts;
apr_int32_t opts_add;
apr_int32_t opts_remove;
char action;
autoindex_config_rec *d_cfg = (autoindex_config_rec *) d;
opts = d_cfg->opts;
opts_add = d_cfg->incremented_opts;
opts_remove = d_cfg->decremented_opts;
for (i = 0; i < argc; i++) {
int option = 0;
w = argv[i];
if ((*w == '+') || (*w == '-')) {
action = *(w++);
}
else {
action = '\0';
}
if (!strcasecmp(w, "FancyIndexing")) {
option = FANCY_INDEXING;
}
else if (!strcasecmp(w, "FoldersFirst")) {
option = FOLDERS_FIRST;
}
else if (!strcasecmp(w, "HTMLTable")) {
option = TABLE_INDEXING;
}
else if (!strcasecmp(w, "IconsAreLinks")) {
option = ICONS_ARE_LINKS;
}
else if (!strcasecmp(w, "IgnoreCase")) {
option = IGNORE_CASE;
}
else if (!strcasecmp(w, "IgnoreClient")) {
option = IGNORE_CLIENT;
}
else if (!strcasecmp(w, "ScanHTMLTitles")) {
option = SCAN_HTML_TITLES;
}
else if (!strcasecmp(w, "SuppressColumnSorting")) {
option = SUPPRESS_COLSORT;
}
else if (!strcasecmp(w, "SuppressDescription")) {
option = SUPPRESS_DESC;
}
else if (!strcasecmp(w, "SuppressHTMLPreamble")) {
option = SUPPRESS_PREAMBLE;
}
else if (!strcasecmp(w, "SuppressIcon")) {
option = SUPPRESS_ICON;
}
else if (!strcasecmp(w, "SuppressLastModified")) {
option = SUPPRESS_LAST_MOD;
}
else if (!strcasecmp(w, "SuppressSize")) {
option = SUPPRESS_SIZE;
}
else if (!strcasecmp(w, "SuppressRules")) {
option = SUPPRESS_RULES;
}
else if (!strcasecmp(w, "TrackModified")) {
option = TRACK_MODIFIED;
}
else if (!strcasecmp(w, "VersionSort")) {
option = VERSION_SORT;
}
else if (!strcasecmp(w, "XHTML")) {
option = EMIT_XHTML;
}
else if (!strcasecmp(w, "ShowForbidden")) {
option = SHOW_FORBIDDEN;
}
else if (!strcasecmp(w, "AddAltClass")) {
option = ADDALTCLASS;
}
else if (!strcasecmp(w, "None")) {
if (action != '\0') {
return "Cannot combine '+' or '-' with 'None' keyword";
}
opts = NO_OPTIONS;
opts_add = 0;
opts_remove = 0;
}
else if (!strcasecmp(w, "IconWidth")) {
if (action != '-') {
d_cfg->icon_width = DEFAULT_ICON_WIDTH;
}
else {
d_cfg->icon_width = 0;
}
}
else if (!strncasecmp(w, "IconWidth=", 10)) {
if (action == '-') {
return "Cannot combine '-' with IconWidth=n";
}
d_cfg->icon_width = atoi(&w[10]);
}
else if (!strcasecmp(w, "IconHeight")) {
if (action != '-') {
d_cfg->icon_height = DEFAULT_ICON_HEIGHT;
}
else {
d_cfg->icon_height = 0;
}
}
else if (!strncasecmp(w, "IconHeight=", 11)) {
if (action == '-') {
return "Cannot combine '-' with IconHeight=n";
}
d_cfg->icon_height = atoi(&w[11]);
}
else if (!strcasecmp(w, "NameWidth")) {
if (action != '-') {
return "NameWidth with no value may only appear as "
"'-NameWidth'";
}
d_cfg->name_width = DEFAULT_NAME_WIDTH;
d_cfg->name_adjust = K_NOADJUST;
}
else if (!strncasecmp(w, "NameWidth=", 10)) {
if (action == '-') {
return "Cannot combine '-' with NameWidth=n";
}
if (w[10] == '*') {
d_cfg->name_adjust = K_ADJUST;
}
else {
int width = atoi(&w[10]);
if (width && (width < 5)) {
return "NameWidth value must be greater than 5";
}
d_cfg->name_width = width;
d_cfg->name_adjust = K_NOADJUST;
}
}
else if (!strcasecmp(w, "DescriptionWidth")) {
if (action != '-') {
return "DescriptionWidth with no value may only appear as "
"'-DescriptionWidth'";
}
d_cfg->desc_width = DEFAULT_DESC_WIDTH;
d_cfg->desc_adjust = K_NOADJUST;
}
else if (!strncasecmp(w, "DescriptionWidth=", 17)) {
if (action == '-') {
return "Cannot combine '-' with DescriptionWidth=n";
}
if (w[17] == '*') {
d_cfg->desc_adjust = K_ADJUST;
}
else {
int width = atoi(&w[17]);
if (width && (width < 12)) {
return "DescriptionWidth value must be greater than 12";
}
d_cfg->desc_width = width;
d_cfg->desc_adjust = K_NOADJUST;
}
}
else if (!strncasecmp(w, "Type=", 5)) {
d_cfg->ctype = apr_pstrdup(cmd->pool, &w[5]);
}
else if (!strncasecmp(w, "Charset=", 8)) {
d_cfg->charset = apr_pstrdup(cmd->pool, &w[8]);
}
else if (!strcasecmp(w, "UseOldDateFormat")) {
d_cfg->datetime_format = "%d-%b-%Y %H:%M";
}
else {
return "Invalid directory indexing option";
}
if (action == '\0') {
opts |= option;
opts_add = 0;
opts_remove = 0;
}
else if (action == '+') {
opts_add |= option;
opts_remove &= ~option;
}
else {
opts_remove |= option;
opts_add &= ~option;
}
}
if ((opts & NO_OPTIONS) && (opts & ~NO_OPTIONS)) {
return "Cannot combine other IndexOptions keywords with 'None'";
}
d_cfg->incremented_opts = opts_add;
d_cfg->decremented_opts = opts_remove;
d_cfg->opts = opts;
return NULL;
}
static const char *set_default_order(cmd_parms *cmd, void *m,
const char *direction, const char *key)
{
autoindex_config_rec *d_cfg = (autoindex_config_rec *) m;
if (!strcasecmp(direction, "Ascending")) {
d_cfg->default_direction = D_ASCENDING;
}
else if (!strcasecmp(direction, "Descending")) {
d_cfg->default_direction = D_DESCENDING;
}
else {
return "First keyword must be 'Ascending' or 'Descending'";
}
if (!strcasecmp(key, "Name")) {
d_cfg->default_keyid = K_NAME;
}
else if (!strcasecmp(key, "Date")) {
d_cfg->default_keyid = K_LAST_MOD;
}
else if (!strcasecmp(key, "Size")) {
d_cfg->default_keyid = K_SIZE;
}
else if (!strcasecmp(key, "Description")) {
d_cfg->default_keyid = K_DESC;
}
else {
return "Second keyword must be 'Name', 'Date', 'Size', or "
"'Description'";
}
return NULL;
}
#define DIR_CMD_PERMS OR_INDEXES
static const command_rec autoindex_cmds[] =
{
AP_INIT_ITERATE2("AddIcon", add_icon, BY_PATH, DIR_CMD_PERMS,
"an icon URL followed by one or more filenames"),
AP_INIT_ITERATE2("AddIconByType", add_icon, BY_TYPE, DIR_CMD_PERMS,
"an icon URL followed by one or more MIME types"),
AP_INIT_ITERATE2("AddIconByEncoding", add_icon, BY_ENCODING, DIR_CMD_PERMS,
"an icon URL followed by one or more content encodings"),
AP_INIT_ITERATE2("AddAlt", add_alt, BY_PATH, DIR_CMD_PERMS,
"alternate descriptive text followed by one or more "
"filenames"),
AP_INIT_ITERATE2("AddAltByType", add_alt, BY_TYPE, DIR_CMD_PERMS,
"alternate descriptive text followed by one or more MIME "
"types"),
AP_INIT_ITERATE2("AddAltByEncoding", add_alt, BY_ENCODING, DIR_CMD_PERMS,
"alternate descriptive text followed by one or more "
"content encodings"),
AP_INIT_TAKE_ARGV("IndexOptions", add_opts, NULL, DIR_CMD_PERMS,
"one or more index options [+|-][]"),
AP_INIT_TAKE2("IndexOrderDefault", set_default_order, NULL, DIR_CMD_PERMS,
"{Ascending,Descending} {Name,Size,Description,Date}"),
AP_INIT_ITERATE("IndexIgnore", add_ignore, NULL, DIR_CMD_PERMS,
"one or more file extensions"),
AP_INIT_FLAG("IndexIgnoreReset", ap_set_flag_slot,
(void *)APR_OFFSETOF(autoindex_config_rec, ign_noinherit),
DIR_CMD_PERMS,
"Reset the inherited list of IndexIgnore filenames"),
AP_INIT_ITERATE2("AddDescription", add_desc, BY_PATH, DIR_CMD_PERMS,
"Descriptive text followed by one or more filenames"),
AP_INIT_TAKE1("HeaderName", ap_set_string_slot,
(void *)APR_OFFSETOF(autoindex_config_rec, header),
DIR_CMD_PERMS, "a filename"),
AP_INIT_TAKE1("ReadmeName", ap_set_string_slot,
(void *)APR_OFFSETOF(autoindex_config_rec, readme),
DIR_CMD_PERMS, "a filename"),
AP_INIT_RAW_ARGS("FancyIndexing", ap_set_deprecated, NULL, OR_ALL,
"The FancyIndexing directive is no longer supported. "
"Use IndexOptions FancyIndexing."),
AP_INIT_TAKE1("DefaultIcon", ap_set_string_slot,
(void *)APR_OFFSETOF(autoindex_config_rec, default_icon),
DIR_CMD_PERMS, "an icon URL"),
AP_INIT_TAKE1("IndexStyleSheet", ap_set_string_slot,
(void *)APR_OFFSETOF(autoindex_config_rec, style_sheet),
DIR_CMD_PERMS, "URL to style sheet"),
AP_INIT_TAKE1("IndexHeadInsert", ap_set_string_slot,
(void *)APR_OFFSETOF(autoindex_config_rec, head_insert),
DIR_CMD_PERMS, "String to insert in HTML HEAD section"),
AP_INIT_FLAG("IndexForbiddenReturn404", ap_set_flag_slot,
(void *)APR_OFFSETOF(autoindex_config_rec, not_found),
DIR_CMD_PERMS,
"Return 404 in place of 403 when Options doesn't allow indexes"),
{NULL}
};
static void *create_autoindex_config(apr_pool_t *p, char *dummy)
{
autoindex_config_rec *new =
(autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
new->icon_width = 0;
new->icon_height = 0;
new->name_width = DEFAULT_NAME_WIDTH;
new->name_adjust = K_UNSET;
new->desc_width = DEFAULT_DESC_WIDTH;
new->desc_adjust = K_UNSET;
new->icon_list = apr_array_make(p, 4, sizeof(struct item));
new->alt_list = apr_array_make(p, 4, sizeof(struct item));
new->desc_list = apr_array_make(p, 4, sizeof(ai_desc_t));
new->ign_list = apr_array_make(p, 4, sizeof(struct item));
new->opts = OPTION_UNSET;
new->incremented_opts = 0;
new->decremented_opts = 0;
new->default_keyid = '\0';
new->default_direction = '\0';
return (void *) new;
}
static void *merge_autoindex_configs(apr_pool_t *p, void *basev, void *addv)
{
autoindex_config_rec *new;
autoindex_config_rec *base = (autoindex_config_rec *) basev;
autoindex_config_rec *add = (autoindex_config_rec *) addv;
new = (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
new->default_icon = add->default_icon ? add->default_icon
: base->default_icon;
new->style_sheet = add->style_sheet ? add->style_sheet
: base->style_sheet;
new->head_insert = add->head_insert ? add->head_insert
: base->head_insert;
new->header = add->header ? add->header
: base->header;
new->readme = add->readme ? add->readme
: base->readme;
new->icon_height = add->icon_height ? add->icon_height : base->icon_height;
new->icon_width = add->icon_width ? add->icon_width : base->icon_width;
new->ctype = add->ctype ? add->ctype : base->ctype;
new->charset = add->charset ? add->charset : base->charset;
new->datetime_format = add->datetime_format ? add->datetime_format : base->datetime_format;
new->alt_list = apr_array_append(p, add->alt_list, base->alt_list);
new->desc_list = apr_array_append(p, add->desc_list, base->desc_list);
new->icon_list = apr_array_append(p, add->icon_list, base->icon_list);
new->ign_list = add->ign_noinherit ? add->ign_list : apr_array_append(p, add->ign_list, base->ign_list);
if (add->opts == NO_OPTIONS) {
/*
* If the current directory explicitly says 'no options' then we also
* clear any incremental mods from being inheritable further down.
*/
new->opts = NO_OPTIONS;
new->incremented_opts = 0;
new->decremented_opts = 0;
}
else {
/*
* If there were any nonincremental options selected for
* this directory, they dominate and we don't inherit *anything.*
* Contrariwise, we *do* inherit if the only settings here are
* incremental ones.
*/
if (add->opts == OPTION_UNSET) {
new->incremented_opts = (base->incremented_opts
| add->incremented_opts)
& ~add->decremented_opts;
new->decremented_opts = (base->decremented_opts
| add->decremented_opts);
/*
* We may have incremental settings, so make sure we don't
* inadvertently inherit an IndexOptions None from above.
*/
new->opts = (base->opts & ~NO_OPTIONS);
}
else {
/*
* There are local nonincremental settings, which clear
* all inheritance from above. They *are* the new base settings.
*/
new->opts = add->opts;
}
/*
* We're guaranteed that there'll be no overlap between
* the add-options and the remove-options.
*/
new->opts |= new->incremented_opts;
new->opts &= ~new->decremented_opts;
}
/*
* Inherit the NameWidth settings if there aren't any specific to
* the new location; otherwise we'll end up using the defaults set in the
* config-rec creation routine.
*/
if (add->name_adjust == K_UNSET) {
new->name_width = base->name_width;
new->name_adjust = base->name_adjust;
}
else {
new->name_width = add->name_width;
new->name_adjust = add->name_adjust;
}
/*
* Likewise for DescriptionWidth.
*/
if (add->desc_adjust == K_UNSET) {
new->desc_width = base->desc_width;
new->desc_adjust = base->desc_adjust;
}
else {
new->desc_width = add->desc_width;
new->desc_adjust = add->desc_adjust;
}
new->default_keyid = add->default_keyid ? add->default_keyid
: base->default_keyid;
new->default_direction = add->default_direction ? add->default_direction
: base->default_direction;
return new;
}
/****************************************************************
*
* Looking things up in config entries...
*/
/* Structure used to hold entries when we're actually building an index */
struct ent {
char *name;
char *icon;
char *alt;
char *desc;
apr_off_t size;
apr_time_t lm;
struct ent *next;
unsigned int ascending : 1;
unsigned int ignore_case : 1;
unsigned int version_sort : 1;
unsigned int isdir : 1;
char key;
};
static char *find_item(const char *content_type, const char *content_encoding,
char *path, apr_array_header_t *list, int path_only)
{
struct item *items = (struct item *) list->elts;
int i;
for (i = 0; i < list->nelts; ++i) {
struct item *p = &items[i];
/* Special cased for ^^DIRECTORY^^ and ^^BLANKICON^^ */
if ((path[0] == '^') || (!ap_strcmp_match(path, p->apply_path))) {
if (!*(p->apply_to)) {
return p->data;
}
else if (p->type == BY_PATH || path[0] == '^') {
if (!ap_strcmp_match(path, p->apply_to)) {
return p->data;
}
}
else if (!path_only) {
if (!content_encoding) {
if (p->type == BY_TYPE) {
if (content_type
&& !ap_strcasecmp_match(content_type,
p->apply_to)) {
return p->data;
}
}
}
else {
if (p->type == BY_ENCODING) {
if (!ap_strcasecmp_match(content_encoding,
p->apply_to)) {
return p->data;
}
}
}
}
}
}
return NULL;
}
static char *find_item_by_request(request_rec *r, apr_array_header_t *list, int path_only)
{
return find_item(ap_field_noparam(r->pool, r->content_type),
r->content_encoding, r->filename, list, path_only);
}
#define find_icon(d,p,t) find_item_by_request(p,d->icon_list,t)
#define find_alt(d,p,t) find_item_by_request(p,d->alt_list,t)
#define find_default_icon(d,n) find_item(NULL, NULL, n, d->icon_list, 1)
#define find_default_alt(d,n) find_item(NULL, NULL, n, d->alt_list, 1)
/*
* Look through the list of pattern/description pairs and return the first one
* if any) that matches the filename in the request. If multiple patterns
* match, only the first one is used; since the order in the array is the
* same as the order in which directives were processed, earlier matching
* directives will dominate.
*/
#ifdef CASE_BLIND_FILESYSTEM
#define MATCH_FLAGS APR_FNM_CASE_BLIND
#else
#define MATCH_FLAGS 0
#endif
static char *find_desc(autoindex_config_rec *dcfg, const char *filename_full)
{
int i;
ai_desc_t *list = (ai_desc_t *) dcfg->desc_list->elts;
const char *filename_only;
const char *filename;
/*
* If the filename includes a path, extract just the name itself
* for the simple matches.
*/
if ((filename_only = ap_strrchr_c(filename_full, '/')) == NULL) {
filename_only = filename_full;
}
else {
filename_only++;
}
for (i = 0; i < dcfg->desc_list->nelts; ++i) {
ai_desc_t *tuple = &list[i];
int found;
/*
* Only use the full-path filename if the pattern contains '/'s.
*/
filename = (tuple->full_path) ? filename_full : filename_only;
/*
* Make the comparison using the cheapest method; only do
* wildcard checking if we must.
*/
if (tuple->wildcards) {
found = (apr_fnmatch(tuple->pattern, filename, MATCH_FLAGS) == 0);
}
else {
found = (ap_strstr_c(filename, tuple->pattern) != NULL);
}
if (found) {
return tuple->description;
}
}
return NULL;
}
static int ignore_entry(autoindex_config_rec *d, char *path)
{
apr_array_header_t *list = d->ign_list;
struct item *items = (struct item *) list->elts;
char *tt;
int i;
if ((tt = strrchr(path, '/')) == NULL) {
tt = path;
}
else {
tt++;
}
for (i = 0; i < list->nelts; ++i) {
struct item *p = &items[i];
char *ap;
if ((ap = strrchr(p->apply_to, '/')) == NULL) {
ap = p->apply_to;
}
else {
ap++;
}
#ifndef CASE_BLIND_FILESYSTEM
if (!ap_strcmp_match(path, p->apply_path)
&& !ap_strcmp_match(tt, ap)) {
return 1;
}
#else /* !CASE_BLIND_FILESYSTEM */
/*
* On some platforms, the match must be case-blind. This is really
* a factor of the filesystem involved, but we can't detect that
* reliably - so we have to granularise at the OS level.
*/
if (!ap_strcasecmp_match(path, p->apply_path)
&& !ap_strcasecmp_match(tt, ap)) {
return 1;
}
#endif /* !CASE_BLIND_FILESYSTEM */
}
return 0;
}
/*****************************************************************
*
* Actually generating output
*/
/*
* Elements of the emitted document:
* Preamble
* Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
* succeeds for the (content_type == text/html) header file.
* Header file
* Emitted if found (and able).
* H1 tag line
* Emitted if a header file is NOT emitted.
* Directory stuff
* Always emitted.
* HR
* Emitted if FANCY_INDEXING is set.
* Readme file
* Emitted if found (and able).
* ServerSig
* Emitted if ServerSignature is not Off AND a readme file
* is NOT emitted.
* Postamble
* Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
* succeeds for the (content_type == text/html) readme file.
*/
/*
* emit a plain text file
*/
static void do_emit_plain(request_rec *r, apr_file_t *f)
{
char buf[AP_IOBUFSIZE + 1];
int ch;
apr_size_t i, c, n;
apr_status_t rv;
ap_rputs("<pre>\n", r);
while (!apr_file_eof(f)) {
do {
n = sizeof(char) * AP_IOBUFSIZE;
rv = apr_file_read(f, buf, &n);
} while (APR_STATUS_IS_EINTR(rv));
if (n == 0 || rv != APR_SUCCESS) {
/* ###: better error here? */
break;
}
buf[n] = '\0';
c = 0;
while (c < n) {
for (i = c; i < n; i++) {
if (buf[i] == '<' || buf[i] == '>' || buf[i] == '&') {
break;
}
}
ch = buf[i];
buf[i] = '\0';
ap_rputs(&buf[c], r);
if (ch == '<') {
ap_rputs("<", r);
}
else if (ch == '>') {
ap_rputs(">", r);
}
else if (ch == '&') {
ap_rputs("&", r);
}
c = i + 1;
}
}
ap_rputs("</pre>\n", r);
}
/*
* Handle the preamble through the H1 tag line, inclusive. Locate