]> git.ipfire.org Git - ipfire.org.git/commitdiff
accounts: Bind to LDAP after reconnect
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 18 May 2019 12:02:38 +0000 (13:02 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 18 May 2019 12:03:31 +0000 (13:03 +0100)
This change makes sure that we are always authenticated
against the LDAP server before performing any write operations

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/accounts.py

index 8c00a35a85db0e3f18d6bdf570d9a439f3d336af..d25bbcb79aa254f506410b3c86fac167c4d320f0 100644 (file)
@@ -35,16 +35,15 @@ class Accounts(Object):
                logging.debug("Connecting to LDAP server: %s" % ldap_uri)
 
                # Connect to the LDAP server
-               conn = ldap.ldapobject.ReconnectLDAPObject(ldap_uri,
+               return ldap.ldapobject.ReconnectLDAPObject(ldap_uri,
                        retry_max=10, retry_delay=3)
 
+       def _authenticate(self):
                # Bind with username and password
-               bind_dn = self.settings.get("ldap_bind_dn")
-               if bind_dn:
-                       bind_pw = self.settings.get("ldap_bind_pw", "")
-                       conn.simple_bind(bind_dn, bind_pw)
-
-               return conn
+               self.ldap.simple_bind(
+                       self.settings.get("ldap_bind_dn"),
+                       self.settings.get("ldap_bind_pw", ""),
+               )
 
        def _query(self, query, attrlist=None, limit=0, search_base=None):
                logging.debug("Performing LDAP query: %s" % query)
@@ -204,6 +203,7 @@ class Accounts(Object):
                dn = "uid=%s,ou=People,dc=mcfly,dc=local" % uid
 
                # Create account on LDAP
+               self.accounts._authenticate()
                self.ldap.add_s(dn, ldap.modlist.addModlist(account))
 
                # Return account
@@ -314,6 +314,9 @@ class Account(Object):
        def _modify(self, modlist):
                logging.debug("Modifying %s: %s" % (self.dn, modlist))
 
+               # Authenticate before performing any write operations
+               self.accounts._authenticate()
+
                # Run modify operation
                self.ldap.modify_s(self.dn, modlist)