parse-sudoers-gpl.pl


#!/usr/bin/perl
###############################################################################
#
# Fast extraction of users from /etc/sudoers file.
#
# Vulcho Nedelchev <kumcho@vulcho.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 of the License, 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.
#
###############################################################################

use strict;
use warnings;

sub parse_sudoers 
{
  # Get filename from argument or system default
  my $sudoers = shift || '/etc/sudoers';

  # Read sudoers files
  open(FH, $sudoers) or die $!;
  my @a = <FH>;
  close FH;

  # Hash to hold unique usernames as hash keys
  my %u;

  # Make string from array
  my $a = join '', @a;

  # Perform search in string
  while ($a =~ m/\!\%?([\w\d_]+)/g) {
    $u{$1} = 1;
  }

  # Return sorted list with usernames
  return [sort keys %u];
}


# This is a test of function above
#{
  use Data::Dumper;
  print Dumper parse_sudoers('sudoers');
#}