[Answered ]-LDAP gidNumber like Auto Integer

1đź‘Ť

âś…

The LDAP protocol has no method for auto-integer.

You need to specify the value when creating the entry.

You can do some tricks to help.
We often put the last used value on an OU (We add an AUX class with custom Attribute to the OU) in LDAP and then read, increment and then use the value when using the gidNumber.

Found this described.
-jim

👤jwilleke

1đź‘Ť

Following @jeemster suggestion, i found the way to manage gidNumber.

Fist of all: i created a new entry on my LDAP called “gidNumber” and i added the optional attribute description to contain the last gidNumber i used (class: organizationalUnit, ou: gidNumber, description: 500).

Then i created the following functions:

def ldap_gid_finder(self):

        # Locates the suport-entry with a simple query
        self.baseDN = "ou=impianti,dc=ldap,dc=dem2m,dc=it"
        self.searchScope = ldap.SCOPE_SUBTREE
        self.retrieveAttributes = None
        self.searchFilter = "ou=*gidNumber*"

        # Results are putted in a dictionary
        self.ldap_result = self.connector.search(
            self.baseDN, self.searchScope, self.searchFilter, self.retrieveAttributes)
        result_set = []
        while 1:
            result_type, result_data = self.connector.result(self.ldap_result, 0)
            if (result_data == []):
                break
            else:
                if result_type == ldap.RES_SEARCH_ENTRY:
                    result_set.append(result_data)

        # The attribute containing gidNumber is passed to an instanced variable
        self.actual_gid_number = int(result_set[0][0][1]['description'][0])

    # Provides to gidNumber incrementation
    def ldap_gid_increment(self):

        dn = "ou=gidNumber,ou=impianti,dc=ldap,dc=dem2m,dc=it"

        old = {'description': str(self.actual_gid_number)}
        new = {'description': str(self.actual_gid_number + 1)}

        ldif = modlist.modifyModlist(old,new)

        self.connector.modify_s(dn, ldif)

As i sad above, these methods are defined in a class of which i overrided constructor and destructor, in order to bind/unbind automatically to LDAP server when i instance or delete the instance.

Then, i used a query on LDAP to find the object called gidNumber (the ou i created before), and i filled a dictionary with resulting information. In the dictionary i found the variable representing the gidNumber and i used integer casting to manipulate it for incrementing. And that’s all.

This procedure i really efficent because i server reboots you don’t lose gidNumber information! Thank you again, jeemster.

Leave a comment