#! /usr/bin/perl -w package Smbconf; require "__WIZ_HOME__/common/scripts/Vareqval.pm"; require "__WIZ_HOME__/common/scripts/DrakconnectConf.pm"; use MDK::Common; 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 $o = DrakconnectConf->new(); sub check { $> and return 1; $o->is_dhcp() and return 2; 0; } sub check_services { $ENV{wiz_do_homes} and return 2; $ENV{wiz_do_file_sharing} and return 1; 0; } sub check_dir { -d ($ENV{wiz_dir}) and return 10; 1; } sub read_conf { my $self = {}; my ($file) = @_; my $menu; my @tab; my %conf; open(FH, $file) or die "$! ($file)"; while () { 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; } 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 $_; } } } sub comment_menu { my $self = shift; my ($menu, $str) = @_; $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; } sub file_sharing { my $self = shift; mkdir_p("/home/samba/public"); standalone->explanations("Enabling samba /home/samba/public sharing"); # comment_menu("homes", ";"); $self->comment_menu("public", ""); $self->chg_var("global", "security", "share"); } sub homes { my $self = shift; standalone->explanations("Enabling samba /home/samba/public sharing"); $self->comment_menu("homes", ""); $self->chg_var("global", "security", "share"); } sub printer_sharing { my $self = shift; standalone->explanations("Enabling samba printer sharing"); $self->comment_menu("printers", ""); } sub check_workgroup { !$ENV{workgroup} and return 1; 10; } sub check_banner { !$ENV{banner} and return 1; 10; } my $old = read_conf("/etc/samba/smb.conf"); sub get_workgroup { $old->{conf}->{global}{workgroup}{value}; } sub get_banner { $old->{conf}->{global}{"server string"}{value}; } sub ask_dir { $ENV{wiz_do_file_sharing}; } sub get_dir { $old->{conf}->{public}{path}{value}; } # remember one variable cannot be commented and not in the same file. sub do_it { my $conf = read_conf("__WIZ_HOME__/samba_wizard/scripts/smb.conf.default"); $conf->chg_var("global", "workgroup", $ENV{wiz_workgroup}); $conf->chg_var("global", "server string", $ENV{wiz_banner}); my $ip = $o->get_from_known_dev("IP"); if ($ENV{wiz_do_file_sharing}) { $conf->file_sharing(); $conf->chg_var("public", "path", $ENV{wiz_dir}); } $conf->printer_sharing() if $ENV{wiz_do_printer_sharing}; $conf->homes() if $ENV{wiz_do_homes}; $conf->chg_var("global", "hosts allow", $ip); $conf->chg_var("global", "security", "share"); $conf->write_conf("/etc/samba/smb.conf"); system("/etc/rc.d/init.d/smb restart"); 10; } 1;