5 # Converts a sudoers file to LDIF format in prepration for loading into
8 # $Sudo: sudoers2ldif,v 1.5 2007/12/08 00:09:28 millert Exp $
12 # Does not yet handle multiple lines with : in them
13 # Does not yet remove quotation marks from options
14 # Does not yet escape + at the beginning of a dn
15 # Does not yet handle line wraps correctly
16 # Does not yet handle multiple roles with same name (needs tiebreaker)
19 # Sudoers entries can have multiple RunAs entries that override former ones,
20 # with LDAP sudoRunAs{Group,User} applies to all commands in a sudoRole
26 my $base=$ENV{SUDOERS_BASE} or die "$0: Container SUDOERS_BASE undefined\n";
31 # parse sudoers one line at a time
38 $_.=<> while s/\\\s*$//s;
46 if (/^Defaults\s+/i) {
48 $opt=~s/\s+$//; # remove trailing whitespace
50 } elsif (/^(\S+)\s+(.+)=\s*(.*)/) {
52 # Aliases or Definitions
53 my ($p1,$p2,$p3)=($1,$2,$3);
54 $p2=~s/\s+$//; # remove trailing whitespace
55 $p3=~s/\s+$//; # remove trailing whitespace
57 if ($p1 eq "User_Alias") {
59 } elsif ($p1 eq "Runas_Alias") {
61 } elsif ($p1 eq "Host_Alias") {
63 } elsif ($p1 eq "Cmnd_Alias") {
66 if (!$did_defaults++){
68 print "dn: cn=defaults,$base\n";
69 print "objectClass: top\n";
70 print "objectClass: sudoRole\n";
71 print "cn: defaults\n";
72 print "description: Default sudoOption's go here\n";
73 print "sudoOption: $_\n" foreach @options;
77 my @users=split /\s*,\s*/,$p1;
78 my @hosts=split /\s*,\s*/,$p2;
79 my @cmds= split /\s*,\s*/,$p3;
81 print "dn: cn=$users[0],$base\n";
82 print "objectClass: top\n";
83 print "objectClass: sudoRole\n";
84 print "cn: $users[0]\n";
85 # will clobber options
86 print "sudoUser: $_\n" foreach expand(\%UA,@users);
87 print "sudoHost: $_\n" foreach expand(\%HA,@hosts);
89 if (s/^\(([^\)]+)\)\s*//) {
90 my @runas = split(/:\s*/, $1);
91 if (defined($runas[0])) {
92 print "sudoRunAsUser: $_\n" foreach expand(\%RA, split(/,\s*/, $runas[0]));
94 if (defined($runas[1])) {
95 print "sudoRunAsGroup: $_\n" foreach expand(\%RA, split(/,\s*/, $runas[1]));
99 print "sudoCommand: $_\n" foreach expand(\%CA,@cmds);
100 print "sudoOption: $_\n" foreach @options;
105 print "parse error: $_\n";
111 # recursively expand hash elements
116 # preen the line a little
118 # if NOPASSWD: directive found, mark entire entry as not requiring
119 s/NOPASSWD:\s*// && push @options,"!authenticate";
120 s/PASSWD:\s*// && push @options,"authenticate";
121 s/NOEXEC:\s*// && push @options,"noexec";
122 s/EXEC:\s*// && push @options,"!noexec";
123 s/SETENV:\s*// && push @options,"setenv";
124 s/NOSETENV:\s*// && push @options,"!setenv";
125 s/\w+://; # silently remove other directives
126 s/\s+$//; # right trim
130 push @a,$ref->{$_} ? expand($ref,split /\s*,\s*/,$ref->{$_}):$_ foreach @_;