]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
allow to programmatically register new response code names
[openldap] / servers / slapd / init.c
1 /* init.c - initialize various things */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/socket.h>
32 #include <ac/string.h>
33 #include <ac/time.h>
34
35 #include "slap.h"
36 #include "lber_pvt.h"
37
38 #include "ldap_rq.h"
39
40 /*
41  * read-only global variables or variables only written by the listener
42  * thread (after they are initialized) - no need to protect them with a mutex.
43  */
44 int             slap_debug = 0;
45
46 #ifdef LDAP_DEBUG
47 int             ldap_syslog = LDAP_DEBUG_STATS;
48 #else
49 int             ldap_syslog;
50 #endif
51
52 #ifdef LOG_DEBUG
53 int             ldap_syslog_level = LOG_DEBUG;
54 #endif
55
56 BerVarray default_referral = NULL;
57
58 struct berval AllUser = BER_BVC( LDAP_ALL_USER_ATTRIBUTES );
59 struct berval AllOper = BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES );
60 struct berval NoAttrs = BER_BVC( LDAP_NO_ATTRS );
61
62 /*
63  * global variables that need mutex protection
64  */
65 ldap_pvt_thread_pool_t  connection_pool;
66 int                     connection_pool_max = SLAP_MAX_WORKER_THREADS;
67 int             slap_tool_thread_max = 1;
68 ldap_pvt_thread_mutex_t gmtime_mutex;
69
70 slap_counters_t                 slap_counters, *slap_counters_list;
71
72 static const char* slap_name = NULL;
73 int slapMode = SLAP_UNDEFINED_MODE;
74
75 int
76 slap_init( int mode, const char *name )
77 {
78         int rc;
79
80         assert( mode );
81
82         if ( slapMode != SLAP_UNDEFINED_MODE ) {
83                 /* Make sure we write something to stderr */
84                 slap_debug |= LDAP_DEBUG_NONE;
85                 Debug( LDAP_DEBUG_ANY,
86                  "%s init: init called twice (old=%d, new=%d)\n",
87                  name, slapMode, mode );
88
89                 return 1;
90         }
91
92         slapMode = mode;
93
94         slap_op_init();
95
96 #ifdef SLAPD_MODULES
97         if ( module_init() != 0 ) {
98                 slap_debug |= LDAP_DEBUG_NONE;
99                 Debug( LDAP_DEBUG_ANY,
100                     "%s: module_init failed\n",
101                         name, 0, 0 );
102                 return 1;
103         }
104 #endif
105
106         if ( slap_schema_init( ) != 0 ) {
107                 slap_debug |= LDAP_DEBUG_NONE;
108                 Debug( LDAP_DEBUG_ANY,
109                     "%s: slap_schema_init failed\n",
110                     name, 0, 0 );
111                 return 1;
112         }
113
114         if ( filter_init() != 0 ) {
115                 slap_debug |= LDAP_DEBUG_NONE;
116                 Debug( LDAP_DEBUG_ANY,
117                     "%s: filter_init failed\n",
118                     name, 0, 0 );
119                 return 1;
120         }
121
122         if ( entry_init() != 0 ) {
123                 slap_debug |= LDAP_DEBUG_NONE;
124                 Debug( LDAP_DEBUG_ANY,
125                     "%s: entry_init failed\n",
126                     name, 0, 0 );
127                 return 1;
128         }
129
130         switch ( slapMode & SLAP_MODE ) {
131         case SLAP_SERVER_MODE:
132                 root_dse_init();
133
134                 /* FALLTHRU */
135         case SLAP_TOOL_MODE:
136                 Debug( LDAP_DEBUG_TRACE,
137                         "%s init: initiated %s.\n",     name,
138                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
139                         0 );
140
141                 slap_name = name;
142
143                 ldap_pvt_thread_pool_init( &connection_pool,
144                                 connection_pool_max, 0);
145
146                 slap_counters_init( &slap_counters );
147
148                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
149                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
150                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
151
152                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
153                 slap_passwd_init();
154
155                 rc = slap_sasl_init();
156
157                 if( rc == 0 ) {
158                         rc = backend_init( );
159                 }
160                 if ( rc )
161                         return rc;
162
163                 break;
164
165         default:
166                 slap_debug |= LDAP_DEBUG_NONE;
167                 Debug( LDAP_DEBUG_ANY,
168                         "%s init: undefined mode (%d).\n", name, mode, 0 );
169
170                 rc = 1;
171                 break;
172         }
173
174         if ( slap_controls_init( ) != 0 ) {
175                 slap_debug |= LDAP_DEBUG_NONE;
176                 Debug( LDAP_DEBUG_ANY,
177                     "%s: slap_controls_init failed\n",
178                     name, 0, 0 );
179                 return 1;
180         }
181
182 #ifdef HAVE_TLS
183         /* Library defaults to full certificate checking. This is correct when
184          * a client is verifying a server because all servers should have a
185          * valid cert. But few clients have valid certs, so we want our default
186          * to be no checking. The config file can override this as usual.
187          */
188         rc = 0;
189         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
190 #endif
191
192         if ( frontend_init() ) {
193                 slap_debug |= LDAP_DEBUG_NONE;
194                 Debug( LDAP_DEBUG_ANY,
195                     "%s: frontend_init failed\n",
196                     name, 0, 0 );
197                 return 1;
198         }
199
200         if ( overlay_init() ) {
201                 slap_debug |= LDAP_DEBUG_NONE;
202                 Debug( LDAP_DEBUG_ANY,
203                     "%s: overlay_init failed\n",
204                     name, 0, 0 );
205                 return 1;
206         }
207
208         if ( glue_sub_init() ) {
209                 slap_debug |= LDAP_DEBUG_NONE;
210                 Debug( LDAP_DEBUG_ANY,
211                     "%s: glue/subordinate init failed\n",
212                     name, 0, 0 );
213
214                 return 1;
215         }
216
217         if ( acl_init() ) {
218                 slap_debug |= LDAP_DEBUG_NONE;
219                 Debug( LDAP_DEBUG_ANY,
220                     "%s: acl_init failed\n",
221                     name, 0, 0 );
222                 return 1;
223         }
224
225         return rc;
226 }
227
228 int slap_startup( Backend *be )
229 {
230         Debug( LDAP_DEBUG_TRACE,
231                 "%s startup: initiated.\n",
232                 slap_name, 0, 0 );
233
234
235         return backend_startup( be );
236 }
237
238 int slap_shutdown( Backend *be )
239 {
240         Debug( LDAP_DEBUG_TRACE,
241                 "%s shutdown: initiated\n",
242                 slap_name, 0, 0 );
243
244         /* let backends do whatever cleanup they need to do */
245         return backend_shutdown( be ); 
246 }
247
248 int slap_destroy(void)
249 {
250         int rc;
251
252         Debug( LDAP_DEBUG_TRACE,
253                 "%s destroy: freeing system resources.\n",
254                 slap_name, 0, 0 );
255
256         if ( default_referral ) {
257                 ber_bvarray_free( default_referral );
258         }
259
260         /* clear out any thread-keys for the main thread */
261         ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());
262
263         rc = backend_destroy();
264
265         slap_sasl_destroy();
266
267         /* rootdse destroy goes before entry_destroy()
268          * because it may use entry_free() */
269         root_dse_destroy();
270         entry_destroy();
271
272         switch ( slapMode & SLAP_MODE ) {
273         case SLAP_SERVER_MODE:
274         case SLAP_TOOL_MODE:
275                 slap_counters_destroy( &slap_counters );
276                 break;
277
278         default:
279                 Debug( LDAP_DEBUG_ANY,
280                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
281
282                 rc = 1;
283                 break;
284
285         }
286
287         slap_op_destroy();
288
289         ldap_pvt_thread_destroy();
290
291         /* should destroy the above mutex */
292         return rc;
293 }
294
295 void slap_counters_init( slap_counters_t *sc )
296 {
297         int i;
298
299         ldap_pvt_thread_mutex_init( &sc->sc_mutex );
300         ldap_pvt_mp_init( sc->sc_bytes );
301         ldap_pvt_mp_init( sc->sc_pdu );
302         ldap_pvt_mp_init( sc->sc_entries );
303         ldap_pvt_mp_init( sc->sc_refs );
304
305         ldap_pvt_mp_init( sc->sc_ops_initiated );
306         ldap_pvt_mp_init( sc->sc_ops_completed );
307
308 #ifdef SLAPD_MONITOR
309         for ( i = 0; i < SLAP_OP_LAST; i++ ) {
310                 ldap_pvt_mp_init( sc->sc_ops_initiated_[ i ] );
311                 ldap_pvt_mp_init( sc->sc_ops_completed_[ i ] );
312         }
313 #endif /* SLAPD_MONITOR */
314 }
315
316 void slap_counters_destroy( slap_counters_t *sc )
317 {
318         int i;
319
320         ldap_pvt_thread_mutex_destroy( &sc->sc_mutex );
321         ldap_pvt_mp_clear( sc->sc_bytes );
322         ldap_pvt_mp_clear( sc->sc_pdu );
323         ldap_pvt_mp_clear( sc->sc_entries );
324         ldap_pvt_mp_clear( sc->sc_refs );
325
326         ldap_pvt_mp_clear( sc->sc_ops_initiated );
327         ldap_pvt_mp_clear( sc->sc_ops_completed );
328
329 #ifdef SLAPD_MONITOR
330         for ( i = 0; i < SLAP_OP_LAST; i++ ) {
331                 ldap_pvt_mp_clear( sc->sc_ops_initiated_[ i ] );
332                 ldap_pvt_mp_clear( sc->sc_ops_completed_[ i ] );
333         }
334 #endif /* SLAPD_MONITOR */
335 }
336