]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
a3cbae3b2d26b175c8655149b8d96d6d58684e2c
[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 #include "lutil.h"                      /* Get lutil_detach() */
17
18 #ifdef LDAP_SIGCHLD
19 static RETSIGTYPE wait4child( int sig );
20 #endif
21
22 /*
23  * when more than one slapd is running on one machine, each one might have
24  * it's own LOCAL for syslogging and must have its own pid/args files
25  */
26
27 #ifdef LOG_LOCAL4
28
29 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
30
31 typedef struct _str2intDispatch {
32         char    *stringVal;
33         int      abbr;
34         int      intVal;
35 } STRDISP, *STRDISP_P;
36
37
38 /* table to compute syslog-options to integer */
39 static STRDISP  syslog_types[] = {
40     { "LOCAL0",         6, LOG_LOCAL0 },
41     { "LOCAL1",         6, LOG_LOCAL1 },
42     { "LOCAL2",         6, LOG_LOCAL2 },
43     { "LOCAL3",         6, LOG_LOCAL3 },
44     { "LOCAL4",         6, LOG_LOCAL4 },
45     { "LOCAL5",         6, LOG_LOCAL5 },
46     { "LOCAL6",         6, LOG_LOCAL6 },
47     { "LOCAL7",         6, LOG_LOCAL7 },
48     { NULL }
49 };
50
51 static int   cnvt_str2int( char *, STRDISP_P, int );
52
53 #endif  /* LOG_LOCAL4 */
54
55
56 static void
57 usage( char *name )
58 {
59         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
60     fprintf( stderr, "\n        [-a bind-address] [-i] [-u]" );
61 #ifdef SLAPD_BDB2
62     fprintf( stderr, " [-t]" );
63 #endif
64 #ifdef LOG_LOCAL4
65     fprintf( stderr, " [-l sysloguser]" );
66 #endif
67 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
68     fprintf( stderr, " [-U user] [-G group]" );
69 #endif
70     fprintf( stderr, "\n" );
71 }
72
73 time_t starttime;
74
75 int
76 main( int argc, char **argv )
77 {
78         int             i;
79         int             inetd = 0;
80         int             rc;
81         struct sockaddr_in      bind_addr;
82         int             tcps;
83         int             udp;
84 #ifdef LOG_LOCAL4
85     int     syslogUser = DEFAULT_SYSLOG_USER;
86 #endif
87 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
88         char            *username = NULL, *groupname = NULL;
89 #endif
90         char            *configfile;
91         char        *serverName;
92         int         serverMode = SLAP_SERVER_MODE;
93
94         configfile = SLAPD_DEFAULT_CONFIGFILE;
95
96         (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
97         bind_addr.sin_family = AF_INET;
98         bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
99         bind_addr.sin_port = htons(LDAP_PORT);
100
101         g_argc = argc;
102         g_argv = argv;
103
104         while ( (i = getopt( argc, argv,
105                              "d:f:ia:p:s:u"
106 #ifdef LOG_LOCAL4
107                              "l:"
108 #endif
109 #ifdef SLAPD_BDB2
110                              "t"
111 #endif
112 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
113                              "U:G:"
114 #endif
115                              )) != EOF ) {
116                 switch ( i ) {
117                 case 'a':       /* bind address */
118 #ifdef HAVE_WINSOCK
119                         if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
120 #else
121                         if(!inet_aton(optarg, &bind_addr.sin_addr))
122 #endif
123                         {
124                                 fprintf(stderr, "invalid address (%s) for -a option", optarg);
125                         }
126             break;
127
128 #ifdef LDAP_DEBUG
129                 case 'd':       /* turn on debugging */
130                         if ( optarg[0] == '?' ) {
131                                 printf( "Debug levels:\n" );
132                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
133                                     LDAP_DEBUG_TRACE );
134                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
135                                     LDAP_DEBUG_PACKETS );
136                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
137                                     LDAP_DEBUG_ARGS );
138                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
139                                     LDAP_DEBUG_CONNS );
140                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
141                                     LDAP_DEBUG_BER );
142                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
143                                     LDAP_DEBUG_FILTER );
144                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
145                                     LDAP_DEBUG_CONFIG );
146                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
147                                     LDAP_DEBUG_ACL );
148                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
149                                     LDAP_DEBUG_STATS );
150                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
151                                     LDAP_DEBUG_STATS2 );
152                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
153                                     LDAP_DEBUG_SHELL );
154                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
155                                     LDAP_DEBUG_PARSE );
156                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
157                                     LDAP_DEBUG_ANY );
158                                 exit( 0 );
159                         } else {
160                                 slap_debug |= atoi( optarg );
161                         }
162                         break;
163 #else
164                 case 'd':       /* turn on debugging */
165                         fprintf( stderr,
166                             "must compile with LDAP_DEBUG for debugging\n" );
167                         break;
168 #endif
169
170                 case 'f':       /* read config file */
171                         configfile = ch_strdup( optarg );
172                         break;
173
174                 case 'i':       /* run from inetd */
175                         inetd = 1;
176                         break;
177
178                 case 'p': {     /* port on which to listen */
179                                 short port = (short)atoi( optarg );
180                                 if(! port ) {
181                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
182                                 } else {
183                                         bind_addr.sin_port = htons(port);
184                                 }
185                         } break;
186
187                 case 's':       /* set syslog level */
188                         ldap_syslog = atoi( optarg );
189                         break;
190
191 #ifdef LOG_LOCAL4
192                 case 'l':       /* set syslog local user */
193                         syslogUser = cnvt_str2int( optarg, syslog_types,
194                                            DEFAULT_SYSLOG_USER );
195                         break;
196 #endif
197
198                 case 'u':       /* do udp */
199                         udp = 1;
200                         break;
201
202 #ifdef SLAPD_BDB2
203                 case 't':  /* timed server */
204                         serverMode = SLAP_TIMEDSERVER_MODE;
205                         break;
206 #endif
207
208 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
209                 case 'U':       /* user name */
210                         username = ch_strdup( optarg );
211                         break;
212
213                 case 'G':       /* group name */
214                         groupname = ch_strdup( optarg );
215                         break;
216 #endif /* HAVE_PWD_H && HAVE_GRP_H */
217
218                 default:
219                         usage( argv[0] );
220                         exit( 1 );
221                 }
222         }
223
224         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
225         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
226         ldif_debug = slap_debug;
227
228         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
229
230         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
231                 serverName = ch_strdup( argv[0] );
232         } else {
233                 serverName = ch_strdup( serverName + 1 );
234         }
235
236 #ifdef LOG_LOCAL4
237         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
238 #else
239         openlog( serverName, OPENLOG_OPTIONS );
240 #endif
241
242         tcps = set_socket( inetd ? NULL : &bind_addr );
243
244 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
245         if ( username != NULL || groupname != NULL )
246                 slap_init_user( username, groupname );
247 #endif
248
249         if ( slap_init( serverMode, serverName ) != 0 ) {
250                 rc = 1;
251                 goto destroy;
252         }
253
254         if ( read_config( configfile ) != 0 ) {
255                 rc = 1;
256                 goto destroy;
257         }
258
259         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
260         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
261 #ifdef SIGPIPE
262         (void) SIGNAL( SIGPIPE, SIG_IGN );
263 #endif
264 #ifdef SIGHUP
265         (void) SIGNAL( SIGHUP, slap_set_shutdown );
266 #endif
267         (void) SIGNAL( SIGINT, slap_set_shutdown );
268         (void) SIGNAL( SIGTERM, slap_set_shutdown );
269 #ifdef LDAP_SIGCHLD
270         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
271 #endif
272
273 #ifndef WIN32
274         if(!inetd) {
275 #ifdef LDAP_DEBUG
276                 lutil_detach( ldap_debug, 0 );
277 #else
278                 lutil_detach( 0, 0 );
279 #endif
280         }
281 #endif /* WIN32 */
282
283         if ( slap_startup(-1)  != 0 ) {
284                 rc = 1;
285                 goto shutdown;
286         }
287
288         if(!inetd) {
289                 FILE *fp;
290
291                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
292
293                 if (( slapd_pid_file != NULL ) &&
294                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
295                 {
296                         fprintf( fp, "%d\n", (int) getpid() );
297                         fclose( fp );
298                 }
299
300                 if (( slapd_args_file != NULL ) &&
301                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
302                 {
303                         for ( i = 0; i < g_argc; i++ ) {
304                                 fprintf( fp, "%s ", g_argv[i] );
305                         }
306                         fprintf( fp, "\n" );
307                         fclose( fp );
308                 }
309         }
310
311         time( &starttime );
312
313         rc = slapd_daemon( inetd, tcps );
314
315 shutdown:
316         /* remember an error during shutdown */
317         rc |= slap_shutdown(-1);
318 destroy:
319         /* remember an error during destroy */
320         rc |= slap_destroy();
321
322         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
323
324         return rc;
325 }
326
327
328 #ifdef LDAP_SIGCHLD
329
330 /*
331  *  Catch and discard terminated child processes, to avoid zombies.
332  */
333
334 static RETSIGTYPE
335 wait4child( int sig )
336 {
337     int save_errno = errno;
338
339 #ifdef WNOHANG
340     errno = 0;
341 #ifdef HAVE_WAITPID
342     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
343         ;       /* NULL */
344 #else
345     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
346         ;       /* NULL */
347 #endif
348 #else
349     (void) wait( NULL );
350 #endif
351     (void) SIGNAL( sig, wait4child );
352     errno = save_errno;
353 }
354
355 #endif /* SIGCHLD || SIGCLD */
356
357
358 #ifdef LOG_LOCAL4
359
360 /*
361  *  Convert a string to an integer by means of a dispatcher table
362  *  if the string is not in the table return the default
363  */
364
365 static int
366 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
367 {
368     int        retVal = defaultVal;
369     STRDISP_P  disp;
370
371     for (disp = dispatcher; disp->stringVal; disp++) {
372
373         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
374
375             retVal = disp->intVal;
376             break;
377
378         }
379     }
380
381     return (retVal);
382
383 } /* cnvt_str2int */
384
385 #endif  /* LOG_LOCAL4 */