]> andersk Git - gssapi-openssh.git/blame - setup/setup-openssh.pl
o Modify output format.
[gssapi-openssh.git] / setup / setup-openssh.pl
CommitLineData
20d3226a 1#
1eab725d 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.
701aa556 6#
7e12c9a7 7# Parts adapted from 'fixpath', a tool found in openssh-3.0.2p1.
1eab725d 8#
9# Send comments/fixes/suggestions to:
10# Chase Phillips <cphillip@ncsa.uiuc.edu>
701aa556 11#
20d3226a 12
1b936a7a 13printf("setup-openssh.pl: Configuring gsi-openssh package\n");
14
7e12c9a7 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
4f276ad7 26$gpath = $ENV{GLOBUS_LOCATION};
ad71c979 27if (!defined($gpath))
28{
53a54c67 29 die "GLOBUS_LOCATION needs to be set before running this script"
ad71c979 30}
31
32#
33# i'm including this because other perl scripts in the gpt setup directories
34# do so
35#
36
7e12c9a7 37if (defined($gptpath))
38{
39 @INC = (@INC, "$gptpath/lib/perl", "$gpath/lib/perl");
40}
41else
42{
43 @INC = (@INC, "$gpath/lib/perl");
44}
ad71c979 45
4f276ad7 46require Grid::GPT::Setup;
47
ad71c979 48my $globusdir = $gpath;
49my $setupdir = "$globusdir/setup/globus";
50my $myname = "setup-openssh.pl";
51
20d3226a 52#
53# Set up path prefixes for use in the path translations
54#
55
d0a1bda7 56$prefix = ${globusdir};
57$exec_prefix = "${prefix}";
58$bindir = "${exec_prefix}/bin";
7c25a6d7 59$sbindir = "${exec_prefix}/sbin";
d0a1bda7 60$mandir = "${prefix}/man";
61$mansubdir = "man";
62$libexecdir = "${exec_prefix}/libexec";
1a1f62a4 63$sysconfdir = "/etc/ssh";
20d3226a 64$piddir = "/var/run";
65$xauth_path = "/usr/bin/X11/xauth";
66
e9ec5455 67#
68# Backup-related variables
69#
70
71$curr_time = time();
823981ba 72$backupdir = "/etc/ssh/globus_backup_${curr_time}";
73
74#
75# Check that we are running as root
76#
77
78$uid = $>;
79
80if ($uid != 0)
81{
82 print "--> NOTE: You must be root to run this script! <--\n";
83 exit 0;
84}
e9ec5455 85
1a1f62a4 86#
ac083f7a 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.
1a1f62a4 90#
91
e9ec5455 92sub test_dirs
93{
823981ba 94 print "\nPreparatory: Checking for existence of critical directories..\n";
e9ec5455 95
96 #
97 # Remember to put in check for /etc
98 #
99
100 #
101 # Test for /etc/ssh
102 #
103
823981ba 104 if ( ! -d "$sysconfdir" )
e9ec5455 105 {
823981ba 106 print "Could not find directory: '${sysconfdir}'.. creating.\n";
107 mkdir($sysconfdir, 16877);
e9ec5455 108 # 16877 should be 755, or drwxr-xr-x
109 }
110
111 #
112 # Test for /etc/ssh/globus_backup_<curr>
113 #
114
823981ba 115 if ( ! -d "${backupdir}" )
e9ec5455 116 {
823981ba 117 print "Could not find directory: '${backupdir}'.. creating.\n";
118 mkdir($backupdir, 16877);
e9ec5455 119 }
120
121 return 0;
122}
123
823981ba 124sub backup_files
1a1f62a4 125{
823981ba 126 print "\nStage 1: Backing up configuration files to '${backupdir}/'..\n";
1a1f62a4 127
1a1f62a4 128 if ( -e "${sysconfdir}/ssh_config" )
129 {
823981ba 130 action("cp ${sysconfdir}/ssh_config ${backupdir}/ssh_config");
1a1f62a4 131 }
823981ba 132 else
1a1f62a4 133 {
823981ba 134 print "${sysconfdir}/ssh_config does not exist.\n";
1a1f62a4 135 }
136
137 if ( -e "${sysconfdir}/sshd_config" )
138 {
823981ba 139 action("cp ${sysconfdir}/sshd_config ${backupdir}/sshd_config");
1a1f62a4 140 }
823981ba 141 else
1a1f62a4 142 {
823981ba 143 print "${sysconfdir}/sshd_config does not exist.\n";
1a1f62a4 144 }
ac083f7a 145
ac083f7a 146 if ( -e "${sysconfdir}/moduli" )
147 {
823981ba 148 action("cp ${sysconfdir}/moduli ${backupdir}/moduli");
ac083f7a 149 }
823981ba 150 else
ac083f7a 151 {
823981ba 152 print "${sysconfdir}/moduli does not exist.\n";
ac083f7a 153 }
1a1f62a4 154}
155
823981ba 156sub 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
1a1f62a4 167sub runkeygen
168{
823981ba 169 print "\nStage 3: Generating ssh host keys..\n";
e9ec5455 170
1a1f62a4 171 if ( ! -d "${sysconfdir}" )
172 {
173 print "Could not find ${sysconfdir} directory... creating\n";
27b0f197 174 mkdir($sysconfdir, 16877);
175 # 16877 should be 755, or drwxr-xr-x
1a1f62a4 176 }
177
1a1f62a4 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..
11b9a41c 185 action("$bindir/ssh-keygen -t rsa1 -f $sysconfdir/ssh_host_key -N \"\"");
1a1f62a4 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..
11b9a41c 195 action("$bindir/ssh-keygen -t dsa -f $sysconfdir/ssh_host_dsa_key -N \"\"");
1a1f62a4 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..
11b9a41c 205 action("$bindir/ssh-keygen -t rsa -f $sysconfdir/ssh_host_rsa_key -N \"\"");
1a1f62a4 206 }
207
208 return 0;
209}
210
20d3226a 211sub fixpaths
212{
7e12c9a7 213 my $g, $h;
823981ba 214
215 print "\nStage 4: Translating strings in config and man files..\n";
e9ec5455 216
20d3226a 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
20d3226a 237 #
238 # Files on which to perform path translations
239 #
240
bc2fc8e1 241 @files = (
de6311a1 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",
20d3226a 254 );
255
bc2fc8e1 256 for my $f (@files)
20d3226a 257 {
258 $f =~ /(.*\/)*(.*)$/;
7536fc6f 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
d0a1bda7 265 $g = "$f.tmp";
20d3226a 266
7e12c9a7 267 #
268 # What is $f's filename? (taken from the qualified path)
269 #
270
271 $h = $f;
272 $h =~ s#^.*/##;
273
7536fc6f 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
7c96a399 282 #
283 # Move $f into a .tmp file for the translation step
284 #
20d3226a 285
7c96a399 286 $result = system("mv $f $g 2>&1");
287 if ($result or $?)
e9ec5455 288 {
7c96a399 289 die "ERROR: Unable to execute command: $!\n";
290 }
291
d0a1bda7 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))
20d3226a 298 {
d0a1bda7 299 s#$s#$def{$s}#;
300 } # for $s
301 print OUT "$_";
302 } # while <IN>
20d3226a 303
d0a1bda7 304 close(OUT);
20d3226a 305 close(IN);
d0a1bda7 306
7c96a399 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 }
7536fc6f 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);
7e12c9a7 325
326 print "$h\n";
20d3226a 327 } # for $f
328
329 return 0;
330}
331
7d7b4cd9 332print "---------------------------------------------------------------\n";
823981ba 333print "Hi, I'm the setup script for the gsi_openssh package! There\n";
334print "are some last minute details that I've got to set straight\n";
335print "in the config and man files, along with generating the ssh keys\n";
336print "for this machine (if it doesn't already have them).\n";
337print "\n";
99d7609c 338print "I like to install my config-related files in:\n";
4a314dfa 339print " ${sysconfdir}/\n";
7e12c9a7 340print "\n";
341print "These files may overwrite your previously existing configuration\n";
342print "files. If you choose to continue, you will find a backup of\n";
343print "those original files in:\n";
823981ba 344print " ${backupdir}/\n";
345print "\n";
346print "Your host keys will remain untouched if they are already present.\n";
347print "If they aren't present, this script will generate them for you.\n";
348print "\n";
349
350$response = query_boolean("Do you wish to continue with the setup package?","y");
351
e9d69a89 352if ($response eq "n")
823981ba 353{
354 print "\n";
355 print "Okay.. exiting gsi_openssh setup.\n";
356
357 exit 0;
358}
e9ec5455 359
360test_dirs();
823981ba 361backup_files();
11b9a41c 362copy_setup_files();
1a1f62a4 363runkeygen();
11b9a41c 364fixpaths();
ad71c979 365
472ec086 366my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
4f276ad7 367
53a54c67 368$metadata->finish();
9ef2f439 369
8570e631 370print "\n";
9ef2f439 371print "$myname: Finished configuring package 'gsi_openssh'.\n";
b0441584 372print "\n";
373print "I see that you have your GLOBUS_LOCATION environmental variable\n";
374print "set to:\n";
375print " $gpath\n";
376print "\n";
377print "Remember to keep this variable set (correctly) when you want\n";
378print "to use the executables that came with this package.\n";
7d7b4cd9 379print "---------------------------------------------------------------\n";
ac083f7a 380
381#
382# Just need a minimal action() subroutine for now..
383#
384
385sub action
386{
387 my ($command) = @_;
388
389 printf "$command\n";
390
391 my $result = system("$command 2>&1");
392
393 if (($result or $?) and $command !~ m!patch!)
394 {
395 die "ERROR: Unable to execute command: $!\n";
396 }
397}
398
399sub query_boolean
400{
401 my ($query_text, $default) = @_;
402 my $nondefault, $foo, $bar;
403
404 #
405 # Set $nondefault to the boolean opposite of $default.
406 #
407
408 if ($default eq "n")
409 {
410 $nondefault = "y";
411 }
412 else
413 {
414 $nondefault = "n";
415 }
416
417 print "${query_text} ";
418 print "[$default] ";
419
e9ec5455 420 $foo = <STDIN>;
421 ($bar) = split //, $foo;
422
e9d69a89 423 if ( grep(/\s/, $bar) )
ac083f7a 424 {
e9d69a89 425 # this is debatable. all whitespace means 'default'
426
427 $bar = $default;
428 }
429 elsif ($bar ne $default)
430 {
431 # everything else means 'nondefault'.
432
433 $bar = $nondefault;
434 }
435 else
436 {
437 # extraneous step. to get here, $bar should be eq to $default anyway.
438
e9ec5455 439 $bar = $default;
ac083f7a 440 }
441
e9ec5455 442 return $bar;
ac083f7a 443}
This page took 0.869049 seconds and 5 git commands to generate.