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