Merge commit 'upstream/1.7.2p2'
[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 # $Sudo: sudoers2ldif,v 1.5 2007/12/08 00:09:28 millert Exp $
9 #
10
11 # BUGS:
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)
17 #
18 # CAVEATS:
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
21
22 my %RA;
23 my %UA;
24 my %HA;
25 my %CA;
26 my $base=$ENV{SUDOERS_BASE} or die "$0: Container SUDOERS_BASE undefined\n";
27 my @options=();
28
29 my $did_defaults=0;
30
31 # parse sudoers one line at a time
32 while (<>){
33
34   # remove comment
35   s/#.*//;
36
37   # line continuation
38   $_.=<> while s/\\\s*$//s;
39
40   # cleanup newline
41   chomp;
42
43   # ignore blank lines
44   next if /^\s*$/;
45
46   if (/^Defaults\s+/i) {
47     my $opt=$';
48     $opt=~s/\s+$//; # remove trailing whitespace
49     push @options,$opt;
50   } elsif (/^(\S+)\s+(.+)=\s*(.*)/) {
51
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
56
57     if ($p1 eq "User_Alias") {
58       $UA{$p2}=$p3;
59     } elsif ($p1 eq "Runas_Alias") {
60       $RA{$p2}=$p3;
61     } elsif ($p1 eq "Host_Alias") {
62       $HA{$p2}=$p3;
63     } elsif ($p1 eq "Cmnd_Alias") {
64       $CA{$p2}=$p3;
65     } else {
66       if (!$did_defaults++){
67         # do this once
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;
74         print "\n";
75       }
76       # Definition
77       my @users=split /\s*,\s*/,$p1;
78       my @hosts=split /\s*,\s*/,$p2;
79       my @cmds= split /\s*,\s*/,$p3;
80       @options=();
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);
88       foreach (@cmds) {
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]));
93           }
94           if (defined($runas[1])) {
95             print "sudoRunAsGroup: $_\n" foreach expand(\%RA, split(/,\s*/, $runas[1]));
96           }
97         }
98       }
99       print "sudoCommand: $_\n" foreach expand(\%CA,@cmds);
100       print "sudoOption: $_\n" foreach @options;
101       print "\n";
102     }
103
104   } else {
105     print "parse error: $_\n";
106   }
107
108 }
109
110 #
111 # recursively expand hash elements
112 sub expand{
113   my $ref=shift;
114   my @a=();
115
116   # preen the line a little
117   foreach (@_){
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
127   }
128
129   # do the expanding
130   push @a,$ref->{$_} ? expand($ref,split /\s*,\s*/,$ref->{$_}):$_ foreach @_;
131   @a;
132 }
133
134