+#
+# argument specification. we offload some processing work from later functions
+# to verify correct args by using anon subs in various places.
+#
+
+my($interactive, $force, $verbose);
+
+GetOptions(
+ 'interactive!' => \$interactive,
+ 'force' => \$force,
+ 'verbose' => \$verbose,
+ ) or pod2usage(2);
+
+#
+# miscellaneous initialization functions
+#
+
+setPrivilegeSeparation(0);
+
+#
+# main execution. This should find its way into a subroutine at some future
+# point.
+#
+
+print "$myname: Configuring package 'gsi_openssh'...\n";
+print "---------------------------------------------------------------------\n";
+print "Hi, I'm the setup script for the gsi_openssh package! I will create\n";
+print "a number of configuration files based on your local system setup. I\n";
+print "will also attempt to copy or create a number of SSH key pairs for\n";
+print "this machine. (Loosely, if I find a pair of host keys in /etc/ssh,\n";
+print "I will copy them into \$GLOBUS_LOCATION/etc/ssh. Otherwise, I will\n";
+print "generate them for you.)\n";
+print "\n";
+print " Jacobim Mugatu says,\n";
+print " \"Utopian Prime Minister Bad! GSI-OpenSSH Good!\"\n";
+print "\n";
+
+if ( isForced() )
+{
+ print "WARNING:\n";
+ print "\n";
+ print " Using the '-force' flag will cause all gsi_openssh_setup files to\n";
+ print " be removed and replaced by new versions! Backup any critical\n";
+ print " SSH configuration files before you choose to continue!\n";
+ print "\n";
+}
+
+$response = query_boolean("Do you wish to continue with the setup package?","y");
+if ($response eq "n")
+{
+ print "\n";
+ print "Exiting gsi_openssh setup.\n";
+
+ exit 0;
+}
+
+print "\n";
+
+makeConfDir();
+copyPRNGFile();
+$keyhash = determineKeys();
+runKeyGen($keyhash->{gen});
+copyKeyFiles($keyhash->{copy});
+copyConfigFiles();
+
+my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
+
+$metadata->finish();
+
+print "\n";
+print "Additional Notes:\n";
+print "\n";
+print " o I see that you have your GLOBUS_LOCATION environmental variable\n";
+print " set to:\n";
+print "\n";
+print " \"$gpath\"\n";
+print "\n";
+print " Remember to keep this variable set (correctly) when you want to\n";
+print " use the executables that came with this package.\n";
+print "\n";
+print " After that you may execute, for example:\n";
+print "\n";
+print " \$ . \$GLOBUS_LOCATION/etc/globus-user-env.sh\n";
+print "\n";
+print " to prepare your environment for running the gsi_openssh\n";
+print " executables.\n";
+
+if ( !getPrivilegeSeparation() )
+{
+ print "\n";
+ print " o For System Administrators:\n";
+ print "\n";
+ print " If you are going to run the GSI-OpenSSH server, we recommend\n";
+ print " enabling privilege separation. Although this package supports\n";
+ print " this feature, your system appears to require some additional\n";
+ print " configuration.\n";
+ print "\n";
+ print " From the file README.privsep, included as a part of the OpenSSH\n";
+ print " distribution:\n";
+ print "\n";
+ print " When privsep is enabled, during the pre-authentication\n";
+ print " phase sshd will chroot(2) to \"/var/empty\" and change its\n";
+ print " privileges to the \"sshd\" user and its primary group. sshd\n";
+ print " is a pseudo-account that should not be used by other\n";
+ print " daemons, and must be locked and should contain a \"nologin\"\n";
+ print " or invalid shell.\n";
+ print "\n";
+ print " You should do something like the following to prepare the\n";
+ print " privsep preauth environment:\n";
+ print "\n";
+ print " \# mkdir /var/empty\n";
+ print " \# chown root:sys /var/empty\n";
+ print " \# chmod 755 /var/empty\n";
+ print " \# groupadd sshd\n";
+ print " \# useradd -g sshd -c 'sshd privsep' -d /var/empty \\\n";
+ print " -s /bin/false sshd\n";
+ print "\n";
+ print " /var/empty should not contain any files.\n";
+}
+
+print "\n";
+print " o For more information about GSI-Enabled OpenSSH, visit:\n";
+print " <http://www.ncsa.uiuc.edu/Divisions/ACES/GSI/openssh/>\n";
+
+#
+# give the user a chance to read all of this output
+#
+
+print "\n";
+print "Press <return> to continue... ";
+$trash = <STDIN>;
+
+print "---------------------------------------------------------------------\n";
+print "$myname: Finished configuring package 'gsi_openssh'.\n";
+
+exit;
+
+#
+# subroutines
+#
+
+### initPRNGHash( )
+#
+# initialize the PRNG pathname hash
+#
+
+sub initPRNGHash( )
+{
+ #
+ # standard prng to executable conversion names
+ #
+
+ addPRNGCommand("\@PROG_LS\@", "ls");
+ addPRNGCommand("\@PROG_NETSTAT\@", "netstat");
+ addPRNGCommand("\@PROG_ARP\@", "arp");
+ addPRNGCommand("\@PROG_IFCONFIG\@", "ifconfig");
+ addPRNGCommand("\@PROG_PS\@", "ps");
+ addPRNGCommand("\@PROG_JSTAT\@", "jstat");
+ addPRNGCommand("\@PROG_W\@", "w");
+ addPRNGCommand("\@PROG_WHO\@", "who");
+ addPRNGCommand("\@PROG_LAST\@", "last");
+ addPRNGCommand("\@PROG_LASTLOG\@", "lastlog");
+ addPRNGCommand("\@PROG_DF\@", "df");
+ addPRNGCommand("\@PROG_SAR\@", "sar");
+ addPRNGCommand("\@PROG_VMSTAT\@", "vmstat");
+ addPRNGCommand("\@PROG_UPTIME\@", "uptime");
+ addPRNGCommand("\@PROG_IPCS\@", "ipcs");
+ addPRNGCommand("\@PROG_TAIL\@", "tail");
+
+ print "Determining paths for PRNG commands...\n";
+
+ $paths = determinePRNGPaths();
+
+ return;
+}
+
+### getDirectoryPaths( )
+#
+# return an array ref containing all of the directories in which we should search
+# for our listing of executable names.
+#
+
+sub getDirectoryPaths( )
+{
+ #
+ # read in the PATH environmental variable and prepend a set of 'safe'
+ # directories from which to test PRNG commands.
+ #
+
+ $path = $ENV{PATH};
+ $path = "/bin:/usr/bin:/sbin:/usr/sbin:/etc:" . $path;
+ @dirs = split(/:/, $path);
+
+ #
+ # sanitize each directory listed in the array.
+ #
+
+ @dirs = map {
+ $tmp = $_;
+ $tmp =~ s:/+:/:g;
+ $tmp =~ s:^\s+|\s+$::g;
+ $tmp;
+ } @dirs;
+
+ return \@dirs;
+}
+
+### addPRNGCommand( $prng_name, $exec_name )
+#
+# given a PRNG name and a corresponding executable name, add it to our list of
+# PRNG commands for which to find on the system.
+#
+
+sub addPRNGCommand
+{
+ my($prng_name, $exec_name) = @_;
+
+ prngAddNode($prng_name, $exec_name);
+}
+
+### copyPRNGFile( )
+#
+# read in ssh_prng_cmds.in, translate the program listings to the paths we have
+# found on the local system, and then write the output to ssh_prng_cmds.
+#
+
+sub copyPRNGFile
+{
+ my($fileInput, $fileOutput);
+ my($mode, $uid, $gid);
+ my($data);
+
+ if ( isPresent("$sysconfdir/ssh_prng_cmds") && !isForced() )
+ {
+ printf("ssh_prng_cmds found and not forced. Not installing ssh_prng_cmds...\n");
+ return;
+ }
+
+ initPRNGHash();
+
+ print "Fixing paths in ssh_prng_cmds...\n";
+
+ $fileInput = "$setupdir/ssh_prng_cmds.in";
+ $fileOutput = "$sysconfdir/ssh_prng_cmds";
+
+ #
+ # verify that we are prepared to work with $fileInput
+ #
+
+ if ( !isReadable($fileInput) )
+ {
+ printf("Cannot read $fileInput... skipping.\n");
+ return;
+ }
+
+ #
+ # verify that we are prepared to work with $fileOuput
+ #
+
+ if ( !prepareFileWrite($fileOutput) )
+ {
+ return;
+ }
+
+ #
+ # Grab the current mode/uid/gid for use later
+ #
+
+ $mode = (stat($fileInput))[2];
+ $uid = (stat($fileInput))[4];
+ $gid = (stat($fileInput))[5];
+
+ #
+ # Open the files for reading and writing, and loop over the input's contents
+ #
+
+ $data = readFile($fileInput);
+ for my $k (keys %$prngcmds)
+ {
+ $sub = prngGetExecPath($k);
+ $data =~ s:$k:$sub:g;
+ }
+ writeFile($fileOutput, $data);
+
+ #
+ # An attempt to revert the new file back to the original file's
+ # mode/uid/gid
+ #
+
+ chmod($mode, $fileOutput);
+ chown($uid, $gid, $fileOutput);
+
+ return 0;
+}
+
+### determinePRNGPaths( )
+#
+# for every entry in the PRNG hash, seek out and find the path for the
+# corresponding executable name.
+#
+
+sub determinePRNGPaths
+{
+ my(@paths, @dirs);
+ my($exec_name, $exec_path);
+
+ $dirs = getDirectoryPaths();
+
+ for my $k (keys %$prngcmds)
+ {
+ $exec_name = prngGetExecName($k);
+ $exec_path = findExecutable($exec_name, $dirs);
+ prngSetExecPath($k, $exec_path);
+ }
+
+ return;
+}
+
+### prngAddNode( $prng_name, $exec_name )
+#
+# add a new node to the PRNG hash
+#
+
+sub prngAddNode
+{
+ my($prng_name, $exec_name) = @_;
+ my($node);
+
+ if (!defined($prngcmds))
+ {
+ $prngcmds = {};
+ }
+
+ $node = {};
+ $node->{prng} = $prng_name;
+ $node->{exec} = $exec_name;
+
+ $prngcmds->{$prng_name} = $node;
+}
+
+### prngGetExecName( $key )
+#
+# get the executable name from the prng commands hash named by $key
+#
+
+sub prngGetExecName
+{
+ my($key) = @_;
+
+ return $prngcmds->{$key}->{exec};
+}
+
+### prngGetExecPath( $key )
+#
+# get the executable path from the prng commands hash named by $key
+#
+
+sub prngGetExecPath
+{
+ my($key) = @_;
+
+ return $prngcmds->{$key}->{exec_path};
+}
+
+### prngGetNode( $key )
+#
+# return a reference to the node named by $key
+#
+
+sub prngGetNode
+{
+ my($key) = @_;
+
+ return ${$prngcmds}{$key};
+}
+
+### prngSetExecPath( $key, $path )
+#
+# given a key, set the executable path in that node to $path
+#
+
+sub prngSetExecPath
+{
+ my($key, $path) = @_;
+
+ $prngcmds->{$key}->{exec_path} = $path;
+}
+
+### findExecutable( $exec_name, $dirs )
+#
+# given an executable name, test each possible path in $dirs to see if such
+# an executable exists.
+#
+
+sub findExecutable
+{
+ my($exec_name, $dirs) = @_;
+
+ for my $d (@$dirs)
+ {
+ $test = "$d/$exec_name";
+
+ if ( isExecutable($test) )
+ {
+ return $test;
+ }
+ }
+
+ return "undef";
+}
+
+### copyKeyFiles( $copylist )
+#
+# given an array of keys to copy, copy both the key and its public variant into
+# the gsi-openssh configuration directory.
+#
+