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