]> andersk Git - gssapi-openssh.git/blob - setup/setup-openssh.pl
o Add 'configuring' message to the beginning of the setup script.
[gssapi-openssh.git] / setup / setup-openssh.pl
1 #
2 # setup-openssh.pl:
3 #   Adapts the installed gsi-ssh environment to the current machine,
4 #   performing actions that originally occurred during the package's
5 #   'make install' phase.
6 #
7 # Parts adapted from 'fixpath', a tool found in openssh-3.0.2p1.
8 #
9 # Send comments/fixes/suggestions to:
10 # Chase Phillips <cphillip@ncsa.uiuc.edu>
11 #
12
13 printf("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};
27 if (!defined($gpath))
28 {
29     die "GLOBUS_LOCATION needs to be set before running this script"
30 }
31
32 #
33 # i'm including this because other perl scripts in the gpt setup directories
34 # do so
35 #
36
37 if (defined($gptpath))
38 {
39     @INC = (@INC, "$gptpath/lib/perl", "$gpath/lib/perl");
40 }
41 else
42 {
43     @INC = (@INC, "$gpath/lib/perl");
44 }
45
46 require Grid::GPT::Setup;
47
48 my $globusdir = $gpath;
49 my $setupdir = "$globusdir/setup/globus";
50 my $myname = "setup-openssh.pl";
51
52 #
53 # Set up path prefixes for use in the path translations
54 #
55
56 $prefix = ${globusdir};
57 $exec_prefix = "${prefix}";
58 $bindir = "${exec_prefix}/bin";
59 $sbindir = "${exec_prefix}/sbin";
60 $mandir = "${prefix}/man";
61 $mansubdir = "man";
62 $libexecdir = "${exec_prefix}/libexec";
63 $sysconfdir = "/etc/ssh";
64 $piddir = "/var/run";
65 $xauth_path = "/usr/bin/X11/xauth";
66
67 #
68 # Backup-related variables
69 #
70
71 $curr_time = time();
72 $backupdir = "/etc/ssh/globus_backup_${curr_time}";
73
74 #
75 # Check that we are running as root
76 #
77
78 $uid = $>;
79
80 if ($uid != 0)
81 {
82     print "--> NOTE: You must be root to run this script! <--\n";
83     exit 0;
84 }
85
86 #
87 # We need to make sure it's okay to copy our setup files (if some files are already
88 # present).  If we do copy any files, we backup the old files so the user can (possibly)
89 # reverse any damage.
90 #
91
92 sub test_dirs
93 {
94     print "\nPreparatory: Checking for existence of critical directories..\n";
95
96     #
97     # Remember to put in check for /etc
98     #
99
100     #
101     # Test for /etc/ssh
102     #
103
104     if ( ! -d "$sysconfdir" )
105     {
106         print "Could not find directory: '${sysconfdir}'.. creating.\n";
107         mkdir($sysconfdir, 16877);
108         # 16877 should be 755, or drwxr-xr-x
109     }
110
111     #
112     # Test for /etc/ssh/globus_backup_<curr>
113     #
114
115     if ( ! -d "${backupdir}" )
116     {
117         print "Could not find directory: '${backupdir}'.. creating.\n";
118         mkdir($backupdir, 16877);
119     }
120
121     return 0;
122 }
123
124 sub backup_files
125 {
126     print "\nStage 1: Backing up configuration files to '${backupdir}/'..\n";
127
128     if ( -e "${sysconfdir}/ssh_config" )
129     {
130         action("cp ${sysconfdir}/ssh_config ${backupdir}/ssh_config");
131     }
132     else
133     {
134         print "${sysconfdir}/ssh_config does not exist.\n";
135     }
136
137     if ( -e "${sysconfdir}/sshd_config" )
138     {
139         action("cp ${sysconfdir}/sshd_config ${backupdir}/sshd_config");
140     }
141     else
142     {
143         print "${sysconfdir}/sshd_config does not exist.\n";
144     }
145
146     if ( -e "${sysconfdir}/moduli" )
147     {
148         action("cp ${sysconfdir}/moduli ${backupdir}/moduli");
149     }
150     else
151     {
152         print "${sysconfdir}/moduli does not exist.\n";
153     }
154 }
155
156 sub copy_setup_files
157 {
158     my $response;
159
160     print "\nStage 2: Copying configuration files into '${sysconfdir}'..\n";
161
162     action("cp ${globusdir}/setup/globus/ssh_config ${sysconfdir}/ssh_config");
163     action("cp ${globusdir}/setup/globus/sshd_config ${sysconfdir}/sshd_config");
164     action("cp ${globusdir}/setup/globus/moduli ${sysconfdir}/moduli");
165 }
166
167 sub runkeygen
168 {
169     print "\nStage 3: Generating ssh host keys..\n";
170
171     if ( ! -d "${sysconfdir}" )
172     {
173         print "Could not find ${sysconfdir} directory... creating\n";
174         mkdir($sysconfdir, 16877);
175         # 16877 should be 755, or drwxr-xr-x
176     }
177
178     if ( -e "${sysconfdir}/ssh_host_key" )
179     {
180         print "${sysconfdir}/ssh_host_key already exists, skipping.\n";
181     }
182     else
183     {
184         # if $sysconfdir/ssh_host_key doesn't exist..
185         action("$bindir/ssh-keygen -t rsa1 -f $sysconfdir/ssh_host_key -N \"\"");
186     }
187
188     if ( -e "${sysconfdir}/ssh_host_dsa_key" )
189     {
190         print "${sysconfdir}/ssh_host_dsa_key already exists, skipping.\n";
191     }
192     else
193     {
194         # if $sysconfdir/ssh_host_dsa_key doesn't exist..
195         action("$bindir/ssh-keygen -t dsa -f $sysconfdir/ssh_host_dsa_key -N \"\"");
196     }
197
198     if ( -e "${sysconfdir}/ssh_host_rsa_key" )
199     {
200         print "${sysconfdir}/ssh_host_rsa_key already exists, skipping.\n";
201     }
202     else
203     {
204         # if $sysconfdir/ssh_host_rsa_key doesn't exist..
205         action("$bindir/ssh-keygen -t rsa -f $sysconfdir/ssh_host_rsa_key -N \"\"");
206     }
207
208     return 0;
209 }
210
211 sub fixpaths
212 {
213     my $g, $h;
214
215     print "\nStage 4: Translating strings in config and man files..\n";
216
217     #
218     # Set up path translations for the installation files
219     #
220
221     %def = (
222         "/etc/ssh_config" => "${sysconfdir}/ssh_config",
223         "/etc/ssh_known_hosts" => "${sysconfdir}/ssh_known_hosts",
224         "/etc/sshd_config" => "${sysconfdir}/sshd_config",
225         "/usr/libexec" => "${libexecdir}",
226         "/etc/shosts.equiv" => "${sysconfdir}/shosts.equiv",
227         "/etc/ssh_host_key" => "${sysconfdir}/ssh_host_key",
228         "/etc/ssh_host_dsa_key" => "${sysconfdir}/ssh_host_dsa_key",
229         "/etc/ssh_host_rsa_key" => "${sysconfdir}/ssh_host_rsa_key",
230         "/var/run/sshd.pid" => "${piddir}/sshd.pid",
231         "/etc/moduli" => "${sysconfdir}/moduli",
232         "/etc/sshrc" => "${sysconfdir}/sshrc",
233         "/usr/X11R6/bin/xauth" => "${xauth_path}",
234         "/usr/bin:/bin:/usr/sbin:/sbin" => "/usr/bin:/bin:/usr/sbin:/sbin:${bindir}",
235         );
236
237     #
238     # Files on which to perform path translations
239     #
240
241     @files = (
242         "${sysconfdir}/ssh_config",
243         "${sysconfdir}/sshd_config",
244         "${sysconfdir}/moduli",
245         "${mandir}/${mansubdir}1/scp.1",
246         "${mandir}/${mansubdir}1/ssh-add.1",
247         "${mandir}/${mansubdir}1/ssh-agent.1",
248         "${mandir}/${mansubdir}1/ssh-keygen.1",
249         "${mandir}/${mansubdir}1/ssh-keyscan.1",
250         "${mandir}/${mansubdir}1/ssh.1",
251         "${mandir}/${mansubdir}8/sshd.8",
252         "${mandir}/${mansubdir}8/sftp-server.8",
253         "${mandir}/${mansubdir}1/sftp.1",
254         );
255
256     for my $f (@files)
257     {
258         $f =~ /(.*\/)*(.*)$/;
259
260         #
261         # we really should create a random filename and make sure that it
262         # doesn't already exist (based off current time_t or something)
263         #
264
265         $g = "$f.tmp";
266
267         #
268         # What is $f's filename? (taken from the qualified path)
269         #
270
271         $h = $f;
272         $h =~ s#^.*/##;
273
274         #
275         # Grab the current mode/uid/gid for use later
276         #
277
278         $mode = (stat($f))[2];
279         $uid = (stat($f))[4];
280         $gid = (stat($f))[5];
281
282         #
283         # Move $f into a .tmp file for the translation step
284         #
285
286         $result = system("mv $f $g 2>&1");
287         if ($result or $?)
288         {
289             die "ERROR: Unable to execute command: $!\n";
290         }
291
292         open(IN, "<$g") || die ("$0: input file $g missing!\n");
293         open(OUT, ">$f") || die ("$0: unable to open output file $f!\n");
294
295         while (<IN>)
296         {
297             for $s (keys(%def))
298             {
299                 s#$s#$def{$s}#;
300             } # for $s
301             print OUT "$_";
302         } # while <IN>
303
304         close(OUT);
305         close(IN);
306
307         #
308         # Remove the old .tmp file
309         #
310
311         $result = system("rm $g 2>&1");
312
313         if ($result or $?)
314         {
315             die "ERROR: Unable to execute command: $!\n";
316         }
317
318         #
319         # An attempt to revert the new file back to the original file's
320         # mode/uid/gid
321         #
322
323         chmod($mode, $f);
324         chown($uid, $gid, $f);
325
326         print "$h\n";
327     } # for $f
328
329     return 0;
330 }
331
332 print "---------------------------------------------------------------\n";
333 print "$myname: Configuring package gsi_openssh..\n";
334 print "\n";
335 print "Hi, I'm the setup script for the gsi_openssh package!  There\n";
336 print "are some last minute details that I've got to set straight\n";
337 print "in the config and man files, along with generating the ssh keys\n";
338 print "for this machine (if it doesn't already have them).\n";
339 print "\n";
340 print "I like to install my config-related files in:\n";
341 print "  ${sysconfdir}/\n";
342 print "\n";
343 print "These files may overwrite your previously existing configuration\n";
344 print "files.  If you choose to continue, you will find a backup of\n";
345 print "those original files in:\n";
346 print "  ${backupdir}/\n";
347 print "\n";
348 print "Your host keys will remain untouched if they are already present.\n";
349 print "If they aren't present, this script will generate them for you.\n";
350 print "\n";
351
352 $response = query_boolean("Do you wish to continue with the setup package?","y");
353
354 if ($response eq "n")
355 {
356     print "\n";
357     print "Okay.. exiting gsi_openssh setup.\n";
358
359     exit 0;
360 }
361
362 test_dirs();
363 backup_files();
364 copy_setup_files();
365 runkeygen();
366 fixpaths();
367
368 my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
369
370 $metadata->finish();
371
372 print "\n";
373 print "$myname: Finished configuring package 'gsi_openssh'.\n";
374 print "\n";
375 print "I see that you have your GLOBUS_LOCATION environmental variable\n";
376 print "set to:\n";
377 print "  $gpath\n";
378 print "\n";
379 print "Remember to keep this variable set (correctly) when you want\n";
380 print "to use the executables that came with this package.\n";
381 print "---------------------------------------------------------------\n";
382
383 #
384 # Just need a minimal action() subroutine for now..
385 #
386
387 sub action
388 {
389     my ($command) = @_;
390
391     printf "$command\n";
392
393     my $result = system("$command 2>&1");
394
395     if (($result or $?) and $command !~ m!patch!)
396     {
397         die "ERROR: Unable to execute command: $!\n";
398     }
399 }
400
401 sub query_boolean
402 {
403     my ($query_text, $default) = @_;
404     my $nondefault, $foo, $bar;
405
406     #
407     # Set $nondefault to the boolean opposite of $default.
408     #
409
410     if ($default eq "n")
411     {
412         $nondefault = "y";
413     }
414     else
415     {
416         $nondefault = "n";
417     }
418
419     print "${query_text} ";
420     print "[$default] ";
421
422     $foo = <STDIN>;
423     ($bar) = split //, $foo;
424
425     if ( grep(/\s/, $bar) )
426     {
427         # this is debatable.  all whitespace means 'default'
428
429         $bar = $default;
430     }
431     elsif ($bar ne $default)
432     {
433         # everything else means 'nondefault'.
434
435         $bar = $nondefault;
436     }
437     else
438     {
439         # extraneous step.  to get here, $bar should be eq to $default anyway.
440
441         $bar = $default;
442     }
443
444     return $bar;
445 }
This page took 0.074679 seconds and 5 git commands to generate.