initialize.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. Various examples how to connect to a LDAP host with the new
  3. factory function ldap.initialize() introduced in OpenLDAP 2 API.
  4. Assuming you have LDAP servers running on
  5. ldap://localhost:1390 (LDAP with StartTLS)
  6. ldaps://localhost:1391 (LDAP over SSL)
  7. ldapi://%2ftmp%2fopenldap2 (domain socket /tmp/openldap2)
  8. """
  9. import sys,ldap
  10. # Set debugging level
  11. ldap.set_option(ldap.OPT_DEBUG_LEVEL,0)
  12. ldapmodule_trace_level = 1
  13. ldapmodule_trace_file = sys.stderr
  14. # Complete path name of the file containing all trusted CA certs
  15. CACERTFILE='/etc/apache2/ssl.crt/ca-bundle.crt'
  16. # TLS-related options have to be set globally since the TLS context is only initialized once
  17. # Force cert validation
  18. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_DEMAND)
  19. # Set path name of file containing all trusted CA certificates
  20. ldap.set_option(ldap.OPT_X_TLS_CACERTFILE,CACERTFILE)
  21. print """##################################################################
  22. # LDAPv3 connection with StartTLS
  23. ##################################################################
  24. """
  25. # Create LDAPObject instance
  26. l = ldap.initialize('ldap://localhost:1390',trace_level=ldapmodule_trace_level,trace_file=ldapmodule_trace_file)
  27. # Set LDAP protocol version used
  28. l.protocol_version=ldap.VERSION3
  29. # Force libldap to create a new SSL context
  30. #l.set_option(ldap.OPT_X_TLS_NEWCTX,ldap.OPT_X_TLS_DEMAND)
  31. # Force cert validation
  32. #l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_DEMAND)
  33. # Set path name of file containing all trusted CA certificates
  34. #l.set_option(ldap.OPT_X_TLS_CACERTFILE,CACERTFILE)
  35. # Now try StartTLS extended operation
  36. l.start_tls_s()
  37. # Try a bind to provoke failure if protocol version is not supported
  38. l.simple_bind_s('','')
  39. # Close connection
  40. l.unbind_s()
  41. print """##################################################################
  42. # LDAPv3 connection over SSL
  43. ##################################################################
  44. """
  45. # Create LDAPObject instance
  46. l = ldap.initialize('ldaps://localhost:1391',trace_level=ldapmodule_trace_level,trace_file=ldapmodule_trace_file)
  47. # Set LDAP protocol version used
  48. l.protocol_version=ldap.VERSION3
  49. # Force libldap to create a new SSL context
  50. #l.set_option(ldap.OPT_X_TLS_NEWCTX,ldap.OPT_X_TLS_DEMAND)
  51. # Force cert validation
  52. #l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_DEMAND)
  53. # Set path name of file containing all trusted CA certificates
  54. #l.set_option(ldap.OPT_X_TLS_CACERTFILE,CACERTFILE)
  55. # Try a bind to provoke failure if protocol version is not supported
  56. l.simple_bind_s('','')
  57. # Close connection
  58. l.unbind_s()
  59. print """##################################################################
  60. # LDAPv3 connection over Unix domain socket
  61. ##################################################################
  62. """
  63. # Create LDAPObject instance
  64. l = ldap.initialize('ldapi://%2ftmp%2fopenldap-socket',trace_level=ldapmodule_trace_level,trace_file=ldapmodule_trace_file)
  65. # Set LDAP protocol version used
  66. l.protocol_version=ldap.VERSION3
  67. # Try a bind to provoke failure if protocol version is not supported
  68. l.bind_s('','',ldap.AUTH_SIMPLE)
  69. # Close connection
  70. l.unbind_s()