|
1
|
#!/usr/bin/tclsh |
|
2
|
# |
|
3
|
# This script scans a directory hierarchy looking for Fossil CGI files - |
|
4
|
# the files that are used to launch Fossil as a CGI program. For each |
|
5
|
# such file found, in prints the name of the file and also the file |
|
6
|
# content, indented, if the --print option is used. |
|
7
|
# |
|
8
|
# tclsh find-fossil-cgis.tcl [OPTIONS] DIRECTORY |
|
9
|
# |
|
10
|
# The argument is the directory from which to begin the search. |
|
11
|
# |
|
12
|
# OPTIONS can be zero or more of the following: |
|
13
|
# |
|
14
|
# --has REGEXP Only show the CGI if the body matches REGEXP. |
|
15
|
# May be repeated multiple times, in which case |
|
16
|
# all must match. |
|
17
|
# |
|
18
|
# --hasnot REGEXP Only show the CGI if it does NOT match the |
|
19
|
# REGEXP. |
|
20
|
# |
|
21
|
# --print Show the content of the CGI, indented by |
|
22
|
# three spaces |
|
23
|
# |
|
24
|
# --symlink Process DIRECTORY arguments that are symlinks. |
|
25
|
# Normally symlinks are silently ignored. |
|
26
|
# |
|
27
|
# -v Show progress information for debugging |
|
28
|
# |
|
29
|
# EXAMPLE USE CASES: |
|
30
|
# |
|
31
|
# Find all CGIs that do not have the "errorlog:" property set |
|
32
|
# |
|
33
|
# find-fossil-cgis.tcl *.website --has '\nrepository:' \ |
|
34
|
# --hasnot '\nerrorlog:' |
|
35
|
# |
|
36
|
# Add the errorlog: property to any CGI that does not have it: |
|
37
|
# |
|
38
|
# find-fossil-cgis.tcl *.website --has '\nrepository:' \ |
|
39
|
# --hasnot '\nerrorlog:' | while read x |
|
40
|
# do |
|
41
|
# echo 'errorlog: /logs/errors.txt' >>$x |
|
42
|
# done |
|
43
|
# |
|
44
|
# Find and print all CGIs that do redirects |
|
45
|
# |
|
46
|
# find-fossil-cgis.tcl *.website --has '\nredirect:' --print |
|
47
|
# |
|
48
|
|
|
49
|
|
|
50
|
# Find the CGIs in directory $dir. Invoke recursively to |
|
51
|
# scan subdirectories. |
|
52
|
# |
|
53
|
proc find_in_one_dir {dir} { |
|
54
|
global HAS HASNOT PRINT V |
|
55
|
if {$V>0} { |
|
56
|
puts "# $dir" |
|
57
|
} |
|
58
|
foreach obj [lsort [glob -nocomplain -directory $dir *]] { |
|
59
|
if {[file isdir $obj]} { |
|
60
|
find_in_one_dir $obj |
|
61
|
continue |
|
62
|
} |
|
63
|
if {![file isfile $obj]} continue |
|
64
|
if {[file size $obj]>5000} continue |
|
65
|
if {![file exec $obj]} continue |
|
66
|
if {![file readable $obj]} continue |
|
67
|
set fd [open $obj rb] |
|
68
|
set txt [read $fd] |
|
69
|
close $fd |
|
70
|
if {![string match #!* $txt]} continue |
|
71
|
if {![regexp {fossil} $txt]} continue |
|
72
|
if {![regexp {\nrepository: } $txt] && |
|
73
|
![regexp {\ndirectory: } $txt] && |
|
74
|
![regexp {\nredirect: } $txt]} continue |
|
75
|
set ok 1 |
|
76
|
foreach re $HAS { |
|
77
|
if {![regexp $re $txt]} {set ok 0; break;} |
|
78
|
} |
|
79
|
if {!$ok} continue |
|
80
|
foreach re $HASNOT { |
|
81
|
if {[regexp $re $txt]} {set ok 0; break;} |
|
82
|
} |
|
83
|
if {!$ok} continue |
|
84
|
# |
|
85
|
# At this point assume we have found a CGI file. |
|
86
|
# |
|
87
|
puts $obj |
|
88
|
if {$PRINT} { |
|
89
|
regsub -all {\n} [string trim $txt] "\n " out |
|
90
|
puts " $out" |
|
91
|
} |
|
92
|
} |
|
93
|
} |
|
94
|
set HAS [list] |
|
95
|
set HASNOT [list] |
|
96
|
set PRINT 0 |
|
97
|
set SYMLINK 0 |
|
98
|
set V 0 |
|
99
|
set N [llength $argv] |
|
100
|
set DIRLIST [list] |
|
101
|
|
|
102
|
# First pass: Gather all the command-line arguments but do no |
|
103
|
# processing. |
|
104
|
# |
|
105
|
for {set i 0} {$i<$N} {incr i} { |
|
106
|
set dir [lindex $argv $i] |
|
107
|
if {($dir eq "-has" || $dir eq "--has") && $i<[expr {$N-1}]} { |
|
108
|
incr i |
|
109
|
lappend HAS [lindex $argv $i] |
|
110
|
continue |
|
111
|
} |
|
112
|
if {($dir eq "-hasnot" || $dir eq "--hasnot") && $i<[expr {$N-1}]} { |
|
113
|
incr i |
|
114
|
lappend HASNOT [lindex $argv $i] |
|
115
|
continue |
|
116
|
} |
|
117
|
if {$dir eq "-print" || $dir eq "--print"} { |
|
118
|
set PRINT 1 |
|
119
|
continue |
|
120
|
} |
|
121
|
if {$dir eq "-symlink" || $dir eq "--symlink"} { |
|
122
|
set SYMLINK 1 |
|
123
|
continue |
|
124
|
} |
|
125
|
if {$dir eq "-v"} { |
|
126
|
set V 1 |
|
127
|
continue |
|
128
|
} |
|
129
|
if {[file type $dir]=="directory"} { |
|
130
|
lappend DIRLIST $dir |
|
131
|
} |
|
132
|
} |
|
133
|
|
|
134
|
# Second pass: Process the non-option arguments. |
|
135
|
# |
|
136
|
foreach dir $DIRLIST { |
|
137
|
set type [file type $dir] |
|
138
|
if {$type eq "directory" || ($SYMLINK && $type eq "link")} { |
|
139
|
find_in_one_dir $dir |
|
140
|
} |
|
141
|
} |
|
142
|
|