]> andersk Git - gssapi-openssh.git/blob - setup/setup-openssh.pl
o Add in backups for config files on setup.
[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 # Large 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 $gpath = $ENV{GLOBUS_LOCATION};
14 if (!defined($gpath))
15 {
16     die "GLOBUS_LOCATION needs to be set before running this script"
17 }
18
19 #
20 # i'm including this because other perl scripts in the gpt setup directories
21 # do so
22 #
23
24 @INC = (@INC, "$gpath/lib/perl");
25
26 require Grid::GPT::Setup;
27
28 my $globusdir = $gpath;
29 my $setupdir = "$globusdir/setup/globus";
30 my $myname = "setup-openssh.pl";
31
32 print "$myname: Configuring package 'gsi_openssh'...\n";
33 print "Run this as root for the intended effect...\n";
34
35 #
36 # Set up path prefixes for use in the path translations
37 #
38
39 $prefix = ${globusdir};
40 $exec_prefix = "${prefix}";
41 $bindir = "${exec_prefix}/bin";
42 $sbindir = "${exec_prefix}/sbin";
43 $mandir = "${prefix}/man";
44 $mansubdir = "man";
45 $libexecdir = "${exec_prefix}/libexec";
46 $sysconfdir = "/etc/ssh";
47 $piddir = "/var/run";
48 $xauth_path = "/usr/bin/X11/xauth";
49
50 #
51 # We need to make sure it's okay to copy our setup files (if some files are already
52 # present).  If we do copy any files, we backup the old files so the user can (possibly)
53 # reverse any damage.
54 #
55
56 sub copy_setup_files
57 {
58     my $response, $curr_time;
59
60     $curr_time = time();
61
62     $response = "y";
63     if ( -e "${sysconfdir}/ssh_config" )
64     {
65         $response = query_boolean("${sysconfdir}/ssh_config already exists.  Overwrite? ", "n");
66     }
67
68     if ($response eq "y")
69     {
70         action("cp ${sysconfdir}/ssh_config ${sysconfdir}/ssh_config.bak_${curr_time}");
71         action("cp ${globusdir}/setup/globus/ssh_config ${sysconfdir}/ssh_config");
72     }
73
74     #
75     # Reset response for our new query
76     #
77
78     $response = "y";
79     if ( -e "${sysconfdir}/sshd_config" )
80     {
81         $response = query_boolean("${sysconfdir}/sshd_config already exists.  Overwrite? ", "n");
82     }
83
84     if ($response eq "y")
85     {
86         action("cp ${sysconfdir}/sshd_config ${sysconfdir}/sshd_config.bak_${curr_time}");
87         action("cp ${globusdir}/setup/globus/sshd_config ${sysconfdir}/sshd_config");
88     }
89
90     #
91     # Reset response for our new query
92     #
93
94     $response = "y";
95     if ( -e "${sysconfdir}/moduli" )
96     {
97         $response = query_boolean("${sysconfdir}/moduli already exists.  Overwrite? ", "n");
98     }
99
100     if ($response eq "y")
101     {
102         action("cp ${sysconfdir}/moduli ${sysconfdir}/moduli.bak_${curr_time}");
103         action("cp ${globusdir}/setup/globus/moduli ${sysconfdir}/moduli");
104     }
105 }
106
107 sub runkeygen
108 {
109     if ( ! -d "${sysconfdir}" )
110     {
111         print "Could not find ${sysconfdir} directory... creating\n";
112         mkdir($sysconfdir, 16877);
113         # 16877 should be 755, or drwxr-xr-x
114     }
115
116     print "Generating ssh keys (if necessary)...\n";
117     if ( -e "${sysconfdir}/ssh_host_key" )
118     {
119         print "${sysconfdir}/ssh_host_key already exists, skipping.\n";
120     }
121     else
122     {
123         # if $sysconfdir/ssh_host_key doesn't exist..
124         action("$bindir/ssh-keygen -t rsa1 -f $sysconfdir/ssh_host_key -N \"\"");
125     }
126
127     if ( -e "${sysconfdir}/ssh_host_dsa_key" )
128     {
129         print "${sysconfdir}/ssh_host_dsa_key already exists, skipping.\n";
130     }
131     else
132     {
133         # if $sysconfdir/ssh_host_dsa_key doesn't exist..
134         action("$bindir/ssh-keygen -t dsa -f $sysconfdir/ssh_host_dsa_key -N \"\"");
135     }
136
137     if ( -e "${sysconfdir}/ssh_host_rsa_key" )
138     {
139         print "${sysconfdir}/ssh_host_rsa_key already exists, skipping.\n";
140     }
141     else
142     {
143         # if $sysconfdir/ssh_host_rsa_key doesn't exist..
144         action("$bindir/ssh-keygen -t rsa -f $sysconfdir/ssh_host_rsa_key -N \"\"");
145     }
146
147     return 0;
148 }
149
150 sub fixpaths
151 {
152     #
153     # Set up path translations for the installation files
154     #
155
156     %def = (
157         "/etc/ssh_config" => "${sysconfdir}/ssh_config",
158         "/etc/ssh_known_hosts" => "${sysconfdir}/ssh_known_hosts",
159         "/etc/sshd_config" => "${sysconfdir}/sshd_config",
160         "/usr/libexec" => "${libexecdir}",
161         "/etc/shosts.equiv" => "${sysconfdir}/shosts.equiv",
162         "/etc/ssh_host_key" => "${sysconfdir}/ssh_host_key",
163         "/etc/ssh_host_dsa_key" => "${sysconfdir}/ssh_host_dsa_key",
164         "/etc/ssh_host_rsa_key" => "${sysconfdir}/ssh_host_rsa_key",
165         "/var/run/sshd.pid" => "${piddir}/sshd.pid",
166         "/etc/moduli" => "${sysconfdir}/moduli",
167         "/etc/sshrc" => "${sysconfdir}/sshrc",
168         "/usr/X11R6/bin/xauth" => "${xauth_path}",
169         "/usr/bin:/bin:/usr/sbin:/sbin" => "/usr/bin:/bin:/usr/sbin:/sbin:${bindir}",
170         "(/path/to/scp.real)" => "${bindir}/scp.real",
171         "(/path/to/ssh)" => "${bindir}/ssh",
172         "(/path/to/sftp.real)" => "${bindir}/sftp.real",
173         "(/path/to/sshd.real)" => "${sbindir}/sshd.real",
174         "(/path/to/ssh_config)" => "${sysconfdir}/ssh_config",
175         "(/path/to/sshd_config)" => "${sysconfdir}/sshd_config",
176         );
177
178     #
179     # Files on which to perform path translations
180     #
181
182     %files = (
183         "${bindir}/scp" => 0,
184         "${bindir}/sftp" => 0,
185         "${sbindir}/sshd" => 0,
186         "${sysconfdir}/ssh_config" => 1,
187         "${sysconfdir}/sshd_config" => 1,
188         "${sysconfdir}/moduli" => 1,
189         "${mandir}/${mansubdir}1/scp.1" => 0,
190         "${mandir}/${mansubdir}1/ssh-add.1" => 0,
191         "${mandir}/${mansubdir}1/ssh-agent.1" => 0,
192         "${mandir}/${mansubdir}1/ssh-keygen.1" => 0,
193         "${mandir}/${mansubdir}1/ssh-keyscan.1" => 0,
194         "${mandir}/${mansubdir}1/ssh.1" => 0,
195         "${mandir}/${mansubdir}8/sshd.8" => 0,
196         "${mandir}/${mansubdir}8/sftp-server.8" => 0,
197         "${mandir}/${mansubdir}1/sftp.1" => 0,
198         );
199
200     print "Translating strings in config/man files...\n";
201     for my $f (keys %files)
202     {
203         $f =~ /(.*\/)*(.*)$/;
204
205         #
206         # we really should create a random filename and make sure that it
207         # doesn't already exist (based off current time_t or something)
208         #
209
210         $g = "$f.tmp";
211
212         #
213         # Grab the current mode/uid/gid for use later
214         #
215
216         $mode = (stat($f))[2];
217         $uid = (stat($f))[4];
218         $gid = (stat($f))[5];
219
220         action("mv $f $g");
221
222         open(IN, "<$g") || die ("$0: input file $g missing!\n");
223         open(OUT, ">$f") || die ("$0: unable to open output file $f!\n");
224
225         while (<IN>)
226         {
227             for $s (keys(%def))
228             {
229                 s#$s#$def{$s}#;
230             } # for $s
231             print OUT "$_";
232         } # while <IN>
233
234         close(OUT);
235         close(IN);
236
237         if ($file{$f} eq 0)
238         {
239             action("rm $g");
240         }
241         else
242         {
243             print "Left backup config file '$g'\n";
244         }
245
246         #
247         # An attempt to revert the new file back to the original file's
248         # mode/uid/gid
249         #
250
251         chmod($mode, $f);
252         chown($uid, $gid, $f);
253     } # for $f
254
255     return 0;
256 }
257
258 copy_setup_files();
259 runkeygen();
260 fixpaths();
261
262 my $metadata = new Grid::GPT::Setup(package_name => "gsi_openssh_setup");
263
264 $metadata->finish();
265
266 print "$myname: Finished configuring package 'gsi_openssh'.\n";
267
268 #
269 # Just need a minimal action() subroutine for now..
270 #
271
272 sub action
273 {
274     my ($command) = @_;
275
276     printf "$command\n";
277
278     my $result = system("$command 2>&1");
279
280     if (($result or $?) and $command !~ m!patch!)
281     {
282         die "ERROR: Unable to execute command: $!\n";
283     }
284 }
285
286 sub query_boolean
287 {
288     my ($query_text, $default) = @_;
289     my $nondefault, $foo, $bar;
290
291     #
292     # Set $nondefault to the boolean opposite of $default.
293     #
294
295     if ($default eq "n")
296     {
297         $nondefault = "y";
298     }
299     else
300     {
301         $nondefault = "n";
302     }
303
304     print "${query_text} ";
305     print "[$default] ";
306
307     $foo = getc(STDIN);
308     $bar = <STDIN>;
309
310     if ($foo ne $nondefault)
311     {
312         $foo = $default;
313     }
314
315     return $foo;
316 }
317
This page took 0.067826 seconds and 5 git commands to generate.