diff options
Diffstat (limited to 'perl-install/standalone')
-rwxr-xr-x | perl-install/standalone/fileshareset | 182 |
1 files changed, 144 insertions, 38 deletions
diff --git a/perl-install/standalone/fileshareset b/perl-install/standalone/fileshareset index 794e8beef..9e0e993ea 100755 --- a/perl-install/standalone/fileshareset +++ b/perl-install/standalone/fileshareset @@ -1,11 +1,40 @@ #!/usr/bin/perl -T use strict; +######################################## +# config files +$nfs_exports::default_options = '*(ro,all_squash)'; +$nfs_exports::conf_file = '/etc/exports'; +$smb_exports::conf_file = '/etc/samba/smb.conf'; +my $authorisation_file = '/etc/security/fileshare.conf'; +my $authorisation_group = 'fileshare'; + + +######################################## +# fileshare utility $Id$ +# Copyright (C) 2001 MandrakeSoft (pixel@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. + + +######################################## my $uid = $<; my $username = getpwuid($uid); -my $authorisation_file = '/etc/security/fileshare.conf'; -my $authorisation_group = 'fileshare'; +######################################## +# errors my $usage = "usage: fileshareset --add <dir> fileshareset --remove <dir>"; @@ -14,7 +43,7 @@ qq(You are not authorised to use fileshare'ing To grant you the rights, either: - put "RESTRICT=no" in $authorisation_file - put user "$username" in group "$authorisation_group"); - +my $no_export_method = "can't export anything: no nfs, no smb"; my %exit_codes = reverse ( 1 => $non_authorised, @@ -27,13 +56,21 @@ my %exit_codes = reverse ( # when removing 5 => "not exported", + 6 => $no_export_method, + 255 => "various", ); -%ENV = (); +################################################################################ +# correct PATH needed to call /etc/init.d/... ? seems not, but... +%ENV = ();#(PATH => '/bin:/sbin:/usr/bin:/usr/sbin'); authorisation::check(); -my $nfs_exports = nfs_exports::read(); +my @exports = ( + -e $nfs_exports::conf_file ? nfs_exports::read() : (), + -e $smb_exports::conf_file ? smb_exports::read() : (), + ); +@exports or error($no_export_method); if ($0 =~ /fileshareset/) { my ($cmd, $dir) = @ARGV; @@ -43,15 +80,19 @@ if ($0 =~ /fileshareset/) { verify_mntpoint($dir); if ($cmd eq '--add') { - nfs_exports::add($nfs_exports, $dir); + my @errs = map { eval { $_->add($dir) }; $@ } @exports; + grep { !$_ } @errs or error("already exported"); } else { - nfs_exports::remove($nfs_exports, $dir); + my @errs = map { eval { $_->remove($dir) }; $@ } @exports; + grep { !$_ } @errs or error("not exported"); + } + foreach my $export (@exports) { + $export->write; + $export->update_server; } - nfs_exports::write($nfs_exports); - nfs_exports::update_server(); } - -print "$_->{mntpoint}\n" foreach grep { own($_->{mntpoint}) } @$nfs_exports; +my @mntpoints = uniq(map { map { $_->{mntpoint} } @$_ } @exports); +print "$_\n" foreach grep { own($_) } @mntpoints; sub own { (stat($_[0]))[4] == $uid } @@ -67,13 +108,13 @@ sub verify_mntpoint { $ok or error("invalid mount point"); } - sub error { my ($string) = @_; print STDERR "$string\n"; exit($exit_codes{$string} || 255); } sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 } +sub uniq { my %l; $l{$_} = 1 foreach @_; grep { delete $l{$_} } @_ } ################################################################################ @@ -109,10 +150,41 @@ sub check { } ################################################################################ +package exports; + +sub find { + my ($exports, $mntpoint) = @_; + foreach (@$exports) { + $_->{mntpoint} eq $mntpoint and return $_; + } + undef; +} + +sub add { + my ($exports, $mntpoint) = @_; + foreach (@$exports) { + $_->{mntpoint} eq $mntpoint and die 'add'; + } + push @$exports, { mntpoint => $mntpoint }; +} + +sub remove { + my ($exports, $mntpoint) = @_; + my @l = grep { $_->{mntpoint} ne $mntpoint } @$exports; + @l < @$exports or die 'remove'; + @$exports = @l; +} + + +################################################################################ package nfs_exports; +use vars qw(@ISA $conf_file $default_options); +BEGIN { @ISA = 'exports' } + sub read { - my $file = '/etc/exports'; + my $file = $conf_file; + local *F; open F, $file or return []; my ($prev_raw, $prev_line, %e, @l); @@ -155,52 +227,86 @@ sub read { $mntpoint =~ m/[\0\n\r]/ and die "i won't handle this"; push @l, { mntpoint => $mntpoint, option => $options, raw => $raw_line }; } - \@l; + bless \@l, 'nfs_exports'; } -sub find { - my ($nfs_exports, $mntpoint) = @_; +sub write { + my ($nfs_exports) = @_; foreach (@$nfs_exports) { - $_->{mntpoint} eq $mntpoint and return $_; + if (!exists $_->{options}) { + $_->{options} = $default_options; + } + if (!exists $_->{raw}) { + my $mntpoint = $_->{mntpoint} =~ /\s/ ? qq("$_->{mntpoint}") : $_->{mntpoint}; + $_->{raw} = sprintf("%s %s\n", $mntpoint, $_->{options}); + } } - undef; + local *F; + open F, ">$conf_file" or die "can't write $conf_file"; + print F $_->{raw} foreach @$nfs_exports; } -sub add { - my ($nfs_exports, $mntpoint) = @_; - foreach (@$nfs_exports) { - $_->{mntpoint} eq $mntpoint and ::error("already exported"); +sub update_server { + if (fork) { + system('/usr/sbin/exportfs', '-r'); + if (system('/sbin/pidof rpc.mountd >/dev/null') != 0 || + system('/sbin/pidof nfsd >/dev/null') != 0) { + # trying to start the server... + system('/etc/init.d/nfs', $_) foreach 'stop', 'start'; + } + exit 0; } - push @$nfs_exports, { mntpoint => $mntpoint, options => '*(ro,all_squash)' }; } -sub remove { - my ($nfs_exports, $mntpoint) = @_; - my @l = grep { $_->{mntpoint} ne $mntpoint } @$nfs_exports; - @l < @$nfs_exports or ::error("not exported"); - @$nfs_exports = @l; +################################################################################ +package smb_exports; + +use vars qw(@ISA $conf_file); +BEGIN { @ISA = 'exports' } + +sub read { + my ($s, @l); + local *F; + open F, $conf_file; + local $_; + while (<F>) { + if (/^\s*\[.*\]/ || eof F) { + #- first line in the category + my ($mntpoint) = $s =~ /^\s*path\s*=\s*(.*)/m; + push @l, { mntpoint => $mntpoint, raw => $s }; + $s = ''; + } + $s .= $_; + } + bless \@l, 'smb_exports'; } sub write { - my ($nfs_exports) = @_; - foreach (@$nfs_exports) { + my ($smb_exports) = @_; + foreach (@$smb_exports) { if (!exists $_->{raw}) { - my $mntpoint = $_->{mntpoint} =~ /\s/ ? qq("$_->{mntpoint}") : $_->{mntpoint}; - $_->{raw} = sprintf("%s %s\n", $mntpoint, $_->{options}); + $_->{raw} = <<EOF; + +[$_->{mntpoint}] + path = $_->{mntpoint} + public = yes + guest ok = yes + writable = no +EOF } } local *F; - open F, ">/etc/exports" or die "can't write /etc/exports"; - print F $_->{raw} foreach @$nfs_exports; + open F, ">$conf_file" or die "can't write $conf_file"; + print F $_->{raw} foreach @$smb_exports; } sub update_server { if (fork) { - system('/usr/sbin/exportfs', '-r'); - if (system('/sbin/pidof rpc.mountd >/dev/null') != 0 || - system('/sbin/pidof nfsd >/dev/null') != 0) { + system('/usr/bin/killall -HUP smbd 2>/dev/null'); + if (system('/sbin/pidof smbd >/dev/null') != 0 || + system('/sbin/pidof nmbd >/dev/null') != 0) { # trying to start the server... - system('/etc/init.d/nfs', $_) foreach 'stop', 'start'; + system('/etc/init.d/smb', $_) foreach 'stop', 'start'; } exit 0; } |