main.c 10.5 KB
Newer Older
1
/* $OpenLDAP$ */
2
3
4
5
/*
 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
8
9

#include <stdio.h>

10
#include <ac/signal.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
11
12
13
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
14
#include <ac/unistd.h>
Hallvard Furuseth's avatar
Hallvard Furuseth committed
15
16
17
#include <ac/wait.h>
#include <ac/signal.h>
#include <ac/errno.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
18

19
#include "slap.h"
20
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
21

22
#ifdef LDAP_SIGCHLD
Hallvard Furuseth's avatar
Hallvard Furuseth committed
23
24
25
static RETSIGTYPE wait4child( int sig );
#endif

26
#ifdef HAVE_NT_SERVICE_MANAGER
27
#define MAIN_RETURN(x) return
28
29
struct sockaddr_in	bind_addr;

30
/* in nt_main.c */
Howard Chu's avatar
Howard Chu committed
31
32
LIBLUTIL_F (SERVICE_STATUS)		SLAPDServiceStatus;
LIBLUTIL_F (SERVICE_STATUS_HANDLE)	hSLAPDServiceStatus;
33
34
35
36
37
38
39
40
extern ldap_pvt_thread_cond_t	started_event,		stopped_event;
extern int	  is_NT_Service;

void CommenceStartupProcessing( LPCTSTR serviceName,
							   void(*stopper)(int));
void ReportSlapdShutdownComplete( void );
void *getRegParam( char *svc, char *value );

41
42
43
44
45
46
47
#define SERVICE_EXIT( e, n )	do { \
	if ( is_NT_Service ) { \
		SLAPDServiceStatus.dwWin32ExitCode				= (e); \
		SLAPDServiceStatus.dwServiceSpecificExitCode	= (n); \
	} \
} while ( 0 )

48
49
#else
#define SERVICE_EXIT( e, n )
50
#define MAIN_RETURN(x) return(x)
51
52
#endif

53
#ifdef HAVE_NT_EVENT_LOG
54
55
56
57
void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
void LogSlapdStoppedEvent( char *svc );
#endif

58
59
60
61
62
/*
 * 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
63
#ifndef HAVE_MKVERSION
Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
65
const char Versionstr[] =
	OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
Kurt Zeilenga's avatar
Kurt Zeilenga committed
66
67
#endif

68
69
70
71
72
#ifdef LOG_LOCAL4

#define DEFAULT_SYSLOG_USER  LOG_LOCAL4

typedef struct _str2intDispatch {
73
74
75
	char    *stringVal;
	int      abbr;
	int      intVal;
76
77
78
79
80
} STRDISP, *STRDISP_P;


/* table to compute syslog-options to integer */
static STRDISP  syslog_types[] = {
81
82
83
84
85
86
87
88
89
	{ "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
	{ "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
	{ "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
	{ "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
	{ "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
	{ "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
	{ "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
	{ "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
	{ NULL }
90
91
};

Hallvard Furuseth's avatar
Hallvard Furuseth committed
92
static int   cnvt_str2int( char *, STRDISP_P, int );
93
94
95
96

#endif  /* LOG_LOCAL4 */


Hallvard Furuseth's avatar
Hallvard Furuseth committed
97
static void
98
usage( char *name )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
100
101
102
	fprintf( stderr,
		"usage: %s options\n", name );
	fprintf( stderr,
103
#if LDAP_CONNECTIONLESS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
		"\t-c\t\tEnable (experimental) Connectionless LDAP\n"
105
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
106
107
108
109
		"\t-d level\tDebug Level" "\n"
		"\t-f filename\tConfiguration File\n"
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
		"\t-g group\tGroup (id or name) to ran as\n"
110
#endif
111
		"\t-h URLs\tList of URLs to serve\n"
112
#ifdef LOG_LOCAL4
Kurt Zeilenga's avatar
Kurt Zeilenga committed
113
		"\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
114
#endif
115
#ifdef HAVE_NT_EVENT_LOG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
116
		"\t-n NTserviceName\tNT service name\n"
117
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
118

Kurt Zeilenga's avatar
Kurt Zeilenga committed
119
120
121
		"\t-s level\tSyslog Level\n"
#ifdef SLAPD_BDB2
		"\t-t\t\tEnable BDB2 timing\n"
Julio Sánchez Fernández's avatar
   
Julio Sánchez Fernández committed
122
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
123
124
125
126
127
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
		"\t-u user\tUser (id or name) to ran as\n"
#endif
    );
}
128

129
#ifdef HAVE_NT_SERVICE_MANAGER
130
131
132
133
void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
#else
int main( int argc, char **argv )
#endif
134
{
135
	int		i, no_detach = 0;
Gary Williams's avatar
NT port    
Gary Williams committed
136
	int		rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
	char *urls = NULL;
138
139
140
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
	char *username = NULL;
	char *groupname = NULL;
141
#endif
142
143
#ifdef LOG_LOCAL4
    int     syslogUser = DEFAULT_SYSLOG_USER;
144
#endif
145
146

#ifdef HAVE_NT_EVENT_LOG
147
	char        *NTservice  = SERVICE_NAME;
148
149
#endif
#ifdef HAVE_NT_SERVICE_MANAGER
150
151
152
	char		*configfile = ".\\slapd.conf";
#else
	char		*configfile = SLAPD_DEFAULT_CONFIGFILE;
153
#endif
154
	char        *serverName;
155
	int         serverMode = SLAP_SERVER_MODE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156

157
158
159
160
161
162
163
#ifdef CSRIMALLOC
	FILE *leakfile;
	if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
		leakfile = stderr;
	}
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
164
165
166
	g_argc = argc;
	g_argv = argv;

167
#ifdef HAVE_NT_SERVICE_MANAGER
168
	{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
169
		int *i;
170
		char *newConfigFile;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
171
172
173
174
175

		if ( is_NT_Service ) {
			CommenceStartupProcessing( NTservice, slap_sig_shutdown );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
176
177
		i = (int*)getRegParam( NULL, "DebugLevel" );
		if ( i != NULL ) 
178
		{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
179
			slap_debug = *i;
180
181
182
183
184
185
186
187
188
189
190
			Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
		}
		newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
		if ( newConfigFile != NULL ) 
		{
			configfile = newConfigFile;
			Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
		}
	}
#endif

191
	while ( (i = getopt( argc, argv,
192
			     "d:f:h:s:"
193
194
195
#ifdef LOG_LOCAL4
			     "l:"
#endif
196
#ifdef SLAPD_BDB2
197
			     "t"
198
#endif
199
200
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
			     "u:g:"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
201
202
203
#endif
#ifdef LDAP_CONNECTIONLESS
				 "c"
204
#endif
205
#ifdef HAVE_NT_EVENT_LOG
206
				 "n:"
207
#endif
208
			     )) != EOF ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
209
		switch ( i ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
210
		case 'h':	/* listen URLs */
Hallvard Furuseth's avatar
Hallvard Furuseth committed
211
			if ( urls != NULL ) free( urls );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
212
			urls = ch_strdup( optarg );
213
214
            break;

215
216
		case 'd':	/* set debug level and 'do not detach' flag */
			no_detach = 1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
217
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
218
			slap_debug |= atoi( optarg );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
219
#else
220
221
222
			if ( atoi( optarg ) != 0 )
				fputs( "must compile with LDAP_DEBUG for debugging\n",
				       stderr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
223
#endif
224
			break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
225
226

		case 'f':	/* read config file */
227
			configfile = ch_strdup( optarg );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
228
229
230
231
232
233
			break;

		case 's':	/* set syslog level */
			ldap_syslog = atoi( optarg );
			break;

234
235
#ifdef LOG_LOCAL4
		case 'l':	/* set syslog local user */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
236
237
			syslogUser = cnvt_str2int( optarg,
				syslog_types, DEFAULT_SYSLOG_USER );
238
239
240
			break;
#endif

241
242
#ifdef LDAP_CONNECTIONLESS
		case 'c':	/* do connectionless (udp) */
243
244
			/* udp = 1; */
			fprintf( stderr, "connectionless support not supported");
Kurt Zeilenga's avatar
Kurt Zeilenga committed
245
			exit( EXIT_FAILURE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
246
			break;
247
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
248

249
250
#ifdef SLAPD_BDB2
		case 't':  /* timed server */
251
			serverMode |= SLAP_TIMED_MODE;
252
253
254
			break;
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
255
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
256
257
		case 'u':	/* user name */
			if( username ) free(username);
258
259
260
			username = ch_strdup( optarg );
			break;

261
262
		case 'g':	/* group name */
			if( groupname ) free(groupname);
263
264
			groupname = ch_strdup( optarg );
			break;
265
#endif /* SETUID && GETUID */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
266

267
#ifdef HAVE_NT_EVENT_LOG
268
		case 'n':  /* NT service name */
269
270
271
			NTservice = ch_strdup( optarg );
			break;
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
272
273
		default:
			usage( argv[0] );
274
275
276
			rc = 1;
			SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
			goto stop;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
277
278
279
		}
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
280
	ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
281
282
283
	ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
	ldif_debug = slap_debug;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
284
285
	Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );

286
	if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
287
		serverName = ch_strdup( argv[0] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
288
	} else {
289
		serverName = ch_strdup( serverName + 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
290
291
292
	}

#ifdef LOG_LOCAL4
293
	openlog( serverName, OPENLOG_OPTIONS, syslogUser );
294
#elif LOG_DEBUG
295
	openlog( serverName, OPENLOG_OPTIONS );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
296
297
#endif

298
	if( slapd_daemon_init( urls ) != 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
299
300
301
302
		rc = 1;
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
		goto stop;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
303

304
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
305
	if ( username != NULL || groupname != NULL ) {
306
		slap_init_user( username, groupname );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
307
	}
308
309
#endif

310
311
	extops_init();

312
313
314
315
316
317
318
319
#ifdef SLAPD_MODULES
	if ( module_init() != 0 ) {
		rc = 1;
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
		goto destroy;
	}
#endif

320
	if ( slap_init( serverMode, serverName ) != 0 ) {
321
		rc = 1;
322
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
323
324
325
326
327
		goto destroy;
	}

	if ( read_config( configfile ) != 0 ) {
		rc = 1;
328
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
329
330
331
		goto destroy;
	}

332
333
334
335
336
#ifdef HAVE_TLS
	ldap_pvt_tls_init();
	ldap_pvt_tls_init_def_ctx();
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
337
338
339
	(void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
	(void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );

340
#ifdef SIGPIPE
341
	(void) SIGNAL( SIGPIPE, SIG_IGN );
342
343
#endif
#ifdef SIGHUP
Kurt Zeilenga's avatar
Kurt Zeilenga committed
344
	(void) SIGNAL( SIGHUP, slap_sig_shutdown );
345
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
346
347
	(void) SIGNAL( SIGINT, slap_sig_shutdown );
	(void) SIGNAL( SIGTERM, slap_sig_shutdown );
348
349
#ifdef LDAP_SIGCHLD
	(void) SIGNAL( LDAP_SIGCHLD, wait4child );
Hallvard Furuseth's avatar
Hallvard Furuseth committed
350
#endif
351
#ifdef SIGBREAK
352
	/* SIGBREAK is generated when Ctrl-Break is pressed. */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
353
	(void) SIGNAL( SIGBREAK, slap_sig_shutdown );
354
#endif
355

356
#ifndef HAVE_WINSOCK
357
	lutil_detach( no_detach, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
358
#endif /* HAVE_WINSOCK */
359

360
361
362
363
#ifdef CSRIMALLOC
	mal_leaktrace(1);
#endif

364
	if ( slap_startup( NULL )  != 0 ) {
365
		rc = 1;
366
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
367
368
		goto shutdown;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
369

Kurt Zeilenga's avatar
Kurt Zeilenga committed
370
	{
371
		FILE *fp;
372

373
		Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
374

375
376
377
378
379
		if (( slapd_pid_file != NULL ) &&
			(( fp = fopen( slapd_pid_file, "w" )) != NULL ))
		{
			fprintf( fp, "%d\n", (int) getpid() );
			fclose( fp );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
380
381
		}

382
383
384
385
386
		if (( slapd_args_file != NULL ) &&
			(( fp = fopen( slapd_args_file, "w" )) != NULL ))
		{
			for ( i = 0; i < g_argc; i++ ) {
				fprintf( fp, "%s ", g_argv[i] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
387
			}
388
389
390
391
			fprintf( fp, "\n" );
			fclose( fp );
		}
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
392

393
#ifdef HAVE_NT_EVENT_LOG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
394
	LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
395
396
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
397
	rc = slapd_daemon();
398

399
#ifdef HAVE_NT_SERVICE_MANAGER
400
	/* Throw away the event that we used during the startup process. */
401
402
403
	if ( is_NT_Service )
		ldap_pvt_thread_cond_destroy( &started_event );
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
404

405
shutdown:
406
	/* remember an error during shutdown */
407
	rc |= slap_shutdown( NULL );
408

409
destroy:
410
411
412
	/* remember an error during destroy */
	rc |= slap_destroy();

413
414
415
416
#ifdef SLAPD_MODULES
	module_kill();
#endif

417
418
	extops_kill();

419
stop:
420
#ifdef HAVE_NT_EVENT_LOG
421
	LogSlapdStoppedEvent( NTservice );
422
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
423

424
	Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
425

426
#ifdef HAVE_NT_SERVICE_MANAGER
427
428
	ReportSlapdShutdownComplete();
#endif
429

430
#ifdef LOG_DEBUG
431
    closelog();
432
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
433
	slapd_daemon_destroy();
434

435
436
437
438
#ifdef CSRIMALLOC
	mal_dumpleaktrace( leakfile );
#endif

439
	MAIN_RETURN(rc);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
440
}
441
442


443
#ifdef LDAP_SIGCHLD
Hallvard Furuseth's avatar
Hallvard Furuseth committed
444
445
446
447
448
449
450
451
452

/*
 *  Catch and discard terminated child processes, to avoid zombies.
 */

static RETSIGTYPE
wait4child( int sig )
{
    int save_errno = errno;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
453
454

#ifdef WNOHANG
Hallvard Furuseth's avatar
Hallvard Furuseth committed
455
    errno = 0;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
456
#ifdef HAVE_WAITPID
Hallvard Furuseth's avatar
Hallvard Furuseth committed
457
458
    while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
	;	/* NULL */
Hallvard Furuseth's avatar
Hallvard Furuseth committed
459
#else
Hallvard Furuseth's avatar
Hallvard Furuseth committed
460
461
    while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
	;	/* NULL */
Hallvard Furuseth's avatar
Hallvard Furuseth committed
462
#endif
463
#else
Hallvard Furuseth's avatar
Hallvard Furuseth committed
464
465
    (void) wait( NULL );
#endif
Hallvard Furuseth's avatar
Hallvard Furuseth committed
466
    (void) SIGNAL( sig, wait4child );
Hallvard Furuseth's avatar
Hallvard Furuseth committed
467
468
469
470
471
472
    errno = save_errno;
}

#endif /* SIGCHLD || SIGCLD */


473
474
475
476
477
478
479
480
#ifdef LOG_LOCAL4

/*
 *  Convert a string to an integer by means of a dispatcher table
 *  if the string is not in the table return the default
 */

static int
Hallvard Furuseth's avatar
Hallvard Furuseth committed
481
cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
{
    int        retVal = defaultVal;
    STRDISP_P  disp;

    for (disp = dispatcher; disp->stringVal; disp++) {

        if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {

            retVal = disp->intVal;
            break;

        }
    }

    return (retVal);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
497
}
498
499

#endif  /* LOG_LOCAL4 */