]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Add missing "\n" in usage statement
[openldap] / servers / slapd / main.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <stdio.h>
8
9 #include <ac/signal.h>
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/signal.h>
16 #include <ac/errno.h>
17
18 #include "ldap_defaults.h"
19 #include "slap.h"
20 #include "lutil.h"
21
22 #ifdef LDAP_SIGCHLD
23 static RETSIGTYPE wait4child( int sig );
24 #endif
25
26 #ifdef HAVE_WINSOCK
27 #define MAIN_RETURN(x) return
28 struct sockaddr_in      bind_addr;
29
30 /* in nt_main.c */
31 extern SERVICE_STATUS                   SLAPDServiceStatus;
32 extern SERVICE_STATUS_HANDLE    hSLAPDServiceStatus;
33 extern ldap_pvt_thread_cond_t   started_event,          stopped_event;
34 extern int        is_NT_Service;
35
36 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
37 void LogSlapdStoppedEvent( char *svc );
38
39 void CommenceStartupProcessing( LPCTSTR serviceName,
40                                                            void(*stopper)(int));
41 void ReportSlapdShutdownComplete( void );
42 void *getRegParam( char *svc, char *value );
43
44 #define SERVICE_EXIT( e, n ) \
45                 if ( is_NT_Service ) \
46 { \
47                         SLAPDServiceStatus.dwWin32ExitCode                              = e; \
48                         SLAPDServiceStatus.dwServiceSpecificExitCode    = n; \
49
50 #else
51 #define SERVICE_EXIT( e, n )
52 #define MAIN_RETURN(x) return(x)
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",         6, LOG_LOCAL0 },
79     { "LOCAL1",         6, LOG_LOCAL1 },
80     { "LOCAL2",         6, LOG_LOCAL2 },
81     { "LOCAL3",         6, LOG_LOCAL3 },
82     { "LOCAL4",         6, LOG_LOCAL4 },
83     { "LOCAL5",         6, LOG_LOCAL5 },
84     { "LOCAL6",         6, LOG_LOCAL6 },
85     { "LOCAL7",         6, LOG_LOCAL7 },
86     { NULL }
87 };
88
89 static int   cnvt_str2int( char *, STRDISP_P, int );
90
91 #endif  /* LOG_LOCAL4 */
92
93
94 static void
95 usage( char *name )
96 {
97         fprintf( stderr,
98                 "usage: %s options\n", name );
99         fprintf( stderr,
100 #if LDAP_CONNECTIONLESS
101                 "\t-c\t\tEnable (experimental) Connectionless LDAP\n"
102 #endif
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 ran as\n"
107 #endif
108                 "\t-h URLs\tList of URLs to serve\n"
109 #ifdef LOG_LOCAL4
110                 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
111 #endif
112 #ifdef HAVE_WINSOCK
113                 "\t-n NTserviceName\tNT service name\n"
114 #endif
115
116                 "\t-p port\tLDAP Port\n"
117 #ifdef HAVE_TLS
118                 "\t-P port\tLDAP over TLS Port\n"
119 #endif
120                 "\t-s level\tSyslog Level\n"
121 #ifdef SLAPD_BDB2
122                 "\t-t\t\tEnable BDB2 timing\n"
123 #endif
124 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
125                 "\t-u user\tUser (id or name) to ran as\n"
126 #endif
127     );
128 }
129
130 #ifdef HAVE_WINSOCK
131 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
132 #else
133 int main( int argc, char **argv )
134 #endif
135 {
136         int             i;
137         int             rc;
138         char *urls = NULL;
139 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
140         char *username = NULL;
141         char *groupname = NULL;
142 #endif
143 #ifdef LOG_LOCAL4
144     int     syslogUser = DEFAULT_SYSLOG_USER;
145 #endif
146 #ifdef HAVE_WINSOCK
147         char        *NTservice  = SERVICE_NAME;
148         char            *configfile = ".\\slapd.conf";
149 #else
150         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
151 #endif
152         char        *serverName;
153         int         serverMode = SLAP_SERVER_MODE;
154
155         int port = LDAP_PORT;
156 #ifdef HAVE_TLS
157         int tls_port = LDAPS_PORT;
158 #else
159         int tls_port = 0;
160 #endif
161
162         g_argc = argc;
163         g_argv = argv;
164
165 #ifdef HAVE_WINSOCK
166         {
167                 int *i;
168                 char *newConfigFile;
169                 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
170                 i = (int*)getRegParam( NULL, "Port" );
171                 if ( i != NULL )
172                 {
173                         port = *i;
174                         Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
175                 }
176 #ifdef HAVE_TLS
177                 i = (int*)getRegParam( NULL, "TLSPort" );
178                 if ( i != NULL )
179                 {
180                         tls_port = *i;
181                         Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
182                 }
183 #endif
184                 i = (int*)getRegParam( NULL, "DebugLevel" );
185                 if ( i != NULL ) 
186                 {
187                         slap_debug = *i;
188                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
189                 }
190                 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
191                 if ( newConfigFile != NULL ) 
192                 {
193                         configfile = newConfigFile;
194                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
195                 }
196         }
197 #endif
198
199         while ( (i = getopt( argc, argv,
200                              "d:f:h:p:s:u"
201 #ifdef LOG_LOCAL4
202                              "l:"
203 #endif
204 #ifdef SLAPD_BDB2
205                              "t"
206 #endif
207 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
208                              "u:g:"
209 #endif
210 #ifdef LDAP_CONNECTIONLESS
211                                  "c"
212 #endif
213 #ifdef HAVE_WINSOCK
214                                  "n:"
215 #endif
216 #ifdef HAVE_TLS
217                              "P:"
218 #endif
219                              )) != EOF ) {
220                 switch ( i ) {
221                 case 'h':       /* listen URLs */
222                         urls = ch_strdup( optarg );
223             break;
224
225 #ifdef LDAP_DEBUG
226                 case 'd':       /* turn on debugging */
227                         slap_debug |= atoi( optarg );
228                         break;
229 #else
230                 case 'd':       /* turn on debugging */
231                         fprintf( stderr,
232                             "must compile with LDAP_DEBUG for debugging\n" );
233                         break;
234 #endif
235
236                 case 'f':       /* read config file */
237                         configfile = ch_strdup( optarg );
238                         break;
239
240                 case 'p': {     /* port on which to listen */
241                                 int p = atoi( optarg );
242                                 if(! p ) {
243                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
244                                 } else if( p < 0 || p >= 1<<16) {
245                                         fprintf(stderr, "-p %s invalid\n", optarg);
246                                 } else {
247                                         port = p;
248                                 }
249                         } break;
250
251 #ifdef HAVE_TLS
252                 case 'P': {     /* port on which to listen for TLS */
253                                 int p = atoi( optarg );
254                                 if(! p ) {
255                                         fprintf(stderr, "-P %s must be numeric\n", optarg);
256                                 } else if( p < 0 || p >= 1<<16) {
257                                         fprintf(stderr, "-P %s invalid\n", optarg);
258                                 } else {
259                                         tls_port = p;
260                                 }
261                         } break;
262 #endif
263
264                 case 's':       /* set syslog level */
265                         ldap_syslog = atoi( optarg );
266                         break;
267
268 #ifdef LOG_LOCAL4
269                 case 'l':       /* set syslog local user */
270                         syslogUser = cnvt_str2int( optarg,
271                                 syslog_types, DEFAULT_SYSLOG_USER );
272                         break;
273 #endif
274
275 #ifdef LDAP_CONNECTIONLESS
276                 case 'c':       /* do connectionless (udp) */
277                         /* udp = 1; */
278                         fprintf( stderr, "connectionless support not supported");
279                         exit( EXIT_FAILURE );
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 #ifdef HAVE_WINSOCK
302                 case 'n':  /* NT service name */
303                         NTservice = ch_strdup( optarg );
304                         break;
305 #endif
306                 default:
307                         usage( argv[0] );
308                         rc = 1;
309                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
310                         goto stop;
311                 }
312         }
313
314         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
315         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
316         ldif_debug = slap_debug;
317
318         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
319
320         if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
321                 serverName = ch_strdup( argv[0] );
322         } else {
323                 serverName = ch_strdup( serverName + 1 );
324         }
325
326 #ifdef LOG_LOCAL4
327         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
328 #else
329         openlog( serverName, OPENLOG_OPTIONS );
330 #endif
331
332         if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
333                 rc = 1;
334                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
335                 goto stop;
336         }
337
338 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
339         if ( username != NULL || groupname != NULL ) {
340                 slap_init_user( username, groupname );
341         }
342 #endif
343
344         if ( slap_init( serverMode, serverName ) != 0 ) {
345                 rc = 1;
346                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
347                 goto destroy;
348         }
349
350         if ( read_config( configfile ) != 0 ) {
351                 rc = 1;
352                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
353                 goto destroy;
354         }
355
356 #ifdef HAVE_TLS
357         ldap_pvt_tls_init();
358         ldap_pvt_tls_init_def_ctx();
359 #endif
360
361         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
362         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
363 #ifdef SIGPIPE
364         (void) SIGNAL( SIGPIPE, SIG_IGN );
365 #endif
366 #ifdef SIGHUP
367         (void) SIGNAL( SIGHUP, slap_set_shutdown );
368 #endif
369         (void) SIGNAL( SIGINT, slap_set_shutdown );
370         (void) SIGNAL( SIGTERM, slap_set_shutdown );
371 #ifdef LDAP_SIGCHLD
372         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
373 #endif
374 #ifdef SIGBREAK
375         /* SIGBREAK is generated when Ctrl-Break is pressed. */
376         (void) SIGNAL( SIGBREAK, slap_set_shutdown );
377 #endif
378
379 #ifndef HAVE_WINSOCK
380 #ifdef LDAP_DEBUG
381                 lutil_detach( ldap_debug, 0 );
382 #else
383                 lutil_detach( 0, 0 );
384 #endif
385 #endif /* HAVE_WINSOCK */
386
387         if ( slap_startup(-1)  != 0 ) {
388                 rc = 1;
389                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
390                 goto shutdown;
391         }
392
393         {
394                 FILE *fp;
395
396                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
397
398                 if (( slapd_pid_file != NULL ) &&
399                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
400                 {
401                         fprintf( fp, "%d\n", (int) getpid() );
402                         fclose( fp );
403                 }
404
405                 if (( slapd_args_file != NULL ) &&
406                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
407                 {
408                         for ( i = 0; i < g_argc; i++ ) {
409                                 fprintf( fp, "%s ", g_argv[i] );
410                         }
411                         fprintf( fp, "\n" );
412                         fclose( fp );
413                 }
414         }
415
416 #ifdef HAVE_WINSOCK
417         LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
418 #endif
419
420         rc = slapd_daemon();
421
422 #ifdef HAVE_WINSOCK
423         /* Throw away the event that we used during the startup process. */
424         if ( is_NT_Service )
425                 ldap_pvt_thread_cond_destroy( &started_event );
426 #endif
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( NTservice );
438 #endif
439
440         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
441
442 #ifdef HAVE_WINSOCK
443         ReportSlapdShutdownComplete();
444 #endif
445
446     closelog();
447         slapd_daemon_destroy();
448
449         MAIN_RETURN(rc);
450 }
451
452
453 #ifdef LDAP_SIGCHLD
454
455 /*
456  *  Catch and discard terminated child processes, to avoid zombies.
457  */
458
459 static RETSIGTYPE
460 wait4child( int sig )
461 {
462     int save_errno = errno;
463
464 #ifdef WNOHANG
465     errno = 0;
466 #ifdef HAVE_WAITPID
467     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
468         ;       /* NULL */
469 #else
470     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
471         ;       /* NULL */
472 #endif
473 #else
474     (void) wait( NULL );
475 #endif
476     (void) SIGNAL( sig, wait4child );
477     errno = save_errno;
478 }
479
480 #endif /* SIGCHLD || SIGCLD */
481
482
483 #ifdef LOG_LOCAL4
484
485 /*
486  *  Convert a string to an integer by means of a dispatcher table
487  *  if the string is not in the table return the default
488  */
489
490 static int
491 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
492 {
493     int        retVal = defaultVal;
494     STRDISP_P  disp;
495
496     for (disp = dispatcher; disp->stringVal; disp++) {
497
498         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
499
500             retVal = disp->intVal;
501             break;
502
503         }
504     }
505
506     return (retVal);
507 }
508
509 #endif  /* LOG_LOCAL4 */