]> andersk Git - gssapi-openssh.git/blame_incremental - setup/setup-openssh.pl
o Make the setup script more resilient to errors.
[gssapi-openssh.git] / setup / setup-openssh.pl
... / ...
CommitLineData
1#!/usr/bin/perl
2#
3# setup-openssh.pl
4#
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.
8#
9# Send comments/fixes/suggestions to:
10# Chase Phillips <cphillip@ncsa.uiuc.edu>
11#
12
13printf("setup-openssh.pl: Configuring gsi-openssh package\n");
14
15#
16# Get user's GPT_LOCATION since we may be installing this using a new(er)
17# version of GPT.
18#
19
20$gptpath = $ENV{GPT_LOCATION};
21
22#
23# And the old standby..
24#
25
26$gpath = $ENV{GLOBUS_LOCATION};
27if (!defined($gpath))
28{
29 die "GLOBUS_LOCATION needs to be set before running this script"
30}
31
32#
33# modify the ld library path for when we call ssh executables
34#
35
36$oldldpath = $ENV{LD_LIBRARY_PATH};
37$newldpath = "$gpath/lib";
38if (length($oldldpath) > 0)
39{
40 $newldpath .= ":$oldldpath";
41}
42$ENV{LD_LIBRARY_PATH} = "$newldpath";
43
44#
45# i'm including this because other perl scripts in the gpt setup directories
46# do so
47#
48
49if (defined($gptpath))
50{
51 @INC = (@INC, "$gptpath/lib/perl", "$gpath/lib/perl");
52}
53else
54{
55 @INC = (@INC, "$gpath/lib/perl");
56}
57
58require Grid::GPT::Setup;
59
60my $globusdir = $gpath;
61my $myname = "setup-openssh.pl";
62
63#
64# Set up path prefixes for use in the path translations
65#
66
67$prefix = ${globusdir};
68$exec_prefix = "${prefix}";
69$bindir = "${exec_prefix}/bin";
70$sbindir = "${exec_prefix}/sbin";
71$sysconfdir = "$prefix/etc/ssh";
72$localsshdir = "/etc/ssh";
73$setupdir = "$prefix/setup/gsi_openssh_setup";
74
75my $keyfiles = {
76 "dsa" => "ssh_host_dsa_key",
77 "rsa" => "ssh_host_rsa_key",
78 "rsa1" => "ssh_host_key",
79 };
80
81sub copyKeyFiles
82{
83 my($copylist) = @_;
84 my($regex, $basename);
85
86 if (@$copylist)
87 {
88 print "Copying ssh host keys...\n";
89
90 for my $f (@$copylist)
91 {
92 $f =~ s:/+:/:g;
93
94 if (length($f) > 0)
95 {
96 $keyfile = "$f";
97 $pubkeyfile = "$f.pub";
98
99 action("cp $localsshdir/$keyfile $sysconfdir/$keyfile");
100 action("cp $localsshdir/$pubkeyfile $sysconfdir/$pubkeyfile");
101 }
102 }
103 }
104}
105
106sub isReadable
107{
108 my($file) = @_;
109
110 if ( ( -e $file ) && ( -r $file ) )
111 {
112 return 1;
113 }
114 else
115 {
116 return 0;
117 }
118}
119
120sub isPresent
121{
122 my($file) = @_;
123
124 if ( -e $file )
125 {
126 return 1;
127 }
128 else
129 {
130 return 0;
131 }
132}
133
134sub determineKeys
135{
136 my($keyhash, $keylist);
137 my($count);
138
139 #
140 # initialize our variables
141 #
142
143 $count = 0;
144
145 $keyhash = {};
146 $keyhash->{gen} = []; # a list of keytypes to generate
147 $keyhash->{copy} = []; # a list of files to copy from the
148
149 $genlist = $keyhash->{gen};
150 $copylist = $keyhash->{copy};
151
152 #
153 # loop over our keytypes and determine what we need to do for each of them
154 #
155
156 for my $keytype (keys %$keyfiles)
157 {
158 $basekeyfile = $keyfiles->{$keytype};
159
160 #
161 # if the key's are already present, we don't need to bother with this rigamarole
162 #
163
164 $gkeyfile = "$sysconfdir/$basekeyfile";
165 $gpubkeyfile = "$sysconfdir/$basekeyfile.pub";
166
167 if ( isPresent($gkeyfile) && isPresent($gpubkeyfile) )
168 {
169 next;
170 }
171
172 #
173 # if we can find a copy of the keys in /etc/ssh, we'll copy them to the user's
174 # globus location
175 #
176
177 $mainkeyfile = "$localsshdir/$basekeyfile";
178 $mainpubkeyfile = "$localsshdir/$basekeyfile.pub";
179
180 if ( isReadable($mainkeyfile) && isReadable($mainpubkeyfile) )
181 {
182 push(@$copylist, $basekeyfile);
183 $count++;
184 next;
185 }
186
187 #
188 # otherwise, we need to generate the key
189 #
190
191 push(@$genlist, $keytype);
192 $count++;
193 }
194
195 if ($count > 0)
196 {
197 if ( ! -d $sysconfdir )
198 {
199 print "Could not find ${sysconfdir} directory... creating\n";
200 action("mkdir -p $sysconfdir");
201 }
202 }
203
204 return $keyhash;
205}
206
207sub runKeyGen
208{
209 my($gen_keys) = @_;
210 my $keygen = "$bindir/ssh-keygen";
211
212 if (@$gen_keys && -x $keygen)
213 {
214 print "Generating ssh host keys...\n";
215
216 for my $k (@$gen_keys)
217 {
218 $keyfile = $keyfiles->{$k};
219
220 # if $sysconfdir/$keyfile doesn't exist..
221 action("$bindir/ssh-keygen -t $k -f $sysconfdir/$keyfile -N \"\"");
222 }
223 }
224
225 return 0;
226}
227
228sub fixpaths
229{
230 my $g, $h;
231
232 print "Fixing sftp-server path in sshd_config...\n";
233
234 $f = "$gpath/etc/ssh/sshd_config";
235 $g = "$f.tmp";
236
237 if ( ! -f "$f" )
238 {
239 printf("Cannot find $f!");
240 return;
241 }
242
243 #
244 # Grab the current mode/uid/gid for use later
245 #
246
247 $mode = (stat($f))[2];
248 $uid = (stat($f))[4];
249 $gid = (stat($f))[5];
250
251 #
252 # Move $f into a .tmp file for the translation step
253 #
254
255 $result = system("mv $f $g 2>&1");
256 if ($result or $?)
257 {
258 die "ERROR: Unable to execute command: $!\n";
259 }
260
261 open(IN, "<$g") || die ("$0: input file $g missing!\n");
262 open(OUT, ">$f") || die ("$0: unable to open output file $f!\n");
263
264 while (<IN>)
265 {
266 #
267 # sorry for the whacky regex, but i need to verify a whole line
268 #
269
270 if ( /^\s*Subsystem\s+sftp\s+\S+\s*$/ )
271 {
272 $_ = "Subsystem\tsftp\t$gpath/libexec/sftp-server\n";
273 $_ =~ s:/+:/:g;
274 }
275 print OUT "$_";
276 } # while <IN>
277
278 close(OUT);
279 close(IN);
280
281 #
282 # Remove the old .tmp file
283 #
284
285 $result = system("rm $g 2>&1");
286
287 if ($result or $?)
288 {
289 die "ERROR: Unable to execute command: $!\n";
290 }
291
292 #
293 # An attempt to revert the new file back to the original file's
294 # mode/uid/gid
295 #
296
297 chmod($mode, $f);
298 chown($uid, $gid, $f);
299
300 return 0;
301}
302
303sub alterFileGlobusLocation
304{
305 my ($in, $out) = @_;
306
307 if ( -r $in )
308 {
309 if ( ( -w $out ) || ( ! -e $out ) )
310 {
311 $data = readFile($in);
312 $data =~ s|\@GLOBUS_LOCATION\@|$gpath|g;
313 writeFile($out, $data);
314 action("chmod 755 $out");
315 }
316 }
317}
318
319sub alterFiles
320{
321 alterFileGlobusLocation("$setupdir/SXXsshd.in", "$sbindir/SXXsshd");
322}
323
324### readFile( $filename )
325#
326# reads and returns $filename's contents
327#
328
329sub readFile
330{
331 my ($filename) = @_;
332 my $data;
333
334 open (IN, "$filename") || die "Can't open '$filename': $!";
335 $/ = undef;
336 $data = <IN>;
337 $/ = "\n";
338 close(IN);
339
340 return $data;
341}
342
343### writeFile( $filename, $fileinput )
344#
345# create the inputs to the ssl program at $filename, appending the common name to the
346# stream in the process
347#
348
349sub writeFile
350{
351 my ($filename, $fileinput) = @_;
352
353 #
354 # test for a valid $filename
355 #
356
357 if ( !defined($filename) || (length($filename) lt 1) )
358 {
359 die "Filename is undefined";
360 }
361
362 if ( ( -e "$filename" ) && ( ! -w "$filename" ) )
363 {
364 die "Cannot write to filename '$filename'";
365 }
366
367 #
368 # write the output to $filename
369 #
370
371 open(OUT, ">$filename");
372 print OUT "$fileinput";
373 close(OUT);
374}
375
376print "---------------------------------------------------------------------\n";
377print "Hi, I'm the setup script for the gsi_openssh package! There\n";
378print "are some last minute details that I've got to set straight\n";
379print "in the sshd config file, along with generating the ssh keys\n";
380print "for this machine (if it doesn't already have them).\n";
381print "\n";
382print "If I find a pair of host keys in /etc/ssh, I will copy them into\n";
383print "\$GLOBUS_LOCATION/etc/ssh. If they aren't present, I will generate\n";
384print "them for you.\n";
385print "\n";
386
387$response = query_boolean("Do you wish to continue with the setup package?","y");
388if ($response eq "n")
389{
390 print "\n";
391 print "Okay.. exiting gsi_openssh setup.\n";
392
393 exit 0;
394}
395
396print "\n";
397
398$keyhash = determineKeys();
399runKeyGen($keyhash->{gen});
400copyKeyFiles($keyhash->{copy});
401fixpaths();
402alterFiles();
403
404my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
405
406$metadata->finish();
407
408print "\n";
409print "Additional Notes:\n";
410print "\n";
411print " o I see that you have your GLOBUS_LOCATION environmental variable\n";
412print " set to:\n";
413print "\n";
414print " \t\"$gpath\"\n";
415print "\n";
416print " Remember to keep this variable set (correctly) when you want to\n";
417print " use the executables that came with this package.\n";
418print "\n";
419print " o You may need to set LD_LIBRARY_PATH to point to the location in\n";
420print " which your globus libraries reside. For example:\n";
421print "\n";
422print " \t\$ LD_LIBRARY_PATH=\"$gpath/lib:\$LD_LIBRARY_PATH\"; \\\n";
423print " \t export LD_LIBRARY_PATH\n";
424print "\n";
425print " If you wish, you may run, e.g.:\n";
426print "\n";
427print " \t\$ . \$GLOBUS_LOCATION/etc/globus-user-env.sh\n";
428print "\n";
429print " to prepare your environment for running the gsi_openssh\n";
430print " executables.\n";
431print "\n";
432print "---------------------------------------------------------------------\n";
433print "$myname: Finished configuring package 'gsi_openssh'.\n";
434
435#
436# Just need a minimal action() subroutine for now..
437#
438
439sub action
440{
441 my ($command) = @_;
442
443 printf "$command\n";
444
445 my $result = system("LD_LIBRARY_PATH=\"$gpath/lib:\$LD_LIBRARY_PATH\"; $command 2>&1");
446
447 if (($result or $?) and $command !~ m!patch!)
448 {
449 die "ERROR: Unable to execute command: $!\n";
450 }
451}
452
453sub query_boolean
454{
455 my ($query_text, $default) = @_;
456 my $nondefault, $foo, $bar;
457
458 #
459 # Set $nondefault to the boolean opposite of $default.
460 #
461
462 if ($default eq "n")
463 {
464 $nondefault = "y";
465 }
466 else
467 {
468 $nondefault = "n";
469 }
470
471 print "${query_text} ";
472 print "[$default] ";
473
474 $foo = <STDIN>;
475 ($bar) = split //, $foo;
476
477 if ( grep(/\s/, $bar) )
478 {
479 # this is debatable. all whitespace means 'default'
480
481 $bar = $default;
482 }
483 elsif ($bar ne $default)
484 {
485 # everything else means 'nondefault'.
486
487 $bar = $nondefault;
488 }
489 else
490 {
491 # extraneous step. to get here, $bar should be eq to $default anyway.
492
493 $bar = $default;
494 }
495
496 return $bar;
497}
This page took 0.297571 seconds and 5 git commands to generate.