]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Import lutil_passwd changes from HEAD
[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-d level\tDebug level" "\n"
96                 "\t-f filename\tConfiguration file\n"
97 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
98                 "\t-g group\tGroup (id or name) to run as\n"
99 #endif
100                 "\t-h URLs\t\tList of URLs to serve\n"
101 #ifdef LOG_LOCAL4
102                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
103 #endif
104                 "\t-n serverName\tService name\n"
105 #ifdef HAVE_CHROOT
106                 "\t-r directory\tSandbox directory to chroot to\n"
107 #endif
108                 "\t-s level\tSyslog level\n"
109                 "\t-t\t\tCheck configuration file and exit\n"
110 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
111                 "\t-u user\t\tUser (id or name) to run as\n"
112 #endif
113     );
114 }
115
116 #ifdef HAVE_NT_SERVICE_MANAGER
117 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
118 #else
119 int main( int argc, char **argv )
120 #endif
121 {
122         int             i, no_detach = 0;
123         int             rc = 1;
124         char *urls = NULL;
125 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
126         char *username = NULL;
127         char *groupname = NULL;
128 #endif
129 #if defined(HAVE_CHROOT)
130         char *sandbox = NULL;
131 #endif
132 #ifdef LOG_LOCAL4
133     int     syslogUser = DEFAULT_SYSLOG_USER;
134 #endif
135         
136         int g_argc = argc;
137         char **g_argv = argv;
138
139 #ifdef HAVE_NT_SERVICE_MANAGER
140         char            *configfile = ".\\slapd.conf";
141 #else
142         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
143 #endif
144         char        *serverName = NULL;
145         int         serverMode = SLAP_SERVER_MODE;
146
147 #ifdef CSRIMALLOC
148         FILE *leakfile;
149         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
150                 leakfile = stderr;
151         }
152 #endif
153
154 #ifdef HAVE_NT_SERVICE_MANAGER
155         {
156                 int *i;
157                 char *newConfigFile;
158                 char *newUrls;
159                 char *regService = NULL;
160
161                 if ( is_NT_Service ) {
162                         serverName = argv[0];
163                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
164                         if ( strcmp(serverName, SERVICE_NAME) )
165                             regService = serverName;
166                 }
167
168                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
169                 if ( i != NULL ) 
170                 {
171                         slap_debug = *i;
172 #ifdef NEW_LOGGING
173                         lutil_log_initialize( argc, argv );
174                         LDAP_LOG( SLAPD, INFO, 
175                                 "main: new debug level from registry is: %d\n", 
176                                 slap_debug, 0, 0 );
177 #else
178                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
179 #endif
180                 }
181
182                 newUrls = (char *) lutil_getRegParam(regService, "Urls");
183                 if (newUrls)
184                 {
185                     if (urls)
186                         ch_free(urls);
187
188                     urls = ch_strdup(newUrls);
189 #ifdef NEW_LOGGING
190                     LDAP_LOG( SLAPD, INFO, 
191                                 "main: new urls from registry: %s\n", urls, 0, 0 );
192 #else
193                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
194                           urls, 0, 0);
195 #endif
196
197                 }
198
199                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
200                 if ( newConfigFile != NULL ) 
201                 {
202                         configfile = newConfigFile;
203 #ifdef NEW_LOGGING
204                         LDAP_LOG( SLAPD, INFO, 
205                                 "main: new config file from registry is: %s\n", configfile, 0, 0 );
206 #else
207                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
208 #endif
209
210                 }
211         }
212 #endif
213
214         while ( (i = getopt( argc, argv,
215                              "d:f:h:s:n:t"
216 #if LDAP_PF_INET6
217                                 "46"
218 #endif
219 #ifdef HAVE_CHROOT
220                                 "r:"
221 #endif
222 #ifdef LOG_LOCAL4
223                              "l:"
224 #endif
225 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
226                              "u:g:"
227 #endif
228                              )) != EOF ) {
229                 switch ( i ) {
230 #ifdef LDAP_PF_INET6
231                 case '4':
232                         slap_inet4or6 = AF_INET;
233                         break;
234                 case '6':
235                         slap_inet4or6 = AF_INET6;
236                         break;
237 #endif
238
239                 case 'h':       /* listen URLs */
240                         if ( urls != NULL ) free( urls );
241                         urls = ch_strdup( optarg );
242             break;
243
244                 case 'd':       /* set debug level and 'do not detach' flag */
245                         no_detach = 1;
246 #ifdef LDAP_DEBUG
247                         slap_debug |= atoi( optarg );
248 #else
249                         if ( atoi( optarg ) != 0 )
250                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
251                                        stderr );
252 #endif
253                         break;
254
255                 case 'f':       /* read config file */
256                         configfile = ch_strdup( optarg );
257                         break;
258
259                 case 's':       /* set syslog level */
260                         ldap_syslog = atoi( optarg );
261                         break;
262
263 #ifdef LOG_LOCAL4
264                 case 'l':       /* set syslog local user */
265                         syslogUser = cnvt_str2int( optarg,
266                                 syslog_types, DEFAULT_SYSLOG_USER );
267                         break;
268 #endif
269
270 #ifdef HAVE_CHROOT
271                 case 'r':
272                         if( sandbox ) free(sandbox);
273                         sandbox = ch_strdup( optarg );
274                         break;
275 #endif
276
277 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
278                 case 'u':       /* user name */
279                         if( username ) free(username);
280                         username = ch_strdup( optarg );
281                         break;
282
283                 case 'g':       /* group name */
284                         if( groupname ) free(groupname);
285                         groupname = ch_strdup( optarg );
286                         break;
287 #endif /* SETUID && GETUID */
288
289                 case 'n':  /* NT service name */
290                         if( serverName != NULL ) free( serverName );
291                         serverName = ch_strdup( optarg );
292                         break;
293
294                 case 't':
295                         check_config++;
296                         break;
297
298                 default:
299                         usage( argv[0] );
300                         rc = 1;
301                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
302                         goto stop;
303                 }
304         }
305
306 #ifdef NEW_LOGGING
307         lutil_log_initialize( argc, argv );
308 #else
309         lutil_set_debug_level( "slapd", slap_debug );
310         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
311         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
312         ldif_debug = slap_debug;
313 #endif
314
315 #ifdef NEW_LOGGING
316         LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
317 #else
318         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
319 #endif
320
321         if( serverName == NULL ) {
322                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
323                         serverName = argv[0];
324                 } else {
325                         serverName = serverName + 1;
326                 }
327         }
328
329 #ifdef LOG_LOCAL4
330         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
331 #elif LOG_DEBUG
332         openlog( serverName, OPENLOG_OPTIONS );
333 #endif
334
335         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
336                 rc = 1;
337                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
338                 goto stop;
339         }
340
341 #if defined(HAVE_CHROOT)
342         if ( sandbox ) {
343                 if ( chdir( sandbox ) ) {
344                         perror("chdir");
345                         rc = 1;
346                         goto stop;
347                 }
348                 if ( chroot( sandbox ) ) {
349                         perror("chroot");
350                         rc = 1;
351                         goto stop;
352                 }
353         }
354 #endif
355
356 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
357         if ( username != NULL || groupname != NULL ) {
358                 slap_init_user( username, groupname );
359         }
360 #endif
361
362         extops_init();
363         slap_op_init();
364         lutil_passwd_init();
365
366 #ifdef SLAPD_MODULES
367         if ( module_init() != 0 ) {
368                 rc = 1;
369                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
370                 goto destroy;
371         }
372 #endif
373
374         if ( slap_init( serverMode, serverName ) != 0 ) {
375                 rc = 1;
376                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
377                 goto destroy;
378         }
379
380         if ( slap_schema_init( ) != 0 ) {
381 #ifdef NEW_LOGGING
382                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
383 #else
384                 Debug( LDAP_DEBUG_ANY,
385                     "schema initialization error\n",
386                     0, 0, 0 );
387 #endif
388
389                 goto destroy;
390         }
391
392 #ifdef HAVE_TLS
393         /* Library defaults to full certificate checking. This is correct when
394          * a client is verifying a server because all servers should have a
395          * valid cert. But few clients have valid certs, so we want our default
396          * to be no checking. The config file can override this as usual.
397          */
398         rc = 0;
399         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
400 #endif
401
402 #ifdef LDAP_SLAPI
403         if ( slapi_init() != 0 ) {
404 #ifdef NEW_LOGGING
405                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
406 #else
407                 Debug( LDAP_DEBUG_ANY,
408                     "slapi initialization error\n",
409                     0, 0, 0 );
410 #endif
411
412                 goto destroy;
413         }
414 #endif /* LDAP_SLAPI */
415
416         if ( read_config( configfile, 0 ) != 0 ) {
417                 rc = 1;
418                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
419
420                 if ( check_config ) {
421                         fprintf( stderr, "config check failed\n" );
422                 }
423
424                 goto destroy;
425         }
426
427         if ( check_config ) {
428                 rc = 0;
429                 fprintf( stderr, "config check succeeded\n" );
430                 goto destroy;
431         }
432
433         if ( glue_sub_init( ) != 0 ) {
434 #ifdef NEW_LOGGING
435                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
436 #else
437                 Debug( LDAP_DEBUG_ANY,
438                     "subordinate config error\n",
439                     0, 0, 0 );
440 #endif
441                 goto destroy;
442         }
443
444         if ( slap_schema_check( ) != 0 ) {
445 #ifdef NEW_LOGGING
446                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
447 #else
448                 Debug( LDAP_DEBUG_ANY,
449                     "schema prep error\n",
450                     0, 0, 0 );
451 #endif
452
453                 goto destroy;
454         }
455
456 #ifdef HAVE_TLS
457         rc = ldap_pvt_tls_init();
458         if( rc != 0) {
459 #ifdef NEW_LOGGING
460                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
461 #else
462                 Debug( LDAP_DEBUG_ANY,
463                     "main: TLS init failed: %d\n",
464                     0, 0, 0 );
465 #endif
466                 rc = 1;
467                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
468                 goto destroy;
469         }
470
471         rc = ldap_pvt_tls_init_def_ctx();
472         if( rc != 0) {
473 #ifdef NEW_LOGGING
474                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
475 #else
476                 Debug( LDAP_DEBUG_ANY,
477                     "main: TLS init def ctx failed: %d\n",
478                     rc, 0, 0 );
479 #endif
480                 rc = 1;
481                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
482                 goto destroy;
483         }
484 #endif
485
486         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
487         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
488
489 #ifdef SIGPIPE
490         (void) SIGNAL( SIGPIPE, SIG_IGN );
491 #endif
492 #ifdef SIGHUP
493         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
494 #endif
495         (void) SIGNAL( SIGINT, slap_sig_shutdown );
496         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
497 #ifdef LDAP_SIGCHLD
498         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
499 #endif
500 #ifdef SIGBREAK
501         /* SIGBREAK is generated when Ctrl-Break is pressed. */
502         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
503 #endif
504
505 #ifndef HAVE_WINSOCK
506         lutil_detach( no_detach, 0 );
507 #endif /* HAVE_WINSOCK */
508
509 #ifdef CSRIMALLOC
510         mal_leaktrace(1);
511 #endif
512
513         if ( slap_startup( NULL )  != 0 ) {
514                 rc = 1;
515                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
516                 goto shutdown;
517         }
518
519 #ifdef NEW_LOGGING
520         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
521 #else
522         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
523 #endif
524
525
526         if ( slapd_pid_file != NULL ) {
527                 FILE *fp = fopen( slapd_pid_file, "w" );
528
529                 if( fp != NULL ) {
530                         fprintf( fp, "%d\n", (int) getpid() );
531                         fclose( fp );
532
533                 } else {
534                         free(slapd_pid_file);
535                         slapd_pid_file = NULL;
536                 }
537         }
538
539         if ( slapd_args_file != NULL ) {
540                 FILE *fp = fopen( slapd_args_file, "w" );
541
542                 if( fp != NULL ) {
543                         for ( i = 0; i < g_argc; i++ ) {
544                                 fprintf( fp, "%s ", g_argv[i] );
545                         }
546                         fprintf( fp, "\n" );
547                         fclose( fp );
548                 } else {
549                         free(slapd_args_file);
550                         slapd_args_file = NULL;
551                 }
552         }
553
554 #ifdef HAVE_NT_EVENT_LOG
555         if (is_NT_Service)
556         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
557 #endif
558
559         rc = slapd_daemon();
560
561 #ifdef HAVE_NT_SERVICE_MANAGER
562         /* Throw away the event that we used during the startup process. */
563         if ( is_NT_Service )
564                 ldap_pvt_thread_cond_destroy( &started_event );
565 #endif
566
567 shutdown:
568         /* remember an error during shutdown */
569         rc |= slap_shutdown( NULL );
570
571 destroy:
572         /* remember an error during destroy */
573         rc |= slap_destroy();
574
575 #ifdef SLAPD_MODULES
576         module_kill();
577 #endif
578
579         slap_op_destroy();
580
581         extops_kill();
582
583 stop:
584 #ifdef HAVE_NT_EVENT_LOG
585         if (is_NT_Service)
586         lutil_LogStoppedEvent( serverName );
587 #endif
588
589 #ifdef NEW_LOGGING
590         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
591 #else
592         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
593 #endif
594
595
596 #ifdef HAVE_NT_SERVICE_MANAGER
597         lutil_ReportShutdownComplete();
598 #endif
599
600 #ifdef LOG_DEBUG
601     closelog();
602 #endif
603         slapd_daemon_destroy();
604
605         schema_destroy();
606
607         lutil_passwd_destroy();
608
609 #ifdef HAVE_TLS
610         ldap_pvt_tls_destroy();
611 #endif
612
613         if ( slapd_pid_file != NULL ) {
614                 unlink( slapd_pid_file );
615         }
616         if ( slapd_args_file != NULL ) {
617                 unlink( slapd_args_file );
618         }
619
620         config_destroy();
621
622 #ifdef CSRIMALLOC
623         mal_dumpleaktrace( leakfile );
624 #endif
625
626         MAIN_RETURN(rc);
627 }
628
629
630 #ifdef LDAP_SIGCHLD
631
632 /*
633  *  Catch and discard terminated child processes, to avoid zombies.
634  */
635
636 static RETSIGTYPE
637 wait4child( int sig )
638 {
639     int save_errno = errno;
640
641 #ifdef WNOHANG
642     errno = 0;
643 #ifdef HAVE_WAITPID
644     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
645         ;       /* NULL */
646 #else
647     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
648         ;       /* NULL */
649 #endif
650 #else
651     (void) wait( NULL );
652 #endif
653     (void) SIGNAL_REINSTALL( sig, wait4child );
654     errno = save_errno;
655 }
656
657 #endif /* LDAP_SIGCHLD */
658
659
660 #ifdef LOG_LOCAL4
661
662 /*
663  *  Convert a string to an integer by means of a dispatcher table
664  *  if the string is not in the table return the default
665  */
666
667 static int
668 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
669 {
670     int        retVal = defaultVal;
671     STRDISP_P  disp;
672
673     for (disp = dispatcher; disp->stringVal; disp++) {
674
675         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
676
677             retVal = disp->intVal;
678             break;
679
680         }
681     }
682
683     return (retVal);
684 }
685
686 #endif  /* LOG_LOCAL4 */