summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/draksec
blob: 34a8b2f3447f347147765d3449d5b3ce8e14ed8d (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/perl
#*****************************************************************************
#
# Copyright (c) 2002-2006 Christian Belisle
#                         Thierry Vignaud <tvignaud@mandrakesoft.com>
#
# This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2, as
#  published by the Free Software Foundation.
#
#  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.
#
#*****************************************************************************

use strict;
use lib qw(/usr/lib/libDrakX);
use common;
use standalone;
use vars qw($MODE %options);
use interactive;
use mygtk2 qw(gtknew gtkset);
use ugtk2 qw(:helpers :wrappers :ask :create);
use run_program;
use security::level;
use security::msec;
use security::help;
use security::l10n;

#$MODE = 'basic';
#$0 =~ /draksec-firewall$/ and $MODE = 'firewall';
#$0 =~ /draksec-perms$/ and $MODE = 'perms';

#/^-?-(\S+)$/ and $options{$1} = 1 foreach @ARGV;


my $in = 'interactive'->vnew('su');

$ugtk2::wm_icon = "/usr/share/mcc/themes/default/draksec-mdk.png";
my ($w, %fields);

############################    I18N     ###################################

my @help;

my %translations = (
	'ALL'     => N("ALL"),
	'LOCAL'   => N("LOCAL"),
	'NONE'    => N("NONE"),
	'default' => N("Default"),
	'ignore'  => N("Ignore"),
 	'no'      => N("No"),
	'yes'     => N("Yes"),
);
my %inv_translations = reverse %translations;

sub to_i18n { map { $translations{$_} || $_ } @_ }
sub from_i18n { $inv_translations{$_[0]} || $_[0] }
sub resize { gtkset($_[0], width => 50) }

%fields = security::l10n::fields();
my %inv_fields = reverse %fields;

# factorize this with rpmdrake and harddrake2
sub wait_msg {
    my $mainw = ugtk2->new(N("Please wait"), (modal => 1, transient => $w->{real_window}));
    $mainw->{window}->add(gtknew('WrappedLabel', text => $_[0]));
    $mainw->{rwindow}->show_all;
    gtkset_mousecursor_wait($mainw->{rwindow}->window);
    # ugly workaround for gtk+ not displaying subdialog contents:
    Glib::Timeout->add(300, sub { Gtk2->main_quit; 0 });
    Gtk2->main;
    $mainw;
}

sub remove_wait_msg { $_[0]->destroy }

sub basic_seclevel_explanations() {
    my $text = gtknew('TextView');
    use Gtk2::Pango;
    my %common_opts = ('left-margin' => '10', 'right-margin' => '10');

    gtktext_insert($text, ugtk2::markup_to_TextView_format(formatAlaTeX(
#-PO: Do not alter the <span ..> and </span> tags.
#-PO: Translate the security levels (Poor, Standard, High, Higher and Paranoid) in the same way, you translated these individuals words.
#-PO: keep the double empty lines between sections, this is formatted a la LaTeX.
					  N("Here, you can setup the security level and administrator of your machine.


The '<span weight=\"bold\">Security Administrator</span>' is the one who will receive security alerts if the
'<span weight=\"bold\">Security Alerts</span>' option is set. It can be a username or an email.


The '<span weight=\"bold\">Security Level</span>' menu allows you to select one of the six preconfigured security levels
provided with msec. These levels range from '<span weight=\"bold\">poor</span>' security and ease of use, to
'<span weight=\"bold\">paranoid</span>' config, suitable for very sensitive server applications:


<span foreground=\"royalblue3\">Poor</span>: This is a totally unsafe but very
easy to use security level. It should only be used for machines not connected to
any network and that are not accessible to everybody.


<span foreground=\"royalblue3\">Standard</span>: This is the standard security
recommended for a computer that will be used to connect to the Internet as a
client.


<span foreground=\"royalblue3\">High</span>: There are already some
restrictions, and more automatic checks are run every night.


<span foreground=\"royalblue3\">Higher</span>: The security is now high enough
to use the system as a server which can accept connections from many clients. If
your machine is only a client on the Internet, you should choose a lower level.


<span foreground=\"royalblue3\">Paranoid</span>: This is similar to the previous
level, but the system is entirely closed and security features are at their
maximum")), \%common_opts));

    gtknew('ScrolledWindow', h_policy => 'never', child => $text);
}

sub new_nonedit_combo {
    my ($string_list, $o_default_value) = @_;
    gtknew('ComboBox', text => to_i18n($o_default_value), list => [ to_i18n(@$string_list) ]);
}

sub register_help_page {
     my ($domain) = @_;
     push @help,
       ([ 
         [ $domain . "\n\n", 
           {
            justification => 'center',
            scale => Gtk2::Pango->PANGO_SCALE_LARGE,
            weight => Gtk2::Pango->PANGO_WEIGHT_BOLD, 
           },
         ],
         [ N("Description of the fields:\n\n"), ],
        ],
       );
}


sub register_help_entry {
     my ($label, $default, $opt) = @_;
     my $help = $security::help::help{$opt};
     push @{$help[-1]},
       ([
         formatAlaTeX($label) . ":\n",
         {  foreground => "royalblue3" },
        ],
        [ join("\n", formatAlaTeX($help), N("(default value: %s)", map { s/ /, /g; $_ } to_i18n($default)), "\n") ]
       );
}

my $msec = new security::msec;
$w = ugtk2->new(N("Security Level and Checks"));
my $window = $w->{window};


############################ MAIN WINDOW ###################################
# Set different options to Gtk2::Window
unless ($::isEmbedded) {
    $w->{rwindow}->set_position('center');
    $w->{rwindow}->set_title("DrakSec");
    $window->set_size_request(598, 520);
}

# Connect the signals
$window->signal_connect('delete_event', sub { $window->destroy });
$window->signal_connect('destroy', sub { ugtk2->exit });

$window->add(my $vbox = gtkshow(gtknew('VBox')));

# Create the notebook (for bookmarks at the top)
my $notebook = create_notebook();

my $common_opts = { col_spacings => 10, row_spacings => 5, mcc => 1 };

######################## BASIC OPTIONS PAGE ################################
my ($seclevel_entry, $secadmin_entry);

$notebook->append_page(gtkshow(gtknew('VBox', children => [
                                       1, basic_seclevel_explanations(),
                                       0, create_packtable($common_opts,
                                                        [ 
                                                          do {
                                                              my @sec_levels = security::level::get_common_list();
                                                              my $current_level = security::level::get_string();
                                                              
                                                              push(@sec_levels, $current_level) unless member($current_level, @sec_levels);
                                                              $seclevel_entry = new_nonedit_combo(\@sec_levels, $current_level);
                                                              
                                                              gtknew('Label_Left', text => N("Security Level:")), $seclevel_entry;
                                                          }
                                                          ],
                                                        [ gtknew('Label_Left', text => N("Security Alerts:")), 
                                                          my $secadmin_check = gtksignal_connect(gtknew('CheckButton'), toggled => sub {
                                                                                                     $secadmin_entry->set_sensitive($_[0]->get_active);
                                                                                                 }) ],
                                                        [ gtknew('Label_Left', text => N("Security Administrator:")),
                                                          $secadmin_entry = Gtk2::Entry->new_with_text($msec->get_check_value("MAIL_USER")) ]) ])),
                               gtknew('Label', text => N("Basic options")));
                       
if ($msec->get_check_value("MAIL_WARN") eq "yes") {
    $secadmin_check->set_active(1);
} else {
    $secadmin_entry->set_sensitive(0);
 }
                       
######################### NETWORK & SYSTEM OPTIONS #########################
my @yesno_choices    = qw(yes no default ignore);
my @alllocal_choices = qw(ALL LOCAL NONE default);
my @all_choices = (@yesno_choices, @alllocal_choices);
my %options_values;

foreach ([ 'network', N("Network Options") ], [ 'system', N("System Options") ]) {
    my ($domain, $label) = @$_;
    register_help_page($label);
    my %values;
    gtkappend_page($notebook, gtkshow(gtknew('VBox', children => [
                                            1, create_scrolled_window(create_packtable($common_opts,
                                                                                       map {
                                                                                           my $i = $_;
                                                                                           
                                                                                           my $entry;
                                                                                           my $opt = $inv_fields{$i} || $i;
                                                                                           my $default = $msec->get_function_default($opt);
                                                                                           if (member($default, @all_choices)) {
                                                                                               $values{$i} = new_nonedit_combo(member($default, @yesno_choices) ? \@yesno_choices : if_(member($default, @alllocal_choices), \@alllocal_choices));
                                                                                               $entry = $values{$i}->entry;
                                                                                           } else {
                                                                                               $values{$i} = gtknew('Entry');
                                                                                               $entry = $values{$i};
                                                                                           }
                                                                                           $entry->set_text(to_i18n($msec->get_function_value($opt)));
                                                                                           register_help_entry($i, $default, $opt);
                                                                                           [ gtknew('Label_Left', line_wrap => 1, text => $i), resize($values{$i}) ];
                                                                                       } sort map { $fields{$_} || $_ } $msec->list_functions($domain),
                                                                                      ),
                                                                      [ 'never', 'automatic' ],
                                                                     ),
                                           ])
                                  ),
                           gtknew('Label_Left', text => $label));
    $options_values{$domain} = \%values;
}

######################## PERIODIC CHECKS ###################################
my %security_checks_value;

my $check_string = N("Periodic Checks");

register_help_page($check_string);
gtkappend_page($notebook, gtkshow(gtknew('VBox', children => [
                                        1, create_scrolled_window(create_packtable($common_opts,
                                                                                   map {
                                                                                       my $i = $_;
                                                                                       my $opt = $inv_fields{$i} || $i;
                                                                                       $security_checks_value{$i} = new_nonedit_combo([ 'yes', 'no', 'default' ], $msec->get_check_value($opt));
                                                                                       register_help_entry($i, $msec->get_check_default($opt), $opt);
                                                                                       [ gtkshow(gtknew('Label_Left', line_wrap => 1, text => $i)), resize($security_checks_value{$i}) ];
                                                                                   } sort map { $fields{$_} || $_ } $msec->list_checks)) ])),
                       gtknew('Label', text => $check_string));


######################## AUTH ###################################
my %progs;

my $auth_string = N("Authentication");

my %auth = (
      no_passwd => N("No password"),
      root_passwd => N("Root password"),
      user_passwd => N("User password"),
);

sub default_auth_value {
    my ($prog) = @_;
    my $link = readlink("/etc/pam.d/$prog");
    if ($link =~ /mandriva-console-auth/) {
        return $auth{no_passwd};
    } elsif ($link =~ /mandriva-simple-auth/) {
        my ($user) = cat_("/etc/security/console.apps/$prog") =~ /USER=(.*)/;
        return $auth{root_passwd} if $user eq 'root';
        return $auth{user_passwd} if $user eq '<user>';
    }
}

sub set_auth_value {
    my ($prog, $auth) = @_;
    if ($auth eq 'no_passwd') {
        symlinkf('../../etc/pam.d/mandriva-console-auth', "/etc/pam.d/$prog");
    } else {
        symlink('../../etc/pam.d/mandriva-simple-auth', "/etc/pam.d/$prog");
        my $value = $auth eq 'user_passwd' ? '<user>' : 'root';
        substInFile {
            s/^USER=.*/USER=$value/;
        } "/etc/security/console.apps/$prog";
    }
}

my %descr = (
    
    rpmdrake => N("Software Management"),
    mandrivaupdate => N("Mandriva Update"),
    'drakrpm-edit-media' => N("Software Media Manager"),
    drak3d => N("Configure 3D Desktop effects"),
    xfdrake => N("Graphical Server Configuration"),
    drakmouse => N("Mouse Configuration"),
    drakkeyboard => N("Keyboard Configuration"),
    drakups => N("UPS Configuration"),
    drakconnect => N("Network Configuration"),
    drakhosts => N("Hosts definitions"),
    draknetcenter => N("Network Center"),
    drakvpn => N("VPN"),
    drakproxy => N("Proxy Configuration"),
    drakgw => N("Connection Sharing"),
    drakauth => N("Authentication"),
    drakbackup => N("Backups"),
    drakfont => N("Import fonts"),
    draklog => N("Logs"),
    drakxservices => N("Services"),
    userdrake => N("Users"),
    drakclock => N("Date, Clock & Time Zone Settings"),
    drakboot => N("Boot Configuratoin"),
);

gtkappend_page(
    $notebook,
    gtkshow(create_scrolled_window(
        gtknew('VBox', children => [
            map {
                my ($title, $progs) = @$_;
                (0, gtknew('Title2', label => $title),
                 1, create_packtable(
                     $common_opts,
                     map {
                         [
                             gtkshow(gtknew('Label_Left', line_wrap => 1, text => $descr{$_} || $_)),
                                 $progs{$_} = new_nonedit_combo([
                                     @auth{qw(user_passwd root_passwd no_passwd)}
                                 ],
                                                                default_auth_value($_)
                                                                #$msec->get_check_value($opt)
                                                            )
                         ];
                     } split(' ', $progs)
                 )
                );
            } (
                [ N("Software Management"), 'rpmdrake mandrivaupdate drakrpm-edit-media' ],
                [ N("Hardware"), 'drak3d xfdrake drakmouse drakkeyboard drakups' ],
                [ N("Network"), 'drakconnect drakhosts draknetcenter drakvpn drakproxy drakgw' ],
                [ N("System"), 'drakauth drakbackup drakfont draklog drakxservices userdrake drakclock' ],
                [ N("Boot"), 'drakboot' ],
            )
        ]))),
    gtknew('Label', text => $auth_string));





####################### OK CANCEL BUTTONS ##################################
gtkpack_($vbox,
         1, gtkshow($notebook),
         0, create_okcancel(my $oc =
                            {
                             cancel_clicked => sub { ugtk2->exit(0) },
                             ok_clicked => sub {
                                my $seclevel_value = $seclevel_entry->entry->get_text;
                                my $secadmin_check_value = $secadmin_check->get_active;
                                my $secadmin_value = $secadmin_entry->get_text;
                                my $w;

                                log::explanations("Configuring msec");

                                if ($seclevel_value ne security::level::get_string()) {
                                    $w = wait_msg(N("Please wait, setting security level..."));
                                    log::explanations(qq(Setting security level to "$seclevel_value"));
                                    security::level::set(security::level::from_string($seclevel_value));
                                    remove_wait_msg($w);
                                }

                                $w = wait_msg(N("Please wait, setting security options..."));
                                log::explanations(qq(Setting security administrator option to ") . bool2yesno($secadmin_check_value) . '"');
                                $msec->set_check('MAIL_WARN', bool2yesno($secadmin_check_value));

                                if ($secadmin_value ne $msec->get_check_value('MAIL_USER') && $secadmin_check_value) {
                                    log::explanations(qq(Setting security administrator contact to "$secadmin_value"));
                                    $msec->set_check('MAIL_USER', $secadmin_value);
                                  }

                                log::explanations("Setting security periodic checks");
                                foreach my $key (keys %security_checks_value) {
                                    $msec->set_check($inv_fields{$key} || $key, from_i18n($security_checks_value{$key}->entry->get_text));
                                }
                                $msec->apply_checks;

                                foreach my $domain (keys %options_values) {
                                    log::explanations("Setting msec functions related to $domain");
                                    foreach my $key (keys %{$options_values{$domain}}) {
                                        my $opt = $options_values{$domain}{$key};
                                        $msec->set_function($inv_fields{$key} || $key, from_i18n($opt->get_text));
                                    }
                                }
                                $msec->apply_functions;
                                log::explanations("Applying msec changes");
                                run_program::run("/usr/sbin/msec");

                                log::explanations("Setting up right delegation");
                                my %rev_auth = reverse %auth;
                                foreach my $key (keys %progs) {
                                    my $value = $progs{$key}->get_text;
                                    set_auth_value($key, $rev_auth{$value});
                                }

                                remove_wait_msg($w);

                                if ($secadmin_value ne $msec->get_check_value('CHKROOTKIT_CHECK')) {
                                    $in->do_pkgs->ensure_is_installed_if_available('chkrootkit', '/usr/sbin/chkrootkit');
                                }

                                ugtk2->exit(0);
                            }
                            },
                            undef, undef, '',
                            [ N("Help"), sub {
                                  my $text = gtknew('TextView');
                                  create_dialog(N("Help"), 
                                                gtktext_insert($text,
                                                               [
                                                                # -1 b/c of main page:
                                                                @{$help[$notebook->get_current_page-1]}
                                                               ]
                                                              ),
                                                { use_markup => 1,
                                                  transient => $w->{real_window},
                                                  height => 400,
                                                  width => 600,
                                                  scroll => 1,
                                                },
                                               );
                              } ],
                           ),
        );

$notebook->set_scrollable(1);
$notebook->signal_connect("switch-page" => sub { $oc->{buttons}{N("Help")}->set_sensitive(!member($_[2], qw(0 4))) });

$oc->{buttons}{N("Help")}->set_sensitive(0);
$oc->{cancel}->can_default(1);
$oc->{cancel}->grab_default;

$in->do_pkgs->ensure_is_installed('msec', '/usr/sbin/msec');

$w->main;
ugtk2->exit(0);