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