]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Fix matched values bug
[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                         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: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 'd':       /* set debug level and 'do not detach' flag */
250                         no_detach = 1;
251 #ifdef LDAP_DEBUG
252                         slap_debug |= atoi( optarg );
253 #else
254                         if ( atoi( optarg ) != 0 )
255                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
256                                        stderr );
257 #endif
258                         break;
259
260                 case 'f':       /* read config file */
261                         configfile = ch_strdup( optarg );
262                         break;
263
264                 case 's':       /* set syslog level */
265                         ldap_syslog = atoi( optarg );
266                         break;
267
268 #ifdef LOG_LOCAL4
269                 case 'l':       /* set syslog local user */
270                         syslogUser = cnvt_str2int( optarg,
271                                 syslog_types, DEFAULT_SYSLOG_USER );
272                         break;
273 #endif
274
275 #ifdef HAVE_CHROOT
276                 case 'r':
277                         if( sandbox ) free(sandbox);
278                         sandbox = ch_strdup( optarg );
279                         break;
280 #endif
281
282 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
283                 case 'u':       /* user name */
284                         if( username ) free(username);
285                         username = ch_strdup( optarg );
286                         break;
287
288                 case 'g':       /* group name */
289                         if( groupname ) free(groupname);
290                         groupname = ch_strdup( optarg );
291                         break;
292 #endif /* SETUID && GETUID */
293
294                 case 'n':  /* NT service name */
295                         if( serverName != NULL ) free( serverName );
296                         serverName = ch_strdup( optarg );
297                         break;
298
299                 case 't':
300                         check_config++;
301                         break;
302
303                 default:
304                         usage( argv[0] );
305                         rc = 1;
306                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
307                         goto stop;
308                 }
309         }
310
311 #ifdef NEW_LOGGING
312         lutil_log_initialize( argc, argv );
313 #endif
314
315         lutil_set_debug_level( "slapd", slap_debug );
316         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
317         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
318         ldif_debug = slap_debug;
319
320 #ifdef NEW_LOGGING
321         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
322                    "%s", Versionstr ));
323 #else
324         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
325 #endif
326
327
328         if( serverName == NULL ) {
329                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
330                         serverName = argv[0];
331                 } else {
332                         serverName = serverName + 1;
333                 }
334         }
335
336 #ifdef LOG_LOCAL4
337         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
338 #elif LOG_DEBUG
339         openlog( serverName, OPENLOG_OPTIONS );
340 #endif
341
342         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
343                 rc = 1;
344                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
345                 goto stop;
346         }
347
348 #if defined(HAVE_CHROOT)
349         if ( sandbox ) {
350                 if ( chdir( sandbox ) ) {
351                         perror("chdir");
352                         rc = 1;
353                         goto stop;
354                 }
355                 if ( chroot( sandbox ) ) {
356                         perror("chroot");
357                         rc = 1;
358                         goto stop;
359                 }
360         }
361 #endif
362
363 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
364         if ( username != NULL || groupname != NULL ) {
365                 slap_init_user( username, groupname );
366         }
367 #endif
368
369         extops_init();
370
371 #ifdef SLAPD_MODULES
372         if ( module_init() != 0 ) {
373                 rc = 1;
374                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
375                 goto destroy;
376         }
377 #endif
378
379         if ( slap_init( serverMode, serverName ) != 0 ) {
380                 rc = 1;
381                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
382                 goto destroy;
383         }
384
385         if ( slap_schema_init( ) != 0 ) {
386 #ifdef NEW_LOGGING
387                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
388                            "main: schema initialization error\n" ));
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(( "operation", LDAP_LEVEL_CRIT,
428                            "main: subordinate config error\n"));
429 #else
430                 Debug( LDAP_DEBUG_ANY,
431                     "subordinate config error\n",
432                     0, 0, 0 );
433 #endif
434                 goto destroy;
435         }
436
437         if ( slap_schema_check( ) != 0 ) {
438 #ifdef NEW_LOGGING
439                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
440                            "main: schema prep error\n"));
441 #else
442                 Debug( LDAP_DEBUG_ANY,
443                     "schema prep error\n",
444                     0, 0, 0 );
445 #endif
446
447                 goto destroy;
448         }
449
450 #ifdef HAVE_TLS
451         rc = ldap_pvt_tls_init();
452         if( rc != 0) {
453 #ifdef NEW_LOGGING
454                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
455                            "main: tls init failed: %d\n", rc ));
456 #else
457                 Debug( LDAP_DEBUG_ANY,
458                     "main: TLS init failed: %d\n",
459                     0, 0, 0 );
460 #endif
461                 rc = 1;
462                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
463                 goto destroy;
464         }
465
466         rc = ldap_pvt_tls_init_def_ctx();
467         if( rc != 0) {
468 #ifdef NEW_LOGGING
469                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
470                            "main: tls init def ctx failed: %d\n", rc ));
471 #else
472                 Debug( LDAP_DEBUG_ANY,
473                     "main: TLS init def ctx failed: %d\n",
474                     0, 0, 0 );
475 #endif
476                 rc = 1;
477                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
478                 goto destroy;
479         }
480 #endif
481
482         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
483         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
484
485 #ifdef SIGPIPE
486         (void) SIGNAL( SIGPIPE, SIG_IGN );
487 #endif
488 #ifdef SIGHUP
489         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
490 #endif
491         (void) SIGNAL( SIGINT, slap_sig_shutdown );
492         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
493 #ifdef LDAP_SIGCHLD
494         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
495 #endif
496 #ifdef SIGBREAK
497         /* SIGBREAK is generated when Ctrl-Break is pressed. */
498         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
499 #endif
500
501 #ifndef HAVE_WINSOCK
502         lutil_detach( no_detach, 0 );
503 #endif /* HAVE_WINSOCK */
504
505 #ifdef CSRIMALLOC
506         mal_leaktrace(1);
507 #endif
508
509         if ( slap_startup( NULL )  != 0 ) {
510                 rc = 1;
511                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
512                 goto shutdown;
513         }
514
515         {
516                 FILE *fp;
517
518 #ifdef NEW_LOGGING
519                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
520                            "main: slapd starting.\n" ));
521 #else
522                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
523 #endif
524
525
526                 if (( slapd_pid_file != NULL ) &&
527                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
528                 {
529                         fprintf( fp, "%d\n", (int) getpid() );
530                         fclose( fp );
531                 }
532
533                 if (( slapd_args_file != NULL ) &&
534                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
535                 {
536                         for ( i = 0; i < g_argc; i++ ) {
537                                 fprintf( fp, "%s ", g_argv[i] );
538                         }
539                         fprintf( fp, "\n" );
540                         fclose( fp );
541                 }
542         }
543
544 #ifdef HAVE_NT_EVENT_LOG
545         if (is_NT_Service)
546         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
547 #endif
548
549         rc = slapd_daemon();
550
551 #ifdef HAVE_NT_SERVICE_MANAGER
552         /* Throw away the event that we used during the startup process. */
553         if ( is_NT_Service )
554                 ldap_pvt_thread_cond_destroy( &started_event );
555 #endif
556
557 shutdown:
558         /* remember an error during shutdown */
559         rc |= slap_shutdown( NULL );
560
561 destroy:
562         /* remember an error during destroy */
563         rc |= slap_destroy();
564
565 #ifdef SLAPD_MODULES
566         module_kill();
567 #endif
568
569         extops_kill();
570
571 stop:
572 #ifdef HAVE_NT_EVENT_LOG
573         if (is_NT_Service)
574         LogSlapdStoppedEvent( serverName );
575 #endif
576
577 #ifdef NEW_LOGGING
578         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
579                    "main: slapd stopped.\n" ));
580 #else
581         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
582 #endif
583
584
585 #ifdef HAVE_NT_SERVICE_MANAGER
586         ReportSlapdShutdownComplete();
587 #endif
588
589 #ifdef LOG_DEBUG
590     closelog();
591 #endif
592         slapd_daemon_destroy();
593
594         schema_destroy();
595
596 #ifdef HAVE_TLS
597         ldap_pvt_tls_destroy();
598 #endif
599
600         config_destroy();
601
602 #ifdef CSRIMALLOC
603         mal_dumpleaktrace( leakfile );
604 #endif
605
606         MAIN_RETURN(rc);
607 }
608
609
610 #ifdef LDAP_SIGCHLD
611
612 /*
613  *  Catch and discard terminated child processes, to avoid zombies.
614  */
615
616 static RETSIGTYPE
617 wait4child( int sig )
618 {
619     int save_errno = errno;
620
621 #ifdef WNOHANG
622     errno = 0;
623 #ifdef HAVE_WAITPID
624     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
625         ;       /* NULL */
626 #else
627     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
628         ;       /* NULL */
629 #endif
630 #else
631     (void) wait( NULL );
632 #endif
633     (void) SIGNAL_REINSTALL( sig, wait4child );
634     errno = save_errno;
635 }
636
637 #endif /* SIGCHLD || SIGCLD */
638
639
640 #ifdef LOG_LOCAL4
641
642 /*
643  *  Convert a string to an integer by means of a dispatcher table
644  *  if the string is not in the table return the default
645  */
646
647 static int
648 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
649 {
650     int        retVal = defaultVal;
651     STRDISP_P  disp;
652
653     for (disp = dispatcher; disp->stringVal; disp++) {
654
655         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
656
657             retVal = disp->intVal;
658             break;
659
660         }
661     }
662
663     return (retVal);
664 }
665
666 #endif  /* LOG_LOCAL4 */