Files

759 lines
19 KiB
Perl
Executable File

#!/usr/bin/perl
#__________________________________________________________
#
# File: wrap
# By: Matt Ho
# Date: 7/23/95
# Purpose: Wrapper to dynamically create HTML documents
# that contain a easy to read directory content.
#__________________________________________________________
require "flush.pl";
#__________________________________________________________
#
# IRIX 6.5 disables the 'download' feature for
# security reasons. This functionality will removed
# entirely in the next release.
#
# The supported way for a user to download a file is
# to use the browser 'Save Link' feature.
#
# If you need to restore this functionality, and you are
# behind a firewall, and trust all others behind that
# firewall, then change the line below to:
# $HANDLER_DISABLED = 0 ;
#__________________________________________________________
$HANDLER_DISABLED = 1 ;
#__________________________________________________________
#
# Set some environment variables, we'll need through the
# script and do some initial error checking.
#__________________________________________________________
$PATH = $ENV{'PATH_INFO'} ;
$ls = "/sbin/ls -a1" ;
$outboxcgi = "/var/www/cgi-bin/outbox-cgi" ;
$wrap = "/cgi-bin/wrap" ;
#__________________________________________________________
#
# Read the form data in. PATH_INFO may be passed here
# if this was called from a SSI. We're going to make
# one final tweak and read from the arg list as well
# for SSI's.
#__________________________________________________________
if( $ENV{'REQUEST_METHOD'} eq "GET" )
{
$buffer=$ENV{'QUERY_STRING'} ;
}
else
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}) ;
}
@pairs = split(/&/, $buffer) ;
foreach (@pairs)
{
tr/+/ / ;
($name,$value) = split(/=/) ;
$value =~ s/%(..)/pack("c",hex($1))/ge ;
$name =~ s/%(..)/pack("c",hex($1))/ge ;
$FORM{$name} = $value ;
}
#_________________________________
#
# Some basic checks to take
# care of
#_________________________________
if( $ARGV[0] )
{
$PATH = $ARGV[0] ;
$arg = 1 ;
}
# trim off undesirable meta chars.
$PATH =~ s/[|;]//go ;
chop $PATH if substr($PATH, -1) eq "/" ;
@_ = split('/', $PATH) ;
$outBoxPath = $PATH ; $outBoxPath =~ s/^\/$_[1]// ;
$pathRoot = $_[$#_] ;
($pwname,$pwpasswd,$pwuid,$pwgid,$pwquota,$pwcomment,$pwgcos,$pwdir,$pwshell) = getpwnam($_[1]) ;
$doc = $pwdir."/public_html".$outBoxPath ;
&ErrBadPath if ! defined $PATH || $PATH eq "" ; # check for empty path
&ErrBadPath unless &ValidPath($doc) ; # Check for server spoofing
&ErrBadUser unless -e $pwdir ; # Check to see it exists
&ErrBadPath unless -e $doc ; # Check to see it exists
&ErrBadPath if -f $doc ; # Don't allow download of file via wrap
&ErrBadUser if $pwname eq "" ; # don't allow listing of non-OutBox folder
#__________________________________________________________
#
# If we get to this point, we can be reasonably certain
# that this is a valid shared directory. We want to
# show it using the appropriate method. The following
# section is the leading data. ShowContents will supply
# the rest of the data.
#
# We do, however, need to account for the user putting
# an index.html file in the directory. We'll handle
# that as follows. If the file exists, it is assumed to
# be in the right. We will append out data at the end,
# using the index.html's head section. Otherwise, we
# will use ours.
#__________________________________________________________
if( $arg == 1 )
{
# This is intentionally left blank
}
elsif( -r "$doc/index.html" )
{
print "Content-type: text/html\n\n" ;
open(INDEX, "$doc/index.html") ;
while( <INDEX> )
{
s/<!--#exec/<!-- exec/go ;
last if /<\/[bB][oO][dD][yY]>/ ;
last if /<\/[hH][tT][mM][lL]>/ ;
print ;
if ( /<BODY/ || /<body/)
{
if ( ! />/ )
{
while( <INDEX> )
{
print ;
last if ( />/ ) ;
}
}
&flush(STDOUT) ;
system '/var/www/cgi-bin/outbox-cgi', $PATH;
}
}
close(INDEX) ;
}
else
{
print <<ENDOFTEXT ;
Content-type: text/html
<head>
<title>
$pathRoot
</title>
</head>
<body bgcolor="c1c1c1" text="000000" link="0000ee" vlink="0000ee"
alink="ff0000" background="/outbox/images/background.gif">
ENDOFTEXT
&flush(STDOUT) ;
system '/var/www/cgi-bin/outbox-cgi', $PATH ;
&flush(STDOUT) ;
print <<ENDOFTEXT ;
<B>$pathRoot:</B>
ENDOFTEXT
}
&ShowContents ;
print "\n</body>\n</html>\n\n" unless $arg == 1 ;
# done
exit ;
#__________________________________________________________
#
# subroutines
#
#__________________________________________________________
#__________________________________________________________
sub ErrBadPath
{
$errorPageTitle = `/usr/bin/gettxt uxoutbox:39 "OutBox: File Not Found"` ;
$errorPageMessage =
`/usr/bin/gettxt uxoutbox:40 "The requested file was not found on this OutBox page:"` ;
&ErrorPage ;
}
sub ErrBadUser
{
$errorPageTitle = `/usr/bin/gettxt uxoutbox:41 "OutBox: User Not Known"` ;
$errorPageMessage =
`/usr/bin/gettxt uxoutbox:42 "The requested file is not in a valid OutBox folder:"` ;
&ErrorPage ;
}
sub ErrorPage
{
print <<ENDOFTEXT ;
Content-type: text/html
<HTML>
<HEAD><TITLE>$errorPageTitle</TITLE></HEAD>
<BODY><H2>$errorPageTitle</H2>
$errorPageMessage
<B>$PATH</B>
<P>
ENDOFTEXT
if( defined $ENV{'HTTP_REFERER'} )
{
$referer = $ENV{'HTTP_REFERER'} ;
print <<ENDOFTEXT ;
<a href="$referer"><IMG SRC="/outbox/images/go-back.gif" WIDTH=34 HEIGHT=32 BORDER=0 ALT="Back"></A>
ENDOFTEXT
}
print <<ENDOFTEXT ;
</BODY>
</HTML>
ENDOFTEXT
exit ;
}
#__________________________________________________________
sub ValidPath
{
local($pt) = $_[$#_];
return 1 unless $pt =~ /\.\./o ;
return '' if $pt =~ /^\.\./o ;
return '' if $pt =~ /\/\.\.\//o ;
return '' if $pt =~ /\.\.$/o ;
return 1 ;
}
#__________________________________________________________
sub IsHiddenFolder
{
local($fname) = $_[$#_];
return 1 if ( $fname =~ /^\./o && $fname ne "\.\." ) ; # .*
return 1 if $fname eq "RCS" ; # RCS
return 1 if $fname eq "SCCS" ; # SCCS
return 1 if $fname eq "CVS" ; # CVS
return '' ;
}
#__________________________________________________________
sub IsHiddenFile
{
local($fname) = $_[$#_];
return 1 if $fname eq "index\.html" ;
return 1 if $fname eq "index\.html\.O" ;
return 1 if $fname eq "index\.html\.N" ;
return 1 if $fname eq "index\.shtml" ;
return 1 if $fname eq "index\.shtml\.O" ;
return 1 if $fname eq "index\.shtml\.N" ;
return 1 if $fname eq "default\.gif" ;
return 1 if ( $fname =~ /^\./o ) ;
return 1 if ( $fname =~ /\.bak$/io ) ;
return 1 if ( $fname =~ /\.sav$/io ) ;
return 1 if ( $fname =~ /~$/o ) ;
return '' ;
}
#__________________________________________________________
sub ShowContents
{
local($format) = $FORM{'format'} ;
#_________________________________
#
# List folder contents and split into subfolders and files
#_________________________________
open (FIND,"$ls $doc |") ;
while( <FIND> )
{
chop ;
next if $_ eq "\." ;
next if ( $_ eq "\.\." && "/$pathRoot" eq $PATH ) ;
next if &IsHiddenFolder($_) ;
next if ( ! -r "$doc/$_" ) ;
next if ( -d "$doc/$_" && ! -x "$doc/$_" ) ;
push(@d, $_), next if -d "$doc/$_" ;
push(@f, $_) ;
}
#_________________________________
#
# Display folder and files
#_________________________________
&ListFolders(@d) ;
&ListFiles(@f) ;
}
#__________________________________________________________
sub ListFolders
{
local($size) = $#_ + 1 ;
local($i) ;
local($j) ;
local($index) ;
local($format) ;
local($cols) = 3 ;
local($rows) = $size / $cols ;
local($left) = $size % $cols ;
local($label) ;
local($image) ;
return if $size == 0 ;
#_________________________________
$format = "?format=$FORM{'format'}" if defined $FORM{'format'} ;
#_________________________________
$rows = sprintf("%.0d", $rows) ;
$rows++ if $left > 0 ;
print <<ENDOFTEXT ;
</center>
<table cellpadding=0 cellspacing=0 width="100%" border=0>
ENDOFTEXT
LOOP:
for( $i = 0 ; $i < $rows ; $i++ )
{
print "<tr>\n" ;
for( $j = 0 ; $j < $cols ; $j++ )
{
$index = $i + $j * $rows ;
if( $index >= $size )
{
print "<td></td>\n" ;
next ;
}
if ( $_[$index] eq ".." )
{
$label = "" ;
$image = "/outbox/images/go-back.gif" ;
$imageWidth="34";
$imageHeight="32";
}
else
{
$label = "$_[$index]" ;
$image = "/outbox/images/file_folder.gif" ;
$imageWidth="40";
$imageHeight="40";
}
print <<ENDOFTEXT ;
<td>
<a href="$wrap$PATH/$_[$index]/$format"><img src="$image" align=middle border=0 width=$imageWidth height=$imageHeight></a>
ENDOFTEXT
if ( $label ne "" )
{
print " <a href=\"$wrap$PATH/$_[$index]/$format\">$label</a>" ;
}
print "</td>"
}
print "</tr>\n" ;
}
print <<ENDOFTEXT ;
</table>
ENDOFTEXT
}
#__________________________________________________________
sub ListFiles
{
local(@notes) ;
local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,@other) ;
local($USERPATH) = $PATH ;
local($first) = 1 ;
local($image) ;
local($url) ;
$dltext = `/usr/bin/gettxt uxoutbox:22 "(download)"` ;
return if ( $#_ == -1 ) ;
substr($USERPATH, 0, 1) = "/~" ;
print <<ENDOFTEXT ;
</center>
<div align=left>
ENDOFTEXT
foreach (@_)
{
next if &IsHiddenFile($_) ;
if ( /\.notes$/o )
{
$base = $_ ;
$base =~ s/\.notes$//o ;
next if -r "$doc/$base" ;
}
if ($first == 1)
{
$first = 0 ;
print "<HR ALIGN=LEFT WIDTH=\"80%\">\n" if ($#d >= 0) ;
print "<table cellpadding=0 cellspacing=1 border=0>" ;
}
#_________________________________
#
# Calculate the size of this
# file
#_________________________________
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,@other) =
stat("$doc/$_") ;
if( $size == 1 )
{
$size = "<b>$size</b> byte" ;
}
elsif( $size >= 1024 )
{
$size = $size / 1024 ;
$size = sprintf("%.0d", $size) ;
$size = "<b>$size</b> KB" ;
}
else
{
$size = "<b>$size</b> bytes" ;
}
#_________________________________
#
# Add notes if applicable
#_________________________________
if( -r "$doc/$_.notes" )
{
open(DATA, "$doc/$_.notes") ;
@notes = <DATA> ;
close(DATA) ;
}
#_________________________________
#
# figure out which image to use
#_________________________________
# defaults
$image = "" ;
$url = "" ;
SWITCH: {
# html
$image = "file_html.gif", last SWITCH if ( /\.html$/io ) ;
$image = "file_html.gif", last SWITCH if ( /\.htm$/io ) ;
$image = "file_html.gif", last SWITCH if ( /\.shtml$/io ) ;
# audio
$image = "file_audio.gif", last SWITCH if ( /\.au$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.aif$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.aif[cf]$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.snd$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.wav$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.ra$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.ram$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.abs$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.mp[2a]$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.mp[2a][2a]$/io ) ;
$image = "file_audio.gif", last SWITCH if ( /\.mpega$/io ) ;
# image
$image = "file_image.gif", last SWITCH if ( /\.gif$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.ief$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.tiff$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.tif$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.jpg$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.jpeg$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.jpe$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.rgb$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.ras$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.pcd$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.p[nbgp]m$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.x[bp]m$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.xwd$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.icon$/io ) ;
$image = "file_image.gif", last SWITCH if ( /\.fti$/io ) ;
# video
$image = "file_movie.gif", last SWITCH if ( /\.movie$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.mov$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.moov$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.mv$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.qt$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.avi$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.mp[egsv]$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.mp[2sv][2sv]$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.mpeg$/io ) ;
$image = "file_movie.gif", last SWITCH if ( /\.mpegv$/io ) ;
# PostScript
$image = "file_ps.gif", last SWITCH if ( /\.ps$/io ) ;
$image = "file_ps.gif", last SWITCH if ( /\.eps$/io ) ;
# text
$image = "file_text.gif", last SWITCH if ( /\.txt$/io ) ;
$image = "file_text.gif", last SWITCH if ( /\.rtx$/io ) ;
$image = "file_text.gif", last SWITCH if ( /\.rtf$/io ) ;
# development
$image = "file_makefile.gif", last SWITCH if ( /makefile.*$/io ) ;
$image = "file_core.gif", last SWITCH if ( $_ eq "core" ) ;
$image = "file_h.gif", last SWITCH if ( /\.h$/io ) ;
$image = "file_c.gif", last SWITCH if ( /\.c$/o ) ;
$image = "file_cxx.gif", last SWITCH if ( /\.cxx$/io ) ;
$image = "file_cxx.gif", last SWITCH if ( /\.c\+\+$/io ) ;
$image = "file_cxx.gif", last SWITCH if ( /\.C$/o ) ;
$image = "file_cxx.gif", last SWITCH if ( /\.cc$/io ) ;
$image = "file_obj.gif", last SWITCH if ( /\.o$/o ) ;
$image = "file_obj.gif", last SWITCH if ( /\.obj$/io ) ;
$image = "file_java.gif", last SWITCH if ( /\.java$/io ) ;
$image = "file_javaclass.gif", last SWITCH if ( /\.class$/io ) ;
# showcase
$image = "file_showcase.gif", last SWITCH if ( /\.sc$/io ) ;
$image = "file_showcase.gif", last SWITCH if ( /\.slides$/io ) ;
$image = "file_showcase.gif", last SWITCH if ( /\.sho$/io ) ;
$image = "file_showcase.gif", last SWITCH if ( /\.show$/io ) ;
# command scripts
$image = "file_shell.gif", last SWITCH if ( /\.pl$/io ) ;
$image = "file_shell.gif", last SWITCH if ( /\.sh$/io ) ;
$image = "file_shell.gif", last SWITCH if ( /\.csh$/io ) ;
# DOS/Windows
$image = "file_ms_exe.gif", last SWITCH if ( /\.bat$/io ) ;
$image = "file_ms_exe.gif", last SWITCH if ( /\.exe$/io ) ;
$image = "file_ms_exe.gif", last SWITCH if ( /\.com$/io ) ;
$image = "file_ms_pp.gif", last SWITCH if ( /\.ppt$/io ) ;
$image = "file_ms_access.gif", last SWITCH if ( /\.adb$/io ) ;
if ( /\.xl.$/io )
{
open(FILE, "$doc/$_") ;
read(FILE,$buf,3);
$image = "file_ms_excel.gif"
if (substr($buf,0,2) eq "\xd0\xcf" ||
substr($buf,0,1) eq "\x09" &&
substr($buf,2,1) eq "\x06") ;
close(FILE);
last SWITCH if ( $image ne "" ) ;
}
if ( /\.do[ct]$/io )
{
open(FILE, "$doc/$_") ;
read(FILE,$buf,2);
$image = "file_ms_word.gif"
if ($buf eq "\xfe\x37" ||
$buf eq "\xdb\xa5" ||
$buf eq "\xd0\xcf") ;
close(FILE);
last SWITCH if ( $image ne "" ) ;
}
if ( /\.fm$/io )
{
open(FILE, "$doc/$_") ;
read(FILE,$buf,4);
$image = "file_claris_fm.gif"
if ($buf eq "\x00\x01\x00\x00" ||
$buf eq "\x00\x02\x00\x00") ;
close(FILE);
last SWITCH if ( $image ne "" ) ;
}
# other
$image = "file_vcard.gif", last SWITCH if ( /\.vcf$/io ) ;
$image = "file_iv.gif", last SWITCH if ( /\.iv$/io ) ;
$image = "file_iv.gif", last SWITCH if ( /\.ano$/io ) ;
$image = "file_tar.gif", last SWITCH if ( /\.tar$/io ) ;
$image = "file_vrml.gif", last SWITCH if ( /\.vrml$/io ) ;
$image = "file_vrml.gif", last SWITCH if ( /\.vrml\.gz$/io ) ;
$image = "file_vrml.gif", last SWITCH if ( /\.wrl$/io ) ;
$image = "file_vrml.gif", last SWITCH if ( /\.wrl\.gz$/io ) ;
$image = "file_midi.gif", last SWITCH if ( /\.mid$/io ) ;
$image = "file_midi.gif", last SWITCH if ( /\.midi$/io ) ;
$image = "file_pdf.gif", last SWITCH if ( /\.pdf$/io ) ;
$image = "file_director.gif", last SWITCH if ( /\.d[cxi]r$/io ) ;
$image = "file_compress.gif", last SWITCH if ( /\.gz$/io ) ;
$image = "file_compress.gif", last SWITCH if ( /\.tgz$/io ) ;
$image = "file_compress.gif", last SWITCH if ( /\.Z$/io ) ;
# generic ms-windows application data
# (i.e. has suffix, no types above match)
if ( $image eq "" && /..*\...*$/o )
{
open(FILE, "$doc/$_") ;
read(FILE,$buf,16);
$image = "file_ms_generic.gif"
if ($buf eq "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00\x00\x00\x00\x00\x00\x00\x00" ||
$buf eq "\x09\x04\x06\x00\x00\x00\x10\x00\xf6\x05\x5c\x00\x20\x00\x03\x2e") ;
close(FILE);
last SWITCH if ( $image ne "" ) ;
}
} # end of SWITCH
# types without suffix, or common NetscapeURL/WebJumper names that
# may have been mis-typed above.
if ( $image eq "" ||
$image eq "file_html.gif" ||
$image eq "file_ms_exe.gif")
{
open(FILE, "$doc/$_") ;
if ( $string = <FILE> )
{
# WebJumper and NetscapeURL shortcuts
if ( $string =~ /^\#SGIWebJumpsite/o || $string =~ /^\#NetscapeURL/o)
{
$image = "file_webjumper.gif" if $string =~ /^\#SGIWebJumpsite/o ;
$image = "file_netscapeurl.gif" if $string =~ /^\#NetscapeURL/o ;
while ( $string = <FILE> )
{
$string =~ s/\n$//o ;
# look for ://, and other url types
if ( $string =~ /:\/\//o ||
$string =~ /^mailto:/io ||
$string =~ /^file:/io ||
$string =~ /^about:/io ||
$string =~ /^news:/io ||
$string =~ /^snews:/io ||
$string =~ /^mailbox:/io )
{
# don't view it, go to it.
$url = $string ;
last ;
}
else
{
# use as a note, if no .notes file
@notes = ( $string )
if ( @notes == 0 &&
$string ne "URL Pasted into Desktop" ) ;
}
}
}
# command script
$image = "file_shell.gif" if ( $string =~ /^\#!\/bin\/.*sh/o ) ;
$image = "file_shell.gif" if ( $string =~ /^\#!\/usr\/s*bin\/perl/o ) ;
}
close(FILE) ;
}
# generic executable
if ( $image eq "" )
{
$image = "file_exec.gif" if ( -x "$doc/$_" ) ;
}
# default to generic data file icon
$image = "file_data.gif" if ( $image eq "" ) ;
# default to view data
$url = "$USERPATH/$_" if ( $url eq "" ) ;
#_________________________________
#
# Diplay the data in table form
#_________________________________
print <<ENDOFTEXT ;
<tr>
<td>
<a href="$url"><img src="/outbox/images/$image" width=40 height=40 align=middle border=0></a>
</td>
<td>
<a href="$url">$_</a>
</td>
<td>
$size
</td>
<td align=left>
ENDOFTEXT
if ( $HANDLER_DISABLED == 0 ) {
print "<a href=\"/cgi-bin/handler$PATH/$_?data=Download\">$dltext</a>\n" ;
}
print " </td>\n" ;
#_________________________________
#
# If there is a .notes file,
# show the message within the
# HTML doc.
#_________________________________
if ( @notes > 0 )
{
print "<td>" ;
foreach (@notes)
{
print ;
}
print "</td>\n" ;
@notes = () ;
}
print "</tr>\n" ;
}
print "</table></center>\n" if $first == 0 ;
}