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