LDAP CGI密码重置帮助我

时间:2016-05-20 22:58:43

标签: python ldap cgi

这是我在stackoverflow上的第一篇文章。我有点困在某些代码上。我一直遇到类型错误解码不支持Unicode

我想要构建的是最终用户可以通过门户网站重置他或她的密码。

你能帮我解决一下如何解决这个问题吗? 如果您需要更多信息,请说我很乐意为您提供。

亲切的问候

乔伊

! /usr/bin/python
# _*_ coding: utf-8 _*_
from __future__ import unicode_literals
import cgi
import uuid
import os
import time
import ldap
import sys

def index():
print """Content-type: text/html
         <meta charset="UTF-8">
<head>
</head>

<h1>Reseting password web program</h1>"""
#Declaring variables for the program for later use we could use input   fields.
PASSWORD_ATTR = "utf-8"
username="joey hendricks" # Username for the user we want to change the password for.
user_dn = "CN=%s,OU=Admin,OU=Assengraaf,DC=Assengraaf,DC=nl" % username
password = 'New12345' # New password for the user we want to change the passwordfor.
attr_name = 'mobile'
attr_val = '12345678'
print """<h1>"""+username,password+"""</h1>"""
# Setting up connection tot the LDAP server we use a SSL engrypted connection to the server.
# Without a SSL connection you could run into certain errors the avoid these we use SSL by defaul$
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
conn = ldap.initialize("ldaps://192.168.210.1:636")
conn.set_option(ldap.OPT_REFERRALS, 0)
conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
conn.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
conn.set_option( ldap.OPT_X_TLS_DEMAND, True )
conn.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
conn.simple_bind_s("administrator@assengraaf.nl","password") # Admin credentials to reset the pa$

if conn.compare_s(user_dn, attr_name, attr_val):
     unicode_pass = unicode("\"" + password + "\"", "iso-8859-1")
     password_value = unicode_pass.encode("utf-8")
     add_pass = [(ldap.MOD_REPLACE, PASSWORD_ATTR, [password_value])]
     try:
         conn.modify_s(user_dn, add_pass)
         conn.unbind_s() # Cancel the connection freeing up recources
         print"<p1> Password set succesfull</p1>"

     except ldap.LDAPError, e:
         sys.stderr.write('Error setting AD password for: ' + username + '\n')
         sys.stderr.write('Message: ' + str(e) + '\n')
         sys.exit(1)
         conn.unbind_s() # Cancel the connection freeing up recources
         print"<p2>Error setting password</p2>"

else:
   print"<p3>No match found</p3>"

如果名称 =='主要':     指数()

1 个答案:

答案 0 :(得分:0)

帖子here在Python中显示了正确编码答案的完整示例。这些行尤其令人感兴趣:

password = "jimbo"
password_attr = "unicodePwd"
unicode1 = unicode("\"" + password + "\"", "iso-8859-1")
unicode2 = unicode1.encode("utf-16-le")
password_value = unicode2
mods = [(ldap.MOD_REPLACE, password_attr, [password_value])]