main.c 11.6 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4

#include <stdio.h>

5
#include <ac/signal.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
9
#include <ac/unistd.h>
Hallvard Furuseth's avatar
Hallvard Furuseth committed
10
11
12
#include <ac/wait.h>
#include <ac/signal.h>
#include <ac/errno.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
13

14
#include "ldap_defaults.h"
15
#include "slap.h"
Hallvard Furuseth's avatar
Hallvard Furuseth committed
16
#include "lutil.h"			/* Get lutil_detach() */
17
18

int set_socket( struct sockaddr_in *addr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
19

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

24
#ifdef HAVE_WINSOCK
25
#define MAIN_RETURN(x) return
26
27
struct sockaddr_in	bind_addr;

28
/* in nt_main.c */
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
extern SERVICE_STATUS			SLAPDServiceStatus;
extern SERVICE_STATUS_HANDLE	hSLAPDServiceStatus;
extern ldap_pvt_thread_cond_t	started_event,		stopped_event;
extern int	  is_NT_Service;

void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, short port, int udp );
void LogSlapdStoppedEvent( char *svc );

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

#define SERVICE_EXIT( e, n ) \
		if ( is_NT_Service ) \
{ \
			SLAPDServiceStatus.dwWin32ExitCode				= e; \
			SLAPDServiceStatus.dwServiceSpecificExitCode	= n; \
} 
#else
#define SERVICE_EXIT( e, n )
50
#define MAIN_RETURN(x) return(x)
51
52
53
#endif

short port = LDAP_PORT;
54
55
56
57
58
/*
 * 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
59
#ifndef HAVE_MKVERSION
Kurt Zeilenga's avatar
Kurt Zeilenga committed
60
61
const char Versionstr[] =
	OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
Kurt Zeilenga's avatar
Kurt Zeilenga committed
62
63
#endif

64
65
66
67
68
#ifdef LOG_LOCAL4

#define DEFAULT_SYSLOG_USER  LOG_LOCAL4

typedef struct _str2intDispatch {
69
70
71
	char    *stringVal;
	int      abbr;
	int      intVal;
72
73
74
75
76
77
78
79
80
81
82
83
84
} STRDISP, *STRDISP_P;


/* table to compute syslog-options to integer */
static STRDISP  syslog_types[] = {
    { "LOCAL0",         6, LOG_LOCAL0 },
    { "LOCAL1",         6, LOG_LOCAL1 },
    { "LOCAL2",         6, LOG_LOCAL2 },
    { "LOCAL3",         6, LOG_LOCAL3 },
    { "LOCAL4",         6, LOG_LOCAL4 },
    { "LOCAL5",         6, LOG_LOCAL5 },
    { "LOCAL6",         6, LOG_LOCAL6 },
    { "LOCAL7",         6, LOG_LOCAL7 },
85
    { NULL }
86
87
};

Hallvard Furuseth's avatar
Hallvard Furuseth committed
88
static int   cnvt_str2int( char *, STRDISP_P, int );
89
90
91
92

#endif  /* LOG_LOCAL4 */


Hallvard Furuseth's avatar
Hallvard Furuseth committed
93
static void
94
usage( char *name )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
95
{
96
	fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
97
    fprintf( stderr, "\n        [-a bind-address] [-i] [-u]" );
98
99
100
#ifdef HAVE_WINSOCK
	fprintf( stderr, " [-v NTserviceName]" );
#endif
101
102
103
#if LDAP_CONNECTIONLESS
	fprintf( stderr, " [-c]" );
#endif
104
105
106
#ifdef SLAPD_BDB2
    fprintf( stderr, " [-t]" );
#endif
107
108
#ifdef LOG_LOCAL4
    fprintf( stderr, " [-l sysloguser]" );
109
#endif
110
111
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
    fprintf( stderr, " [-u user] [-g group]" );
112
113
#endif
    fprintf( stderr, "\n" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
114
115
}

116
time_t starttime;
117
struct sockaddr_in	bind_addr;
118
int tcps;
119

120
121
122
123
124
#ifdef HAVE_WINSOCK
void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
{
#else
int main( int argc, char **argv )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
125
{
126
127
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
128
129
	int		i;
	int		inetd = 0;
Gary Williams's avatar
NT port    
Gary Williams committed
130
	int		rc;
131
	struct slapd_args args;
132
	int		udp;
133
134
135
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
	char *username = NULL;
	char *groupname = NULL;
136
#endif
137
138
#ifdef LOG_LOCAL4
    int     syslogUser = DEFAULT_SYSLOG_USER;
139
#endif
140
#ifdef HAVE_WINSOCK
141
	char        *NTservice  = SERVICE_NAME;
142
143
144
	char		*configfile = ".\\slapd.conf";
#else
	char		*configfile = SLAPD_DEFAULT_CONFIGFILE;
145
#endif
146
	char        *serverName;
147
	int         serverMode = SLAP_SERVER_MODE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
148

149
150
151
	(void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
	bind_addr.sin_family = AF_INET;
	bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
152
	bind_addr.sin_port = htons(port);
153

Kurt Zeilenga's avatar
Kurt Zeilenga committed
154
155
156
	g_argc = argc;
	g_argv = argv;

157
158
159
160
161
162
#ifdef HAVE_WINSOCK
	{
		int *newPort;
		int *newDebugLevel;
		char *newConfigFile;
		ldap_debug = 0xffff;
163
		if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		newPort = (int*)getRegParam( NULL, "Port" );
		if ( newPort != NULL )
		{
			port = *newPort;
			bind_addr.sin_port = htons(port);
			Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
		}
		newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
		if ( newDebugLevel != NULL ) 
		{
			slap_debug = *newDebugLevel;
			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

187
	while ( (i = getopt( argc, argv,
188
			     "d:f:ia:p:s:u"
189
190
191
#ifdef LOG_LOCAL4
			     "l:"
#endif
192
#ifdef SLAPD_BDB2
193
			     "t"
194
#endif
195
196
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
			     "u:g:"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
197
198
199
#endif
#ifdef LDAP_CONNECTIONLESS
				 "c"
200
201
202
#endif
#ifdef HAVE_WINSOCK
				 "v:"
203
#endif
204
			     )) != EOF ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
205
		switch ( i ) {
206
		case 'a':	/* bind address */
Gary Williams's avatar
NT port    
Gary Williams committed
207
#ifdef HAVE_WINSOCK
208
			if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
Gary Williams's avatar
NT port    
Gary Williams committed
209
#else
210
			if(!inet_aton(optarg, &bind_addr.sin_addr))
Gary Williams's avatar
NT port    
Gary Williams committed
211
#endif
212
			{
213
214
215
216
				fprintf(stderr, "invalid address (%s) for -a option", optarg);
			}
            break;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifdef LDAP_DEBUG
		case 'd':	/* turn on debugging */
			if ( optarg[0] == '?' ) {
				printf( "Debug levels:\n" );
				printf( "\tLDAP_DEBUG_TRACE\t%d\n",
				    LDAP_DEBUG_TRACE );
				printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
				    LDAP_DEBUG_PACKETS );
				printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
				    LDAP_DEBUG_ARGS );
				printf( "\tLDAP_DEBUG_CONNS\t%d\n",
				    LDAP_DEBUG_CONNS );
				printf( "\tLDAP_DEBUG_BER\t\t%d\n",
				    LDAP_DEBUG_BER );
				printf( "\tLDAP_DEBUG_FILTER\t%d\n",
				    LDAP_DEBUG_FILTER );
				printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
				    LDAP_DEBUG_CONFIG );
				printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
				    LDAP_DEBUG_ACL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
237
				printf( "\tLDAP_DEBUG_STATS\t%d\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
				    LDAP_DEBUG_STATS );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
				printf( "\tLDAP_DEBUG_STATS2\t%d\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
240
				    LDAP_DEBUG_STATS2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
241
				printf( "\tLDAP_DEBUG_SHELL\t%d\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242
				    LDAP_DEBUG_SHELL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
243
				printf( "\tLDAP_DEBUG_PARSE\t%d\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
244
245
246
247
248
				    LDAP_DEBUG_PARSE );
				printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
				    LDAP_DEBUG_ANY );
				exit( 0 );
			} else {
249
				slap_debug |= atoi( optarg );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
250
251
252
253
254
255
256
257
258
259
			}
			break;
#else
		case 'd':	/* turn on debugging */
			fprintf( stderr,
			    "must compile with LDAP_DEBUG for debugging\n" );
			break;
#endif

		case 'f':	/* read config file */
260
			configfile = ch_strdup( optarg );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
261
262
263
264
265
266
			break;

		case 'i':	/* run from inetd */
			inetd = 1;
			break;

267
		case 'p': {	/* port on which to listen */
268
				port = (short)atoi( optarg );
269
270
271
272
273
274
				if(! port ) {
					fprintf(stderr, "-p %s must be numeric\n", optarg);
				} else {
					bind_addr.sin_port = htons(port);
				}
			} break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
275
276
277
278
279

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

280
281
282
283
284
285
286
#ifdef LOG_LOCAL4
		case 'l':	/* set syslog local user */
			syslogUser = cnvt_str2int( optarg, syslog_types,
                                           DEFAULT_SYSLOG_USER );
			break;
#endif

287
288
#ifdef LDAP_CONNECTIONLESS
		case 'c':	/* do connectionless (udp) */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
289
290
			udp = 1;
			break;
291
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
292

293
294
295
296
297
298
#ifdef SLAPD_BDB2
		case 't':  /* timed server */
			serverMode = SLAP_TIMEDSERVER_MODE;
			break;
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
299
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
300
301
		case 'u':	/* user name */
			if( username ) free(username);
302
303
304
			username = ch_strdup( optarg );
			break;

305
306
		case 'g':	/* group name */
			if( groupname ) free(groupname);
307
308
			groupname = ch_strdup( optarg );
			break;
309
#endif /* SETUID && GETUID */
310
311
312
313
314
#ifdef HAVE_WINSOCK
		case 'v':  /* NT service name */
			NTservice = ch_strdup( optarg );
			break;
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
315
316
		default:
			usage( argv[0] );
317
318
319
			rc = 1;
			SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
			goto stop;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320
321
322
		}
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
323
	ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
324
325
326
	ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
	ldif_debug = slap_debug;

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

329
	if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
330
		serverName = ch_strdup( argv[0] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
331
	} else {
332
		serverName = ch_strdup( serverName + 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
333
334
335
	}

#ifdef LOG_LOCAL4
336
	openlog( serverName, OPENLOG_OPTIONS, syslogUser );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
337
#else
338
	openlog( serverName, OPENLOG_OPTIONS );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
339
340
#endif

341
#if defined(HAVE_SETUID) && defined(HAVE_SETGID)
342
343
344
345
	if ( username != NULL || groupname != NULL )
		slap_init_user( username, groupname );
#endif

346
	if ( slap_init( serverMode, serverName ) != 0 ) {
347
		rc = 1;
348
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
349
350
351
352
353
		goto destroy;
	}

	if ( read_config( configfile ) != 0 ) {
		rc = 1;
354
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
355
356
357
		goto destroy;
	}

358
359

	tcps = set_socket( inetd ? NULL : &bind_addr );
360
361
	if ( tcps == -1 )
		goto destroy;
362

363
364
	(void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
	(void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
365
#ifdef SIGPIPE
366
	(void) SIGNAL( SIGPIPE, SIG_IGN );
367
368
#endif
#ifdef SIGHUP
369
	(void) SIGNAL( SIGHUP, slap_set_shutdown );
370
#endif
371
372
	(void) SIGNAL( SIGINT, slap_set_shutdown );
	(void) SIGNAL( SIGTERM, slap_set_shutdown );
373
374
#ifdef LDAP_SIGCHLD
	(void) SIGNAL( LDAP_SIGCHLD, wait4child );
Hallvard Furuseth's avatar
Hallvard Furuseth committed
375
#endif
376
#ifdef HAVE_WINSOCK
377
	/* SIGBREAK is generated when Ctrl-Break is pressed. */
378
379
	(void) SIGNAL( SIGBREAK, slap_set_shutdown );
#endif
380

381
#ifndef HAVE_WINSOCK
382
	if(!inetd) {
383
384
385
386
387
#ifdef LDAP_DEBUG
		lutil_detach( ldap_debug, 0 );
#else
		lutil_detach( 0, 0 );
#endif
388
	}
389
#endif /* HAVE_WINSOC */
390

391
392
	if ( slap_startup(-1)  != 0 ) {
		rc = 1;
393
		SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
394
395
		goto shutdown;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
396

397
398
	if(!inetd) {
		FILE *fp;
399

400
401
		args.addr = &bind_addr;

402
		Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
403

404
405
406
407
408
		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
409
410
		}

411
412
413
414
415
		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
416
			}
417
418
419
			fprintf( fp, "\n" );
			fclose( fp );
		}
420
421
422

	} else {
		args.addr = NULL;
423
	}
424
	args.tcps = tcps;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
425

426
	time( &starttime );
427
#ifdef HAVE_WINSOCK
428
	LogSlapdStartedEvent( NTservice, slap_debug, configfile, port, udp );
429
430
431
432
433
#endif

	rc = slapd_daemon( &args );

#ifdef HAVE_WINSOCK
434
	/* Throw away the event that we used during the startup process. */
435
436
437
	if ( is_NT_Service )
		ldap_pvt_thread_cond_destroy( &started_event );
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
438

439
440

shutdown:
441
442
	/* remember an error during shutdown */
	rc |= slap_shutdown(-1);
443
destroy:
444
445
446
	/* remember an error during destroy */
	rc |= slap_destroy();

447
448
stop:
#ifdef HAVE_WINSOCK
449
	LogSlapdStoppedEvent( NTservice );
450
#endif
451
	Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
452
453
454
#ifdef HAVE_WINSOCK
	ReportSlapdShutdownComplete();
#endif
455

456
    closelog();
457

458
	MAIN_RETURN(rc);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
459
}
460
461


462
#ifdef LDAP_SIGCHLD
Hallvard Furuseth's avatar
Hallvard Furuseth committed
463
464
465
466
467
468
469
470
471

/*
 *  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
472
473

#ifdef WNOHANG
Hallvard Furuseth's avatar
Hallvard Furuseth committed
474
    errno = 0;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
475
#ifdef HAVE_WAITPID
Hallvard Furuseth's avatar
Hallvard Furuseth committed
476
477
    while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
	;	/* NULL */
Hallvard Furuseth's avatar
Hallvard Furuseth committed
478
#else
Hallvard Furuseth's avatar
Hallvard Furuseth committed
479
480
    while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
	;	/* NULL */
Hallvard Furuseth's avatar
Hallvard Furuseth committed
481
#endif
482
#else
Hallvard Furuseth's avatar
Hallvard Furuseth committed
483
484
    (void) wait( NULL );
#endif
Hallvard Furuseth's avatar
Hallvard Furuseth committed
485
    (void) SIGNAL( sig, wait4child );
Hallvard Furuseth's avatar
Hallvard Furuseth committed
486
487
488
489
490
491
    errno = save_errno;
}

#endif /* SIGCHLD || SIGCLD */


492
493
494
495
496
497
498
499
#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
500
cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
{
    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);

} /* cnvt_str2int */

#endif  /* LOG_LOCAL4 */