Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Combination/Parser.pm

raw
1 package Lemonldap::NG::Common::Combination::Parser;
2
3 use strict;
4 use Mouse;
5 use Safe;
6 use constant PE_OK => 0;
7
8 our $VERSION = '2.0.14';
9
10 # Handle "if then else" (used during init)
11 # return a sub that can be called with ($req) to get a [array] of combination
12 #
13 # During auth, these combinations represents "or" (like Multi)
14 # Each combination is a [authSub,userSub] called like this:
15 # $authSub->('authenticate',$req)
16 # This means that the 'authenticate' method of the real auth module will be
17 # called with $req
18
19 sub parse {
20 my ( $self, $moduleList, $expr ) = @_;
21
22 my $sub = '';
23 my $rest = $expr;
24 if ( $rest =~ s/^\s*if\s*\(// ) {
25 my ( $cond, $then, $else );
26 ( $cond, $rest ) = $self->findB( $rest, ')' );
27 unless ( length $cond ) {
28 die('Bad combination: unmatched bracket');
29 }
30 unless ( $rest =~ s/^\s*\bthen\b\s*// ) {
31 die('Bad combination: missing "then"');
32 }
33 if ( $rest =~ /(.*?)\s*\belse\b\s*(.*)$/ ) {
34 ( $then, $else ) = ( $1, $2 );
35 }
36 else {
37 die('Bad combination: missing "else"');
38 }
39 unless ($then) {
40 die('Bad combination: missing "then" content');
41 }
42 unless ($else) {
43 die('Bad combination: missing "else" content');
44 }
45
46 $cond = $self->buildSub($cond);
47 $then = $self->parseOr( $moduleList, $then );
48 $else = $self->parse( $moduleList, $else );
49 unless ( $then and $else ) {
50 die('Bad combination: bad then or else');
51 }
52 return sub {
53 my ($env) = @_;
54 return ( $cond->($env) ? $then : $else->($env) );
55 };
56 }
57 else {
58 my $res = $self->parseOr( $moduleList, $rest );
59 return sub { $res };
60 }
61 }
62
63 # Internal request to manage "or" boolean expr.
64 # Returns [ [authSub,userSub], [authSub,userSub] ] array
65 sub parseOr {
66 my ( $self, $moduleList, $expr ) = @_;
67 my @res;
68 foreach my $part ( split /\s+or\s+/, $expr ) {
69 push @res, $self->parseAnd( $moduleList, $part );
70 }
71 return \@res;
72 }
73
74 # Internal request to manage "and" boolean expr
75 # Returns [authSub,userSub] array
76 sub parseAnd {
77 my ( $self, $moduleList, $expr ) = @_;
78 if ( $expr =~ /\]\s*and\s*\[/ ) {
79 my @mod = ( [], [] );
80 foreach my $part ( split /\s*and\s*/, $expr ) {
81 my $tmp = $self->parseBlock( $moduleList, $part );
82 push @{ $mod[0] }, $tmp->[0];
83 push @{ $mod[1] }, $tmp->[1];
84 }
85 my @res;
86 foreach my $type (@mod) {
87 push @res, sub {
88 my %str;
89 foreach my $obj (@$type) {
90 my ( $r, $name ) = $obj->(@_);
91
92 # Case "string" (form type)
93 if ( $r && $r & ~$r ) {
94 $str{$r}++;
95 }
96 else {
97 return ( wantarray ? ( $r, $name ) : $r )
98 unless ( !$r || $r == PE_OK );
99 }
100 }
101 my $res = %str ? join( ',', keys %str ) : PE_OK;
102 return wantarray ? ( $res, $expr ) : $res;
103 };
104 }
105 return \@res;
106 }
107 else {
108 return $self->parseBlock( $moduleList, $expr );
109 }
110 }
111
112 # Internal method to parse [AuthModule,UserModule] expr
113 # Returns [authSub,userSub] array
114 sub parseBlock {
115 my ( $self, $moduleList, $expr ) = @_;
116 if ( $expr =~ /^\s*\[(.*?)\s*(?:,\s*(.*?))?\s*\]\s*$/ ) {
117 my @res = ( $1, $2 || $1 );
118 @res = (
119 $self->parseMod( $moduleList, 0, $res[0] ),
120 $self->parseMod( $moduleList, 1, $res[1] )
121 );
122 return \@res;
123 }
124 else {
125 die "Bad expression: $expr";
126 }
127 }
128
129 # Internal method to parse auth or userDB expr
130 # These expressions can be "LDAP" or "LDAP and DBI"
131 # Return sub
132 sub parseMod {
133 my ( $self, $moduleList, $type, $expr ) = @_;
134 my @list = split( /\s+and\s+/, $expr );
135 my @mods = map {
136 die "Undeclared module $_"
137 unless ( $moduleList->{$_}->[$type] );
138 $moduleList->{$_}->[$type]
139 } @list;
140 if ( @mods == 1 ) {
141 my ($m) = @mods;
142 return sub {
143 my $sub = shift;
144 my $res = $m->$sub(@_);
145 return wantarray ? ( $res, $expr ) : $res;
146 };
147 }
148 return sub {
149 my $sub = shift;
150 my %str;
151 for ( my $i = 0 ; $i < @list ; $i++ ) {
152 my $res = $mods[$i]->$sub(@_);
153
154 # Case "string" (form type)
155 if ( $res & ~$res ) {
156 $str{$res}++;
157 }
158 else {
159 return ( wantarray ? ( $res, $list[$i] ) : $res )
160 unless ( $res == PE_OK );
161 }
162 }
163 my $res = %str ? join( ',', keys %str ) : PE_OK;
164 return wantarray ? ( $res, $expr ) : $res;
165 };
166 }
167
168 # Internal request to find brackets
169 sub findB {
170 my ( $self, $expr, $char ) = @_;
171 my $res;
172 my @chars = split //, $expr;
173 while (@chars) {
174 my $c = shift @chars;
175 if ( $c eq "\\" ) {
176 $res .= $c . shift(@chars);
177 next;
178 }
179 if ( $c eq $char ) {
180 my $rest = join( '', @chars );
181 $res =~ s/^\s*(.*?)\s*/$1/;
182 $rest =~ s/^\s*(.*?)\s*/$1/;
183 return ( $res, $rest );
184 }
185 if ( $c =~ /^(?:\(|\{|\[|'|")$/ ) {
186 my $wanted = {
187 '(' => ')',
188 '{' => '}',
189 '[' => ']',
190 "'" => "'",
191 '"' => '"'
192 }->{$c};
193 my ( $m, $rest ) =
194 $self->findB( join( '', @chars ), $wanted );
195 unless ( length $m ) {
196 die("Bad combination: unmatched $c");
197 }
198 $res .= "$c$m$wanted";
199 @chars = split //, $rest;
200 next;
201 }
202 $res .= $c;
203 }
204 return undef;
205 }
206
207 # Compiles condition into sub
208 sub buildSub {
209 my ( $self, $cond ) = @_;
210 my $safe = Safe->new;
211 my $res = $safe->reval("sub{my(\$env)=\@_;local *_;return ($cond)}");
212 die "Bad condition $cond: $@" if ($@);
213 return $res;
214 }
215
216 1;