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

package Smbconf;
require "/usr/share/wizards/common/scripts/Vareqval.pm";
require "/usr/share/wizards/common/scripts/DrakconnectConf.pm";

use strict;

# 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 %conf;
my @tab;

my $o = DrakconnectConf->new();

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

sub	read_conf {
    my	($file) = @_;
    my	$menu;
    
    open(FH, $file) or die "$!";
    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, $_;
    }
}

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

sub	comment_menu {
    my	($menu, $str) = @_;
    
    $conf{$menu}{__comment} = $str;
    foreach (keys %{$conf{$menu}}) {
	($_ eq "__comment") and next;
	$conf{$menu}{$_}{comment} = $str;
    }
}

sub	comment_var {
    my	($menu, $var, $str) = @_;
    
    $conf{$menu}{$var}{comment} = $str;
}

sub	chg_var {
    my	($menu, $var, $str) = @_;
    
    $conf{$menu}{$var}{value} = $str;
}


sub	file_sharing {
    standalone->explanations("Enabling samba file sharing");
    comment_menu("homes", ";");
    comment_menu("public", "");
    chg_var("global", "security", "share");
}

sub	printer_sharing {
    standalone->explanations("Enabling samba printer sharing");
    comment_menu("printers", "");
}

# remember one variable cannot be commented and not in the same file.

sub	do_it {

    read_conf("/usr/share/wizards/samba_wizard/scripts/smb.conf.default");
    chg_var("global", "workgroup", $ENV{wiz_workgroup});
    chg_var("global", "server string", $ENV{wiz_banner});
    my $ip = $o->get_from_known_dev("IP");
    file_sharing() if $ENV{wiz_do_file_sharing};
    printer_sharing() if $ENV{wiz_do_printer_sharing};
    chg_var("global", "hosts allow", $ip);
    chg_var("global", "security", "share");
    write_conf("/etc/samba/smb.conf");
    system("/etc/rc.d/init.d/smb restart >/dev/null 2>&1");
    10;
}
1;