summaryrefslogtreecommitdiffstats
path: root/servers/lloadd/init.c
blob: 7dbe58f1c3c08408339fead7e6a4523765f977e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* init.c - initialize various things */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1998-2022 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

#include "portable.h"

#include <stdio.h>

#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>

#include "lload.h"
#include "lber_pvt.h"

#include "ldap_rq.h"

#ifndef BALANCER_MODULE
/*
 * read-only global variables or variables only written by the listener
 * thread (after they are initialized) - no need to protect them with a mutex.
 */
int slap_debug = 0;

#ifdef LDAP_DEBUG
int ldap_syslog = LDAP_DEBUG_STATS;
#else
int ldap_syslog;
#endif

#ifdef LOG_DEBUG
int ldap_syslog_level = LOG_DEBUG;
#endif

/*
 * global variables that need mutex protection
 */
ldap_pvt_thread_pool_t connection_pool;
int connection_pool_max = SLAP_MAX_WORKER_THREADS;
int connection_pool_queues = 1;
int slap_tool_thread_max = 1;

int slapMode = SLAP_UNDEFINED_MODE;
#endif /* !BALANCER_MODULE */

static const char *lload_name = NULL;

int
lload_global_init( void )
{
    int rc;

    if ( lload_libevent_init() ) {
        return -1;
    }

#ifdef HAVE_TLS
    if ( ldap_create( &lload_tls_backend_ld ) ) {
        return -1;
    }
    if ( ldap_create( &lload_tls_ld ) ) {
        return -1;
    }

    /* Library defaults to full certificate checking. This is correct when
     * a client is verifying a server because all servers should have a
     * valid cert. But few clients have valid certs, so we want our default
     * to be no checking. The config file can override this as usual.
     */
    rc = LDAP_OPT_X_TLS_NEVER;
    (void)ldap_pvt_tls_set_option(
            lload_tls_ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
#endif

    ldap_pvt_thread_mutex_init( &lload_wait_mutex );
    ldap_pvt_thread_cond_init( &lload_wait_cond );
    ldap_pvt_thread_cond_init( &lload_pause_cond );

    ldap_pvt_thread_mutex_init( &backend_mutex );
    ldap_pvt_thread_mutex_init( &clients_mutex );
    ldap_pvt_thread_mutex_init( &lload_pin_mutex );

    if ( lload_exop_init() ) {
        return -1;
    }
    return 0;
}

int
lload_tls_init( void )
{
#ifdef HAVE_TLS
    int rc, opt = 1;

    /* Force new ctx to be created */
    rc = ldap_pvt_tls_set_option( lload_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
    if ( rc == 0 ) {
        /* The ctx's refcount is bumped up here */
        ldap_pvt_tls_get_option(
                lload_tls_ld, LDAP_OPT_X_TLS_CTX, &lload_tls_ctx );
    } else if ( rc != LDAP_NOT_SUPPORTED ) {
        Debug( LDAP_DEBUG_ANY, "lload_global_init: "
                "TLS init def ctx failed: %d\n",
                rc );
        return -1;
    }
#endif
    return 0;
}

int
lload_init( int mode, const char *name )
{
    int rc = LDAP_SUCCESS;

    assert( mode );

    if ( slapMode != SLAP_UNDEFINED_MODE ) {
        /* Make sure we write something to stderr */
        slap_debug |= LDAP_DEBUG_NONE;
        Debug( LDAP_DEBUG_ANY, "%s init: "
                "init called twice (old=%d, new=%d)\n",
                name, slapMode, mode );

        return 1;
    }

    slapMode = mode;

    switch ( slapMode & SLAP_MODE ) {
        case SLAP_SERVER_MODE:
            Debug( LDAP_DEBUG_TRACE, "%s init: "
                    "initiated server.\n",
                    name );

            lload_name = name;

            ldap_pvt_thread_pool_init_q( &connection_pool, connection_pool_max,
                    0, connection_pool_queues );

            ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
            LDAP_STAILQ_INIT( &slapd_rq.task_list );
            LDAP_STAILQ_INIT( &slapd_rq.run_list );

            rc = lload_global_init();
            break;

        default:
            slap_debug |= LDAP_DEBUG_NONE;
            Debug( LDAP_DEBUG_ANY, "%s init: "
                    "undefined mode (%d).\n",
                    name, mode );

            rc = 1;
            break;
    }

    return rc;
}

int
lload_destroy( void )
{
    int rc = LDAP_SUCCESS;

    Debug( LDAP_DEBUG_TRACE, "%s destroy: "
            "freeing system resources.\n",
            lload_name );

    ldap_pvt_thread_pool_free( &connection_pool );

    switch ( slapMode & SLAP_MODE ) {
        case SLAP_SERVER_MODE:
            break;

        default:
            Debug( LDAP_DEBUG_ANY, "lload_destroy(): "
                    "undefined mode (%d).\n",
                    slapMode );

            rc = 1;
            break;
    }

    ldap_pvt_thread_destroy();

    /* should destroy the above mutex */
    return rc;
}