1/* $NetBSD: init.c,v 1.2 2021/08/14 16:14:58 christos Exp $ */ 2 3/* init.c - initialize various things */ 4/* $OpenLDAP$ */ 5/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2021 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18/* Portions Copyright (c) 1995 Regents of the University of Michigan. 19 * All rights reserved. 20 * 21 * Redistribution and use in source and binary forms are permitted 22 * provided that this notice is preserved and that due credit is given 23 * to the University of Michigan at Ann Arbor. The name of the University 24 * may not be used to endorse or promote products derived from this 25 * software without specific prior written permission. This software 26 * is provided ``as is'' without express or implied warranty. 27 */ 28 29#include <sys/cdefs.h> 30__RCSID("$NetBSD: init.c,v 1.2 2021/08/14 16:14:58 christos Exp $"); 31 32#include "portable.h" 33 34#include <stdio.h> 35 36#include <ac/socket.h> 37#include <ac/string.h> 38#include <ac/time.h> 39 40#include "lload.h" 41#include "lber_pvt.h" 42 43#include "ldap_rq.h" 44 45#ifndef BALANCER_MODULE 46/* 47 * read-only global variables or variables only written by the listener 48 * thread (after they are initialized) - no need to protect them with a mutex. 49 */ 50int slap_debug = 0; 51 52#ifdef LDAP_DEBUG 53int ldap_syslog = LDAP_DEBUG_STATS; 54#else 55int ldap_syslog; 56#endif 57 58#ifdef LOG_DEBUG 59int ldap_syslog_level = LOG_DEBUG; 60#endif 61 62/* 63 * global variables that need mutex protection 64 */ 65ldap_pvt_thread_pool_t connection_pool; 66int connection_pool_max = SLAP_MAX_WORKER_THREADS; 67int connection_pool_queues = 1; 68int slap_tool_thread_max = 1; 69 70int slapMode = SLAP_UNDEFINED_MODE; 71#endif /* !BALANCER_MODULE */ 72 73static const char *lload_name = NULL; 74 75int 76lload_global_init( void ) 77{ 78 int rc; 79 80 if ( lload_libevent_init() ) { 81 return -1; 82 } 83 84#ifdef HAVE_TLS 85 if ( ldap_create( &lload_tls_backend_ld ) ) { 86 return -1; 87 } 88 if ( ldap_create( &lload_tls_ld ) ) { 89 return -1; 90 } 91 92 /* Library defaults to full certificate checking. This is correct when 93 * a client is verifying a server because all servers should have a 94 * valid cert. But few clients have valid certs, so we want our default 95 * to be no checking. The config file can override this as usual. 96 */ 97 rc = LDAP_OPT_X_TLS_NEVER; 98 (void)ldap_pvt_tls_set_option( 99 lload_tls_ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc ); 100#endif 101 102 ldap_pvt_thread_mutex_init( &lload_wait_mutex ); 103 ldap_pvt_thread_cond_init( &lload_wait_cond ); 104 ldap_pvt_thread_cond_init( &lload_pause_cond ); 105 106 ldap_pvt_thread_mutex_init( &backend_mutex ); 107 ldap_pvt_thread_mutex_init( &clients_mutex ); 108 ldap_pvt_thread_mutex_init( &lload_pin_mutex ); 109 110 if ( lload_exop_init() ) { 111 return -1; 112 } 113 return 0; 114} 115 116int 117lload_tls_init( void ) 118{ 119#ifdef HAVE_TLS 120 int rc, opt = 1; 121 122 /* Force new ctx to be created */ 123 rc = ldap_pvt_tls_set_option( lload_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt ); 124 if ( rc == 0 ) { 125 /* The ctx's refcount is bumped up here */ 126 ldap_pvt_tls_get_option( 127 lload_tls_ld, LDAP_OPT_X_TLS_CTX, &lload_tls_ctx ); 128 } else if ( rc != LDAP_NOT_SUPPORTED ) { 129 Debug( LDAP_DEBUG_ANY, "lload_global_init: " 130 "TLS init def ctx failed: %d\n", 131 rc ); 132 return -1; 133 } 134#endif 135 return 0; 136} 137 138int 139lload_init( int mode, const char *name ) 140{ 141 int rc = LDAP_SUCCESS; 142 143 assert( mode ); 144 145 if ( slapMode != SLAP_UNDEFINED_MODE ) { 146 /* Make sure we write something to stderr */ 147 slap_debug |= LDAP_DEBUG_NONE; 148 Debug( LDAP_DEBUG_ANY, "%s init: " 149 "init called twice (old=%d, new=%d)\n", 150 name, slapMode, mode ); 151 152 return 1; 153 } 154 155 slapMode = mode; 156 157 switch ( slapMode & SLAP_MODE ) { 158 case SLAP_SERVER_MODE: 159 Debug( LDAP_DEBUG_TRACE, "%s init: " 160 "initiated server.\n", 161 name ); 162 163 lload_name = name; 164 165 ldap_pvt_thread_pool_init_q( &connection_pool, connection_pool_max, 166 0, connection_pool_queues ); 167 168 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex ); 169 LDAP_STAILQ_INIT( &slapd_rq.task_list ); 170 LDAP_STAILQ_INIT( &slapd_rq.run_list ); 171 172 rc = lload_global_init(); 173 break; 174 175 default: 176 slap_debug |= LDAP_DEBUG_NONE; 177 Debug( LDAP_DEBUG_ANY, "%s init: " 178 "undefined mode (%d).\n", 179 name, mode ); 180 181 rc = 1; 182 break; 183 } 184 185 return rc; 186} 187 188int 189lload_destroy( void ) 190{ 191 int rc = LDAP_SUCCESS; 192 193 Debug( LDAP_DEBUG_TRACE, "%s destroy: " 194 "freeing system resources.\n", 195 lload_name ); 196 197 ldap_pvt_thread_pool_free( &connection_pool ); 198 199 switch ( slapMode & SLAP_MODE ) { 200 case SLAP_SERVER_MODE: 201 break; 202 203 default: 204 Debug( LDAP_DEBUG_ANY, "lload_destroy(): " 205 "undefined mode (%d).\n", 206 slapMode ); 207 208 rc = 1; 209 break; 210 } 211 212 ldap_pvt_thread_destroy(); 213 214 /* should destroy the above mutex */ 215 return rc; 216} 217