main.c 25.5 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1998-2021 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
8
9
10
11
12
13
 * 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>.
14
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
16
17
18
19
20
21
22
23
24
25
/* 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.
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
28
29

#include <stdio.h>

30
#include <ac/ctype.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
31
32
33
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
34
#include <ac/unistd.h>
Hallvard Furuseth's avatar
Hallvard Furuseth committed
35
36
#include <ac/wait.h>
#include <ac/errno.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
37

38
#include "slap.h"
39
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
#include "ldif.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
41

42
#ifdef LDAP_SLAPI
43
#include "slapi/slapi.h"
44
45
#endif

46
#ifdef LDAP_SIGCHLD
Hallvard Furuseth's avatar
Hallvard Furuseth committed
47
48
49
static RETSIGTYPE wait4child( int sig );
#endif

50
#ifdef HAVE_NT_SERVICE_MANAGER
51
#define MAIN_RETURN(x) return
Kurt Zeilenga's avatar
Kurt Zeilenga committed
52
static struct sockaddr_in	bind_addr;
53

54
55
#define SERVICE_EXIT( e, n )	do { \
	if ( is_NT_Service ) { \
56
57
		lutil_ServiceStatus.dwWin32ExitCode				= (e); \
		lutil_ServiceStatus.dwServiceSpecificExitCode	= (n); \
58
59
60
	} \
} while ( 0 )

61
62
#else
#define SERVICE_EXIT( e, n )
63
#define MAIN_RETURN(x) return(x)
64
65
#endif

66
typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
67
extern MainFunc slapadd, slapcat, slapdn, slapindex, slappasswd,
68
	slaptest, slapauth, slapacl, slapschema, slapmodify;
69
70
71
72
73
74
75

static struct {
	char *name;
	MainFunc *func;
} tools[] = {
	{"slapadd", slapadd},
	{"slapcat", slapcat},
76
	{"slapdn", slapdn},
77
	{"slapindex", slapindex},
78
	{"slapmodify", slapmodify},
79
	{"slappasswd", slappasswd},
80
	{"slapschema", slapschema},
81
	{"slaptest", slaptest},
Kurt Zeilenga's avatar
Kurt Zeilenga committed
82
	{"slapauth", slapauth},
Pierangelo Masarati's avatar
Pierangelo Masarati committed
83
	{"slapacl", slapacl},
84
85
86
87
88
89
	/* NOTE: new tools must be added in chronological order,
	 * not in alphabetical order, because for backwards
	 * compatibility name[4] is used to identify the
	 * tools; so name[4]=='a' must refer to "slapadd" and
	 * not to "slapauth".  Alphabetical order can be used
	 * for tools whose name[4] is not used yet */
90
91
92
	{NULL, NULL}
};

93
94
95
96
97
/*
 * when more than one slapd is running on one machine, each one might have
 * it's own LOCAL for syslogging and must have its own pid/args files
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
98
#ifndef HAVE_MKVERSION
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
100
const char Versionstr[] =
	OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
Kurt Zeilenga's avatar
Kurt Zeilenga committed
101
102
#endif

103
104
105
extern OverlayInit slap_oinfo[];
extern BackendInfo slap_binfo[];

106
107
108
#define	CHECK_NONE	0x00
#define	CHECK_CONFIG	0x01
#define	CHECK_LOGLEVEL	0x02
109
static int check = CHECK_NONE;
110
static int version = 0;
111

112
void *slap_tls_ctx;
Howard Chu's avatar
Howard Chu committed
113
LDAP *slap_tls_ld;
114

115
116
117
118
119
static int
slapd_opt_slp( const char *val, void *arg )
{
#ifdef HAVE_SLP
	/* NULL is default */
120
	if ( val == NULL || *val == '(' || strcasecmp( val, "on" ) == 0 ) {
121
		slapd_register_slp = 1;
122
		slapd_slp_attrs = (val != NULL && *val == '(') ? val : NULL;
123
124

	} else if ( strcasecmp( val, "off" ) == 0 ) {
125
		slapd_register_slp = 0;
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

	/* NOTE: add support for URL specification? */

	} else {
		fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
		return -1;
	}

	return 0;
		
#else
	fputs( "slapd: SLP support is not available\n", stderr );
	return 0;
#endif
}

142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Option helper structure:
 * 
 * oh_nam	is left-hand part of <option>[=<value>]
 * oh_fnc	is handler function
 * oh_arg	is an optional arg to oh_fnc
 * oh_usage	is the one-line usage string related to the option,
 *		which is assumed to start with <option>[=<value>]
 *
 * please leave valid options in the structure, and optionally #ifdef
 * their processing inside the helper, so that reasonable and helpful
 * error messages can be generated if a disabled option is requested.
 */
155
156
157
158
struct option_helper {
	struct berval	oh_name;
	int		(*oh_fnc)(const char *val, void *arg);
	void		*oh_arg;
159
	const char	*oh_usage;
160
} option_helpers[] = {
161
	{ BER_BVC("slp"),	slapd_opt_slp,	NULL, "slp[={on|off|(attrs)}] enable/disable SLP using (attrs)" },
Hallvard Furuseth's avatar
Cleanup    
Hallvard Furuseth committed
162
	{ BER_BVNULL, 0, NULL, NULL }
163
164
};

Howard Chu's avatar
Howard Chu committed
165
#if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
Pierangelo Masarati's avatar
Pierangelo Masarati committed
166
#ifdef LOG_LOCAL4
Pierangelo Masarati's avatar
Pierangelo Masarati committed
167
int
Pierangelo Masarati's avatar
Pierangelo Masarati committed
168
parse_syslog_user( const char *arg, int *syslogUser )
169
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	static slap_verbmasks syslogUsers[] = {
		{ BER_BVC( "LOCAL0" ), LOG_LOCAL0 },
		{ BER_BVC( "LOCAL1" ), LOG_LOCAL1 },
		{ BER_BVC( "LOCAL2" ), LOG_LOCAL2 },
		{ BER_BVC( "LOCAL3" ), LOG_LOCAL3 },
		{ BER_BVC( "LOCAL4" ), LOG_LOCAL4 },
		{ BER_BVC( "LOCAL5" ), LOG_LOCAL5 },
		{ BER_BVC( "LOCAL6" ), LOG_LOCAL6 },
		{ BER_BVC( "LOCAL7" ), LOG_LOCAL7 },
#ifdef LOG_USER
		{ BER_BVC( "USER" ), LOG_USER },
#endif /* LOG_USER */
#ifdef LOG_DAEMON
		{ BER_BVC( "DAEMON" ), LOG_DAEMON },
#endif /* LOG_DAEMON */
		{ BER_BVNULL, 0 }
	};
Pierangelo Masarati's avatar
Pierangelo Masarati committed
187
	int i = verb_to_mask( arg, syslogUsers );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
188
189

	if ( BER_BVISNULL( &syslogUsers[ i ].word ) ) {
190
191
		Debug( LDAP_DEBUG_ANY,
			"unrecognized syslog user \"%s\".\n",
192
			arg );
193
194
195
		return 1;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
196
197
	*syslogUser = syslogUsers[ i ].mask;

198
199
	return 0;
}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
200
#endif /* LOG_LOCAL4 */
201

Pierangelo Masarati's avatar
Pierangelo Masarati committed
202
int
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
parse_syslog_level( const char *arg, int *levelp )
{
	static slap_verbmasks	str2syslog_level[] = {
		{ BER_BVC( "EMERG" ),	LOG_EMERG },
		{ BER_BVC( "ALERT" ),	LOG_ALERT },
		{ BER_BVC( "CRIT" ),	LOG_CRIT },
		{ BER_BVC( "ERR" ),	LOG_ERR },
		{ BER_BVC( "WARNING" ),	LOG_WARNING },
		{ BER_BVC( "NOTICE" ),	LOG_NOTICE },
		{ BER_BVC( "INFO" ),	LOG_INFO },
		{ BER_BVC( "DEBUG" ),	LOG_DEBUG },
		{ BER_BVNULL, 0 }
	};
	int i = verb_to_mask( arg, str2syslog_level );
	if ( BER_BVISNULL( &str2syslog_level[ i ].word ) ) {
		Debug( LDAP_DEBUG_ANY,
			"unknown syslog level \"%s\".\n",
220
			arg );
221
222
223
224
225
226
227
		return 1;
	}
	
	*levelp = str2syslog_level[ i ].mask;

	return 0;
}
Howard Chu's avatar
Howard Chu committed
228
#endif /* LDAP_DEBUG && LDAP_SYSLOG */
229

230
231
232
static char **debug_unknowns;
static char **syslog_unknowns;

233
int
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
parse_debug_unknowns( char **unknowns, int *levelp )
{
	int i, level, rc = 0;

	for ( i = 0; unknowns[ i ] != NULL; i++ ) {
		level = 0;
		if ( str2loglevel( unknowns[ i ], &level )) {
			fprintf( stderr,
				"unrecognized log level \"%s\"\n", unknowns[ i ] );
			rc = 1;
		} else {
			*levelp |= level;
		}
	}
	return rc;
}

int
parse_debug_level( const char *arg, int *levelp, char ***unknowns )
253
254
255
{
	int	level;

256
	if ( arg && arg[ 0 ] != '-' && !isdigit( (unsigned char) arg[ 0 ] ) )
257
	{
258
		int	i;
259
260
261
262
263
264
265
266
		char	**levels;

		levels = ldap_str2charray( arg, "," );

		for ( i = 0; levels[ i ] != NULL; i++ ) {
			level = 0;

			if ( str2loglevel( levels[ i ], &level ) ) {
267
268
				/* remember this for later */
				ldap_charray_add( unknowns, levels[ i ] );
269
				fprintf( stderr,
270
271
					"unrecognized log level \"%s\" (deferred)\n",
					levels[ i ] );
272
273
274
275
276
277
278
279
			} else {
				*levelp |= level;
			}
		}

		ldap_charray_free( levels );

	} else {
280
281
282
283
284
285
286
287
288
289
290
291
		int rc;

		if ( arg[0] == '-' ) {
			rc = lutil_atoix( &level, arg, 0 );
		} else {
			unsigned ulevel;

			rc = lutil_atoux( &ulevel, arg, 0 );
			level = (int)ulevel;
		}

		if ( rc ) {
292
293
294
295
296
297
			fprintf( stderr,
				"unrecognized log level "
				"\"%s\"\n", arg );
			return 1;
		}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
298
299
300
301
302
303
		if ( level == 0 ) {
			*levelp = 0;

		} else {
			*levelp |= level;
		}
304
305
306
307
308
	}

	return 0;
}

309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
void slap_check_unknown_level( char *levelstr, int level )
{
	int i;

	if ( debug_unknowns ) {
		for ( i = 0; debug_unknowns[ i ]; i++ ) {
			if ( !strcasecmp( debug_unknowns[ i ], levelstr )) {
				slap_debug |= level;
				break;
			}
		}
	}

	if ( syslog_unknowns ) {
		for ( i = 0; syslog_unknowns[ i ]; i++ ) {
			if ( !strcasecmp( syslog_unknowns[ i ], levelstr )) {
				ldap_syslog |= level;
				break;
			}
		}
	}
}

Hallvard Furuseth's avatar
Hallvard Furuseth committed
332
static void
333
usage( char *name )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
334
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
335
336
337
	fprintf( stderr,
		"usage: %s options\n", name );
	fprintf( stderr,
338
		"\t-4\t\tIPv4 only\n"
339
#ifdef LDAP_PF_INET6
340
		"\t-6\t\tIPv6 only\n"
341
#endif
342
		"\t-T {acl|add|auth|cat|dn|index|modify|passwd|test}\n"
343
		"\t\t\tRun in Tool mode\n"
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
344
		"\t-c cookie\tSync cookie of consumer\n"
345
346
		"\t-d level\tDebug level" "\n"
		"\t-f filename\tConfiguration file\n"
Pierangelo Masarati's avatar
Pierangelo Masarati committed
347
		"\t-F dir\tConfiguration directory\n"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
349
		"\t-g group\tGroup (id or name) to run as\n"
350
#endif
351
		"\t-h URLs\t\tList of URLs to serve\n"
352
#ifdef SLAP_DEFAULT_SYSLOG_USER
353
		"\t-l facility\tSyslog facility (default: LOCAL4)\n"
354
#endif
355
		"\t-n serverName\tService name\n"
Pierangelo Masarati's avatar
Pierangelo Masarati committed
356
		"\t-o <opt>[=val] generic means to specify options" );
357
358
359
360
361
362
363
364
365
366
367
	if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
		int	i;

		fprintf( stderr, "; supported options:\n" );
		for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
			fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
		}
	} else {
		fprintf( stderr, "\n" );
	}
	fprintf( stderr,	
Kurt Zeilenga's avatar
Kurt Zeilenga committed
368
#ifdef HAVE_CHROOT
369
		"\t-r directory\tSandbox directory to chroot to\n"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
370
#endif
371
		"\t-s level\tSyslog level\n"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
372
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
373
		"\t-u user\t\tUser (id or name) to run as\n"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
374
#endif
Ralf Haferkamp's avatar
Ralf Haferkamp committed
375
376
		"\t-V\t\tprint version info (-VV exit afterwards, -VVV print\n"
		"\t\t\tinfo about static overlays and backends)\n"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
377
378
    );
}
379

380

381
#ifdef HAVE_NT_SERVICE_MANAGER
382
383
384
385
void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
#else
int main( int argc, char **argv )
#endif
386
{
387
	int		i, no_detach = 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
388
	int		rc = 1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
389
	char *urls = NULL;
390
391
392
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
	char *username = NULL;
	char *groupname = NULL;
393
#endif
394
395
396
#if defined(HAVE_CHROOT)
	char *sandbox = NULL;
#endif
397
#ifdef SLAP_DEFAULT_SYSLOG_USER
Pierangelo Masarati's avatar
Pierangelo Masarati committed
398
	int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
399
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
400
	
401
#ifndef HAVE_WINSOCK
402
	int pid, waitfds[2];
403
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
404
405
	int g_argc = argc;
	char **g_argv = argv;
406

407
408
409
410
	char *configfile = NULL;
	char *configdir = NULL;
	char *serverName;
	int serverMode = SLAP_SERVER_MODE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
411

412
413
	struct sync_cookie *scp = NULL;
	struct sync_cookie *scp_entry = NULL;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
414

415
	char *serverNamePrefix = "";
Kurt Zeilenga's avatar
Kurt Zeilenga committed
416
417
	size_t	l;

418
	int slapd_pid_file_unlink = 0, slapd_args_file_unlink = 0;
419
	int firstopt = 1;
420

421
422
423
424
425
426
427
#ifdef CSRIMALLOC
	FILE *leakfile;
	if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
		leakfile = stderr;
	}
#endif

428
	slap_sl_mem_init();
429

430

431
432
	(void) ldap_pvt_thread_initialize();

433
434
435
	serverName = lutil_progname( "slapd", argc, argv );

	if ( strcmp( serverName, "slapd" ) ) {
436
437
438
439
#ifdef DEBUG_CLOSE
		extern void slapd_debug_close();
		slapd_debug_close();
#endif
440
441
442
443
444
445
446
447
		for (i=0; tools[i].name; i++) {
			if ( !strcmp( serverName, tools[i].name ) ) {
				rc = tools[i].func(argc, argv);
				MAIN_RETURN(rc);
			}
		}
	}

448
#ifdef HAVE_NT_SERVICE_MANAGER
449
	{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
450
		int *ip;
451
		char *newConfigFile;
452
		char *newConfigDir;
Howard Chu's avatar
Howard Chu committed
453
		char *newUrls;
454
		char *regService = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
455
456

		if ( is_NT_Service ) {
457
			lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
458
459
			if ( strcmp(serverName, SERVICE_NAME) )
			    regService = serverName;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
460
461
		}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
462
463
464
		ip = (int*)lutil_getRegParam( regService, "DebugLevel" );
		if ( ip != NULL ) {
			slap_debug = *ip;
465
			Debug( LDAP_DEBUG_ANY,
466
				"new debug level from registry is: %d\n", slap_debug );
467
		}
Howard Chu's avatar
Howard Chu committed
468

469
		newUrls = (char *) lutil_getRegParam(regService, "Urls");
470
		if (newUrls) {
Howard Chu's avatar
Howard Chu committed
471
472
473
474
475
		    if (urls)
			ch_free(urls);

		    urls = ch_strdup(newUrls);
		    Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
476
				urls );
Howard Chu's avatar
Howard Chu committed
477
478
		}

479
		newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
480
		if ( newConfigFile != NULL ) {
481
			configfile = ch_strdup(newConfigFile);
482
			Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile );
483
		}
484
485
486

		newConfigDir = (char*)lutil_getRegParam( regService, "ConfigDir" );
		if ( newConfigDir != NULL ) {
487
			configdir = ch_strdup(newConfigDir);
488
			Debug ( LDAP_DEBUG_ANY, "new config dir from registry is: %s\n", configdir );
489
		}
490
491
492
	}
#endif

493
	while ( (i = getopt( argc, argv,
494
			     "c:d:f:F:h:n:o:s:tT:V"
495
#ifdef LDAP_PF_INET6
496
497
				"46"
#endif
498
499
500
#ifdef HAVE_CHROOT
				"r:"
#endif
Howard Chu's avatar
Howard Chu committed
501
#if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
502
				"S:"
503
#ifdef LOG_LOCAL4
504
				"l:"
505
#endif
Howard Chu's avatar
Howard Chu committed
506
#endif
507
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
508
				"u:g:"
509
#endif
510
			     )) != EOF ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
511
		switch ( i ) {
512
513
514
		case '4':
			slap_inet4or6 = AF_INET;
			break;
515
#ifdef LDAP_PF_INET6
516
517
518
519
520
		case '6':
			slap_inet4or6 = AF_INET6;
			break;
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
521
		case 'h':	/* listen URLs */
Hallvard Furuseth's avatar
Hallvard Furuseth committed
522
			if ( urls != NULL ) free( urls );
523
			urls = optarg;
524
			break;
525

526
		case 'c':	/* provide sync cookie, override if exist in consumer */
527
528
			scp = (struct sync_cookie *) ch_calloc( 1,
										sizeof( struct sync_cookie ));
Howard Chu's avatar
Howard Chu committed
529
			ber_str2bv( optarg, 0, 1, &scp->octet_str );
530
531
			
			/* This only parses out the rid at this point */
Howard Chu's avatar
Howard Chu committed
532
			slap_parse_sync_cookie( scp, NULL );
533

534
535
536
			if ( scp->rid == -1 ) {
				Debug( LDAP_DEBUG_ANY,
						"main: invalid cookie \"%s\"\n",
537
						optarg );
538
539
540
541
				slap_sync_cookie_free( scp, 1 );
				goto destroy;
			}

542
			LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
543
544
				if ( scp->rid == scp_entry->rid ) {
					Debug( LDAP_DEBUG_ANY,
545
						    "main: duplicated replica id in cookies\n" );
546
547
548
549
					slap_sync_cookie_free( scp, 1 );
					goto destroy;
				}
			}
550
			LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
551
552
			break;

553
554
555
		case 'd': {	/* set debug level and 'do not detach' flag */
			int	level = 0;

556
			if ( strcmp( optarg, "?" ) == 0 ) {
557
558
				check |= CHECK_LOGLEVEL;
				break;
559
560
			}

561
			no_detach = 1;
562
			if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
563
				goto destroy;
564
			}
565
566
#ifdef LDAP_DEBUG
			slap_debug |= level;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
567
#else
568
			if ( level != 0 )
569
570
				fputs( "must compile with LDAP_DEBUG for debugging\n",
				       stderr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
571
#endif
572
			} break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
573
574

		case 'f':	/* read config file */
575
			configfile = optarg;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
576
577
			break;

578
		case 'F':	/* use config dir */
579
			configdir = optarg;
580
581
			break;

582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
		case 'o': {
			char		*val = strchr( optarg, '=' );
			struct berval	opt;

			opt.bv_val = optarg;
			
			if ( val ) {
				opt.bv_len = ( val - optarg );
				val++;
			
			} else {
				opt.bv_len = strlen( optarg );
			}

			for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
				if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
598
599
600
601
602
603
					assert( option_helpers[i].oh_fnc != NULL );
					if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
						/* we assume the option parsing helper
						 * issues appropriate and self-explanatory
						 * error messages... */
						goto stop;
604
					}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
605
					break;
606
607
608
609
610
611
612
613
614
				}
			}

			if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
				goto unhandled_option;
			}
			break;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
615
		case 's':	/* set syslog level */
616
			if ( strcmp( optarg, "?" ) == 0 ) {
617
618
				check |= CHECK_LOGLEVEL;
				break;
619
620
			}

621
			if ( parse_debug_level( optarg, &ldap_syslog, &syslog_unknowns ) ) {
622
623
				goto destroy;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
624
625
			break;

626
627
628
629
630
631
632
#if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
		case 'S':
			if ( parse_syslog_level( optarg, &ldap_syslog_level ) ) {
				goto destroy;
			}
			break;

633
634
#ifdef LOG_LOCAL4
		case 'l':	/* set syslog local user */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
635
636
637
			if ( parse_syslog_user( optarg, &syslogUser ) ) {
				goto destroy;
			}
638
639
			break;
#endif
Howard Chu's avatar
Howard Chu committed
640
#endif /* LDAP_DEBUG && LDAP_SYSLOG */
641

642
643
#ifdef HAVE_CHROOT
		case 'r':
644
			sandbox = optarg;
645
646
647
			break;
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
648
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
649
		case 'u':	/* user name */
650
			username = optarg;
651
652
			break;

653
		case 'g':	/* group name */
654
			groupname = optarg;
655
			break;
656
#endif /* SETUID && GETUID */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
657

658
		case 'n':  /* NT service name */
659
			serverName = optarg;
660
			break;
661

662
		case 't':
Pierangelo Masarati's avatar
Pierangelo Masarati committed
663
664
665
			/* deprecated; use slaptest instead */
			fprintf( stderr, "option -t deprecated; "
				"use slaptest command instead\n" );
666
			check |= CHECK_CONFIG;
667
			break;
668

669
670
671
		case 'V':
			version++;
			break;
672

673
		case 'T':
674
675
676
677
678
679
			if ( firstopt == 0 ) {
				fprintf( stderr, "warning: \"-T %s\" "
					"should be the first option.\n",
					optarg );
			}

680
681
682
683
#ifdef DEBUG_CLOSE
			extern void slapd_debug_close();
			slapd_debug_close();
#endif
684
685
			/* try full option string first */
			for ( i = 0; tools[i].name; i++ ) {
686
				if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
687
688
					rc = tools[i].func( argc, argv );
					MAIN_RETURN( rc );
689
690
				}
			}
691
692
693
694
695
696
697
698
699
700
701

			/* try bits of option string (backward compatibility for single char) */
			l = strlen( optarg );
			for ( i = 0; tools[i].name; i++ ) {
				if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
					rc = tools[i].func( argc, argv );
					MAIN_RETURN( rc );
				}
			}
			
			/* issue error */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
702
703
704
705
			serverName = optarg;
			serverNamePrefix = "slap";
			fprintf( stderr, "program name \"%s%s\" unrecognized; "
					"aborting...\n", serverNamePrefix, serverName );
706
			/* FALLTHRU */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
707
		default:
708
unhandled_option:;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
709
			usage( argv[0] );
710
711
712
			rc = 1;
			SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
			goto stop;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
713
		}
714
715
716
717

		if ( firstopt ) {
			firstopt = 0;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
718
719
	}

720
721
722
	if ( optind != argc )
		goto unhandled_option;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
723
	ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
724
725
726
	ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
	ldif_debug = slap_debug;

727
728
	if ( version ) {
		fprintf( stderr, "%s\n", Versionstr );
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
		if ( version > 2 ) {
			if ( slap_oinfo[0].ov_type ) {
				fprintf( stderr, "Included static overlays:\n");
				for ( i= 0 ; slap_oinfo[i].ov_type; i++ ) {
					fprintf( stderr, "    %s\n", slap_oinfo[i].ov_type );
				}
			}
			if ( slap_binfo[0].bi_type ) {
				fprintf( stderr, "Included static backends:\n");
				for ( i= 0 ; slap_binfo[i].bi_type; i++ ) {
					fprintf( stderr, "    %s\n", slap_binfo[i].bi_type );
				}
			}
		}

744
745
		if ( version > 1 ) goto stop;
	}
746

747
#if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
Howard Chu's avatar
Howard Chu committed
748
749
750
751
752
753
754
755
756
	{
		char *logName;
#ifdef HAVE_EBCDIC
		logName = ch_strdup( serverName );
		__atoe( logName );
#else
		logName = serverName;
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
757
#ifdef LOG_LOCAL4
Howard Chu's avatar
Howard Chu committed
758
		openlog( logName, OPENLOG_OPTIONS, syslogUser );
759
#elif defined LOG_DEBUG
Howard Chu's avatar
Howard Chu committed
760
761
762
763
		openlog( logName, OPENLOG_OPTIONS );
#endif
#ifdef HAVE_EBCDIC
		free( logName );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
764
#endif
Howard Chu's avatar
Howard Chu committed
765
	}
766
#endif /* LDAP_DEBUG && LDAP_SYSLOG */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
767

768
	Debug( LDAP_DEBUG_ANY, "%s", Versionstr );
769

Howard Chu's avatar
Howard Chu committed
770
	global_host = ldap_pvt_get_fqdn( NULL );
771
	ber_str2bv( global_host, 0, 0, &global_host_bv );
Howard Chu's avatar
Howard Chu committed
772

773
	if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
774
775
776
777
		rc = 1;
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
		goto stop;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
778

779
#if defined(HAVE_CHROOT)
780
781
782
783
784
785
786
787
788
789
790
	if ( sandbox ) {
		if ( chdir( sandbox ) ) {
			perror("chdir");
			rc = 1;
			goto stop;
		}
		if ( chroot( sandbox ) ) {
			perror("chroot");
			rc = 1;
			goto stop;
		}
Howard Chu's avatar
Howard Chu committed
791
792
793
794
795
		if ( chdir( "/" ) ) {
			perror("chdir");
			rc = 1;
			goto stop;
		}
796
797
798
	}
#endif

799
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
800
	if ( username != NULL || groupname != NULL ) {
801
		slap_init_user( username, groupname );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
802
	}
803
804
#endif

805
	extops_init();
806
	lutil_passwd_init();
807

Howard Chu's avatar
Howard Chu committed
808
809
810
811
812
813
#ifdef HAVE_TLS
	rc = ldap_create( &slap_tls_ld );
	if ( rc ) {
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
		goto destroy;
	}
Howard Chu's avatar
Howard Chu committed
814
815
816
817
818
819
820
	/* 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( slap_tls_ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
Howard Chu's avatar
Howard Chu committed
821
822
#endif

823
824
	rc = slap_init( serverMode, serverName );
	if ( rc ) {
825
826
827
828
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
		goto destroy;
	}

829
	if ( read_config( configfile, configdir ) != 0 ) {
830
		rc = 1;
831
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
832

833
		if ( check & CHECK_CONFIG ) {
834
835
836
837
838
839
			fprintf( stderr, "config check failed\n" );
		}

		goto destroy;
	}

840
841
842
843
844
845
846
847
848
849
850
851
852
	if ( debug_unknowns ) {
		rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
		ldap_charray_free( debug_unknowns );
		debug_unknowns = NULL;
		if ( rc )
			goto destroy;
	}
	if ( syslog_unknowns ) {
		rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
		ldap_charray_free( syslog_unknowns );
		syslog_unknowns = NULL;
		if ( rc )
			goto destroy;
853
854
855
856
857
	}	

	if ( check & CHECK_LOGLEVEL ) {
		rc = 0;
		goto destroy;
858
859
	}

860
	if ( check & CHECK_CONFIG ) {
861
		fprintf( stderr, "config check succeeded\n" );
862
863
864
865
866
867

		check &= ~CHECK_CONFIG;
		if ( check == CHECK_NONE ) {
			rc = 0;
			goto destroy;
		}
868
869
	}

870
	if ( glue_sub_attach( 0 ) != 0 ) {
871
		Debug( LDAP_DEBUG_ANY,
872
		    "subordinate config error\n" );
873
874
875
876

		goto destroy;
	}

877
	if ( slap_schema_check( ) != 0 ) {
878
		Debug( LDAP_DEBUG_ANY,
879
		    "schema prep error\n" );
880

881
882
883
		goto destroy;
	}

884
#ifdef HAVE_TLS
885
	rc = ldap_pvt_tls_init( 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
886
887
888
	if( rc != 0) {
		Debug( LDAP_DEBUG_ANY,
		    "main: TLS init failed: %d\n",
889
		    rc );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
890
891
892
893
		rc = 1;
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
		goto destroy;
	}
Howard Chu's avatar
Howard Chu committed
894

Howard Chu's avatar
Howard Chu committed
895
	{
Howard Chu's avatar
Howard Chu committed
896
		int opt = 1;
Howard Chu's avatar
Howard Chu committed
897
898

		/* Force new ctx to be created */
Howard Chu's avatar
Howard Chu committed
899
		rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
900
		if( rc == 0 ) {
Howard Chu's avatar
Howard Chu committed
901
902
			/* The ctx's refcount is bumped up here */
			ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
903
904
			load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
		} else if ( rc != LDAP_NOT_SUPPORTED ) {
Howard Chu's avatar
Howard Chu committed
905
906
			Debug( LDAP_DEBUG_ANY,
			    "main: TLS init def ctx failed: %d\n",
907
			    rc );
Howard Chu's avatar
Howard Chu committed
908
909
910
911
			rc = 1;
			SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
			goto destroy;
		}
Howard Chu's avatar
Howard Chu committed
912
	}
913
914
#endif

915
916
917
918
919
920
#ifdef HAVE_CYRUS_SASL
	if( sasl_host == NULL ) {
		sasl_host = ch_strdup( global_host );
	}
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
921
922
923
	(void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
	(void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );

924
#ifdef SIGPIPE
925
	(void) SIGNAL( SIGPIPE, SIG_IGN );
926
927
#endif
#ifdef SIGHUP
Kurt Zeilenga's avatar
Kurt Zeilenga committed
928
	(void) SIGNAL( SIGHUP, slap_sig_shutdown );
929
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
930
931
	(void) SIGNAL( SIGINT, slap_sig_shutdown );
	(void) SIGNAL( SIGTERM, slap_sig_shutdown );
932
#ifdef SIGTRAP
933
	(void) SIGNAL( SIGTRAP, slap_sig_shutdown );
934
#endif
935
936
#ifdef LDAP_SIGCHLD
	(void) SIGNAL( LDAP_SIGCHLD, wait4child );
Hallvard Furuseth's avatar
Hallvard Furuseth committed
937
#endif
938
#ifdef SIGBREAK
939
	/* SIGBREAK is generated when Ctrl-Break is pressed. */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
940
	(void) SIGNAL( SIGBREAK, slap_sig_shutdown );
941
#endif
942

943
#ifndef HAVE_WINSOCK
944
945
946
947
	if ( !no_detach ) {
		if ( lutil_pair( waitfds ) < 0 ) {
			Debug( LDAP_DEBUG_ANY,
				"main: lutil_pair failed: %d\n",
948
				0 );
949
950
951
952
953
954
955
956
957
958
959
960
961
962
			rc = 1;
			goto destroy;
		}
		pid = lutil_detach( no_detach, 0 );
		if ( pid ) {
			char buf[4];
			rc = EXIT_SUCCESS;
			close( waitfds[1] );
			if ( read( waitfds[0], buf, 1 ) != 1 )
				rc = EXIT_FAILURE;
			_exit( rc );
		} else {
			close( waitfds[0] );
		}
963
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
964
#endif /* HAVE_WINSOCK */
965

966
967
968
969
#ifdef CSRIMALLOC
	mal_leaktrace(1);
#endif

970
971
	if ( slapd_pid_file != NULL ) {
		FILE *fp = fopen( slapd_pid_file, "w" );
972

973
		if ( fp == NULL ) {
974
			char ebuf[128];
975
976
977
978
979
			int save_errno = errno;

			Debug( LDAP_DEBUG_ANY, "unable to open pid file "
				"\"%s\": %d (%s)\n",
				slapd_pid_file,
980
				save_errno, AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) );
981

982
			free( slapd_pid_file );
983
			slapd_pid_file = NULL;
984
985

			rc = 1;
986
			goto destroy;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
987
		}
988
989
		fprintf( fp, "%d\n", (