]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
c08bb79551b4c185e2aaf3925b3b508a79d2bbaf
[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  * 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
26         char    *stringVal;
27         int      abbr;
28         int      intVal;
29
30 } STRDISP, *STRDISP_P;
31
32
33 /* table to compute syslog-options to integer */
34 static STRDISP  syslog_types[] = {
35
36     { "LOCAL0",         6, LOG_LOCAL0 },
37     { "LOCAL1",         6, LOG_LOCAL1 },
38     { "LOCAL2",         6, LOG_LOCAL2 },
39     { "LOCAL3",         6, LOG_LOCAL3 },
40     { "LOCAL4",         6, LOG_LOCAL4 },
41     { "LOCAL5",         6, LOG_LOCAL5 },
42     { "LOCAL6",         6, LOG_LOCAL6 },
43     { "LOCAL7",         6, LOG_LOCAL7 },
44     NULL
45
46 };
47
48 static int   cnvt_str2int();
49
50 #endif  /* LOG_LOCAL4 */
51
52 extern int listener_running;
53
54
55 static void
56 usage( char *name )
57 {
58         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
59 #ifdef LOG_LOCAL4
60     fprintf( stderr, " [-l sysloguser]" );
61 #endif
62     fprintf( stderr, "\n" );
63 }
64
65 int
66 main( int argc, char **argv )
67 {
68         int             i;
69         int             inetd = 0;
70         int             port;
71         int             udp;
72         Backend         *be = NULL;
73         FILE            *fp = NULL;
74 #ifdef LOG_LOCAL4
75     int     syslogUser = DEFAULT_SYSLOG_USER;
76 #endif
77         char            *configfile;
78         char        *serverName;
79
80         configfile = SLAPD_DEFAULT_CONFIGFILE;
81         port = LDAP_PORT;
82         g_argc = argc;
83         g_argv = argv;
84
85         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
86                 switch ( i ) {
87 #ifdef LDAP_DEBUG
88                 case 'd':       /* turn on debugging */
89                         if ( optarg[0] == '?' ) {
90                                 printf( "Debug levels:\n" );
91                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
92                                     LDAP_DEBUG_TRACE );
93                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
94                                     LDAP_DEBUG_PACKETS );
95                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
96                                     LDAP_DEBUG_ARGS );
97                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
98                                     LDAP_DEBUG_CONNS );
99                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
100                                     LDAP_DEBUG_BER );
101                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
102                                     LDAP_DEBUG_FILTER );
103                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
104                                     LDAP_DEBUG_CONFIG );
105                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
106                                     LDAP_DEBUG_ACL );
107                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
108                                     LDAP_DEBUG_STATS );
109                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
110                                     LDAP_DEBUG_STATS2 );
111                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
112                                     LDAP_DEBUG_SHELL );
113                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
114                                     LDAP_DEBUG_PARSE );
115                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
116                                     LDAP_DEBUG_ANY );
117                                 exit( 0 );
118                         } else {
119                                 slap_debug |= atoi( optarg );
120                         }
121                         break;
122 #else
123                 case 'd':       /* turn on debugging */
124                         fprintf( stderr,
125                             "must compile with LDAP_DEBUG for debugging\n" );
126                         break;
127 #endif
128
129                 case 'f':       /* read config file */
130                         configfile = ch_strdup( optarg );
131                         break;
132
133                 case 'i':       /* run from inetd */
134                         inetd = 1;
135                         break;
136
137                 case 'p':       /* port on which to listen */
138                         port = atoi( optarg );
139                         break;
140
141                 case 's':       /* set syslog level */
142                         ldap_syslog = atoi( optarg );
143                         break;
144
145 #ifdef LOG_LOCAL4
146
147                 case 'l':       /* set syslog local user */
148                         syslogUser = cnvt_str2int( optarg, syslog_types,
149                                            DEFAULT_SYSLOG_USER );
150                         break;
151
152 #endif
153
154                 case 'u':       /* do udp */
155                         udp = 1;
156                         break;
157
158                 default:
159                         usage( argv[0] );
160                         exit( 1 );
161                 }
162         }
163
164         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
165         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
166         ldif_debug = slap_debug;
167
168         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
169
170         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
171                 serverName = ch_strdup( argv[0] );
172         } else {
173                 serverName = ch_strdup( serverName + 1 );
174         }
175
176         if ( ! inetd ) {
177                 /* pre-open config file before detach in case it is a relative path */
178                 fp = fopen( configfile, "r" );
179 #ifdef LDAP_DEBUG
180                 lutil_detach( ldap_debug, 0 );
181 #else
182                 lutil_detach( 0, 0 );
183 #endif
184         }
185
186 #ifdef LOG_LOCAL4
187         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
188 #else
189         openlog( serverName, OPENLOG_OPTIONS );
190 #endif
191
192 #ifdef SLAPD_BDB2
193         bdb2i_do_timing = 1;
194 #endif
195
196         init();
197         read_config_env( configfile, &be, fp, 1 );
198
199         if ( ! inetd ) {
200                 int             status;
201
202                 time( &starttime );
203
204                 if ( status = pthread_create( &listener_tid, NULL,
205                         slapd_daemon, (void *) port ) != 0 )
206                 {
207                         Debug( LDAP_DEBUG_ANY,
208                             "listener pthread_create failed (%d)\n", status, 0, 0 );
209                         exit( 1 );
210                 }
211
212                 /*  We must prevent the Father of All Threads to terminate
213             before the listener thread has done it's clean-up work
214             (that's not always garuanteed when using LINUX kernel
215              threads :-( ) So we have to withhold the Father, until
216              the listener says OK
217         */
218                 while ( listener_running ) {
219                         ldap_pvt_thread_join( listener_tid, (void *) NULL );
220                 }
221
222                 return 0;
223
224         } else {
225                 Connection              c;
226                 Operation               *o;
227                 BerElement              ber;
228                 unsigned long           len, tag;
229                 long                    msgid;
230                 int                     flen;
231                 struct sockaddr_in      from;
232                 struct hostent          *hp;
233
234                 c.c_dn = NULL;
235                 c.c_cdn = NULL;
236                 c.c_ops = NULL;
237                 c.c_sb.sb_sd = 0;
238                 c.c_sb.sb_options = 0;
239                 c.c_sb.sb_naddr = udp ? 1 : 0;
240                 c.c_sb.sb_ber.ber_buf = NULL;
241                 c.c_sb.sb_ber.ber_ptr = NULL;
242                 c.c_sb.sb_ber.ber_end = NULL;
243                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
244                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
245                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
246 #ifdef notdefcldap
247                 c.c_sb.sb_addrs = (void **) saddrlist;
248                 c.c_sb.sb_fromaddr = &faddr;
249                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
250 #endif
251                 flen = sizeof(from);
252                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
253 #ifdef SLAPD_RLOOKUPS
254                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
255                             sizeof(from.sin_addr.s_addr), AF_INET );
256 #else
257                         hp = NULL;
258 #endif
259
260                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
261                             hp == NULL ? "unknown" : hp->h_name,
262                             inet_ntoa( from.sin_addr ), 0 );
263
264                         c.c_addr = inet_ntoa( from.sin_addr );
265                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
266                 } else {
267                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
268                             0, 0, 0 );
269                 }
270
271                 ber_init_w_nullc( &ber, 0 );
272
273                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
274                     == LDAP_TAG_MESSAGE ) {
275                         pthread_mutex_lock( &currenttime_mutex );
276                         time( &currenttime );
277                         pthread_mutex_unlock( &currenttime_mutex );
278
279                         if ( (tag = ber_get_int( &ber, &msgid ))
280                             != LDAP_TAG_MSGID ) {
281                                 /* log and send error */
282                                 Debug( LDAP_DEBUG_ANY,
283                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
284                                 ber_free( &ber, 1 );
285                                 return 1;
286                         }
287
288                         if ( (tag = ber_peek_tag( &ber, &len ))
289                             == LBER_ERROR ) {
290                                 /* log, close and send error */
291                                 Debug( LDAP_DEBUG_ANY,
292                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
293                                 ber_free( &ber, 1 );
294                                 close( c.c_sb.sb_sd );
295                                 c.c_sb.sb_sd = -1;
296                                 return 1;
297                         }
298
299                         connection_activity( &c );
300
301                         ber_free( &ber, 1 );
302                 }
303         }
304         return 1;
305 }
306
307
308 #ifdef LOG_LOCAL4
309
310 /*
311  *  Convert a string to an integer by means of a dispatcher table
312  *  if the string is not in the table return the default
313  */
314
315 static int
316 cnvt_str2int (stringVal, dispatcher, defaultVal)
317 char      *stringVal;
318 STRDISP_P  dispatcher;
319 int        defaultVal;
320 {
321     int        retVal = defaultVal;
322     STRDISP_P  disp;
323
324     for (disp = dispatcher; disp->stringVal; disp++) {
325
326         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
327
328             retVal = disp->intVal;
329             break;
330
331         }
332     }
333
334     return (retVal);
335
336 } /* cnvt_str2int */
337
338 #endif  /* LOG_LOCAL4 */
339