summaryrefslogtreecommitdiffstats
path: root/nfs_wizard/NFS.pm
blob: 6474e75cb389b197f7a81c421a8d3cd5b31dc5da (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
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002,2003 Mandrakesoft
#
# Authors: Arnaud Desmons <adesmons@mandrakesoft.com>
#          Florent Villard <warly@mandrakesoft.com>
#
# 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::NFS;
use strict;

use common;
use services;
use MDK::Wizard::Wizcommon;

my $wiz = new MDK::Wizard::Wizcommon;

my $o = { 
    name => N("NFS Wizard"),
	var => { 
	    wiz_nfs_dir => '',
	    wiz_nfs_level => '',
	    wiz_netmask => ''
	},
	needed_rpm => [ 'nfs-utils' ],
	defaultimage => "$ENV{__WIZ_HOME__}nfs_wizard/images/NFS.png"
    };

my %level = (
    1 => N("All - No access restriction"),
    2 => N("Local Network - access for local network (recommended)")
);
    
$o->{pages} = { 
    welcome => {
        name => N("NFS Server Configuration Wizard") . "\n\n" . N("This wizard will help you configuring the NFS server for your network."),
        no_back => 1,
        next => 'nfs'
    },
    nfs => {
        name => N("NFS server"),
        post => \&check,
        data => [
            { label => N("Directory:"), val => \$o->{var}{wiz_nfs_dir} },
            ],
        next => 'ask_level'
    },
    ask_level => {
      name => N("Access control") . "\n\n" . N("NFS can be restricted to a certain ip class") . "\n\n" . N("Choose the level that suits your needs. If you don't know, the local network level is usually the most appropriate. Beware that the all level may be not secure."),
      pre => sub {
	$o->{var}{wiz_netmask} = network_mask() if !$o->{var}{wiz_netmask} || $o->{var}{wiz_netmask} eq '0.0.0.0/0.0.0.0'
      },
      data => [ 
            { val => \$o->{var}{wiz_nfs_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
	    ],
      post => \&chooser,
      next => 'summary'
    },
    shownet => {
       name => N("Grant access on local network") . "\n\n" . N("Access will be allowed for hosts on the network. Here is the information found about your current local network, you can modify it if needed."),
      data => [ 
            { label => N("Authorized network:"), val => \$o->{var}{wiz_netmask} },
	    ],
      next => 'summary'

    },
    error_dir => {
      name => N("Error.") . "\n\n" . N("The path you entered does not exist."),
      ignore => 1,
      next => 'nfs'
    },
    summary => {
      name =>  N("The wizard collected the following parameters."),
      pre => sub {
	  $o->{var}{wiz_text_level} = $level{$o->{var}{wiz_nfs_level}};
	  $o->{var}{wiz_netmask} = $o->{var}{wiz_nfs_level} == 1 ? "0.0.0.0/0.0.0.0" : $o->{var}{wiz_netmask} 
      },
      data => [
            { label => N("Exported dir:"), fixed_val => \$o->{var}{wiz_nfs_dir} },
            { label => N("Access :"), fixed_val => \$o->{var}{wiz_text_level} },
            { label => N("Netmask :"), fixed_val => \$o->{var}{wiz_netmask} },
        ],
      post => \&do_it,
      next => 'end'
    },
	       end => { 
		       name => N("Congratulations") . "\n\n" . N("The wizard successfully configured your NFS server"),
		       end => 1,
		       next => 0
		      },
	       error_end => {
			     name => N("Failed"),
                             data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
                             next => 0,
                            },

	      };

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

sub check {
    -d $o->{var}{wiz_nfs_dir} or return 'error_dir'
}

sub network_mask {
    my $wiz_ip_server = $wiz->{net}->itf_get("IPADDR");
    my $mask = $wiz->{net}->itf_get("NETMASK");
    $mask = $mask || "255.255.255.0";
    $wiz_ip_server = $wiz_ip_server || "192.168.1.0";
    "$1.$2.$3.0/$mask" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
}

sub chooser {
    $o->{var}{wiz_nfs_level} == 2 || 'shownet'
}

sub do_it {
    $::testing and return;
    my $line;
    my $file = "/etc/exports";
    chomp($o->{var}{wiz_nfs_dir});
    -f $file and cp_af($file, $file.".orig");
    if ($o->{var}{wiz_nfs_level} == 2) {
	#my $mask = $wiz->{net}->itf_get("NETMASK");
	$line = "$o->{var}{wiz_nfs_dir} $o->{var}{wiz_netmask}(rw,no_root_squash,sync)\n";
    }
    else {
	$line = "$o->{var}{wiz_nfs_dir} *(rw,no_root_squash,sync)\n";
    }
    my $t;
    foreach (cat_($file)) {
	if (/^(?!#).*$o->{var}{wiz_nfs_dir}\s/) {
	    $t = $_;
	    last;
	}
    }
    substInFile { s|^(?!#).*$o->{var}{wiz_nfs_dir}\s.*|#$&| } $file;
    append_to_file($file, $line);
    system("/usr/sbin/exportfs -a");
    services::start('nfs') if services::is_service_running('nfs');
    check_started('nfsd');
}

1;