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 die "GLOBUS_LOCATION needs to be set before running this script"
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($interactive, $force, $verbose);
103 'interactive!' => \$interactive,
105 'verbose' => \$verbose,
109 # miscellaneous initialization functions
112 setPrivilegeSeparation(0);
115 # main execution. This should find its way into a subroutine at some future
119 print "$myname: Configuring package 'gsi_openssh'...\n";
120 print "---------------------------------------------------------------------\n";
121 print "Hi, I'm the setup script for the gsi_openssh package! I will create\n";
122 print "a number of configuration files based on your local system setup. I\n";
123 print "will also attempt to copy or create a number of SSH key pairs for\n";
124 print "this machine. (Loosely, if I find a pair of host keys in /etc/ssh,\n";
125 print "I will copy them into \$GLOBUS_LOCATION/etc/ssh. Otherwise, I will\n";
126 print "generate them for you.)\n";
128 print " Jacobim Mugatu says,\n";
129 print " \"Utopian Prime Minister Bad! GSI-OpenSSH Good!\"\n";
136 print " Using the '-force' flag will cause all gsi_openssh_setup files to\n";
137 print " be removed and replaced by new versions! Backup any critical\n";
138 print " SSH configuration files before you choose to continue!\n";
142 $response = query_boolean("Do you wish to continue with the setup package?","y");
143 if ($response eq "n")
146 print "Exiting gsi_openssh setup.\n";
155 $keyhash = determineKeys();
156 runKeyGen($keyhash->{gen});
157 copyKeyFiles($keyhash->{copy});
160 my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
165 print "Additional Notes:\n";
167 print " o I see that you have your GLOBUS_LOCATION environmental variable\n";
170 print " \"$gpath\"\n";
172 print " Remember to keep this variable set (correctly) when you want to\n";
173 print " use the executables that came with this package.\n";
175 print " After that you may execute, for example:\n";
177 print " \$ . \$GLOBUS_LOCATION/etc/globus-user-env.sh\n";
179 print " to prepare your environment for running the gsi_openssh\n";
180 print " executables.\n";
182 if ( !getPrivilegeSeparation() )
185 print " o For System Administrators:\n";
187 print " If you are going to run the GSI-OpenSSH server, we recommend\n";
188 print " enabling privilege separation. Although this package supports\n";
189 print " this feature, your system appears to require some additional\n";
190 print " configuration.\n";
192 print " From the file README.privsep, included as a part of the OpenSSH\n";
193 print " distribution:\n";
195 print " When privsep is enabled, during the pre-authentication\n";
196 print " phase sshd will chroot(2) to \"/var/empty\" and change its\n";
197 print " privileges to the \"sshd\" user and its primary group. sshd\n";
198 print " is a pseudo-account that should not be used by other\n";
199 print " daemons, and must be locked and should contain a \"nologin\"\n";
200 print " or invalid shell.\n";
202 print " You should do something like the following to prepare the\n";
203 print " privsep preauth environment:\n";
205 print " \# mkdir /var/empty\n";
206 print " \# chown root:sys /var/empty\n";
207 print " \# chmod 755 /var/empty\n";
208 print " \# groupadd sshd\n";
209 print " \# useradd -g sshd -c 'sshd privsep' -d /var/empty \\\n";
210 print " -s /bin/false sshd\n";
212 print " /var/empty should not contain any files.\n";
216 print " o For more information about GSI-Enabled OpenSSH, visit:\n";
217 print " <http://www.ncsa.uiuc.edu/Divisions/ACES/GSI/openssh/>\n";
220 # give the user a chance to read all of this output
224 print "Press <return> to continue... ";
227 print "---------------------------------------------------------------------\n";
228 print "$myname: Finished configuring package 'gsi_openssh'.\n";
238 # initialize the PRNG pathname hash
244 # standard prng to executable conversion names
247 addPRNGCommand("\@PROG_LS\@", "ls");
248 addPRNGCommand("\@PROG_NETSTAT\@", "netstat");
249 addPRNGCommand("\@PROG_ARP\@", "arp");
250 addPRNGCommand("\@PROG_IFCONFIG\@", "ifconfig");
251 addPRNGCommand("\@PROG_PS\@", "ps");
252 addPRNGCommand("\@PROG_JSTAT\@", "jstat");
253 addPRNGCommand("\@PROG_W\@", "w");
254 addPRNGCommand("\@PROG_WHO\@", "who");
255 addPRNGCommand("\@PROG_LAST\@", "last");
256 addPRNGCommand("\@PROG_LASTLOG\@", "lastlog");
257 addPRNGCommand("\@PROG_DF\@", "df");
258 addPRNGCommand("\@PROG_SAR\@", "sar");
259 addPRNGCommand("\@PROG_VMSTAT\@", "vmstat");
260 addPRNGCommand("\@PROG_UPTIME\@", "uptime");
261 addPRNGCommand("\@PROG_IPCS\@", "ipcs");
262 addPRNGCommand("\@PROG_TAIL\@", "tail");
264 print "Determining paths for PRNG commands...\n";
266 $paths = determinePRNGPaths();
271 ### getDirectoryPaths( )
273 # return an array ref containing all of the directories in which we should search
274 # for our listing of executable names.
277 sub getDirectoryPaths( )
280 # read in the PATH environmental variable and prepend a set of 'safe'
281 # directories from which to test PRNG commands.
285 $path = "/bin:/usr/bin:/sbin:/usr/sbin:/etc:" . $path;
286 @dirs = split(/:/, $path);
289 # sanitize each directory listed in the array.
295 $tmp =~ s:^\s+|\s+$::g;
302 ### addPRNGCommand( $prng_name, $exec_name )
304 # given a PRNG name and a corresponding executable name, add it to our list of
305 # PRNG commands for which to find on the system.
310 my($prng_name, $exec_name) = @_;
312 prngAddNode($prng_name, $exec_name);
317 # read in ssh_prng_cmds.in, translate the program listings to the paths we have
318 # found on the local system, and then write the output to ssh_prng_cmds.
323 my($fileInput, $fileOutput);
324 my($mode, $uid, $gid);
327 if ( isPresent("$sysconfdir/ssh_prng_cmds") && !isForced() )
329 printf("ssh_prng_cmds found and not forced. Not installing ssh_prng_cmds...\n");
335 print "Fixing paths in ssh_prng_cmds...\n";
337 $fileInput = "$setupdir/ssh_prng_cmds.in";
338 $fileOutput = "$sysconfdir/ssh_prng_cmds";
341 # verify that we are prepared to work with $fileInput
344 if ( !isReadable($fileInput) )
346 printf("Cannot read $fileInput... skipping.\n");
351 # verify that we are prepared to work with $fileOuput
354 if ( !prepareFileWrite($fileOutput) )
360 # Grab the current mode/uid/gid for use later
363 $mode = (stat($fileInput))[2];
364 $uid = (stat($fileInput))[4];
365 $gid = (stat($fileInput))[5];
368 # Open the files for reading and writing, and loop over the input's contents
371 $data = readFile($fileInput);
372 for my $k (keys %$prngcmds)
374 $sub = prngGetExecPath($k);
375 $data =~ s:$k:$sub:g;
377 writeFile($fileOutput, $data);
380 # An attempt to revert the new file back to the original file's
384 chmod($mode, $fileOutput);
385 chown($uid, $gid, $fileOutput);
390 ### determinePRNGPaths( )
392 # for every entry in the PRNG hash, seek out and find the path for the
393 # corresponding executable name.
396 sub determinePRNGPaths
399 my($exec_name, $exec_path);
401 $dirs = getDirectoryPaths();
403 for my $k (keys %$prngcmds)
405 $exec_name = prngGetExecName($k);
406 $exec_path = findExecutable($exec_name, $dirs);
407 prngSetExecPath($k, $exec_path);
413 ### prngAddNode( $prng_name, $exec_name )
415 # add a new node to the PRNG hash
420 my($prng_name, $exec_name) = @_;
423 if (!defined($prngcmds))
429 $node->{prng} = $prng_name;
430 $node->{exec} = $exec_name;
432 $prngcmds->{$prng_name} = $node;
435 ### prngGetExecName( $key )
437 # get the executable name from the prng commands hash named by $key
444 return $prngcmds->{$key}->{exec};
447 ### prngGetExecPath( $key )
449 # get the executable path from the prng commands hash named by $key
456 return $prngcmds->{$key}->{exec_path};
459 ### prngGetNode( $key )
461 # return a reference to the node named by $key
468 return ${$prngcmds}{$key};
471 ### prngSetExecPath( $key, $path )
473 # given a key, set the executable path in that node to $path
478 my($key, $path) = @_;
480 $prngcmds->{$key}->{exec_path} = $path;
483 ### findExecutable( $exec_name, $dirs )
485 # given an executable name, test each possible path in $dirs to see if such
486 # an executable exists.
491 my($exec_name, $dirs) = @_;
495 $test = "$d/$exec_name";
497 if ( isExecutable($test) )
506 ### copyKeyFiles( $copylist )
508 # given an array of keys to copy, copy both the key and its public variant into
509 # the gsi-openssh configuration directory.
515 my($regex, $basename);
519 print "Copying ssh host keys...\n";
521 for my $f (@$copylist)
528 $pubkeyfile = "$f.pub";
530 copyFile("$localsshdir/$keyfile", "$sysconfdir/$keyfile");
531 copyFile("$localsshdir/$pubkeyfile", "$sysconfdir/$pubkeyfile");
539 # return true if the user passed in the force flag. return false otherwise.
544 if ( defined($force) && $force )
554 ### isReadable( $file )
556 # given a file, return true if that file both exists and is readable by the
557 # effective user id. return false otherwise.
564 if ( ( -e $file ) && ( -r $file ) )
574 ### isExecutable( $file )
576 # return true if $file is executable. return false otherwise.
593 ### isWritable( $file )
595 # given a file, return true if that file does not exist or is writable by the
596 # effective user id. return false otherwise.
603 if ( ( ! -e $file ) || ( -w $file ) )
613 ### isPresent( $file )
615 # given a file, return true if that file exists. return false otherwise.
634 # make the gsi-openssh configuration directory if it doesn't already exist.
639 if ( isPresent($sysconfdir) )
641 if ( -d $sysconfdir )
646 die("${sysconfdir} already exists and is not a directory!\n");
649 print "Could not find ${sysconfdir} directory... creating.\n";
650 action("mkdir -p $sysconfdir");
657 # based on a set of key types, triage them to determine if for each key type, that
658 # key type should be copied from the main ssh configuration directory, or if it
659 # should be generated using ssh-keygen.
664 my($keyhash, $keylist);
668 # initialize our variables
674 $keyhash->{gen} = []; # a list of keytypes to generate
675 $keyhash->{copy} = []; # a list of files to copy from the
677 $genlist = $keyhash->{gen};
678 $copylist = $keyhash->{copy};
681 # loop over our keytypes and determine what we need to do for each of them
684 for my $keytype (keys %$keyfiles)
686 $basekeyfile = $keyfiles->{$keytype};
689 # if the key's are already present, we don't need to bother with this rigamarole
692 $gkeyfile = "$sysconfdir/$basekeyfile";
693 $gpubkeyfile = "$sysconfdir/$basekeyfile.pub";
695 if ( isPresent($gkeyfile) && isPresent($gpubkeyfile) )
699 if ( isWritable("$sysconfdir/$basekeyfile") && isWritable("$sysconfdir/$basekeyfile.pub") )
701 action("rm $sysconfdir/$basekeyfile");
702 action("rm $sysconfdir/$basekeyfile.pub");
712 # if we can find a copy of the keys in /etc/ssh, we'll copy them to the user's
716 $mainkeyfile = "$localsshdir/$basekeyfile";
717 $mainpubkeyfile = "$localsshdir/$basekeyfile.pub";
719 if ( isReadable($mainkeyfile) && isReadable($mainpubkeyfile) )
721 push(@$copylist, $basekeyfile);
727 # otherwise, we need to generate the key
730 push(@$genlist, $keytype);
737 ### runKeyGen( $gen_keys )
739 # given a set of key types, generate private and public keys for that key type and
740 # place them in the gsi-openssh configuration directory.
746 my $keygen = "$bindir/ssh-keygen";
748 if (@$gen_keys && -x $keygen)
750 print "Generating ssh host keys...\n";
752 for my $k (@$gen_keys)
754 $keyfile = $keyfiles->{$k};
756 if ( !isPresent("$sysconfdir/$keyfile") )
758 action("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
766 ### copySSHDConfigFile( )
768 # this subroutine 'edits' the paths in sshd_config to suit them to the current environment
769 # in which the setup script is being run.
772 sub copySSHDConfigFile
774 my($fileInput, $fileOutput);
775 my($mode, $uid, $gid);
777 my($privsep_enabled);
779 print "Fixing paths in sshd_config...\n";
781 $fileInput = "$setupdir/sshd_config.in";
782 $fileOutput = "$sysconfdir/sshd_config";
785 # verify that we are prepared to work with $fileInput
788 if ( !isReadable($fileInput) )
790 printf("Cannot read $fileInput... skipping.\n");
795 # verify that we are prepared to work with $fileOuput
798 if ( !prepareFileWrite($fileOutput) )
804 # check to see whether we should enable privilege separation
807 if ( userExists("sshd") && ( -d "/var/empty" ) && ( getOwnerID("/var/empty") eq 0 ) )
809 setPrivilegeSeparation(1);
813 setPrivilegeSeparation(0);
816 if ( getPrivilegeSeparation() )
818 $privsep_enabled = "yes";
822 $privsep_enabled = "no";
826 # Grab the current mode/uid/gid for use later
829 $mode = (stat($fileInput))[2];
830 $uid = (stat($fileInput))[4];
831 $gid = (stat($fileInput))[5];
834 # Open the files for reading and writing, and loop over the input's contents
837 $data = readFile($fileInput);
840 # alter the PidFile config
843 $text = "PidFile\t$gpath/var/sshd.pid";
844 $data =~ s:^[\s|#]*PidFile.*$:$text:gm;
847 # set the sftp directive
850 $text = "Subsystem\tsftp\t$gpath/libxec/sftp-server";
851 $data =~ s:^[\s|#]*Subsystem\s+sftp\s+.*$:$text:gm;
854 # set the privilege separation directive
857 $text = "UsePrivilegeSeparation\t${privsep_enabled}";
858 $data =~ s:^[\s|#]*UsePrivilegeSeparation.*$:$text:gm;
861 # dump the modified output to the config file
864 writeFile($fileOutput, $data);
867 # An attempt to revert the new file back to the original file's
871 chmod($mode, $fileOutput);
872 chown($uid, $gid, $fileOutput);
877 ### setPrivilegeSeparation( $value )
879 # set the privilege separation variable to $value
882 sub setPrivilegeSeparation
889 ### getPrivilegeSeparation( )
891 # return the value of the privilege separation variable
894 sub getPrivilegeSeparation
899 ### prepareFileWrite( $file )
901 # test $file to prepare for writing to it.
908 if ( isPresent($file) )
910 printf("$file already exists... ");
914 if ( isWritable($file) )
916 printf("removing.\n");
922 printf("not writable -- skipping.\n");
928 printf("skipping.\n");
936 ### copyConfigFiles( )
938 # subroutine that copies some extra config files to their proper location in
939 # $GLOBUS_LOCATION/etc/ssh.
945 # copy the sshd_config file into the ssh configuration directory and alter
946 # the paths in the file.
949 copySSHDConfigFile();
952 # do straight copies of the ssh_config and moduli files.
955 printf("Copying ssh_config and moduli to their proper location...\n");
957 copyFile("$setupdir/ssh_config", "$sysconfdir/ssh_config");
958 copyFile("$setupdir/moduli", "$sysconfdir/moduli");
961 # copy and alter the SXXsshd script.
964 copySXXScript("$setupdir/SXXsshd.in", "$sbindir/SXXsshd");
967 ### copyFile( $src, $dest )
969 # copy the file pointed to by $src to the location specified by $dest. in the
970 # process observe the rules regarding when the '-force' flag was passed to us.
975 my($src, $dest) = @_;
977 if ( !isReadable($src) )
979 printf("$src is not readable... not creating $dest.\n");
983 if ( !prepareFileWrite($dest) )
988 action("cp $src $dest");
991 ### copySXXScript( $in, $out )
993 # parse the input file, substituting in place the value of GLOBUS_LOCATION, and
994 # write the result to the output file.
1002 if ( !isReadable($in) )
1004 printf("$in is not readable... not creating $out.\n");
1008 if ( !prepareFileWrite($out) )
1014 # clean up any junk in the globus path variable
1018 $tmpgpath =~ s:/+:/:g;
1019 $tmpgpath =~ s:([^/]+)/$:\1:g;
1022 # read in the script, substitute globus location, then write it back out
1025 $data = readFile($in);
1026 $data =~ s|\@GLOBUS_LOCATION\@|$tmpgpath|g;
1027 writeFile($out, $data);
1028 action("chmod 755 $out");
1031 ### readFile( $filename )
1033 # reads and returns $filename's contents
1041 open(IN, "$filename") || die "Can't open '$filename': $!";
1050 ### writeFile( $filename, $fileinput )
1052 # create the inputs to the ssl program at $filename, appending the common name to the
1053 # stream in the process
1058 my($filename, $fileinput) = @_;
1061 # test for a valid $filename
1064 if ( !defined($filename) || (length($filename) lt 1) )
1066 die "Filename is undefined";
1070 # verify that we are prepared to work with $filename
1073 if ( !prepareFileWrite($filename) )
1079 # write the output to $filename
1082 open(OUT, ">$filename");
1083 print OUT "$fileinput";
1087 ### action( $command )
1089 # run $command within a proper system() command.
1096 printf "$command\n";
1098 my $result = system("LD_LIBRARY_PATH=\"$gpath/lib:\$LD_LIBRARY_PATH\"; $command 2>&1");
1100 if (($result or $?) and $command !~ m!patch!)
1102 die "ERROR: Unable to execute command: $!\n";
1106 ### query_boolean( $query_text, $default )
1108 # query the user with a string, and expect a response. If the user hits
1109 # 'enter' instead of entering an input, then accept the default response.
1114 my($query_text, $default) = @_;
1115 my($nondefault, $foo, $bar);
1118 # Set $nondefault to the boolean opposite of $default.
1121 if ($default eq "n")
1130 print "${query_text} ";
1131 print "[$default] ";
1134 ($bar) = split //, $foo;
1136 if ( grep(/\s/, $bar) )
1138 # this is debatable. all whitespace means 'default'
1142 elsif ($bar ne $default)
1144 # everything else means 'nondefault'.
1150 # extraneous step. to get here, $bar should be eq to $default anyway.
1158 ### absolutePath( $file )
1160 # converts a given pathname into a canonical path using the abs_path function.
1166 my $home = $ENV{'HOME'};
1167 $file =~ s!~!$home!;
1169 $file =~ s!^\./!$startd/!;
1170 $file = "$startd/$file" if $file !~ m!^\s*/!;
1171 $file = abs_path($file);
1175 ### getOwnerID( $file )
1177 # return the uid containing the owner ID of the given file.
1186 # call stat() to get the mode of the file
1189 $uid = (stat($file))[4];
1194 ### getMode( $file )
1196 # return a string containing the mode of the given file.
1202 my($tempmode, $mode);
1205 # call stat() to get the mode of the file
1208 $tempmode = (stat($file))[2];
1209 if (length($tempmode) < 1)
1215 # call sprintf to format the mode into a UNIX-like string
1218 $mode = sprintf("%04o", $tempmode & 07777);
1223 ### userExists( $username )
1225 # given a username, return true if the user exists on the system. return false
1235 # retrieve the userid of the user with the given username
1238 $uid = getpwnam($username);
1241 # return true if $uid is defined and has a length greater than 0
1244 if ( defined($uid) and (length($uid) > 0) )