#! /usr/bin/perl -w package Smbconf; require "__WIZ_HOME__/common/scripts/Vareqval.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; sub read_conf { my ($file) = @_; my $menu; open(FH, $file) or die "$!"; 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, $_; } } 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 finish { my ($file, $device, $ip); read_conf("/home/logarno/drakwizard/samba_wizard/scripts/smb.conf.default"); $file = "/etc/sysconfig/mdk_serv"; my %mdk = Vareqval->get($file); foreach (qw(wiz_banner wiz_workgroup wiz_do_printer_sharing wiz_do_file_sharing)) { $mdk{$_} = $ENV{$_} if defined $ENV{$_} or die "$_ not in env"; print $ENV{$_} . "\n"; } Vareqval->commit($file, \%mdk); chg_var("global", "workgroup", $ENV{wiz_workgroup}); chg_var("global", "server string", $ENV{wiz_banner}); $device = $mdk{wiz_device}; my %mdk = Vareqval->get("/etc/sysconfig/network-scripts/ifcfg-".$device); $ip = $mdk{IPADDR}; 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"); 1; } 1;