Guest User

Untitled

a guest
Apr 14th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. """
  2. authen_msad
  3. A port of Perl's Apache::AuthenMSAD
  4.  
  5. Takes advantage of Microsoft Active Directory allowing a user to be verified
  6. with 'user@domain' instead of searching for the distinguished name.
  7.  
  8. To merge as seamlessly as possible with existing systems (i.e. SharePoint,
  9. etc.) munge the incoming 'domain\user' into 'user@domain'.
  10.  
  11. Usage:
  12. <Location /directory.to.protect/>
  13. AddHandler mod_python .py
  14. PythonHandler authen_msad
  15. PythonAuthenHandler authen_msad
  16. PythonOption authen_msad.ldap_url ldap://<server ip address>:1234/
  17. AuthType Basic
  18. AuthName "This Content is Protected"
  19. require valid-user
  20. </Location>
  21. """
  22. from mod_python import apache
  23. import ldap
  24.  
  25. def authenhandler(req):
  26. """This function gets called by mod_python to handle Apache's authentication phase"""
  27. try:
  28. LDAP_PASSWORD = req.get_basic_auth_pw()
  29. LDAP_DOMAIN, LDAP_USER = req.user.split("\\")
  30. LDAP_SERVER = req.get_options().get('authen_msad.ldap_url')
  31. except:
  32. return apache.HTTP_UNAUTHORIZED
  33.  
  34. try:
  35. ldap_client = ldap.initialize(LDAP_SERVER)
  36. ldap_client.simple_bind_s("%s@%s" % (LDAP_USER, LDAP_DOMAIN), LDAP_PASSWORD)
  37. return apache.OK
  38. except ldap.INVALID_CREDENTIALS:
  39. return apache.HTTP_UNAUTHORIZED
  40. except ldap.SERVER_DOWN:
  41. return apache.HTTP_EXPECTATION_FAILED
Add Comment
Please, Sign In to add comment