]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
8a42a012eab5362c3c1e2e8300cb42b875fd3dff
[openldap] / servers / slapd / main.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/signal.h>
6 #include <ac/socket.h>
7 #include <ac/string.h>
8 #include <ac/time.h>
9 #include <ac/unistd.h>
10 #include <ac/wait.h>
11 #include <ac/signal.h>
12 #include <ac/errno.h>
13
14 #include "ldap_defaults.h"
15 #include "slap.h"
16 #ifndef HAVE_WINSOCK
17 #include "lutil.h"                      /* Get lutil_detach() */
18 #endif
19
20 #ifdef LDAP_SIGCHLD
21 static RETSIGTYPE wait4child( int sig );
22 #endif
23
24 #ifdef HAVE_WINSOCK
25 #define SERVICE_NAME "OpenLDAP"
26
27 struct sockaddr_in      bind_addr;
28
29 // in nt_main.c
30 extern SERVICE_STATUS                   SLAPDServiceStatus;
31 extern SERVICE_STATUS_HANDLE    hSLAPDServiceStatus;
32 extern ldap_pvt_thread_cond_t   started_event,          stopped_event;
33 extern int        is_NT_Service;
34
35 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, short port, int udp );
36 void LogSlapdStoppedEvent( char *svc );
37
38 void CommenceStartupProcessing( LPCTSTR serviceName,
39                                                            void(*stopper)(int));
40 void ReportSlapdShutdownComplete( void );
41 void *getRegParam( char *svc, char *value );
42
43 #define SERVICE_EXIT( e, n ) \
44                 if ( is_NT_Service ) \
45 { \
46                         SLAPDServiceStatus.dwWin32ExitCode                              = e; \
47                         SLAPDServiceStatus.dwServiceSpecificExitCode    = n; \
48
49 #else
50 #define SERVICE_EXIT( e, n )
51 #endif
52
53 short port = LDAP_PORT;
54 /*
55  * when more than one slapd is running on one machine, each one might have
56  * it's own LOCAL for syslogging and must have its own pid/args files
57  */
58
59 #ifndef HAVE_MKVERSION
60 const char Versionstr[] =
61         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
62 #endif
63
64 #ifdef LOG_LOCAL4
65
66 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
67
68 typedef struct _str2intDispatch {
69         char    *stringVal;
70         int      abbr;
71         int      intVal;
72 } STRDISP, *STRDISP_P;
73
74
75 /* table to compute syslog-options to integer */
76 static STRDISP  syslog_types[] = {
77     { "LOCAL0",         6, LOG_LOCAL0 },
78     { "LOCAL1",         6, LOG_LOCAL1 },
79     { "LOCAL2",         6, LOG_LOCAL2 },
80     { "LOCAL3",         6, LOG_LOCAL3 },
81     { "LOCAL4",         6, LOG_LOCAL4 },
82     { "LOCAL5",         6, LOG_LOCAL5 },
83     { "LOCAL6",         6, LOG_LOCAL6 },
84     { "LOCAL7",         6, LOG_LOCAL7 },
85     { NULL }
86 };
87
88 static int   cnvt_str2int( char *, STRDISP_P, int );
89
90 #endif  /* LOG_LOCAL4 */
91
92
93 static void
94 usage( char *name )
95 {
96         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
97     fprintf( stderr, "\n        [-a bind-address] [-i] [-u]" );
98 #if LDAP_CONNECTIONLESS
99         fprintf( stderr, " [-c]" );
100 #endif
101 #ifdef SLAPD_BDB2
102     fprintf( stderr, " [-t]" );
103 #endif
104 #ifdef LOG_LOCAL4
105     fprintf( stderr, " [-l sysloguser]" );
106 #endif
107 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
108     fprintf( stderr, " [-u user] [-g group]" );
109 #endif
110     fprintf( stderr, "\n" );
111 }
112
113 time_t starttime;
114 struct sockaddr_in      bind_addr;
115 int tcps;
116
117 #ifdef HAVE_WINSOCK
118 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
119 {
120 #else
121 int main( int argc, char **argv )
122 {
123 #endif
124
125         int             i;
126         int             inetd = 0;
127         int             rc;
128         struct slapd_args args;
129         int             udp;
130 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
131         char *username = NULL;
132         char *groupname = NULL;
133 #endif
134 #ifdef LOG_LOCAL4
135     int     syslogUser = DEFAULT_SYSLOG_USER;
136 #endif
137 #ifdef HAVE_WINSOCK
138         char            *configfile = ".\\slapd.conf";
139 #else
140         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
141 #endif
142         char        *serverName;
143         int         serverMode = SLAP_SERVER_MODE;
144
145         (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
146         bind_addr.sin_family = AF_INET;
147         bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
148         bind_addr.sin_port = htons(port);
149
150         g_argc = argc;
151         g_argv = argv;
152
153 #ifdef HAVE_WINSOCK
154         //if ( is_NT_Service )
155         {
156                 int *newPort;
157                 int *newDebugLevel;
158                 char *newConfigFile;
159                 ldap_debug = 0xffff;
160                 if ( is_NT_Service ) CommenceStartupProcessing( SERVICE_NAME, slap_set_shutdown );
161                 newPort = (int*)getRegParam( NULL, "Port" );
162                 if ( newPort != NULL )
163                 {
164                         port = *newPort;
165                         bind_addr.sin_port = htons(port);
166                         Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
167                 }
168                 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
169                 if ( newDebugLevel != NULL ) 
170                 {
171                         slap_debug = *newDebugLevel;
172                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
173                 }
174                 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
175                 if ( newConfigFile != NULL ) 
176                 {
177                         configfile = newConfigFile;
178                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
179                 }
180         }
181                 
182 #endif
183
184         while ( (i = getopt( argc, argv,
185                              "d:f:ia:p:s:u"
186 #ifdef LOG_LOCAL4
187                              "l:"
188 #endif
189 #ifdef SLAPD_BDB2
190                              "t"
191 #endif
192 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
193                              "u:g:"
194 #endif
195 #ifdef LDAP_CONNECTIONLESS
196                                  "c"
197 #endif
198                              )) != EOF ) {
199                 switch ( i ) {
200                 case 'a':       /* bind address */
201 #ifdef HAVE_WINSOCK
202                         if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
203 #else
204                         if(!inet_aton(optarg, &bind_addr.sin_addr))
205 #endif
206                         {
207                                 fprintf(stderr, "invalid address (%s) for -a option", optarg);
208                         }
209             break;
210
211 #ifdef LDAP_DEBUG
212                 case 'd':       /* turn on debugging */
213                         if ( optarg[0] == '?' ) {
214                                 printf( "Debug levels:\n" );
215                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
216                                     LDAP_DEBUG_TRACE );
217                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
218                                     LDAP_DEBUG_PACKETS );
219                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
220                                     LDAP_DEBUG_ARGS );
221                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
222                                     LDAP_DEBUG_CONNS );
223                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
224                                     LDAP_DEBUG_BER );
225                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
226                                     LDAP_DEBUG_FILTER );
227                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
228                                     LDAP_DEBUG_CONFIG );
229                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
230                                     LDAP_DEBUG_ACL );
231                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
232                                     LDAP_DEBUG_STATS );
233                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
234                                     LDAP_DEBUG_STATS2 );
235                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
236                                     LDAP_DEBUG_SHELL );
237                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
238                                     LDAP_DEBUG_PARSE );
239                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
240                                     LDAP_DEBUG_ANY );
241                                 exit( 0 );
242                         } else {
243                                 slap_debug |= atoi( optarg );
244                         }
245                         break;
246 #else
247                 case 'd':       /* turn on debugging */
248                         fprintf( stderr,
249                             "must compile with LDAP_DEBUG for debugging\n" );
250                         break;
251 #endif
252
253                 case 'f':       /* read config file */
254                         configfile = ch_strdup( optarg );
255                         break;
256
257                 case 'i':       /* run from inetd */
258                         inetd = 1;
259                         break;
260
261                 case 'p': {     /* port on which to listen */
262                                 port = (short)atoi( optarg );
263                                 if(! port ) {
264                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
265                                 } else {
266                                         bind_addr.sin_port = htons(port);
267                                 }
268                         } break;
269
270                 case 's':       /* set syslog level */
271                         ldap_syslog = atoi( optarg );
272                         break;
273
274 #ifdef LOG_LOCAL4
275                 case 'l':       /* set syslog local user */
276                         syslogUser = cnvt_str2int( optarg, syslog_types,
277                                            DEFAULT_SYSLOG_USER );
278                         break;
279 #endif
280
281 #ifdef LDAP_CONNECTIONLESS
282                 case 'c':       /* do connectionless (udp) */
283                         udp = 1;
284                         break;
285 #endif
286
287 #ifdef SLAPD_BDB2
288                 case 't':  /* timed server */
289                         serverMode = SLAP_TIMEDSERVER_MODE;
290                         break;
291 #endif
292
293 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
294                 case 'u':       /* user name */
295                         if( username ) free(username);
296                         username = ch_strdup( optarg );
297                         break;
298
299                 case 'g':       /* group name */
300                         if( groupname ) free(groupname);
301                         groupname = ch_strdup( optarg );
302                         break;
303 #endif /* SETUID && GETUID */
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         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
314         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
315         ldif_debug = slap_debug;
316
317         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
318
319         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
320                 serverName = ch_strdup( argv[0] );
321         } else {
322                 serverName = ch_strdup( serverName + 1 );
323         }
324
325 #ifdef LOG_LOCAL4
326         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
327 #else
328         openlog( serverName, OPENLOG_OPTIONS );
329 #endif
330
331 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
332         if ( username != NULL || groupname != NULL )
333                 slap_init_user( username, groupname );
334 #endif
335
336         if ( slap_init( serverMode, serverName ) != 0 ) {
337                 rc = 1;
338                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
339                 goto destroy;
340         }
341
342         if ( read_config( configfile ) != 0 ) {
343                 rc = 1;
344                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
345                 goto destroy;
346         }
347
348
349         tcps = set_socket( inetd ? NULL : &bind_addr );
350
351         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
352         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
353 #ifdef SIGPIPE
354         (void) SIGNAL( SIGPIPE, SIG_IGN );
355 #endif
356 #ifdef SIGHUP
357         (void) SIGNAL( SIGHUP, slap_set_shutdown );
358 #endif
359         (void) SIGNAL( SIGINT, slap_set_shutdown );
360         (void) SIGNAL( SIGTERM, slap_set_shutdown );
361 #ifdef LDAP_SIGCHLD
362         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
363 #endif
364 #ifdef HAVE_WINSOCK
365         // SIGBREAK is generated when Ctrl-Break is pressed.
366         (void) SIGNAL( SIGBREAK, slap_set_shutdown );
367 #endif
368
369 #ifndef HAVE_WINSOCK
370         if(!inetd) {
371 #ifdef LDAP_DEBUG
372                 lutil_detach( ldap_debug, 0 );
373 #else
374                 lutil_detach( 0, 0 );
375 #endif
376         }
377 #endif /* HAVE_WINSOC */
378
379         if ( slap_startup(-1)  != 0 ) {
380                 rc = 1;
381                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
382                 goto shutdown;
383         }
384
385         if(!inetd) {
386                 FILE *fp;
387
388                 args.addr = &bind_addr;
389
390                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
391
392                 if (( slapd_pid_file != NULL ) &&
393                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
394                 {
395                         fprintf( fp, "%d\n", (int) getpid() );
396                         fclose( fp );
397                 }
398
399                 if (( slapd_args_file != NULL ) &&
400                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
401                 {
402                         for ( i = 0; i < g_argc; i++ ) {
403                                 fprintf( fp, "%s ", g_argv[i] );
404                         }
405                         fprintf( fp, "\n" );
406                         fclose( fp );
407                 }
408
409         } else {
410                 args.addr = NULL;
411         }
412         args.tcps = tcps;
413
414         time( &starttime );
415 #ifdef HAVE_WINSOCK
416         LogSlapdStartedEvent( SERVICE_NAME, slap_debug, configfile, port, udp );
417 #endif
418
419         rc = slapd_daemon( &args );
420
421 #ifdef HAVE_WINSOCK
422         // Throw away the event that we used during the startup process.
423         if ( is_NT_Service )
424                 ldap_pvt_thread_cond_destroy( &started_event );
425 #endif
426
427
428 shutdown:
429         /* remember an error during shutdown */
430         rc |= slap_shutdown(-1);
431 destroy:
432         /* remember an error during destroy */
433         rc |= slap_destroy();
434
435 stop:
436 #ifdef HAVE_WINSOCK
437         LogSlapdStoppedEvent( SERVICE_NAME );
438 #endif
439         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
440 #ifdef HAVE_WINSOCK
441         ReportSlapdShutdownComplete();
442 #endif
443
444     closelog();
445
446         return rc;
447 }
448
449
450 #ifdef LDAP_SIGCHLD
451
452 /*
453  *  Catch and discard terminated child processes, to avoid zombies.
454  */
455
456 static RETSIGTYPE
457 wait4child( int sig )
458 {
459     int save_errno = errno;
460
461 #ifdef WNOHANG
462     errno = 0;
463 #ifdef HAVE_WAITPID
464     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
465         ;       /* NULL */
466 #else
467     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
468         ;       /* NULL */
469 #endif
470 #else
471     (void) wait( NULL );
472 #endif
473     (void) SIGNAL( sig, wait4child );
474     errno = save_errno;
475 }
476
477 #endif /* SIGCHLD || SIGCLD */
478
479
480 #ifdef LOG_LOCAL4
481
482 /*
483  *  Convert a string to an integer by means of a dispatcher table
484  *  if the string is not in the table return the default
485  */
486
487 static int
488 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
489 {
490     int        retVal = defaultVal;
491     STRDISP_P  disp;
492
493     for (disp = dispatcher; disp->stringVal; disp++) {
494
495         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
496
497             retVal = disp->intVal;
498             break;
499
500         }
501     }
502
503     return (retVal);
504
505 } /* cnvt_str2int */
506
507 #endif  /* LOG_LOCAL4 */