Blame test/testldap.c

Packit 383869
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 383869
 * contributor license agreements.  See the NOTICE file distributed with
Packit 383869
 * this work for additional information regarding copyright ownership.
Packit 383869
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 383869
 * (the "License"); you may not use this file except in compliance with
Packit 383869
 * the License.  You may obtain a copy of the License at
Packit 383869
 *
Packit 383869
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 383869
 *
Packit 383869
 * Unless required by applicable law or agreed to in writing, software
Packit 383869
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 383869
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 383869
 * See the License for the specific language governing permissions and
Packit 383869
 * limitations under the License.
Packit 383869
 */
Packit 383869
 
Packit 383869
 /* Setup:
Packit 383869
  *  - Create or edit the file data/host.data and add an
Packit 383869
  *     ldap server DN.  Multiple DNs may be listed on
Packit 383869
  *     a single line.
Packit 383869
  *  - Copy the server certificates to the data/ directory.
Packit 383869
  *     All DER type certificates must have the .der extention.
Packit 383869
  *     All BASE64 or PEM certificates must have the .b64
Packit 383869
  *     extension.  All certificate files copied to the /data
Packit 383869
  *     directory will be added to the ldap certificate store.
Packit 383869
  */
Packit 383869
   
Packit 383869
 /* This test covers the following three types of connections:
Packit 383869
  *  - Unsecure ldap://
Packit 383869
  *  - Secure ldaps://
Packit 383869
  *  - Secure ldap://+Start_TLS
Packit 383869
  *
Packit 383869
  *  - (TBD) Mutual authentication 
Packit 383869
  *
Packit 383869
  * There are other variations that should be tested:
Packit 383869
  *  - All of the above with multiple redundant LDAP servers
Packit 383869
  *     This can be done by listing more than one server DN
Packit 383869
  *      in the host.data file.  The DNs should all be listed
Packit 383869
  *      on one line separated by a space.
Packit 383869
  *  - All of the above with multiple certificates
Packit 383869
  *     If more than one certificate is found in the data/
Packit 383869
  *      directory, each certificate found will be added
Packit 383869
  *      to the certificate store.
Packit 383869
  *  - All of the above on alternate ports
Packit 383869
  *     An alternate port can be specified as part of the
Packit 383869
  *      host in the host.data file.  The ":port" should 
Packit 383869
  *      follow each DN listed.  Default is 389 and 636.
Packit 383869
  *  - Secure connections with mutual authentication
Packit 383869
  */
Packit 383869
Packit 383869
#include "testutil.h"
Packit 383869
Packit 383869
#include "apr.h"
Packit 383869
#include "apr_general.h"
Packit 383869
#include "apr_ldap.h"
Packit 383869
#include "apr_file_io.h"
Packit 383869
#include "apr_file_info.h"
Packit 383869
#include "apr_strings.h"
Packit 383869
#if APR_HAVE_STDLIB_H
Packit 383869
#include <stdlib.h>
Packit 383869
#endif
Packit 383869
#define APR_WANT_STDIO
Packit 383869
#define APR_WANT_STRFUNC
Packit 383869
#include "apr_want.h"
Packit 383869
Packit 383869
#define DIRNAME "data"
Packit 383869
#define FILENAME DIRNAME "/host.data"
Packit 383869
#define CERTFILEDER DIRNAME "/*.der"
Packit 383869
#define CERTFILEB64 DIRNAME "/*.b64"
Packit 383869
Packit 383869
#if APR_HAS_LDAP
Packit 383869
Packit 383869
static char ldap_host[256];
Packit 383869
Packit 383869
static int get_ldap_host(void)
Packit 383869
{
Packit 383869
    apr_status_t rv;
Packit 383869
    apr_file_t *thefile = NULL;
Packit 383869
    char *ptr;
Packit 383869
Packit 383869
    ldap_host[0] = '\0';
Packit 383869
    rv = apr_file_open(&thefile, FILENAME, 
Packit 383869
                       APR_FOPEN_READ,
Packit 383869
                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
Packit 383869
    if (rv != APR_SUCCESS) {
Packit 383869
        return 0;
Packit 383869
    }
Packit 383869
Packit 383869
    rv = apr_file_gets(ldap_host, sizeof(ldap_host), thefile);
Packit 383869
    if (rv != APR_SUCCESS) {
Packit 383869
        return 0;
Packit 383869
    }
Packit 383869
Packit 383869
    ptr = strstr (ldap_host, "\r\n");
Packit 383869
    if (ptr) {
Packit 383869
        *ptr = '\0';
Packit 383869
    }
Packit 383869
    apr_file_close(thefile);
Packit 383869
Packit 383869
    return 1;
Packit 383869
Packit 383869
}
Packit 383869
Packit 383869
static int add_ldap_certs(abts_case *tc)
Packit 383869
{
Packit 383869
    apr_status_t status;
Packit 383869
    apr_dir_t *thedir;
Packit 383869
    apr_finfo_t dirent;
Packit 383869
    apr_ldap_err_t *result = NULL;
Packit 383869
Packit 383869
    if ((status = apr_dir_open(&thedir, DIRNAME, p)) == APR_SUCCESS) {
Packit 383869
        apr_ldap_opt_tls_cert_t *cert = (apr_ldap_opt_tls_cert_t *)apr_pcalloc(p, sizeof(apr_ldap_opt_tls_cert_t));
Packit 383869
Packit 383869
        do {
Packit 383869
            status = apr_dir_read(&dirent, APR_FINFO_MIN | APR_FINFO_NAME, thedir);
Packit 383869
            if (APR_STATUS_IS_INCOMPLETE(status)) {
Packit 383869
                continue; /* ignore un-stat()able files */
Packit 383869
            }
Packit 383869
            else if (status != APR_SUCCESS) {
Packit 383869
                break;
Packit 383869
            }
Packit 383869
Packit 383869
            if (strstr(dirent.name, ".der")) {
Packit 383869
                cert->type = APR_LDAP_CA_TYPE_DER;
Packit 383869
                cert->path = apr_pstrcat (p, DIRNAME, "/", dirent.name, NULL);
Packit 383869
                apr_ldap_set_option(p, NULL, APR_LDAP_OPT_TLS_CERT, (void *)cert, &result);
Packit 383869
                ABTS_TRUE(tc, result->rc == LDAP_SUCCESS);
Packit 383869
            }
Packit 383869
            if (strstr(dirent.name, ".b64")) {
Packit 383869
                cert->type = APR_LDAP_CA_TYPE_BASE64;
Packit 383869
                cert->path = apr_pstrcat (p, DIRNAME, "/", dirent.name, NULL);
Packit 383869
                apr_ldap_set_option(p, NULL, APR_LDAP_OPT_TLS_CERT, (void *)cert, &result);
Packit 383869
                ABTS_TRUE(tc, result->rc == LDAP_SUCCESS);
Packit 383869
            }
Packit 383869
Packit 383869
        } while (1);
Packit 383869
Packit 383869
        apr_dir_close(thedir);
Packit 383869
    }
Packit 383869
    return 0;
Packit 383869
}
Packit 383869
Packit 383869
static void test_ldap_connection(abts_case *tc, LDAP *ldap)
Packit 383869
{
Packit 383869
    int version  = LDAP_VERSION3;
Packit 383869
    int failures, result;
Packit 383869
    
Packit 383869
    /* always default to LDAP V3 */
Packit 383869
    ldap_set_option(ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
Packit 383869
Packit 383869
    for (failures=0; failures<10; failures++)
Packit 383869
    {
Packit 383869
        result = ldap_simple_bind_s(ldap,
Packit 383869
                                    (char *)NULL,
Packit 383869
                                    (char *)NULL);
Packit 383869
        if (LDAP_SERVER_DOWN != result)
Packit 383869
            break;
Packit 383869
    }
Packit 383869
Packit 383869
    ABTS_TRUE(tc, result == LDAP_SUCCESS);
Packit 383869
    if (result != LDAP_SUCCESS) {
Packit 383869
        abts_log_message("%s\n", ldap_err2string(result));
Packit 383869
    }
Packit 383869
Packit 383869
    ldap_unbind_s(ldap);
Packit 383869
Packit 383869
    return;
Packit 383869
}
Packit 383869
Packit 383869
static void test_ldap(abts_case *tc, void *data)
Packit 383869
{
Packit 383869
    apr_pool_t *pool = p;
Packit 383869
    LDAP *ldap;
Packit 383869
    apr_ldap_err_t *result = NULL;
Packit 383869
Packit 383869
Packit 383869
    ABTS_ASSERT(tc, "failed to get host", ldap_host[0] != '\0');
Packit 383869
    
Packit 383869
    apr_ldap_init(pool, &ldap,
Packit 383869
                  ldap_host, LDAP_PORT,
Packit 383869
                  APR_LDAP_NONE, &(result));
Packit 383869
Packit 383869
    ABTS_TRUE(tc, ldap != NULL);
Packit 383869
    ABTS_PTR_NOTNULL(tc, result);
Packit 383869
Packit 383869
    if (result->rc == LDAP_SUCCESS) {
Packit 383869
        test_ldap_connection(tc, ldap);
Packit 383869
    }
Packit 383869
}
Packit 383869
Packit 383869
static void test_ldaps(abts_case *tc, void *data)
Packit 383869
{
Packit 383869
    apr_pool_t *pool = p;
Packit 383869
    LDAP *ldap;
Packit 383869
    apr_ldap_err_t *result = NULL;
Packit 383869
Packit 383869
    apr_ldap_init(pool, &ldap,
Packit 383869
                  ldap_host, LDAPS_PORT,
Packit 383869
                  APR_LDAP_SSL, &(result));
Packit 383869
Packit 383869
    ABTS_TRUE(tc, ldap != NULL);
Packit 383869
    ABTS_PTR_NOTNULL(tc, result);
Packit 383869
Packit 383869
    if (result->rc == LDAP_SUCCESS) {
Packit 383869
        add_ldap_certs(tc);
Packit 383869
Packit 383869
        test_ldap_connection(tc, ldap);
Packit 383869
    }
Packit 383869
}
Packit 383869
Packit 383869
static void test_ldap_tls(abts_case *tc, void *data)
Packit 383869
{
Packit 383869
    apr_pool_t *pool = p;
Packit 383869
    LDAP *ldap;
Packit 383869
    apr_ldap_err_t *result = NULL;
Packit 383869
Packit 383869
    apr_ldap_init(pool, &ldap,
Packit 383869
                  ldap_host, LDAP_PORT,
Packit 383869
                  APR_LDAP_STARTTLS, &(result));
Packit 383869
Packit 383869
    ABTS_TRUE(tc, ldap != NULL);
Packit 383869
    ABTS_PTR_NOTNULL(tc, result);
Packit 383869
Packit 383869
    if (result->rc == LDAP_SUCCESS) {
Packit 383869
        add_ldap_certs(tc);
Packit 383869
Packit 383869
        test_ldap_connection(tc, ldap);
Packit 383869
    }
Packit 383869
}
Packit 383869
Packit 383869
#endif /* APR_HAS_LDAP */
Packit 383869
Packit 383869
abts_suite *testldap(abts_suite *suite)
Packit 383869
{
Packit 383869
#if APR_HAS_LDAP
Packit 383869
    apr_ldap_err_t *result = NULL;
Packit 383869
    suite = ADD_SUITE(suite);
Packit 383869
Packit 383869
    apr_ldap_ssl_init(p, NULL, 0, &result);
Packit 383869
Packit 383869
    if (get_ldap_host()) {
Packit 383869
        abts_run_test(suite, test_ldap, NULL);
Packit 383869
        abts_run_test(suite, test_ldaps, NULL);
Packit 383869
        abts_run_test(suite, test_ldap_tls, NULL);
Packit 383869
    }
Packit 383869
#endif /* APR_HAS_LDAP */
Packit 383869
Packit 383869
    return suite;
Packit 383869
}
Packit 383869