]> andersk Git - gssapi-openssh.git/blame - setup/setup-openssh.pl
merging OPENSSH_GSSAPI_Protocol1-branch to trunk from tag
[gssapi-openssh.git] / setup / setup-openssh.pl
CommitLineData
7a7884ad 1#!/usr/bin/perl
20d3226a 2#
5002372c 3# setup-openssh.pl
4#
95f536ac 5# Adapts the installed gsi-openssh environment to the current machine,
5002372c 6# performing actions that originally occurred during the package's
7# 'make install' phase.
701aa556 8#
1eab725d 9# Send comments/fixes/suggestions to:
10# Chase Phillips <cphillip@ncsa.uiuc.edu>
701aa556 11#
20d3226a 12
7e12c9a7 13#
14# Get user's GPT_LOCATION since we may be installing this using a new(er)
15# version of GPT.
16#
17
18$gptpath = $ENV{GPT_LOCATION};
19
20#
21# And the old standby..
22#
23
4f276ad7 24$gpath = $ENV{GLOBUS_LOCATION};
ad71c979 25if (!defined($gpath))
26{
53a54c67 27 die "GLOBUS_LOCATION needs to be set before running this script"
ad71c979 28}
29
7a7884ad 30#
31# Include standard modules
32#
33
34use Getopt::Long;
35use Cwd;
36use Cwd 'abs_path';
37
8b73e3d0 38#
39# modify the ld library path for when we call ssh executables
40#
41
42$oldldpath = $ENV{LD_LIBRARY_PATH};
43$newldpath = "$gpath/lib";
44if (length($oldldpath) > 0)
45{
46 $newldpath .= ":$oldldpath";
47}
48$ENV{LD_LIBRARY_PATH} = "$newldpath";
49
ad71c979 50#
51# i'm including this because other perl scripts in the gpt setup directories
52# do so
53#
54
7e12c9a7 55if (defined($gptpath))
56{
57 @INC = (@INC, "$gptpath/lib/perl", "$gpath/lib/perl");
58}
59else
60{
61 @INC = (@INC, "$gpath/lib/perl");
62}
ad71c979 63
4f276ad7 64require Grid::GPT::Setup;
65
7a7884ad 66#
67# script-centred variable initialization
68#
69
ad71c979 70my $globusdir = $gpath;
ad71c979 71my $myname = "setup-openssh.pl";
72
20d3226a 73#
74# Set up path prefixes for use in the path translations
75#
76
d0a1bda7 77$prefix = ${globusdir};
78$exec_prefix = "${prefix}";
79$bindir = "${exec_prefix}/bin";
9cc10d0e 80$sbindir = "${exec_prefix}/sbin";
95f536ac 81$sysconfdir = "$prefix/etc/ssh";
82$localsshdir = "/etc/ssh";
20bb6dc8 83$setupdir = "$prefix/setup/gsi_openssh_setup";
e9ec5455 84
7a7884ad 85#
86# standard key types and their root file name mappings
87#
88
95f536ac 89my $keyfiles = {
90 "dsa" => "ssh_host_dsa_key",
91 "rsa" => "ssh_host_rsa_key",
92 "rsa1" => "ssh_host_key",
93 };
823981ba 94
7a7884ad 95#
96# argument specification. we offload some processing work from later functions
97# to verify correct args by using anon subs in various places.
98#
99
100my($interactive, $force, $verbose);
101
102GetOptions(
103 'interactive!' => \$interactive,
104 'force' => \$force,
105 'verbose' => \$verbose,
106 ) or pod2usage(2);
107
108#
109# main execution. This should find its way into a subroutine at some future
110# point.
111#
112
4f3224f2 113print "$myname: Configuring package 'gsi_openssh'...\n";
114print "---------------------------------------------------------------------\n";
70d921fc 115print "Hi, I'm the setup script for the gsi_openssh package! I will create\n";
116print "a number of configuration files based on your local system setup. I\n";
117print "will also attempt to copy or create a number of SSH key pairs for\n";
118print "this machine.\n";
4f3224f2 119print "\n";
70d921fc 120print "(Loosely, if I find a pair of host keys in /etc/ssh, I will copy them\n";
121print "into \$GLOBUS_LOCATION/etc/ssh. Otherwise, I will generate them for\n";
122print "you.)\n";
4f3224f2 123print "\n";
124
70d921fc 125if ( isForced() )
126{
127 print "WARNING:\n";
128 print "\n";
129 print " Using the '-force' flag will cause all gsi_openssh_setup files to\n";
130 print " be removed and replaced by new versions! Backup any critical\n";
131 print " SSH configuration files before you choose to continue!\n";
132 print "\n";
133}
134
4f3224f2 135$response = query_boolean("Do you wish to continue with the setup package?","y");
136if ($response eq "n")
137{
138 print "\n";
139 print "Exiting gsi_openssh setup.\n";
140
141 exit 0;
142}
143
144print "\n";
145
146makeConfDir();
614e6d8b 147copyPRNGFile();
4f3224f2 148$keyhash = determineKeys();
149runKeyGen($keyhash->{gen});
150copyKeyFiles($keyhash->{copy});
4f3224f2 151copyConfigFiles();
4f3224f2 152
153my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
154
155$metadata->finish();
156
157print "\n";
158print "Additional Notes:\n";
159print "\n";
160print " o I see that you have your GLOBUS_LOCATION environmental variable\n";
161print " set to:\n";
162print "\n";
163print " \t\"$gpath\"\n";
164print "\n";
165print " Remember to keep this variable set (correctly) when you want to\n";
166print " use the executables that came with this package.\n";
167print "\n";
168print " After that you may run, e.g.:\n";
169print "\n";
170print " \t\$ . \$GLOBUS_LOCATION/etc/globus-user-env.sh\n";
171print "\n";
172print " to prepare your environment for running the gsi_openssh\n";
173print " executables.\n";
174print "---------------------------------------------------------------------\n";
175print "$myname: Finished configuring package 'gsi_openssh'.\n";
176
177exit;
178
179#
180# subroutines
181#
182
7a7884ad 183### initPRNGHash( )
184#
185# initialize the PRNG pathname hash
186#
187
188sub initPRNGHash( )
189{
190 #
191 # standard prng to executable conversion names
192 #
193
194 addPRNGCommand("\@PROG_LS\@", "ls");
195 addPRNGCommand("\@PROG_NETSTAT\@", "netstat");
196 addPRNGCommand("\@PROG_ARP\@", "arp");
197 addPRNGCommand("\@PROG_IFCONFIG\@", "ifconfig");
198 addPRNGCommand("\@PROG_PS\@", "ps");
199 addPRNGCommand("\@PROG_JSTAT\@", "jstat");
200 addPRNGCommand("\@PROG_W\@", "w");
201 addPRNGCommand("\@PROG_WHO\@", "who");
202 addPRNGCommand("\@PROG_LAST\@", "last");
203 addPRNGCommand("\@PROG_LASTLOG\@", "lastlog");
204 addPRNGCommand("\@PROG_DF\@", "df");
205 addPRNGCommand("\@PROG_SAR\@", "sar");
206 addPRNGCommand("\@PROG_VMSTAT\@", "vmstat");
207 addPRNGCommand("\@PROG_UPTIME\@", "uptime");
208 addPRNGCommand("\@PROG_IPCS\@", "ipcs");
209 addPRNGCommand("\@PROG_TAIL\@", "tail");
210
211 print "Determining paths for PRNG commands...\n";
212
213 $paths = determinePRNGPaths();
214
215 return;
216}
217
218### getDirectoryPaths( )
219#
220# return an array ref containing all of the directories in which we should search
221# for our listing of executable names.
222#
223
224sub getDirectoryPaths( )
225{
226 #
227 # read in the PATH environmental variable and prepend a set of 'safe'
228 # directories from which to test PRNG commands.
229 #
230
231 $path = $ENV{PATH};
232 $path = "/bin:/usr/bin:/sbin:/usr/sbin:/etc:" . $path;
233 @dirs = split(/:/, $path);
234
235 #
236 # sanitize each directory listed in the array.
237 #
238
239 @dirs = map {
240 $tmp = $_;
241 $tmp =~ s:/+:/:g;
242 $tmp =~ s:^\s+|\s+$::g;
243 $tmp;
244 } @dirs;
245
246 return \@dirs;
247}
248
249### addPRNGCommand( $prng_name, $exec_name )
250#
251# given a PRNG name and a corresponding executable name, add it to our list of
252# PRNG commands for which to find on the system.
253#
254
255sub addPRNGCommand
256{
257 my($prng_name, $exec_name) = @_;
258
259 prngAddNode($prng_name, $exec_name);
260}
261
262### copyPRNGFile( )
263#
264# read in ssh_prng_cmds.in, translate the program listings to the paths we have
265# found on the local system, and then write the output to ssh_prng_cmds.
266#
267
268sub copyPRNGFile
269{
270 my($fileInput, $fileOutput);
271 my($mode, $uid, $gid);
272 my($data);
273
274 if ( isPresent("/dev/random") && !isForced() )
275 {
276 printf("/dev/random found and not forced. Not installing ssh_prng_cmds...\n");
277 return;
278 }
279
280 initPRNGHash();
281
282 print "Fixing paths in ssh_prng_cmds...\n";
283
284 $fileInput = "$setupdir/ssh_prng_cmds.in";
285 $fileOutput = "$sysconfdir/ssh_prng_cmds";
286
287 #
288 # verify that we are prepared to work with $fileInput
289 #
290
291 if ( !isReadable($fileInput) )
292 {
293 printf("Cannot read $fileInput... skipping.\n");
294 return;
295 }
296
297 #
298 # verify that we are prepared to work with $fileOuput
299 #
300
301 if ( !prepareFileWrite($fileOutput) )
302 {
303 return;
304 }
305
306 #
307 # Grab the current mode/uid/gid for use later
308 #
309
310 $mode = (stat($fileInput))[2];
311 $uid = (stat($fileInput))[4];
312 $gid = (stat($fileInput))[5];
313
314 #
315 # Open the files for reading and writing, and loop over the input's contents
316 #
317
318 $data = readFile($fileInput);
319 for my $k (keys %$prngcmds)
320 {
321 $sub = prngGetExecPath($k);
322 $data =~ s:$k:$sub:g;
323 }
324 writeFile($fileOutput, $data);
325
326 #
327 # An attempt to revert the new file back to the original file's
328 # mode/uid/gid
329 #
330
331 chmod($mode, $fileOutput);
332 chown($uid, $gid, $fileOutput);
333
334 return 0;
335}
336
337### determinePRNGPaths( )
338#
339# for every entry in the PRNG hash, seek out and find the path for the
340# corresponding executable name.
341#
342
343sub determinePRNGPaths
344{
345 my(@paths, @dirs);
346 my($exec_name, $exec_path);
347
348 $dirs = getDirectoryPaths();
349
350 for my $k (keys %$prngcmds)
351 {
352 $exec_name = prngGetExecName($k);
353 $exec_path = findExecutable($exec_name, $dirs);
354 prngSetExecPath($k, $exec_path);
355 }
356
357 return;
358}
359
360### prngAddNode( $prng_name, $exec_name )
361#
362# add a new node to the PRNG hash
363#
364
365sub prngAddNode
366{
367 my($prng_name, $exec_name) = @_;
368 my($node);
369
370 if (!defined($prngcmds))
371 {
372 $prngcmds = {};
373 }
374
375 $node = {};
376 $node->{prng} = $prng_name;
377 $node->{exec} = $exec_name;
378
379 $prngcmds->{$prng_name} = $node;
380}
381
382### prngGetExecName( $key )
383#
384# get the executable name from the prng commands hash named by $key
385#
386
387sub prngGetExecName
388{
389 my($key) = @_;
390
391 return $prngcmds->{$key}->{exec};
392}
393
394### prngGetExecPath( $key )
395#
396# get the executable path from the prng commands hash named by $key
397#
398
399sub prngGetExecPath
400{
401 my($key) = @_;
402
403 return $prngcmds->{$key}->{exec_path};
404}
405
406### prngGetNode( $key )
407#
408# return a reference to the node named by $key
409#
410
411sub prngGetNode
412{
413 my($key) = @_;
414
415 return ${$prngcmds}{$key};
416}
417
418### prngSetExecPath( $key, $path )
419#
420# given a key, set the executable path in that node to $path
421#
422
423sub prngSetExecPath
424{
425 my($key, $path) = @_;
426
427 $prngcmds->{$key}->{exec_path} = $path;
428}
429
430### findExecutable( $exec_name, $dirs )
431#
432# given an executable name, test each possible path in $dirs to see if such
433# an executable exists.
434#
435
436sub findExecutable
437{
438 my($exec_name, $dirs) = @_;
439
440 for my $d (@$dirs)
441 {
442 $test = "$d/$exec_name";
443
444 if ( isExecutable($test) )
445 {
446 return $test;
447 }
448 }
449
450 return "undef";
451}
452
4f3224f2 453### copyKeyFiles( $copylist )
454#
455# given an array of keys to copy, copy both the key and its public variant into
456# the gsi-openssh configuration directory.
457#
458
95f536ac 459sub copyKeyFiles
e9ec5455 460{
95f536ac 461 my($copylist) = @_;
462 my($regex, $basename);
e9ec5455 463
712b003f 464 if (@$copylist)
e9ec5455 465 {
712b003f 466 print "Copying ssh host keys...\n";
e9ec5455 467
712b003f 468 for my $f (@$copylist)
95f536ac 469 {
712b003f 470 $f =~ s:/+:/:g;
471
472 if (length($f) > 0)
473 {
474 $keyfile = "$f";
475 $pubkeyfile = "$f.pub";
95f536ac 476
7a7884ad 477 copyFile("$localsshdir/$keyfile", "$sysconfdir/$keyfile");
478 copyFile("$localsshdir/$pubkeyfile", "$sysconfdir/$pubkeyfile");
712b003f 479 }
95f536ac 480 }
e9ec5455 481 }
e9ec5455 482}
483
7a7884ad 484### isForced( )
485#
486# return true if the user passed in the force flag. return false otherwise.
487#
488
489sub isForced
490{
491 if ( defined($force) && $force )
492 {
493 return 1;
494 }
495 else
496 {
497 return 0;
498 }
499}
500
4f3224f2 501### isReadable( $file )
502#
503# given a file, return true if that file both exists and is readable by the
504# effective user id. return false otherwise.
505#
506
95f536ac 507sub isReadable
1a1f62a4 508{
95f536ac 509 my($file) = @_;
1a1f62a4 510
95f536ac 511 if ( ( -e $file ) && ( -r $file ) )
1a1f62a4 512 {
95f536ac 513 return 1;
1a1f62a4 514 }
823981ba 515 else
1a1f62a4 516 {
95f536ac 517 return 0;
ac083f7a 518 }
1a1f62a4 519}
520
7a7884ad 521### isExecutable( $file )
522#
523# return true if $file is executable. return false otherwise.
524#
525
526sub isExecutable
527{
528 my($file) = @_;
529
530 if ( -x $file )
531 {
532 return 1;
533 }
534 else
535 {
536 return 0;
537 }
538}
539
540### isWritable( $file )
541#
542# given a file, return true if that file does not exist or is writable by the
543# effective user id. return false otherwise.
544#
545
546sub isWritable
547{
548 my($file) = @_;
549
550 if ( ( ! -e $file ) || ( -w $file ) )
551 {
552 return 1;
553 }
554 else
555 {
556 return 0;
557 }
558}
559
4f3224f2 560### isPresent( $file )
561#
562# given a file, return true if that file exists. return false otherwise.
563#
564
712b003f 565sub isPresent
566{
567 my($file) = @_;
568
569 if ( -e $file )
570 {
571 return 1;
572 }
573 else
574 {
575 return 0;
576 }
577}
578
c096bf39 579### makeConfDir( )
580#
581# make the gsi-openssh configuration directory if it doesn't already exist.
582#
583
584sub makeConfDir
585{
586 if ( isPresent($sysconfdir) )
587 {
588 if ( -d $sysconfdir )
589 {
590 return;
591 }
592
593 die("${sysconfdir} already exists and is not a directory!\n");
594 }
595
596 print "Could not find ${sysconfdir} directory... creating.\n";
597 action("mkdir -p $sysconfdir");
598
599 return;
600}
601
4f3224f2 602### determineKeys( )
603#
604# based on a set of key types, triage them to determine if for each key type, that
605# key type should be copied from the main ssh configuration directory, or if it
606# should be generated using ssh-keygen.
607#
608
95f536ac 609sub determineKeys
823981ba 610{
95f536ac 611 my($keyhash, $keylist);
612 my($count);
823981ba 613
712b003f 614 #
615 # initialize our variables
616 #
617
95f536ac 618 $count = 0;
823981ba 619
95f536ac 620 $keyhash = {};
621 $keyhash->{gen} = []; # a list of keytypes to generate
622 $keyhash->{copy} = []; # a list of files to copy from the
712b003f 623
95f536ac 624 $genlist = $keyhash->{gen};
625 $copylist = $keyhash->{copy};
e9ec5455 626
712b003f 627 #
628 # loop over our keytypes and determine what we need to do for each of them
629 #
630
95f536ac 631 for my $keytype (keys %$keyfiles)
1a1f62a4 632 {
95f536ac 633 $basekeyfile = $keyfiles->{$keytype};
1a1f62a4 634
712b003f 635 #
636 # if the key's are already present, we don't need to bother with this rigamarole
637 #
638
639 $gkeyfile = "$sysconfdir/$basekeyfile";
640 $gpubkeyfile = "$sysconfdir/$basekeyfile.pub";
641
642 if ( isPresent($gkeyfile) && isPresent($gpubkeyfile) )
95f536ac 643 {
7a7884ad 644 if ( isForced() )
645 {
646 if ( isWritable("$sysconfdir/$basekeyfile") && isWritable("$sysconfdir/$basekeyfile.pub") )
647 {
648 action("rm $sysconfdir/$basekeyfile");
649 action("rm $sysconfdir/$basekeyfile.pub");
650 }
651 else
652 {
653 next;
654 }
655 }
95f536ac 656 }
1a1f62a4 657
712b003f 658 #
659 # if we can find a copy of the keys in /etc/ssh, we'll copy them to the user's
660 # globus location
661 #
662
663 $mainkeyfile = "$localsshdir/$basekeyfile";
664 $mainpubkeyfile = "$localsshdir/$basekeyfile.pub";
665
666 if ( isReadable($mainkeyfile) && isReadable($mainpubkeyfile) )
95f536ac 667 {
712b003f 668 push(@$copylist, $basekeyfile);
95f536ac 669 $count++;
712b003f 670 next;
95f536ac 671 }
712b003f 672
673 #
674 # otherwise, we need to generate the key
675 #
676
677 push(@$genlist, $keytype);
678 $count++;
1a1f62a4 679 }
680
95f536ac 681 return $keyhash;
682}
683
4f3224f2 684### runKeyGen( $gen_keys )
685#
686# given a set of key types, generate private and public keys for that key type and
687# place them in the gsi-openssh configuration directory.
688#
689
95f536ac 690sub runKeyGen
691{
692 my($gen_keys) = @_;
ce935927 693 my $keygen = "$bindir/ssh-keygen";
95f536ac 694
ce935927 695 if (@$gen_keys && -x $keygen)
1a1f62a4 696 {
712b003f 697 print "Generating ssh host keys...\n";
698
699 for my $k (@$gen_keys)
700 {
701 $keyfile = $keyfiles->{$k};
95f536ac 702
7a7884ad 703 if ( !isPresent("$sysconfdir/$keyfile") )
704 {
705 action("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
706 }
712b003f 707 }
1a1f62a4 708 }
709
710 return 0;
711}
712
7a7884ad 713### copySSHDConfigFile( )
5b105785 714#
715# this subroutine 'edits' the paths in sshd_config to suit them to the current environment
716# in which the setup script is being run.
717#
718
7a7884ad 719sub copySSHDConfigFile
20d3226a 720{
5b105785 721 my($fileInput, $fileOutput);
722 my($mode, $uid, $gid);
723 my($line, $newline);
823981ba 724
5b105785 725 print "Fixing paths in sshd_config...\n";
95f536ac 726
5b105785 727 $fileInput = "$setupdir/sshd_config.in";
728 $fileOutput = "$sysconfdir/sshd_config";
95f536ac 729
7a7884ad 730 #
731 # verify that we are prepared to work with $fileInput
732 #
733
734 if ( !isReadable($fileInput) )
95f536ac 735 {
7a7884ad 736 printf("Cannot read $fileInput... skipping.\n");
737 return;
5b105785 738 }
739
7a7884ad 740 #
741 # verify that we are prepared to work with $fileOuput
742 #
743
744 if ( !prepareFileWrite($fileOutput) )
5b105785 745 {
7a7884ad 746 return;
95f536ac 747 }
e9ec5455 748
20d3226a 749 #
95f536ac 750 # Grab the current mode/uid/gid for use later
20d3226a 751 #
752
5b105785 753 $mode = (stat($fileInput))[2];
754 $uid = (stat($fileInput))[4];
755 $gid = (stat($fileInput))[5];
20d3226a 756
20d3226a 757 #
5b105785 758 # Open the files for reading and writing, and loop over the input's contents
20d3226a 759 #
760
5b105785 761 open(IN, "<$fileInput") || die ("$0: input file $fileInput missing!\n");
762 open(OUT, ">$fileOutput") || die ("$0: unable to open output file $fileOutput!\n");
20d3226a 763
95f536ac 764 while (<IN>)
765 {
6193a4af 766 #
767 # sorry for the whacky regex, but i need to verify a whole line
768 #
769
eb4172f6 770 $line = $_;
771 if ( $line =~ /^\s*Subsystem\s+sftp\s+\S+\s*$/ )
e9ec5455 772 {
7a7884ad 773 $line = "Subsystem\tsftp\t$gpath/libexec/sftp-server\n";
774 $line =~ s:/+:/:g;
7c96a399 775 }
eb4172f6 776 elsif ( $line =~ /^\s*PidFile.*$/ )
777 {
7a7884ad 778 $line = "PidFile\t$gpath/var/sshd.pid\n";
779 $line =~ s:/+:/:g;
eb4172f6 780 }
781 else
782 {
7a7884ad 783 # do nothing
eb4172f6 784 }
785
7a7884ad 786 print OUT "$line";
95f536ac 787 } # while <IN>
7c96a399 788
95f536ac 789 close(OUT);
790 close(IN);
7c96a399 791
95f536ac 792 #
793 # An attempt to revert the new file back to the original file's
794 # mode/uid/gid
795 #
7e12c9a7 796
5b105785 797 chmod($mode, $fileOutput);
798 chown($uid, $gid, $fileOutput);
20d3226a 799
800 return 0;
801}
802
7a7884ad 803### prepareFileWrite( $file )
804#
805# test $file to prepare for writing to it.
806#
807
808sub prepareFileWrite
809{
810 my($file) = @_;
811
812 if ( isPresent($file) )
813 {
814 printf("$file already exists... ");
815
816 if ( isForced() )
817 {
818 if ( isWritable($file) )
819 {
820 printf("removing.\n");
821 action("rm $file");
822 return 1;
823 }
824 else
825 {
826 printf("not writable -- skipping.\n");
827 return 0;
828 }
829 }
830 else
831 {
832 printf("skipping.\n");
833 return 0;
834 }
835 }
836
837 return 1;
838}
839
5b105785 840### copyConfigFiles( )
841#
842# subroutine that copies some extra config files to their proper location in
843# $GLOBUS_LOCATION/etc/ssh.
844#
845
846sub copyConfigFiles
847{
7a7884ad 848 #
849 # copy the sshd_config file into the ssh configuration directory and alter
850 # the paths in the file.
851 #
852
853 copySSHDConfigFile();
854
855 #
856 # do straight copies of the ssh_config and moduli files.
857 #
858
859 printf("Copying ssh_config and moduli to their proper location...\n");
860
861 copyFile("$setupdir/ssh_config", "$sysconfdir/ssh_config");
862 copyFile("$setupdir/moduli", "$sysconfdir/moduli");
5b105785 863
7a7884ad 864 #
865 # copy and alter the SXXsshd script.
866 #
867
868 copySXXScript("$setupdir/SXXsshd.in", "$sbindir/SXXsshd");
5b105785 869}
870
7a7884ad 871### copyFile( $src, $dest )
4f3224f2 872#
7a7884ad 873# copy the file pointed to by $src to the location specified by $dest. in the
874# process observe the rules regarding when the '-force' flag was passed to us.
4f3224f2 875#
876
7a7884ad 877sub copyFile
a26c150d 878{
7a7884ad 879 my($src, $dest) = @_;
d58b3a33 880
7a7884ad 881 if ( !isReadable($src) )
20bb6dc8 882 {
7a7884ad 883 printf("$src is not readable... not creating $dest.\n");
884 return;
20bb6dc8 885 }
7a7884ad 886
887 if ( !prepareFileWrite($dest) )
888 {
889 return;
890 }
891
892 action("cp $src $dest");
d58b3a33 893}
894
7a7884ad 895### copySXXScript( $in, $out )
4f3224f2 896#
7a7884ad 897# parse the input file, substituting in place the value of GLOBUS_LOCATION, and
898# write the result to the output file.
4f3224f2 899#
900
7a7884ad 901sub copySXXScript
d58b3a33 902{
7a7884ad 903 my($in, $out) = @_;
904
905 if ( !isReadable($in) )
906 {
907 printf("$in is not readable... not creating $out.\n");
908 return;
909 }
910
911 if ( !prepareFileWrite($out) )
912 {
913 return;
914 }
915
916 $data = readFile($in);
917 $data =~ s|\@GLOBUS_LOCATION\@|$gpath|g;
918 writeFile($out, $data);
919 action("chmod 755 $out");
a26c150d 920}
921
922### readFile( $filename )
923#
924# reads and returns $filename's contents
925#
926
927sub readFile
928{
7a7884ad 929 my($filename) = @_;
930 my($data);
a26c150d 931
7a7884ad 932 open(IN, "$filename") || die "Can't open '$filename': $!";
a26c150d 933 $/ = undef;
934 $data = <IN>;
935 $/ = "\n";
936 close(IN);
937
938 return $data;
939}
940
941### writeFile( $filename, $fileinput )
942#
943# create the inputs to the ssl program at $filename, appending the common name to the
944# stream in the process
945#
946
947sub writeFile
948{
7a7884ad 949 my($filename, $fileinput) = @_;
a26c150d 950
951 #
952 # test for a valid $filename
953 #
954
955 if ( !defined($filename) || (length($filename) lt 1) )
956 {
957 die "Filename is undefined";
958 }
959
7a7884ad 960 #
961 # verify that we are prepared to work with $filename
962 #
963
964 if ( !prepareFileWrite($filename) )
a26c150d 965 {
7a7884ad 966 return;
a26c150d 967 }
968
969 #
970 # write the output to $filename
971 #
972
973 open(OUT, ">$filename");
974 print OUT "$fileinput";
975 close(OUT);
976}
977
7a7884ad 978### action( $command )
ac083f7a 979#
7a7884ad 980# run $command within a proper system() command.
ac083f7a 981#
982
983sub action
984{
7a7884ad 985 my($command) = @_;
ac083f7a 986
987 printf "$command\n";
988
8b73e3d0 989 my $result = system("LD_LIBRARY_PATH=\"$gpath/lib:\$LD_LIBRARY_PATH\"; $command 2>&1");
ac083f7a 990
991 if (($result or $?) and $command !~ m!patch!)
992 {
993 die "ERROR: Unable to execute command: $!\n";
994 }
995}
996
7a7884ad 997### query_boolean( $query_text, $default )
998#
999# query the user with a string, and expect a response. If the user hits
1000# 'enter' instead of entering an input, then accept the default response.
1001#
1002
ac083f7a 1003sub query_boolean
1004{
7a7884ad 1005 my($query_text, $default) = @_;
1006 my($nondefault, $foo, $bar);
ac083f7a 1007
1008 #
1009 # Set $nondefault to the boolean opposite of $default.
1010 #
1011
1012 if ($default eq "n")
1013 {
1014 $nondefault = "y";
1015 }
1016 else
1017 {
1018 $nondefault = "n";
1019 }
1020
1021 print "${query_text} ";
1022 print "[$default] ";
1023
e9ec5455 1024 $foo = <STDIN>;
1025 ($bar) = split //, $foo;
1026
e9d69a89 1027 if ( grep(/\s/, $bar) )
ac083f7a 1028 {
e9d69a89 1029 # this is debatable. all whitespace means 'default'
1030
1031 $bar = $default;
1032 }
1033 elsif ($bar ne $default)
1034 {
1035 # everything else means 'nondefault'.
1036
1037 $bar = $nondefault;
1038 }
1039 else
1040 {
1041 # extraneous step. to get here, $bar should be eq to $default anyway.
1042
e9ec5455 1043 $bar = $default;
ac083f7a 1044 }
1045
e9ec5455 1046 return $bar;
ac083f7a 1047}
c096bf39 1048
1049### absolutePath( $file )
1050#
1051# converts a given pathname into a canonical path using the abs_path function.
1052#
1053
1054sub absolutePath
1055{
1056 my($file) = @_;
1057 my $home = $ENV{'HOME'};
1058 $file =~ s!~!$home!;
1059 my $startd = cwd();
1060 $file =~ s!^\./!$startd/!;
1061 $file = "$startd/$file" if $file !~ m!^\s*/!;
1062 $file = abs_path($file);
1063 return $file;
1064}
This page took 0.863906 seconds and 5 git commands to generate.