initialize.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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,os,ldap
  10. # Switch off processing .ldaprc or ldap.conf
  11. os.environ['LDAPNOINIT']='1'
  12. # Set debugging level
  13. ldap.set_option(ldap.OPT_DEBUG_LEVEL,255)
  14. ldapmodule_trace_level = 2
  15. ldapmodule_trace_file = sys.stderr
  16. ldap._trace_level = ldapmodule_trace_level
  17. # Complete path name of the file containing all trusted CA certs
  18. CACERTFILE='/etc/apache2/ssl.crt/ca-bundle.crt'
  19. # TLS-related options have to be set globally since the TLS context is only initialized once
  20. # Force libldap to create a new SSL context
  21. #ldap.set_option(ldap.OPT_X_TLS_NEWCTX,ldap.OPT_ON)
  22. # Force cert validation
  23. #ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_DEMAND)
  24. # Set path name of file containing all trusted CA certificates
  25. #ldap.set_option(ldap.OPT_X_TLS_CACERTFILE,CACERTFILE)
  26. print """##################################################################
  27. # LDAPv3 connection with StartTLS
  28. ##################################################################
  29. """
  30. # Create LDAPObject instance
  31. l = ldap.initialize('ldap://localhost:1390',trace_level=ldapmodule_trace_level,trace_file=ldapmodule_trace_file)
  32. # Set LDAP protocol version used
  33. l.protocol_version=ldap.VERSION3
  34. # Force libldap to create a new SSL context
  35. l.set_option(ldap.OPT_X_TLS_NEWCTX,ldap.OPT_ON)
  36. # Force cert validation
  37. l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_DEMAND)
  38. # Set path name of file containing all trusted CA certificates
  39. l.set_option(ldap.OPT_X_TLS_CACERTFILE,CACERTFILE)
  40. # Now try StartTLS extended operation
  41. l.start_tls_s()
  42. # Try a bind to provoke failure if protocol version is not supported
  43. l.simple_bind_s('','')
  44. # Close connection
  45. l.unbind_s()
  46. print """##################################################################
  47. # LDAPv3 connection over SSL
  48. ##################################################################
  49. """
  50. # Create LDAPObject instance
  51. l = ldap.initialize('ldaps://localhost:1391',trace_level=ldapmodule_trace_level,trace_file=ldapmodule_trace_file)
  52. # Set LDAP protocol version used
  53. l.protocol_version=ldap.VERSION3
  54. # Force libldap to create a new SSL context
  55. l.set_option(ldap.OPT_X_TLS_NEWCTX,ldap.OPT_X_TLS_DEMAND)
  56. # Force cert validation
  57. l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_DEMAND)
  58. # Set path name of file containing all trusted CA certificates
  59. l.set_option(ldap.OPT_X_TLS_CACERTFILE,CACERTFILE)
  60. # Try a bind to provoke failure if protocol version is not supported
  61. l.simple_bind_s('','')
  62. # Close connection
  63. l.unbind_s()
  64. print """##################################################################
  65. # LDAPv3 connection over Unix domain socket
  66. ##################################################################
  67. """
  68. # Create LDAPObject instance
  69. l = ldap.initialize('ldapi://%2ftmp%2fopenldap-socket',trace_level=ldapmodule_trace_level,trace_file=ldapmodule_trace_file)
  70. # Set LDAP protocol version used
  71. l.protocol_version=ldap.VERSION3
  72. # Try a bind to provoke failure if protocol version is not supported
  73. l.bind_s('','',ldap.AUTH_SIMPLE)
  74. # Close connection
  75. l.unbind_s()