| 1 | package DJabberd::Plugin::VCard::LDAP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use base 'DJabberd::Plugin::VCard'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Net::LDAP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use DJabberd::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | 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 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Provides an LDAP VCard backend for DJabberd | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | <Vhost mydomain.com> | ||||||||||||||||||||||||||||
| 31 | <Plugin DJabberd::Plugin::VCard::LDAP> | ||||||||||||||||||||||||||||
| 32 | LDAPURI ldap://localhost/ | ||||||||||||||||||||||||||||
| 33 | LDAPBindDN cn=reader | ||||||||||||||||||||||||||||
| 34 | LDAPBindPW pass | ||||||||||||||||||||||||||||
| 35 | LDAPBaseDN ou=people | ||||||||||||||||||||||||||||
| 36 | LDAPVersion 2 | ||||||||||||||||||||||||||||
| 37 | LDAPFilter (uid=%u) | ||||||||||||||||||||||||||||
| 38 | </Plugin> | ||||||||||||||||||||||||||||
| 39 | </VHost> | ||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub set_config_ldapuri { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapuri: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | my ($self, $ldapuri) = @_; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( $ldapuri =~ /((?:ldap[si]?\:\/\/)?[\w\.%\d]+\/?)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $self->{'ldap_uri'} = $ldapuri; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub set_config_ldapversion { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapversion: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($self, $ldapversion) = @_; | |||||||||||||||||||||||||||
| 62 | 0 | $self->{'ldap_version'} = $ldapversion; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub set_config_ldapbinddn { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapbinddn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ($self, $ldapbinddn) = @_; | |||||||||||||||||||||||||||
| 67 | 0 | $self->{'ldap_binddn'} = $ldapbinddn; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub set_config_ldapbindpw { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapbindpw: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($self, $ldapbindpw) = @_; | |||||||||||||||||||||||||||
| 72 | 0 | $self->{'ldap_bindpw'} = $ldapbindpw; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub set_config_ldapbasedn { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapbasedn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 0 | my ($self, $ldapbasedn) = @_; | |||||||||||||||||||||||||||
| 77 | 0 | $self->{'ldap_basedn'} = $ldapbasedn; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub set_config_ldapfilter { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapfilter: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($self, $ldapfilter) = @_; | |||||||||||||||||||||||||||
| 82 | 0 | $self->{'ldap_filter'} = $ldapfilter; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub finalize { | ||||||||||||||||||||||||||
|
Subroutine
finalize: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $logger->error_die("Invalid LDAP URI") unless $self->{ldap_uri}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $logger->error_die("No LDAP BaseDN Specified") unless $self->{ldap_basedn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $logger->error_die("Must specify filter with userid as %u") unless $self->{ldap_filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my %options; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $options{version} = $self->{ldap_version} if $self->{ldap_version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Initialize ldap connection | ||||||||||||||||||||||||||||
| 95 | $self->{'ldap_conn'} = Net::LDAP->new($self->{ldap_uri}, %options) | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | or $logger->error_die("Could not connect to LDAP Server ".$self->{ldap_uri}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (defined $self->{'ldap_binddn'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if (not $self->{'ldap_conn'}->bind($self->{'ldap_binddn'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | password=>$self->{'ldap_bindpw'})) { | ||||||||||||||||||||||||||||
| 101 | 0 | $logger->error_die("Could not bind to ldap server"); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | $self->SUPER::finalize; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _isdef { | ||||||||||||||||||||||||||
|
Subroutine
_isdef: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 109 | 0 | my $arg = shift; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | return $arg if defined $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | return ''; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub load_vcard { | ||||||||||||||||||||||||||
|
Subroutine
load_vcard: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 0 | my ($self, $user) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | $user =~ /^(.+)\@(.+)$/; | |||||||||||||||||||||||||||
| 118 | 0 | my ($userid,$domain) = ($1, $2); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $filter = $self->{'ldap_filter'}; | |||||||||||||||||||||||||||
| 121 | 0 | $filter =~ s/%u/$userid/; | |||||||||||||||||||||||||||
| 122 | 0 | $filter =~ s/%d/$domain/; | |||||||||||||||||||||||||||
| 123 | 0 | $logger->info("Searching $filter on ".$self->{'ldap_basedn'}); | |||||||||||||||||||||||||||
| 124 | my $srch = $self->{'ldap_conn'}->search( | ||||||||||||||||||||||||||||
| 125 | 0 | 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 | 0 | ▶ | if ($srch->code || $srch->count < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$srch->code or $srch->count < 1
MC/DC0%0/2
$srch->code or $srch->count < 1
$srch->code
$srch->count < 1
Truth table0%0/3
A or B
A= $srch->codeB= $srch->count < 1
| |||||||||||||||||||||||||||||
| 131 | 0 | $logger->info("Account $user not found."); | |||||||||||||||||||||||||||
| 132 | 0 | return; | |||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 0 | my $entry = $srch->entry(0); | |||||||||||||||||||||||||||
| 135 | 0 | my $photo = $entry->get_value('jpegPhoto'); | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (defined $photo) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $photo = '<PHOTO><BINVAL>'.encode_base64($photo).'</BINVAL></PHOTO>'; | |||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||
| 139 | 0 | $photo = ''; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 0 | my $vCard = '<vCard xmlns="vcard-temp" version="3.0">' | |||||||||||||||||||||||||||
| 142 | .'<FN>'._isdef($entry->get_value('cn')).'</FN>' | ||||||||||||||||||||||||||||
| 143 | .'<N>' | ||||||||||||||||||||||||||||
| 144 | .'<FAMILY>'._isdef($entry->get_value('sn')).'</FAMILY>' | ||||||||||||||||||||||||||||
| 145 | .'<GIVEN>'._isdef($entry->get_value('givenName')).'</GIVEN>' | ||||||||||||||||||||||||||||
| 146 | .'</N>' | ||||||||||||||||||||||||||||
| 147 | .'<NICKNAME>'._isdef($entry->get_value('displayName')).'</NICKNAME>' | ||||||||||||||||||||||||||||
| 148 | .'<ORG>' | ||||||||||||||||||||||||||||
| 149 | .'<ORGNAME>'._isdef($entry->get_value('company')).'</ORGNAME>' | ||||||||||||||||||||||||||||
| 150 | .'<ORGUNIT>'._isdef($entry->get_value('department')).'</ORGUNIT>' | ||||||||||||||||||||||||||||
| 151 | .'</ORG>' | ||||||||||||||||||||||||||||
| 152 | .'<TITLE>'._isdef($entry->get_value('title')).'</TITLE>' | ||||||||||||||||||||||||||||
| 153 | .'<TEL><HOME/><VOICE/><NUMBER>'._isdef($entry->get_value('homePhone')).'</NUMBER></TEL>' | ||||||||||||||||||||||||||||
| 154 | .'<TEL><WORK/><VOICE/><NUMBER>'._isdef($entry->get_value('telephoneNumber')).'</NUMBER></TEL>' | ||||||||||||||||||||||||||||
| 155 | .'<TEL><WORK/><FAX/><NUMBER>'._isdef($entry->get_value('facsimileTelephoneNumber')).'</NUMBER></TEL>' | ||||||||||||||||||||||||||||
| 156 | .'<TEL><WORK/><MSG/><NUMBER>'._isdef($entry->get_value('pager')).'</NUMBER></TEL>' | ||||||||||||||||||||||||||||
| 157 | .'<TEL><HOME/><CELL/><NUMBER>'._isdef($entry->get_value('mobile')).'</NUMBER></TEL>' | ||||||||||||||||||||||||||||
| 158 | .'<EMAIL><INTERNET/><PREF/><USERID>'._isdef($entry->get_value('mail')).'</USERID></EMAIL>' | ||||||||||||||||||||||||||||
| 159 | .'<ADR>' | ||||||||||||||||||||||||||||
| 160 | .'<HOME/>' | ||||||||||||||||||||||||||||
| 161 | .'<EXTADDR/>' | ||||||||||||||||||||||||||||
| 162 | .'<STREET>'._isdef($entry->get_value('streetAddress')).'</STREET>' | ||||||||||||||||||||||||||||
| 163 | .'<LOCALITY>'._isdef($entry->get_value('l')).'</LOCALITY>' | ||||||||||||||||||||||||||||
| 164 | .'<REGION>'._isdef($entry->get_value('st')).'</REGION>' | ||||||||||||||||||||||||||||
| 165 | .'<PCODE>'._isdef($entry->get_value('postalCode')).'</PCODE>' | ||||||||||||||||||||||||||||
| 166 | .'<CTRY>'._isdef($entry->get_value('co')).'</CTRY>' | ||||||||||||||||||||||||||||
| 167 | .'</ADR>' | ||||||||||||||||||||||||||||
| 168 | .'<JABBERID>'.$user.'</JABBERID>' | ||||||||||||||||||||||||||||
| 169 | .$photo | ||||||||||||||||||||||||||||
| 170 | .'<DESC>'._isdef($entry->get_value('description')).'</DESC>' | ||||||||||||||||||||||||||||
| 171 | .'</vCard>'; | ||||||||||||||||||||||||||||
| 172 | 0 | undef($entry); | |||||||||||||||||||||||||||
| 173 | 0 | undef($srch); | |||||||||||||||||||||||||||
| 174 | 0 | return $vCard; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub store_vcard { 0 } | ||||||||||||||||||||||||||
|
Subroutine
store_vcard: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Edward Rudd, C<< <urkle at outoforder.cc> >> | ||||||||||||||||||||||||||||
| 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 | 1; # End of DJabberd::Plugin::VCard::LDAP | |||||||||||||||||||||||||||