]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
1. Session history support
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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/socket.h>
11 #include <ac/string.h>
12 #include <ac/time.h>
13 #include <ac/unistd.h>
14 #include <ac/wait.h>
15 #include <ac/errno.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20 #include "lutil.h"
21 #include "ldif.h"
22
23 #ifdef LDAP_SLAPI
24 #include "slapi.h"
25 #endif
26
27 #ifdef LDAP_SIGCHLD
28 static RETSIGTYPE wait4child( int sig );
29 #endif
30
31 #ifdef HAVE_NT_SERVICE_MANAGER
32 #define MAIN_RETURN(x) return
33 static struct sockaddr_in       bind_addr;
34
35 #define SERVICE_EXIT( e, n )    do { \
36         if ( is_NT_Service ) { \
37                 lutil_ServiceStatus.dwWin32ExitCode                             = (e); \
38                 lutil_ServiceStatus.dwServiceSpecificExitCode   = (n); \
39         } \
40 } while ( 0 )
41
42 #else
43 #define SERVICE_EXIT( e, n )
44 #define MAIN_RETURN(x) return(x)
45 #endif
46
47 /*
48  * when more than one slapd is running on one machine, each one might have
49  * it's own LOCAL for syslogging and must have its own pid/args files
50  */
51
52 #ifndef HAVE_MKVERSION
53 const char Versionstr[] =
54         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
55 #endif
56
57 #ifdef LOG_LOCAL4
58
59 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
60
61 typedef struct _str2intDispatch {
62         char    *stringVal;
63         int      abbr;
64         int      intVal;
65 } STRDISP, *STRDISP_P;
66
67
68 /* table to compute syslog-options to integer */
69 static STRDISP  syslog_types[] = {
70         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
71         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
72         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
73         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
74         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
75         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
76         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
77         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
78         { NULL, 0, 0 }
79 };
80
81 static int   cnvt_str2int( char *, STRDISP_P, int );
82
83 #endif  /* LOG_LOCAL4 */
84
85 static int check_config = 0;
86
87 static void
88 usage( char *name )
89 {
90         fprintf( stderr,
91                 "usage: %s options\n", name );
92         fprintf( stderr,
93                 "\t-4\t\tIPv4 only\n"
94                 "\t-6\t\tIPv6 only\n"
95                 "\t-c cookie\tSync cookie of consumer\n"
96                 "\t-d level\tDebug level" "\n"
97                 "\t-f filename\tConfiguration file\n"
98 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
99                 "\t-g group\tGroup (id or name) to run as\n"
100 #endif
101                 "\t-h URLs\t\tList of URLs to serve\n"
102 #ifdef LOG_LOCAL4
103                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
104 #endif
105                 "\t-n serverName\tService name\n"
106 #ifdef HAVE_CHROOT
107                 "\t-r directory\tSandbox directory to chroot to\n"
108 #endif
109                 "\t-s level\tSyslog level\n"
110                 "\t-t\t\tCheck configuration file and exit\n"
111 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
112                 "\t-u user\t\tUser (id or name) to run as\n"
113 #endif
114     );
115 }
116
117 #ifdef HAVE_NT_SERVICE_MANAGER
118 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
119 #else
120 int main( int argc, char **argv )
121 #endif
122 {
123         int             i, no_detach = 0;
124         int             rc = 1;
125         char *urls = NULL;
126 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
127         char *username = NULL;
128         char *groupname = NULL;
129 #endif
130 #if defined(HAVE_CHROOT)
131         char *sandbox = NULL;
132 #endif
133 #ifdef LOG_LOCAL4
134     int     syslogUser = DEFAULT_SYSLOG_USER;
135 #endif
136         
137         int g_argc = argc;
138         char **g_argv = argv;
139
140 #ifdef HAVE_NT_SERVICE_MANAGER
141         char            *configfile = ".\\slapd.conf";
142 #else
143         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
144 #endif
145         char        *serverName = NULL;
146         int         serverMode = SLAP_SERVER_MODE;
147
148         struct berval cookie = { 0, NULL };
149
150 #ifdef CSRIMALLOC
151         FILE *leakfile;
152         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
153                 leakfile = stderr;
154         }
155 #endif
156
157         sl_mem_init();
158
159 #ifdef HAVE_NT_SERVICE_MANAGER
160         {
161                 int *i;
162                 char *newConfigFile;
163                 char *newUrls;
164                 char *regService = NULL;
165
166                 if ( is_NT_Service ) {
167                         serverName = argv[0];
168                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
169                         if ( strcmp(serverName, SERVICE_NAME) )
170                             regService = serverName;
171                 }
172
173                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
174                 if ( i != NULL ) 
175                 {
176                         slap_debug = *i;
177 #ifdef NEW_LOGGING
178                         lutil_log_initialize( argc, argv );
179                         LDAP_LOG( SLAPD, INFO, 
180                                 "main: new debug level from registry is: %d\n", 
181                                 slap_debug, 0, 0 );
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 *) lutil_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( SLAPD, INFO, 
196                                 "main: new urls from registry: %s\n", urls, 0, 0 );
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*)lutil_getRegParam( regService, "ConfigFile" );
205                 if ( newConfigFile != NULL ) 
206                 {
207                         configfile = newConfigFile;
208 #ifdef NEW_LOGGING
209                         LDAP_LOG( SLAPD, INFO, 
210                                 "main: new config file from registry is: %s\n", configfile, 0, 0 );
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                              "c:d:f:h:s:n:t"
221 #if LDAP_PF_INET6
222                                 "46"
223 #endif
224 #ifdef HAVE_CHROOT
225                                 "r:"
226 #endif
227 #ifdef LOG_LOCAL4
228                              "l:"
229 #endif
230 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
231                              "u:g:"
232 #endif
233                              )) != EOF ) {
234                 switch ( i ) {
235 #ifdef LDAP_PF_INET6
236                 case '4':
237                         slap_inet4or6 = AF_INET;
238                         break;
239                 case '6':
240                         slap_inet4or6 = AF_INET6;
241                         break;
242 #endif
243
244                 case 'h':       /* listen URLs */
245                         if ( urls != NULL ) free( urls );
246                         urls = ch_strdup( optarg );
247             break;
248
249                 case 'c':       /* provide sync cookie, override if exist in replica */
250                         if ( slap_sync_cookie ) {
251                                 slap_sync_cookie_free( slap_sync_cookie, 1 );
252                         }
253                         slap_sync_cookie = (struct sync_cookie *) ch_calloc( 1,
254                                                                 sizeof( struct sync_cookie ));
255                         ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
256                         ber_bvarray_add( &slap_sync_cookie->octet_str, &cookie );
257                         break;
258
259                 case 'd':       /* set debug level and 'do not detach' flag */
260                         no_detach = 1;
261 #ifdef LDAP_DEBUG
262                         slap_debug |= atoi( optarg );
263 #else
264                         if ( atoi( optarg ) != 0 )
265                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
266                                        stderr );
267 #endif
268                         break;
269
270                 case 'f':       /* read config file */
271                         configfile = ch_strdup( optarg );
272                         break;
273
274                 case 's':       /* set syslog level */
275                         ldap_syslog = atoi( optarg );
276                         break;
277
278 #ifdef LOG_LOCAL4
279                 case 'l':       /* set syslog local user */
280                         syslogUser = cnvt_str2int( optarg,
281                                 syslog_types, DEFAULT_SYSLOG_USER );
282                         break;
283 #endif
284
285 #ifdef HAVE_CHROOT
286                 case 'r':
287                         if( sandbox ) free(sandbox);
288                         sandbox = ch_strdup( optarg );
289                         break;
290 #endif
291
292 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
293                 case 'u':       /* user name */
294                         if( username ) free(username);
295                         username = ch_strdup( optarg );
296                         break;
297
298                 case 'g':       /* group name */
299                         if( groupname ) free(groupname);
300                         groupname = ch_strdup( optarg );
301                         break;
302 #endif /* SETUID && GETUID */
303
304                 case 'n':  /* NT service name */
305                         if( serverName != NULL ) free( serverName );
306                         serverName = ch_strdup( optarg );
307                         break;
308
309                 case 't':
310                         check_config++;
311                         break;
312
313                 default:
314                         usage( argv[0] );
315                         rc = 1;
316                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
317                         goto stop;
318                 }
319         }
320
321 #ifdef NEW_LOGGING
322         lutil_log_initialize( argc, argv );
323 #else
324         lutil_set_debug_level( "slapd", slap_debug );
325         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
326         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
327         ldif_debug = slap_debug;
328 #endif
329
330 #ifdef NEW_LOGGING
331         LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
332 #else
333         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
334 #endif
335
336         if( serverName == NULL ) {
337                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
338                         serverName = argv[0];
339                 } else {
340                         serverName = serverName + 1;
341                 }
342         }
343
344 #ifdef LOG_LOCAL4
345         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
346 #elif LOG_DEBUG
347         openlog( serverName, OPENLOG_OPTIONS );
348 #endif
349
350         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
351                 rc = 1;
352                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
353                 goto stop;
354         }
355
356 #if defined(HAVE_CHROOT)
357         if ( sandbox ) {
358                 if ( chdir( sandbox ) ) {
359                         perror("chdir");
360                         rc = 1;
361                         goto stop;
362                 }
363                 if ( chroot( sandbox ) ) {
364                         perror("chroot");
365                         rc = 1;
366                         goto stop;
367                 }
368         }
369 #endif
370
371 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
372         if ( username != NULL || groupname != NULL ) {
373                 slap_init_user( username, groupname );
374         }
375 #endif
376
377         extops_init();
378         lutil_passwd_init();
379         slap_op_init();
380
381 #ifdef SLAPD_MODULES
382         if ( module_init() != 0 ) {
383                 rc = 1;
384                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
385                 goto destroy;
386         }
387 #endif
388
389         if ( slap_schema_init( ) != 0 ) {
390 #ifdef NEW_LOGGING
391                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
392 #else
393                 Debug( LDAP_DEBUG_ANY,
394                     "schema initialization error\n",
395                     0, 0, 0 );
396 #endif
397
398                 goto destroy;
399         }
400
401         if ( slap_init( serverMode, serverName ) != 0 ) {
402                 rc = 1;
403                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
404                 goto destroy;
405         }
406
407         if ( slap_controls_init( ) != 0 ) {
408 #ifdef NEW_LOGGING
409                 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
410 #else
411                 Debug( LDAP_DEBUG_ANY,
412                     "controls initialization error\n",
413                     0, 0, 0 );
414 #endif
415
416                 goto destroy;
417         }
418
419 #ifdef HAVE_TLS
420         /* Library defaults to full certificate checking. This is correct when
421          * a client is verifying a server because all servers should have a
422          * valid cert. But few clients have valid certs, so we want our default
423          * to be no checking. The config file can override this as usual.
424          */
425         rc = 0;
426         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
427 #endif
428
429 #ifdef LDAP_SLAPI
430         if ( slapi_init() != 0 ) {
431 #ifdef NEW_LOGGING
432                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
433 #else
434                 Debug( LDAP_DEBUG_ANY,
435                     "slapi initialization error\n",
436                     0, 0, 0 );
437 #endif
438
439                 goto destroy;
440         }
441 #endif /* LDAP_SLAPI */
442
443         if ( read_config( configfile, 0 ) != 0 ) {
444                 rc = 1;
445                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
446
447                 if ( check_config ) {
448                         fprintf( stderr, "config check failed\n" );
449                 }
450
451                 goto destroy;
452         }
453
454         if ( check_config ) {
455                 rc = 0;
456                 fprintf( stderr, "config check succeeded\n" );
457                 goto destroy;
458         }
459
460         if ( glue_sub_init( ) != 0 ) {
461 #ifdef NEW_LOGGING
462                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
463 #else
464                 Debug( LDAP_DEBUG_ANY,
465                     "subordinate config error\n",
466                     0, 0, 0 );
467 #endif
468                 goto destroy;
469         }
470
471         if ( slap_schema_check( ) != 0 ) {
472 #ifdef NEW_LOGGING
473                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
474 #else
475                 Debug( LDAP_DEBUG_ANY,
476                     "schema prep error\n",
477                     0, 0, 0 );
478 #endif
479
480                 goto destroy;
481         }
482
483 #ifdef HAVE_TLS
484         rc = ldap_pvt_tls_init();
485         if( rc != 0) {
486 #ifdef NEW_LOGGING
487                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
488 #else
489                 Debug( LDAP_DEBUG_ANY,
490                     "main: TLS init failed: %d\n",
491                     0, 0, 0 );
492 #endif
493                 rc = 1;
494                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
495                 goto destroy;
496         }
497
498         rc = ldap_pvt_tls_init_def_ctx();
499         if( rc != 0) {
500 #ifdef NEW_LOGGING
501                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
502 #else
503                 Debug( LDAP_DEBUG_ANY,
504                     "main: TLS init def ctx failed: %d\n",
505                     rc, 0, 0 );
506 #endif
507                 rc = 1;
508                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
509                 goto destroy;
510         }
511 #endif
512
513         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
514         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
515
516 #ifdef SIGPIPE
517         (void) SIGNAL( SIGPIPE, SIG_IGN );
518 #endif
519 #ifdef SIGHUP
520         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
521 #endif
522         (void) SIGNAL( SIGINT, slap_sig_shutdown );
523         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
524 #ifdef LDAP_SIGCHLD
525         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
526 #endif
527 #ifdef SIGBREAK
528         /* SIGBREAK is generated when Ctrl-Break is pressed. */
529         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
530 #endif
531
532 #ifndef HAVE_WINSOCK
533         lutil_detach( no_detach, 0 );
534 #endif /* HAVE_WINSOCK */
535
536 #ifdef CSRIMALLOC
537         mal_leaktrace(1);
538 #endif
539
540         /*
541          * FIXME: moved here from slapd_daemon_task()
542          * because back-monitor db_open() needs it
543          */
544         time( &starttime );
545
546         if ( slap_startup( NULL )  != 0 ) {
547                 rc = 1;
548                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
549                 goto shutdown;
550         }
551
552 #ifdef NEW_LOGGING
553         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
554 #else
555         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
556 #endif
557
558
559         if ( slapd_pid_file != NULL ) {
560                 FILE *fp = fopen( slapd_pid_file, "w" );
561
562                 if( fp != NULL ) {
563                         fprintf( fp, "%d\n", (int) getpid() );
564                         fclose( fp );
565
566                 } else {
567                         free(slapd_pid_file);
568                         slapd_pid_file = NULL;
569                 }
570         }
571
572         if ( slapd_args_file != NULL ) {
573                 FILE *fp = fopen( slapd_args_file, "w" );
574
575                 if( fp != NULL ) {
576                         for ( i = 0; i < g_argc; i++ ) {
577                                 fprintf( fp, "%s ", g_argv[i] );
578                         }
579                         fprintf( fp, "\n" );
580                         fclose( fp );
581                 } else {
582                         free(slapd_args_file);
583                         slapd_args_file = NULL;
584                 }
585         }
586
587 #ifdef HAVE_NT_EVENT_LOG
588         if (is_NT_Service)
589         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
590 #endif
591
592         rc = slapd_daemon();
593
594 #ifdef HAVE_NT_SERVICE_MANAGER
595         /* Throw away the event that we used during the startup process. */
596         if ( is_NT_Service )
597                 ldap_pvt_thread_cond_destroy( &started_event );
598 #endif
599
600 shutdown:
601         /* remember an error during shutdown */
602         rc |= slap_shutdown( NULL );
603
604 destroy:
605         /* remember an error during destroy */
606         rc |= slap_destroy();
607
608 #ifdef SLAPD_MODULES
609         module_kill();
610 #endif
611
612         slap_op_destroy();
613
614         extops_kill();
615
616 stop:
617 #ifdef HAVE_NT_EVENT_LOG
618         if (is_NT_Service)
619         lutil_LogStoppedEvent( serverName );
620 #endif
621
622 #ifdef NEW_LOGGING
623         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
624 #else
625         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
626 #endif
627
628
629 #ifdef HAVE_NT_SERVICE_MANAGER
630         lutil_ReportShutdownComplete();
631 #endif
632
633 #ifdef LOG_DEBUG
634     closelog();
635 #endif
636         slapd_daemon_destroy();
637
638         controls_destroy();
639
640         schema_destroy();
641
642         lutil_passwd_destroy();
643
644 #ifdef HAVE_TLS
645         ldap_pvt_tls_destroy();
646 #endif
647
648         if ( slapd_pid_file != NULL ) {
649                 unlink( slapd_pid_file );
650         }
651         if ( slapd_args_file != NULL ) {
652                 unlink( slapd_args_file );
653         }
654
655         config_destroy();
656
657 #ifdef CSRIMALLOC
658         mal_dumpleaktrace( leakfile );
659 #endif
660
661         MAIN_RETURN(rc);
662 }
663
664
665 #ifdef LDAP_SIGCHLD
666
667 /*
668  *  Catch and discard terminated child processes, to avoid zombies.
669  */
670
671 static RETSIGTYPE
672 wait4child( int sig )
673 {
674     int save_errno = errno;
675
676 #ifdef WNOHANG
677     errno = 0;
678 #ifdef HAVE_WAITPID
679     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
680         ;       /* NULL */
681 #else
682     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
683         ;       /* NULL */
684 #endif
685 #else
686     (void) wait( NULL );
687 #endif
688     (void) SIGNAL_REINSTALL( sig, wait4child );
689     errno = save_errno;
690 }
691
692 #endif /* LDAP_SIGCHLD */
693
694
695 #ifdef LOG_LOCAL4
696
697 /*
698  *  Convert a string to an integer by means of a dispatcher table
699  *  if the string is not in the table return the default
700  */
701
702 static int
703 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
704 {
705     int        retVal = defaultVal;
706     STRDISP_P  disp;
707
708     for (disp = dispatcher; disp->stringVal; disp++) {
709
710         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
711
712             retVal = disp->intVal;
713             break;
714
715         }
716     }
717
718     return (retVal);
719 }
720
721 #endif  /* LOG_LOCAL4 */