update init.d to clean new state location /var/lib/sudo, prepare to upload
[debian/sudo] / sudoers2ldif
1 #!/usr/bin/env perl
2 use strict;
3
4 #
5 # Converts a sudoers file to LDIF format in prepration for loading into
6 # the LDAP server.
7 #
8
9 # BUGS:
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)
15 #
16 # CAVEATS:
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
19
20 my %RA;
21 my %UA;
22 my %HA;
23 my %CA;
24 my $base=$ENV{SUDOERS_BASE} or die "$0: Container SUDOERS_BASE undefined\n";
25 my @options=();
26
27 my $did_defaults=0;
28
29 # parse sudoers one line at a time
30 while (<>){
31
32   # remove comment
33   s/#.*//;
34
35   # line continuation
36   $_.=<> while s/\\\s*$//s;
37
38   # cleanup newline
39   chomp;
40
41   # ignore blank lines
42   next if /^\s*$/;
43
44   if (/^Defaults\s+/i) {
45     my $opt=$';
46     $opt=~s/\s+$//; # remove trailing whitespace
47     push @options,$opt;
48   } elsif (/^(\S+)\s+(.+)=\s*(.*)/) {
49
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
54
55     if ($p1 eq "User_Alias") {
56       $UA{$p2}=$p3;
57     } elsif ($p1 eq "Runas_Alias") {
58       $RA{$p2}=$p3;
59     } elsif ($p1 eq "Host_Alias") {
60       $HA{$p2}=$p3;
61     } elsif ($p1 eq "Cmnd_Alias") {
62       $CA{$p2}=$p3;
63     } else {
64       if (!$did_defaults++){
65         # do this once
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;
72         print "\n";
73       }
74       # Definition
75       my @users=split /\s*,\s*/,$p1;
76       my @hosts=split /\s*,\s*/,$p2;
77       my @cmds= split /\s*,\s*/,$p3;
78       @options=();
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);
86       foreach (@cmds) {
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]));
91           }
92           if (defined($runas[1])) {
93             print "sudoRunAsGroup: $_\n" foreach expand(\%RA, split(/,\s*/, $runas[1]));
94           }
95         }
96       }
97       print "sudoCommand: $_\n" foreach expand(\%CA,@cmds);
98       print "sudoOption: $_\n" foreach @options;
99       print "\n";
100     }
101
102   } else {
103     print "parse error: $_\n";
104   }
105
106 }
107
108 #
109 # recursively expand hash elements
110 sub expand{
111   my $ref=shift;
112   my @a=();
113
114   # preen the line a little
115   foreach (@_){
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
125   }
126
127   # do the expanding
128   push @a,$ref->{$_} ? expand($ref,split /\s*,\s*/,$ref->{$_}):$_ foreach @_;
129   @a;
130 }
131
132