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