]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
PROTOTYPE: New connection management infrastructure designed to
[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
11 #include "ldapconfig.h"
12 #include "slap.h"
13 #include "lutil.h"                      /* Get lutil_detach() */
14
15 /*
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
18  */
19
20 #ifdef LOG_LOCAL4
21
22 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
23
24 typedef struct _str2intDispatch {
25         char    *stringVal;
26         int      abbr;
27         int      intVal;
28 } STRDISP, *STRDISP_P;
29
30
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 },
41     { NULL }
42 };
43
44 static int   cnvt_str2int( char *, STRDISP_P, int );
45
46 #endif  /* LOG_LOCAL4 */
47
48
49 static void
50 usage( char *name )
51 {
52         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
53 #ifdef LOG_LOCAL4
54     fprintf( stderr, " [-l sysloguser]" );
55 #endif
56     fprintf( stderr, "\n" );
57 }
58
59 time_t starttime;
60
61 int
62 main( int argc, char **argv )
63 {
64         int             i;
65         int             inetd = 0;
66         int             status, rc;
67         struct sockaddr_in      bind_addr, *slapd_addr;
68         int             udp;
69 #ifdef LOG_LOCAL4
70     int     syslogUser = DEFAULT_SYSLOG_USER;
71 #endif
72         char            *configfile;
73         char        *serverName;
74         int         serverMode = SLAP_SERVER_MODE;
75
76         configfile = SLAPD_DEFAULT_CONFIGFILE;
77
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);
82
83         g_argc = argc;
84         g_argv = argv;
85
86 #ifdef SLAPD_BDB2
87         while ( (i = getopt( argc, argv, "d:f:ia:p:s:ut" )) != EOF ) {
88 #else
89         while ( (i = getopt( argc, argv, "d:f:ia:p:s:u" )) != EOF ) {
90 #endif
91                 switch ( i ) {
92                 case 'a':       /* bind address */
93                         if(!inet_aton(optarg, &bind_addr.sin_addr)) {
94                                 fprintf(stderr, "invalid address (%s) for -a option", optarg);
95                         }
96             break;
97
98 #ifdef LDAP_DEBUG
99                 case 'd':       /* turn on debugging */
100                         if ( optarg[0] == '?' ) {
101                                 printf( "Debug levels:\n" );
102                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
103                                     LDAP_DEBUG_TRACE );
104                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
105                                     LDAP_DEBUG_PACKETS );
106                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
107                                     LDAP_DEBUG_ARGS );
108                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
109                                     LDAP_DEBUG_CONNS );
110                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
111                                     LDAP_DEBUG_BER );
112                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
113                                     LDAP_DEBUG_FILTER );
114                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
115                                     LDAP_DEBUG_CONFIG );
116                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
117                                     LDAP_DEBUG_ACL );
118                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
119                                     LDAP_DEBUG_STATS );
120                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
121                                     LDAP_DEBUG_STATS2 );
122                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
123                                     LDAP_DEBUG_SHELL );
124                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
125                                     LDAP_DEBUG_PARSE );
126                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
127                                     LDAP_DEBUG_ANY );
128                                 exit( 0 );
129                         } else {
130                                 slap_debug |= atoi( optarg );
131                         }
132                         break;
133 #else
134                 case 'd':       /* turn on debugging */
135                         fprintf( stderr,
136                             "must compile with LDAP_DEBUG for debugging\n" );
137                         break;
138 #endif
139
140                 case 'f':       /* read config file */
141                         configfile = ch_strdup( optarg );
142                         break;
143
144                 case 'i':       /* run from inetd */
145                         inetd = 1;
146                         break;
147
148                 case 'p': {     /* port on which to listen */
149                                 int port = atoi( optarg );
150                                 if(! port ) {
151                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
152                                 } else {
153                                         bind_addr.sin_port = htons(port);
154                                 }
155                         } break;
156
157                 case 's':       /* set syslog level */
158                         ldap_syslog = atoi( optarg );
159                         break;
160
161 #ifdef LOG_LOCAL4
162                 case 'l':       /* set syslog local user */
163                         syslogUser = cnvt_str2int( optarg, syslog_types,
164                                            DEFAULT_SYSLOG_USER );
165                         break;
166 #endif
167
168                 case 'u':       /* do udp */
169                         udp = 1;
170                         break;
171
172 #ifdef SLAPD_BDB2
173                 case 't':  /* timed server */
174                         serverMode = SLAP_TIMEDSERVER_MODE;
175                         break;
176 #endif
177
178                 default:
179                         usage( argv[0] );
180                         exit( 1 );
181                 }
182         }
183
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;
187
188         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
189
190         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
191                 serverName = ch_strdup( argv[0] );
192         } else {
193                 serverName = ch_strdup( serverName + 1 );
194         }
195
196 #ifdef LOG_LOCAL4
197         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
198 #else
199         openlog( serverName, OPENLOG_OPTIONS );
200 #endif
201
202         if ( slap_init( serverMode, serverName ) != 0 ) {
203                 rc = 1;
204                 goto destroy;
205         }
206
207         if ( read_config( configfile ) != 0 ) {
208                 rc = 1;
209                 goto destroy;
210         }
211
212         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
213         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
214 #ifdef SIGPIPE
215         (void) SIGNAL( SIGPIPE, SIG_IGN );
216 #endif
217 #ifdef SIGHUP
218         (void) SIGNAL( SIGHUP, slap_set_shutdown );
219 #endif
220         (void) SIGNAL( SIGINT, slap_set_shutdown );
221         (void) SIGNAL( SIGTERM, slap_set_shutdown );
222
223         if(!inetd) {
224 #ifdef LDAP_DEBUG
225                 lutil_detach( ldap_debug, 0 );
226 #else
227                 lutil_detach( 0, 0 );
228 #endif
229         }
230
231         if ( slap_startup(-1)  != 0 ) {
232                 rc = 1;
233                 goto shutdown;
234         }
235
236         if(!inetd) {
237                 FILE *fp;
238
239                 slapd_addr = &bind_addr;
240
241                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
242
243                 if (( slapd_pid_file != NULL ) &&
244                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
245                 {
246                         fprintf( fp, "%d\n", (int) getpid() );
247                         fclose( fp );
248                 }
249
250                 if (( slapd_args_file != NULL ) &&
251                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
252                 {
253                         for ( i = 0; i < g_argc; i++ ) {
254                                 fprintf( fp, "%s ", g_argv[i] );
255                         }
256                         fprintf( fp, "\n" );
257                         fclose( fp );
258                 }
259
260         } else {
261                 slapd_addr = NULL;
262         }
263
264         time( &starttime );
265
266         rc = slapd_daemon( slapd_addr );
267
268 shutdown:
269         /* remember an error during shutdown */
270         rc |= slap_shutdown(-1);
271 destroy:
272         /* remember an error during destroy */
273         rc |= slap_destroy();
274
275         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
276
277         return rc;
278 }
279
280
281 #ifdef LOG_LOCAL4
282
283 /*
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
286  */
287
288 static int
289 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
290 {
291     int        retVal = defaultVal;
292     STRDISP_P  disp;
293
294     for (disp = dispatcher; disp->stringVal; disp++) {
295
296         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
297
298             retVal = disp->intVal;
299             break;
300
301         }
302     }
303
304     return (retVal);
305
306 } /* cnvt_str2int */
307
308 #endif  /* LOG_LOCAL4 */
309