]> andersk Git - gssapi-openssh.git/blobdiff - setup/setup-openssh.pl
o Checkpoint commit of changes to setup-openssh.pl.
[gssapi-openssh.git] / setup / setup-openssh.pl
index a83ed02e815459182cfad4d431101f4e89bbceba..574d3f15508854c90d76a8a7e1f5e352c95a0811 100644 (file)
@@ -1,15 +1,30 @@
+#!/usr/bin/perl
 #
-# setup-openssh.pl:
-#   Adapts the installed gsi-ssh environment to the current machine,
-#   performing actions that originally occurred during the package's
-#   'make install' phase.
+# setup-openssh.pl
 #
-# Large parts adapted from 'fixpath', a tool found in openssh-3.0.2p1.
+# 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>
 #
 
+use Data::Dumper;
+
+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))
 {
@@ -21,12 +36,18 @@ if (!defined($gpath))
 # do so
 #
 
-@INC = (@INC, "$gpath/lib/perl");
+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 $setupdir = "$globusdir/setup/globus";
 my $myname = "setup-openssh.pl";
 
 #
@@ -36,186 +57,129 @@ my $myname = "setup-openssh.pl";
 $prefix = ${globusdir};
 $exec_prefix = "${prefix}";
 $bindir = "${exec_prefix}/bin";
-$sbindir = "${exec_prefix}/sbin";
-$mandir = "${prefix}/man";
-$mansubdir = "man";
-$libexecdir = "${exec_prefix}/libexec";
-$sysconfdir = "/etc/ssh";
-$piddir = "/var/run";
-$xauth_path = "/usr/bin/X11/xauth";
+$sysconfdir = "$prefix/etc/ssh";
+$localsshdir = "/etc/ssh";
 
-#
-# Backup-related variables
-#
-
-$curr_time = time();
-$backupdir = "globus_backup_${curr_time}";
-$confbackupdir = "$backupdir/s1_conf";
-$transbackupdir = "$backupdir/s3_trans";
+my $keyfiles = {
+                 "dsa" => "ssh_host_dsa_key",
+                 "rsa" => "ssh_host_rsa_key",
+                 "rsa1" => "ssh_host_key",
+               };
 
 #
-# We need to make sure it's okay to copy our setup files (if some files are already
-# present).  If we do copy any files, we backup the old files so the user can (possibly)
-# reverse any damage.
+# Check that we are running as root
 #
 
-sub test_dirs
-{
-    my $composite;
+$uid = $>;
 
-    print "\nPreparatory: checking for existence of critical directories\n";
+if ($uid != 0)
+{
+    print "--> NOTE: You must be root to run this script! <--\n";
+    exit 0;
+}
 
-    #
-    # Remember to put in check for /etc
-    #
+sub copyKeyFiles
+{
+    my($copylist) = @_;
+    my($regex, $basename);
 
-    #
-    # Test for /etc/ssh
-    #
+    print "Copying ssh host keys...\n";
 
-    $composite = $sysconfdir;
-    if ( ! -d "$composite" )
+    for my $f (@$copylist)
     {
-        print "Could not find directory: '${composite}'.. creating.\n";
-        mkdir($composite, 16877);
-        # 16877 should be 755, or drwxr-xr-x
-    }
-
-    #
-    # Test for /etc/ssh/globus_backup_<curr>
-    #
+        $f =~ s:/+:/:g;
 
-    $composite = "$sysconfdir/$backupdir";
-    if ( ! -d "${composite}" )
-    {
-        print "Could not find directory: '${composite}'.. creating.\n";
-        mkdir($composite, 16877);
+        if (length($f) > 0)
+        {
+            $keyfile = "$f";
+            $pubkeyfile = "$f.pub";
+
+#            printf("cp $localsshdir/$keyfile $sysconfdir/$keyfile");
+#            printf("\n");
+#            printf("cp $localsshdir/$pubkeyfile $sysconfdir/$pubkeyfile");
+#            printf("\n");
+            action("cp $localsshdir/$keyfile $sysconfdir/$keyfile");
+            action("cp $localsshdir/$pubkeyfile $sysconfdir/$pubkeyfile");
+        }
     }
+}
 
-    #
-    # Test for /etc/ssh/globus_backup_<curr>/s1_conf
-    #
+sub isReadable
+{
+    my($file) = @_;
 
-    $composite = "$sysconfdir/$confbackupdir";
-    if ( ! -d "${composite}" )
+    if ( ( -e $file ) && ( -r $file ) )
     {
-        print "Could not find directory: '${composite}'.. creating.\n";
-        mkdir($composite, 16877);
+        return 1;
     }
-
-    #
-    # Test for /etc/ssh/globus_backup_<curr>/s2_trans
-    #
-
-    $composite = "$sysconfdir/$transbackupdir";
-    if ( ! -d "${composite}" )
+    else
     {
-        print "Could not find directory: '${composite}'.. creating.\n";
-        mkdir($composite, 16877);
+        return 0;
     }
-
-    return 0;
 }
 
-sub copy_setup_files
+sub determineKeys
 {
-    my $response;
+    my($keyhash, $keylist);
+    my($count);
 
-    print "\nStage 1: Copying configuration files into '${sysconfdir}'..\n";
+    $count = 0;
 
-    $response = "y";
-    if ( -e "${sysconfdir}/ssh_config" )
-    {
-        $response = query_boolean("${sysconfdir}/ssh_config already exists.  Overwrite?", "n");
-        if ($response eq "y")
-        {
-            action("cp ${sysconfdir}/ssh_config ${sysconfdir}/${confbackupdir}/ssh_config");
-        }
-    }
+    $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};
 
-    if ($response eq "y")
+    for my $keytype (keys %$keyfiles)
     {
-        action("cp ${globusdir}/setup/globus/ssh_config ${sysconfdir}/ssh_config");
-    }
+        $basekeyfile = $keyfiles->{$keytype};
+        $keyfile = "$localsshdir/$basekeyfile";
+        $pubkeyfile = "$keyfile.pub";
 
-    #
-    # Reset response for our new query
-    #
-
-    $response = "y";
-    if ( -e "${sysconfdir}/sshd_config" )
-    {
-        $response = query_boolean("${sysconfdir}/sshd_config already exists.  Overwrite?", "n");
-        if ($response eq "y")
+        if ( !isReadable($keyfile) || !isReadable($pubkeyfile) )
         {
-            action("cp ${sysconfdir}/sshd_config ${sysconfdir}/${confbackupdir}/sshd_config");
+            push(@$genlist, $keytype);
+            $count++;
         }
     }
 
-    if ($response eq "y")
+    for my $keytype (keys %$keyfiles)
     {
-        action("cp ${globusdir}/setup/globus/sshd_config ${sysconfdir}/sshd_config");
+        if ( !grep(/^$keytype$/, @$genlist) )
+        {
+            $keyfile = $keyfiles->{$keytype};
+            push(@$copylist, $keyfile);
+            $count++;
+        }
     }
 
-    #
-    # Reset response for our new query
-    #
-
-    $response = "y";
-    if ( -e "${sysconfdir}/moduli" )
+    if ($count > 0)
     {
-        $response = query_boolean("${sysconfdir}/moduli already exists.  Overwrite?", "n");
-        if ($response eq "y")
+        if ( ! -d $sysconfdir )
         {
-            action("cp ${sysconfdir}/moduli ${sysconfdir}/${confbackupdir}/moduli");
+            print "Could not find ${sysconfdir} directory... creating\n";
+            action("mkdir -p $sysconfdir");
         }
     }
 
-    if ($response eq "y")
-    {
-        action("cp ${globusdir}/setup/globus/moduli ${sysconfdir}/moduli");
-    }
+    return $keyhash;
 }
 
-sub runkeygen
+sub runKeyGen
 {
-    print "\nStage 2: Generating ssh host keys..\n";
+    my($gen_keys) = @_;
 
-    if ( ! -d "${sysconfdir}" )
-    {
-        print "Could not find ${sysconfdir} directory... creating\n";
-        mkdir($sysconfdir, 16877);
-        # 16877 should be 755, or drwxr-xr-x
-    }
-
-    if ( -e "${sysconfdir}/ssh_host_key" )
-    {
-        print "${sysconfdir}/ssh_host_key already exists, skipping.\n";
-    }
-    else
-    {
-        # if $sysconfdir/ssh_host_key doesn't exist..
-        action("$bindir/ssh-keygen -t rsa1 -f $sysconfdir/ssh_host_key -N \"\"");
-    }
+    print "Generating ssh host keys...\n";
 
-    if ( -e "${sysconfdir}/ssh_host_dsa_key" )
+    for my $k (@$gen_keys)
     {
-        print "${sysconfdir}/ssh_host_dsa_key already exists, skipping.\n";
-    }
-    else
-    {
-        # if $sysconfdir/ssh_host_dsa_key doesn't exist..
-        action("$bindir/ssh-keygen -t dsa -f $sysconfdir/ssh_host_dsa_key -N \"\"");
-    }
+        $keyfile = $keyfiles->{$k};
 
-    if ( -e "${sysconfdir}/ssh_host_rsa_key" )
-    {
-        print "${sysconfdir}/ssh_host_rsa_key already exists, skipping.\n";
-    }
-    else
-    {
-        # if $sysconfdir/ssh_host_rsa_key doesn't exist..
-        action("$bindir/ssh-keygen -t rsa -f $sysconfdir/ssh_host_rsa_key -N \"\"");
+        # if $sysconfdir/ssh_host_key doesn't exist..
+#        printf("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
+#        printf("\n");
+        action("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
     }
 
     return 0;
@@ -225,154 +189,166 @@ sub fixpaths
 {
     my $g, $h;
 
+    print "Fixing sftp-server path in sshd_config...\n";
+
+    $f = "$gpath/etc/ssh/sshd_config";
+    $g = "$f.tmp";
+
+    if ( ! -f "$f" )
+    {
+        die("Cannot find $f!");
+    }
+
     #
-    # Set up path translations for the installation files
+    # Grab the current mode/uid/gid for use later
     #
 
-    %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}",
-        "(/path/to/scp.real)" => "${bindir}/scp.real",
-        "(/path/to/ssh)" => "${bindir}/ssh",
-        "(/path/to/sftp.real)" => "${bindir}/sftp.real",
-        "(/path/to/sshd.real)" => "${sbindir}/sshd.real",
-        "(/path/to/ssh_config)" => "${sysconfdir}/ssh_config",
-        "(/path/to/sshd_config)" => "${sysconfdir}/sshd_config",
-        );
+    $mode = (stat($f))[2];
+    $uid = (stat($f))[4];
+    $gid = (stat($f))[5];
 
     #
-    # Files on which to perform path translations
+    # Move $f into a .tmp file for the translation step
     #
 
-    %files = (
-        "${bindir}/scp" => 0,
-        "${bindir}/sftp" => 0,
-        "${sbindir}/sshd" => 0,
-        "${sysconfdir}/ssh_config" => 1,
-        "${sysconfdir}/sshd_config" => 1,
-        "${sysconfdir}/moduli" => 1,
-        "${mandir}/${mansubdir}1/scp.1" => 0,
-        "${mandir}/${mansubdir}1/ssh-add.1" => 0,
-        "${mandir}/${mansubdir}1/ssh-agent.1" => 0,
-        "${mandir}/${mansubdir}1/ssh-keygen.1" => 0,
-        "${mandir}/${mansubdir}1/ssh-keyscan.1" => 0,
-        "${mandir}/${mansubdir}1/ssh.1" => 0,
-        "${mandir}/${mansubdir}8/sshd.8" => 0,
-        "${mandir}/${mansubdir}8/sftp-server.8" => 0,
-        "${mandir}/${mansubdir}1/sftp.1" => 0,
-        );
-
-    print "\nStage 3: Translating strings in config and man files...\n";
-
-    for my $f (keys %files)
+    $result = system("mv $f $g 2>&1");
+    if ($result or $?)
     {
-        $f =~ /(.*\/)*(.*)$/;
+        die "ERROR: Unable to execute command: $!\n";
+    }
 
-        #
-        # we really should create a random filename and make sure that it
-        # doesn't already exist (based off current time_t or something)
-        #
+    open(IN, "<$g") || die ("$0: input file $g missing!\n");
+    open(OUT, ">$f") || die ("$0: unable to open output file $f!\n");
 
-        $g = "$f.tmp";
+    while (<IN>)
+    {
+        if ( /Subsystem\s+sftp\s+\S+/ )
+        {
+            $_ = "Subsystem\tsftp\t$gpath/libexec/sftp-server\n";
+            $_ =~ s:/+:/:g;
+        }
+        print OUT "$_";
+    } # while <IN>
 
-        #
-        # get the filename for $f and place it in $h.
-        #
+    close(OUT);
+    close(IN);
 
-        $h = $f;
-        $h =~ s#^.*/##;
+    #
+    # Remove the old .tmp file
+    #
 
-        #
-        # Grab the current mode/uid/gid for use later
-        #
+    $result = system("rm $g 2>&1");
 
-        $mode = (stat($f))[2];
-        $uid = (stat($f))[4];
-        $gid = (stat($f))[5];
+    if ($result or $?)
+    {
+        die "ERROR: Unable to execute command: $!\n";
+    }
 
-        #
-        # Move $f into a .tmp file for the translation step
-        #
+    #
+    # An attempt to revert the new file back to the original file's
+    # mode/uid/gid
+    #
 
-        $result = system("mv $f $g 2>&1");
-        if ($result or $?)
-        {
-            die "ERROR: Unable to execute command: $!\n";
-        }
+    chmod($mode, $f);
+    chown($uid, $gid, $f);
 
-        #
-        # Create a backup of this file if it's flagged
-        #
+    return 0;
+}
 
-        if ($files{$f} == 1)
-        {
-            $result = system("cp $g ${sysconfdir}/${transbackupdir}/$h 2>&1");
+sub alterFileGlobusLocation
+{
+    my ($file) = @_;
 
-            if ($result or $?)
-            {
-                die "ERROR: Unable to execute command: $!\n";
-            }
-        }
+    $data = readFile($file);
+    $data =~ s|\@GSI_OPENSSH_GLOBUS_LOCATION\@|$gpath|g;
+    writeFile($file, $data);
+}
 
-        open(IN, "<$g") || die ("$0: input file $g missing!\n");
-        open(OUT, ">$f") || die ("$0: unable to open output file $f!\n");
+sub alterFiles
+{
+    my (@files);
 
-        while (<IN>)
-        {
-            for $s (keys(%def))
-            {
-                s#$s#$def{$s}#;
-            } # for $s
-            print OUT "$_";
-        } # while <IN>
+    @files = (
+        "$gosharedir/contrib/caldera/sshd.init",
+             );
+}
 
-        close(OUT);
-        close(IN);
+### readFile( $filename )
+#
+# reads and returns $filename's contents
+#
 
-        #
-        # Remove the old .tmp file
-        #
+sub readFile
+{
+    my ($filename) = @_;
+    my $data;
 
-        $result = system("rm $g 2>&1");
+    open (IN, "$filename") || die "Can't open '$filename': $!";
+    $/ = undef;
+    $data = <IN>;
+    $/ = "\n";
+    close(IN);
 
-        if ($result or $?)
-        {
-            die "ERROR: Unable to execute command: $!\n";
-        }
+    return $data;
+}
 
-        #
-        # An attempt to revert the new file back to the original file's
-        # mode/uid/gid
-        #
+### writeFile( $filename, $fileinput )
+#
+# create the inputs to the ssl program at $filename, appending the common name to the
+# stream in the process
+#
 
-        chmod($mode, $f);
-        chown($uid, $gid, $f);
-    } # for $f
+sub writeFile
+{
+    my ($filename, $fileinput) = @_;
 
-    print "complete.\n";
+    #
+    # test for a valid $filename
+    #
 
-    return 0;
+    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 "$myname: Configuring package 'gsi_openssh'..\n";
+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 "$gpath/etc/ssh.  If they aren't present, I will generate them\n";
+print "for you.\n";
 print "\n";
-print "--> NOTE: Run this as root for the intended effect. <--\n";
 
-test_dirs();
-copy_setup_files();
-runkeygen();
+$response = query_boolean("Do you wish to continue with the setup package?","y");
+
+if ($response eq "n")
+{
+    print "\n";
+    print "Okay.. exiting gsi_openssh setup.\n";
+
+    exit 0;
+}
+
+$keyhash = determineKeys();
+runKeyGen($keyhash->{gen});
+copyKeyFiles($keyhash->{copy});
 fixpaths();
 
 my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
@@ -381,7 +357,20 @@ $metadata->finish();
 
 print "\n";
 print "$myname: Finished configuring package 'gsi_openssh'.\n";
-print "------------------------------------------------------\n";
+print "\n";
+print "I see that you have your GLOBUS_LOCATION environmental variable\n";
+print "set to:\n";
+print "  $gpath\n";
+print "\n";
+print "Remember to keep this variable set (correctly) when you want\n";
+print "to use the executables that came with this package.\n";
+print "\n";
+print "Additionally, you may need to set LD_LIBRARY_PATH to point to\n";
+print "the location in which your globus libraries reside.  For example:\n";
+print "\n";
+print "  export LD_LIBRARY_PATH=\"$gpath/lib\"\n";
+print "\n";
+print "---------------------------------------------------------------\n";
 
 #
 # Just need a minimal action() subroutine for now..
@@ -425,11 +414,24 @@ sub query_boolean
     $foo = <STDIN>;
     ($bar) = split //, $foo;
 
-    if ($bar ne $nondefault)
+    if ( grep(/\s/, $bar) )
+    {
+        # this is debatable.  all whitespace means 'default'
+
+        $bar = $default;
+    }
+    elsif ($bar ne $default)
     {
+        # everything else means 'nondefault'.
+
+        $bar = $nondefault;
+    }
+    else
+    {
+        # extraneous step.  to get here, $bar should be eq to $default anyway.
+
         $bar = $default;
     }
 
     return $bar;
 }
-
This page took 0.053943 seconds and 4 git commands to generate.