Python - 添加一个具有“userPassword”和“userAccountControl”定义的AD用户返回LDAP 53 - “不愿意执行”

问题描述:

前言,我只用了约5个月的Python。我一直在努力编写一个程序,它将(最终)完成批量用户的创建。当格式如下所示时,它将成功创建一个新的用户对象,但“userAccountControl”属性将默认为546,ACCOUNTDISABLE | PASSWD_NOTREQD | NORMAL_ACCOUNT,并且“userPass”的值将在AD的对象的属性编辑器中以明文形式显示为八位字节字符串。该程序正在使用LDAP3库:https://pypi.python.org/pypi/ldap3Python - 添加一个具有“userPassword”和“userAccountControl”定义的AD用户返回LDAP 53 - “不愿意执行”

class fromconfig: 
    def __init__(self): 
     Config = configparser.ConfigParser() 
     Config.read("config.ini") 
     self.serverip = Config.get('serverinfo', 'ip') 
     self.basepath = Config.get('serverinfo', 'base') 
     self.container = Config.get('serverinfo', 'container') 
     self.dc1 = Config.get('serverinfo', 'dc1') 
     self.dc2 = Config.get('serverinfo', 'dc2') 
     self.ou = Config.get('serverinfo', 'ou') 

def add_user(username, givenname, surname, userPrincipalName, SAMAccountName, userPassword): 

    ad_server = Server(config.serverip, use_ssl=True, get_info=ALL) 

    ad_c = Connection(ad_server, user='domain\\user', password='password', authentication=NTLM) 

    if ad_c.bind(): 
     ad_c.add('cn={},cn={},dc={},dc={}'.format(username, config.ou, config.dc1, config.dc2), ['person', 'user'], {'givenName': givenname, 'sn': surname, 'userPrincipalName': userPrincipalName, 'sAMAccountName': SAMAccountName, 'userPassword': userPassword}) 
     print(ad_c.result) 

    ad_c.unbind() 

我希望能够在程序中定义一个512值userAccountControl的,否则成功启用该帐户,这样我就不必通过回去稍后在AD中取消选中“帐户已禁用”。当我尝试通过它时,ad_c.result返回错误53.这是我通过AD进入并尝试直接修改该属性时收到的错误,或取消选中禁用帐户复选框。 AD服务器上的错误53的对话框显示“密码不符合长度或复杂性要求”,但我用于测试的密码是我以前在AD上使用过的密码,没有任何问题。所以我认为这个问题与如何存储userPassword而不是复杂性或权限有关。

我相信我已经知道了,这要归功于来自this的示例。在以后手动启用帐户时,为属性字典中的“userPassword”键定义值将不会起作用。相反,在添加用户帐户后,您可以使用扩展操作来解锁帐户,修改密码,然后使用所需的值更新“userAccountControl”属性(在这种情况下,我希望512:NORMAL_ACCOUNT)。

这是非常混乱的,但幸运的是它的工作原理,所以现在我要开始重构程序的其余部分,以便它看起来并不可怕

(continued from above) 
    ad_c.add(...) 
    ad_c.extend.microsoft.unlock_account(user='cn={},cn={},dc={},dc={}'.format(username, config.container, config.dc1, config.dc2)) 
    ad_c.extend.microsoft.modify_password(user='cn={},cn={},dc={},dc={}'.format(username, config.container, config.dc1, config.dc2), new_password=userpassword, old_password=None) 
    changeUACattribute = {"userAccountControl": (MODIFY_REPLACE, [512])} 
    ad_c.modify('cn={},cn={},dc={},dc={}'.format(username, config.container, config.dc1, config.dc2), changes=changeUACattribute)