11 #include "ldapconfig.h"
13 #include "lutil.h" /* Get lutil_detach() */
16 * when more than one slapd is running on one machine, each one might have
17 * it's own LOCAL for syslogging and must have its own pid/args files
22 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
24 typedef struct _str2intDispatch {
28 } STRDISP, *STRDISP_P;
31 /* table to compute syslog-options to integer */
32 static STRDISP syslog_types[] = {
33 { "LOCAL0", 6, LOG_LOCAL0 },
34 { "LOCAL1", 6, LOG_LOCAL1 },
35 { "LOCAL2", 6, LOG_LOCAL2 },
36 { "LOCAL3", 6, LOG_LOCAL3 },
37 { "LOCAL4", 6, LOG_LOCAL4 },
38 { "LOCAL5", 6, LOG_LOCAL5 },
39 { "LOCAL6", 6, LOG_LOCAL6 },
40 { "LOCAL7", 6, LOG_LOCAL7 },
44 static int cnvt_str2int( char *, STRDISP_P, int );
46 #endif /* LOG_LOCAL4 */
52 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
54 fprintf( stderr, " [-l sysloguser]" );
56 fprintf( stderr, "\n" );
62 main( int argc, char **argv )
67 struct sockaddr_in bind_addr, *slapd_addr;
70 int syslogUser = DEFAULT_SYSLOG_USER;
74 int serverMode = SLAP_SERVER_MODE;
76 configfile = SLAPD_DEFAULT_CONFIGFILE;
78 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
79 bind_addr.sin_family = AF_INET;
80 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
81 bind_addr.sin_port = htons(LDAP_PORT);
87 while ( (i = getopt( argc, argv, "d:f:ia:p:s:ut" )) != EOF ) {
89 while ( (i = getopt( argc, argv, "d:f:ia:p:s:u" )) != EOF ) {
92 case 'a': /* bind address */
93 if(!inet_aton(optarg, &bind_addr.sin_addr)) {
94 fprintf(stderr, "invalid address (%s) for -a option", optarg);
99 case 'd': /* turn on debugging */
100 if ( optarg[0] == '?' ) {
101 printf( "Debug levels:\n" );
102 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
104 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
105 LDAP_DEBUG_PACKETS );
106 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
108 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
110 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
112 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
114 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
116 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
118 printf( "\tLDAP_DEBUG_STATS\t%d\n",
120 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
122 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
124 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
126 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
130 slap_debug |= atoi( optarg );
134 case 'd': /* turn on debugging */
136 "must compile with LDAP_DEBUG for debugging\n" );
140 case 'f': /* read config file */
141 configfile = ch_strdup( optarg );
144 case 'i': /* run from inetd */
148 case 'p': { /* port on which to listen */
149 int port = atoi( optarg );
151 fprintf(stderr, "-p %s must be numeric\n", optarg);
153 bind_addr.sin_port = htons(port);
157 case 's': /* set syslog level */
158 ldap_syslog = atoi( optarg );
162 case 'l': /* set syslog local user */
163 syslogUser = cnvt_str2int( optarg, syslog_types,
164 DEFAULT_SYSLOG_USER );
168 case 'u': /* do udp */
173 case 't': /* timed server */
174 serverMode = SLAP_TIMEDSERVER_MODE;
184 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
185 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
186 ldif_debug = slap_debug;
188 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
190 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
191 serverName = ch_strdup( argv[0] );
193 serverName = ch_strdup( serverName + 1 );
197 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
199 openlog( serverName, OPENLOG_OPTIONS );
202 if ( slap_init( serverMode, serverName ) != 0 ) {
207 if ( read_config( configfile ) != 0 ) {
212 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
213 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
215 (void) SIGNAL( SIGPIPE, SIG_IGN );
218 (void) SIGNAL( SIGHUP, slap_set_shutdown );
220 (void) SIGNAL( SIGINT, slap_set_shutdown );
221 (void) SIGNAL( SIGTERM, slap_set_shutdown );
225 lutil_detach( ldap_debug, 0 );
227 lutil_detach( 0, 0 );
231 if ( slap_startup(-1) != 0 ) {
239 slapd_addr = &bind_addr;
241 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
243 if (( slapd_pid_file != NULL ) &&
244 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
246 fprintf( fp, "%d\n", (int) getpid() );
250 if (( slapd_args_file != NULL ) &&
251 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
253 for ( i = 0; i < g_argc; i++ ) {
254 fprintf( fp, "%s ", g_argv[i] );
266 rc = slapd_daemon( slapd_addr );
269 /* remember an error during shutdown */
270 rc |= slap_shutdown(-1);
272 /* remember an error during destroy */
273 rc |= slap_destroy();
275 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
284 * Convert a string to an integer by means of a dispatcher table
285 * if the string is not in the table return the default
289 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
291 int retVal = defaultVal;
294 for (disp = dispatcher; disp->stringVal; disp++) {
296 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
298 retVal = disp->intVal;
308 #endif /* LOG_LOCAL4 */