]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
ldif.h include cleanup
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/signal.h>
11 #include <ac/socket.h>
12 #include <ac/string.h>
13 #include <ac/time.h>
14 #include <ac/unistd.h>
15 #include <ac/wait.h>
16 #include <ac/errno.h>
17
18 #include "slap.h"
19 #include "lutil.h"
20 #include "ldif.h"
21
22 #ifdef LDAP_SIGCHLD
23 static RETSIGTYPE wait4child( int sig );
24 #endif
25
26 #ifdef HAVE_NT_SERVICE_MANAGER
27 #define MAIN_RETURN(x) return
28 struct sockaddr_in      bind_addr;
29
30 /* in nt_main.c */
31 LDAP_LUTIL_V(SERVICE_STATUS)            SLAPDServiceStatus;
32 LDAP_LUTIL_V(SERVICE_STATUS_HANDLE)     hSLAPDServiceStatus;
33 extern ldap_pvt_thread_cond_t   started_event,          stopped_event;
34 extern int        is_NT_Service;
35
36 void CommenceStartupProcessing( LPCTSTR serverName,
37                                                            void(*stopper)(int));
38 void ReportSlapdShutdownComplete( void );
39 void *getRegParam( char *svc, char *value );
40
41 #define SERVICE_EXIT( e, n )    do { \
42         if ( is_NT_Service ) { \
43                 SLAPDServiceStatus.dwWin32ExitCode                              = (e); \
44                 SLAPDServiceStatus.dwServiceSpecificExitCode    = (n); \
45         } \
46 } while ( 0 )
47
48 #else
49 #define SERVICE_EXIT( e, n )
50 #define MAIN_RETURN(x) return(x)
51 #endif
52
53 #ifdef HAVE_NT_EVENT_LOG
54 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
55 void LogSlapdStoppedEvent( char *svc );
56 #endif
57
58 /*
59  * when more than one slapd is running on one machine, each one might have
60  * it's own LOCAL for syslogging and must have its own pid/args files
61  */
62
63 #ifndef HAVE_MKVERSION
64 const char Versionstr[] =
65         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
66 #endif
67
68 #ifdef LOG_LOCAL4
69
70 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
71
72 typedef struct _str2intDispatch {
73         char    *stringVal;
74         int      abbr;
75         int      intVal;
76 } STRDISP, *STRDISP_P;
77
78
79 /* table to compute syslog-options to integer */
80 static STRDISP  syslog_types[] = {
81         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
82         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
83         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
84         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
85         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
86         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
87         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
88         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
89         { NULL }
90 };
91
92 static int   cnvt_str2int( char *, STRDISP_P, int );
93
94 #endif  /* LOG_LOCAL4 */
95
96
97 static void
98 usage( char *name )
99 {
100         fprintf( stderr,
101                 "usage: %s options\n", name );
102         fprintf( stderr,
103                 "\t-d level\tDebug Level" "\n"
104                 "\t-f filename\tConfiguration File\n"
105 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
106                 "\t-g group\tGroup (id or name) to ran as\n"
107 #endif
108                 "\t-h URLs\tList of URLs to serve\n"
109 #ifdef LOG_LOCAL4
110                 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
111 #endif
112                 "\t-n serverName\tservice name\n"
113 #ifdef HAVE_CHROOT
114                 "\t-r directory\n"
115 #endif
116                 "\t-s level\tSyslog Level\n"
117 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
118                 "\t-u user\tUser (id or name) to ran as\n"
119 #endif
120     );
121 }
122
123 #ifdef HAVE_NT_SERVICE_MANAGER
124 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
125 #else
126 int main( int argc, char **argv )
127 #endif
128 {
129         int             i, no_detach = 0;
130         int             rc;
131         char *urls = NULL;
132 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
133         char *username = NULL;
134         char *groupname = NULL;
135 #endif
136 #if defined(HAVE_CHROOT)
137         char *sandbox = NULL;
138 #endif
139 #ifdef LOG_LOCAL4
140     int     syslogUser = DEFAULT_SYSLOG_USER;
141 #endif
142
143 #ifdef HAVE_NT_SERVICE_MANAGER
144         char            *configfile = ".\\slapd.conf";
145 #else
146         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
147 #endif
148         char        *serverName = NULL;
149         int         serverMode = SLAP_SERVER_MODE;
150
151 #ifdef CSRIMALLOC
152         FILE *leakfile;
153         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
154                 leakfile = stderr;
155         }
156 #endif
157
158         g_argc = argc;
159         g_argv = argv;
160
161 #ifdef HAVE_NT_SERVICE_MANAGER
162         {
163                 int *i;
164                 char *newConfigFile;
165                 char *newUrls;
166                 char *regService = NULL;
167
168                 if ( is_NT_Service ) {
169                         serverName = argv[0];
170                         CommenceStartupProcessing( serverName, slap_sig_shutdown );
171                         if ( strcmp(serverName, SERVICE_NAME) )
172                             regService = serverName;
173                 }
174
175                 i = (int*)getRegParam( regService, "DebugLevel" );
176                 if ( i != NULL ) 
177                 {
178                         slap_debug = *i;
179 #ifdef NEW_LOGGING
180                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
181                                    "main: new debug level from registry is: %d\n", slap_debug ));
182 #else
183                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
184 #endif
185                 }
186
187                 newUrls = (char *) getRegParam(regService, "Urls");
188                 if (newUrls)
189                 {
190                     if (urls)
191                         ch_free(urls);
192
193                     urls = ch_strdup(newUrls);
194 #ifdef NEW_LOGGING
195                     LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
196                                "main: new urls from registry: %s\n", urls ));
197 #else
198                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
199                           urls, 0, 0);
200 #endif
201
202                 }
203
204                 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
205                 if ( newConfigFile != NULL ) 
206                 {
207                         configfile = newConfigFile;
208 #ifdef NEW_LOGGING
209                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
210                                    "main: new config file from registry is: %s\n", configfile ));
211 #else
212                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
213 #endif
214
215                 }
216         }
217 #endif
218
219         while ( (i = getopt( argc, argv,
220                              "d:f:h:s:n:"
221 #ifdef HAVE_CHROOT
222                                 "r:"
223 #endif
224 #ifdef LOG_LOCAL4
225                              "l:"
226 #endif
227 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
228                              "u:g:"
229 #endif
230                              )) != EOF ) {
231                 switch ( i ) {
232                 case 'h':       /* listen URLs */
233                         if ( urls != NULL ) free( urls );
234                         urls = ch_strdup( optarg );
235             break;
236
237                 case 'd':       /* set debug level and 'do not detach' flag */
238                         no_detach = 1;
239 #ifdef LDAP_DEBUG
240                         slap_debug |= atoi( optarg );
241 #else
242                         if ( atoi( optarg ) != 0 )
243                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
244                                        stderr );
245 #endif
246                         break;
247
248                 case 'f':       /* read config file */
249                         configfile = ch_strdup( optarg );
250                         break;
251
252                 case 's':       /* set syslog level */
253                         ldap_syslog = atoi( optarg );
254                         break;
255
256 #ifdef LOG_LOCAL4
257                 case 'l':       /* set syslog local user */
258                         syslogUser = cnvt_str2int( optarg,
259                                 syslog_types, DEFAULT_SYSLOG_USER );
260                         break;
261 #endif
262
263 #ifdef HAVE_CHROOT
264                 case 'r':
265                         if( sandbox ) free(sandbox);
266                         sandbox = ch_strdup( optarg );
267                         break;
268 #endif
269
270 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
271                 case 'u':       /* user name */
272                         if( username ) free(username);
273                         username = ch_strdup( optarg );
274                         break;
275
276                 case 'g':       /* group name */
277                         if( groupname ) free(groupname);
278                         groupname = ch_strdup( optarg );
279                         break;
280 #endif /* SETUID && GETUID */
281
282                 case 'n':  /* NT service name */
283                         if( serverName != NULL ) free( serverName );
284                         serverName = ch_strdup( optarg );
285                         break;
286
287                 default:
288                         usage( argv[0] );
289                         rc = 1;
290                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
291                         goto stop;
292                 }
293         }
294
295 #ifdef NEW_LOGGING
296         lutil_log_initialize( argc, argv );
297 #endif
298
299         lutil_set_debug_level( "slapd", slap_debug );
300         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
301         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
302         ldif_debug = slap_debug;
303
304 #ifdef NEW_LOGGING
305         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
306                    "%s", Versionstr ));
307 #else
308         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
309 #endif
310
311
312         if( serverName == NULL ) {
313                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
314                         serverName = ch_strdup( argv[0] );
315                 } else {
316                         serverName = ch_strdup( serverName + 1 );
317                 }
318         }
319
320 #ifdef LOG_LOCAL4
321         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
322 #elif LOG_DEBUG
323         openlog( serverName, OPENLOG_OPTIONS );
324 #endif
325
326         if( slapd_daemon_init( urls ) != 0 ) {
327                 rc = 1;
328                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
329                 goto stop;
330         }
331
332 #if defined(HAVE_CHROOT)
333         if ( sandbox ) {
334                 if ( chdir( sandbox ) ) {
335                         perror("chdir");
336                         rc = 1;
337                         goto stop;
338                 }
339                 if ( chroot( sandbox ) ) {
340                         perror("chroot");
341                         rc = 1;
342                         goto stop;
343                 }
344         }
345 #endif
346
347 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
348         if ( username != NULL || groupname != NULL ) {
349                 slap_init_user( username, groupname );
350         }
351 #endif
352
353         extops_init();
354
355 #ifdef SLAPD_MODULES
356         if ( module_init() != 0 ) {
357                 rc = 1;
358                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
359                 goto destroy;
360         }
361 #endif
362
363         if ( slap_init( serverMode, serverName ) != 0 ) {
364                 rc = 1;
365                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
366                 goto destroy;
367         }
368
369         if ( schema_init( ) != 0 ) {
370 #ifdef NEW_LOGGING
371                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
372                            "main: schema initialization error\n" ));
373 #else
374                 Debug( LDAP_DEBUG_ANY,
375                     "schema initialization error\n",
376                     0, 0, 0 );
377 #endif
378
379                 goto destroy;
380         }
381
382         if ( read_config( configfile ) != 0 ) {
383                 rc = 1;
384                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
385                 goto destroy;
386         }
387
388         if ( schema_prep( ) != 0 ) {
389 #ifdef NEW_LOGGING
390                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
391                            "main: schema prep error\n"));
392 #else
393                 Debug( LDAP_DEBUG_ANY,
394                     "schema prep error\n",
395                     0, 0, 0 );
396 #endif
397
398                 goto destroy;
399         }
400
401 #ifdef HAVE_TLS
402         rc = ldap_pvt_tls_init();
403         if( rc != 0) {
404 #ifdef NEW_LOGGING
405                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
406                            "main: tls init failed: %d\n", rc ));
407 #else
408                 Debug( LDAP_DEBUG_ANY,
409                     "main: TLS init failed: %d\n",
410                     0, 0, 0 );
411 #endif
412                 rc = 1;
413                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
414                 goto destroy;
415         }
416
417         rc = ldap_pvt_tls_init_def_ctx();
418         if( rc != 0) {
419 #ifdef NEW_LOGGING
420                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
421                            "main: tls init def ctx failed: %d\n", rc ));
422 #else
423                 Debug( LDAP_DEBUG_ANY,
424                     "main: TLS init def ctx failed: %d\n",
425                     0, 0, 0 );
426 #endif
427                 rc = 1;
428                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
429                 goto destroy;
430         }
431 #endif
432
433         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
434         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
435
436 #ifdef SIGPIPE
437         (void) SIGNAL( SIGPIPE, SIG_IGN );
438 #endif
439 #ifdef SIGHUP
440         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
441 #endif
442         (void) SIGNAL( SIGINT, slap_sig_shutdown );
443         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
444 #ifdef LDAP_SIGCHLD
445         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
446 #endif
447 #ifdef SIGBREAK
448         /* SIGBREAK is generated when Ctrl-Break is pressed. */
449         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
450 #endif
451
452 #ifndef HAVE_WINSOCK
453         lutil_detach( no_detach, 0 );
454 #endif /* HAVE_WINSOCK */
455
456 #ifdef CSRIMALLOC
457         mal_leaktrace(1);
458 #endif
459
460         if ( slap_startup( NULL )  != 0 ) {
461                 rc = 1;
462                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
463                 goto shutdown;
464         }
465
466         {
467                 FILE *fp;
468
469 #ifdef NEW_LOGGING
470                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
471                            "main: slapd starting.\n" ));
472 #else
473                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
474 #endif
475
476
477                 if (( slapd_pid_file != NULL ) &&
478                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
479                 {
480                         fprintf( fp, "%d\n", (int) getpid() );
481                         fclose( fp );
482                 }
483
484                 if (( slapd_args_file != NULL ) &&
485                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
486                 {
487                         for ( i = 0; i < g_argc; i++ ) {
488                                 fprintf( fp, "%s ", g_argv[i] );
489                         }
490                         fprintf( fp, "\n" );
491                         fclose( fp );
492                 }
493         }
494
495 #ifdef HAVE_NT_EVENT_LOG
496         if (is_NT_Service)
497         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
498 #endif
499
500         rc = slapd_daemon();
501
502 #ifdef HAVE_NT_SERVICE_MANAGER
503         /* Throw away the event that we used during the startup process. */
504         if ( is_NT_Service )
505                 ldap_pvt_thread_cond_destroy( &started_event );
506 #endif
507
508 shutdown:
509         /* remember an error during shutdown */
510         rc |= slap_shutdown( NULL );
511
512 destroy:
513         /* remember an error during destroy */
514         rc |= slap_destroy();
515
516 #ifdef SLAPD_MODULES
517         module_kill();
518 #endif
519
520         extops_kill();
521
522 stop:
523 #ifdef HAVE_NT_EVENT_LOG
524         if (is_NT_Service)
525         LogSlapdStoppedEvent( serverName );
526 #endif
527
528 #ifdef NEW_LOGGING
529         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
530                    "main: slapd stopped.\n" ));
531 #else
532         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
533 #endif
534
535
536 #ifdef HAVE_NT_SERVICE_MANAGER
537         ReportSlapdShutdownComplete();
538 #endif
539
540 #ifdef LOG_DEBUG
541     closelog();
542 #endif
543         slapd_daemon_destroy();
544
545 #ifdef CSRIMALLOC
546         mal_dumpleaktrace( leakfile );
547 #endif
548
549         MAIN_RETURN(rc);
550 }
551
552
553 #ifdef LDAP_SIGCHLD
554
555 /*
556  *  Catch and discard terminated child processes, to avoid zombies.
557  */
558
559 static RETSIGTYPE
560 wait4child( int sig )
561 {
562     int save_errno = errno;
563
564 #ifdef WNOHANG
565     errno = 0;
566 #ifdef HAVE_WAITPID
567     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
568         ;       /* NULL */
569 #else
570     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
571         ;       /* NULL */
572 #endif
573 #else
574     (void) wait( NULL );
575 #endif
576     (void) SIGNAL_REINSTALL( sig, wait4child );
577     errno = save_errno;
578 }
579
580 #endif /* SIGCHLD || SIGCLD */
581
582
583 #ifdef LOG_LOCAL4
584
585 /*
586  *  Convert a string to an integer by means of a dispatcher table
587  *  if the string is not in the table return the default
588  */
589
590 static int
591 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
592 {
593     int        retVal = defaultVal;
594     STRDISP_P  disp;
595
596     for (disp = dispatcher; disp->stringVal; disp++) {
597
598         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
599
600             retVal = disp->intVal;
601             break;
602
603         }
604     }
605
606     return (retVal);
607 }
608
609 #endif  /* LOG_LOCAL4 */