Add support for #include and #includedir from Natale Vinto.
This commit is contained in:
@@ -45,28 +45,27 @@ my $notAfter;
|
|||||||
my $did_defaults = 0;
|
my $did_defaults = 0;
|
||||||
my $order = 0;
|
my $order = 0;
|
||||||
my %seen_users;
|
my %seen_users;
|
||||||
|
my @sudoersdirs;
|
||||||
|
|
||||||
# parse sudoers one line at a time
|
# Parsing is now through callbacks reading @ARGV or STDIN
|
||||||
while (<>){
|
sub parse_sudoers_line {
|
||||||
|
my $line = shift;
|
||||||
|
|
||||||
# remove comment
|
# remove comment
|
||||||
s/#.*//;
|
$line =~ s/#.*//;
|
||||||
|
|
||||||
# line continuation
|
|
||||||
$_.=<> while s/\\\s*$//s;
|
|
||||||
|
|
||||||
# cleanup newline
|
# cleanup newline
|
||||||
chomp;
|
chomp $line;
|
||||||
|
|
||||||
# ignore blank lines
|
# ignore blank lines
|
||||||
next if /^\s*$/;
|
return if $line =~ /^\s*$/;
|
||||||
|
|
||||||
if (s/^Defaults\s+//) {
|
if ($line =~ s/^Defaults\s+//) {
|
||||||
s/\s+$//; # remove trailing whitespace
|
$line =~ s/\s+$//; # remove trailing whitespace
|
||||||
# remove spaces between '!', '=', '+=' and '-='
|
# remove spaces between '!', '=', '+=' and '-='
|
||||||
s/^(\S+)\s*([\+-]?=)\s*(\S.*)$/$1$2$3/ unless s/^!\s*(\S.*)$/!$1/;
|
$line =~ s/^(\S+)\s*([\+-]?=)\s*(\S.*)$/$1$2$3/ unless $line =~ s/^!\s*(\S.*)$/!$1/;
|
||||||
push @options, $_;
|
push @options, $line;
|
||||||
} elsif (/^(\S+)\s+([^=]+)=\s*(.*)/) {
|
} elsif ($line =~ /^(\S+)\s+([^=]+)=\s*(.*)/) {
|
||||||
# Aliases or Definitions
|
# Aliases or Definitions
|
||||||
my ($p1, $p2, $p3) = ($1, $2, $3);
|
my ($p1, $p2, $p3) = ($1, $2, $3);
|
||||||
$p2 =~ s/\s+$//; # remove trailing whitespace
|
$p2 =~ s/\s+$//; # remove trailing whitespace
|
||||||
@@ -93,7 +92,7 @@ while (<>){
|
|||||||
print "\n";
|
print "\n";
|
||||||
}
|
}
|
||||||
# Definition
|
# Definition
|
||||||
my @users=split /\s*,\s*/,$p1;
|
my @users = split(/\s*,\s*/, $p1);
|
||||||
my $username = $users[0];
|
my $username = $users[0];
|
||||||
if ($seen_users{$username}) {
|
if ($seen_users{$username}) {
|
||||||
$seen_users{$username} += 1;
|
$seen_users{$username} += 1;
|
||||||
@@ -101,8 +100,8 @@ while (<>){
|
|||||||
} else {
|
} else {
|
||||||
$seen_users{$username} = 1;
|
$seen_users{$username} = 1;
|
||||||
}
|
}
|
||||||
my @hosts=split /\s*,\s*/,$p2;
|
my @hosts = split(/\s*,\s*/, $p2);
|
||||||
my @cmds= split /\s*,\s*/,$p3;
|
my @cmds = split(/\s*,\s*/, $p3);
|
||||||
@options=();
|
@options=();
|
||||||
undef $notBefore;
|
undef $notBefore;
|
||||||
undef $notAfter;
|
undef $notAfter;
|
||||||
@@ -132,12 +131,11 @@ while (<>){
|
|||||||
print "\n";
|
print "\n";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print "parse error: $_\n";
|
warn "parse error: $line\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
# Recursively expand hash elements
|
||||||
# recursively expand hash elements
|
|
||||||
sub expand {
|
sub expand {
|
||||||
my $ref=shift;
|
my $ref=shift;
|
||||||
my @a=();
|
my @a=();
|
||||||
@@ -172,3 +170,80 @@ sub expand{
|
|||||||
push @a,$ref->{$_} ? expand($ref,split /\s*,\s*/,$ref->{$_}):$_ foreach @_;
|
push @a,$ref->{$_} ? expand($ref,split /\s*,\s*/,$ref->{$_}):$_ foreach @_;
|
||||||
@a;
|
@a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Parse sudoers, expanding #include and #includedir directives
|
||||||
|
sub scan_sudoers_fh {
|
||||||
|
my ($filehandle, $callback) = @_;
|
||||||
|
|
||||||
|
while (<$filehandle>) {
|
||||||
|
if (/^#include\s+(.+?)\s*$/){
|
||||||
|
scan_sudoers_filename($1, 0, $callback);
|
||||||
|
} elsif (/^#includedir\s+(.+?)\s*$/){
|
||||||
|
scan_sudoers_dirname($1, $callback);
|
||||||
|
} else {
|
||||||
|
$callback->($_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Read from #include file
|
||||||
|
sub scan_sudoers_filename {
|
||||||
|
my ($filename, $quiet, $callback) = @_;
|
||||||
|
|
||||||
|
# Keep track of the parent dir of sudoers for relative includes
|
||||||
|
if ($filename =~ m:^(/.*/):) {
|
||||||
|
push(@sudoersdirs, $1);
|
||||||
|
} elsif ($#sudoersdirs != -1) {
|
||||||
|
$filename = $sudoersdirs[$#sudoersdirs] . "/$filename";
|
||||||
|
push(@sudoersdirs, $sudoersdirs[$#sudoersdirs]);
|
||||||
|
}
|
||||||
|
if (open(my $fh, '<', $filename)) {
|
||||||
|
scan_sudoers_fh($fh, $callback);
|
||||||
|
} else {
|
||||||
|
warn "unable to open $filename: $!\n" unless $quiet;
|
||||||
|
}
|
||||||
|
pop(@sudoersdirs) unless $#sudoersdirs == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Read from #includedir directory
|
||||||
|
sub scan_sudoers_dirname {
|
||||||
|
my ($dirname, $callback) = @_;
|
||||||
|
|
||||||
|
# Ignore a missing include dir
|
||||||
|
if (opendir(my $dh, $dirname)) {
|
||||||
|
while (readdir $dh) {
|
||||||
|
# Ignore files that end in '~' or have a '.' in them
|
||||||
|
next if /\./ || /~$/;
|
||||||
|
scan_sudoers_filename("$dirname/$_", 1, $callback);
|
||||||
|
}
|
||||||
|
closedir($dh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Returns a sub that reads lines until continuations,
|
||||||
|
# then it invokes the callback to do the parsing
|
||||||
|
sub make_continuation_handler {
|
||||||
|
my ($callback) = @_;
|
||||||
|
|
||||||
|
my $buffer = '';
|
||||||
|
return sub {
|
||||||
|
my ($line) = @_;
|
||||||
|
|
||||||
|
if ($line =~ s/\\\s*$//) {
|
||||||
|
# line continuation
|
||||||
|
$buffer .= $line;
|
||||||
|
} else {
|
||||||
|
# parse buffer
|
||||||
|
$callback->($buffer . $line);
|
||||||
|
# empty buffer
|
||||||
|
$buffer='';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
my $parser_cb = make_continuation_handler(\&parse_sudoers_line);
|
||||||
|
if (@ARGV) {
|
||||||
|
scan_sudoers_filename($_, 0, $parser_cb) for @ARGV;
|
||||||
|
} else {
|
||||||
|
scan_sudoers_fh(\*STDIN, $parser_cb);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user