summaryrefslogtreecommitdiffstats
path: root/pykolab/auth/__init__.py
blob: 66eb99390b6c3c31079e519791384a3348ffbbac (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
# Copyright 2010-2012 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 logging
import os
import time

import pykolab

from pykolab.translate import _

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

class Auth(object):
    """
        This is the Authentication and Authorization module for PyKolab.
    """

    def __init__(self):
        """
            Initialize the authentication class.

            self._auth is the placeholder for domain-specific authentication
            backends. The keys are the primary domain names for each domain.
        """
        self._auth = None

        # Placeholder mapping back to the primary domain name space
        self.secondary_domains = {}

    def authenticate(self, login):
        """
            Verify login credentials supplied in login against the appropriate
            authentication backend.

            Login is a simple list of username, password, service and,
            optionally, the realm.
        """

        if len(login) == 3:
            # The realm has not been specified. See if we know whether or not
            # to use virtual_domains, as this may be a cause for the realm not
            # having been specified seperately.
            use_virtual_domains = conf.get('imap', 'virtual_domains')

            # TODO: Insert debug statements
            #if use_virtual_domains == "userid":
                #print "# Derive domain from login[0]"
            #elif not use_virtual_domains:
                #print "# Explicitly do not user virtual domains??"
            #else:
                ## Do use virtual domains, derive domain from login[0]
                #print "# Derive domain from login[0]"

        if len(login[0].split('@')) > 1:
            domain = login[0].split('@')[1]
        else:
            domain = conf.get("kolab", "primary_domain")

        # realm overrides domain
        if len(login) == 4:
            domain = login[3]

        self.connect(domain)

        retval = self._auth._authenticate(login, domain)

        return retval

    def connect(self, domain=None):
        """
            Connect to the domain authentication backend using domain, or fall
            back to the primary domain specified by the configuration.
        """

        if not self._auth == None:
            return

        if domain == None:
            section = 'kolab'
            domain = conf.get('kolab', 'primary_domain')
        else:
            self.list_domains()
            section = domain

        if self.secondary_domains.has_key(domain):
            section = self.secondary_domains[domain]
            domain = self.secondary_domains[domain]

        log.debug(
                _("Connecting to Authentication backend for domain %s") %(
                        domain
                    ),
                level=8
            )

        if not conf.has_section(section):
            section = 'kolab'

        if not conf.has_option(section, 'auth_mechanism'):
            section = 'kolab'

        if conf.get(section, 'auth_mechanism') == 'ldap':
            from pykolab.auth import ldap
            self._auth = ldap.LDAP()
        elif conf.get(section, 'auth_mechanism') == 'sql':
            from pykolab.auth import sql
            self._auth = sql.SQL()

        else:
            from pykolab.auth import ldap
            self._auth = ldap.LDAP()

    def disconnect(self, domain=None):
        """
            Connect to the domain authentication backend using domain, or fall
            back to the primary domain specified by the configuration.
        """

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

        if not self._auth or self._auth == None:
            return

        self._auth._disconnect()

    def find_user(self, attr, value, domain=None, **kw):
        self.connect(domain)

        if self.secondary_domains.has_key(domain):
            log.debug(
                    _("Using primary domain %s instead of secondary domain %s")
                    %(
                            self.secondary_domains[domain],
                            domain
                        ),
                    level=9
                )

            domain = self.secondary_domains[domain]

        return self._auth._find_user(attr, value, domain=domain, **kw)

    def search_users(self, attr, value, domain=None, **kw):
        self.connect(domain)

        if self.secondary_domains.has_key(domain):
            log.debug(
                    _("Using primary domain %s instead of secondary domain %s")
                    %(
                            self.secondary_domains[domain],
                            domain
                        ),
                    level=9
                )

            domain = self.secondary_domains[domain]

        return self._auth._search_users(attr, value, domain=domain, **kw)

    def list_domains(self):
        """
            List the domains using the auth_mechanism setting in the kolab
            section of the configuration file, either ldap or sql or (...).

            The actual setting would be used by self.connect(), and stuffed
            into self._auth, for use with self._auth._list_domains()

            For each domain found, returns a two-part tuple of the primary
            domain and a list of secondary domains (aliases).
        """

        # Connect to the global namespace
        self.connect()

        # Find the domains in the authentication backend.
        kolab_primary_domain = conf.get('kolab', 'primary_domain')

        domains = self._auth._list_domains()

        # If no domains are found, the primary domain is used.
        if len(domains) < 1:
            domains = [(kolab_primary_domain, [])]
        else:
            for primary, secondaries in domains:
                for secondary in secondaries:
                    self.secondary_domains[secondary] = primary

        return domains

    def list_users(self, primary_domain, secondary_domains=[], callback=None):
        self.connect(domain=primary_domain)
        users = self._auth._list_users(
                primary_domain,
                secondary_domains,
                callback
            )
        self.disconnect(domain=primary_domain)
        return users

    def synchronize(self, primary_domain, secondary_domains=[]):
        self.connect(domain=primary_domain)
        self.list_users(
                primary_domain,
                secondary_domains,
                callback=self._auth.sync_user
            )

    def domain_default_quota(self, domain):
        self.connect(domain=domain)

        if self.secondary_domains.has_key(domain):
            domain = self.secondary_domains[domain]

        return self._auth._domain_default_quota(domain)

    def domain_section(self, domain):
        self.connect(domain=domain)

        if self.secondary_domains.has_key(domain):
            domain = self.secondary_domains[domain]

        return self._auth._domain_section(domain)

    def get_user_attribute(self, domain, user, attribute):
        self.connect(domain=domain)

        if self.secondary_domains.has_key(domain):
            domain = self.secondary_domains[domain]

        return self._auth._get_user_attribute(user, attribute)

    def get_user_attributes(self, domain, user, attributes):
        self.connect(domain=domain)

        if self.secondary_domains.has_key(domain):
            domain = self.secondary_domains[domain]

        return self._auth._get_user_attributes(user, attributes)

    def search_mail_address(self, domain, mail_address):
        self.connect(domain=domain)

        if self.secondary_domains.has_key(domain):
            domain = self.secondary_domains[domain]

        return self._auth._search_mail_address(domain, mail_address)

    def set_user_attribute(self, domain, user, attribute, value):
        self.connect(domain=domain)

        if self.secondary_domains.has_key(domain):
            domain = self.secondary_domains[domain]

        self._auth._set_user_attribute(user, attribute, value)