#!/home/product/code/webui-perl/bin/perl -T -I /home/product/code/firmware/current/lib/perl5
#SUDOER_USERS: nobody
#SUDOER_RUNAS_USERS: root, mta
#SUDOER_TAGS: NOPASSWD, NOEXEC
use strict;
use warnings;
use Barracuda::Sudo::Lib;
use Barracuda::Sudo::SafeOpen;
use File::stat;
use FileHandle;
use Fcntl qw(:mode);
use Fcntl;

=pod

=head1 NAME

chmod_aw

=head1 DESCRIPTION

This sudoer changes the permissons of the file to be world writeable

=cut

my $validator = Barracuda::Sudo::SafeOpen::Validator->new();
my $test_validator = Barracuda::Sudo::SafeOpen::Validator->new();
$test_validator->allow_world_writeable();

my %whitelist = (
    '/mail/log/stats_import' => $validator,
    '/tmp/test_chmod_aw/file.txt' => $test_validator
);

my ($file) = get_argv(is_whitelisted(keys %whitelist));
my $fd = Barracuda::Sudo::SafeOpen::safe_open($file, $whitelist{$file}, O_WRONLY | O_NOFOLLOW);
my $fh = FileHandle->new_from_fd($fd, 'r+');

my $stat = stat($fh) or die "No $fh: $!";
my $mode = ($stat->mode & 07777);

my $new_mode = $mode | S_IWUSR | S_IWGRP | S_IWOTH;

my $ret = ! chmod($new_mode, $fh);
close($fh);
exit $ret;
