ZLDAP.py 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
""" 
   An LDAP connection product.  Depends on David Leonard's ldapmodule.
   $Id: ZLDAP.py,v 1.11 2000/12/18 22:17:50 jeffrey Exp $
   Started by Anthony Baxter, <anthony@interlink.com.au>
   Continued by the folks @ CodeIt <http://www.codeit.com/>

   Now by Jeffrey P Shell <jeffrey@Digicool.com>.
"""

__version__ = "$Revision: 1.11 $"[11:-2]

import Acquisition, AccessControl, OFS, string
13
from Acquisition import aq_base
14 15 16
from App.special_dtml import HTMLFile
from App.Dialogs import MessageDialog
from Persistence import Persistent
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
import ldap, urllib

import LDCAccessors
from Entry import ZopeEntry, GenericEntry, TransactionalEntry
ConnectionError='ZLDAP Connection Error'

manage_addZLDAPConnectionForm = HTMLFile('add', globals())

class NoBrainer:
    """ Empty class for mixin to EntryFactory """

class ZLDAPConnection(
    Acquisition.Implicit,
    Persistent, OFS.SimpleItem.Item,
    LDCAccessors.LDAPConnectionAccessors,
    AccessControl.Role.RoleManager):
    '''LDAP Connection Object'''

    isPrincipiaFolderish=1

    meta_type='LDAP Connection'

    manage_options=(
        {'label':'Connection Properties','action':'manage_main'},
        {'label':'Open/Close','action':'manage_connection'},
        {'label':'Browse', 'action':'manage_browse'},
        {'label':'Security','action':'manage_access'},
        )

    __ac_permissions__=(
        ('Access contents information',
         ('canBrowse',),),
        ('View management screens',('manage_tabs','manage_main'),
         ('Manager',)),
        ('Edit connection',('manage_edit',),('Manager',)),
        ('Change permissions',('manage_access',)),
        ('Open/Close Connection',('manage_connection',
                                  'manage_open','manage_close',),
         ('Manager',)),
        ('Browse Connection Entries', ('manage_browse',),('Manager',),),
        )

    manage_browse=HTMLFile('browse',globals())
    manage_connection=HTMLFile('connection',globals())

    ### dealing with browseability on the root.
    def canBrowse(self):
        """ Returns true if the connection is open *and* the '_canBrowse'
        property is set to true """
        return self.shouldBeOpen() and self.getBrowsable()


    ### constructor
    def __init__(self, id, title, host, port, basedn, bind_as, pw, openc,
                 transactional=1):
        "init method"
        self._v_conn = None
        self._v_delete = []
        self._v_openc = openc
        self.openc = openc
        self.setId(id)
        self.setTitle(title)
        self.setHost(host)
        self.setPort(port)
        self.setBindAs(bind_as)
        self.setPW(pw)
        self.setDN(basedn)
        self.setOpenConnection(openc)
        self.setTransactional(transactional)

        # if connection is specified to be open, open it up
        if openc: self._open()

    ### upgrade path...
    def __setstate__(self, state):
        # Makes sure we have an Entry class now
        self._refreshEntryClass()
        Persistent.__setstate__(self, state)

    ### Entry Factory stuff
    def _refreshEntryClass(self):
        """ This class sets up the Entry class used to return results. """
        transactional = self.getTransactional()
        if transactional:
            EntryBase = TransactionalEntry
        else:
            EntryBase = GenericEntry

        class LdapEntry(EntryBase, ZopeEntry):
            pass

        self._v_entryclass = LdapEntry
        return LdapEntry

    def _EntryFactory(self):
        """ Stamps out an Entry class to be used for every entry returned,
        taking into account transactional versus non-transactional """
114
        return getattr(aq_base(self), '_v_entryclass', self._refreshEntryClass())
115 116 117 118

    ### Tree stuff
    def __bobo_traverse__(self, REQUEST, key):
        key=urllib.unquote(key)
119
        if getattr(self, key, None) is not None:
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
            return getattr(self, key)
        return self.getRoot()[key]

    def tpId(self):
        return self.id

    def tpURL(self):
        return self.id

    def tpValues(self):
        if self.canBrowse():
            return self.getRoot().tpValues()
        else:
            return []

    #### TransactionalObjectManager stuff #####
    def tpc_begin(self,*ignored):
        #make sure we're open!
        if not self.__ping():      #we're not open
            raise (ConnectionError,
                   'LDAP Connection is not open for commiting')
        self._v_okobjects=[]

    def commit(self, o, *ignored):
        ' o = object to commit '
        # check to see if object exists
        oko=[]
        if self.hasEntry(o.dn):
            oko.append(o)
        elif o._isNew or o._isDeleted:
            oko.append(o)
        self._v_okobjects=oko
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
    def tpc_finish(self, *ignored):
        " really really commit and DON'T FAIL "
        oko=self._v_okobjects
        self._isCommitting=1
        d=getattr(self,'_v_delete',[])

        for deldn in d: self._deleteEntry(deldn)
        self._v_delete=[]

        for o in oko:
            try:
                if o._isDeleted:
                    pass
                    # we shouldn't need to do anything now that
                    # the mass delete has happened
                elif o._isNew:
                    self._addEntry(o.dn, o._data.items())
                    o._isNew=0
                    del self._v_add[o.dn]
                else:
                    o._modify()
                o._registered=0
            except:
                pass    #XXX We should log errors here

        del self._v_okobjects
        del self._isCommitting
        self.GetConnection().destroy_cache()

    def tpc_abort(self, *ignored):
        " really really rollback and DON'T FAIL "
        try:
            self._abort()
        except:
            pass        #XXX We should also log errors here

    def abort(self, o, *ignored):
        if o.dn in getattr(self,'_v_delete',()):
            self._v_delete.remove(o.dn)
        if o._isDeleted: o.undelete()
        o._rollback()
        o._registered=0
        if o._isNew:
            if o.dn in getattr(self,'_v_add',{}).keys():
                del self._v_add[o.dn]
        self.GetConnection().destroy_cache()
        
    def _abort(self):
        oko=self._v_okobjects
        for o in oko:
            self.abort(o)
        self.GetConnection().destroy_cache()
            
    def tpc_vote(self, *ignored):
        pass


    ### getting entries and attributes

    def hasEntry(self, dn):
        if getattr(self, '_v_add',{}).has_key(dn):
            #object is marked for adding
            return 1
        elif dn in getattr(self,'_v_delete',()):
            #object is marked for deletion
            return 0

        try:
            e=self._connection().search_s(dn, ldap.SCOPE_BASE,
                                          'objectclass=*')
            if e: return 1
        except ldap.NO_SUCH_OBJECT:
            return 0
        return 0

    def getRawEntry(self, dn):
        " return raw entry from LDAP module "
        if getattr(self, '_v_add',{}).has_key(dn):
            return (dn, self._v_add[dn]._data)
        elif dn in getattr(self,'_v_delete',()):
            raise ldap.NO_SUCH_OBJECT, "Entry '%s' has been deleted" % dn

        try:
            e=self._connection().search_s(
                dn, ldap.SCOPE_BASE, 'objectclass=*'
                )
            if e: return e[0]
        except:
            raise ldap.NO_SUCH_OBJECT, "Cannot retrieve entry '%s'" % dn


    def getEntry(self, dn, o=None):
        " return **unwrapped** Entry object, unless o is specified "
        Entry = self._EntryFactory()

        if getattr(self, '_v_add',{}).has_key(dn):
            e=self._v_add[dn]
        else:
            e=self.getRawEntry(dn)
            e=Entry(e[0],e[1],self)

        if o is not None:
            return e.__of__(o)
        else:
            return e

    def getRoot(self):
        " return root entry object "
        return self.getEntry(self.dn, self)

    def getAttributes(self, dn):
        " get raw attributes from entry from LDAP module "
        return self.getRawEntry(dn)[1]

    ### listing subentries

    def getRawSubEntries(self, dn):
        " get the raw entry objects of entry dn's immediate children "
        # XXX Do something soon to account for added but noncommited..?
        if dn in getattr(self,'_v_delete',()):
            raise ldap.NO_SUCH_OBJECT
        results=self._connection().search_s(
            dn, ldap.SCOPE_ONELEVEL, 'objectclass=*')
        r=[]
        for entry in results:
            #make sure that the subentry isn't marked for deletion
            if entry[0] not in getattr(self, '_v_delete',()):
                r.append(entry)
        return r

    def getSubEntries(self, dn, o=None):
        Entry = self._EntryFactory()
        
        r=[]
        se=self.getRawSubEntries(dn)

        for entry in se:
            e=Entry(entry[0],entry[1],self)
            if o is not None:
                e=e.__of__(o)
            r.append(e)

        return r

    ### modifying entries
    def _modifyEntry(self, dn, modlist):
        if not getattr(self,'_isCommitting',0):
            raise AccessError, 'Cannot modify unless in a commit'
            #someone's trying to be sneaky and modify an object
            #outside of a commit.  We're not going to allow that!
        c=self._connection()
        c.modify_s(dn, modlist)
        
    ### deleting entries
    def _registerDelete(self, dn):
        " register DN for deletion "
        d=getattr(self,'_v_delete',[])
        if dn not in d:
            d.append(dn)
        self._v_delete=d

    def _unregisterDelete(self, dn):
        " unregister DN for deletion "
        d=getattr(self, '_v_delete',[])
        if dn in d: d.remove(dn)
        self._v_delete=d

        self._unregisterAdd(dn)

    def _deleteEntry(self, dn):
        if not getattr(self, '_isCommitting',0):
            raise AccessError, 'Cannot delete unless in a commit'
        c=self._connection()
        c.delete_s(dn)

    ### adding entries
    def _registerAdd(self, o):
        a=getattr(self, '_v_add',{})
        if not a.has_key(o.dn):
            a[o.dn]=o
        self._v_add=a

    def _unregisterAdd(self, o=None, dn=None):
        a=getattr(self, '_v_add',{})
        if o and o in a.values():
            del a[o.dn]
        elif dn and a.has_key(dn):
            del a[dn]
        self._v_add=a

    def _addEntry(self, dn, attrs):
        if not getattr(self, '_isCommitting',0):
            raise AccessError, 'Cannot add unless in a commit'
        c=self._connection()
        c.add_s(dn, attrs)
        
    ### other stuff
    def title_and_id(self):
        "title and id, with conn state"
        s=ZLDAPConnection.inheritedAttribute('title_and_id')(self)
        if self.shouldBeOpen():
            s="%s (connected)" % s
        else:
            s='%s (<font color="red"> not connected</font>)' % s
        return s


    ### connection checking stuff

    def _connection(self):
363
        if self.getOpenConnection():
364 365 366 367 368 369 370
            if not self.isOpen(): self._open()
            return self._v_conn
        else:
            raise ConnectionError, 'Connection Closed'

    GetConnection=_connection

371 372 373 374 375 376 377
    def getForcedConnection(self):
        if self.getOpenConnection():
            self._open()
            return self._v_conn
        else:
            raise ConnectionError, 'Connection Closed'

378 379
    def isOpen(self):
        " quickly checks to see if the connection's open "
380
        if getattr(aq_base(self), '_v_conn', None) is None:
381 382 383 384 385 386 387 388 389
            self._v_conn = None
        if self._v_conn is None or not self.shouldBeOpen():
            return 0
        else:
            return 1

    def __ping(self):
        " more expensive check on the connection and validity of conn "
        try:
390
            self._connection().whoami_s()
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
            return 1
        except:
            self._close()
            return 0

    def _open(self):
        """ open a connection """
        try:
            self._close()
        except:
            pass
        self._v_conn = ldap.open(self.host, self.port)
        #Nicolas the version of pythonldap doesn't use the enable_cache method
        #self._v_conn.enable_cache()
        try:
            self._v_conn.simple_bind_s(self.bind_as, self.pw)
        except ldap.NO_SUCH_OBJECT:
            return """
   Error: LDAP Server returned `no such object' for %s. Possibly 
   the bind string or password are incorrect"""%(self.bind_as)
        self._v_openc = 1

    def manage_open(self, REQUEST=None):
        """ open a connection. """
        self.setOpenConnection(1)
        ret = self._open()
        if not getattr(self, '_v_openc', 0):
            return ret
        if REQUEST is not None:
            m='Connection has been opened.'
            return self.manage_connection(self,REQUEST,manage_tabs_message=m)

    def _close(self):
        """ close a connection """
425
        if self.getOpenConnection() is None:
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
            #I'm already closed, but someone is still trying to close me
            self._v_conn = None
            self._v_openc = 0
        else:
            try: self._v_conn.unbind_s()
            except AttributeError: pass
            self._v_conn = None
            self._v_openc = 0

    def manage_close(self, REQUEST=None):
        """ close a connection. """
        self._close()
        if REQUEST is not None:
            m='Connection has been closed.'
            return self.manage_connection(self,REQUEST,manage_tabs_message=m)

    def manage_clearcache(self, REQUEST=None):
        """ clear the cache """
        self._connection().destroy_cache()
        if REQUEST is not None:
            m='Cache has been cleared.'
            return self.manage_connection(self,REQUEST,manage_tabs_message=m)


    manage_main=HTMLFile("edit",globals())

    def manage_edit(self, title, hostport, basedn, bind_as, pw, openc=0,
                    canBrowse=0, transactional=1, REQUEST=None):
        """ handle changes to a connection """
        self.title = title
        host, port = splitHostPort(hostport)
        if self.host != host:
            self._close()
            self.setHost(host)
        if self.port != port:
            self._close()
            self.setPort(port)
        if self.bind_as != bind_as:
            self._close()
            self.setBindAs(bind_as)
        if self.pw != pw:
            self._close()
            self.setPW(pw)
        if openc and not self.getOpenConnection():
            self.setOpenConnection(1)
            ret = self._open()
            if not self._v_openc:
                return ret
        if not openc and self.getOpenConnection():
            self.setOpenConnection(0)
            self._close()

        self.setBrowsable(canBrowse)
        self.setTransactional(transactional)
        self.setDN(basedn)

        if REQUEST is not None:
            return MessageDialog(
                title='Edited',
                message='<strong>%s</strong> has been edited.' % self.id,
                action ='./manage_main',
                )

    def _isAnLDAPConnection(self):
        return 1
    
def splitHostPort(hostport):
    import string
    l = string.split(hostport,':')
    host = l[0]
    if len(l) == 1:
        port = 389
    else:
        port = string.atoi(l[1])
    return host, port


def manage_addZLDAPConnection(self, id, title, hostport,
504
                              basedn, bind_as, pw, openc=None,
505 506 507 508 509 510 511
                              REQUEST=None):
    """create an LDAP connection and install it"""
    host, port = splitHostPort(hostport)
    conn = ZLDAPConnection(id, title, host, port, basedn, bind_as, pw, openc)
    self._setObject(id, conn)
    if REQUEST is not None:
        return self.manage_main(self, REQUEST)