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