web-dev-qa-db-ja.com

python + ldapを使用したActive Directoryに対する認証

Python + LDAPを使用してADに対して認証するにはどうすればよいですか。現在python-ldapライブラリを使用していますが、生成されるのは涙だけです。

バインドして簡単なクエリを実行することもできません。

import sys
import ldap


Server = "ldap://my-ldap-server"
DN, Secret, un = sys.argv[1:4]

Base = "dc=mydomain,dc=co,dc=uk"
Scope = ldap.SCOPE_SUBTREE
Filter = "(&(objectClass=user)(sAMAccountName="+un+"))"
Attrs = ["displayName"]

l = ldap.initialize(Server)
l.protocol_version = 3
print l.simple_bind_s(DN, Secret)

r = l.search(Base, Scope, Filter, Attrs)
Type,user = l.result(r,60)
Name,Attrs = user[0]
if hasattr(Attrs, 'has_key') and Attrs.has_key('displayName'):
  displayName = Attrs['displayName'][0]
  print displayName

sys.exit()

[email protected] password usernameでこれを実行すると、次の2つのエラーのいずれかが表示されます。

Invalid Credentials-入力を間違えたり、間違った資格情報を意図的に使用すると、認証に失敗します。

ldap.INVALID_CREDENTIALS:{'info': '80090308:LdapErr:DSID-0C090334、comment:AcceptSecurityContext error、data 52e、vece'、 'desc': 'Invalid credentials'}

または

ldap.OPERATIONS_ERROR:{'info': '00000000:LdapErr:DSID-0C090627、comment:この操作を実行するには、接続で成功したバインドを完了する必要があります。データ0、vece'、 'desc': '操作エラー'}

適切にバインドするために何が欠けていますか?

FedoraとWindowsで同じエラーが発生します。

85
1729

行方不明でした

l.set_option(ldap.OPT_REFERRALS, 0)

初期化から。

44
1729

Pywin32を使用できる場合は、PythonからWin32呼び出しを使用できます。これは、CherryPy Webサーバーで行うことです。

import win32security
token = win32security.LogonUser(
    username,
    domain,
    password,
    win32security.LOGON32_LOGON_NETWORK,
    win32security.LOGON32_PROVIDER_DEFAULT)
authenticated = bool(token)
25
davidavr

それは私のために働いた、l.set_option(ldap.OPT_REFERRALS、0)はActiveDirectoryにアクセスするためのキーでした。さらに、スクリプトを終了する前に接続を閉じるために「con.unbind()」を追加する必要があると思います。

7
alfredocambera

私に役立ついくつかの簡単なコードを次に示します。

import ldap  # run 'pip install python-ldap' to install ldap module.
conn = ldap.open("ldaphost.company.com")
conn.simple_bind_s("[email protected]", "mypassword")

これは 前の回答 に基づいています。

5
JohnMudd

kerberosをインストールしてADと通信している場合、たとえばCentrify Expressをインストールして実行している場合は、python-kerberosを使用するだけです。例えば。

import kerberos
kerberos.checkPassword('joe','pizza','krbtgt/x.pizza.com','X.PIZZA.COM')`

kerberosレルムX.PIZZA.COMでユーザー「joe」がパスワード「pizza」を持っている場合、Trueを返します。 (通常、後者はADドメインの名前と同じだと思います)

3
Dima Pasechnik

DNが問題を解決していないというコメントを@Johan Buretに見ますが、それもあなたが検討すべきだと思います。

例では、ADのデフォルトの管理者アカウントのDNは次のようになります。cn= Administrator、cn = Users、dc = mydomain、dc = co、dc = uk-それを試してください。

2
Daniel Bungert

追加しようとしました

l.set_option(ldap.OPT_REFERRALS、0)

しかし、エラーの代わりにPythonがハングし、これ以上何にも応答しません。検索クエリを間違って作成している可能性があります。検索の基本部分は何ですか?シンプルバインドのDNと同じです(ああ、私はl.simple_bind、 の代わりに l.simple_bind_s):

import ldap
local = ldap.initialize("ldap://127.0.0.1")
local.simple_bind("CN=staff,DC=mydomain,DC=com")
#my pc is not actually connected to this domain 
result_id = local.search("CN=staff,DC=mydomain,DC=com", ldap.SCOPE_SUBTREE, "cn=foobar", None)
local.set_option(ldap.OPT_REFERRALS, 0)
result_type, result_data = local.result(result_id, 0)

AD LDSを使用していますが、インスタンスは現在のアカウントに登録されています。

1
lanoxx

私は同じ問題を抱えていましたが、パスワードのエンコードに関するものでした

.encode('iso-8859-1')

問題を解決しました。

1
Dr.Ü

優れた ldap3チュートリアル に基づく:

>>> from ldap3 import Server, Connection, ALL, NTLM
>>> server = Server('server_name_or_ip', get_info=ALL)
>>> conn = Connection(server, user="user_name", password="password", auto_bind=True)
>>> conn.extend.standard.who_am_i()
>>> server.info

Python3で上記を行いましたが、Python 2と互換性があるはずです。

1
Nagev

私にとっては、simple_bind_s()からbind()に変更することでうまくいきました。

0
xcl

識別名を使用してシステムにログオンします。"CN=Your user,CN=Users,DC=b2t,DC=local" ADを含むすべてのLDAPシステムで動作するはずです

0
Johan Buret