Apply patch from bug #41250, by Chris Darroch
[apr-util.git] / ldap / apr_ldap_init.c
blob767faade4bd594f445d907ff682c5a2bbe5a374d
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * apr_ldap_init.c: LDAP v2/v3 common initialise
20 * Original code from auth_ldap module for Apache v1.3:
21 * Copyright 1998, 1999 Enbridge Pipelines Inc.
22 * Copyright 1999-2001 Dave Carrigan
25 #include "apr.h"
26 #include "apu.h"
27 #include "apr_ldap.h"
28 #include "apr_errno.h"
29 #include "apr_pools.h"
30 #include "apr_strings.h"
32 #if APR_HAS_LDAP
34 /**
35 * APR LDAP SSL Initialise function
37 * This function initialises SSL on the underlying LDAP toolkit
38 * if this is necessary.
40 * If a CA certificate is provided, this is set, however the setting
41 * of certificates via this method has been deprecated and will be removed in
42 * APR v2.0.
44 * The apr_ldap_set_option() function with the APR_LDAP_OPT_TLS_CERT option
45 * should be used instead to set certificates.
47 * If SSL support is not available on this platform, or a problem
48 * was encountered while trying to set the certificate, the function
49 * will return APR_EGENERAL. Further LDAP specific error information
50 * can be found in result_err.
52 APU_DECLARE(int) apr_ldap_ssl_init(apr_pool_t *pool,
53 const char *cert_auth_file,
54 int cert_file_type,
55 apr_ldap_err_t **result_err) {
57 apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
58 *result_err = result;
60 #if APR_HAS_LDAP_SSL /* compiled with ssl support */
62 /* Novell */
63 #if APR_HAS_NOVELL_LDAPSDK
64 ldapssl_client_init(NULL, NULL);
65 #endif
67 /* if a certificate was specified, set it */
68 if (cert_auth_file) {
69 apr_ldap_opt_tls_cert_t *cert = (apr_ldap_opt_tls_cert_t *)apr_pcalloc(pool, sizeof(apr_ldap_opt_tls_cert_t));
70 cert->type = cert_file_type;
71 cert->path = cert_auth_file;
72 return apr_ldap_set_option(pool, NULL, APR_LDAP_OPT_TLS_CERT, (void *)cert, result_err);
75 #else /* not compiled with SSL Support */
76 if (cert_auth_file) {
77 result->reason = "LDAP: Attempt to set certificate store failed. "
78 "Not built with SSL support";
79 result->rc = -1;
81 #endif /* APR_HAS_LDAP_SSL */
83 if (result->rc != -1) {
84 result->msg = ldap_err2string(result->rc);
87 if (LDAP_SUCCESS != result->rc) {
88 return APR_EGENERAL;
91 return APR_SUCCESS;
96 /**
97 * APR LDAP SSL De-Initialise function
99 * This function tears down any SSL certificate setup previously
100 * set using apr_ldap_ssl_init(). It should be called to clean
101 * up if a graceful restart of a service is attempted.
103 * This function only does anything on Netware.
105 * @todo currently we do not check whether apr_ldap_ssl_init()
106 * has been called first - should we?
108 APU_DECLARE(int) apr_ldap_ssl_deinit(void) {
110 #if APR_HAS_LDAP_SSL && APR_HAS_LDAPSSL_CLIENT_DEINIT
111 ldapssl_client_deinit();
112 #endif
113 return APR_SUCCESS;
119 * APR LDAP initialise function
121 * This function is responsible for initialising an LDAP
122 * connection in a toolkit independant way. It does the
123 * job of ldap_init() from the C api.
125 * It handles both the SSL and non-SSL case, and attempts
126 * to hide the complexity setup from the user. This function
127 * assumes that any certificate setup necessary has already
128 * been done.
130 * If SSL or STARTTLS needs to be enabled, and the underlying
131 * toolkit supports it, the following values are accepted for
132 * secure:
134 * APR_LDAP_NONE: No encryption
135 * APR_LDAP_SSL: SSL encryption (ldaps://)
136 * APR_LDAP_STARTTLS: Force STARTTLS on ldap://
138 APU_DECLARE(int) apr_ldap_init(apr_pool_t *pool,
139 LDAP **ldap,
140 const char *hostname,
141 int portno,
142 int secure,
143 apr_ldap_err_t **result_err) {
145 apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
146 *result_err = result;
148 #if APR_HAS_LDAPSSL_INIT
149 *ldap = ldapssl_init(hostname, portno, 0);
150 #elif APR_HAS_LDAP_SSLINIT
151 *ldap = ldap_sslinit((char *)hostname, portno, 0);
152 #else
153 *ldap = ldap_init((char *)hostname, portno);
154 #endif
155 if (*ldap != NULL) {
156 return apr_ldap_set_option(pool, *ldap, APR_LDAP_OPT_TLS, &secure, result_err);
158 else {
159 /* handle the error case */
160 apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
161 *result_err = result;
163 result->reason = "APR LDAP: Unable to initialize the LDAP connection";
164 result->rc = -1;
165 return APR_EGENERAL;
172 * APR LDAP info function
174 * This function returns a string describing the LDAP toolkit
175 * currently in use. The string is placed inside result_err->reason.
177 APU_DECLARE(int) apr_ldap_info(apr_pool_t *pool, apr_ldap_err_t **result_err)
179 apr_ldap_err_t *result = (apr_ldap_err_t *)apr_pcalloc(pool, sizeof(apr_ldap_err_t));
180 *result_err = result;
182 result->reason = "APR LDAP: Built with "
183 LDAP_VENDOR_NAME
184 " LDAP SDK";
185 return APR_SUCCESS;
189 #endif /* APR_HAS_LDAP */