ActiveDirectoryのLDAP認証もどき

python-ldapを使ってみた。

ついでに、以前から試そうと思って手をつけていなかった、ActiveDirectoryのLDAP認証も。試しに、bindしてみるだけ。平文でパスワードが流れるのでこのままではちょっと使えない。

#!/usr/bin/env python
# -*- coding: utf-8-*-
# python-ldapを使ってActiveDirectoryで認証してみる
# http://python-ldap.sourceforge.net/

def ADAuth(username,password,host,port=389):
    """ActiveDirectoryのドメイコントローラにユーザ名とパスワードでbindしてみる。
    bindできれば認証OK。認証NGなら例外が起きる。
    """
    import ldap
    url = "ldap://%s:%d" % (host,port)
    l = ldap.initialize(url)
    l.simple_bind_s(username,password)
    l.unbind_s()

if __name__=='__main__':
    if len(sys.argv)<3:
        print "Usage: %s username password" % (sys.argv[0])
    else:
        try:
            userid = sys.argv[1]
            password = sys.argv[2]
            ADAuth(userid,password,"adserver.example.com")
            print "OK"
        except:
            print "NG"

Gist:227565

Related posts:

  1. Pythonでモジュラス10ウエイト3とisbn10to13

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>