user7098280
user7098280

Reputation: 1

Python LDAP error with spaces:ldap.INVALID_DN_SYNTAX: info -00002081: NameErr: DSID-03050C42

import ldap
import base64
import ldap.modlist as modlist
server = "ldaps://X.X.X.X:636"
who = '[email protected]'
cred = base64.b64decode(XXXXXXXXXXXXXX)
path = 'dc=bru,dc=com'
dn ='CN=saauto\ user8,CN=Users,dc=bru,dc=com'
domain='bru.com'
password = base64.b64decode(b'cmVzZXRwd2RDQVBTQDEyMw==')
firstname='saauto'
surname='user8'
username = firstname+surname
upn = firstname+surname+'@'+domain 
department = 'SME'
country = 'India'
email = firstname+surname+'@'+ domain
manager = 'CN=Administrator,CN=Users,DC=bru,DC=com'
displayname = firstname + surname
city = 'XXXXXXXX'
description = 'Joined on 06-11-2016'
mobile = 'XXXXXXXXXXX'
title = 'SME'
attrs = {}
attrs['objectclass'] = ['top','person','organizationalPerson','user']
attrs['cn'] = str(username)
attrs['userPassword'] = str(password)
attrs['givenName']=str(firstname)
attrs['sn']=str(surname)
attrs['description'] = str(description)
attrs['userPrincipalName'] = str(upn)
attrs['sAMAccountName'] = str(username)
attrs['department'] = str(department)
attrs['c'] = ['IN']
attrs['co'] = str (country)
attrs['l'] = str (city)
attrs['mobile'] = str (mobile)
attrs['title'] = str(title)
attrs['userAccountControl'] = ['544']
attrs['mail']=str (email)
attrs['displayName'] = str(displayname)
attrs['manager'] = str(manager)
add_member = [(ldap.MOD_ADD, 'member', dn)]
mods = [(ldap.MOD_REPLACE, 'unicodePwd', ''.join(('"', password, '"')).encode('utf-16').lstrip('\377\376'),)]
ldif = modlist.addModlist(attrs)
l = ldap.initialize(server)
l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
l.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
l.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
l.simple_bind_s(who, cred)
l.add_s(dn, ldif)
l.modify(dn, mods)
l.unbind_s()

When trying to execute this code getting error as ldap.INVALID_DN_SYNTAX: {'info': "00002081: NameErr: DSID-03050C42, problem 2003 (BAD_ATT_SYNTAX), data 0, best match of:\n\t'CN=s aauto user8,CN=Users,dc=bru,dc=com'\n", 'desc': 'Invalid DN syntax'}

If i remove space in CN part of DN i.e if dn ='CN=saautouser8,CN=Users,dc=bru,dc=com' then it is working. I have also tried backslash. ALso when reading articles it says having space in between names is not problem if it was at beginning or end we need to use escape characters. But i dont know why i am not able to make this work.

Upvotes: 0

Views: 2336

Answers (1)

JBR
JBR

Reputation: 1

As far as I can see you'll have to match both 'cn'-occurences, not only

dn ='CN=saauto\ user8,CN=Users,dc=bru,dc=com' 

If you want to create the user with the "saauto user8" then provide also correct cn-attributes to that user:

attrs['cn'] = str(username) should be replaced with attrs['cn'] = 'saauto user8'

I've dealt with this issue also and this was the way I solved it. I didn't have any problems regarding whitespaces. Hope this helps.

Upvotes: 0

Related Questions