]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
c999033db32c78672839fa7044fa4f9b5a4e1cd2
[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 "ldapconfig.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 void main( int argc, char **argv )
122 {
123 #endif
124
125         int             i;
126         int             inetd = 0;
127         int             rc;
128         struct sockaddr_in      *slapd_addr;
129         int             udp;
130 #ifdef LOG_LOCAL4
131     int     syslogUser = DEFAULT_SYSLOG_USER;
132 #endif
133 #ifdef HAVE_WINSOCK
134         char            *configfile = ".\\slapd.conf";
135 #else
136         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
137 #endif
138         char        *serverName;
139         int         serverMode = SLAP_SERVER_MODE;
140
141         (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
142         bind_addr.sin_family = AF_INET;
143         bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
144         bind_addr.sin_port = htons(port);
145
146         g_argc = argc;
147         g_argv = argv;
148
149 #ifdef HAVE_WINSOCK
150         //if ( is_NT_Service )
151         {
152                 int *newPort;
153                 int *newDebugLevel;
154                 char *newConfigFile;
155                 ldap_debug = 0xffff;
156                 if ( is_NT_Service ) CommenceStartupProcessing( SERVICE_NAME, slap_set_shutdown );
157                 newPort = (int*)getRegParam( NULL, "Port" );
158                 if ( newPort != NULL )
159                 {
160                         port = *newPort;
161                         bind_addr.sin_port = htons(port);
162                         Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
163                 }
164                 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
165                 if ( newDebugLevel != NULL ) 
166                 {
167                         slap_debug = *newDebugLevel;
168                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
169                 }
170                 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
171                 if ( newConfigFile != NULL ) 
172                 {
173                         configfile = newConfigFile;
174                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
175                 }
176         }
177                 
178 #endif
179
180         while ( (i = getopt( argc, argv,
181                              "d:f:ia:p:s:u"
182 #ifdef LOG_LOCAL4
183                              "l:"
184 #endif
185 #ifdef SLAPD_BDB2
186                              "t"
187 #endif
188 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
189                              "u:g:"
190 #endif
191 #ifdef LDAP_CONNECTIONLESS
192                                  "c"
193 #endif
194                              )) != EOF ) {
195                 switch ( i ) {
196                 case 'a':       /* bind address */
197 #ifdef HAVE_WINSOCK
198                         if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
199 #else
200                         if(!inet_aton(optarg, &bind_addr.sin_addr))
201 #endif
202                         {
203                                 fprintf(stderr, "invalid address (%s) for -a option", optarg);
204                         }
205             break;
206
207 #ifdef LDAP_DEBUG
208                 case 'd':       /* turn on debugging */
209                         if ( optarg[0] == '?' ) {
210                                 printf( "Debug levels:\n" );
211                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
212                                     LDAP_DEBUG_TRACE );
213                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
214                                     LDAP_DEBUG_PACKETS );
215                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
216                                     LDAP_DEBUG_ARGS );
217                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
218                                     LDAP_DEBUG_CONNS );
219                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
220                                     LDAP_DEBUG_BER );
221                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
222                                     LDAP_DEBUG_FILTER );
223                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
224                                     LDAP_DEBUG_CONFIG );
225                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
226                                     LDAP_DEBUG_ACL );
227                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
228                                     LDAP_DEBUG_STATS );
229                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
230                                     LDAP_DEBUG_STATS2 );
231                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
232                                     LDAP_DEBUG_SHELL );
233                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
234                                     LDAP_DEBUG_PARSE );
235                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
236                                     LDAP_DEBUG_ANY );
237                                 exit( 0 );
238                         } else {
239                                 slap_debug |= atoi( optarg );
240                         }
241                         break;
242 #else
243                 case 'd':       /* turn on debugging */
244                         fprintf( stderr,
245                             "must compile with LDAP_DEBUG for debugging\n" );
246                         break;
247 #endif
248
249                 case 'f':       /* read config file */
250                         configfile = ch_strdup( optarg );
251                         break;
252
253                 case 'i':       /* run from inetd */
254                         inetd = 1;
255                         break;
256
257                 case 'p': {     /* port on which to listen */
258                                 port = (short)atoi( optarg );
259                                 if(! port ) {
260                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
261                                 } else {
262                                         bind_addr.sin_port = htons(port);
263                                 }
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, syslog_types,
273                                            DEFAULT_SYSLOG_USER );
274                         break;
275 #endif
276
277 #ifdef LDAP_CONNECTIONLESS
278                 case 'c':       /* do connectionless (udp) */
279                         udp = 1;
280                         break;
281 #endif
282
283 #ifdef SLAPD_BDB2
284                 case 't':  /* timed server */
285                         serverMode = SLAP_TIMEDSERVER_MODE;
286                         break;
287 #endif
288
289 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
290                 case 'u':       /* user name */
291                         if( username ) free(username);
292                         username = ch_strdup( optarg );
293                         break;
294
295                 case 'g':       /* group name */
296                         if( groupname ) free(groupname);
297                         groupname = ch_strdup( optarg );
298                         break;
299 #endif /* SETUID && GETUID */
300
301                 default:
302                         usage( argv[0] );
303                         rc = 1;
304                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
305                         goto stop;
306                 }
307         }
308
309         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
310         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
311         ldif_debug = slap_debug;
312
313         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
314
315         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
316                 serverName = ch_strdup( argv[0] );
317         } else {
318                 serverName = ch_strdup( serverName + 1 );
319         }
320
321 #ifdef LOG_LOCAL4
322         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
323 #else
324         openlog( serverName, OPENLOG_OPTIONS );
325 #endif
326
327 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
328         if ( username != NULL || groupname != NULL )
329                 slap_init_user( username, groupname );
330 #endif
331
332         if ( slap_init( serverMode, serverName ) != 0 ) {
333                 rc = 1;
334                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
335                 goto destroy;
336         }
337
338         if ( read_config( configfile ) != 0 ) {
339                 rc = 1;
340                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
341                 goto destroy;
342         }
343
344
345         tcps = set_socket( inetd ? NULL : &bind_addr );
346
347         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
348         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
349 #ifdef SIGPIPE
350         (void) SIGNAL( SIGPIPE, SIG_IGN );
351 #endif
352 #ifdef SIGHUP
353         (void) SIGNAL( SIGHUP, slap_set_shutdown );
354 #endif
355         (void) SIGNAL( SIGINT, slap_set_shutdown );
356         (void) SIGNAL( SIGTERM, slap_set_shutdown );
357 #ifdef LDAP_SIGCHLD
358         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
359 #endif
360 #ifdef HAVE_WINSOCK
361         // SIGBREAK is generated when Ctrl-Break is pressed.
362         (void) SIGNAL( SIGBREAK, slap_set_shutdown );
363 #endif
364
365 #ifndef HAVE_WINSOCK
366         if(!inetd) {
367 #ifdef LDAP_DEBUG
368                 lutil_detach( ldap_debug, 0 );
369 #else
370                 lutil_detach( 0, 0 );
371 #endif
372         }
373 #endif /* HAVE_WINSOC */
374
375         if ( slap_startup(-1)  != 0 ) {
376                 rc = 1;
377                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
378                 goto shutdown;
379         }
380
381         if(!inetd) {
382                 FILE *fp;
383
384                 slapd_addr = &bind_addr;
385
386                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
387
388                 if (( slapd_pid_file != NULL ) &&
389                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
390                 {
391                         fprintf( fp, "%d\n", (int) getpid() );
392                         fclose( fp );
393                 }
394
395                 if (( slapd_args_file != NULL ) &&
396                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
397                 {
398                         for ( i = 0; i < g_argc; i++ ) {
399                                 fprintf( fp, "%s ", g_argv[i] );
400                         }
401                         fprintf( fp, "\n" );
402                         fclose( fp );
403                 }
404
405         } else {
406                 slapd_addr = NULL;
407         }
408
409         time( &starttime );
410 #ifdef HAVE_WINSOCK
411         LogSlapdStartedEvent( SERVICE_NAME, slap_debug, configfile, port, udp );
412 #endif
413         rc = slapd_daemon( slapd_addr, tcps );
414
415 #ifdef HAVE_WINSOCK
416         // Throw away the event that we used during the startup process.
417         if ( is_NT_Service )
418                 ldap_pvt_thread_cond_destroy( &started_event );
419 #endif
420
421
422 shutdown:
423         /* remember an error during shutdown */
424         rc |= slap_shutdown(-1);
425 destroy:
426         /* remember an error during destroy */
427         rc |= slap_destroy();
428
429 stop:
430 #ifdef HAVE_WINSOCK
431         LogSlapdStoppedEvent( SERVICE_NAME );
432 #endif
433         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
434 #ifdef HAVE_WINSOCK
435         ReportSlapdShutdownComplete();
436 #endif
437
438     closelog();
439
440         return;
441 }
442
443
444 #ifdef LDAP_SIGCHLD
445
446 /*
447  *  Catch and discard terminated child processes, to avoid zombies.
448  */
449
450 static RETSIGTYPE
451 wait4child( int sig )
452 {
453     int save_errno = errno;
454
455 #ifdef WNOHANG
456     errno = 0;
457 #ifdef HAVE_WAITPID
458     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
459         ;       /* NULL */
460 #else
461     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
462         ;       /* NULL */
463 #endif
464 #else
465     (void) wait( NULL );
466 #endif
467     (void) SIGNAL( sig, wait4child );
468     errno = save_errno;
469 }
470
471 #endif /* SIGCHLD || SIGCLD */
472
473
474 #ifdef LOG_LOCAL4
475
476 /*
477  *  Convert a string to an integer by means of a dispatcher table
478  *  if the string is not in the table return the default
479  */
480
481 static int
482 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
483 {
484     int        retVal = defaultVal;
485     STRDISP_P  disp;
486
487     for (disp = dispatcher; disp->stringVal; disp++) {
488
489         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
490
491             retVal = disp->intVal;
492             break;
493
494         }
495     }
496
497     return (retVal);
498
499 } /* cnvt_str2int */
500
501 #endif  /* LOG_LOCAL4 */