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