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