Working on login and ticket system
[CGIscriptor.git] / BinaryMapFile.xmr
blob4fe542bbc5ea2a5df81d900af62df12733d69899
1 <META CONTENT="text/ssperl, CGI='$SERVER_ROOT $CGI_BINARY_FILE $REMOTE_ADDR'"><SCRIPT TYPE=text/ssperl>
2 # NOTE: no empty lines outside the script tags
3
4 # 030300        Added directory access blocking for removing CVS directories from downloads
6 # This program contains two tricks:
7 # 1 .gz compressed and tar.gz compressed archive files are constructed on-line
8 #   when they don't exist.
9 # 2 URL: "http:/localhost:8080/<file1>*<file2>*...<fileN>.multipart" requests are treated  
10 #   as a single multi file 'multipart/mixed' MIME type request. That is, a single multipart
11 #   document with all requested files is constructed on-line (when it doesn't exist). 
12 #   If only the first file contains a URL path, the others will be prefixed with it, e.g.,
13 #   '/Audio/Daudio/AMDaudio/BLAMD001.WAV*BLAMD001.WAV.MULTIPART' is identical to
14 #   '/Audio/Daudio/AMDaudio/BLAMD001.WAV*/Audio/Daudio/AMDaudio/BLAMD001.WAV.MULTIPART'
15 #   NOTE: the view of multipart/mixed documents depends on the BROWSER used.
18 ###############################################################################
20 # Author and Copyright (c):
21 # Rob van Son, © 1999,2000
22 # Institute of Phonetic Sciences & IFOTT/ACLC
23 # University of Amsterdam
24 # Email: R.J.J.H.vanSon@uva.nl
25 # WWW  : http://www.fon.hum.uva.nl/rob/
27 # License for use and disclaimers
29 # CGIscriptor merges plain ASCII HTML files transparantly  
30 # with CGI variables, in-line PERL code, shell commands, 
31 # and executable scripts in other scripting languages. 
32
33 # This program is free software; you can redistribute it and/or
34 # modify it under the terms of the GNU General Public License
35 # as published by the Free Software Foundation; either version 2
36 # of the License, or (at your option) any later version.
38 # This program is distributed in the hope that it will be useful,
39 # but WITHOUT ANY WARRANTY; without even the implied warranty of
40 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41 # GNU General Public License for more details.
43 # You should have received a copy of the GNU General Public License
44 # along with this program; if not, write to the Free Software
45 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
48 #######################################################>>>>>>>>>>Start Remove
50 # DIRECTORY ACCESS CONTROL: "foo|bar|thing" used as: =~ m@/($BlockAccess)/@
51 my $BlockAccess = "CVS";    # Block access to the CVS information
53 # Define mime types extension => mimetype (can be found in .mime.types file)
54 my %mimeType = (
55 'HTML' => "text/html",    # These are PROCESSED by CGIscriptor, not just written to STDOUT
56 'TXT'  => "text/plain",
57 'PL'  => "text/plain",    # This is incorrect, of course
58 #'CGI'  => "text/osshell", # Executing shell scripts should not be done
59 'JPG'  => "image/jpeg",
60 'JPEG' => "image/jpeg",
61 'GIF'  => "image/gif",
62 'TIFF' => "image/tiff",
63 'TIF'  => "image/tiff",
64 'AU'   => "audio/basic",
65 'AIF'  => "audio/aiff",
66 'AIFC' => "audio/aiff",
67 'AIFF' => "audio/aiff",
68 'WAV'  => "audio/wav",
69 'MPGA' => "audio/mpeg",
70 'MP2'  => "audio/mpeg",
71 'MPEG' => "video/mpeg",
72 'MPG'  => "video/mpeg",
73 'MPE'  => "video/mpeg",
74 'MULTIPART' => "multipart/mixed;boundary=NxtCntntTpZdFjgHk".time,  # use a '*' separated list
75 'QT'   => "video/quicktime",
76 'MOV'  => "video/quicktime",
77 'GZ'   => "application/gzip"   # Constructs .gz and .tar.gz files on-line/run-time
80 $CGI_BINARY_FILE =~ /\.([\w]+)$/;       # Get extension
81 my $extension = uc($1);
82 unless(exists($mimeType{$extension})) # actually illegal mime
84     exit 0;
86 my $mime = $mimeType{$extension};
88 my $String;
89 my $number_of_bytes; 
91 # Print the content type
92 $String = "Content-type: $mime\n\n";
93 $number_of_bytes = length($String); 
94 # Actually print the document header 
95 # (but NOT for OSSHELL scripts, which supply their own headers)
96 syswrite(STDOUT, $String, $number_of_bytes) unless $mime eq "text/osshell";
98 # THIS LINE PREVENTS ACCESS TO SPECIFIC PARTS OF THE DIRECTORY TREE
99 exit if $CGI_BINARY_FILE =~ m@/($BlockAccess)/@; # Prevent access to specific subtrees
101 # First, construct tar.gz and .gz files on-line
102 if($mime eq "application/gzip" &&  ! -e "$SERVER_ROOT$CGI_BINARY_FILE")
104     # This is a gimmick to construct gnu-zipped tar-files from every requested directory at 
105     # run-time This could be a security weakness and a CPU hog, so be carefull
106     if($CGI_BINARY_FILE =~ m@\.tar\.gz$@is)
107     {
108         $SERVER_ROOT =~ m@/[^/]+$@;     # Get last directory name
109         my $HomeDirectory = $&;         # The name of the current SERVER_ROOT dir
110         my $PrePath = $`;                       # The path tho the parent of the server root
111         # THIS LINE PREVENTs SECURITY BREAKS: INSIST ON FULL PATHS STARTING AT SERVER-ROOT
112         exit unless $CGI_BINARY_FILE =~ m@^$HomeDirectory@; # Prevent tricks
113         
114         $CGI_BINARY_FILE =~ m@\.tar\.gz$@is;    # Remove extensions (e.g., .tar.gz)
115         my $TarPath = $PrePath.$`;              # The path to the directory to be TARRED
116         die "Illegal tar request from $REMOTE_ADDR: $TarPath\n" unless -d $TarPath; # Only directories allowed
117         
118         # Use a pipe to prevent memory overloads (I hope). 
119         # Note: the tar start at the PARENT directory of the directory to be
120         # tarred. The tar excludes symbolic links and .log files.
121         open(BINARY,
122         "cd $PrePath;find $TarPath ! -type l ! -type d -print"
123         ."|grep -v '.log'|egrep -v '/$BlockAccess/'"
124         ."|sed 's\@$PrePath\@.\@g'|tar -cf - -T -|gzip|")  # Linux tar
125         || open(BINARY,
126         "cd $PrePath;find $TarPath ! -type l ! -type d -print"
127     ."|grep -v '.log'|egrep -v '/$BlockAccess/'"
128         ."|sed 's\@$PrePath\@.\@g'|tar -cf - -|gzip|")     # SGI Irix tar
129         || die "$!\n";
130         # If you want to tar WHOLE directories unconditionally, uncomment the 
131         # following (and remove the above)
132         # open(BINARY, "cd $PrePath;tar -cf - .$HomeDirectory|gzip|");
133         
134         # read and write block of 1024 bytes
135         while($number_of_bytes = sysread(BINARY, $String, 1024))
136         {
137             syswrite(STDOUT, $String, $number_of_bytes); # Actually print the file content
138         };
139         close(BINARY);
140     }
141     # This will construct a gnu-zipped file from every file requested: GET foo.bar.gz will
142     # result in foo.bar being zipped before delivery.
143     else
144     {
145         $CGI_BINARY_FILE =~ m@\.gz$@is; # Remove extensions (e.g., .tar.gz)
146         my $GzipPath = $SERVER_ROOT.$`;        # The path to the file to be zipped
148         # Limit gzip activity to supported file-types
149         $GzipPath =~ /\.([\w]+)$/;      # Get extension
150         my $GzipExtension = uc($1);
151         my $GzipMime = $mimeType{$GzipExtension}; # mime of file to be zipped
152         die "Illegal tar request from $REMOTE_ADDR: $GzipPath\n" unless $GzipMime && ($GzipMime ne $mime);
153         
154         # Use a pipe to prevent memory overloads (I hope)
155         open(BINARY, "gzip -c $GzipPath|");
156         # read and write block of 1024 bytes
157         while($number_of_bytes = sysread(BINARY, $String, 1024))
158         {
159             syswrite(STDOUT, $String, $number_of_bytes); # Actually print the file content
160         };
161         close(BINARY);
162     };  
164 # Second, trick to handle 'multipart/mixed', ie, combined pages, on-line.
165 # This allows the playback of a sound file coupled to the display of an 
166 # HTML file
167 elsif($mime =~ m@^multipart/mixed@isg &&  ! -e "$SERVER_ROOT$CGI_BINARY_FILE")
169     $CGI_BINARY_FILE =~ m@[^/]+$@;
170     my $URLpath = $`;
171     my $BinaryFileList = $&;
172     if($URLpath =~ /\*/)   # If each file has separate path, do not use common URLpath
173     {
174         $BinaryFileList = $CGI_BINARY_FILE;
175         $URLpath = '';
176     };
178     $BinaryFileList =~ s/\.$extension$//isg;  # Remove Multipart extension
179     my @FileList = split('\*', $BinaryFileList);
180     my $InputFile;
181     $mime =~ /\;boundary\=/;
182     my $MultipartSeparator = $';
183     foreach $InputFile (@FileList)
184     {
185         
186        # Write separation string
187        my $String = "\n--$MultipartSeparator\n";
188        my $number_of_bytes = length($String);
189        syswrite(STDOUT, $String, $number_of_bytes); # Actually print the separation string
191        # Recursively process file parts
192        $CGI_BINARY_FILE = $URLpath.$InputFile;
193        $ENV{'CGI_BINARY_FILE'} = $URLpath.$InputFile;
194        main::ProcessFile("~/BinaryMapFile.xmr");
195     };
196     # Write closing separation string
197     my $String = "\n--$MultipartSeparator\n";
198     my $number_of_bytes = length($String);
199     syswrite(STDOUT, $String, $number_of_bytes); # Actually print the separation string
200        
202 # Third, process HTML files as CGIscriptor HTML files (when they are part of MULTIPART/MIXED files)
203 elsif($mime eq 'text/html')
205     main::ProcessFile("~/$CGI_BINARY_FILE");
207 # If you realy cannot live without shell scripts (note the ./)
208 # Note that the script must supply the content type!
209 elsif($mime eq 'text/osshell')
211     # Check filename for safety
212     die "./".$CGI_BINARY_FILE." from $REMOTE_ADDR\n" 
213         unless CGIscriptor::CGIsafeFileName($CGI_BINARY_FILE); 
214     # This doesn't "feel" safe, but it works
215     print STDOUT `./$CGI_BINARY_FILE`;
217 else  # All other files are just printed
219     open(BINARY, "<$SERVER_ROOT$CGI_BINARY_FILE") || die "$SERVER_ROOT$CGI_BINARY_FILE: $!";
220     
221     # read and write block of 1024 bytes
222     while($number_of_bytes = sysread(BINARY, $String, 1024))
223     {
224             syswrite(STDOUT, $String, $number_of_bytes); # Actually print the file content
225     };
226     close(BINARY);
230 </SCRIPT>