File Coverage

blib/lib/Mail/MtPolicyd/Plugin/LdapUserConfig.pm
Criterion Covered Total %
statement 38 45 84.4
branch 5 10 50.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 1 2 50.0
total 51 66 77.2


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