#!/usr/bin/perl -w use gsdlCGI; # The version of Perl distributed with Greenstone on Windows doesn't have crypt, so use this instead use Crypt::UnixCrypt; my $authentication_enabled = 1; my $debugging_enabled = 0; sub main { my $gsdl_cgi = new gsdlCGI("+cmdline"); # Encrypt the password if (defined $gsdl_cgi->param("pw")) { $gsdl_cgi->param('-name' => "pw", '-value' => crypt($gsdl_cgi->clean_param("pw"), "Tp")); } $gsdl_cgi->parse_cgi_args(); $gsdl_cgi->setup_gsdl(); my $gsdlhome = $ENV{'GSDLHOME'}; $gsdl_cgi->checked_chdir($gsdlhome); require "$gsdlhome/perllib/util.pm"; # This is OK on Windows my $cmd = $gsdl_cgi->clean_param("cmd"); if (!defined $cmd) { $gsdl_cgi->generate_error("No command specified."); } $gsdl_cgi->delete("cmd"); # All commands require a username, for locking and authentication my $username = $gsdl_cgi->clean_param("un"); if ((!defined $username) || ($username =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No username specified."); } $gsdl_cgi->delete("un"); if ($cmd eq "delete-collection") { &delete_collection($gsdl_cgi, $username); } elsif ($cmd eq "download-collection") { &download_collection($gsdl_cgi, $username); } elsif ($cmd eq "download-collection-archives") { &download_collection_archives($gsdl_cgi, $username); } elsif ($cmd eq "download-collection-configurations") { &download_collection_configurations($gsdl_cgi, $username); } elsif ($cmd eq "download-collection-file") { &download_collection_file($gsdl_cgi, $username); } elsif ($cmd eq "delete-collection-file") { &delete_collection_file($gsdl_cgi, $username); } elsif ($cmd eq "get-script-options") { &get_script_options($gsdl_cgi, $username); } elsif ($cmd eq "move-collection-file") { &move_collection_file($gsdl_cgi, $username); } elsif ($cmd eq "new-collection-directory") { &new_collection_directory($gsdl_cgi, $username); } elsif ($cmd eq "run-script") { &run_script($gsdl_cgi, $username); } elsif ($cmd eq "timeout-test") { while (1) { } } elsif ($cmd eq "upload-collection-file") { &upload_collection_file($gsdl_cgi, $username); } else { $gsdl_cgi->generate_error("Unrecognised command: '$cmd'"); } } sub authenticate_user { my $gsdl_cgi = shift(@_); my $username = shift(@_); my $user_group_required = shift(@_); # Even if we're not authenticating remove the un and pw arguments, since these can mess up other scripts my $user_password = $gsdl_cgi->clean_param("pw"); $gsdl_cgi->delete("pw"); # Only authenticate if it is enabled return if (!$authentication_enabled); if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) { $gsdl_cgi->generate_error("Authentication failed: no password specified."); } my $gsdlhome = $ENV{'GSDLHOME'}; my $etc_directory = &util::filename_cat($gsdlhome, "etc"); my $users_db_file_path = &util::filename_cat($etc_directory, "users.db"); # Use db2txt instead of GDBM_File to get the user accounts information my $users_db_content = ""; open(USERS_DB, "db2txt \"$users_db_file_path\" |"); while () { $users_db_content .= $_; } # Get the user account information from the users.db database my %users_db_data = (); foreach my $users_db_entry (split(/-{70}/, $users_db_content)) { if ($users_db_entry =~ /\n?\[(.+)\]\n/) { $users_db_data{$1} = $users_db_entry; } } # Check username my $user_data = $users_db_data{$username}; if (!defined $user_data) { $gsdl_cgi->generate_error("Authentication failed: no account for user '$username'."); } # Check password my ($valid_user_password) = ($user_data =~ /\(.*)/); if ($user_password ne $valid_user_password) { $gsdl_cgi->generate_error("Authentication failed: incorrect password."); } # Check group my ($user_groups) = ($user_data =~ /\(.*)/); foreach my $user_group (split(/\,/, $user_groups)) { if ($user_group eq $user_group_required || $user_group eq "remote-superuser") { # Authentication successful! return; } } $gsdl_cgi->generate_error("Authentication failed: user is not in the required group."); } sub lock_collection { my $gsdl_cgi = shift(@_); my $collection = shift(@_); my $username = shift(@_); my $steal_lock = $gsdl_cgi->clean_param("steal_lock"); $gsdl_cgi->delete("steal_lock"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection); $gsdl_cgi->checked_chdir($collection_directory); # Check if a lock file already exists for this collection my $lock_file_name = "gli.lck"; if (-e $lock_file_name) { # A lock file already exists... check if it's ours open(LOCK_FILE, "<$lock_file_name"); my $lock_file_owner = ; close(LOCK_FILE); # The lock file is ours, so there is no problem if ($lock_file_owner eq $username) { return; } # The lock file is not ours, so throw an error unless "steal_lock" is set unless (defined $steal_lock) { $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner"); } } # Create a lock file for us and we're done open(LOCK_FILE, ">$lock_file_name"); print LOCK_FILE "$username"; close(LOCK_FILE); } # ---------------------------------------------------------------------------------------------------- # ACTIONS # ---------------------------------------------------------------------------------------------------- sub delete_collection { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collect_directory = &util::filename_cat($gsdlhome, "collect"); $gsdl_cgi->checked_chdir($collect_directory); # Check that the collection exists if (!-d $collection) { $gsdl_cgi->generate_error("Collection $collection does not exist."); } # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); $gsdl_cgi->checked_chdir($collect_directory); $gsdl_cgi->local_rm_r("$collection"); # Check that the collection was deleted if (-e $collection) { $gsdl_cgi->generate_error("Could not delete collection $collection."); } $gsdl_cgi->generate_ok_message("Collection $collection deleted successfully."); } sub delete_collection_file { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } my $file = $gsdl_cgi->clean_param("file"); if ((!defined $file) || ($file =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No file specified."); } $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS # Make sure we don't try to delete anything outside the collection if ($file =~ /\.\./) { $gsdl_cgi->generate_error("Illegal file specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection); $gsdl_cgi->checked_chdir($collection_directory); # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); # Check that the collection file exists if (!-e $file) { $gsdl_cgi->generate_error("Collection file $file does not exist."); } $gsdl_cgi->local_rm_r("$file"); # Check that the collection file was deleted if (-e $file) { $gsdl_cgi->generate_error("Could not delete collection file $file."); } $gsdl_cgi->generate_ok_message("Collection file $file deleted successfully."); } sub download_collection { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collect_directory = &util::filename_cat($gsdlhome, "collect"); $gsdl_cgi->checked_chdir($collect_directory); # Check that the collection exists if (!-d $collection) { $gsdl_cgi->generate_error("Collection $collection does not exist."); } # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); # Zip up the collection my $java = $gsdl_cgi->get_java_path(); my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar"); my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . $collection . ".zip"); my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\""; my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args"; my $java_output = `$java_command`; my $java_status = $?; if ($java_status > 0) { $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home()); } # Check that the zip file was created successfully if (!-e $zip_file_path || -z $zip_file_path) { $gsdl_cgi->generate_error("Collection zip file $zip_file_path could not be created."); } &put_file($gsdl_cgi, $zip_file_path, "application/zip"); unlink("$zip_file_path") unless $debugging_enabled; } sub download_collection_archives { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collect_directory = &util::filename_cat($gsdlhome, "collect"); $gsdl_cgi->checked_chdir($collect_directory); # Check that the collection archives exist if (!-d &util::filename_cat($collection, "archives")) { $gsdl_cgi->generate_error("Collection archives do not exist."); } # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); # Zip up the collection archives my $java = $gsdl_cgi->get_java_path(); my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar"); my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . $collection . "-archives.zip"); my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\""; my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args"; my $java_output = `$java_command`; my $java_status = $?; if ($java_status > 0) { $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home()); } # Check that the zip file was created successfully if (!-e $zip_file_path || -z $zip_file_path) { $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created."); } &put_file($gsdl_cgi, $zip_file_path, "application/zip"); unlink("$zip_file_path") unless $debugging_enabled; } # Collection locking unnecessary because this action isn't related to a particular collection sub download_collection_configurations { my ($gsdl_cgi, $username) = @_; # Users must be in the remote-collection-builder group to perform this action &authenticate_user($gsdl_cgi, $username, "remote-collection-builder"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collect_directory = &util::filename_cat($gsdlhome, "collect"); $gsdl_cgi->checked_chdir($collect_directory); # Zip up the collection configurations my $java = $gsdl_cgi->get_java_path(); my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar"); my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . "collection-configurations.zip"); my $java_args = "\"$zip_file_path\" \"$collect_directory\""; my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args"; my $java_output = `$java_command`; my $java_status = $?; if ($java_status > 0) { $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home()); } # Check that the zip file was created successfully if (!-e $zip_file_path || -z $zip_file_path) { $gsdl_cgi->generate_error("Collection configurations zip file $zip_file_path could not be created."); } &put_file($gsdl_cgi, $zip_file_path, "application/zip"); unlink("$zip_file_path") unless $debugging_enabled; } sub download_collection_file { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } my $file = $gsdl_cgi->clean_param("file"); if ((!defined $file) || ($file =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No file specified."); } $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS # Make sure we don't try to download anything outside the collection if ($file =~ /\.\./) { $gsdl_cgi->generate_error("Illegal file specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection); $gsdl_cgi->checked_chdir($collection_directory); # Check that the collection file exists if (!-e $file) { $gsdl_cgi->generate_error("Collection file $file does not exist."); } # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); # Zip up the collection file my $java = $gsdl_cgi->get_java_path(); my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar"); my $zip_file_path = &util::filename_cat($collection_directory, $username . "-" . $collection . "-file.zip"); my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\""; my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args"; my $java_output = `$java_command`; my $java_status = $?; if ($java_status > 0) { $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home()); } # Check that the zip file was created successfully if (!-e $zip_file_path || -z $zip_file_path) { $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created."); } &put_file($gsdl_cgi, $zip_file_path, "application/zip"); unlink("$zip_file_path") unless $debugging_enabled; } # Collection locking unnecessary because this action isn't related to a particular collection sub get_script_options { my ($gsdl_cgi, $username) = @_; my $script = $gsdl_cgi->clean_param("script"); if ((!defined $script) || ($script =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No script specified."); } $gsdl_cgi->delete("script"); # Users must be in the remote-collection-builder group to perform this action &authenticate_user($gsdl_cgi, $username, "remote-collection-builder"); my $perl_args = ""; if ($script eq "classinfo.pl") { $perl_args = $gsdl_cgi->clean_param("classifier") || ""; $gsdl_cgi->delete("classifier"); } if ($script eq "pluginfo.pl") { $perl_args = $gsdl_cgi->clean_param("plugin") || ""; $gsdl_cgi->delete("plugin"); } foreach my $cgi_arg_name ($gsdl_cgi->param) { my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name)); if ($cgi_arg_value eq "") { $perl_args = "-$cgi_arg_name " . $perl_args; } else { $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args; } } my $perl_command = "perl -S $script $perl_args 2>&1"; my $perl_output = `$perl_command`; my $perl_status = $?; if ($perl_status > 0) { $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256)); } print STDOUT "Content-type:text/plain\n\n"; print STDOUT $perl_output; } sub move_collection_file { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } my $source_file = $gsdl_cgi->clean_param("source"); if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No source file specified."); } $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS my $target_file = $gsdl_cgi->clean_param("target"); if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No target file specified."); } $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS # Make sure we don't try to move anything outside the collection if ($source_file =~ /\.\./ || $target_file =~ /\.\./) { $gsdl_cgi->generate_error("Illegal file specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection); $gsdl_cgi->checked_chdir($collection_directory); # Check that the collection source file exists if (!-e $source_file) { $gsdl_cgi->generate_error("Collection file $source_file does not exist."); } # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); &util::mv($source_file, $target_file); # Check that the collection source file was moved if (-e $source_file || !-e $target_file) { $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file."); } $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully."); } sub new_collection_directory { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } my $directory = $gsdl_cgi->clean_param("directory"); if ((!defined $directory) || ($directory =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No directory specified."); } $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS # Make sure we don't try to create anything outside the collection if ($directory =~ /\.\./) { $gsdl_cgi->generate_error("Illegal directory specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection); $gsdl_cgi->checked_chdir($collection_directory); # Check that the collection directory doesn't already exist if (-d $directory) { $gsdl_cgi->generate_error("Collection directory $directory already exists."); } # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); &util::mk_dir($directory); # Check that the collection directory was created if (!-d $directory) { $gsdl_cgi->generate_error("Could not create collection directory $directory."); } $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully."); } sub run_script { my ($gsdl_cgi, $username) = @_; my $script = $gsdl_cgi->clean_param("script"); if ((!defined $script) || ($script =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No script specified."); } $gsdl_cgi->delete("script"); my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } $gsdl_cgi->delete("c"); # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course) &lock_collection($gsdl_cgi, $collection, $username) unless ($script eq "mkcol.pl"); my $perl_args = $collection; foreach my $cgi_arg_name ($gsdl_cgi->param) { my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name)); if ($cgi_arg_value eq "") { $perl_args = "-$cgi_arg_name " . $perl_args; } else { $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args; } } my $perl_command = "perl -S $script $perl_args 2>&1"; if (!open(PIN, "$perl_command |")) { $gsdl_cgi->generate_error("Unable to execute command: $perl_command"); } print STDOUT "Content-type:text/plain\n\n"; while (defined (my $perl_output_line = )) { print STDOUT $perl_output_line; } close(PIN); my $perl_status = $?; if ($perl_status > 0) { $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256)); } } sub upload_collection_file { my ($gsdl_cgi, $username) = @_; my $collection = $gsdl_cgi->clean_param("c"); if ((!defined $collection) || ($collection =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No collection specified."); } my $file = $gsdl_cgi->clean_param("file"); if ((!defined $file) || ($file =~ m/^\s*$/)) { $gsdl_cgi->generate_error("No file specified."); } my $directory = $gsdl_cgi->clean_param("directory") || ""; $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS my $zip = $gsdl_cgi->clean_param("zip"); # Make sure we don't try to upload anything outside the collection if ($file =~ /\.\./) { $gsdl_cgi->generate_error("Illegal file specified."); } if ($directory =~ /\.\./) { $gsdl_cgi->generate_error("Illegal directory specified."); } # Users must be in the -maintainer group to perform this action &authenticate_user($gsdl_cgi, $username, "$collection-maintainer"); my $gsdlhome = $ENV{'GSDLHOME'}; my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection); $gsdl_cgi->checked_chdir($collection_directory); # Make sure the collection isn't locked by someone else &lock_collection($gsdl_cgi, $collection, $username); my $directory_path = &util::filename_cat($collection_directory, $directory); &util::mk_dir($directory_path); if (!-e $directory_path) { $gsdl_cgi->generate_error("Could not create directory $directory_path."); } my $file_path = &util::filename_cat($directory_path, $username . "-" . $file); if (!open(FOUT, ">$file_path")) { $gsdl_cgi->generate_error("Unable to write file $file_path"); } # Read the uploaded data and write it out to file my $buf; my $num_bytes = 0; binmode(FOUT); while (read(STDIN, $buf, 1024) > 0) { print FOUT $buf; $num_bytes += length($buf); } close(FOUT); # If we have downloaded a zip file, unzip it if (defined $zip) { my $java = $gsdl_cgi->get_java_path(); my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar"); my $java_args = "\"$file_path\" \"$directory_path\""; my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.util.Unzip $java_args"; my $java_output = `$java_command`; my $java_status = $?; # Remove the zip file once we have unzipped it, since it is an intermediate file only unlink("$file_path"); if ($java_status > 0) { $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home()); } } $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully."); } sub put_file { my $gsdl_cgi = shift(@_); my $file_path = shift(@_); my $content_type = shift(@_); if (open(PIN, "<$file_path")) { print STDOUT "Content-type:$content_type\n\n"; my $buf; my $num_bytes = 0; binmode(PIN); while (read(PIN, $buf, 1024) > 0) { print STDOUT $buf; $num_bytes += length($buf); } close(PIN); } else { $gsdl_cgi->generate_error("Unable to read file $file_path\n $!"); } } &main();