line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::Plugin::LdapUserConfig; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2314
|
use Moose; |
|
2
|
|
|
|
|
26
|
|
|
2
|
|
|
|
|
16
|
|
4
|
2
|
|
|
2
|
|
13139
|
use namespace::autoclean; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
22
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.23'; # VERSION |
7
|
|
|
|
|
|
|
# ABSTRACT: mtpolicyd plugin for retrieving per user configuration from LDAP |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
extends 'Mail::MtPolicyd::Plugin'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
230
|
use Mail::MtPolicyd::LdapConnection; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
67
|
|
13
|
2
|
|
|
2
|
|
11
|
use Mail::MtPolicyd::Plugin::Result; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
52
|
|
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
7544
|
use Net::LDAP::Util qw( escape_filter_value ); |
|
2
|
|
|
|
|
212
|
|
|
2
|
|
|
|
|
1296
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub init { |
18
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
19
|
0
|
0
|
|
|
|
|
if( ! Mail::MtPolicyd::LdapConnection->is_initialized ) { |
20
|
0
|
|
|
|
|
|
die('no ldap connection initialized, but required for plugin '.$self->name); |
21
|
|
|
|
|
|
|
} |
22
|
0
|
|
|
|
|
|
return; |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'basedn' => ( is => 'rw', isa => 'Str', default => '' ); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has 'filter' => ( is => 'rw', isa => 'Str', required => 1 ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Plugin::Role::ConfigurableFields' => { |
30
|
|
|
|
|
|
|
'fields' => { |
31
|
|
|
|
|
|
|
'filter' => { |
32
|
|
|
|
|
|
|
isa => 'Str', |
33
|
|
|
|
|
|
|
default => 'sasl_username', |
34
|
|
|
|
|
|
|
value_isa => 'Str', |
35
|
|
|
|
|
|
|
}, |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has 'config_fields' => ( is => 'rw', isa => 'Str', required => 1 ); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
has '_config_fields' => ( |
43
|
|
|
|
|
|
|
is => 'ro', isa => 'ArrayRef[Str]', lazy => 1, |
44
|
|
|
|
|
|
|
default => sub { |
45
|
|
|
|
|
|
|
my $self = shift; |
46
|
|
|
|
|
|
|
return [ split(/\s*,\s*/, $self->config_fields ) ]; |
47
|
|
|
|
|
|
|
}, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub retrieve_ldap_entry { |
51
|
0
|
|
|
0
|
0
|
|
my ( $self, $r ) = @_; |
52
|
0
|
|
|
|
|
|
my $ldap = Mail::MtPolicyd::LdapConnection->handle; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my $value = $self->get_filter_value( $r ); |
55
|
0
|
0
|
|
|
|
|
if( ! defined $value ) { |
56
|
0
|
|
|
|
|
|
$self->log( $r, 'filter_field('.$self->filter_field.') is not defined in request. skipping ldap search.'); |
57
|
0
|
|
|
|
|
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
my $filter = $self->filter; |
60
|
0
|
|
|
|
|
|
my $filter_value = escape_filter_value($value); |
61
|
0
|
|
|
|
|
|
$filter =~ s/%s/$filter_value/g; |
62
|
0
|
|
|
|
|
|
$self->log( $r, 'ldap filter is: '.$filter); |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my $msg; |
65
|
0
|
|
|
|
|
|
eval { |
66
|
0
|
|
|
|
|
|
$msg = $ldap->search( |
67
|
|
|
|
|
|
|
base => $self->basedn, |
68
|
|
|
|
|
|
|
filter => $filter, |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
}; |
71
|
0
|
0
|
|
|
|
|
if( $@ ) { |
72
|
0
|
|
|
|
|
|
$self->log( $r, 'ldap search failed: '.$@ ); |
73
|
0
|
|
|
|
|
|
return; |
74
|
|
|
|
|
|
|
} |
75
|
0
|
0
|
|
|
|
|
if( $msg->count != 1 ) { |
76
|
0
|
|
|
|
|
|
$self->log( $r, 'ldap search return '.$msg->count.' entries' ); |
77
|
0
|
|
|
|
|
|
return; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $entry = $msg->entry( 0 ); |
81
|
0
|
|
|
|
|
|
$self->log( $r, 'found in ldap: '.$entry->dn ); |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
return $entry; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub run { |
87
|
0
|
|
|
0
|
1
|
|
my ( $self, $r ) = @_; |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
my $entry = $self->retrieve_ldap_entry( $r ); |
90
|
0
|
0
|
|
|
|
|
if( defined $entry ) { |
91
|
0
|
|
|
|
|
|
foreach my $field ( @{$self->_config_fields} ) { |
|
0
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my ($value) = $entry->get_value( $field ); |
93
|
0
|
0
|
0
|
|
|
|
if( defined $value && $value ne '' ) { |
94
|
0
|
|
|
|
|
|
$self->log( $r, 'retrieved ldap attribute: '.$field.'='.$value ); |
95
|
0
|
|
|
|
|
|
$r->session->{$field} = $value; |
96
|
|
|
|
|
|
|
} else { |
97
|
0
|
|
|
|
|
|
$self->log( $r, 'LDAP attribute '.$field.' is empty. skipping.' ); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=pod |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=encoding UTF-8 |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 NAME |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Mail::MtPolicyd::Plugin::LdapUserConfig - mtpolicyd plugin for retrieving per user configuration from LDAP |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 VERSION |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
version 1.23 |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 SYNOPSIS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
ldap_host="localhost" |
126
|
|
|
|
|
|
|
ldap_binddn="cn=readonly,dc=domain,dc=com" |
127
|
|
|
|
|
|
|
ldap_password="secret" |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
<Plugin user_config> |
130
|
|
|
|
|
|
|
module="LdapUserConfig" |
131
|
|
|
|
|
|
|
basedn="ou=users,dc=domain,dc=com" |
132
|
|
|
|
|
|
|
filter="(mail=%s)" |
133
|
|
|
|
|
|
|
filter_field="sasl_username" |
134
|
|
|
|
|
|
|
config_fields="mailMessageLimit,mailSendExternal" |
135
|
|
|
|
|
|
|
</Plugin> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 DESCRIPTION |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This plugin could be used to retrieve session variables/user configuration |
140
|
|
|
|
|
|
|
from a LDAP server. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 PARAMETERS |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
The LDAP connection must be configured in the global configuration section |
145
|
|
|
|
|
|
|
of mtpolicyd. See L<mtpolicyd>. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=over |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item basedn (default: '') |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
The basedn to use for the search. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item filter (required) |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
The filter to use for the search. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The pattern %s will be replaced with the content of filter_field. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item filter_field (required) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
The content of this request field will be used to replace %s in the |
162
|
|
|
|
|
|
|
filter string. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item config_fields (required) |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
A comma seperated list of LDAP attributes to retrieve and |
167
|
|
|
|
|
|
|
copy into the current mtpolicyd session. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=back |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 AUTHOR |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Markus Benning <ich@markusbenning.de> |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
This is free software, licensed under: |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
The GNU General Public License, Version 2, June 1991 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |