main.c revision 1.1 1 1.1 lukem /* $OpenLDAP: pkg/ldap/servers/slapd/main.c,v 1.239.2.12 2008/02/12 00:46:46 quanah Exp $ */
2 1.1 lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 1.1 lukem *
4 1.1 lukem * Copyright 1998-2008 The OpenLDAP Foundation.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Redistribution and use in source and binary forms, with or without
8 1.1 lukem * modification, are permitted only as authorized by the OpenLDAP
9 1.1 lukem * Public License.
10 1.1 lukem *
11 1.1 lukem * A copy of this license is available in the file LICENSE in the
12 1.1 lukem * top-level directory of the distribution or, alternatively, at
13 1.1 lukem * <http://www.OpenLDAP.org/license.html>.
14 1.1 lukem */
15 1.1 lukem /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 1.1 lukem * All rights reserved.
17 1.1 lukem *
18 1.1 lukem * Redistribution and use in source and binary forms are permitted
19 1.1 lukem * provided that this notice is preserved and that due credit is given
20 1.1 lukem * to the University of Michigan at Ann Arbor. The name of the University
21 1.1 lukem * may not be used to endorse or promote products derived from this
22 1.1 lukem * software without specific prior written permission. This software
23 1.1 lukem * is provided ``as is'' without express or implied warranty.
24 1.1 lukem */
25 1.1 lukem
26 1.1 lukem #include "portable.h"
27 1.1 lukem
28 1.1 lukem #include <stdio.h>
29 1.1 lukem
30 1.1 lukem #include <ac/ctype.h>
31 1.1 lukem #include <ac/socket.h>
32 1.1 lukem #include <ac/string.h>
33 1.1 lukem #include <ac/time.h>
34 1.1 lukem #include <ac/unistd.h>
35 1.1 lukem #include <ac/wait.h>
36 1.1 lukem #include <ac/errno.h>
37 1.1 lukem
38 1.1 lukem #include "slap.h"
39 1.1 lukem #include "lutil.h"
40 1.1 lukem #include "ldif.h"
41 1.1 lukem
42 1.1 lukem #ifdef LDAP_SLAPI
43 1.1 lukem #include "slapi/slapi.h"
44 1.1 lukem #endif
45 1.1 lukem
46 1.1 lukem #ifdef LDAP_SIGCHLD
47 1.1 lukem static RETSIGTYPE wait4child( int sig );
48 1.1 lukem #endif
49 1.1 lukem
50 1.1 lukem #ifdef HAVE_NT_SERVICE_MANAGER
51 1.1 lukem #define MAIN_RETURN(x) return
52 1.1 lukem static struct sockaddr_in bind_addr;
53 1.1 lukem
54 1.1 lukem #define SERVICE_EXIT( e, n ) do { \
55 1.1 lukem if ( is_NT_Service ) { \
56 1.1 lukem lutil_ServiceStatus.dwWin32ExitCode = (e); \
57 1.1 lukem lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
58 1.1 lukem } \
59 1.1 lukem } while ( 0 )
60 1.1 lukem
61 1.1 lukem #else
62 1.1 lukem #define SERVICE_EXIT( e, n )
63 1.1 lukem #define MAIN_RETURN(x) return(x)
64 1.1 lukem #endif
65 1.1 lukem
66 1.1 lukem typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
67 1.1 lukem extern MainFunc slapadd, slapcat, slapdn, slapindex, slappasswd,
68 1.1 lukem slaptest, slapauth, slapacl;
69 1.1 lukem
70 1.1 lukem static struct {
71 1.1 lukem char *name;
72 1.1 lukem MainFunc *func;
73 1.1 lukem } tools[] = {
74 1.1 lukem {"slapadd", slapadd},
75 1.1 lukem {"slapcat", slapcat},
76 1.1 lukem {"slapdn", slapdn},
77 1.1 lukem {"slapindex", slapindex},
78 1.1 lukem {"slappasswd", slappasswd},
79 1.1 lukem {"slaptest", slaptest},
80 1.1 lukem {"slapauth", slapauth},
81 1.1 lukem {"slapacl", slapacl},
82 1.1 lukem /* NOTE: new tools must be added in chronological order,
83 1.1 lukem * not in alphabetical order, because for backwards
84 1.1 lukem * compatibility name[4] is used to identify the
85 1.1 lukem * tools; so name[4]=='a' must refer to "slapadd" and
86 1.1 lukem * not to "slapauth". Alphabetical order can be used
87 1.1 lukem * for tools whose name[4] is not used yet */
88 1.1 lukem {NULL, NULL}
89 1.1 lukem };
90 1.1 lukem
91 1.1 lukem /*
92 1.1 lukem * when more than one slapd is running on one machine, each one might have
93 1.1 lukem * it's own LOCAL for syslogging and must have its own pid/args files
94 1.1 lukem */
95 1.1 lukem
96 1.1 lukem #ifndef HAVE_MKVERSION
97 1.1 lukem const char Versionstr[] =
98 1.1 lukem OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
99 1.1 lukem #endif
100 1.1 lukem
101 1.1 lukem #define CHECK_NONE 0x00
102 1.1 lukem #define CHECK_CONFIG 0x01
103 1.1 lukem #define CHECK_LOGLEVEL 0x02
104 1.1 lukem static int check = CHECK_NONE;
105 1.1 lukem static int version = 0;
106 1.1 lukem
107 1.1 lukem void *slap_tls_ctx;
108 1.1 lukem LDAP *slap_tls_ld;
109 1.1 lukem
110 1.1 lukem static int
111 1.1 lukem slapd_opt_slp( const char *val, void *arg )
112 1.1 lukem {
113 1.1 lukem #ifdef HAVE_SLP
114 1.1 lukem /* NULL is default */
115 1.1 lukem if ( val == NULL || *val == '(' || strcasecmp( val, "on" ) == 0 ) {
116 1.1 lukem slapd_register_slp = 1;
117 1.1 lukem slapd_slp_attrs = (val != NULL && *val == '(') ? val : NULL;
118 1.1 lukem
119 1.1 lukem } else if ( strcasecmp( val, "off" ) == 0 ) {
120 1.1 lukem slapd_register_slp = 0;
121 1.1 lukem
122 1.1 lukem /* NOTE: add support for URL specification? */
123 1.1 lukem
124 1.1 lukem } else {
125 1.1 lukem fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
126 1.1 lukem return -1;
127 1.1 lukem }
128 1.1 lukem
129 1.1 lukem return 0;
130 1.1 lukem
131 1.1 lukem #else
132 1.1 lukem fputs( "slapd: SLP support is not available\n", stderr );
133 1.1 lukem return 0;
134 1.1 lukem #endif
135 1.1 lukem }
136 1.1 lukem
137 1.1 lukem /*
138 1.1 lukem * Option helper structure:
139 1.1 lukem *
140 1.1 lukem * oh_nam is left-hand part of <option>[=<value>]
141 1.1 lukem * oh_fnc is handler function
142 1.1 lukem * oh_arg is an optional arg to oh_fnc
143 1.1 lukem * oh_usage is the one-line usage string related to the option,
144 1.1 lukem * which is assumed to start with <option>[=<value>]
145 1.1 lukem *
146 1.1 lukem * please leave valid options in the structure, and optionally #ifdef
147 1.1 lukem * their processing inside the helper, so that reasonable and helpful
148 1.1 lukem * error messages can be generated if a disabled option is requested.
149 1.1 lukem */
150 1.1 lukem struct option_helper {
151 1.1 lukem struct berval oh_name;
152 1.1 lukem int (*oh_fnc)(const char *val, void *arg);
153 1.1 lukem void *oh_arg;
154 1.1 lukem const char *oh_usage;
155 1.1 lukem } option_helpers[] = {
156 1.1 lukem { BER_BVC("slp"), slapd_opt_slp, NULL, "slp[={on|off|(attrs)}] enable/disable SLP using (attrs)" },
157 1.1 lukem { BER_BVNULL, 0, NULL, NULL }
158 1.1 lukem };
159 1.1 lukem
160 1.1 lukem #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
161 1.1 lukem #ifdef LOG_LOCAL4
162 1.1 lukem int
163 1.1 lukem parse_syslog_user( const char *arg, int *syslogUser )
164 1.1 lukem {
165 1.1 lukem static slap_verbmasks syslogUsers[] = {
166 1.1 lukem { BER_BVC( "LOCAL0" ), LOG_LOCAL0 },
167 1.1 lukem { BER_BVC( "LOCAL1" ), LOG_LOCAL1 },
168 1.1 lukem { BER_BVC( "LOCAL2" ), LOG_LOCAL2 },
169 1.1 lukem { BER_BVC( "LOCAL3" ), LOG_LOCAL3 },
170 1.1 lukem { BER_BVC( "LOCAL4" ), LOG_LOCAL4 },
171 1.1 lukem { BER_BVC( "LOCAL5" ), LOG_LOCAL5 },
172 1.1 lukem { BER_BVC( "LOCAL6" ), LOG_LOCAL6 },
173 1.1 lukem { BER_BVC( "LOCAL7" ), LOG_LOCAL7 },
174 1.1 lukem #ifdef LOG_USER
175 1.1 lukem { BER_BVC( "USER" ), LOG_USER },
176 1.1 lukem #endif /* LOG_USER */
177 1.1 lukem #ifdef LOG_DAEMON
178 1.1 lukem { BER_BVC( "DAEMON" ), LOG_DAEMON },
179 1.1 lukem #endif /* LOG_DAEMON */
180 1.1 lukem { BER_BVNULL, 0 }
181 1.1 lukem };
182 1.1 lukem int i = verb_to_mask( arg, syslogUsers );
183 1.1 lukem
184 1.1 lukem if ( BER_BVISNULL( &syslogUsers[ i ].word ) ) {
185 1.1 lukem Debug( LDAP_DEBUG_ANY,
186 1.1 lukem "unrecognized syslog user \"%s\".\n",
187 1.1 lukem arg, 0, 0 );
188 1.1 lukem return 1;
189 1.1 lukem }
190 1.1 lukem
191 1.1 lukem *syslogUser = syslogUsers[ i ].mask;
192 1.1 lukem
193 1.1 lukem return 0;
194 1.1 lukem }
195 1.1 lukem #endif /* LOG_LOCAL4 */
196 1.1 lukem
197 1.1 lukem int
198 1.1 lukem parse_syslog_level( const char *arg, int *levelp )
199 1.1 lukem {
200 1.1 lukem static slap_verbmasks str2syslog_level[] = {
201 1.1 lukem { BER_BVC( "EMERG" ), LOG_EMERG },
202 1.1 lukem { BER_BVC( "ALERT" ), LOG_ALERT },
203 1.1 lukem { BER_BVC( "CRIT" ), LOG_CRIT },
204 1.1 lukem { BER_BVC( "ERR" ), LOG_ERR },
205 1.1 lukem { BER_BVC( "WARNING" ), LOG_WARNING },
206 1.1 lukem { BER_BVC( "NOTICE" ), LOG_NOTICE },
207 1.1 lukem { BER_BVC( "INFO" ), LOG_INFO },
208 1.1 lukem { BER_BVC( "DEBUG" ), LOG_DEBUG },
209 1.1 lukem { BER_BVNULL, 0 }
210 1.1 lukem };
211 1.1 lukem int i = verb_to_mask( arg, str2syslog_level );
212 1.1 lukem if ( BER_BVISNULL( &str2syslog_level[ i ].word ) ) {
213 1.1 lukem Debug( LDAP_DEBUG_ANY,
214 1.1 lukem "unknown syslog level \"%s\".\n",
215 1.1 lukem arg, 0, 0 );
216 1.1 lukem return 1;
217 1.1 lukem }
218 1.1 lukem
219 1.1 lukem *levelp = str2syslog_level[ i ].mask;
220 1.1 lukem
221 1.1 lukem return 0;
222 1.1 lukem }
223 1.1 lukem #endif /* LDAP_DEBUG && LDAP_SYSLOG */
224 1.1 lukem
225 1.1 lukem int
226 1.1 lukem parse_debug_unknowns( char **unknowns, int *levelp )
227 1.1 lukem {
228 1.1 lukem int i, level, rc = 0;
229 1.1 lukem
230 1.1 lukem for ( i = 0; unknowns[ i ] != NULL; i++ ) {
231 1.1 lukem level = 0;
232 1.1 lukem if ( str2loglevel( unknowns[ i ], &level )) {
233 1.1 lukem fprintf( stderr,
234 1.1 lukem "unrecognized log level \"%s\"\n", unknowns[ i ] );
235 1.1 lukem rc = 1;
236 1.1 lukem } else {
237 1.1 lukem *levelp |= level;
238 1.1 lukem }
239 1.1 lukem }
240 1.1 lukem return rc;
241 1.1 lukem }
242 1.1 lukem
243 1.1 lukem int
244 1.1 lukem parse_debug_level( const char *arg, int *levelp, char ***unknowns )
245 1.1 lukem {
246 1.1 lukem int level;
247 1.1 lukem
248 1.1 lukem if ( arg && arg[ 0 ] != '-' && !isdigit( (unsigned char) arg[ 0 ] ) )
249 1.1 lukem {
250 1.1 lukem int i;
251 1.1 lukem char **levels;
252 1.1 lukem
253 1.1 lukem levels = ldap_str2charray( arg, "," );
254 1.1 lukem
255 1.1 lukem for ( i = 0; levels[ i ] != NULL; i++ ) {
256 1.1 lukem level = 0;
257 1.1 lukem
258 1.1 lukem if ( str2loglevel( levels[ i ], &level ) ) {
259 1.1 lukem /* remember this for later */
260 1.1 lukem ldap_charray_add( unknowns, levels[ i ] );
261 1.1 lukem fprintf( stderr,
262 1.1 lukem "unrecognized log level \"%s\" (deferred)\n",
263 1.1 lukem levels[ i ] );
264 1.1 lukem } else {
265 1.1 lukem *levelp |= level;
266 1.1 lukem }
267 1.1 lukem }
268 1.1 lukem
269 1.1 lukem ldap_charray_free( levels );
270 1.1 lukem
271 1.1 lukem } else {
272 1.1 lukem if ( lutil_atoix( &level, arg, 0 ) != 0 ) {
273 1.1 lukem fprintf( stderr,
274 1.1 lukem "unrecognized log level "
275 1.1 lukem "\"%s\"\n", arg );
276 1.1 lukem return 1;
277 1.1 lukem }
278 1.1 lukem
279 1.1 lukem if ( level == 0 ) {
280 1.1 lukem *levelp = 0;
281 1.1 lukem
282 1.1 lukem } else {
283 1.1 lukem *levelp |= level;
284 1.1 lukem }
285 1.1 lukem }
286 1.1 lukem
287 1.1 lukem return 0;
288 1.1 lukem }
289 1.1 lukem
290 1.1 lukem static void
291 1.1 lukem usage( char *name )
292 1.1 lukem {
293 1.1 lukem fprintf( stderr,
294 1.1 lukem "usage: %s options\n", name );
295 1.1 lukem fprintf( stderr,
296 1.1 lukem "\t-4\t\tIPv4 only\n"
297 1.1 lukem "\t-6\t\tIPv6 only\n"
298 1.1 lukem "\t-T {acl|add|auth|cat|dn|index|passwd|test}\n"
299 1.1 lukem "\t\t\tRun in Tool mode\n"
300 1.1 lukem "\t-c cookie\tSync cookie of consumer\n"
301 1.1 lukem "\t-d level\tDebug level" "\n"
302 1.1 lukem "\t-f filename\tConfiguration file\n"
303 1.1 lukem "\t-F dir\tConfiguration directory\n"
304 1.1 lukem #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
305 1.1 lukem "\t-g group\tGroup (id or name) to run as\n"
306 1.1 lukem #endif
307 1.1 lukem "\t-h URLs\t\tList of URLs to serve\n"
308 1.1 lukem #ifdef SLAP_DEFAULT_SYSLOG_USER
309 1.1 lukem "\t-l facility\tSyslog facility (default: LOCAL4)\n"
310 1.1 lukem #endif
311 1.1 lukem "\t-n serverName\tService name\n"
312 1.1 lukem "\t-o <opt>[=val] generic means to specify options" );
313 1.1 lukem if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
314 1.1 lukem int i;
315 1.1 lukem
316 1.1 lukem fprintf( stderr, "; supported options:\n" );
317 1.1 lukem for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
318 1.1 lukem fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
319 1.1 lukem }
320 1.1 lukem } else {
321 1.1 lukem fprintf( stderr, "\n" );
322 1.1 lukem }
323 1.1 lukem fprintf( stderr,
324 1.1 lukem #ifdef HAVE_CHROOT
325 1.1 lukem "\t-r directory\tSandbox directory to chroot to\n"
326 1.1 lukem #endif
327 1.1 lukem "\t-s level\tSyslog level\n"
328 1.1 lukem #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
329 1.1 lukem "\t-u user\t\tUser (id or name) to run as\n"
330 1.1 lukem #endif
331 1.1 lukem "\t-V\t\tprint version info (-VV only)\n"
332 1.1 lukem );
333 1.1 lukem }
334 1.1 lukem
335 1.1 lukem #ifdef HAVE_NT_SERVICE_MANAGER
336 1.1 lukem void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
337 1.1 lukem #else
338 1.1 lukem int main( int argc, char **argv )
339 1.1 lukem #endif
340 1.1 lukem {
341 1.1 lukem int i, no_detach = 0;
342 1.1 lukem int rc = 1;
343 1.1 lukem char *urls = NULL;
344 1.1 lukem #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
345 1.1 lukem char *username = NULL;
346 1.1 lukem char *groupname = NULL;
347 1.1 lukem #endif
348 1.1 lukem #if defined(HAVE_CHROOT)
349 1.1 lukem char *sandbox = NULL;
350 1.1 lukem #endif
351 1.1 lukem #ifdef SLAP_DEFAULT_SYSLOG_USER
352 1.1 lukem int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
353 1.1 lukem #endif
354 1.1 lukem
355 1.1 lukem int g_argc = argc;
356 1.1 lukem char **g_argv = argv;
357 1.1 lukem
358 1.1 lukem char *configfile = NULL;
359 1.1 lukem char *configdir = NULL;
360 1.1 lukem char *serverName;
361 1.1 lukem int serverMode = SLAP_SERVER_MODE;
362 1.1 lukem
363 1.1 lukem struct sync_cookie *scp = NULL;
364 1.1 lukem struct sync_cookie *scp_entry = NULL;
365 1.1 lukem
366 1.1 lukem char **debug_unknowns = NULL;
367 1.1 lukem char **syslog_unknowns = NULL;
368 1.1 lukem
369 1.1 lukem char *serverNamePrefix = "";
370 1.1 lukem size_t l;
371 1.1 lukem
372 1.1 lukem int slapd_pid_file_unlink = 0, slapd_args_file_unlink = 0;
373 1.1 lukem int firstopt = 1;
374 1.1 lukem
375 1.1 lukem #ifdef CSRIMALLOC
376 1.1 lukem FILE *leakfile;
377 1.1 lukem if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
378 1.1 lukem leakfile = stderr;
379 1.1 lukem }
380 1.1 lukem #endif
381 1.1 lukem
382 1.1 lukem slap_sl_mem_init();
383 1.1 lukem
384 1.1 lukem (void) ldap_pvt_thread_initialize();
385 1.1 lukem
386 1.1 lukem serverName = lutil_progname( "slapd", argc, argv );
387 1.1 lukem
388 1.1 lukem if ( strcmp( serverName, "slapd" ) ) {
389 1.1 lukem for (i=0; tools[i].name; i++) {
390 1.1 lukem if ( !strcmp( serverName, tools[i].name ) ) {
391 1.1 lukem rc = tools[i].func(argc, argv);
392 1.1 lukem MAIN_RETURN(rc);
393 1.1 lukem }
394 1.1 lukem }
395 1.1 lukem }
396 1.1 lukem
397 1.1 lukem #ifdef HAVE_NT_SERVICE_MANAGER
398 1.1 lukem {
399 1.1 lukem int *ip;
400 1.1 lukem char *newConfigFile;
401 1.1 lukem char *newConfigDir;
402 1.1 lukem char *newUrls;
403 1.1 lukem char *regService = NULL;
404 1.1 lukem
405 1.1 lukem if ( is_NT_Service ) {
406 1.1 lukem lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
407 1.1 lukem if ( strcmp(serverName, SERVICE_NAME) )
408 1.1 lukem regService = serverName;
409 1.1 lukem }
410 1.1 lukem
411 1.1 lukem ip = (int*)lutil_getRegParam( regService, "DebugLevel" );
412 1.1 lukem if ( ip != NULL ) {
413 1.1 lukem slap_debug = *ip;
414 1.1 lukem Debug( LDAP_DEBUG_ANY,
415 1.1 lukem "new debug level from registry is: %d\n", slap_debug, 0, 0 );
416 1.1 lukem }
417 1.1 lukem
418 1.1 lukem newUrls = (char *) lutil_getRegParam(regService, "Urls");
419 1.1 lukem if (newUrls) {
420 1.1 lukem if (urls)
421 1.1 lukem ch_free(urls);
422 1.1 lukem
423 1.1 lukem urls = ch_strdup(newUrls);
424 1.1 lukem Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
425 1.1 lukem urls, 0, 0);
426 1.1 lukem }
427 1.1 lukem
428 1.1 lukem newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
429 1.1 lukem if ( newConfigFile != NULL ) {
430 1.1 lukem configfile = newConfigFile;
431 1.1 lukem Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
432 1.1 lukem }
433 1.1 lukem
434 1.1 lukem newConfigDir = (char*)lutil_getRegParam( regService, "ConfigDir" );
435 1.1 lukem if ( newConfigDir != NULL ) {
436 1.1 lukem configdir = newConfigDir;
437 1.1 lukem Debug ( LDAP_DEBUG_ANY, "new config dir from registry is: %s\n", configdir, 0, 0 );
438 1.1 lukem }
439 1.1 lukem }
440 1.1 lukem #endif
441 1.1 lukem
442 1.1 lukem while ( (i = getopt( argc, argv,
443 1.1 lukem "c:d:f:F:h:n:o:s:tT:V"
444 1.1 lukem #ifdef LDAP_PF_INET6
445 1.1 lukem "46"
446 1.1 lukem #endif
447 1.1 lukem #ifdef HAVE_CHROOT
448 1.1 lukem "r:"
449 1.1 lukem #endif
450 1.1 lukem #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
451 1.1 lukem "S:"
452 1.1 lukem #ifdef LOG_LOCAL4
453 1.1 lukem "l:"
454 1.1 lukem #endif
455 1.1 lukem #endif
456 1.1 lukem #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
457 1.1 lukem "u:g:"
458 1.1 lukem #endif
459 1.1 lukem )) != EOF ) {
460 1.1 lukem switch ( i ) {
461 1.1 lukem #ifdef LDAP_PF_INET6
462 1.1 lukem case '4':
463 1.1 lukem slap_inet4or6 = AF_INET;
464 1.1 lukem break;
465 1.1 lukem case '6':
466 1.1 lukem slap_inet4or6 = AF_INET6;
467 1.1 lukem break;
468 1.1 lukem #endif
469 1.1 lukem
470 1.1 lukem case 'h': /* listen URLs */
471 1.1 lukem if ( urls != NULL ) free( urls );
472 1.1 lukem urls = ch_strdup( optarg );
473 1.1 lukem break;
474 1.1 lukem
475 1.1 lukem case 'c': /* provide sync cookie, override if exist in replica */
476 1.1 lukem scp = (struct sync_cookie *) ch_calloc( 1,
477 1.1 lukem sizeof( struct sync_cookie ));
478 1.1 lukem ber_str2bv( optarg, 0, 1, &scp->octet_str );
479 1.1 lukem
480 1.1 lukem /* This only parses out the rid at this point */
481 1.1 lukem slap_parse_sync_cookie( scp, NULL );
482 1.1 lukem
483 1.1 lukem if ( scp->rid == -1 ) {
484 1.1 lukem Debug( LDAP_DEBUG_ANY,
485 1.1 lukem "main: invalid cookie \"%s\"\n",
486 1.1 lukem optarg, 0, 0 );
487 1.1 lukem slap_sync_cookie_free( scp, 1 );
488 1.1 lukem goto destroy;
489 1.1 lukem }
490 1.1 lukem
491 1.1 lukem LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
492 1.1 lukem if ( scp->rid == scp_entry->rid ) {
493 1.1 lukem Debug( LDAP_DEBUG_ANY,
494 1.1 lukem "main: duplicated replica id in cookies\n",
495 1.1 lukem 0, 0, 0 );
496 1.1 lukem slap_sync_cookie_free( scp, 1 );
497 1.1 lukem goto destroy;
498 1.1 lukem }
499 1.1 lukem }
500 1.1 lukem LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
501 1.1 lukem break;
502 1.1 lukem
503 1.1 lukem case 'd': { /* set debug level and 'do not detach' flag */
504 1.1 lukem int level = 0;
505 1.1 lukem
506 1.1 lukem if ( strcmp( optarg, "?" ) == 0 ) {
507 1.1 lukem check |= CHECK_LOGLEVEL;
508 1.1 lukem break;
509 1.1 lukem }
510 1.1 lukem
511 1.1 lukem no_detach = 1;
512 1.1 lukem if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
513 1.1 lukem goto destroy;
514 1.1 lukem }
515 1.1 lukem #ifdef LDAP_DEBUG
516 1.1 lukem slap_debug |= level;
517 1.1 lukem #else
518 1.1 lukem if ( level != 0 )
519 1.1 lukem fputs( "must compile with LDAP_DEBUG for debugging\n",
520 1.1 lukem stderr );
521 1.1 lukem #endif
522 1.1 lukem } break;
523 1.1 lukem
524 1.1 lukem case 'f': /* read config file */
525 1.1 lukem configfile = ch_strdup( optarg );
526 1.1 lukem break;
527 1.1 lukem
528 1.1 lukem case 'F': /* use config dir */
529 1.1 lukem configdir = ch_strdup( optarg );
530 1.1 lukem break;
531 1.1 lukem
532 1.1 lukem case 'o': {
533 1.1 lukem char *val = strchr( optarg, '=' );
534 1.1 lukem struct berval opt;
535 1.1 lukem
536 1.1 lukem opt.bv_val = optarg;
537 1.1 lukem
538 1.1 lukem if ( val ) {
539 1.1 lukem opt.bv_len = ( val - optarg );
540 1.1 lukem val++;
541 1.1 lukem
542 1.1 lukem } else {
543 1.1 lukem opt.bv_len = strlen( optarg );
544 1.1 lukem }
545 1.1 lukem
546 1.1 lukem for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
547 1.1 lukem if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
548 1.1 lukem assert( option_helpers[i].oh_fnc != NULL );
549 1.1 lukem if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
550 1.1 lukem /* we assume the option parsing helper
551 1.1 lukem * issues appropriate and self-explanatory
552 1.1 lukem * error messages... */
553 1.1 lukem goto stop;
554 1.1 lukem }
555 1.1 lukem break;
556 1.1 lukem }
557 1.1 lukem }
558 1.1 lukem
559 1.1 lukem if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
560 1.1 lukem goto unhandled_option;
561 1.1 lukem }
562 1.1 lukem break;
563 1.1 lukem }
564 1.1 lukem
565 1.1 lukem case 's': /* set syslog level */
566 1.1 lukem if ( strcmp( optarg, "?" ) == 0 ) {
567 1.1 lukem check |= CHECK_LOGLEVEL;
568 1.1 lukem break;
569 1.1 lukem }
570 1.1 lukem
571 1.1 lukem if ( parse_debug_level( optarg, &ldap_syslog, &syslog_unknowns ) ) {
572 1.1 lukem goto destroy;
573 1.1 lukem }
574 1.1 lukem break;
575 1.1 lukem
576 1.1 lukem #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
577 1.1 lukem case 'S':
578 1.1 lukem if ( parse_syslog_level( optarg, &ldap_syslog_level ) ) {
579 1.1 lukem goto destroy;
580 1.1 lukem }
581 1.1 lukem break;
582 1.1 lukem
583 1.1 lukem #ifdef LOG_LOCAL4
584 1.1 lukem case 'l': /* set syslog local user */
585 1.1 lukem if ( parse_syslog_user( optarg, &syslogUser ) ) {
586 1.1 lukem goto destroy;
587 1.1 lukem }
588 1.1 lukem break;
589 1.1 lukem #endif
590 1.1 lukem #endif /* LDAP_DEBUG && LDAP_SYSLOG */
591 1.1 lukem
592 1.1 lukem #ifdef HAVE_CHROOT
593 1.1 lukem case 'r':
594 1.1 lukem if( sandbox ) free(sandbox);
595 1.1 lukem sandbox = ch_strdup( optarg );
596 1.1 lukem break;
597 1.1 lukem #endif
598 1.1 lukem
599 1.1 lukem #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
600 1.1 lukem case 'u': /* user name */
601 1.1 lukem if( username ) free(username);
602 1.1 lukem username = ch_strdup( optarg );
603 1.1 lukem break;
604 1.1 lukem
605 1.1 lukem case 'g': /* group name */
606 1.1 lukem if( groupname ) free(groupname);
607 1.1 lukem groupname = ch_strdup( optarg );
608 1.1 lukem break;
609 1.1 lukem #endif /* SETUID && GETUID */
610 1.1 lukem
611 1.1 lukem case 'n': /* NT service name */
612 1.1 lukem serverName = ch_strdup( optarg );
613 1.1 lukem break;
614 1.1 lukem
615 1.1 lukem case 't':
616 1.1 lukem /* deprecated; use slaptest instead */
617 1.1 lukem fprintf( stderr, "option -t deprecated; "
618 1.1 lukem "use slaptest command instead\n" );
619 1.1 lukem check |= CHECK_CONFIG;
620 1.1 lukem break;
621 1.1 lukem
622 1.1 lukem case 'V':
623 1.1 lukem version++;
624 1.1 lukem break;
625 1.1 lukem
626 1.1 lukem case 'T':
627 1.1 lukem if ( firstopt == 0 ) {
628 1.1 lukem fprintf( stderr, "warning: \"-T %s\" "
629 1.1 lukem "should be the first option.\n",
630 1.1 lukem optarg );
631 1.1 lukem }
632 1.1 lukem
633 1.1 lukem /* try full option string first */
634 1.1 lukem for ( i = 0; tools[i].name; i++ ) {
635 1.1 lukem if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
636 1.1 lukem rc = tools[i].func( argc, argv );
637 1.1 lukem MAIN_RETURN( rc );
638 1.1 lukem }
639 1.1 lukem }
640 1.1 lukem
641 1.1 lukem /* try bits of option string (backward compatibility for single char) */
642 1.1 lukem l = strlen( optarg );
643 1.1 lukem for ( i = 0; tools[i].name; i++ ) {
644 1.1 lukem if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
645 1.1 lukem rc = tools[i].func( argc, argv );
646 1.1 lukem MAIN_RETURN( rc );
647 1.1 lukem }
648 1.1 lukem }
649 1.1 lukem
650 1.1 lukem /* issue error */
651 1.1 lukem serverName = optarg;
652 1.1 lukem serverNamePrefix = "slap";
653 1.1 lukem fprintf( stderr, "program name \"%s%s\" unrecognized; "
654 1.1 lukem "aborting...\n", serverNamePrefix, serverName );
655 1.1 lukem /* FALLTHRU */
656 1.1 lukem default:
657 1.1 lukem unhandled_option:;
658 1.1 lukem usage( argv[0] );
659 1.1 lukem rc = 1;
660 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
661 1.1 lukem goto stop;
662 1.1 lukem }
663 1.1 lukem
664 1.1 lukem if ( firstopt ) {
665 1.1 lukem firstopt = 0;
666 1.1 lukem }
667 1.1 lukem }
668 1.1 lukem
669 1.1 lukem ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
670 1.1 lukem ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
671 1.1 lukem ldif_debug = slap_debug;
672 1.1 lukem
673 1.1 lukem if ( version ) {
674 1.1 lukem fprintf( stderr, "%s\n", Versionstr );
675 1.1 lukem if ( version > 1 ) goto stop;
676 1.1 lukem }
677 1.1 lukem
678 1.1 lukem #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
679 1.1 lukem {
680 1.1 lukem char *logName;
681 1.1 lukem #ifdef HAVE_EBCDIC
682 1.1 lukem logName = ch_strdup( serverName );
683 1.1 lukem __atoe( logName );
684 1.1 lukem #else
685 1.1 lukem logName = serverName;
686 1.1 lukem #endif
687 1.1 lukem
688 1.1 lukem #ifdef LOG_LOCAL4
689 1.1 lukem openlog( logName, OPENLOG_OPTIONS, syslogUser );
690 1.1 lukem #elif defined LOG_DEBUG
691 1.1 lukem openlog( logName, OPENLOG_OPTIONS );
692 1.1 lukem #endif
693 1.1 lukem #ifdef HAVE_EBCDIC
694 1.1 lukem free( logName );
695 1.1 lukem #endif
696 1.1 lukem }
697 1.1 lukem #endif /* LDAP_DEBUG && LDAP_SYSLOG */
698 1.1 lukem
699 1.1 lukem Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
700 1.1 lukem
701 1.1 lukem global_host = ldap_pvt_get_fqdn( NULL );
702 1.1 lukem
703 1.1 lukem if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
704 1.1 lukem rc = 1;
705 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
706 1.1 lukem goto stop;
707 1.1 lukem }
708 1.1 lukem
709 1.1 lukem #if defined(HAVE_CHROOT)
710 1.1 lukem if ( sandbox ) {
711 1.1 lukem if ( chdir( sandbox ) ) {
712 1.1 lukem perror("chdir");
713 1.1 lukem rc = 1;
714 1.1 lukem goto stop;
715 1.1 lukem }
716 1.1 lukem if ( chroot( sandbox ) ) {
717 1.1 lukem perror("chroot");
718 1.1 lukem rc = 1;
719 1.1 lukem goto stop;
720 1.1 lukem }
721 1.1 lukem }
722 1.1 lukem #endif
723 1.1 lukem
724 1.1 lukem #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
725 1.1 lukem if ( username != NULL || groupname != NULL ) {
726 1.1 lukem slap_init_user( username, groupname );
727 1.1 lukem }
728 1.1 lukem #endif
729 1.1 lukem
730 1.1 lukem extops_init();
731 1.1 lukem lutil_passwd_init();
732 1.1 lukem
733 1.1 lukem #ifdef HAVE_TLS
734 1.1 lukem rc = ldap_create( &slap_tls_ld );
735 1.1 lukem if ( rc ) {
736 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
737 1.1 lukem goto destroy;
738 1.1 lukem }
739 1.1 lukem /* Library defaults to full certificate checking. This is correct when
740 1.1 lukem * a client is verifying a server because all servers should have a
741 1.1 lukem * valid cert. But few clients have valid certs, so we want our default
742 1.1 lukem * to be no checking. The config file can override this as usual.
743 1.1 lukem */
744 1.1 lukem rc = LDAP_OPT_X_TLS_NEVER;
745 1.1 lukem (void) ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
746 1.1 lukem #endif
747 1.1 lukem
748 1.1 lukem rc = slap_init( serverMode, serverName );
749 1.1 lukem if ( rc ) {
750 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
751 1.1 lukem goto destroy;
752 1.1 lukem }
753 1.1 lukem
754 1.1 lukem if ( read_config( configfile, configdir ) != 0 ) {
755 1.1 lukem rc = 1;
756 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
757 1.1 lukem
758 1.1 lukem if ( check & CHECK_CONFIG ) {
759 1.1 lukem fprintf( stderr, "config check failed\n" );
760 1.1 lukem }
761 1.1 lukem
762 1.1 lukem goto destroy;
763 1.1 lukem }
764 1.1 lukem
765 1.1 lukem if ( debug_unknowns ) {
766 1.1 lukem rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
767 1.1 lukem ldap_charray_free( debug_unknowns );
768 1.1 lukem debug_unknowns = NULL;
769 1.1 lukem if ( rc )
770 1.1 lukem goto destroy;
771 1.1 lukem }
772 1.1 lukem if ( syslog_unknowns ) {
773 1.1 lukem rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
774 1.1 lukem ldap_charray_free( syslog_unknowns );
775 1.1 lukem syslog_unknowns = NULL;
776 1.1 lukem if ( rc )
777 1.1 lukem goto destroy;
778 1.1 lukem }
779 1.1 lukem
780 1.1 lukem if ( check & CHECK_LOGLEVEL ) {
781 1.1 lukem rc = 0;
782 1.1 lukem goto destroy;
783 1.1 lukem }
784 1.1 lukem
785 1.1 lukem if ( check & CHECK_CONFIG ) {
786 1.1 lukem fprintf( stderr, "config check succeeded\n" );
787 1.1 lukem
788 1.1 lukem check &= ~CHECK_CONFIG;
789 1.1 lukem if ( check == CHECK_NONE ) {
790 1.1 lukem rc = 0;
791 1.1 lukem goto destroy;
792 1.1 lukem }
793 1.1 lukem }
794 1.1 lukem
795 1.1 lukem if ( glue_sub_attach( ) != 0 ) {
796 1.1 lukem Debug( LDAP_DEBUG_ANY,
797 1.1 lukem "subordinate config error\n",
798 1.1 lukem 0, 0, 0 );
799 1.1 lukem
800 1.1 lukem goto destroy;
801 1.1 lukem }
802 1.1 lukem
803 1.1 lukem if ( slap_schema_check( ) != 0 ) {
804 1.1 lukem Debug( LDAP_DEBUG_ANY,
805 1.1 lukem "schema prep error\n",
806 1.1 lukem 0, 0, 0 );
807 1.1 lukem
808 1.1 lukem goto destroy;
809 1.1 lukem }
810 1.1 lukem
811 1.1 lukem #ifdef HAVE_TLS
812 1.1 lukem rc = ldap_pvt_tls_init();
813 1.1 lukem if( rc != 0) {
814 1.1 lukem Debug( LDAP_DEBUG_ANY,
815 1.1 lukem "main: TLS init failed: %d\n",
816 1.1 lukem 0, 0, 0 );
817 1.1 lukem rc = 1;
818 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
819 1.1 lukem goto destroy;
820 1.1 lukem }
821 1.1 lukem
822 1.1 lukem {
823 1.1 lukem int opt = 1;
824 1.1 lukem
825 1.1 lukem /* Force new ctx to be created */
826 1.1 lukem rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
827 1.1 lukem if( rc == 0 ) {
828 1.1 lukem /* The ctx's refcount is bumped up here */
829 1.1 lukem ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
830 1.1 lukem load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
831 1.1 lukem } else if ( rc != LDAP_NOT_SUPPORTED ) {
832 1.1 lukem Debug( LDAP_DEBUG_ANY,
833 1.1 lukem "main: TLS init def ctx failed: %d\n",
834 1.1 lukem rc, 0, 0 );
835 1.1 lukem rc = 1;
836 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
837 1.1 lukem goto destroy;
838 1.1 lukem }
839 1.1 lukem }
840 1.1 lukem #endif
841 1.1 lukem
842 1.1 lukem #ifdef HAVE_CYRUS_SASL
843 1.1 lukem if( sasl_host == NULL ) {
844 1.1 lukem sasl_host = ch_strdup( global_host );
845 1.1 lukem }
846 1.1 lukem #endif
847 1.1 lukem
848 1.1 lukem (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
849 1.1 lukem (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
850 1.1 lukem
851 1.1 lukem #ifdef SIGPIPE
852 1.1 lukem (void) SIGNAL( SIGPIPE, SIG_IGN );
853 1.1 lukem #endif
854 1.1 lukem #ifdef SIGHUP
855 1.1 lukem (void) SIGNAL( SIGHUP, slap_sig_shutdown );
856 1.1 lukem #endif
857 1.1 lukem (void) SIGNAL( SIGINT, slap_sig_shutdown );
858 1.1 lukem (void) SIGNAL( SIGTERM, slap_sig_shutdown );
859 1.1 lukem #ifdef SIGTRAP
860 1.1 lukem (void) SIGNAL( SIGTRAP, slap_sig_shutdown );
861 1.1 lukem #endif
862 1.1 lukem #ifdef LDAP_SIGCHLD
863 1.1 lukem (void) SIGNAL( LDAP_SIGCHLD, wait4child );
864 1.1 lukem #endif
865 1.1 lukem #ifdef SIGBREAK
866 1.1 lukem /* SIGBREAK is generated when Ctrl-Break is pressed. */
867 1.1 lukem (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
868 1.1 lukem #endif
869 1.1 lukem
870 1.1 lukem #ifndef HAVE_WINSOCK
871 1.1 lukem lutil_detach( no_detach, 0 );
872 1.1 lukem #endif /* HAVE_WINSOCK */
873 1.1 lukem
874 1.1 lukem #ifdef CSRIMALLOC
875 1.1 lukem mal_leaktrace(1);
876 1.1 lukem #endif
877 1.1 lukem
878 1.1 lukem if ( slapd_pid_file != NULL ) {
879 1.1 lukem FILE *fp = fopen( slapd_pid_file, "w" );
880 1.1 lukem
881 1.1 lukem if ( fp == NULL ) {
882 1.1 lukem int save_errno = errno;
883 1.1 lukem
884 1.1 lukem Debug( LDAP_DEBUG_ANY, "unable to open pid file "
885 1.1 lukem "\"%s\": %d (%s)\n",
886 1.1 lukem slapd_pid_file,
887 1.1 lukem save_errno, strerror( save_errno ) );
888 1.1 lukem
889 1.1 lukem free( slapd_pid_file );
890 1.1 lukem slapd_pid_file = NULL;
891 1.1 lukem
892 1.1 lukem rc = 1;
893 1.1 lukem goto destroy;
894 1.1 lukem }
895 1.1 lukem fprintf( fp, "%d\n", (int) getpid() );
896 1.1 lukem fclose( fp );
897 1.1 lukem slapd_pid_file_unlink = 1;
898 1.1 lukem }
899 1.1 lukem
900 1.1 lukem if ( slapd_args_file != NULL ) {
901 1.1 lukem FILE *fp = fopen( slapd_args_file, "w" );
902 1.1 lukem
903 1.1 lukem if ( fp == NULL ) {
904 1.1 lukem int save_errno = errno;
905 1.1 lukem
906 1.1 lukem Debug( LDAP_DEBUG_ANY, "unable to open args file "
907 1.1 lukem "\"%s\": %d (%s)\n",
908 1.1 lukem slapd_args_file,
909 1.1 lukem save_errno, strerror( save_errno ) );
910 1.1 lukem
911 1.1 lukem free( slapd_args_file );
912 1.1 lukem slapd_args_file = NULL;
913 1.1 lukem
914 1.1 lukem rc = 1;
915 1.1 lukem goto destroy;
916 1.1 lukem }
917 1.1 lukem
918 1.1 lukem for ( i = 0; i < g_argc; i++ ) {
919 1.1 lukem fprintf( fp, "%s ", g_argv[i] );
920 1.1 lukem }
921 1.1 lukem fprintf( fp, "\n" );
922 1.1 lukem fclose( fp );
923 1.1 lukem slapd_args_file_unlink = 1;
924 1.1 lukem }
925 1.1 lukem
926 1.1 lukem /*
927 1.1 lukem * FIXME: moved here from slapd_daemon_task()
928 1.1 lukem * because back-monitor db_open() needs it
929 1.1 lukem */
930 1.1 lukem time( &starttime );
931 1.1 lukem
932 1.1 lukem if ( slap_startup( NULL ) != 0 ) {
933 1.1 lukem rc = 1;
934 1.1 lukem SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
935 1.1 lukem goto shutdown;
936 1.1 lukem }
937 1.1 lukem
938 1.1 lukem Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
939 1.1 lukem
940 1.1 lukem #ifdef HAVE_NT_EVENT_LOG
941 1.1 lukem if (is_NT_Service)
942 1.1 lukem lutil_LogStartedEvent( serverName, slap_debug, configfile ?
943 1.1 lukem configfile : SLAPD_DEFAULT_CONFIGFILE , urls );
944 1.1 lukem #endif
945 1.1 lukem
946 1.1 lukem rc = slapd_daemon();
947 1.1 lukem
948 1.1 lukem #ifdef HAVE_NT_SERVICE_MANAGER
949 1.1 lukem /* Throw away the event that we used during the startup process. */
950 1.1 lukem if ( is_NT_Service )
951 1.1 lukem ldap_pvt_thread_cond_destroy( &started_event );
952 1.1 lukem #endif
953 1.1 lukem
954 1.1 lukem shutdown:
955 1.1 lukem /* remember an error during shutdown */
956 1.1 lukem rc |= slap_shutdown( NULL );
957 1.1 lukem
958 1.1 lukem destroy:
959 1.1 lukem if ( check & CHECK_LOGLEVEL ) {
960 1.1 lukem (void)loglevel_print( stdout );
961 1.1 lukem }
962 1.1 lukem /* remember an error during destroy */
963 1.1 lukem rc |= slap_destroy();
964 1.1 lukem
965 1.1 lukem while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
966 1.1 lukem scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
967 1.1 lukem LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
968 1.1 lukem ch_free( scp );
969 1.1 lukem }
970 1.1 lukem
971 1.1 lukem #ifdef SLAPD_MODULES
972 1.1 lukem module_kill();
973 1.1 lukem #endif
974 1.1 lukem
975 1.1 lukem extops_kill();
976 1.1 lukem
977 1.1 lukem supported_feature_destroy();
978 1.1 lukem entry_info_destroy();
979 1.1 lukem
980 1.1 lukem stop:
981 1.1 lukem #ifdef HAVE_NT_EVENT_LOG
982 1.1 lukem if (is_NT_Service)
983 1.1 lukem lutil_LogStoppedEvent( serverName );
984 1.1 lukem #endif
985 1.1 lukem
986 1.1 lukem Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
987 1.1 lukem
988 1.1 lukem
989 1.1 lukem #ifdef HAVE_NT_SERVICE_MANAGER
990 1.1 lukem lutil_ReportShutdownComplete();
991 1.1 lukem #endif
992 1.1 lukem
993 1.1 lukem #ifdef LOG_DEBUG
994 1.1 lukem closelog();
995 1.1 lukem #endif
996 1.1 lukem slapd_daemon_destroy();
997 1.1 lukem
998 1.1 lukem controls_destroy();
999 1.1 lukem
1000 1.1 lukem filter_destroy();
1001 1.1 lukem
1002 1.1 lukem schema_destroy();
1003 1.1 lukem
1004 1.1 lukem lutil_passwd_destroy();
1005 1.1 lukem
1006 1.1 lukem #ifdef HAVE_TLS
1007 1.1 lukem if ( slap_tls_ld ) {
1008 1.1 lukem ldap_pvt_tls_ctx_free( slap_tls_ctx );
1009 1.1 lukem ldap_unbind_ext( slap_tls_ld, NULL, NULL );
1010 1.1 lukem }
1011 1.1 lukem ldap_pvt_tls_destroy();
1012 1.1 lukem #endif
1013 1.1 lukem
1014 1.1 lukem slap_sasl_regexp_destroy();
1015 1.1 lukem
1016 1.1 lukem if ( slapd_pid_file_unlink ) {
1017 1.1 lukem unlink( slapd_pid_file );
1018 1.1 lukem }
1019 1.1 lukem if ( slapd_args_file_unlink ) {
1020 1.1 lukem unlink( slapd_args_file );
1021 1.1 lukem }
1022 1.1 lukem
1023 1.1 lukem config_destroy();
1024 1.1 lukem
1025 1.1 lukem if ( configfile )
1026 1.1 lukem ch_free( configfile );
1027 1.1 lukem if ( configdir )
1028 1.1 lukem ch_free( configdir );
1029 1.1 lukem if ( urls )
1030 1.1 lukem ch_free( urls );
1031 1.1 lukem
1032 1.1 lukem /* kludge, get symbols referenced */
1033 1.1 lukem tavl_free( NULL, NULL );
1034 1.1 lukem
1035 1.1 lukem #ifdef CSRIMALLOC
1036 1.1 lukem mal_dumpleaktrace( leakfile );
1037 1.1 lukem #endif
1038 1.1 lukem
1039 1.1 lukem MAIN_RETURN(rc);
1040 1.1 lukem }
1041 1.1 lukem
1042 1.1 lukem
1043 1.1 lukem #ifdef LDAP_SIGCHLD
1044 1.1 lukem
1045 1.1 lukem /*
1046 1.1 lukem * Catch and discard terminated child processes, to avoid zombies.
1047 1.1 lukem */
1048 1.1 lukem
1049 1.1 lukem static RETSIGTYPE
1050 1.1 lukem wait4child( int sig )
1051 1.1 lukem {
1052 1.1 lukem int save_errno = errno;
1053 1.1 lukem
1054 1.1 lukem #ifdef WNOHANG
1055 1.1 lukem errno = 0;
1056 1.1 lukem #ifdef HAVE_WAITPID
1057 1.1 lukem while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
1058 1.1 lukem ; /* NULL */
1059 1.1 lukem #else
1060 1.1 lukem while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
1061 1.1 lukem ; /* NULL */
1062 1.1 lukem #endif
1063 1.1 lukem #else
1064 1.1 lukem (void) wait( NULL );
1065 1.1 lukem #endif
1066 1.1 lukem (void) SIGNAL_REINSTALL( sig, wait4child );
1067 1.1 lukem errno = save_errno;
1068 1.1 lukem }
1069 1.1 lukem
1070 1.1 lukem #endif /* LDAP_SIGCHLD */
1071 1.1 lukem
1072