]> git.sur5r.net Git - openldap/blob - servers/slapd/init.c
make sure NULL pointers are not dereferenced
[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-2006 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 #ifndef HAVE_GMTIME_R
69 ldap_pvt_thread_mutex_t gmtime_mutex;
70 #endif
71
72 slap_counters_t                 slap_counters;
73
74 /*
75  * these mutexes must be used when calling the entry2str()
76  * routine since it returns a pointer to static data.
77  */
78 ldap_pvt_thread_mutex_t entry2str_mutex;
79 ldap_pvt_thread_mutex_t replog_mutex;
80
81 static const char* slap_name = NULL;
82 int slapMode = SLAP_UNDEFINED_MODE;
83
84 int
85 slap_init( int mode, const char *name )
86 {
87         int rc;
88         int i;
89
90         assert( mode );
91
92         if ( slapMode != SLAP_UNDEFINED_MODE ) {
93                 /* Make sure we write something to stderr */
94                 slap_debug |= LDAP_DEBUG_NONE;
95                 Debug( LDAP_DEBUG_ANY,
96                  "%s init: init called twice (old=%d, new=%d)\n",
97                  name, slapMode, mode );
98
99                 return 1;
100         }
101
102         slapMode = mode;
103
104 #ifdef SLAPD_MODULES
105         if ( module_init() != 0 ) {
106                 slap_debug |= LDAP_DEBUG_NONE;
107                 Debug( LDAP_DEBUG_ANY,
108                     "%s: module_init failed\n",
109                         name, 0, 0 );
110                 return 1;
111         }
112 #endif
113
114         if ( slap_schema_init( ) != 0 ) {
115                 slap_debug |= LDAP_DEBUG_NONE;
116                 Debug( LDAP_DEBUG_ANY,
117                     "%s: slap_schema_init failed\n",
118                     name, 0, 0 );
119                 return 1;
120         }
121
122
123         switch ( slapMode & SLAP_MODE ) {
124         case SLAP_SERVER_MODE:
125
126                 /* FALLTHRU */
127         case SLAP_TOOL_MODE:
128                 Debug( LDAP_DEBUG_TRACE,
129                         "%s init: initiated %s.\n",     name,
130                         (mode & SLAP_MODE) == SLAP_TOOL_MODE ? "tool" : "server",
131                         0 );
132
133                 slap_name = name;
134
135                 ldap_pvt_thread_pool_init( &connection_pool,
136                                 connection_pool_max, 0);
137                 ldap_pvt_thread_mutex_init( &entry2str_mutex );
138                 ldap_pvt_thread_mutex_init( &replog_mutex );
139
140                 ldap_pvt_thread_mutex_init( &slap_counters.sc_sent_mutex );
141                 ldap_pvt_thread_mutex_init( &slap_counters.sc_ops_mutex );
142                 ldap_pvt_mp_init( slap_counters.sc_bytes );
143                 ldap_pvt_mp_init( slap_counters.sc_pdu );
144                 ldap_pvt_mp_init( slap_counters.sc_entries );
145                 ldap_pvt_mp_init( slap_counters.sc_refs );
146
147                 ldap_pvt_mp_init( slap_counters.sc_ops_initiated );
148                 ldap_pvt_mp_init( slap_counters.sc_ops_completed );
149
150                 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
151                 LDAP_STAILQ_INIT( &slapd_rq.task_list );
152                 LDAP_STAILQ_INIT( &slapd_rq.run_list );
153
154 #ifdef SLAPD_MONITOR
155                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
156                         ldap_pvt_mp_init( slap_counters.sc_ops_initiated_[ i ] );
157                         ldap_pvt_mp_init( slap_counters.sc_ops_completed_[ i ] );
158                 }
159 #endif /* SLAPD_MONITOR */
160
161 #ifndef HAVE_GMTIME_R
162                 ldap_pvt_thread_mutex_init( &gmtime_mutex );
163 #endif
164                 slap_passwd_init();
165
166                 rc = slap_sasl_init();
167
168                 if( rc == 0 ) {
169                         rc = backend_init( );
170                 }
171
172                 break;
173
174         default:
175                 slap_debug |= LDAP_DEBUG_NONE;
176                 Debug( LDAP_DEBUG_ANY,
177                         "%s init: undefined mode (%d).\n", name, mode, 0 );
178
179                 rc = 1;
180                 break;
181         }
182
183         if ( slap_controls_init( ) != 0 ) {
184                 slap_debug |= LDAP_DEBUG_NONE;
185                 Debug( LDAP_DEBUG_ANY,
186                     "%s: slap_controls_init failed\n",
187                     name, 0, 0 );
188                 return 1;
189         }
190
191 #ifdef HAVE_TLS
192         /* Library defaults to full certificate checking. This is correct when
193          * a client is verifying a server because all servers should have a
194          * valid cert. But few clients have valid certs, so we want our default
195          * to be no checking. The config file can override this as usual.
196          */
197         rc = 0;
198         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
199 #endif
200
201         if ( frontend_init() ) {
202                 slap_debug |= LDAP_DEBUG_NONE;
203                 Debug( LDAP_DEBUG_ANY,
204                     "%s: frontend_init failed\n",
205                     name, 0, 0 );
206                 return 1;
207         }
208
209         if ( overlay_init() ) {
210                 slap_debug |= LDAP_DEBUG_NONE;
211                 Debug( LDAP_DEBUG_ANY,
212                     "%s: overlay_init failed\n",
213                     name, 0, 0 );
214                 return 1;
215         }
216
217         if ( glue_sub_init() ) {
218                 slap_debug |= LDAP_DEBUG_NONE;
219                 Debug( LDAP_DEBUG_ANY,
220                     "%s: glue/subordinate init failed\n",
221                     name, 0, 0 );
222
223                 return 1;
224         }
225
226         if ( acl_init() ) {
227                 slap_debug |= LDAP_DEBUG_NONE;
228                 Debug( LDAP_DEBUG_ANY,
229                     "%s: acl_init failed\n",
230                     name, 0, 0 );
231                 return 1;
232         }
233
234         return rc;
235 }
236
237 int slap_startup( Backend *be )
238 {
239         Debug( LDAP_DEBUG_TRACE,
240                 "%s startup: initiated.\n",
241                 slap_name, 0, 0 );
242
243
244         return backend_startup( be );
245 }
246
247 int slap_shutdown( Backend *be )
248 {
249         Debug( LDAP_DEBUG_TRACE,
250                 "%s shutdown: initiated\n",
251                 slap_name, 0, 0 );
252
253         /* let backends do whatever cleanup they need to do */
254         return backend_shutdown( be ); 
255 }
256
257 int slap_destroy(void)
258 {
259         int rc;
260         int i;
261
262         Debug( LDAP_DEBUG_TRACE,
263                 "%s destroy: freeing system resources.\n",
264                 slap_name, 0, 0 );
265
266         if ( default_referral ) {
267                 ber_bvarray_free( default_referral );
268         }
269
270         rc = backend_destroy();
271
272         slap_sasl_destroy();
273
274         entry_destroy();
275
276         switch ( slapMode & SLAP_MODE ) {
277         case SLAP_SERVER_MODE:
278         case SLAP_TOOL_MODE:
279
280                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_sent_mutex );
281                 ldap_pvt_thread_mutex_destroy( &slap_counters.sc_ops_mutex );
282                 ldap_pvt_mp_clear( slap_counters.sc_bytes );
283                 ldap_pvt_mp_clear( slap_counters.sc_pdu );
284                 ldap_pvt_mp_clear( slap_counters.sc_entries );
285                 ldap_pvt_mp_clear( slap_counters.sc_refs );
286                 ldap_pvt_mp_clear( slap_counters.sc_ops_initiated );
287                 ldap_pvt_mp_clear( slap_counters.sc_ops_completed );
288
289 #ifdef SLAPD_MONITOR
290                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
291                         ldap_pvt_mp_clear( slap_counters.sc_ops_initiated_[ i ] );
292                         ldap_pvt_mp_clear( slap_counters.sc_ops_completed_[ i ] );
293                 }
294 #endif /* SLAPD_MONITOR */
295                 break;
296
297         default:
298                 Debug( LDAP_DEBUG_ANY,
299                         "slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );
300
301                 rc = 1;
302                 break;
303
304         }
305
306         ldap_pvt_thread_destroy();
307
308         /* should destory the above mutex */
309         return rc;
310 }