-#!/usr/bin/perl -w
+#!/usr/bin/perl
#
-# setup-openssh.pl - substitutes variables into text files and runs
-# ssh key gen programs
+# setup-openssh.pl
#
-# adapted from 'fixpath', located in the openssh-3.0.2p1 package
+# Adapts the installed gsi-openssh environment to the current machine,
+# performing actions that originally occurred during the package's
+# 'make install' phase.
#
+# Send comments/fixes/suggestions to:
+# Chase Phillips <cphillip@ncsa.uiuc.edu>
+#
+
+printf("setup-openssh.pl: Configuring gsi-openssh package\n");
+
+#
+# Get user's GPT_LOCATION since we may be installing this using a new(er)
+# version of GPT.
+#
+
+$gptpath = $ENV{GPT_LOCATION};
+
+#
+# And the old standby..
+#
+
+$gpath = $ENV{GLOBUS_LOCATION};
+if (!defined($gpath))
+{
+ die "GLOBUS_LOCATION needs to be set before running this script"
+}
+
+#
+# modify the ld library path for when we call ssh executables
+#
+
+$oldldpath = $ENV{LD_LIBRARY_PATH};
+$newldpath = "$gpath/lib";
+if (length($oldldpath) > 0)
+{
+ $newldpath .= ":$oldldpath";
+}
+$ENV{LD_LIBRARY_PATH} = "$newldpath";
+
+#
+# i'm including this because other perl scripts in the gpt setup directories
+# do so
+#
+
+if (defined($gptpath))
+{
+ @INC = (@INC, "$gptpath/lib/perl", "$gpath/lib/perl");
+}
+else
+{
+ @INC = (@INC, "$gpath/lib/perl");
+}
+
+require Grid::GPT::Setup;
+
+my $globusdir = $gpath;
+my $myname = "setup-openssh.pl";
#
# Set up path prefixes for use in the path translations
#
-$prefix = "/home/cphillip/gsi-openssh/install";
-$exec_prefix = "$prefix";
-$bindir = "$exec_prefix/bin";
-$libexecdir = "$exec_prefix/libexec";
-$sysconfdir = "$prefix/etc";
-$piddir = "/var/run";
-$xauth_path = "/usr/bin/X11/xauth";
+$prefix = ${globusdir};
+$exec_prefix = "${prefix}";
+$bindir = "${exec_prefix}/bin";
+$sbindir = "${exec_prefix}/sbin";
+$sysconfdir = "$prefix/etc/ssh";
+$localsshdir = "/etc/ssh";
+$setupdir = "$prefix/setup/gsi_openssh_setup";
-sub fixpaths
+my $keyfiles = {
+ "dsa" => "ssh_host_dsa_key",
+ "rsa" => "ssh_host_rsa_key",
+ "rsa1" => "ssh_host_key",
+ };
+
+sub copyKeyFiles
+{
+ my($copylist) = @_;
+ my($regex, $basename);
+
+ if (@$copylist)
+ {
+ print "Copying ssh host keys...\n";
+
+ for my $f (@$copylist)
+ {
+ $f =~ s:/+:/:g;
+
+ if (length($f) > 0)
+ {
+ $keyfile = "$f";
+ $pubkeyfile = "$f.pub";
+
+ action("cp $localsshdir/$keyfile $sysconfdir/$keyfile");
+ action("cp $localsshdir/$pubkeyfile $sysconfdir/$pubkeyfile");
+ }
+ }
+ }
+}
+
+sub isReadable
+{
+ my($file) = @_;
+
+ if ( ( -e $file ) && ( -r $file ) )
+ {
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+sub isPresent
+{
+ my($file) = @_;
+
+ if ( -e $file )
+ {
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+### makeConfDir( )
+#
+# make the gsi-openssh configuration directory if it doesn't already exist.
+#
+
+sub makeConfDir
+{
+ if ( isPresent($sysconfdir) )
+ {
+ if ( -d $sysconfdir )
+ {
+ return;
+ }
+
+ die("${sysconfdir} already exists and is not a directory!\n");
+ }
+
+ print "Could not find ${sysconfdir} directory... creating.\n";
+ action("mkdir -p $sysconfdir");
+
+ return;
+}
+
+sub determineKeys
{
+ my($keyhash, $keylist);
+ my($count);
+
#
- # Set up path translations for the installation files
+ # initialize our variables
#
- %def = (
- "/etc/ssh_config" => "${sysconfdir}/ssh_config",
- "/etc/ssh_known_hosts" => "${sysconfdir}/ssh_known_hosts",
- "/etc/sshd_config" => "${sysconfdir}/sshd_config",
- "/usr/libexec" => "${libexecdir}",
- "/etc/shosts.equiv" => "${sysconfdir}/shosts.equiv",
- "/etc/ssh_host_key" => "${sysconfdir}/ssh_host_key",
- "/etc/ssh_host_dsa_key" => "${sysconfdir}/ssh_host_dsa_key",
- "/etc/ssh_host_rsa_key" => "${sysconfdir}/ssh_host_rsa_key",
- "/var/run/sshd.pid" => "${piddir}/sshd.pid",
- "/etc/moduli" => "${sysconfdir}/moduli",
- "/etc/sshrc" => "${sysconfdir}/sshrc",
- "/usr/X11R6/bin/xauth" => "${xauth_path}",
- "/usr/bin:/bin:/usr/sbin:/sbin" => "/usr/bin:/bin:/usr/sbin:/sbin:${bindir}",
- );
+ $count = 0;
+
+ $keyhash = {};
+ $keyhash->{gen} = []; # a list of keytypes to generate
+ $keyhash->{copy} = []; # a list of files to copy from the
+
+ $genlist = $keyhash->{gen};
+ $copylist = $keyhash->{copy};
#
- # Files on which to perform path translations
+ # loop over our keytypes and determine what we need to do for each of them
#
- @files = (
- "ssh_config",
- "sshd_config",
- "moduli",
- "scp.1",
- "ssh-add.1",
- "ssh-agent.1",
- "ssh-keygen.1",
- "ssh-keyscan.1",
- "ssh.1",
- "sshd.8",
- "sftp-server.8",
- "sftp.1",
- );
+ for my $keytype (keys %$keyfiles)
+ {
+ $basekeyfile = $keyfiles->{$keytype};
+
+ #
+ # if the key's are already present, we don't need to bother with this rigamarole
+ #
+
+ $gkeyfile = "$sysconfdir/$basekeyfile";
+ $gpubkeyfile = "$sysconfdir/$basekeyfile.pub";
+
+ if ( isPresent($gkeyfile) && isPresent($gpubkeyfile) )
+ {
+ next;
+ }
+
+ #
+ # if we can find a copy of the keys in /etc/ssh, we'll copy them to the user's
+ # globus location
+ #
- print "\nTranslating strings in config/man files..\n";
- for $f (@files)
+ $mainkeyfile = "$localsshdir/$basekeyfile";
+ $mainpubkeyfile = "$localsshdir/$basekeyfile.pub";
+
+ if ( isReadable($mainkeyfile) && isReadable($mainpubkeyfile) )
+ {
+ push(@$copylist, $basekeyfile);
+ $count++;
+ next;
+ }
+
+ #
+ # otherwise, we need to generate the key
+ #
+
+ push(@$genlist, $keytype);
+ $count++;
+ }
+
+ if ($count > 0)
{
- $f =~ /(.*\/)*(.*)$/;
- $g = "$f.out";
+ if ( ! -d $sysconfdir )
+ {
+ print "Could not find ${sysconfdir} directory... creating\n";
+ action("mkdir -p $sysconfdir");
+ }
+ }
- open(IN, "<$f") || die ("$0: input file $f missing!\n");
+ return $keyhash;
+}
- if ( -e $g )
+sub runKeyGen
+{
+ my($gen_keys) = @_;
+ my $keygen = "$bindir/ssh-keygen";
+
+ if (@$gen_keys && -x $keygen)
+ {
+ print "Generating ssh host keys...\n";
+
+ for my $k (@$gen_keys)
{
- print "$g already exists, skipping.\n";
+ $keyfile = $keyfiles->{$k};
+
+ # if $sysconfdir/$keyfile doesn't exist..
+ action("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
+ }
+ }
+
+ return 0;
+}
+
+### fixpaths( )
+#
+# this subroutine 'edits' the paths in sshd_config to suit them to the current environment
+# in which the setup script is being run.
+#
+
+sub fixpaths
+{
+ my($fileInput, $fileOutput);
+ my($mode, $uid, $gid);
+ my($line, $newline);
+
+ print "Fixing paths in sshd_config...\n";
+
+ $fileInput = "$setupdir/sshd_config.in";
+ $fileOutput = "$sysconfdir/sshd_config";
+
+ if ( ! -f "$fileInput" )
+ {
+ printf("Cannot find $fileInput!\n");
+ die();
+ }
+
+ if ( -e "$fileOutput" )
+ {
+ printf("$fileOutput already exists!\n");
+ die();
+ }
+
+ #
+ # 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
+ #
+
+ open(IN, "<$fileInput") || die ("$0: input file $fileInput missing!\n");
+ open(OUT, ">$fileOutput") || die ("$0: unable to open output file $fileOutput!\n");
+
+ while (<IN>)
+ {
+ #
+ # sorry for the whacky regex, but i need to verify a whole line
+ #
+
+ $line = $_;
+ if ( $line =~ /^\s*Subsystem\s+sftp\s+\S+\s*$/ )
+ {
+ $newline = "Subsystem\tsftp\t$gpath/libexec/sftp-server\n";
+ $newline =~ s:/+:/:g;
+ }
+ elsif ( $line =~ /^\s*PidFile.*$/ )
+ {
+ $newline = "PidFile\t$gpath/var/sshd.pid\n";
+ $newline =~ s:/+:/:g;
}
else
{
- open(OUT, ">$g") || die ("$0: unable to open output file $g!\n");
-
- while (<IN>)
- {
- for $s (keys(%def))
- {
- s#$s#$def{$s}#;
- } # for $s
- print OUT "$_";
- } # while <IN>
-
- close(OUT);
+ $newline = $line;
}
- close(IN);
- } # for $f
+ print OUT "$newline";
+ } # while <IN>
+
+ close(OUT);
+ close(IN);
+
+ #
+ # 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;
}
-sub runkeygen
+### copyConfigFiles( )
+#
+# subroutine that copies some extra config files to their proper location in
+# $GLOBUS_LOCATION/etc/ssh.
+#
+
+sub copyConfigFiles
+{
+ print "Copying ssh_config and moduli to their proper location...\n";
+
+ action("cp $setupdir/ssh_config $sysconfdir/ssh_config");
+ action("cp $setupdir/moduli $sysconfdir/moduli");
+}
+
+sub alterFileGlobusLocation
{
- print "\nGenerating ssh keys (if necessary)..\n";
- if ( -e "${sysconfdir}/ssh_host_key" )
+ my ($in, $out) = @_;
+
+ if ( -r $in )
{
- print "${sysconfdir}/ssh_host_key already exists, skipping.\n";
+ if ( ( -w $out ) || ( ! -e $out ) )
+ {
+ $data = readFile($in);
+ $data =~ s|\@GLOBUS_LOCATION\@|$gpath|g;
+ writeFile($out, $data);
+ action("chmod 755 $out");
+ }
}
- else
+}
+
+sub alterFiles
+{
+ alterFileGlobusLocation("$setupdir/SXXsshd.in", "$sbindir/SXXsshd");
+}
+
+### readFile( $filename )
+#
+# reads and returns $filename's contents
+#
+
+sub readFile
+{
+ my ($filename) = @_;
+ my $data;
+
+ open (IN, "$filename") || die "Can't open '$filename': $!";
+ $/ = undef;
+ $data = <IN>;
+ $/ = "\n";
+ close(IN);
+
+ return $data;
+}
+
+### writeFile( $filename, $fileinput )
+#
+# create the inputs to the ssl program at $filename, appending the common name to the
+# stream in the process
+#
+
+sub writeFile
+{
+ my ($filename, $fileinput) = @_;
+
+ #
+ # test for a valid $filename
+ #
+
+ if ( !defined($filename) || (length($filename) lt 1) )
+ {
+ die "Filename is undefined";
+ }
+
+ if ( ( -e "$filename" ) && ( ! -w "$filename" ) )
+ {
+ die "Cannot write to filename '$filename'";
+ }
+
+ #
+ # write the output to $filename
+ #
+
+ open(OUT, ">$filename");
+ print OUT "$fileinput";
+ close(OUT);
+}
+
+print "---------------------------------------------------------------------\n";
+print "Hi, I'm the setup script for the gsi_openssh package! There\n";
+print "are some last minute details that I've got to set straight\n";
+print "in the sshd config file, along with generating the ssh keys\n";
+print "for this machine (if it doesn't already have them).\n";
+print "\n";
+print "If I find a pair of host keys in /etc/ssh, I will copy them into\n";
+print "\$GLOBUS_LOCATION/etc/ssh. If they aren't present, I will generate\n";
+print "them for you.\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();
+$keyhash = determineKeys();
+runKeyGen($keyhash->{gen});
+copyKeyFiles($keyhash->{copy});
+fixpaths();
+copyConfigFiles();
+alterFiles();
+
+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 " \t\"$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 run, e.g.:\n";
+print "\n";
+print " \t\$ . \$GLOBUS_LOCATION/etc/globus-user-env.sh\n";
+print "\n";
+print " to prepare your environment for running the gsi_openssh\n";
+print " executables.\n";
+print "\n";
+print "---------------------------------------------------------------------\n";
+print "$myname: Finished configuring package 'gsi_openssh'.\n";
+
+#
+# Just need a minimal action() subroutine for now..
+#
+
+sub action
+{
+ my ($command) = @_;
+
+ printf "$command\n";
+
+ my $result = system("LD_LIBRARY_PATH=\"$gpath/lib:\$LD_LIBRARY_PATH\"; $command 2>&1");
+
+ if (($result or $?) and $command !~ m!patch!)
{
- # if $sysconfdir/ssh_host_key doesn't exist..
- system("$bindir/ssh-keygen -t rsa1 -f $sysconfdir/ssh_host_key -N \"\"");
+ die "ERROR: Unable to execute command: $!\n";
}
+}
+
+sub query_boolean
+{
+ my ($query_text, $default) = @_;
+ my $nondefault, $foo, $bar;
+
+ #
+ # Set $nondefault to the boolean opposite of $default.
+ #
- if ( -e "${sysconfdir}/ssh_host_dsa_key" )
+ if ($default eq "n")
{
- print "${sysconfdir}/ssh_host_dsa_key already exists, skipping.\n";
+ $nondefault = "y";
}
else
{
- # if $sysconfdir/ssh_host_dsa_key doesn't exist..
- system("$bindir/ssh-keygen -t dsa -f $sysconfdir/ssh_host_dsa_key -N \"\"");
+ $nondefault = "n";
}
- if ( -e "${sysconfdir}/ssh_host_rsa_key" )
+ print "${query_text} ";
+ print "[$default] ";
+
+ $foo = <STDIN>;
+ ($bar) = split //, $foo;
+
+ if ( grep(/\s/, $bar) )
{
- print "${sysconfdir}/ssh_host_rsa_key already exists, skipping.\n";
+ # this is debatable. all whitespace means 'default'
+
+ $bar = $default;
+ }
+ elsif ($bar ne $default)
+ {
+ # everything else means 'nondefault'.
+
+ $bar = $nondefault;
}
else
{
- # if $sysconfdir/ssh_host_rsa_key doesn't exist..
- system("$bindir/ssh-keygen -t rsa -f $sysconfdir/ssh_host_rsa_key -N \"\"");
+ # extraneous step. to get here, $bar should be eq to $default anyway.
+
+ $bar = $default;
}
- return 0;
+ return $bar;
}
-fixpaths();
+### absolutePath( $file )
+#
+# converts a given pathname into a canonical path using the abs_path function.
+#
-runkeygen();
+sub absolutePath
+{
+ my($file) = @_;
+ my $home = $ENV{'HOME'};
+ $file =~ s!~!$home!;
+ my $startd = cwd();
+ $file =~ s!^\./!$startd/!;
+ $file = "$startd/$file" if $file !~ m!^\s*/!;
+ $file = abs_path($file);
+ return $file;
+}