]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Apply fix suggested by Ben Collins <bmc@visi.net>
[openldap] / servers / slapd / main.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/socket.h>
6 #include <ac/string.h>
7 #include <ac/time.h>
8 #include <ac/unistd.h>
9
10 #include "ldapconfig.h"
11 #include "slap.h"
12 #include "lutil.h"                      /* Get lutil_detach() */
13
14
15 /*
16  * read-only global variables or variables only written by the listener
17  * thread (after they are initialized) - no need to protect them with a mutex.
18  */
19 int             slap_debug = 0;
20
21 #ifdef LDAP_DEBUG
22 int             ldap_syslog = LDAP_DEBUG_STATS;
23 #else
24 int             ldap_syslog;
25 #endif
26
27 int             ldap_syslog_level = LOG_DEBUG;
28 int             udp;
29 char            *default_referral;
30 char            *configfile;
31 time_t          starttime;
32 pthread_t       listener_tid;
33 int             g_argc;
34 char            **g_argv;
35 /*
36  * global variables that need mutex protection
37  */
38 time_t          currenttime;
39 pthread_mutex_t currenttime_mutex;
40 pthread_mutex_t strtok_mutex;
41 int             active_threads;
42 pthread_mutex_t active_threads_mutex;
43 pthread_mutex_t new_conn_mutex;
44 #ifdef SLAPD_CRYPT
45 pthread_mutex_t crypt_mutex;
46 #endif
47 long            ops_initiated;
48 long            ops_completed;
49 int             num_conns;
50 pthread_mutex_t ops_mutex;
51 long            num_entries_sent;
52 long            num_bytes_sent;
53 pthread_mutex_t num_sent_mutex;
54 /*
55  * these mutexes must be used when calling the entry2str()
56  * routine since it returns a pointer to static data.
57  */
58 pthread_mutex_t entry2str_mutex;
59 pthread_mutex_t replog_mutex;
60
61 /*
62  * when more than one slapd is running on one machine, each one might have
63  * it's own LOCAL for syslogging and must have its own pid/args files
64  */
65
66 #ifdef LOG_LOCAL4
67
68 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
69
70 typedef struct _str2intDispatch {
71
72         char    *stringVal;
73         int      abbr;
74         int      intVal;
75
76 } STRDISP, *STRDISP_P;
77
78
79 /* table to compute syslog-options to integer */
80 static STRDISP  syslog_types[] = {
81
82     { "LOCAL0",         6, LOG_LOCAL0 },
83     { "LOCAL1",         6, LOG_LOCAL1 },
84     { "LOCAL2",         6, LOG_LOCAL2 },
85     { "LOCAL3",         6, LOG_LOCAL3 },
86     { "LOCAL4",         6, LOG_LOCAL4 },
87     { "LOCAL5",         6, LOG_LOCAL5 },
88     { "LOCAL6",         6, LOG_LOCAL6 },
89     { "LOCAL7",         6, LOG_LOCAL7 },
90     NULL
91
92 };
93
94 static int   cnvt_str2int();
95
96 #endif  /* LOG_LOCAL4 */
97
98 /*
99  * the server's name must be accessible from the daemon module,
100  * to construct the pid/args file names
101  */
102 char  *serverName = NULL;
103
104
105 static void
106 usage( char *name )
107 {
108         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
109 #ifdef LOG_LOCAL4
110     fprintf( stderr, " [-l sysloguser]" );
111 #endif
112     fprintf( stderr, "\n" );
113 }
114
115 int
116 main( int argc, char **argv )
117 {
118         int             i;
119         int             inetd = 0;
120         int             port;
121         Backend         *be = NULL;
122         FILE            *fp = NULL;
123 #ifdef LOG_LOCAL4
124     int     syslogUser = DEFAULT_SYSLOG_USER;
125 #endif
126
127         configfile = SLAPD_DEFAULT_CONFIGFILE;
128         port = LDAP_PORT;
129         g_argc = argc;
130         g_argv = argv;
131
132         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
133                 switch ( i ) {
134 #ifdef LDAP_DEBUG
135                 case 'd':       /* turn on debugging */
136                         if ( optarg[0] == '?' ) {
137                                 printf( "Debug levels:\n" );
138                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
139                                     LDAP_DEBUG_TRACE );
140                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
141                                     LDAP_DEBUG_PACKETS );
142                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
143                                     LDAP_DEBUG_ARGS );
144                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
145                                     LDAP_DEBUG_CONNS );
146                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
147                                     LDAP_DEBUG_BER );
148                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
149                                     LDAP_DEBUG_FILTER );
150                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
151                                     LDAP_DEBUG_CONFIG );
152                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
153                                     LDAP_DEBUG_ACL );
154                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
155                                     LDAP_DEBUG_STATS );
156                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
157                                     LDAP_DEBUG_STATS2 );
158                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
159                                     LDAP_DEBUG_SHELL );
160                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
161                                     LDAP_DEBUG_PARSE );
162                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
163                                     LDAP_DEBUG_ANY );
164                                 exit( 0 );
165                         } else {
166                                 slap_debug |= atoi( optarg );
167                         }
168                         break;
169 #else
170                 case 'd':       /* turn on debugging */
171                         fprintf( stderr,
172                             "must compile with LDAP_DEBUG for debugging\n" );
173                         break;
174 #endif
175
176                 case 'f':       /* read config file */
177                         configfile = ch_strdup( optarg );
178                         break;
179
180                 case 'i':       /* run from inetd */
181                         inetd = 1;
182                         break;
183
184                 case 'p':       /* port on which to listen */
185                         port = atoi( optarg );
186                         break;
187
188                 case 's':       /* set syslog level */
189                         ldap_syslog = atoi( optarg );
190                         break;
191
192 #ifdef LOG_LOCAL4
193
194                 case 'l':       /* set syslog local user */
195                         syslogUser = cnvt_str2int( optarg, syslog_types,
196                                            DEFAULT_SYSLOG_USER );
197                         break;
198
199 #endif
200
201                 case 'u':       /* do udp */
202                         udp = 1;
203                         break;
204
205                 default:
206                         usage( argv[0] );
207                         exit( 1 );
208                 }
209         }
210
211         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
212         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
213         ldif_debug = slap_debug;
214
215         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
216
217         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
218                 serverName = ch_strdup( argv[0] );
219         } else {
220                 serverName = ch_strdup( serverName + 1 );
221         }
222
223         if ( ! inetd ) {
224                 /* pre-open config file before detach in case it is a relative path */
225                 fp = fopen( configfile, "r" );
226 #ifdef LDAP_DEBUG
227                 lutil_detach( ldap_debug, 0 );
228 #else
229                 lutil_detach( 0, 0 );
230 #endif
231         }
232
233 #ifdef LOG_LOCAL4
234         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
235 #else
236         openlog( serverName, OPENLOG_OPTIONS );
237 #endif
238
239         init();
240         read_config( configfile, &be, fp );
241
242         if ( ! inetd ) {
243                 int             status;
244
245                 time( &starttime );
246
247                 if ( pthread_create( &listener_tid, NULL, slapd_daemon,
248                     (void *) port ) != 0 ) {
249                         Debug( LDAP_DEBUG_ANY,
250                             "listener pthread_create failed\n", 0, 0, 0 );
251                         exit( 1 );
252                 }
253
254 #ifdef HAVE_PHREADS_FINAL
255                 pthread_join( listener_tid, (void *) NULL );
256 #else
257                 pthread_join( listener_tid, (void *) &status );
258 #endif
259
260                 return 0;
261
262         } else {
263                 Connection              c;
264                 Operation               *o;
265                 BerElement              ber;
266                 unsigned long           len, tag;
267                 long                    msgid;
268                 int                     flen;
269                 struct sockaddr_in      from;
270                 struct hostent          *hp;
271
272                 c.c_dn = NULL;
273                 c.c_ops = NULL;
274                 c.c_sb.sb_sd = 0;
275                 c.c_sb.sb_options = 0;
276                 c.c_sb.sb_naddr = udp ? 1 : 0;
277                 c.c_sb.sb_ber.ber_buf = NULL;
278                 c.c_sb.sb_ber.ber_ptr = NULL;
279                 c.c_sb.sb_ber.ber_end = NULL;
280                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
281                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
282                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
283 #ifdef notdefcldap
284                 c.c_sb.sb_addrs = (void **) saddrlist;
285                 c.c_sb.sb_fromaddr = &faddr;
286                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
287 #endif
288                 flen = sizeof(from);
289                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
290 #ifdef SLAPD_RLOOKUPS
291                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
292                             sizeof(from.sin_addr.s_addr), AF_INET );
293 #else
294                         hp = NULL;
295 #endif
296
297                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
298                             hp == NULL ? "unknown" : hp->h_name,
299                             inet_ntoa( from.sin_addr ), 0 );
300
301                         c.c_addr = inet_ntoa( from.sin_addr );
302                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
303                 } else {
304                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
305                             0, 0, 0 );
306                 }
307
308                 ber_init_w_nullc( &ber, 0 );
309
310                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
311                     == LDAP_TAG_MESSAGE ) {
312                         pthread_mutex_lock( &currenttime_mutex );
313                         time( &currenttime );
314                         pthread_mutex_unlock( &currenttime_mutex );
315
316                         if ( (tag = ber_get_int( &ber, &msgid ))
317                             != LDAP_TAG_MSGID ) {
318                                 /* log and send error */
319                                 Debug( LDAP_DEBUG_ANY,
320                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
321                                 ber_free( &ber, 1 );
322                                 return 1;
323                         }
324
325                         if ( (tag = ber_peek_tag( &ber, &len ))
326                             == LBER_ERROR ) {
327                                 /* log, close and send error */
328                                 Debug( LDAP_DEBUG_ANY,
329                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
330                                 ber_free( &ber, 1 );
331                                 close( c.c_sb.sb_sd );
332                                 c.c_sb.sb_sd = -1;
333                                 return 1;
334                         }
335
336                         connection_activity( &c );
337
338                         ber_free( &ber, 1 );
339                 }
340         }
341         return 1;
342 }
343
344
345 #ifdef LOG_LOCAL4
346
347 /*
348  *  Convert a string to an integer by means of a dispatcher table
349  *  if the string is not in the table return the default
350  */
351
352 static int
353 cnvt_str2int (stringVal, dispatcher, defaultVal)
354 char      *stringVal;
355 STRDISP_P  dispatcher;
356 int        defaultVal;
357 {
358     int        retVal = defaultVal;
359     STRDISP_P  disp;
360
361     for (disp = dispatcher; disp->stringVal; disp++) {
362
363         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
364
365             retVal = disp->intVal;
366             break;
367
368         }
369     }
370
371     return (retVal);
372
373 } /* cnvt_str2int */
374
375 #endif  /* LOG_LOCAL4 */
376