summaryrefslogtreecommitdiffstats
path: root/samba_wizard/scripts/Smbconf.pm
blob: b44c61e9e4dc245091e832635105e9539e8a6a71 (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
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002 MandrakeSoft Arnaud Desmons (adesmons@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 Smbconf;
require "__WIZ_HOME__/common/scripts/Vareqval.pm";
require "__WIZ_HOME__/common/scripts/IFCFG.pm";
use MDK::Common;
use strict;
use Data::Dumper;

# All possibilies in the config file must be precedeed by ";" 
# This script can just comment, uncomment or/and change values
# but can not add anything.
# so one variable cannot be commented and not in the same file.

my $o = IFCFG->new();

sub	check {
    $> and return 1;
    $o->is_dhcp() and return 2;
    0;
}

sub	check_services {
    $ENV{wiz_do_homes} and return 3;
    $ENV{wiz_do_file_sharing} and return 2;
    $ENV{wiz_do_printer_sharing} and return 1;
    0;
}

sub	check_dir {
    -d ($ENV{wiz_dir}) and return 10;
    1;
}

# the "__" before comment is to avoid conflicts with possible "comment" variable
# for variables value is in value key and comment idem (no risque of conflict)

sub	read_conf {
    my	$self = {};
    my	($file) = @_;
    my	$menu;
    my	@tab;
    my	%conf;

    open(FH, $file) or die "$! ($file)";
    while (<FH>) {
	if (/^(\s*\;?\s*)\[(.*)\].*/) {
	    $menu = $2;
	    $conf{$menu}{__comment} = $1;
	}
	elsif (/^(?!\#)(\s*\;*\s*)(.*?)\s*=\s*(.*)\s*$/) {
	    $conf{$menu}{$2}{value} = $3;
	    $conf{$menu}{$2}{comment} = $1;
	}
	push @tab, $_;
    }
    $self->{conf} = \%conf;
    $self->{tab} = \@tab;
    bless $self;
}

# we parse @tab to check for values in the struct.

sub	write_conf {
    my	$self = shift;
    my	($file) = @_;
    my	$menu;

    open(FH, "> $file") or die "$!";
    foreach (@{$self->{tab}}) {
	if (/^\s*\;?\s*\[(.*)\].*/) {
	    $menu = $1;
	    print FH "$self->{conf}->{$menu}{__comment}"."[$menu]\n";
	}
	elsif (/^(?!\#)\s*\;*\s*(.*?)\s*=/) {
	    print FH "$self->{conf}->{$menu}{$1}{comment}"."$1" ." = ". "$self->{conf}->{$menu}{$1}{value}\n";
	}
	else {
	    print FH $_;
	}
    }
}

#section has the name of the printer
sub	add_printer {
    my	$self = shift;
    my	($printer) = @_;

    if (exists $self->{conf}->{$printer}) {
	$self->comment_menu($printer, " ");
    }
    else {
	$self->{conf}->{$printer}{printable}{comment} = " ";
	$self->{conf}->{$printer}{printer}{comment} = " ";
	push @{$self->{tab}}, "[$printer]\n";
	push @{$self->{tab}}, "printer = $printer\n"; 
	push @{$self->{tab}}, "printable = yes\n"; 
	$self->{conf}->{$printer}{printer}{value} = $printer;
	$self->{conf}->{$printer}{printable}{value} = "yes";
    }
}

sub     list_printers {
    my	@list if 0;

    return @list if @list;
    @list = sort grep /^(?!\#).*/, map {
        my ($printer) = split(':', $_);
    } cat_("/etc/printcap");
}


sub	check_users {
    return 1 if $ENV{wiz_do_printer_sharing};
    0;
}

sub	comment_menu {
    my	$self = shift;
    my	($menu, $str) = @_;

    return if (!$menu or !exists $self->{conf}->{$menu});
    $self->{conf}->{$menu}{__comment} = $str;
    foreach (keys %{$self->{conf}->{$menu}}) {
	($_ eq "__comment") and next;
	$self->{conf}->{$menu}{$_}{comment} = $str;
    }
}

sub	comment_var {
    my	$self = shift;
    my	($menu, $var, $str) = @_;

    $self->{conf}->{$menu}{$var}{comment} = $str;
}

sub	chg_var {
    my	$self = shift;
    my	($menu, $var, $str) = @_;

    $self->{conf}->{$menu}{$var}{value} = $str;
}

# all or selected printers
sub	printer_sharing {
    my	$self = shift;

    if ($ENV{wiz_all_printers}) {
	$self->comment_menu("printers", " ");
	foreach my $printer (keys (%::bool)) {
	    $self->comment_menu($printer, ";");
	}
    }
    else {
	$self->comment_menu("printers", ";");
	foreach my $printer (keys (%::bool)) {
	    if (int($::bool{$printer})) {
		$self->comment_menu($printer, " ");
		$self->add_printer($printer);
	    }
	    else {
		$self->comment_menu($printer, ";");
	    }
	}
    }
}

sub	get_printers {
    if ($ENV{wiz_do_printer_sharing}) {
	my $string;
	$ENV{wiz_all_printers} and return "all printers";
	foreach (keys (%::bool)) {
	    $string .= "$_, " if int($::bool{$_});
	}
	"$string";
    }
    else {
	"disabled";
    }
}

sub	check_workgroup {
    !$ENV{wiz_workgroup} and return 1;
    10;
}

sub	ask_acces {
    10;
}

my $old = read_conf("/etc/samba/smb.conf");

sub	get_write {
    $old->{conf}->{public}{"write list"}{value};
}

sub	get_read {
    $old->{conf}->{public}{"read list"}{value};
}

sub	check_banner {
    !$ENV{wiz_banner} and return 1;
    10;
}

sub	get_workgroup {
    $old->{conf}->{global}{workgroup}{value}; 
}

sub	get_banner {
    $old->{conf}->{global}{"server string"}{value};
}

sub	ask_dir {
    return 2 if $ENV{wiz_do_file_sharing};
    return 1 if $ENV{wiz_do_printer_sharing};
    0;
}

sub	get_dir {
    $old->{conf}->{public}{path}{value};
}

sub	get_file_sharing {
    return 0 if ($old->{conf}->{public}{__comment} =~ /\;|\#/);
    1;
}

sub	get_home_sharing {
    return 0 if ($old->{conf}->{homes}{__comment} =~ /\;|\#/);
    1;
}

sub	get_netmask {
    "192.168.100.1/255.255.255.0";
}

sub	get_allow_host {
    $old->{conf}->{global}{"hosts allow"}{value};
}

sub	get_deny_host {
    $old->{conf}->{global}{"hosts deny"}{value};
}

sub	chk_level {
#     if ($ENV{wiz_level} == 2) {
# 	$ENV{wiz_allow_allow} = "toto";
# 	$ENV{wiz_allow_allow} = "toto";
#     }
#     else {
# 	$ENV{wiz_hosts_deny} = "tata";
# 	$ENV{wiz_hosts_deny} = "tata";
#     }
#     if ($ENV{wiz_level} == 3) {
# 	$ENV{wiz_allow_allow} = "toto";
# 	$ENV{wiz_allow_allow} = "toto";
#     }
#     else {
# 	$ENV{wiz_hosts_deny} = "tata";
# 	$ENV{wiz_hosts_deny} = "tata";
#     }
    $ENV{wiz_level};
}

# remember one variable cannot be commented and not in the same file.
sub	do_it {
    my	$file = "__WIZ_HOME__/samba_wizard/scripts/smb.conf.default";

    my $conf = read_conf($file);
    $conf->chg_var("global", "workgroup", $ENV{wiz_workgroup});
    $conf->chg_var("global", "server string", $ENV{wiz_banner});
    $conf->chg_var("public", "write list", $ENV{wiz_write_list}) if $ENV{wiz_do_file_sharing};
    $conf->chg_var("public", "read list", $ENV{wiz_read_list}) if $ENV{wiz_do_file_sharing};
    my $ip = $o->itf_get("IPADDR");
    if ($ENV{wiz_do_file_sharing}) {
	standalone->explanations("Enabling $ENV{wiz_dir} samba file sharing");
 	$conf->comment_menu("public", " ");
	$conf->chg_var("public", "path", $ENV{wiz_dir});
    }
    else {
	standalone->explanations("Disabling samba file sharing");
	$conf->comment_menu("public", ";");
    }
    if ($ENV{wiz_do_homes}) {
	standalone->explanations("Enabling samba homes sharing");
	$conf->comment_menu("homes", " ");
    }
    else {
	standalone->explanations("Disabling samba homes sharing");
	$conf->comment_menu("homes", ";");
    }
    standalone->explanations("Samba deny $ENV{wiz_hosts_deny}");
    standalone->explanations("Samba allow $ENV{wiz_hosts_allow}");
#    $conf->chg_var("global", "hosts deny", $ENV{wiz_hosts_deny});
#    $conf->chg_var("global", "hosts allow", $ENV{wiz_hosts_allow});

    if ($ENV{wiz_do_printer_sharing}) {
	standalone->explanations("Enabling printer sharing");
	$conf->printer_sharing();
    }
    else {
 	standalone->explanations("Disabling printer sharing");
 	foreach my $printer (keys (%::bool)) {
	    if (!int($::bool{$printer})) {
		$conf->comment_menu("$printer", ";");
 	    }
 	}
	$conf->comment_menu("printers", ";");
     }
    $conf->write_conf("/etc/samba/smb.conf");
    system("/etc/rc.d/init.d/smb restart");
    10;
}
1;