]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
actually, get_perms is needed by ldapi:// rgeardless of -DSLAP_X_LISTENER_MOD
[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 LDAP_CLIENT_UPDATE
399         build_uuid_attr();
400 #endif
401
402 #ifdef HAVE_TLS
403         /* Library defaults to full certificate checking. This is correct when
404          * a client is verifying a server because all servers should have a
405          * valid cert. But few clients have valid certs, so we want our default
406          * to be no checking. The config file can override this as usual.
407          */
408         rc = 0;
409         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
410 #endif
411
412         if ( read_config( configfile, 0 ) != 0 ) {
413                 rc = 1;
414                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
415
416                 if ( check_config ) {
417                         fprintf( stderr, "config check failed\n" );
418                 }
419
420                 goto destroy;
421         }
422
423         if ( check_config ) {
424                 rc = 0;
425                 fprintf( stderr, "config check succeeded\n" );
426                 goto destroy;
427         }
428
429         if ( glue_sub_init( ) != 0 ) {
430 #ifdef NEW_LOGGING
431                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
432 #else
433                 Debug( LDAP_DEBUG_ANY,
434                     "subordinate config error\n",
435                     0, 0, 0 );
436 #endif
437                 goto destroy;
438         }
439
440         if ( slap_schema_check( ) != 0 ) {
441 #ifdef NEW_LOGGING
442                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
443 #else
444                 Debug( LDAP_DEBUG_ANY,
445                     "schema prep error\n",
446                     0, 0, 0 );
447 #endif
448
449                 goto destroy;
450         }
451
452 #ifdef HAVE_TLS
453         rc = ldap_pvt_tls_init();
454         if( rc != 0) {
455 #ifdef NEW_LOGGING
456                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
457 #else
458                 Debug( LDAP_DEBUG_ANY,
459                     "main: TLS init failed: %d\n",
460                     0, 0, 0 );
461 #endif
462                 rc = 1;
463                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
464                 goto destroy;
465         }
466
467         rc = ldap_pvt_tls_init_def_ctx();
468         if( rc != 0) {
469 #ifdef NEW_LOGGING
470                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
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( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
520 #else
521                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
522 #endif
523
524
525                 if (( slapd_pid_file != NULL ) &&
526                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
527                 {
528                         fprintf( fp, "%d\n", (int) getpid() );
529                         fclose( fp );
530                 }
531
532                 if (( slapd_args_file != NULL ) &&
533                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
534                 {
535                         for ( i = 0; i < g_argc; i++ ) {
536                                 fprintf( fp, "%s ", g_argv[i] );
537                         }
538                         fprintf( fp, "\n" );
539                         fclose( fp );
540                 }
541         }
542
543 #ifdef HAVE_NT_EVENT_LOG
544         if (is_NT_Service)
545         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
546 #endif
547
548         rc = slapd_daemon();
549
550 #ifdef HAVE_NT_SERVICE_MANAGER
551         /* Throw away the event that we used during the startup process. */
552         if ( is_NT_Service )
553                 ldap_pvt_thread_cond_destroy( &started_event );
554 #endif
555
556 shutdown:
557         /* remember an error during shutdown */
558         rc |= slap_shutdown( NULL );
559
560 destroy:
561         /* remember an error during destroy */
562         rc |= slap_destroy();
563
564 #ifdef SLAPD_MODULES
565         module_kill();
566 #endif
567
568         extops_kill();
569
570 stop:
571 #ifdef HAVE_NT_EVENT_LOG
572         if (is_NT_Service)
573         LogSlapdStoppedEvent( serverName );
574 #endif
575
576 #ifdef NEW_LOGGING
577         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
578 #else
579         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
580 #endif
581
582
583 #ifdef HAVE_NT_SERVICE_MANAGER
584         ReportSlapdShutdownComplete();
585 #endif
586
587 #ifdef LOG_DEBUG
588     closelog();
589 #endif
590         slapd_daemon_destroy();
591
592         schema_destroy();
593
594 #ifdef HAVE_TLS
595         ldap_pvt_tls_destroy();
596 #endif
597
598         config_destroy();
599
600 #ifdef CSRIMALLOC
601         mal_dumpleaktrace( leakfile );
602 #endif
603
604         MAIN_RETURN(rc);
605 }
606
607
608 #ifdef LDAP_SIGCHLD
609
610 /*
611  *  Catch and discard terminated child processes, to avoid zombies.
612  */
613
614 static RETSIGTYPE
615 wait4child( int sig )
616 {
617     int save_errno = errno;
618
619 #ifdef WNOHANG
620     errno = 0;
621 #ifdef HAVE_WAITPID
622     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
623         ;       /* NULL */
624 #else
625     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
626         ;       /* NULL */
627 #endif
628 #else
629     (void) wait( NULL );
630 #endif
631     (void) SIGNAL_REINSTALL( sig, wait4child );
632     errno = save_errno;
633 }
634
635 #endif /* LDAP_SIGCHLD */
636
637
638 #ifdef LOG_LOCAL4
639
640 /*
641  *  Convert a string to an integer by means of a dispatcher table
642  *  if the string is not in the table return the default
643  */
644
645 static int
646 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
647 {
648     int        retVal = defaultVal;
649     STRDISP_P  disp;
650
651     for (disp = dispatcher; disp->stringVal; disp++) {
652
653         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
654
655             retVal = disp->intVal;
656             break;
657
658         }
659     }
660
661     return (retVal);
662 }
663
664 #endif  /* LOG_LOCAL4 */