File Coverage

blib/lib/DJabberd/Plugin/VCard/LDAP.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package DJabberd::Plugin::VCard::LDAP;
2              
3 1     1   24466 use warnings;
  1         3  
  1         37  
4 1     1   6 use strict;
  1         2  
  1         39  
5              
6 1     1   6 use base 'DJabberd::Plugin::VCard';
  1         7  
  1         1000  
7              
8 1     1   1195 use Net::LDAP;
  1         213467  
  1         7  
9 1     1   1054 use MIME::Base64;
  1         743  
  1         60  
10 1     1   426 use DJabberd::Log;
  0            
  0            
11              
12             our $logger = DJabberd::Log->get_logger();
13              
14             =head1 NAME
15              
16             DJabberd::VCard::LDAP - LDAP VCard Provider for DJabberd
17              
18             =head1 VERSION
19              
20             Version 0.04
21              
22             =cut
23              
24             our $VERSION = '0.04';
25              
26             =head1 SYNOPSIS
27              
28             Provides an LDAP VCard backend for DJabberd
29              
30            
31            
32             LDAPURI ldap://localhost/
33             LDAPBindDN cn=reader
34             LDAPBindPW pass
35             LDAPBaseDN ou=people
36             LDAPVersion 2
37             LDAPFilter (uid=%u)
38            
39            
40              
41             LDAPURI , LDAPBaseDN, and LDAPFilter are required
42             Everything else is optional.
43              
44             LDAPFilter is an LDAP filter substutions
45             - %u will be substituted with the incoming userid (w/o the domain) (ie. myuser)
46             - %d will be substituted with the incoming userid's domain (ie. mydoman.com)
47              
48             LDAPVersion is either 2 or 3, if nothing is specified then default to Net::LDAP default.
49             This value is passed straight to Net::LDAP
50              
51             =cut
52              
53             sub set_config_ldapuri {
54             my ($self, $ldapuri) = @_;
55             if ( $ldapuri =~ /((?:ldap[si]?\:\/\/)?[\w\.%\d]+\/?)/ ) {
56             $self->{'ldap_uri'} = $ldapuri;
57             }
58             }
59              
60             sub set_config_ldapversion {
61             my ($self, $ldapversion) = @_;
62             $self->{'ldap_version'} = $ldapversion;
63             }
64              
65             sub set_config_ldapbinddn {
66             my ($self, $ldapbinddn) = @_;
67             $self->{'ldap_binddn'} = $ldapbinddn;
68             }
69              
70             sub set_config_ldapbindpw {
71             my ($self, $ldapbindpw) = @_;
72             $self->{'ldap_bindpw'} = $ldapbindpw;
73             }
74              
75             sub set_config_ldapbasedn {
76             my ($self, $ldapbasedn) = @_;
77             $self->{'ldap_basedn'} = $ldapbasedn;
78             }
79              
80             sub set_config_ldapfilter {
81             my ($self, $ldapfilter) = @_;
82             $self->{'ldap_filter'} = $ldapfilter;
83             }
84              
85             sub finalize {
86             my $self = shift;
87             $logger->error_die("Invalid LDAP URI") unless $self->{ldap_uri};
88             $logger->error_die("No LDAP BaseDN Specified") unless $self->{ldap_basedn};
89             $logger->error_die("Must specify filter with userid as %u") unless $self->{ldap_filter};
90              
91             my %options;
92             $options{version} = $self->{ldap_version} if $self->{ldap_version};
93              
94             # Initialize ldap connection
95             $self->{'ldap_conn'} = Net::LDAP->new($self->{ldap_uri}, %options)
96             or $logger->error_die("Could not connect to LDAP Server ".$self->{ldap_uri});
97              
98             if (defined $self->{'ldap_binddn'}) {
99             if (not $self->{'ldap_conn'}->bind($self->{'ldap_binddn'},
100             password=>$self->{'ldap_bindpw'})) {
101             $logger->error_die("Could not bind to ldap server");
102             }
103             }
104              
105             $self->SUPER::finalize;
106             }
107              
108             sub _isdef {
109             my $arg = shift;
110             return $arg if defined $arg;
111             return '';
112             }
113              
114             sub load_vcard {
115             my ($self, $user) = @_;
116              
117             $user =~ /^(.+)\@(.+)$/;
118             my ($userid,$domain) = ($1, $2);
119              
120             my $filter = $self->{'ldap_filter'};
121             $filter =~ s/%u/$userid/;
122             $filter =~ s/%d/$domain/;
123             $logger->info("Searching $filter on ".$self->{'ldap_basedn'});
124             my $srch = $self->{'ldap_conn'}->search(
125             base=>$self->{'ldap_basedn'},
126             filter=>$filter,
127             attrs=>['dn','sn','cn','givenName','mail','telephoneNumber','title','description','displayName',
128             'mobile','company','department','wWWHomePage','homePhone','facsimileTelephoneNumber','pager',
129             'streetAddress','co','postalCode','postOfficeBox','st','l','jpegPhoto']);
130             if ($srch->code || $srch->count < 1) {
131             $logger->info("Account $user not found.");
132             return;
133             } else {
134             my $entry = $srch->entry(0);
135             my $photo = $entry->get_value('jpegPhoto');
136             if (defined $photo) {
137             $photo = ''.encode_base64($photo).'';
138             } else {
139             $photo = '';
140             }
141             my $vCard = ''
142             .''._isdef($entry->get_value('cn')).''
143             .''
144             .''._isdef($entry->get_value('sn')).''
145             .''._isdef($entry->get_value('givenName')).''
146             .''
147             .''._isdef($entry->get_value('displayName')).''
148             .''
149             .''._isdef($entry->get_value('company')).''
150             .''._isdef($entry->get_value('department')).''
151             .''
152             .''._isdef($entry->get_value('title')).''
153             .''._isdef($entry->get_value('homePhone')).''
154             .''._isdef($entry->get_value('telephoneNumber')).''
155             .''._isdef($entry->get_value('facsimileTelephoneNumber')).''
156             .''._isdef($entry->get_value('pager')).''
157             .''._isdef($entry->get_value('mobile')).''
158             .''._isdef($entry->get_value('mail')).''
159             .''
160             .''
161             .''
162             .''._isdef($entry->get_value('streetAddress')).''
163             .''._isdef($entry->get_value('l')).''
164             .''._isdef($entry->get_value('st')).''
165             .''._isdef($entry->get_value('postalCode')).''
166             .''._isdef($entry->get_value('co')).''
167             .''
168             .''.$user.''
169             .$photo
170             .''._isdef($entry->get_value('description')).''
171             .'';
172             undef($entry);
173             undef($srch);
174             return $vCard;
175             }
176             }
177              
178              
179             sub store_vcard { 0 }
180              
181             =head1 AUTHOR
182              
183             Edward Rudd, C<< >>
184              
185             =head1 SUPPORT
186              
187             You can find documentation for this module with the perldoc command.
188              
189             perldoc DJabberd::Plugin::VCard::LDAP
190              
191             =head1 ACKNOWLEDGEMENTS
192              
193             =head1 COPYRIGHT & LICENSE
194              
195             Copyright 2007 Edward Rudd, all rights reserved.
196              
197             This program is free software; you can redistribute it and/or modify it
198             under the same terms as Perl itself.
199              
200             =cut
201              
202             1; # End of DJabberd::Plugin::VCard::LDAP