One Hat Cyber Team
  • Dir : ~/proc/thread-self/root/usr/share/webmin/proftpd/
  • View File Name : userpermissions_form.cgi

    \n"; ######################################### # print footer &ui_print_footer("$return?$args", $rmsg); exit; ######################################################### sub GetUsers(){ my $UserCount=0; setpwent(); while(my @uinfo = getpwent()) { if ($uinfo[2] > 100) { $UserCount++; $Users[$UserCount]=$uinfo[0]; $Usernames.=" ".$uinfo[0]; } } endpwent(); } sub GetFTPAccessUserPerms(){ # Fills global variables: # $UsedUsernames, %UserAllowedCommands, %UserDeniedCommands my ($FTPAccessFile) = @_; ################################################## # Read .ftpaccess file my $Commands = ""; open FTPACCESS, "$FTPAccessFile" or &error("Can't open $FTPAccessFile: $!"); while (my $line=){ chomp $line; #print $line."\n"; if($line =~ //i){ $Commands = $1; #print "Limit $Commands\n"; } if($line =~ /<\/Limit(.*)>/i){ $Commands = ""; #print "End Limit $Commands\n"; } if($Commands){ #print "$line\n"; if($line =~ /AllowUser (.+)/i){ my $AllowedUsernames = $1; #print "AllowUser $AllowedUsernames\n"; foreach $AllowedUsername (split (" ",$AllowedUsernames)){ next unless ($AllowedUsername); $UserAllowedCommands{$AllowedUsername}.=" ".$Commands; #print "AllowUser $AllowedUsername\n"; } } if($line =~ /DenyUser (.+)/i){ my $DeniedUsernames = $1; foreach $DeniedUsername (split (" ",$DeniedUsernames)){ next unless ($DeniedUsername); $UserDeniedCommands{$DeniedUsername}.=" ".$Commands; } } } } close FTPACCESS; ################################################## # collect all mentioned users in table $UsedUsernames=""; foreach $Username(keys %UserAllowedCommands){ #print "Adding $Username\n"; $UserAllowedCommands{$Username}= &UnifyAndExpandCommands($UserAllowedCommands{$Username}." ".$Commands); if($UsedUsernames !~ /\b$Username\b/){ $UsedUsernames.=$Username." "; } } foreach $Username(keys %UserDeniedCommands){ $UserDeniedCommands{$Username}= &UnifyAndExpandCommands($UserDeniedCommands{$Username}." ".$Commands); if($UsedUsernames !~ /\b$Username\b/){ $UsedUsernames.=$Username." "; } } } sub UnifyAndExpandCommands(){ (my $Commands) = @_; my $NewCommands = ""; foreach $Command(split(" ",$Commands)){ next unless($Command); if($CommandTuples{$Command}){ $NewCommands.=" ".$CommandTuples{$Command}; } else { $NewCommands.=" ".$Command; } } return &UnifyCommands($NewCommands); } sub UnifyCommands(){ (my $Commands) = @_; my $NewCommands = ""; foreach $Command(split(" ",$Commands)){ next unless($Command); next if($NewCommands =~ /\b$Command\b/i); if($NewCommands){ $NewCommands.=" "; } $NewCommands.=$Command; } return $NewCommands; } sub AddUser(){ (my $Username, $FTPAccessFile) = @_; if($Usernames =~ /\b$Usernames\b/){ print "

    Username $Username does not exist.

    \n"; return; } if ($UserAllowedCommands{$Username} || $UserDeniedCommands{$Username}){ # user already exists print "

    Username $Username already exists.

    \n"; return; } $UserAllowedCommands{$Username}=$MinimumCommands; $UserDeniedCommands{$Username}=""; if($UsedUsernames !~ /\b$Username\b/){ $UsedUsernames.=$Username." "; } &WritePermissions($FTPAccessFile); } sub DeleteUser(){ (my $Username, $FTPAccessFile) = @_; if($UsedUsernames =~ /\b$Usernames\b/){ print "

    Username $Username does not exist in table.

    \n"; return; } if ((!$UserAllowedCommands{$Username}) && (!$UserDeniedCommands{$Username})){ # user already deleted print "

    Username $Username is already not in table.

    \n"; return; } $UserAllowedCommands{$Username}=""; $UserDeniedCommands{$Username}=""; $UsedUsernames =~ s/\b$Username\b *//; &WritePermissions($FTPAccessFile); } sub ChangePermissions(){ (my $Username, $FTPAccessFile) = @_; if($UsedUsernames =~ /\b$Usernames\b/){ print "

    Username $Username does not exist in table.

    \n"; return; } foreach $Command(keys %Commands){ #print "$Command value=".$in{$Command}."
    \n"; if($CommandToTuple{$Command}){ # skip commands in tuples next; } my $FTPCommands=$Command; if($CommandTuples{$FTPCommands}){ $FTPCommands = $CommandTuples{$FTPCommands}; } if ($in{$Command} eq "allow"){ $UserAllowedCommands{$Username}.=" ".$FTPCommands; #print "Allow $Username $Command
    \n"; } else { $UserAllowedCommands{$Username} = &RemoveCommands($UserAllowedCommands{$Username},$FTPCommands); } if ($in{$Command} eq "deny"){ $UserDeniedCommands{$Username}.=" ".$FTPCommands; #print "Deny $Username $Command
    \n"; } else { $UserDeniedCommands{$Username} = &RemoveCommands($UserDeniedCommands{$Username},$FTPCommands); } } $UserAllowedCommands{$Username}= &UnifyCommands($MinimumCommands." ".$UserAllowedCommands{$Username}); $UserDeniedCommands{$Username}= &UnifyCommands($UserDeniedCommands{$Username}); &WritePermissions($FTPAccessFile); } sub WritePermissions(){ # Read .ftpaccess file, remove all user command permissions # and add new set of user permissions (my $FTPAccessFile) = @_; my $NewConfig = ""; my $OldCommands = ""; my $Username; # Lock .ftpaccess file &lock_file($FTPAccessFile); &lock_file($FTPAccessFile); # Read old .ftpaccess file open FTPACCESS, "$FTPAccessFile" or die "Can't read $FTPAccessFile: $!"; $DenyAllBlockFound = 0; while(my $line = ){ my $ShortLine = $line; chomp $ShortLine; #print $ShortLine."\n"; if($ShortLine =~ //i){ # start of Limit block $OldCommands = $1; #print "Limit $OldCommands\n"; $LimitBlock = $line; $ImportantLimitLineFound = 0; $DenyAllFound = 0; } elsif($ShortLine =~ /<\/Limit(.*)>/i){ # end of Limit block #print "End Limit $OldCommands\n"; $LimitBlock .= $line; if($ImportantLimitLineFound){ $NewConfig .= $LimitBlock; } if(($OldCommands =~ /\bALL\b/i) && ($DenyAllFound)){ # this was a DenyAll for All commands block $DenyAllBlockFound = 1; } $OldCommands = ""; } elsif($OldCommands){ #print "$ShortLine\n"; if($ShortLine =~ /AllowUser (.*)/i){ # AllowUser line -> will be replaced, not important } elsif($ShortLine =~ /DenyUser (.*)/i){ # DenyUser line -> will be replaced, not important } elsif($ShortLine =~ /^ +$/){ # empty line -> not important, but keep it for readability $LimitBlock .= $line; } else { # other limit directive -> important $LimitBlock .= $line; $ImportantLimitLineFound = 1; if($ShortLine =~ /\bDenyAll\b/i){ $DenyAllFound = 1; } } } else { # other directives -> keep $NewConfig .= $line; } } close FTPACCESS; # Append new directives # Append DenyAll block if not already there if(!$DenyAllBlockFound){ $NewConfig.="\n"; $NewConfig.=" DenyAll\n"; $NewConfig.="\n"; } # Append Limit blocks for users foreach $Username (sort split(" ",$Usernames)){ my $CurAllow = $UserAllowedCommands{$Username}; if ($CurAllow){ $NewConfig.="\n"; $NewConfig.=" AllowUser ".$Username."\n"; $NewConfig.="\n"; } my $CurDeny = $UserDeniedCommands{$Username}; if ($CurDeny){ $NewConfig.="\n"; $NewConfig.=" DenyUser ".$Username."\n"; $NewConfig.="\n"; } } #print "
    \n".$NewConfig."
    \n"; # Write new .ftpaccess file open FTPACCESS, "> $FTPAccessFile" or die "Can't append to $FTPAccessFile: $!"; print FTPACCESS $NewConfig; close FTPACCESS; # Unlock .ftpaccess file &unlock_file($FTPAccessFile); $logtype = 'ftpaccess'; $logname = $in{'file'}; &webmin_log($logtype, "user permissions", $logname, \%in); } sub CommandContains(){ (my $Commands, my $SubSet) = @_; foreach my $Command(split(" ",$SubSet)){ next unless($Command); if($Commands =~ /\b$Command\b/i){ return 1; } } return 0; } sub RemoveCommands(){ (my $Commands, my $SubSet) = @_; foreach my $Command(split(" ",$SubSet)){ next unless($Command); $Commands =~ s/\b$Command\b *//gi; } return $Commands; } # end.