apr_ldap_init.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * apr_ldap_init.c: LDAP v2/v3 common initialise
  18. *
  19. * Original code from auth_ldap module for Apache v1.3:
  20. * Copyright 1998, 1999 Enbridge Pipelines Inc.
  21. * Copyright 1999-2001 Dave Carrigan
  22. */
  23. #include "apr.h"
  24. #include "apu.h"
  25. #include "apr_ldap.h"
  26. #include "apr_errno.h"
  27. #include "apr_pools.h"
  28. #include "apr_strings.h"
  29. #if APR_HAS_LDAP
  30. /**
  31. * APR LDAP SSL Initialise function
  32. *
  33. * This function initialises SSL on the underlying LDAP toolkit
  34. * if this is necessary.
  35. *
  36. * If a CA certificate is provided, this is set, however the setting
  37. * of certificates via this method has been deprecated and will be removed in
  38. * APR v2.0.
  39. *
  40. * The apr_ldap_set_option() function with the APR_LDAP_OPT_TLS_CERT option
  41. * should be used instead to set certificates.
  42. *
  43. * If SSL support is not available on this platform, or a problem
  44. * was encountered while trying to set the certificate, the function
  45. * will return APR_EGENERAL. Further LDAP specific error information
  46. * can be found in result_err.
  47. */
  48. APU_DECLARE(int) apr_ldap_ssl_init(apr_pool_t *pool,
  49. const char *cert_auth_file,
  50. int cert_file_type,
  51. apr_ldap_err_t **result_err) {
  52. apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
  53. *result_err = result;
  54. #if APR_HAS_LDAP_SSL /* compiled with ssl support */
  55. /* Novell */
  56. #if APR_HAS_NOVELL_LDAPSDK
  57. ldapssl_client_init(NULL, NULL);
  58. #endif
  59. /* if a certificate was specified, set it */
  60. if (cert_auth_file) {
  61. apr_ldap_opt_tls_cert_t *cert = (apr_ldap_opt_tls_cert_t *)apr_pcalloc(pool, sizeof(apr_ldap_opt_tls_cert_t));
  62. cert->type = cert_file_type;
  63. cert->path = cert_auth_file;
  64. return apr_ldap_set_option(pool, NULL, APR_LDAP_OPT_TLS_CERT, (void *)cert, result_err);
  65. }
  66. #else /* not compiled with SSL Support */
  67. if (cert_auth_file) {
  68. result->reason = "LDAP: Attempt to set certificate store failed. "
  69. "Not built with SSL support";
  70. result->rc = -1;
  71. }
  72. #endif /* APR_HAS_LDAP_SSL */
  73. if (result->rc != -1) {
  74. result->msg = ldap_err2string(result->rc);
  75. }
  76. if (LDAP_SUCCESS != result->rc) {
  77. return APR_EGENERAL;
  78. }
  79. return APR_SUCCESS;
  80. }
  81. /**
  82. * APR LDAP SSL De-Initialise function
  83. *
  84. * This function tears down any SSL certificate setup previously
  85. * set using apr_ldap_ssl_init(). It should be called to clean
  86. * up if a graceful restart of a service is attempted.
  87. *
  88. * This function only does anything on Netware.
  89. *
  90. * @todo currently we do not check whether apr_ldap_ssl_init()
  91. * has been called first - should we?
  92. */
  93. APU_DECLARE(int) apr_ldap_ssl_deinit(void) {
  94. #if APR_HAS_LDAP_SSL && APR_HAS_LDAPSSL_CLIENT_DEINIT
  95. ldapssl_client_deinit();
  96. #endif
  97. return APR_SUCCESS;
  98. }
  99. /**
  100. * APR LDAP initialise function
  101. *
  102. * This function is responsible for initialising an LDAP
  103. * connection in a toolkit independant way. It does the
  104. * job of ldap_init() from the C api.
  105. *
  106. * It handles both the SSL and non-SSL case, and attempts
  107. * to hide the complexity setup from the user. This function
  108. * assumes that any certificate setup necessary has already
  109. * been done.
  110. *
  111. * If SSL or STARTTLS needs to be enabled, and the underlying
  112. * toolkit supports it, the following values are accepted for
  113. * secure:
  114. *
  115. * APR_LDAP_NONE: No encryption
  116. * APR_LDAP_SSL: SSL encryption (ldaps://)
  117. * APR_LDAP_STARTTLS: Force STARTTLS on ldap://
  118. */
  119. APU_DECLARE(int) apr_ldap_init(apr_pool_t *pool,
  120. LDAP **ldap,
  121. const char *hostname,
  122. int portno,
  123. int secure,
  124. apr_ldap_err_t **result_err) {
  125. apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
  126. *result_err = result;
  127. #if APR_HAS_LDAPSSL_INIT
  128. *ldap = ldapssl_init(hostname, portno, 0);
  129. #elif APR_HAS_LDAP_SSLINIT
  130. *ldap = ldap_sslinit((char *)hostname, portno, 0);
  131. #else
  132. *ldap = ldap_init((char *)hostname, portno);
  133. #endif
  134. if (*ldap != NULL) {
  135. return apr_ldap_set_option(pool, *ldap, APR_LDAP_OPT_TLS, &secure, result_err);
  136. }
  137. else {
  138. /* handle the error case */
  139. apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
  140. *result_err = result;
  141. result->reason = "APR LDAP: Unable to initialize the LDAP connection";
  142. result->rc = -1;
  143. return APR_EGENERAL;
  144. }
  145. }
  146. /**
  147. * APR LDAP info function
  148. *
  149. * This function returns a string describing the LDAP toolkit
  150. * currently in use. The string is placed inside result_err->reason.
  151. */
  152. APU_DECLARE(int) apr_ldap_info(apr_pool_t *pool, apr_ldap_err_t **result_err)
  153. {
  154. apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
  155. *result_err = result;
  156. result->reason = "APR LDAP: Built with "
  157. LDAP_VENDOR_NAME
  158. " LDAP SDK";
  159. return APR_SUCCESS;
  160. }
  161. #endif /* APR_HAS_LDAP */