I have an error when I try to create a new user in my active directory with python ldap. The connection to the server works since the search function works. Here is my code, I apologize in advance if several elements are mixed up, I have tried a lot of things since different tutorial:
import ldap
import ldap.modlist as modlist
ldap_client = ldap.initialize('ldap://serverName')
ldap_client.set_option(ldap.OPT_REFERRALS, 0)
ldap_client.simple_bind_s('root@domain', 'password')
baseDN = "OU=ou_where_create,OU=users,OU=tree_of_ou,OU=FIM,DC=domain,DC=LAN"
sam = 'TTEST'
cn = "TEST Toto"
sn = "TEST"
givenname = "Toto"
mail = "TTEST"
userPrincipalName = '[email protected]'
name = "TEST Toto"
displayName = "TEST Toto"
# user_dn = 'CN=' + sn + ' ' + givenname + ',' + baseDN
user_dn = "CN=TOTOTest,OU=ou_where_create,OU=users,OU=tree_of_ou,OU=FIM,DC=domain,DC=LAN"
user_attrs = {}
user_attrs['objectclass'] = [b'top', b'person', b'organizationalPerson', b'user']
user_attrs['cn'] = [b'cn']
user_attrs['givenName'] = [b'givenname']
user_attrs['sn'] = [b'sn']
user_attrs['displayName'] = [b'displayName']
user_attrs['mail'] = [b'mail']
user_attrs['userPrincipalName'] = [b'userPrincipalName']
user_attrs['sAMAccountname'] = [b'sam']
user_attrs['name'] = [b'name']
user_ldif = modlist.addModlist(user_attrs)
ldap_client.add_s(user_dn, user_ldif)
this code gives me the error:
ldap.INVALID_DN_SYNTAX: {'msgtype': 105, 'msgid': 2, 'result': 34, 'desc': 'Invalid DN syntax', 'ctrls': [], 'info': "00002081: NameErr: DSID-03050C55, problem 2003 (BAD_ATT_SYNTAX), data 0, best match of:\n\t'CN=TOTO Test,OU=ou_where_create,OU=users,OU=tree_of_ou,OU=FIM,DC=domain,DC=LAN'\n"}
I tried with other syntax for my attributes, without list [], without byte but with .encode(utf8)... but the error is no longer the same. The problem must come from my user_dn but I don't see what. Thanking you in advance for any help or clarification you can provide. regards,
CN=part should be exactly the same.nameattribute. It will get set from the value incn.