]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
eb320394b85f2243d0cc169d19c7d7015bb855ba
[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();
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 int
60 main( int argc, char **argv )
61 {
62         int             i;
63         int             inetd = 0;
64         int             rc = 0;
65         int             port;
66         int             udp;
67 #ifdef LOG_LOCAL4
68     int     syslogUser = DEFAULT_SYSLOG_USER;
69 #endif
70         char            *configfile;
71         char        *serverName;
72
73         configfile = SLAPD_DEFAULT_CONFIGFILE;
74         port = LDAP_PORT;
75         g_argc = argc;
76         g_argv = argv;
77
78         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
79                 switch ( i ) {
80 #ifdef LDAP_DEBUG
81                 case 'd':       /* turn on debugging */
82                         if ( optarg[0] == '?' ) {
83                                 printf( "Debug levels:\n" );
84                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
85                                     LDAP_DEBUG_TRACE );
86                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
87                                     LDAP_DEBUG_PACKETS );
88                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
89                                     LDAP_DEBUG_ARGS );
90                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
91                                     LDAP_DEBUG_CONNS );
92                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
93                                     LDAP_DEBUG_BER );
94                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
95                                     LDAP_DEBUG_FILTER );
96                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
97                                     LDAP_DEBUG_CONFIG );
98                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
99                                     LDAP_DEBUG_ACL );
100                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
101                                     LDAP_DEBUG_STATS );
102                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
103                                     LDAP_DEBUG_STATS2 );
104                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
105                                     LDAP_DEBUG_SHELL );
106                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
107                                     LDAP_DEBUG_PARSE );
108                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
109                                     LDAP_DEBUG_ANY );
110                                 exit( 0 );
111                         } else {
112                                 slap_debug |= atoi( optarg );
113                         }
114                         break;
115 #else
116                 case 'd':       /* turn on debugging */
117                         fprintf( stderr,
118                             "must compile with LDAP_DEBUG for debugging\n" );
119                         break;
120 #endif
121
122                 case 'f':       /* read config file */
123                         configfile = ch_strdup( optarg );
124                         break;
125
126                 case 'i':       /* run from inetd */
127                         inetd = 1;
128                         break;
129
130                 case 'p':       /* port on which to listen */
131                         port = atoi( optarg );
132                         break;
133
134                 case 's':       /* set syslog level */
135                         ldap_syslog = atoi( optarg );
136                         break;
137
138 #ifdef LOG_LOCAL4
139
140                 case 'l':       /* set syslog local user */
141                         syslogUser = cnvt_str2int( optarg, syslog_types,
142                                            DEFAULT_SYSLOG_USER );
143                         break;
144
145 #endif
146
147                 case 'u':       /* do udp */
148                         udp = 1;
149                         break;
150
151                 default:
152                         usage( argv[0] );
153                         exit( 1 );
154                 }
155         }
156
157         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
158         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
159         ldif_debug = slap_debug;
160
161         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
162
163         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
164                 serverName = ch_strdup( argv[0] );
165         } else {
166                 serverName = ch_strdup( serverName + 1 );
167         }
168
169 #ifdef LOG_LOCAL4
170         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
171 #else
172         openlog( serverName, OPENLOG_OPTIONS );
173 #endif
174
175 #ifdef SLAPD_BDB2
176         bdb2i_do_timing = 1;
177 #endif
178
179         if ( slap_init( SLAP_SERVER_MODE, serverName ) != 0 ) {
180                 rc = 1;
181                 goto destroy;
182         }
183
184         if ( read_config( configfile ) != 0 ) {
185                 rc = 1;
186                 goto destroy;
187         }
188
189         if ( slap_startup(-1)  != 0 ) {
190                 rc = 1;
191                 goto shutdown;
192         }
193
194         if ( ! inetd ) {
195                 int             status;
196
197                 (void) SIGNAL( SIGPIPE, SIG_IGN );
198                 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
199                 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
200                 (void) SIGNAL( SIGTERM, slap_set_shutdown );
201                 (void) SIGNAL( SIGINT, slap_set_shutdown );
202                 (void) SIGNAL( SIGHUP, slap_set_shutdown );
203
204 #ifdef LDAP_DEBUG
205                 lutil_detach( ldap_debug, 0 );
206 #else
207                 lutil_detach( 0, 0 );
208 #endif
209
210                 time( &starttime );
211
212                 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
213                         slapd_daemon, (void *) port ) != 0 )
214                 {
215                         Debug( LDAP_DEBUG_ANY,
216                             "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
217
218                         rc = 1;
219
220                 } else {
221                         /* wait for the listener thread to complete */
222                         ldap_pvt_thread_join( listener_tid, (void *) NULL );
223                 }
224
225         } else {
226                 Connection              c;
227                 Operation               *o;
228                 BerElement              ber;
229                 unsigned long           len, tag;
230                 long                    msgid;
231                 int                     flen;
232                 struct sockaddr_in      from;
233                 struct hostent          *hp;
234
235                 c.c_dn = NULL;
236                 c.c_cdn = NULL;
237                 c.c_ops = NULL;
238                 c.c_sb.sb_sd = 0;
239                 c.c_sb.sb_options = 0;
240                 c.c_sb.sb_naddr = udp ? 1 : 0;
241                 c.c_sb.sb_ber.ber_buf = NULL;
242                 c.c_sb.sb_ber.ber_ptr = NULL;
243                 c.c_sb.sb_ber.ber_end = NULL;
244                 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
245                 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
246                 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
247 #ifdef notdefcldap
248                 c.c_sb.sb_addrs = (void **) saddrlist;
249                 c.c_sb.sb_fromaddr = &faddr;
250                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
251 #endif
252                 flen = sizeof(from);
253                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
254 #ifdef SLAPD_RLOOKUPS
255                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
256                             sizeof(from.sin_addr.s_addr), AF_INET );
257 #else
258                         hp = NULL;
259 #endif
260
261                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
262                             hp == NULL ? "unknown" : hp->h_name,
263                             inet_ntoa( from.sin_addr ), 0 );
264
265                         c.c_addr = inet_ntoa( from.sin_addr );
266                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
267                 } else {
268                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
269                             0, 0, 0 );
270                 }
271
272                 ber_init_w_nullc( &ber, 0 );
273
274                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
275                     == LDAP_TAG_MESSAGE ) {
276                         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
277                         time( &currenttime );
278                         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
279
280                         if ( (tag = ber_get_int( &ber, &msgid ))
281                             != LDAP_TAG_MSGID ) {
282                                 /* log and send error */
283                                 Debug( LDAP_DEBUG_ANY,
284                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
285                                 ber_free( &ber, 1 );
286                                 return 1;
287                         }
288
289                         if ( (tag = ber_peek_tag( &ber, &len ))
290                             == LBER_ERROR ) {
291                                 /* log, close and send error */
292                                 Debug( LDAP_DEBUG_ANY,
293                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
294                                 ber_free( &ber, 1 );
295                                 close( c.c_sb.sb_sd );
296                                 c.c_sb.sb_sd = -1;
297                                 return 1;
298                         }
299
300                         connection_activity( &c );
301
302                         ber_free( &ber, 1 );
303                 }
304         }
305
306 shutdown:
307         /* remember an error during shutdown */
308         rc |= slap_shutdown(-1);
309 destroy:
310         /* remember an error during destroy */
311         rc |= slap_destroy();
312
313         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
314
315         return rc;
316 }
317
318
319 #ifdef LOG_LOCAL4
320
321 /*
322  *  Convert a string to an integer by means of a dispatcher table
323  *  if the string is not in the table return the default
324  */
325
326 static int
327 cnvt_str2int (stringVal, dispatcher, defaultVal)
328 char      *stringVal;
329 STRDISP_P  dispatcher;
330 int        defaultVal;
331 {
332     int        retVal = defaultVal;
333     STRDISP_P  disp;
334
335     for (disp = dispatcher; disp->stringVal; disp++) {
336
337         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
338
339             retVal = disp->intVal;
340             break;
341
342         }
343     }
344
345     return (retVal);
346
347 } /* cnvt_str2int */
348
349 #endif  /* LOG_LOCAL4 */
350