5 # Converts a sudoers file to LDIF format in prepration for loading into
10 # Does not yet handle multiple lines with : in them
11 # Does not yet remove quotation marks from options
12 # Does not yet escape + at the beginning of a dn
13 # Does not yet handle line wraps correctly
14 # Does not yet handle multiple roles with same name (needs tiebreaker)
17 # Sudoers entries can have multiple RunAs entries that override former ones,
18 # with LDAP sudoRunAs{Group,User} applies to all commands in a sudoRole
24 my $base=$ENV{SUDOERS_BASE} or die "$0: Container SUDOERS_BASE undefined\n";
29 # parse sudoers one line at a time
36 $_.=<> while s/\\\s*$//s;
44 if (/^Defaults\s+/i) {
46 $opt=~s/\s+$//; # remove trailing whitespace
48 } elsif (/^(\S+)\s+(.+)=\s*(.*)/) {
50 # Aliases or Definitions
51 my ($p1,$p2,$p3)=($1,$2,$3);
52 $p2=~s/\s+$//; # remove trailing whitespace
53 $p3=~s/\s+$//; # remove trailing whitespace
55 if ($p1 eq "User_Alias") {
57 } elsif ($p1 eq "Runas_Alias") {
59 } elsif ($p1 eq "Host_Alias") {
61 } elsif ($p1 eq "Cmnd_Alias") {
64 if (!$did_defaults++){
66 print "dn: cn=defaults,$base\n";
67 print "objectClass: top\n";
68 print "objectClass: sudoRole\n";
69 print "cn: defaults\n";
70 print "description: Default sudoOption's go here\n";
71 print "sudoOption: $_\n" foreach @options;
75 my @users=split /\s*,\s*/,$p1;
76 my @hosts=split /\s*,\s*/,$p2;
77 my @cmds= split /\s*,\s*/,$p3;
79 print "dn: cn=$users[0],$base\n";
80 print "objectClass: top\n";
81 print "objectClass: sudoRole\n";
82 print "cn: $users[0]\n";
83 # will clobber options
84 print "sudoUser: $_\n" foreach expand(\%UA,@users);
85 print "sudoHost: $_\n" foreach expand(\%HA,@hosts);
87 if (s/^\(([^\)]+)\)\s*//) {
88 my @runas = split(/:\s*/, $1);
89 if (defined($runas[0])) {
90 print "sudoRunAsUser: $_\n" foreach expand(\%RA, split(/,\s*/, $runas[0]));
92 if (defined($runas[1])) {
93 print "sudoRunAsGroup: $_\n" foreach expand(\%RA, split(/,\s*/, $runas[1]));
97 print "sudoCommand: $_\n" foreach expand(\%CA,@cmds);
98 print "sudoOption: $_\n" foreach @options;
103 print "parse error: $_\n";
109 # recursively expand hash elements
114 # preen the line a little
116 # if NOPASSWD: directive found, mark entire entry as not requiring
117 s/NOPASSWD:\s*// && push @options,"!authenticate";
118 s/PASSWD:\s*// && push @options,"authenticate";
119 s/NOEXEC:\s*// && push @options,"noexec";
120 s/EXEC:\s*// && push @options,"!noexec";
121 s/SETENV:\s*// && push @options,"setenv";
122 s/NOSETENV:\s*// && push @options,"!setenv";
123 s/\w+://; # silently remove other directives
124 s/\s+$//; # right trim
128 push @a,$ref->{$_} ? expand($ref,split /\s*,\s*/,$ref->{$_}):$_ foreach @_;