summaryrefslogtreecommitdiff
path: root/pykolab/auth/ldap/__init__.py
blob: c3cc3538139a4e766b45745d463de97df085f2df (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# Copyright 2010-2011 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# 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; version 3 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import _ldap
import ldap
import ldap.async
import ldap.controls
import logging
import time

import pykolab

from pykolab import utils
from pykolab.constants import *
from pykolab.errors import *
from pykolab.translate import _

log = pykolab.getLogger('pykolab.auth')
conf = pykolab.getConf()

# Catch python-ldap-2.4 changes
from distutils import version

if version.StrictVersion('2.4.0') <= version.StrictVersion(ldap.__version__):
    LDAP_CONTROL_PAGED_RESULTS = ldap.CONTROL_PAGEDRESULTS
else:
    LDAP_CONTROL_PAGED_RESULTS = ldap.LDAP_CONTROL_PAGE_OID

try:
    from ldap.controls import psearch
except:
    log.warning(_("Python LDAP library does not support persistent search"))

class SimplePagedResultsControl(ldap.controls.SimplePagedResultsControl):
    """

        Python LDAP 2.4 and later breaks the API. This is an abstraction class
        so that we can handle either.
    """

    def __init__(self, page_size=0, cookie=''):
        if version.StrictVersion('2.4.0') <= version.StrictVersion(ldap.__version__):
            ldap.controls.SimplePagedResultsControl.__init__(
                    self,
                    size=page_size,
                    cookie=cookie
                )
        else:
            ldap.controls.SimplePagedResultsControl.__init__(
                    self,
                    LDAP_CONTROL_PAGED_RESULTS,
                    True,
                    (page_size, '')
                )

    def cookie(self):
        if version.StrictVersion('2.4.0') <= version.StrictVersion(ldap.__version__):
            return self.cookie
        else:
            return self.controlValue[1]

    def size(self):
        if version.StrictVersion('2.4.0') <= version.StrictVersion(ldap.__version__):
            return self.size
        else:
            return self.controlValue[0]

class LDAP(object):
    """
        Abstraction layer for the LDAP authentication / authorization backend,
        for use with Kolab.
    """

    def __init__(self):
        self.ldap = None
        self.bind = False

    def _authenticate(self, login, domain):
        log.debug(_("Attempting to authenticate user %s in domain %s")
            %(login, domain), level=8)

        self._connect()
        user_dn = self._find_dn(login[0], domain)
        try:
            log.debug(_("Binding with user_dn %s and password %s")
                %(user_dn, login[1]))

            # Needs to be synchronous or succeeds and continues setting retval
            # to True!!
            self.ldap.simple_bind_s(user_dn, login[1])
            retval = True
        except:
            retval = False
        return retval

    def _connect(self, domain=None):
        """
            Connect to the LDAP server through the uri configured.

            Pass it a configuration section name to get the ldap options from
            that section instead of the default [ldap] section.
        """
        if not self.ldap == None:
            return

        if domain == None:
            section = 'ldap'
        elif not conf.has_option(domain, 'uri'):
            section = 'ldap'

        log.debug(_("Connecting to LDAP..."), level=8)

        uri = conf.get(section, 'uri')

        log.debug(_("Attempting to use LDAP URI %s") %(uri), level=8)

        trace_level = 0

        if conf.debuglevel > 8:
            trace_level = 1

        self.ldap = ldap.initialize(uri, trace_level=trace_level)
        self.ldap.protocol_version = 3
        self.ldap.supported_controls = []

    def _bind(self):
        # TODO: Implement some mechanism for r/o, r/w and mgmt binding.
        self._connect()

        if not self.bind:
            # TODO: Use bind credentials for the domain itself.
            bind_dn = conf.get('ldap', 'bind_dn')
            bind_pw = conf.get('ldap', 'bind_pw')

            # TODO: Binding errors control
            try:
                self.ldap.simple_bind_s(bind_dn, bind_pw)
                self.bind = True
            except ldap.SERVER_DOWN:
                # Can't contact LDAP server
                #
                # - Service not started
                # - Service faulty
                # - Firewall
                pass
            except ldap.INVALID_CREDENTIALS:
                log.error(_("Invalid bind credentials"))

    def _unbind(self):
        self.ldap.unbind()
        self.bind = False

    def _reconnect(self):
        self._disconnect()
        self._connect()

    def _disconnect(self):
        self._unbind()
        del self.ldap
        self.ldap = None
        self.bind = False

    def _find_dn(self, login, domain=None):
        self._connect()
        self._bind()

        if domain == None:
            domain = conf.get('kolab', 'primary_domain')

        domain_root_dn = self._kolab_domain_root_dn(domain)

        if conf.has_option(domain, 'user_base_dn'):
            section = domain
        else:
            section = 'ldap'

        user_base_dn = conf.get_raw(
                section,
                'user_base_dn'
            ) %({'base_dn': domain_root_dn})

        if conf.has_option(domain, 'kolab_user_filter'):
            section = domain
        else:
            section = 'ldap'

        kolab_user_filter = conf.get(section, 'kolab_user_filter', quiet=True)

        if kolab_user_filter == "":
            kolab_user_filter = conf.get('ldap', 'kolab_user_filter')

        search_filter = "(&(%s=%s)%s)" %(
                conf.get(
                        'cyrus-sasl',
                        'result_attribute'
                    ),
                login,
                kolab_user_filter
            )

        _results = self._search(
                user_base_dn,
                filterstr=search_filter,
                attrlist=[ 'dn' ]
            )

        if len(_results) == 1:
            (_user_dn, _user_attrs) = _results[0]
        else:
            # Retry to find the user_dn with just uid=%s against the root_dn,
            # if the login is not fully qualified
            if len(login.split('@')) < 2:
                search_filter = "(uid=%s)" %(login)
                _results = self._search(
                        domain,
                        filterstr=search_filter,
                        attrlist=[ 'dn' ]
                    )
                if len(_results) == 1:
                    (_user_dn, _user_attrs) = _results[0]
                else:
                    # Overall fail
                    return False
            else:
                return False

        return _user_dn

    def _find_user(self, attr, value, domain=None, additional_filter=None):
        self._connect()
        self._bind()

        if domain == None:
            domain = conf.get('kolab', 'primary_domain')

        domain_root_dn = self._kolab_domain_root_dn(domain)

        if conf.has_option(domain, 'user_base_dn'):
            section = domain
        else:
            section = 'ldap'

        user_base_dn = conf.get_raw(
                section,
                'user_base_dn'
            ) %({'base_dn': domain_root_dn})

        if type(attr) == str:
            search_filter = "(%s=%s)" %(
                    attr,
                    value
                )
        elif type(attr) == list:
            search_filter = "(|"
            for _attr in attr:
                search_filter = "%s(%s=%s)" %(search_filter, _attr, value)
            search_filter = "%s)" %(search_filter)

        if additional_filter:
            search_filter = additional_filter % { 'search_filter': search_filter }

        log.debug(_("Attempting to find the user with search filter: %s") %(search_filter), level=8)

        _results = self.ldap.search_s(
                user_base_dn,
                scope=ldap.SCOPE_SUBTREE,
                filterstr=search_filter,
                attrlist=[ 'dn' ]
            )

        if len(_results) == 1:
            (_user_dn, _user_attrs) = _results[0]
        else:
            return False

        return _user_dn

    def _persistent_search(self,
            base_dn,
            scope=ldap.SCOPE_SUBTREE,
            filterstr="(objectClass=*)",
            attrlist=None,
            attrsonly=0,
            timeout=-1,
            callback=False,
            primary_domain=None,
            secondary_domains=[]
        ):
        _results = []

        psearch_server_controls = []

        psearch_server_controls.append(psearch.PersistentSearchControl(
                    criticality=True,
                    changeTypes=[ 'add', 'delete', 'modify', 'modDN' ],
                    changesOnly=False,
                    returnECs=True
                )
            )

        _search = self.ldap.search_ext(
                base_dn,
                scope=scope,
                filterstr=filterstr,
                attrlist=attrlist,
                attrsonly=attrsonly,
                timeout=timeout,
                serverctrls=psearch_server_controls
            )

        while True:
            res_type,res_data,res_msgid,_,_,_ = self.ldap.result4(
                    _search,
                    all=0,
                    add_ctrls=1,
                    add_intermediates=1,
                    resp_ctrl_classes={psearch.EntryChangeNotificationControl.controlType:psearch.EntryChangeNotificationControl}
                )

            change_type = None
            previous_dn = None
            change_number = None

            for dn,entry,srv_ctrls in res_data:
                ecn_ctrls = [
                        c for c in srv_ctrls
                        if c.controlType == psearch.EntryChangeNotificationControl.controlType
                    ]

                if ecn_ctrls:
                    change_type = ecn_ctrls[0].changeType
                    previous_dn = ecn_ctrls[0].previousDN
                    change_number = ecn_ctrls[0].changeNumber
                    change_type_desc = psearch.CHANGE_TYPES_STR[change_type]
                    #if change_type == 'modDN':
                        #log.info(_("Object relocated from %r to %r") %(previous_dn,dn))
                    #else:
                        #log.info(_("Object %r modified (%r)") %(dn,change_type_desc))

                if callback:
                    callback(
                            dn=dn,
                            previous_dn=previous_dn,
                            change_type=change_type,
                            change_number=change_number,
                            primary_domain=primary_domain,
                            secondary_domains=secondary_domains
                        )

    def _paged_search(self,
            base_dn,
            scope=ldap.SCOPE_SUBTREE,
            filterstr="(objectClass=*)",
            attrlist=None,
            attrsonly=0,
            timeout=-1,
            callback=False,
            primary_domain=None,
            secondary_domains=[]
        ):

        page_size = 500
        critical = True
        _results = []

        server_page_control = SimplePagedResultsControl(page_size=page_size)

        _search = self.ldap.search_ext(
                base_dn,
                scope=scope,
                filterstr=filterstr,
                attrlist=attrlist,
                attrsonly=attrsonly,
                serverctrls=[server_page_control]
            )

        pages = 0
        while True:
            pages += 1
            try:
                (
                        _result_type,
                        _result_data,
                        _result_msgid,
                        _result_controls
                    ) = self.ldap.result3(_search)

            except ldap.NO_SUCH_OBJECT, e:
                log.warning(_("Object %s searched no longer exists") %(base_dn))
                break

            if callback:
                callback(
                        user=_result_data,
                        primary_domain=primary_domain,
                        secondary_domains=secondary_domains
                    )

            _results.extend(_result_data)
            if (pages % 2) == 0:
                log.debug(_("%d results...") %(len(_results)))

            pctrls = [
                    c for c in _result_controls
                        if c.controlType == LDAP_CONTROL_PAGED_RESULTS
                ]

            if pctrls:
                size = pctrls[0].size
                cookie = pctrls[0].cookie
                if cookie:
                    server_page_control.cookie = cookie
                    _search = self.ldap.search_ext(
                            base_dn,
                            scope=scope,
                            filterstr=filterstr,
                            attrlist=attrlist,
                            attrsonly=attrsonly,
                            serverctrls=[server_page_control]
                        )
                else:
                    # TODO: Error out more verbose
                    break
            else:
                # TODO: Error out more verbose
                print "Warning:  Server ignores RFC 2696 control."
                break

        return _results

    def _vlv_search(self,
            base_dn,
            scope=ldap.SCOPE_SUBTREE,
            filterstr="(objectClass=*)",
            attrlist=None,
            attrsonly=0,
            timeout=-1,
            callback=False,
            primary_domain=None,
            secondary_domains=[]
        ):
        pass

    def _regular_search(self,
            base_dn,
            scope=ldap.SCOPE_SUBTREE,
            filterstr="(objectClass=*)",
            attrlist=None,
            attrsonly=0,
            timeout=-1,
            callback=False,
            primary_domain=None,
            secondary_domains=[]
        ):
        self._connect()
        self._bind()

        _search = self.ldap.search(
                base_dn,
                scope=scope,
                filterstr=filterstr,
                attrlist=attrlist,
                attrsonly=attrsonly
            )

        _results = []
        _result_type = None

        while not _result_type == ldap.RES_SEARCH_RESULT:
            (_result_type, _result) = self.ldap.result(_search, False, 0)

            if not _result == None:
                for result in _result:
                    _results.append(result)

        return _results

    def _search(self,
            base_dn,
            scope=ldap.SCOPE_SUBTREE,
            filterstr="(objectClass=*)",
            attrlist=None,
            attrsonly=0,
            timeout=-1,
            override_search=False,
            callback=False,
            primary_domain=None,
            secondary_domains=[]
        ):
        """
            Search LDAP.

            Use the priority ordered SUPPORTED_LDAP_CONTROLS and use
            the first one supported.
        """

        if len(self.ldap.supported_controls) < 1:
            for control_num in SUPPORTED_LDAP_CONTROLS.keys():
                log.debug(_("Checking for support for %s") %(SUPPORTED_LDAP_CONTROLS[control_num]['desc']), level=8)

            _search = self.ldap.search_s(
                    '',
                    scope=ldap.SCOPE_BASE,
                    attrlist=['supportedControl']
                )

            for (_result,_supported_controls) in _search:
                supported_controls = _supported_controls.values()[0]
                for control_num in SUPPORTED_LDAP_CONTROLS.keys():
                    if SUPPORTED_LDAP_CONTROLS[control_num]['oid'] in supported_controls:
                        self.ldap.supported_controls.append(SUPPORTED_LDAP_CONTROLS[control_num]['func'])

        _results = []

        if not override_search == False:
            _use_ldap_controls = [ override_search ]
        else:
            _use_ldap_controls = self.ldap.supported_controls

        for supported_control in _use_ldap_controls:
            exec("""_results = self.%s(
                    %r,
                    scope=%r,
                    filterstr=%r,
                    attrlist=%r,
                    attrsonly=%r,
                    timeout=%r,
                    callback=callback,
                    primary_domain=%r,
                    secondary_domains=%r
                )""" %(
                        supported_control,
                        base_dn,
                        scope,
                        filterstr,
                        attrlist,
                        attrsonly,
                        timeout,
                        primary_domain,
                        secondary_domains
                    )
                )

        return _results

    def _result(self, msgid=ldap.RES_ANY, all=1, timeout=-1):
        return self.ldap.result(msgid, all, timeout)

    def _domain_default_quota(self, domain):
        if conf.has_option(domain, 'default_quota'):
            return conf.get(domain, 'default_quota', quiet=True)
        elif conf.has_option('ldap', 'default_quota'):
            return conf.get('ldap', 'default_quota', quiet=True)
        elif conf.has_option('kolab', 'default_quota'):
            return conf.get('kolab', 'default_quota', quiet=True)

    def _domain_section(self, domain):
        if conf.has_section(domain):
            return domain
        else:
            return 'ldap'

    def _get_user_attribute(self, user, attribute):
        self._bind()

        attribute = attribute.lower()

        _result_type = None

        _search = self.ldap.search_ext(
                user['dn'],
                ldap.SCOPE_BASE,
                '(objectclass=*)',
                [ attribute ]
            )

        (
                _result_type,
                _result_data,
                _result_msgid,
                _result_controls
            ) = self.ldap.result3(_search)

        if len(_result_data) >= 1:
            (user_dn, user_attrs) = _result_data[0]
        else:
            log.warning(_("Could not get user attribute %s for %s")
                %(attribute,user['dn']))

            return None

        user_attrs = utils.normalize(user_attrs)

        if not user_attrs.has_key(attribute):
            log.debug(_("Attribute wanted does not exist"), level=8)
            user_attrs[attribute] = None

        return user_attrs[attribute]

    def _get_user_attributes(self, user, attributes):
        _user_attrs = {}

        self._bind()

        _search = self.ldap.search_ext(
                user['dn'],
                ldap.SCOPE_BASE,
                '(objectclass=*)',
                attributes
            )

        try:
            (
                    _result_type,
                    _result_data,
                    _result_msgid,
                    _result_controls
                ) = self.ldap.result3(_search)

            if len(_result_data) >= 1:
                (user_dn, user_attrs) = _result_data[0]
            else:
                log.warning(_("Could not get user attributes for %s") %(user['dn']))
                return None

            user_attrs = utils.normalize(user_attrs)

            # Only return the attributes requested in the function call.
            _user_attrs = {}
            for _attr in attributes:
                _user_attrs[_attr] = user_attrs[_attr]

        except ldap.NO_SUCH_OBJECT, e:
            log.warning(_("Object %s has changed") %(user['dn']))

        return _user_attrs

    def _set_user_attribute(self, user, attribute, value):
        self._bind()

        #print "user:", user

        attribute = attribute.lower()

        # TODO: This should be a schema check!
        if attribute in [ 'mailquota', 'mailalternateaddress' ]:
            if not user.has_key('objectclass'):
                user['objectclass'] = self._get_user_attribute(
                        user,
                        'objectclass'
                    )

                if user['objectclass'] == None:
                    return
            if not 'mailrecipient' in user['objectclass']:
                user['objectclass'].append('mailrecipient')
                self._set_user_attribute(
                        user,
                        'objectclass',
                        user['objectclass']
                    )

        try:
            self.ldap.modify(user['dn'], [(ldap.MOD_REPLACE, attribute, value)])
        except:
            log.warning(_("LDAP modification of attribute %s" + \
                " for %s to value %s failed") %(attribute,user_dn,value))

    def _list_domains(self):
        """
            Find the domains related to this Kolab setup, and return a list of
            DNS domain names.

            Returns a list of tuples, each tuple containing the primary domain
            name and a list of secondary domain names.
        """

        log.info(_("Listing domains..."))

        self._connect()

        bind_dn = conf.get('ldap', 'bind_dn')
        bind_pw = conf.get('ldap', 'bind_pw')

        domain_base_dn = conf.get('ldap', 'domain_base_dn', quiet=True)

        if domain_base_dn == "":
            # No domains are to be found in LDAP, return an empty list.
            # Note that the Auth() base itself handles this case.
            return []

        # If we haven't returned already, let's continue searching
        kolab_domain_filter = conf.get('ldap', 'kolab_domain_filter')

        # TODO: this function should be wrapped for error handling
        try:
            self.ldap.simple_bind_s(bind_dn, bind_pw)
        except ldap.SERVER_DOWN, e:
            raise AuthBackendError, _("Authentication database DOWN")

        _search = self._search(
                domain_base_dn,
                ldap.SCOPE_SUBTREE,
                kolab_domain_filter,
                # TODO: Where we use associateddomain is actually configurable
                [ 'associateddomain' ],
                override_search='_regular_search'
            )

        domains = []

        for domain_dn, domain_attrs in _search:
            primary_domain = None
            secondary_domains = []

            domain_attrs = utils.normalize(domain_attrs)

            # TODO: Where we use associateddomain is actually configurable
            if type(domain_attrs['associateddomain']) == list:
                primary_domain = domain_attrs['associateddomain'].pop(0)
                secondary_domains = domain_attrs['associateddomain']
            else:
                primary_domain = domain_attrs['associateddomain']

            domains.append((primary_domain,secondary_domains))

        return domains

    def _kolab_domain_root_dn(self, domain):
        self._bind()

        log.debug(_("Finding domain root dn for domain %s") %(domain), level=8)

        bind_dn = conf.get('ldap', 'bind_dn')
        bind_pw = conf.get('ldap', 'bind_pw')

        domain_base_dn = conf.get('ldap', 'domain_base_dn', quiet=True)

        if not domain_base_dn == "":

            # If we haven't returned already, let's continue searching
            domain_name_attribute = conf.get('ldap', 'domain_name_attribute')

            _results = self._search(
                    domain_base_dn,
                    ldap.SCOPE_SUBTREE,
                    "(%s=%s)" %(domain_name_attribute,domain),
                    override_search='_regular_search'
                )

            domains = []
            for _domain in _results:
                (domain_dn, _domain_attrs) = _domain
                domain_rootdn_attribute = conf.get(
                        'ldap',
                        'domain_rootdn_attribute'
                    )
                _domain_attrs = utils.normalize(_domain_attrs)
                if _domain_attrs.has_key(domain_rootdn_attribute):
                    return _domain_attrs[domain_rootdn_attribute]

        else:
            if conf.has_option('ldap', 'base_dn'):
                return conf.get('ldap', 'base_dn')

        return utils.standard_root_dn(domain)

    def _list_users(self, primary_domain, secondary_domains=[], callback=None):

        # Track state for psearch and paged searches.
        self._initial_sync_done = False

        log.info(_("Listing users for domain %s (and %s)")
            %(primary_domain, ', '.join(secondary_domains)))

        self._bind()

        # TODO: Bind with read-only credentials, perhaps even domain-specific
        bind_dn = conf.get('ldap', 'bind_dn')
        #bind_dn = conf.get('ldap', 'ro_bind_dn')
        bind_pw = conf.get('ldap', 'bind_pw')
        #bind_pw = conf.get('ldap', 'ro_bind_pw')

        if conf.has_option(primary_domain, 'user_base_dn'):
            section = primary_domain
        else:
            section = 'ldap'

        domain_root_dn = self._kolab_domain_root_dn(primary_domain)

        user_base_dn = conf.get_raw(
                section,
                'user_base_dn'
            ) %({'base_dn': domain_root_dn})

        if conf.has_option(primary_domain, 'kolab_user_filter'):
            section = primary_domain
        else:
            section = 'ldap'

        kolab_user_filter = conf.get(section, 'kolab_user_filter', quiet=True)

        if conf.has_option(primary_domain, 'kolab_user_scope'):
            section = primary_domain
        else:
            section = 'ldap'

        _kolab_user_scope = conf.get(section, 'kolab_user_scope', quiet=True)

        if LDAP_SCOPE.has_key(_kolab_user_scope):
            kolab_user_scope = LDAP_SCOPE[_kolab_user_scope]
        else:
            log.warning(_("LDAP Search scope %s not found, using 'sub'") %(_kolab_user_scope))
            kolab_user_scope = ldap.SCOPE_SUBTREE

        # TODO: Is, perhaps, a domain specific setting
        result_attribute = conf.get(
                'cyrus-sasl',
                'result_attribute',
                quiet=True
            )

        self.ldap.simple_bind_s(bind_dn, bind_pw)

        # TODO: The quota and alternative address attributes are actually
        # supposed to be settings.
        _search = self._search(
                user_base_dn,
                kolab_user_scope,
                kolab_user_filter,
                attrlist=[
                        'dn',
                        result_attribute,
                        'sn',
                        'givenname',
                        'cn',
                        'uid'
                    ],
                attrsonly=0,
                callback=callback,
                primary_domain=primary_domain,
                secondary_domains=secondary_domains
            )

        if callback == None:
            log.info(_("Found %d users") %(len(_search)))

            log.debug(_("Iterating over %d users, making sure we have the " + \
                "necessary attributes...") %(len(_search)), level=6)

            users = []

            num_users = len(_search)
            num_user = 0

            for user_dn, user_attrs in _search:
                num_user += 1

                # Placeholder for the user attributes
                user = user_attrs
                user['dn'] = user_dn

                user = self._get_user_details(user, primary_domain, secondary_domains)

                if user:
                    users.append(user)

                if (num_user % 1000) == 0:
                    log.debug(
                            _("Done iterating over user %d of %d")
                                %(num_user,num_users),
                            level=3
                        )

            return users

    def _get_user_details(self, user, primary_domain, secondary_domains=[]):

        # TODO: Is, perhaps, a domain specific setting
        result_attribute = conf.get(
                'cyrus-sasl',
                'result_attribute',
                quiet=True
            )

        if not user.has_key('standard_domain'):
            user['standard_domain'] = (primary_domain, secondary_domains)

        user = utils.normalize(user)

        _get_attrs = []

        for attribute in [
                result_attribute,
                'sn',
                'givenname',
                'cn',
                'uid'
            ]:
            if not user.has_key(attribute):
                _get_attrs.append(attribute)
                #user[attribute] = self._get_user_attribute(user, attribute)

        if len(_get_attrs) > 0:
            _user_attrs = self._get_user_attributes(user, _get_attrs)
            for key in _user_attrs.keys():
                user[key] = _user_attrs[key]
        else:
            return False

        # Check to see if we want to apply a primary mail recipient policy
        if conf.has_option(primary_domain, 'primary_mail'):
            primary_mail = conf.plugins.exec_hook(
                    "set_primary_mail",
                    kw={
                            'primary_mail':
                                conf.get_raw(
                                        primary_domain,
                                        'primary_mail'
                                    ),
                            'user_attrs': user,
                            'primary_domain': primary_domain,
                            'secondary_domains': secondary_domains
                        }
                )

            if not primary_mail == None:
                if not user.has_key('mail'):
                    self._set_user_attribute(user, 'mail', primary_mail)
                    user['mail'] = primary_mail
                else:
                    if not primary_mail == user['mail']:
                        self._set_user_attribute(user, 'mail', primary_mail)
                        user['old_mail'] = user['mail']
                        user['mail'] = primary_mail

            # Check to see if we want to apply a secondary mail recipient
            # policy.
            section = None

            if conf.has_option(primary_domain, 'secondary_mail'):
                section = primary_domain
            elif conf.has_option('kolab', 'secondary_mail'):
                section = 'kolab'

            if not section == None:
                # Execute the plugin hook
                secondary_mail = conf.plugins.exec_hook(
                        "set_secondary_mail",
                        kw={
                                'secondary_mail':
                                    conf.get_raw(
                                            primary_domain,
                                            'secondary_mail'
                                        ),
                                'user_attrs': user,
                                'primary_domain': primary_domain,
                                'secondary_domains': secondary_domains
                            }
                    ) # end of conf.plugins.exec_hook() call

                if not secondary_mail == None:
                    secondary_mail = list(set(secondary_mail))
                    # Avoid duplicates
                    while primary_mail in secondary_mail:
                        secondary_mail.pop(secondary_mail.index(primary_mail))

                    if not user.has_key('mailalternateaddress'):
                        if not len(secondary_mail) == 0:
                            self._set_user_attribute(
                                    user,
                                    'mailalternateaddress',
                                    secondary_mail
                                )

                            user['mailalternateaddress'] = secondary_mail
                    else:
                        if not secondary_mail == user['mailalternateaddress']:
                            self._set_user_attribute(
                                    user,
                                    'mailalternateaddress',
                                    secondary_mail
                                )

                            user['mailalternateaddress'] = secondary_mail

        return user

    def sync_user(self, *args, **kw):
        # See if kw['dn'] has been set.
        if kw.has_key('dn'):
            self.sync_ldap_user(*args, **kw)
        elif kw.has_key('user'):
            for user_dn, user_attrs in kw['user']:
                _user = utils.normalize(user_attrs)
                _user['dn'] = user_dn
                kw['user'] = _user
                self.sync_ldap_user(*args, **kw)
        else:
            # TODO: Not yet implemented
            pass

    def sync_ldap_user(self, *args, **kw):
        user = None

        if kw.has_key('change_type'):
            # This is a EntryChangeControl notification
            user = {'dn': kw['dn']}
            if kw['change_type'] == None:
                # This user has not been changed, but existed already.
                user =  self._get_user_details(user, kw['primary_domain'], kw['secondary_domains'])
            else:
                self._initial_sync_done = True

        if kw.has_key('user'):
            user = kw['user']

        if user:
            pykolab.imap.synchronize(users=[user], primary_domain=kw['primary_domain'], secondary_domains=kw['secondary_domains'])

        if self._initial_sync_done:
            log.debug(_("Initial synchronization is done, now purging old mailboxes"), level=8)
            pykolab.imap.expunge_user_folders(inbox_folders=pykolab.imap.inbox_folders)