summaryrefslogtreecommitdiffstats
path: root/sshd_wizard/Sshd.pm
blob: 97c181fdc597c28d7648f16243a86e756c85807e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/perl

# Drakwizard
# Copyright (C) 2002,2005 Mandriva
#
# Authors: antoine Ginies <aginies@mandriva>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

package MDK::Wizard::Sshd;
use strict;

use common;
use services;
use MDK::Wizard::Wizcommon;
use Libconf::Templates;
use Libconf::Glueconf::Ssh::Sshd_config;


my $wiz = new MDK::Wizard::Wizcommon;
my $conf = "/etc/ssh/sshd_config";
my $DOMAINNAME = chomp_(`dnsdomainname`);
my $SHORTHOSTNAME = chomp_(`hostname -s`);

# we ask glueconf to give us the structure representing /etc/ssh/sshd_config
my $sshd = new Libconf::Glueconf::Ssh::Sshd_config({ filename => '/etc/ssh/sshd_config' });

# ------ debug
use Data::Dumper;
print Dumper($sshd);

my $o = {
         name => 'SSH wizard',
         var => {
		 wiz_port => '',
		},
#	 init => sub {
#	   test_host_domain($SHORTHOSTNAME, $DOMAINNAME);
#         },
         needed_rpm => [ 'openssh-server' ],
         defaultimage => "/usr/share/wizards/sshd_wizard/images/sshd.png"
	};

my @yesorno = qw(yes no); push @yesorno, "";
my @yesnoother = qw(yes no without-password forced-commands-only); push @yesnoother, "";
my @syslog = qw(DAEMON USER AUTH LOCAL0 LOCAL1 LOCAL2 LOCAL3 LOCAL4 LOCAL5 LOCAL6 LOCAL7); push @syslog, "";
my @loglevel = qw(QUIET FATAL ERROR INFO VERBOSE DEBUG DEBUG1 DEBUG2 DEBUG3); push @loglevel, "";

my %type = (
            1 => N('Newbie - classical options'),
	    2 => N('Expert - advanced ssh options'),
	   );

$o->{pages} = {
               welcome => {
			   name => N('OpenSSH SSH daemon configuration'),
                           data => [
                                    { label => N("Which type of configuration do you you want to do:"), type => 'list', val => \$o->{var}{wiz_type}, list => [ keys %type ], format => sub { $type{$_[0]} } },
				   ],
			   next => 'config_step1',
			  },
	       config_step1 => {
				name => N('SSH server, classical options'),
				pre => sub {
				  $sshd->{ListenAddress} and $o->{var}{ListenAddress} = $sshd->{ListenAddress} or $o->{var}{ListenAddress} = "0.0.0.0";
				  $sshd->{Port} and $o->{var}{Port} = $sshd->{Port} or $o->{var}{Port} = "22";
				  $o->{var}{PermitRootLogin} ||= $sshd->{PermitRootLogin};
				},
				data => [
					 { label => N('Permit root login:'), val => \$o->{var}{PermitRootLogin}, fixed_list => \@yesnoother },
					 { label => N('Listen address:'), val => \$o->{var}{ListenAddress}, help => N('Specifies the local addresses sshd should listen on.') },
					 { label => N('Port number:'), val => \$o->{var}{Port}, help => N('Specifies the port number that sshd listens on. The default is 22.') },
					],
				complete => sub {
				  if ($o->{var}{Port} !~ /^\d+$/) { $::in->ask_warn(N('Error'), N('Port should be a number')); return 1; }
				},
				post => sub {
				  if ($o->{var}{wiz_type} == 2) {
				    return 'auth_options' }
				  elsif ($o->{var}{wiz_type} == 1) {
				    return 'summary' }
				},
				next => 'auth_options',
			       },
	       auth_options => {
				name => N('Authentication Method'),
				pre => sub {
				  $o->{var}{RSAAuthentication} ||= $sshd->{RSAAuthentication};
				  $o->{var}{PubkeyAuthentication} ||= $sshd->{PubkeyAuthentication};
				  $sshd->{AuthorizedKeysFile} and $o->{var}{AuthorizedKeysFile} = $sshd->{AuthorizedKeysFile} or $o->{var}{AuthorizedKeysFile} = ".ssh/authorized_keys2";
				  $o->{var}{PasswordAuthentication} ||= $sshd->{PasswordAuthentication};
				  $o->{var}{IgnoreRhosts} ||= $sshd->{IgnoreRhosts};
				  $o->{var}{PermitEmptyPasswords} ||= $sshd->{PermitEmptyPasswords};
				},
				data => [
					 { label => N('RSA auth:'), val => \$o->{var}{RSAAuthentication}, fixed_list => \@yesorno },
					 { label => N('PubKey auth:'), val => \$o->{var}{PubkeyAuthentication}, fixed_list => \@yesorno },
					 { label => N('Auth key file:'), val => \$o->{var}{AuthorizedKeysFile} },
					 { label => N('Password auth:'), val => \$o->{var}{PasswordAuthentication}, fixed_list => \@yesorno },
					 { label => N('Ignore rhosts file:'), val => \$o->{var}{IgnoreRhosts}, fixed_list => \@yesorno },
					 { label => N('Permit empty password:'), val => \$o->{var}{PermitEmptyPasswords}, fixed_list => \@yesorno },
					],
				next => 'log_options',
			       },
	       log_options => {
			       name => N('Log') . "\n\n" . N('Syslog facility: gives the facility code that is used when logging messages from sshd') . "\n" . N('Log level: gives the verbosity level that is used when logging messages from sshd.'),
			       pre => sub {
				 $o->{var}{SyslogFacility} ||= $sshd->{SyslogFacility};
				 $o->{var}{LogLevel} ||= $sshd->{LogLevel};
			       },
			       data => [
					{ label => N('Syslog facility:'), val => \$o->{var}{SyslogFacility}, fixed_list => \@syslog },
					{ label => N('Log level:'), val => \$o->{var}{LogLevel}, fixed_list => \@loglevel },
					],
			       next => 'login_options',
			      },
	       login_options => {
				 name => N('Login options') . "\n\n" . N('Print last log: whether sshd should print the date and time when the user last logged in'),
				 pre => sub {
				   $o->{var}{LoginGraceTime} ||= $sshd->{LoginGraceTime};
				   $o->{var}{KeepAlive} ||= $sshd->{KeepAlive};
				   $o->{var}{PrintMotd} ||= $sshd->{PrintMotd};
				   $o->{var}{PrintLastLog} ||= $sshd->{PrintLastLog};
				 },
				 data => [
					 { label => N('Login Grace time:'), val => \$o->{var}{LoginGraceTime}, help => N('The server disconnects after this time if the user has not successfully logged in. If the value is 0, there is no time limit. The default is 120 seconds.') },
					 { label => N('Keep alive:'), val => \$o->{var}{KeepAlive}, fixed_list => \@yesorno },
					 { label => N('Print motd:'), val => \$o->{var}{PrintMotd}, fixed_list => \@yesorno },
					 { label => N('Print last log:'), val => \$o->{var}{PrintLastLog}, fixed_list => \@yesorno },
					 ],
				 complete => sub {
				   if ($o->{var}{LoginGraceTime} !~ /^\d+$/ and $o->{var}{LoginGraceTime}) { $::in->ask_warn(N('Error'), N('Login grace time should be a number')); return 1; }
				 },
				 next => 'user_login',
				},
	       user_login => {
			      name => N('User Login options') . "\n\n" . N('Strict modes: specifies whether sshd should check file modes and ownership of the user\'s files and home directory before accepting login. This is normally desirable because novices sometimes accidentally leave their directory or files world-writable'),
			      pre => sub {
				$o->{var}{StrictModes} ||= $sshd->{StrictModes};
				$o->{var}{AllowUsers} ||= $sshd->{AllowUsers};
				$o->{var}{DenyUsers} ||= $sshd->{DenyUsers};
			      },
			      data => [

				       { label => N('Strict modes:'), val => \$o->{var}{StrictModes}, fixed_list => \@yesorno },
				       { label => N('Allow users:'), val => \$o->{var}{AllowUsers}, help => N('If specified, login is allowed only for user names that match one of the patterns. ie: erwan aginies guibo') },
				       { label => N('Deny users:'), val => \$o->{var}{DenyUsers}, help => N('Login is disallowed for user names that match one of the patterns. ie: pirate guillomovitch') },
				      ],
			      next => 'other',
			     },
	       other => {
			 name => N('Compression: Specifies whether compression is allowed.') . "\n" . N('X11 forwarding: specifies whether X11 forwarding is permitted. Note that disabling X11 forwarding does not prevent users from forwarding X11 traffic, as users can always install their own forwarders.'),
			 pre => sub {
			   $o->{var}{Compression} ||= $sshd->{Compression};
			   $o->{var}{X11Forwarding} ||= $sshd->{X11Forwarding};
			 },
			 data => [
				  { label => N('Compression:'), val => \$o->{var}{Compression}, fixed_list => \@yesorno },
				  { label => N('X11 forwarding:'), val => \$o->{var}{X11Forwarding}, fixed_list => \@yesorno },
				 ],
			 next => 'summary',
			},
	       summary => {
			   name => N('Summary of OpenSSH configuration.'),
			   data => [
				    { label => N('Permit root login:'), fixed_val => \$o->{var}{PermitRootLogin} },
				    { label => N('Listen address:'), fixed_val => \$o->{var}{ListenAddress} },
				    { label => N('Port number:'), fixed_val => \$o->{var}{Port} },
				   ],
			   post => \&do_it,
			   next => 'end',
			  },
	       end => {
                       name => N('Congratulations') . "\n\n" . N('The wizard successfully configured your SSH server.'),
                       no_back => 1,
                       end => 1,
                       next => 0
                      },
               error_end => {
                             name => N("Failed"),
                             data => [ { label => N("Please relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
			     next => 0,
                            },
	       	      };

sub new {
  my ($class, $conf) = @_;
  bless {
         o   => $o,
        }, $class;
}

sub write_conf_restart_ssh {
  $sshd->write_conf($conf);
  if (services::is_service_running('sshd')) {
    services::restart('sshd')
    } else { services::start('sshd') }
}

sub global_config {
  $sshd->{Port} = $o->{var}{Port};
  # force sshd protocol 2
  $sshd->{Protocol} and $o->{var}{Protocol} = $sshd->{Protocol} or $o->{var}{Protocol} = "2";
  $o->{var}{ListenAddress} and $sshd->{ListenAddress} = $o->{var}{ListenAddress};

  $o->{var}{SyslogFacility} and $sshd->{SyslogFacility} = $o->{var}{SyslogFacility};
  $o->{var}{LogLevel} and $sshd->{LogLevel} = $o->{var}{LogLevel};

  $o->{var}{PermitRootLogin} and $sshd->{PermitRootLogin} = $o->{var}{PermitRootLogin};
  $o->{var}{StrictModes} and $sshd->{StrictModes} = $o->{var}{StrictModes};
  $o->{var}{AllowUsers} and $sshd->{AllowUsers} = $o->{var}{AllowUsers};
  $o->{var}{DenyUsers} and $sshd->{DenyUsers} = $o->{var}{DenyUsers};

  $o->{var}{LoginGraceTime} and $sshd->{LoginGraceTime} = $o->{var}{LoginGraceTime};
  $o->{var}{KeepAlive} and $sshd->{KeepAlive} = $o->{var}{KeepAlive};
  $o->{var}{PrintMotd} and $sshd->{PrintMotd} = $o->{var}{PrintMotd};
  $o->{var}{PrintLastLog} and $sshd->{PrintLastLog} = $o->{var}{PrintLastLog};

  $o->{var}{RSAAuthentication} and $sshd->{RSAAuthentication} = $o->{var}{RSAAuthentication};
  $o->{var}{PubkeyAuthentication} and $sshd->{PubkeyAuthentication} = $o->{var}{PubkeyAuthentication};
  $o->{var}{AuthorizedKeysFile} and $sshd->{AuthorizedKeysFile} = $o->{var}{AuthorizedKeysFile};
  $o->{var}{PasswordAuthentication} and $sshd->{PasswordAuthentication} = $o->{var}{PasswordAuthentication};
  $o->{var}{IgnoreRhosts} and $sshd->{IgnoreRhosts} = $o->{var}{IgnoreRhosts};
  $o->{var}{PermitEmptyPasswords} and $sshd->{PermitEmptyPasswords} = $o->{var}{PermitEmptyPasswords};
  $o->{var}{X11Forwarding} and $sshd->{X11Forwarding} = $o->{var}{X11Forwarding};
  $o->{var}{Compression} and $sshd->{Compression} = $o->{var}{Compression};
#$sshd->{UsePrivilegeSeparation} yes
}

sub do_it {
  $::testing and return;
  # display a wait dialog box
  my $in = 'interactive'->vnew('su', 'SSH');
  my $w = $in->wait_message(N("OpenSSH server"), N("Configuring your OpenSSH server..."));
  global_config();
  write_conf_restart_ssh();
  # remove wait message
  undef $w;
  check_started('sshd');
}

1;