5 # Adapts the installed gsi-openssh environment to the current machine,
6 # performing actions that originally occurred during the package's
7 # 'make install' phase.
9 # Send comments/fixes/suggestions to:
10 # Chase Phillips <cphillip@ncsa.uiuc.edu>
14 # Get user's GPT_LOCATION since we may be installing this using a new(er)
18 $gptpath = $ENV{GPT_LOCATION};
21 # And the old standby..
24 $gpath = $ENV{GLOBUS_LOCATION};
27 exitDie("ERROR: GLOBUS_LOCATION needs to be set before running this script!\n");
31 # Include standard modules
39 # modify the ld library path for when we call ssh executables
42 $oldldpath = $ENV{LD_LIBRARY_PATH};
43 $newldpath = "$gpath/lib";
44 if (length($oldldpath) > 0)
46 $newldpath .= ":$oldldpath";
48 $ENV{LD_LIBRARY_PATH} = "$newldpath";
51 # i'm including this because other perl scripts in the gpt setup directories
55 if (defined($gptpath))
57 @INC = (@INC, "$gptpath/lib/perl", "$gpath/lib/perl");
61 @INC = (@INC, "$gpath/lib/perl");
64 require Grid::GPT::Setup;
67 # script-centred variable initialization
70 my $globusdir = $gpath;
71 my $myname = "setup-openssh.pl";
74 # Set up path prefixes for use in the path translations
77 $prefix = ${globusdir};
78 $exec_prefix = "${prefix}";
79 $bindir = "${exec_prefix}/bin";
80 $sbindir = "${exec_prefix}/sbin";
81 $sysconfdir = "$prefix/etc/ssh";
82 $localsshdir = "/etc/ssh";
83 $setupdir = "$prefix/setup/gsi_openssh_setup";
86 # standard key types and their root file name mappings
90 "dsa" => "ssh_host_dsa_key",
91 "rsa" => "ssh_host_rsa_key",
92 "rsa1" => "ssh_host_key",
96 # argument specification. we offload some processing work from later functions
97 # to verify correct args by using anon subs in various places.
100 my($prompt, $force, $verbose);
106 'prompt!' => \$prompt,
108 'verbose' => \$verbose,
112 # miscellaneous initialization functions
115 setPrivilegeSeparation(0);
118 # main execution. This should find its way into a subroutine at some future
122 debug0("$myname: Configuring package 'gsi_openssh'...\n");
123 debug0("---------------------------------------------------------------------\n");
127 $keyhash = determineKeys();
128 runKeyGen($keyhash->{gen});
129 copyKeyFiles($keyhash->{copy});
132 my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
137 debug0("Notes:\n\n");
139 if ( getPrivilegeSeparation() )
141 debug0(" o Privilege separation is on.\n");
143 elsif ( !getPrivilegeSeparation() )
145 debug0(" o Privilege separation is off.\n");
148 debug0(" o GSI-OpenSSH website is <http://grid.ncsa.uiuc.edu/ssh/>.\n");
149 debug0("---------------------------------------------------------------------\n");
150 debug0("$myname: Finished configuring package 'gsi_openssh'.\n");
160 # initialize the PRNG pathname hash
166 # standard prng to executable conversion names
169 addPRNGCommand("\@PROG_LS\@", "ls");
170 addPRNGCommand("\@PROG_NETSTAT\@", "netstat");
171 addPRNGCommand("\@PROG_ARP\@", "arp");
172 addPRNGCommand("\@PROG_IFCONFIG\@", "ifconfig");
173 addPRNGCommand("\@PROG_PS\@", "ps");
174 addPRNGCommand("\@PROG_JSTAT\@", "jstat");
175 addPRNGCommand("\@PROG_W\@", "w");
176 addPRNGCommand("\@PROG_WHO\@", "who");
177 addPRNGCommand("\@PROG_LAST\@", "last");
178 addPRNGCommand("\@PROG_LASTLOG\@", "lastlog");
179 addPRNGCommand("\@PROG_DF\@", "df");
180 addPRNGCommand("\@PROG_SAR\@", "sar");
181 addPRNGCommand("\@PROG_VMSTAT\@", "vmstat");
182 addPRNGCommand("\@PROG_UPTIME\@", "uptime");
183 addPRNGCommand("\@PROG_IPCS\@", "ipcs");
184 addPRNGCommand("\@PROG_TAIL\@", "tail");
186 debug1("Determining paths for PRNG commands...\n");
188 $paths = determinePRNGPaths();
193 ### getDirectoryPaths( )
195 # return an array ref containing all of the directories in which we should search
196 # for our listing of executable names.
199 sub getDirectoryPaths( )
202 # read in the PATH environmental variable and prepend a set of 'safe'
203 # directories from which to test PRNG commands.
207 $path = "/bin:/usr/bin:/sbin:/usr/sbin:/etc:" . $path;
208 @dirs = split(/:/, $path);
211 # sanitize each directory listed in the array.
217 $tmp =~ s:^\s+|\s+$::g;
224 ### addPRNGCommand( $prng_name, $exec_name )
226 # given a PRNG name and a corresponding executable name, add it to our list of
227 # PRNG commands for which to find on the system.
232 my($prng_name, $exec_name) = @_;
234 prngAddNode($prng_name, $exec_name);
239 # read in ssh_prng_cmds.in, translate the program listings to the paths we have
240 # found on the local system, and then write the output to ssh_prng_cmds.
245 my($fileInput, $fileOutput);
246 my($mode, $uid, $gid);
249 if ( isPresent("$sysconfdir/ssh_prng_cmds") && !isForced() )
251 debug1("ssh_prng_cmds found and not forced. Not installing ssh_prng_cmds...\n");
257 debug1("Fixing paths in ssh_prng_cmds...\n");
259 $fileInput = "$setupdir/ssh_prng_cmds.in";
260 $fileOutput = "$sysconfdir/ssh_prng_cmds";
263 # verify that we are prepared to work with $fileInput
266 if ( !isReadable($fileInput) )
268 debug1("Cannot read $fileInput... skipping.\n");
273 # verify that we are prepared to work with $fileOuput
276 if ( !prepareFileWrite($fileOutput) )
282 # Grab the current mode/uid/gid for use later
285 $mode = (stat($fileInput))[2];
286 $uid = (stat($fileInput))[4];
287 $gid = (stat($fileInput))[5];
290 # Open the files for reading and writing, and loop over the input's contents
293 $data = readFile($fileInput);
294 for my $k (keys %$prngcmds)
296 $sub = prngGetExecPath($k);
297 $data =~ s:$k:$sub:g;
299 writeFile($fileOutput, $data);
302 # An attempt to revert the new file back to the original file's
306 chmod($mode, $fileOutput);
307 chown($uid, $gid, $fileOutput);
312 ### determinePRNGPaths( )
314 # for every entry in the PRNG hash, seek out and find the path for the
315 # corresponding executable name.
318 sub determinePRNGPaths
321 my($exec_name, $exec_path);
323 $dirs = getDirectoryPaths();
325 for my $k (keys %$prngcmds)
327 $exec_name = prngGetExecName($k);
328 $exec_path = findExecutable($exec_name, $dirs);
329 prngSetExecPath($k, $exec_path);
335 ### prngAddNode( $prng_name, $exec_name )
337 # add a new node to the PRNG hash
342 my($prng_name, $exec_name) = @_;
345 if (!defined($prngcmds))
351 $node->{prng} = $prng_name;
352 $node->{exec} = $exec_name;
354 $prngcmds->{$prng_name} = $node;
357 ### prngGetExecName( $key )
359 # get the executable name from the prng commands hash named by $key
366 return $prngcmds->{$key}->{exec};
369 ### prngGetExecPath( $key )
371 # get the executable path from the prng commands hash named by $key
378 return $prngcmds->{$key}->{exec_path};
381 ### prngGetNode( $key )
383 # return a reference to the node named by $key
390 return ${$prngcmds}{$key};
393 ### prngSetExecPath( $key, $path )
395 # given a key, set the executable path in that node to $path
400 my($key, $path) = @_;
402 $prngcmds->{$key}->{exec_path} = $path;
405 ### findExecutable( $exec_name, $dirs )
407 # given an executable name, test each possible path in $dirs to see if such
408 # an executable exists.
413 my($exec_name, $dirs) = @_;
417 $test = "$d/$exec_name";
419 if ( isExecutable($test) )
428 ### copyKeyFiles( $copylist )
430 # given an array of keys to copy, copy both the key and its public variant into
431 # the gsi-openssh configuration directory.
437 my($regex, $basename);
441 debug1("Copying ssh host keys...\n");
443 for my $f (@$copylist)
450 $pubkeyfile = "$f.pub";
452 copyFile("$localsshdir/$keyfile", "$sysconfdir/$keyfile");
453 copyFile("$localsshdir/$pubkeyfile", "$sysconfdir/$pubkeyfile");
461 # return true if the user passed in the force flag. return false otherwise.
466 if ( defined($force) && $force )
476 ### isReadable( $file )
478 # given a file, return true if that file both exists and is readable by the
479 # effective user id. return false otherwise.
486 if ( ( -e $file ) && ( -r $file ) )
496 ### isExecutable( $file )
498 # return true if $file is executable. return false otherwise.
515 ### isWritable( $file )
517 # given a file, return true if that file does not exist or is writable by the
518 # effective user id. return false otherwise.
525 if ( ( ! -e $file ) || ( -w $file ) )
535 ### isPresent( $file )
537 # given a file, return true if that file exists. return false otherwise.
556 # make the gsi-openssh configuration directory if it doesn't already exist.
561 if ( isPresent($sysconfdir) )
563 if ( -d $sysconfdir )
568 debug1("${sysconfdir} already exists and is not a directory!\n");
572 debug1("Could not find ${sysconfdir} directory... creating.\n");
573 action("mkdir -p $sysconfdir");
580 # based on a set of key types, triage them to determine if for each key type, that
581 # key type should be copied from the main ssh configuration directory, or if it
582 # should be generated using ssh-keygen.
587 my($keyhash, $keylist);
591 # initialize our variables
597 $keyhash->{gen} = []; # a list of keytypes to generate
598 $keyhash->{copy} = []; # a list of files to copy from the
600 $genlist = $keyhash->{gen};
601 $copylist = $keyhash->{copy};
604 # loop over our keytypes and determine what we need to do for each of them
607 for my $keytype (keys %$keyfiles)
609 $basekeyfile = $keyfiles->{$keytype};
612 # if the key's are already present, we don't need to bother with this rigamarole
615 $gkeyfile = "$sysconfdir/$basekeyfile";
616 $gpubkeyfile = "$sysconfdir/$basekeyfile.pub";
618 if ( isPresent($gkeyfile) && isPresent($gpubkeyfile) )
622 if ( isWritable("$sysconfdir/$basekeyfile") && isWritable("$sysconfdir/$basekeyfile.pub") )
624 action("rm $sysconfdir/$basekeyfile");
625 action("rm $sysconfdir/$basekeyfile.pub");
635 # if we can find a copy of the keys in /etc/ssh, we'll copy them to the user's
639 $mainkeyfile = "$localsshdir/$basekeyfile";
640 $mainpubkeyfile = "$localsshdir/$basekeyfile.pub";
642 if ( isReadable($mainkeyfile) && isReadable($mainpubkeyfile) )
644 push(@$copylist, $basekeyfile);
650 # otherwise, we need to generate the key
653 push(@$genlist, $keytype);
660 ### runKeyGen( $gen_keys )
662 # given a set of key types, generate private and public keys for that key type and
663 # place them in the gsi-openssh configuration directory.
669 my $keygen = "$bindir/ssh-keygen";
671 if (@$gen_keys && -x $keygen)
673 debug1("Generating ssh host keys...\n");
675 for my $k (@$gen_keys)
677 $keyfile = $keyfiles->{$k};
679 if ( !isPresent("$sysconfdir/$keyfile") )
681 action("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
689 ### copySSHDConfigFile( )
691 # this subroutine 'edits' the paths in sshd_config to suit them to the current environment
692 # in which the setup script is being run.
695 sub copySSHDConfigFile
697 my($fileInput, $fileOutput);
698 my($mode, $uid, $gid);
700 my($privsep_enabled);
702 debug1("Fixing paths in sshd_config...\n");
704 $fileInput = "$setupdir/sshd_config.in";
705 $fileOutput = "$sysconfdir/sshd_config";
708 # verify that we are prepared to work with $fileInput
711 if ( !isReadable($fileInput) )
713 debug1("Cannot read $fileInput... skipping.\n");
718 # verify that we are prepared to work with $fileOuput
721 if ( !prepareFileWrite($fileOutput) )
727 # check to see whether we should enable privilege separation
730 if ( userExists("sshd") && ( -d "/var/empty" ) && ( getOwnerID("/var/empty") eq 0 ) )
732 setPrivilegeSeparation(1);
736 setPrivilegeSeparation(0);
739 if ( getPrivilegeSeparation() )
741 $privsep_enabled = "yes";
745 $privsep_enabled = "no";
749 # Grab the current mode/uid/gid for use later
752 $mode = (stat($fileInput))[2];
753 $uid = (stat($fileInput))[4];
754 $gid = (stat($fileInput))[5];
757 # Open the files for reading and writing, and loop over the input's contents
760 $data = readFile($fileInput);
763 # # alter the PidFile config
766 # $text = "PidFile\t$gpath/var/sshd.pid";
767 # $data =~ s:^[\s|#]*PidFile.*$:$text:gm;
770 # set the sftp directive
773 $text = "Subsystem\tsftp\t$gpath/libexec/sftp-server";
774 $data =~ s:^[\s|#]*Subsystem\s+sftp\s+.*$:$text:gm;
777 # set the privilege separation directive
780 $text = "UsePrivilegeSeparation\t${privsep_enabled}";
781 $data =~ s:^[\s|#]*UsePrivilegeSeparation.*$:$text:gm;
784 # dump the modified output to the config file
787 writeFile($fileOutput, $data);
790 # An attempt to revert the new file back to the original file's
794 chmod($mode, $fileOutput);
795 chown($uid, $gid, $fileOutput);
800 ### setPrivilegeSeparation( $value )
802 # set the privilege separation variable to $value
805 sub setPrivilegeSeparation
812 ### getPrivilegeSeparation( )
814 # return the value of the privilege separation variable
817 sub getPrivilegeSeparation
822 ### prepareFileWrite( $file )
824 # test $file to prepare for writing to it.
831 if ( isPresent($file) )
833 debug1("$file already exists... ");
837 if ( isWritable($file) )
839 debug1("removing.\n");
845 debug1("not writable -- skipping.\n");
851 debug1("skipping.\n");
859 ### copyConfigFiles( )
861 # subroutine that copies some extra config files to their proper location in
862 # $GLOBUS_LOCATION/etc/ssh.
868 # copy the sshd_config file into the ssh configuration directory and alter
869 # the paths in the file.
872 copySSHDConfigFile();
875 # do straight copies of the ssh_config and moduli files.
878 debug1("Copying ssh_config and moduli to their proper location...\n");
880 copyFile("$setupdir/ssh_config", "$sysconfdir/ssh_config");
881 copyFile("$setupdir/moduli", "$sysconfdir/moduli");
884 # copy and alter the SXXsshd script.
887 copySXXScript("$setupdir/SXXsshd.in", "$sbindir/SXXsshd");
890 ### copyFile( $src, $dest )
892 # copy the file pointed to by $src to the location specified by $dest. in the
893 # process observe the rules regarding when the '-force' flag was passed to us.
898 my($src, $dest) = @_;
900 if ( !isReadable($src) )
902 debug1("$src is not readable... not creating $dest.\n");
906 if ( !prepareFileWrite($dest) )
911 action("cp $src $dest");
914 ### copySXXScript( $in, $out )
916 # parse the input file, substituting in place the value of GLOBUS_LOCATION, and
917 # write the result to the output file.
925 if ( !isReadable($in) )
927 debug1("$in is not readable... not creating $out.\n");
931 if ( !prepareFileWrite($out) )
937 # clean up any junk in the globus path variable
941 $tmpgpath =~ s:/+:/:g;
942 $tmpgpath =~ s:([^/]+)/$:\1:g;
945 # read in the script, substitute globus location, then write it back out
948 $data = readFile($in);
949 $data =~ s|\@GLOBUS_LOCATION\@|$tmpgpath|g;
950 writeFile($out, $data);
951 action("chmod 755 $out");
954 ### readFile( $filename )
956 # reads and returns $filename's contents
964 open(IN, "$filename") || exitDie("ERROR: Can't open '$filename': $!\n");
973 ### writeFile( $filename, $fileinput )
975 # create the inputs to the ssl program at $filename, appending the common name to the
976 # stream in the process
981 my($filename, $fileinput) = @_;
984 # test for a valid $filename
987 if ( !defined($filename) || (length($filename) lt 1) )
989 exitDie("ERROR: Filename is undefined!\n");
993 # verify that we are prepared to work with $filename
996 if ( !prepareFileWrite($filename) )
1002 # write the output to $filename
1005 open(OUT, ">$filename");
1006 print OUT "$fileinput";
1010 ### debug1( $arg1, $arg2 )
1012 # Print out a debugging message at level 1.
1017 debug(string => \@_, level => 1);
1020 ### debug0( $arg1, $arg2 )
1022 # Print out a debugging message at level 0.
1027 debug(string => \@_, level => 0);
1030 ### debug( string => $string, level => $level )
1032 # Print out debugging messages at various levels. Feel free to use debugN() directly
1033 # which in turn calls this subroutine.
1040 if (!defined($args{'level'}))
1045 if ($verbose >= $args{'level'})
1047 printf(@{$args{'string'}});
1051 ### action( $command )
1053 # run $command within a proper system() command.
1060 debug1("$command\n");
1062 my $result = system("LD_LIBRARY_PATH=\"$gpath/lib:\$LD_LIBRARY_PATH\"; $command >/dev/null 2>&1");
1064 if (($result or $?) and $command !~ m!patch!)
1066 exitDie("ERROR: Unable to execute command: $!\n");
1070 ### exitDie( $error )
1072 # a horribly named method meant to look like die but only exit, thereby not causing
1073 # gpt-postinstall to croak.
1084 ### query_boolean( $query_text, $default )
1086 # query the user with a string, and expect a response. If the user hits
1087 # 'enter' instead of entering an input, then accept the default response.
1092 my($query_text, $default) = @_;
1093 my($nondefault, $foo, $bar);
1097 print "Prompt suppressed. Continuing...\n";
1102 # Set $nondefault to the boolean opposite of $default.
1105 if ($default eq "n")
1114 print "${query_text} ";
1115 print "[$default] ";
1118 ($bar) = split //, $foo;
1120 if ( grep(/\s/, $bar) )
1122 # this is debatable. all whitespace means 'default'
1130 elsif ($bar ne $default)
1132 # everything else means 'nondefault'.
1138 # extraneous step. to get here, $bar should be eq to $default anyway.
1146 ### absolutePath( $file )
1148 # converts a given pathname into a canonical path using the abs_path function.
1154 my $home = $ENV{'HOME'};
1155 $file =~ s!~!$home!;
1157 $file =~ s!^\./!$startd/!;
1158 $file = "$startd/$file" if $file !~ m!^\s*/!;
1159 $file = abs_path($file);
1163 ### getOwnerID( $file )
1165 # return the uid containing the owner ID of the given file.
1174 # call stat() to get the mode of the file
1177 $uid = (stat($file))[4];
1182 ### getMode( $file )
1184 # return a string containing the mode of the given file.
1190 my($tempmode, $mode);
1193 # call stat() to get the mode of the file
1196 $tempmode = (stat($file))[2];
1197 if (length($tempmode) < 1)
1203 # call sprintf to format the mode into a UNIX-like string
1206 $mode = sprintf("%04o", $tempmode & 07777);
1211 ### userExists( $username )
1213 # given a username, return true if the user exists on the system. return false
1223 # retrieve the userid of the user with the given username
1226 $uid = getpwnam($username);
1229 # return true if $uid is defined and has a length greater than 0
1232 if ( defined($uid) and (length($uid) > 0) )