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