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 # i'm including this because other perl scripts in the gpt setup directories
43 if (defined($gptpath))
45 @INC = (@INC, "$gptpath/lib/perl", "$gpath/lib/perl");
49 @INC = (@INC, "$gpath/lib/perl");
52 require Grid::GPT::Setup;
55 # script-centred variable initialization
58 my $globusdir = $gpath;
59 my $myname = "setup-openssh.pl";
62 # Set up path prefixes for use in the path translations
65 $prefix = ${globusdir};
66 $exec_prefix = "${prefix}";
67 $bindir = "${exec_prefix}/bin/ssh.d";
68 $sbindir = "${exec_prefix}/sbin";
69 $sysconfdir = "$prefix/etc/ssh";
70 $localsshdir = "/etc/ssh";
71 $setupdir = "$prefix/setup/gsi_openssh_setup";
74 # standard key types and their root file name mappings
78 "dsa" => "ssh_host_dsa_key",
79 "rsa" => "ssh_host_rsa_key",
80 "rsa1" => "ssh_host_key",
84 # argument specification. we offload some processing work from later functions
85 # to verify correct args by using anon subs in various places.
88 my($prompt, $force, $verbose);
94 'prompt!' => \$prompt,
96 'verbose' => \$verbose,
100 # miscellaneous initialization functions
103 setPrivilegeSeparation(0);
106 # main execution. This should find its way into a subroutine at some future
110 debug0("Configuring gsi_openssh\n");
111 debug0("------------------------------------------------------------\n");
112 debug0("Executing...\n");
116 $keyhash = determineKeys();
117 runKeyGen($keyhash->{gen});
118 linkKeyFiles($keyhash->{link});
121 my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
126 debug0("Notes:\n\n");
128 if ( getPrivilegeSeparation() )
130 debug0(" o Privilege separation is on.\n");
132 elsif ( !getPrivilegeSeparation() )
134 debug0(" o Privilege separation is off.\n");
137 debug0(" o GSI-OpenSSH website is <http://grid.ncsa.uiuc.edu/ssh/>.\n");
138 debug0("------------------------------------------------------------\n");
139 debug0("Finished configuring gsi_openssh.\n");
149 # initialize the PRNG pathname hash
155 # standard prng to executable conversion names
158 addPRNGCommand("\@PROG_LS\@", "ls");
159 addPRNGCommand("\@PROG_NETSTAT\@", "netstat");
160 addPRNGCommand("\@PROG_ARP\@", "arp");
161 addPRNGCommand("\@PROG_IFCONFIG\@", "ifconfig");
162 addPRNGCommand("\@PROG_PS\@", "ps");
163 addPRNGCommand("\@PROG_JSTAT\@", "jstat");
164 addPRNGCommand("\@PROG_W\@", "w");
165 addPRNGCommand("\@PROG_WHO\@", "who");
166 addPRNGCommand("\@PROG_LAST\@", "last");
167 addPRNGCommand("\@PROG_LASTLOG\@", "lastlog");
168 addPRNGCommand("\@PROG_DF\@", "df");
169 addPRNGCommand("\@PROG_SAR\@", "sar");
170 addPRNGCommand("\@PROG_VMSTAT\@", "vmstat");
171 addPRNGCommand("\@PROG_UPTIME\@", "uptime");
172 addPRNGCommand("\@PROG_IPCS\@", "ipcs");
173 addPRNGCommand("\@PROG_TAIL\@", "tail");
175 debug1("Determining paths for PRNG commands...\n");
177 $paths = determinePRNGPaths();
182 ### getDirectoryPaths( )
184 # return an array ref containing all of the directories in which we should search
185 # for our listing of executable names.
188 sub getDirectoryPaths( )
191 # read in the PATH environmental variable and prepend a set of 'safe'
192 # directories from which to test PRNG commands.
196 $path = "/bin:/usr/bin:/sbin:/usr/sbin:/etc:" . $path;
197 @dirs = split(/:/, $path);
200 # sanitize each directory listed in the array.
206 $tmp =~ s:^\s+|\s+$::g;
213 ### addPRNGCommand( $prng_name, $exec_name )
215 # given a PRNG name and a corresponding executable name, add it to our list of
216 # PRNG commands for which to find on the system.
221 my($prng_name, $exec_name) = @_;
223 prngAddNode($prng_name, $exec_name);
228 # read in ssh_prng_cmds.in, translate the program listings to the paths we have
229 # found on the local system, and then write the output to ssh_prng_cmds.
234 my($fileInput, $fileOutput);
235 my($mode, $uid, $gid);
238 if ( isPresent("$sysconfdir/ssh_prng_cmds") && !isForced() )
240 debug1("ssh_prng_cmds found and not forced. Not installing ssh_prng_cmds...\n");
246 debug1("Fixing paths in ssh_prng_cmds...\n");
248 $fileInput = "$setupdir/ssh_prng_cmds.in";
249 $fileOutput = "$sysconfdir/ssh_prng_cmds";
252 # verify that we are prepared to work with $fileInput
255 if ( !isReadable($fileInput) )
257 debug1("Cannot read $fileInput... skipping.\n");
262 # verify that we are prepared to work with $fileOuput
265 if ( !prepareFileWrite($fileOutput) )
271 # Grab the current mode/uid/gid for use later
274 $mode = (stat($fileInput))[2];
275 $uid = (stat($fileInput))[4];
276 $gid = (stat($fileInput))[5];
279 # Open the files for reading and writing, and loop over the input's contents
282 $data = readFile($fileInput);
283 for my $k (keys %$prngcmds)
285 $sub = prngGetExecPath($k);
286 $data =~ s:$k:$sub:g;
288 writeFile($fileOutput, $data);
291 # An attempt to revert the new file back to the original file's
295 chmod($mode, $fileOutput);
296 chown($uid, $gid, $fileOutput);
301 ### determinePRNGPaths( )
303 # for every entry in the PRNG hash, seek out and find the path for the
304 # corresponding executable name.
307 sub determinePRNGPaths
310 my($exec_name, $exec_path);
312 $dirs = getDirectoryPaths();
314 for my $k (keys %$prngcmds)
316 $exec_name = prngGetExecName($k);
317 $exec_path = findExecutable($exec_name, $dirs);
318 prngSetExecPath($k, $exec_path);
324 ### prngAddNode( $prng_name, $exec_name )
326 # add a new node to the PRNG hash
331 my($prng_name, $exec_name) = @_;
334 if (!defined($prngcmds))
340 $node->{prng} = $prng_name;
341 $node->{exec} = $exec_name;
343 $prngcmds->{$prng_name} = $node;
346 ### prngGetExecName( $key )
348 # get the executable name from the prng commands hash named by $key
355 return $prngcmds->{$key}->{exec};
358 ### prngGetExecPath( $key )
360 # get the executable path from the prng commands hash named by $key
367 return $prngcmds->{$key}->{exec_path};
370 ### prngGetNode( $key )
372 # return a reference to the node named by $key
379 return ${$prngcmds}{$key};
382 ### prngSetExecPath( $key, $path )
384 # given a key, set the executable path in that node to $path
389 my($key, $path) = @_;
391 $prngcmds->{$key}->{exec_path} = $path;
394 ### findExecutable( $exec_name, $dirs )
396 # given an executable name, test each possible path in $dirs to see if such
397 # an executable exists.
402 my($exec_name, $dirs) = @_;
406 $test = "$d/$exec_name";
408 if ( isExecutable($test) )
417 ### linkKeyFiles( $linklist )
419 # given an array of keys to link, link both the key and its public variant into
420 # the gsi-openssh configuration directory.
426 my($regex, $basename);
430 debug1("Linking ssh host keys...\n");
432 for my $f (@$linklist)
439 $pubkeyfile = "$f.pub";
441 linkFile("$localsshdir/$keyfile", "$sysconfdir/$keyfile");
442 linkFile("$localsshdir/$pubkeyfile", "$sysconfdir/$pubkeyfile");
450 # return true if the user passed in the force flag. return false otherwise.
455 if ( defined($force) && $force )
465 ### isReadable( $file )
467 # given a file, return true if that file both exists and is readable by the
468 # effective user id. return false otherwise.
475 if ( ( -e $file ) && ( -r $file ) )
485 ### isExecutable( $file )
487 # return true if $file is executable. return false otherwise.
504 ### isWritable( $file )
506 # given a file, return true if that file does not exist or is writable by the
507 # effective user id. return false otherwise.
514 if ( ( ! -e $file ) || ( -w $file ) )
524 ### isPresent( $file )
526 # given a file, return true if that file exists. return false otherwise.
545 # make the gsi-openssh configuration directory if it doesn't already exist.
550 if ( isPresent($sysconfdir) )
552 if ( -d $sysconfdir )
557 debug1("${sysconfdir} already exists and is not a directory!\n");
561 debug1("Could not find ${sysconfdir} directory... creating.\n");
562 action("mkdir -p $sysconfdir");
569 # based on a set of key types, triage them to determine if for each key type, that
570 # key type should be copied from the main ssh configuration directory, or if it
571 # should be generated using ssh-keygen.
576 my($keyhash, $keylist);
580 # initialize our variables
586 $keyhash->{gen} = []; # a list of keytypes to generate
587 $keyhash->{link} = []; # a list of files to link
589 $genlist = $keyhash->{gen};
590 $linklist = $keyhash->{link};
593 # loop over our keytypes and determine what we need to do for each of them
596 for my $keytype (keys %$keyfiles)
598 $basekeyfile = $keyfiles->{$keytype};
601 # if the key's are already present, we don't need to bother with this rigamarole
604 $gkeyfile = "$sysconfdir/$basekeyfile";
605 $gpubkeyfile = "$sysconfdir/$basekeyfile.pub";
607 if ( isPresent($gkeyfile) && isPresent($gpubkeyfile) )
611 if ( isWritable("$sysconfdir/$basekeyfile") && isWritable("$sysconfdir/$basekeyfile.pub") )
613 action("rm $sysconfdir/$basekeyfile");
614 action("rm $sysconfdir/$basekeyfile.pub");
624 # if we can find a copy of the keys in /etc/ssh, we'll link them to the user's
628 $mainkeyfile = "$localsshdir/$basekeyfile";
629 $mainpubkeyfile = "$localsshdir/$basekeyfile.pub";
631 if ( isPresent($mainkeyfile) && isPresent($mainpubkeyfile) )
633 push(@$linklist, $basekeyfile);
639 # otherwise, we need to generate the key
642 push(@$genlist, $keytype);
649 ### runKeyGen( $gen_keys )
651 # given a set of key types, generate private and public keys for that key type and
652 # place them in the gsi-openssh configuration directory.
658 my $keygen = "$bindir/ssh-keygen";
660 if (@$gen_keys && -x $keygen)
662 debug1("Generating ssh host keys...\n");
664 for my $k (@$gen_keys)
666 $keyfile = $keyfiles->{$k};
668 if ( !isPresent("$sysconfdir/$keyfile") )
670 action("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
678 ### copySSHDConfigFile( )
680 # this subroutine 'edits' the paths in sshd_config to suit them to the current environment
681 # in which the setup script is being run.
684 sub copySSHDConfigFile
686 my($fileInput, $fileOutput);
687 my($mode, $uid, $gid);
689 my($privsep_enabled);
691 debug1("Fixing paths in sshd_config...\n");
693 $fileInput = "$setupdir/sshd_config.in";
694 $fileOutput = "$sysconfdir/sshd_config";
697 # verify that we are prepared to work with $fileInput
700 if ( !isReadable($fileInput) )
702 debug1("Cannot read $fileInput... skipping.\n");
707 # verify that we are prepared to work with $fileOuput
710 if ( !prepareFileWrite($fileOutput) )
716 # check to see whether we should enable privilege separation
719 if ( userExists("sshd") && ( -d "/var/empty" ) && ( getOwnerID("/var/empty") eq 0 ) )
721 setPrivilegeSeparation(1);
725 setPrivilegeSeparation(0);
728 if ( getPrivilegeSeparation() )
730 $privsep_enabled = "yes";
734 $privsep_enabled = "no";
738 # Grab the current mode/uid/gid for use later
741 $mode = (stat($fileInput))[2];
742 $uid = (stat($fileInput))[4];
743 $gid = (stat($fileInput))[5];
746 # Open the files for reading and writing, and loop over the input's contents
749 $data = readFile($fileInput);
752 # # alter the PidFile config
755 # $text = "PidFile\t$gpath/var/sshd.pid";
756 # $data =~ s:^[\s|#]*PidFile.*$:$text:gm;
759 # set the sftp directive
762 $text = "Subsystem\tsftp\t$gpath/libexec/sftp-server";
763 $data =~ s:^[\s|#]*Subsystem\s+sftp\s+.*$:$text:gm;
766 # set the privilege separation directive
769 $text = "UsePrivilegeSeparation\t${privsep_enabled}";
770 $data =~ s:^[\s|#]*UsePrivilegeSeparation.*$:$text:gm;
773 # dump the modified output to the config file
776 writeFile($fileOutput, $data);
779 # An attempt to revert the new file back to the original file's
783 chmod($mode, $fileOutput);
784 chown($uid, $gid, $fileOutput);
789 ### setPrivilegeSeparation( $value )
791 # set the privilege separation variable to $value
794 sub setPrivilegeSeparation
801 ### getPrivilegeSeparation( )
803 # return the value of the privilege separation variable
806 sub getPrivilegeSeparation
811 ### prepareFileWrite( $file )
813 # test $file to prepare for writing to it.
820 if ( isPresent($file) )
822 debug1("$file already exists... ");
826 if ( isWritable($file) )
828 debug1("removing.\n");
834 debug1("not writable -- skipping.\n");
840 debug1("skipping.\n");
848 ### copyConfigFiles( )
850 # subroutine that copies some extra config files to their proper location in
851 # $GLOBUS_LOCATION/etc/ssh.
857 # copy the sshd_config file into the ssh configuration directory and alter
858 # the paths in the file.
861 copySSHDConfigFile();
864 # do straight copies of the ssh_config and moduli files.
867 debug1("Copying ssh_config and moduli to their proper location...\n");
869 copyFile("$setupdir/ssh_config", "$sysconfdir/ssh_config");
870 copyFile("$setupdir/moduli", "$sysconfdir/moduli");
873 # copy and alter the SXXsshd script.
876 copySXXScript("$setupdir/SXXsshd.in", "$sbindir/SXXsshd");
879 ### linkFile( $src, $dest )
881 # create a symbolic link from $src to $dest.
886 my($src, $dest) = @_;
888 if ( !isPresent($src) )
890 debug1("$src is not readable... not creating $dest.\n");
894 if ( !prepareFileWrite($dest) )
899 action("ln -s $src $dest");
902 ### copyFile( $src, $dest )
904 # copy the file pointed to by $src to the location specified by $dest. in the
905 # process observe the rules regarding when the '-force' flag was passed to us.
910 my($src, $dest) = @_;
912 if ( !isReadable($src) )
914 debug1("$src is not readable... not creating $dest.\n");
918 if ( !prepareFileWrite($dest) )
923 action("cp $src $dest");
926 ### copySXXScript( $in, $out )
928 # parse the input file, substituting in place the value of GLOBUS_LOCATION, and
929 # write the result to the output file.
937 if ( !isReadable($in) )
939 debug1("$in is not readable... not creating $out.\n");
943 if ( !prepareFileWrite($out) )
949 # clean up any junk in the globus path variable
953 $tmpgpath =~ s:/+:/:g;
954 $tmpgpath =~ s:([^/]+)/$:\1:g;
957 # read in the script, substitute globus location, then write it back out
960 $data = readFile($in);
961 $data =~ s|\@GLOBUS_LOCATION\@|$tmpgpath|g;
962 writeFile($out, $data);
963 action("chmod 755 $out");
966 ### readFile( $filename )
968 # reads and returns $filename's contents
976 open(IN, "$filename") || exitDie("ERROR: Can't open '$filename': $!\n");
985 ### writeFile( $filename, $fileinput )
987 # create the inputs to the ssl program at $filename, appending the common name to the
988 # stream in the process
993 my($filename, $fileinput) = @_;
996 # test for a valid $filename
999 if ( !defined($filename) || (length($filename) lt 1) )
1001 exitDie("ERROR: Filename is undefined!\n");
1005 # verify that we are prepared to work with $filename
1008 if ( !prepareFileWrite($filename) )
1014 # write the output to $filename
1017 open(OUT, ">$filename");
1018 print OUT "$fileinput";
1022 ### debug1( $arg1, $arg2 )
1024 # Print out a debugging message at level 1.
1029 debug(string => \@_, level => 1);
1032 ### debug0( $arg1, $arg2 )
1034 # Print out a debugging message at level 0.
1039 debug(string => \@_, level => 0);
1042 ### debug( string => $string, level => $level )
1044 # Print out debugging messages at various levels. Feel free to use debugN() directly
1045 # which in turn calls this subroutine.
1052 if (!defined($args{'level'}))
1057 if ($verbose >= $args{'level'})
1059 printf(@{$args{'string'}});
1063 ### action( $command )
1065 # run $command within a proper system() command.
1072 debug1("$command\n");
1074 my $result = system("$command >/dev/null 2>&1");
1076 if (($result or $?) and $command !~ m!patch!)
1078 exitDie("ERROR: Unable to execute $command: $!\n");
1082 ### exitDie( $error )
1084 # a horribly named method meant to look like die but only exit, thereby not causing
1085 # gpt-postinstall to croak.
1096 ### query_boolean( $query_text, $default )
1098 # query the user with a string, and expect a response. If the user hits
1099 # 'enter' instead of entering an input, then accept the default response.
1104 my($query_text, $default) = @_;
1105 my($nondefault, $foo, $bar);
1109 print "Prompt suppressed. Continuing...\n";
1114 # Set $nondefault to the boolean opposite of $default.
1117 if ($default eq "n")
1126 print "${query_text} ";
1127 print "[$default] ";
1130 ($bar) = split //, $foo;
1132 if ( grep(/\s/, $bar) )
1134 # 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) )