diff options
Diffstat (limited to 'perl-install/standalone/fileshareset')
-rwxr-xr-x | perl-install/standalone/fileshareset | 130 |
1 files changed, 74 insertions, 56 deletions
diff --git a/perl-install/standalone/fileshareset b/perl-install/standalone/fileshareset index f5390a382..9223d6f29 100755 --- a/perl-install/standalone/fileshareset +++ b/perl-install/standalone/fileshareset @@ -3,7 +3,7 @@ use strict; ######################################## # config files -$nfs_exports::default_options = '*(ro,all_squash)'; +$nfs_exports::default_options = '*(ro,all_squash,sync,no_subtree_check)'; $nfs_exports::conf_file = '/etc/exports'; $smb_exports::conf_file = '/etc/samba/smb.conf'; my $authorisation_file = '/etc/security/fileshare.conf'; @@ -11,8 +11,7 @@ my $authorisation_group = 'fileshare'; ######################################## -# fileshare utility $Id$ -# Copyright (C) 2001-2002 MandrakeSoft (pixel@mandrakesoft.com) +# Copyright (C) 2001-2008 Mandriva (pixel) # # 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 @@ -38,14 +37,25 @@ my $username = getpwuid($uid); my $usage = "usage: fileshareset --add <dir> fileshareset --remove <dir>"; + +my $not_enabled = +qq(File sharing is not enabled. +To enable file sharing put +"FILESHARING=yes" in $authorisation_file); + +my $not_simple_enabled = +qq(Simple file sharing is not enabled. +To enable simple file sharing put +"SHARINGMODE=simple" in $authorisation_file); + my $non_authorised = qq(You are not authorised to use fileshare'ing To grant you the rights: - put "RESTRICT=no" in $authorisation_file - or put user "$username" in group "$authorisation_group"); -my $no_export_method = "can't export anything: no nfs, no smb"; +my $no_export_method = "cannot export anything: no nfs, no smb"; -my %exit_codes = reverse ( +my %exit_codes = reverse( 1 => $non_authorised, 2 => $usage, @@ -57,6 +67,10 @@ my %exit_codes = reverse ( 5 => "not exported", 6 => $no_export_method, + + 7 => $not_enabled, + + 8 => $not_simple_enabled, 255 => "various", ); @@ -78,7 +92,7 @@ my @exports = ( if ($modify) { my ($cmd, $dir) = @ARGV; $< = $>; - @ARGV == 2 && ($cmd eq '--add' || $cmd eq '--remove') or error($usage); + @ARGV == 2 && (member($cmd, qw(--add --remove))) or error($usage); verify_mntpoint($dir); @@ -94,7 +108,7 @@ if ($modify) { $export->update_server; } } -my @mntpoints = grep {$_} uniq(map { map { $_->{mntpoint} } @$_ } @exports); +my @mntpoints = grep { $_ } uniq(map { map { $_->{mntpoint} } @$_ } @exports); print "$_\n" foreach grep { own($_) } @mntpoints; @@ -104,7 +118,7 @@ sub verify_mntpoint { local ($_) = @_; my $ok = 1; $ok &&= m|^/|; - $ok &&= !m|/../|; + $ok &&= !m|\Q/../|; $ok &&= !m|[\0\n\r]|; $ok &&= -d $_; $ok &&= own($_); @@ -123,12 +137,13 @@ sub uniq { my %l; $l{$_} = 1 foreach @_; grep { delete $l{$_} } @_ } ################################################################################ package authorisation; +my $F_lock; sub read_conf { my ($exclusive_lock) = @_; - open F_lock, $authorisation_file; # don't care if it's missing - flock(F_lock, $exclusive_lock ? 2 : 1) or die "can't lock"; + open $F_lock, $authorisation_file; # do not care if it's missing + flock($F_lock, $exclusive_lock ? 2 : 1) or die "cannot lock"; my %conf; - foreach (<F_lock>) { + foreach (<$F_lock>) { s/#.*//; # remove comments s/^\s+//; s/\s+$//; @@ -136,19 +151,25 @@ sub read_conf { my ($cmd, $value) = split('=', $_, 2); $conf{$cmd} = $value || warn qq(suspicious line "$_" in $authorisation_file\n); } - # no close F_lock, keep it locked - \%conf + # no close $F_lock, keep it locked + \%conf; } sub check { my ($exclusive_lock) = @_; my $conf = read_conf($exclusive_lock); - if (lc($conf->{RESTRICT}) eq 'no') { + if (lc($conf->{FILESHARING}) eq 'no') { + ::error($not_enabled); + } elsif (lc($conf->{SHARINGMODE}) eq 'advanced') { + ::error($not_simple_enabled); + } elsif ($conf->{FILESHAREGROUP}) { + $authorisation_group = $conf->{FILESHAREGROUP}; + } elsif (lc($conf->{RESTRICT}) eq 'no') { # ok, access granted for everybody } else { my @l; - while (@l = getgrent) { + while (@l = getgrent()) { last if $l[0] eq $authorisation_group; } ::member($username, split(' ', $l[3])) or ::error($non_authorised); @@ -158,7 +179,7 @@ sub check { ################################################################################ package exports; -sub find { +sub _find { my ($exports, $mntpoint) = @_; foreach (@$exports) { $_->{mntpoint} eq $mntpoint and return $_; @@ -168,9 +189,7 @@ sub find { sub add { my ($exports, $mntpoint) = @_; - foreach (@$exports) { - $_->{mntpoint} eq $mntpoint and die 'add'; - } + _find($exports, $mntpoint) and die 'add'; push @$exports, my $e = { mntpoint => $mntpoint }; $e; } @@ -189,14 +208,13 @@ package nfs_exports; use vars qw(@ISA $conf_file $default_options); BEGIN { @ISA = 'exports' } -sub read { +sub read() { my $file = $conf_file; - local *F; - open F, $file or return []; + open(my $F, $file) or return []; - my ($prev_raw, $prev_line, %e, @l); + my ($prev_raw, $prev_line, @l); my $line_nb = 0; - foreach my $raw (<F>) { + foreach my $raw (<$F>) { $line_nb++; local $_ = $raw; $raw .= "\n" if !/\n/; @@ -231,7 +249,7 @@ sub read { $mntpoint =~ s/\\(\d{3})/chr(oct $1)/ge; # not accepting weird characters that would break the output - $mntpoint =~ m/[\0\n\r]/ and die "i won't handle this"; + $mntpoint =~ m/[\0\n\r]/ and die "i will not handle this"; push @l, { mntpoint => $mntpoint, option => $options, raw => $raw_line }; } bless \@l, 'nfs_exports'; @@ -248,18 +266,17 @@ sub write { $_->{raw} = sprintf("%s %s\n", $mntpoint, $_->{options}); } } - local *F; - open F, ">$conf_file" or die "can't write $conf_file"; - print F $_->{raw} foreach @$nfs_exports; + open(my $F, ">$conf_file") or die "cannot write $conf_file"; + print $F $_->{raw} foreach @$nfs_exports; } -sub update_server { - if (fork) { +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) { + if (system('PATH=/bin:/sbin pidof rpc.mountd >/dev/null') != 0 || + system('PATH=/bin:/sbin pidof nfsd >/dev/null') != 0) { # trying to start the server... - system('/etc/init.d/portmap start') if system('/etc/init.d/portmap status') != 0; + system('/etc/init.d/portmap start') if system('/etc/init.d/portmap status >/dev/null') != 0; system('/etc/init.d/nfs', $_) foreach 'stop', 'start'; } exit 0; @@ -272,13 +289,12 @@ package smb_exports; use vars qw(@ISA $conf_file); BEGIN { @ISA = 'exports' } -sub read { +sub read() { my ($s, @l); - local *F; - open F, $conf_file; + open(my $F, $conf_file); local $_; - while (<F>) { - if (/^\s*\[.*\]/ || eof F) { + while (<$F>) { + if (/^\s*\[.*\]/ || eof $F) { #- first line in the category my ($label) = $s =~ /^\s*\[(.*)\]/; my ($mntpoint) = $s =~ /^\s*path\s*=\s*(.*)/m; @@ -306,9 +322,8 @@ sub write { EOF } } - local *F; - open F, ">$conf_file" or die "can't write $conf_file"; - print F $_->{raw} foreach @$smb_exports; + open(my $F, ">$conf_file") or die "cannot write $conf_file"; + print $F $_->{raw} foreach @$smb_exports; } sub add { @@ -341,15 +356,13 @@ sub name_mangle { if (length($s) > 8 && !grep { /\Q$s/ } @others) { # dropping leading directories when the resulting is still long and meaningful $_ = $s; - next; + } else { + s|(.*)[0-9#\-_!/]|$1| + # inspired by "Christian Brolin" "Long names are doom" on comp.lang.functional + || s|(.+)[AEIOU]|$1|# allButFirstVowels + || s|(.*)(.)\2|$1$2| # adjacentDuplicates + || s|(.*).|$1|; # booh, :'-( } - s|(.*)[0-9#\-_!/]|$1| and next; - - # inspired by "Christian Brolin" "Long names are doom" on comp.lang.functional - s|(.+)[AEIOU]|$1| and next; # allButFirstVowels - s|(.*)(.)\2|$1$2| and next; # adjacentDuplicates - - s|(.*).|$1|; # booh, :'-( } # 5. remove "/"s still there @@ -366,8 +379,8 @@ sub name_mangle { last A; } } - $_ or die "can't find a unique name"; - # can't find a unique name, dropping the last letter + $_ or die "cannot find a unique name"; + # cannot find a unique name, dropping the last letter s|(.*).|$1|; } } @@ -376,13 +389,18 @@ sub name_mangle { $_; } -sub update_server { - if (fork) { +sub update_server() { + if (fork()) { system('/usr/bin/killall -HUP smbd 2>/dev/null'); - if (system('/sbin/pidof smbd >/dev/null') != 0 || - system('/sbin/pidof nmbd >/dev/null') != 0) { + if (system('PATH=/bin:/sbin pidof smbd >/dev/null') != 0 || + system('PATH=/bin:/sbin pidof nmbd >/dev/null') != 0) { # trying to start the server... - system('/etc/init.d/smb', $_) foreach 'stop', 'start'; + my ($f) = grep { -f $_ } map { "/etc/init.d/$_" } 'smb', 'samba', 'rc.samba'; + if ($f) { + system($f, $_) foreach 'stop', 'start'; + } else { + print STDERR "Error: Can't find the samba init script \n"; + } } exit 0; } |