summaryrefslogtreecommitdiff
path: root/lib/Kolab/LDAP/Backend/ad.pm
blob: 33f0789ba6a461fea0b11df9cd3312aad200cc84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package Kolab::LDAP::Backend::ad;

##  COPYRIGHT
##  ---------
##
##  See AUTHORS file
##
##
##  LICENSE
##  -------
##
##  This  program is free  software; you can redistribute  it and/or
##  modify it  under the terms of the GNU  General Public License as
##  published by the  Free Software Foundation; either version 2, or
##  (at your option) any later version.
##
##  This program is  distributed in the hope that it will be useful,
##  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
##  General Public License for more details.
##
##  You can view the  GNU General Public License, online, at the GNU
##  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
##
##  $Revision$

use 5.008;
use strict;
use warnings;
use Kolab;
use Kolab::Util;
use Kolab::LDAP;
use Net::LDAP;
use Net::LDAP::Control;
use vars qw($ldap $cyrus);

require Exporter;

our @ISA = qw(Exporter);

our %EXPORT_TAGS = (
    'all' => [ qw(
    &startup
    &run
    ) ]
);

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(

);

our $VERSION = '0.9';

sub startup { 1; }

sub shutdown
{
    Kolab::log('AD', 'Shutting down');
    exit(0);
}

sub abort
{
    Kolab::log('AD', 'Aborting');
    exit(1);
}

sub changeCallback
{
    Kolab::log('AD', 'Change notification received', KOLAB_DEBUG);

    ###   $_[0]   isa     Net::LDAP::Message
    ###   $_[1]   shouldbea   Net::LDAP::Entry

    my $mesg = shift || 0;
    my $entry = shift || 0;

    my $issearch = $mesg->isa("Net::LDAP::Search");

    if (!$issearch) {
        Kolab::log('AD', 'mesg is not a search object, testing code...', KOLAB_DEBUG);
        if ($mesg->code == 88) {
            Kolab::log('AD', 'changeCallback() -> Exit code received, returning', KOLAB_DEBUG);
            return;
        } elsif ($mesg->code) {
            Kolab::log('AD', "mesg->code = `" . $mesg->code . "', mesg->msg = `" . $mesg->error . "'", KOLAB_DEBUG);
            &abort;
        }
    } else {
        Kolab::log('AD', 'mesg is a search object, not testing code', KOLAB_DEBUG);
    }

    if (!$entry) {
        Kolab::log('AD', 'changeCallback() called with a null entry', KOLAB_DEBUG);
        return;
    } elsif (!$entry->isa("Net::LDAP::Entry")) {
        Kolab::log('AD', 'changeCallback() called with an invalid entry', KOLAB_DEBUG);
        return;
    }

    if (!Kolab::LDAP::isObject($entry, $Kolab::config{'user_object_class'})) {
        Kolab::log('AD', "Entry is not a `" . $Kolab::config{'user_object_class'} . "', returning", KOLAB_DEBUG);
        return;
    }

    my $deleted = $entry->get_value($Kolab::config{'user_field_deleted'}) || 0;
    if ($deleted) {
        Kolab::LDAP::deleteObject($ldap, $cyrus, $entry);
        return;
    }

    Kolab::LDAP::createObject($ldap, $cyrus, $entry);
}

sub run
{
    # This should be called from a separate thread, as we set our
    # own interrupt handlers here

    $SIG{'INT'} = \&shutdown;
    $SIG{'TERM'} = \&shutdown;

    END {
        alarm 0;
        Kolab::LDAP::destroy($ldap);
    }

    my $mesg;

    Kolab::log('AD', 'Listener starting up');

    $cyrus = Kolab::Cyrus::create;

    Kolab::log('AD', 'Cyrus connection established', KOLAB_DEBUG);

    while (1) {
        Kolab::log('AD', 'Creating LDAP connection to AD server', KOLAB_DEBUG);

        $ldap = Kolab::LDAP::create(
            $Kolab::config{'user_ldap_ip'},
            $Kolab::config{'user_ldap_port'},
            $Kolab::config{'user_bind_dn'},
            $Kolab::config{'user_bind_pw'},
            1
        );

        if (!$ldap) {
            Kolab::log('AD', 'Sleeping 5 seconds...');
            sleep 5;
            next;
        }

        Kolab::log('AD', 'LDAP connection established', KOLAB_DEBUG);

        Kolab::LDAP::ensureAsync($ldap);

        Kolab::log('AD', 'Async checked', KOLAB_DEBUG);

        my $ctrl = Net::LDAP::Control->new(
            type    => '1.2.840.113556.1.4.528',
            critical    => 'true'
        );

        Kolab::log('AD', 'Control created', KOLAB_DEBUG);

        my @userdns = split(/;/, $Kolab::config{'user_dn_list'});
        my $userdn;

        Kolab::log('AD', 'User DN list = ' . $Kolab::config{'user_dn_list'}, KOLAB_DEBUG);

        if (length(@userdns) == 0) {
            Kolab::log('AD', 'No user DNs specified, exiting', KOLAB_ERROR);
            exit(1);
        }

        foreach $userdn (@userdns) {
            Kolab::log('AD', "Registering change notification on DN `$userdn'");

            $mesg = $ldap->search (
            base    => $userdn,
            scope       => 'one',
            control     => [ $ctrl ],
            callback    => \&changeCallback,
            filter      => '(objectClass=*)',
            attrs   => [
                '*',
                $Kolab::config{'user_field_guid'},
                $Kolab::config{'user_field_modified'},
                $Kolab::config{'user_field_quota'},
                $Kolab::config{'user_field_deleted'},
            ],
            );

            Kolab::log('AD', "Change notification registered on `$userdn'");
        }

        eval {
            local $SIG{ALRM} = sub {
                alarm 0;
                Kolab::log('AD', 'Connection refresh period expired; tearing down connection');

                Kolab::LDAP::destroy($ldap);
                next;
            };

            Kolab::log('AD', 'Waiting for changes (refresh period = ' . $Kolab::config{'conn_refresh_period'} . ' minutes)...');
            alarm $Kolab::config{'conn_refresh_period'} * 60;
            $mesg->sync;
            alarm 0;
        };
    } # End of while

    1;
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

Kolab::LDAP::Backend::ad - Perl extension for an Active Directory backend

=head1 ABSTRACT

  Kolab::LDAP::Backend::ad handles an Active Directory backend to the
  kolab daemon.

=head1 COPYRIGHT AND AUTHORS

Stuart Bingë and others (see AUTHORS file)

=head1 LICENSE

This  program is free  software; you can redistribute  it and/or
modify it  under the terms of the GNU  General Public License as
published by the  Free Software Foundation; either version 2, or
(at your option) any later version.

This program is  distributed in the hope that it will be useful,
but WITHOUT  ANY WARRANTY; without even the  implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You can view the  GNU General Public License, online, at the GNU
Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.

=cut