]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
e93777db73085f6e6f8967771ebeed8912b3e81b
[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
9 #include "slap.h"
10 #include "ldapconfig.h"
11
12 extern void     slapd_daemon();
13 extern int      lber_debug;
14
15 extern char Versionstr[];
16
17
18 /*
19  * read-only global variables or variables only written by the listener
20  * thread (after they are initialized) - no need to protect them with a mutex.
21  */
22 int             ldap_debug = 0;
23 #ifdef LDAP_DEBUG
24 int             ldap_syslog = LDAP_DEBUG_STATS;
25 #else
26 int             ldap_syslog;
27 #endif
28 int             ldap_syslog_level = LOG_DEBUG;
29 int             udp;
30 int             slapd_shutdown;
31 char            *default_referral;
32 char            *configfile;
33 time_t          starttime;
34 pthread_t       listener_tid;
35 int             g_argc;
36 char            **g_argv;
37 /*
38  * global variables that need mutex protection
39  */
40 time_t          currenttime;
41 pthread_mutex_t currenttime_mutex;
42 int             active_threads;
43 pthread_mutex_t active_threads_mutex;
44 pthread_mutex_t new_conn_mutex;
45 long            ops_initiated;
46 long            ops_completed;
47 int             num_conns;
48 pthread_mutex_t ops_mutex;
49 long            num_entries_sent;
50 long            num_bytes_sent;
51 pthread_mutex_t num_sent_mutex;
52 /*
53  * these mutexes must be used when calling the entry2str()
54  * routine since it returns a pointer to static data.
55  */
56 pthread_mutex_t entry2str_mutex;
57 pthread_mutex_t replog_mutex;
58
59 static
60 usage( name )
61     char        *name;
62 {
63         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
64 }
65
66 main( argc, argv )
67     int         argc;
68     char        **argv;
69 {
70         int             i;
71         int             inetd = 0;
72         int             port;
73         char            *myname;
74         Backend         *be = NULL;
75         FILE            *fp = NULL;
76         extern char     *optarg;
77
78         configfile = SLAPD_DEFAULT_CONFIGFILE;
79         port = LDAP_PORT;
80         g_argc = argc;
81         g_argv = argv;
82
83         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
84                 switch ( i ) {
85 #ifdef LDAP_DEBUG
86                 case 'd':       /* turn on debugging */
87                         if ( optarg[0] == '?' ) {
88                                 printf( "Debug levels:\n" );
89                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
90                                     LDAP_DEBUG_TRACE );
91                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
92                                     LDAP_DEBUG_PACKETS );
93                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
94                                     LDAP_DEBUG_ARGS );
95                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
96                                     LDAP_DEBUG_CONNS );
97                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
98                                     LDAP_DEBUG_BER );
99                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
100                                     LDAP_DEBUG_FILTER );
101                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
102                                     LDAP_DEBUG_CONFIG );
103                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
104                                     LDAP_DEBUG_ACL );
105                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
106                                     LDAP_DEBUG_STATS );
107                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
108                                     LDAP_DEBUG_STATS2 );
109                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
110                                     LDAP_DEBUG_SHELL );
111                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
112                                     LDAP_DEBUG_PARSE );
113                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
114                                     LDAP_DEBUG_ANY );
115                                 exit( 0 );
116                         } else {
117                                 ldap_debug |= atoi( optarg );
118                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
119                         }
120                         break;
121 #else
122                 case 'd':       /* turn on debugging */
123                         fprintf( stderr,
124                             "must compile with LDAP_DEBUG for debugging\n" );
125                         break;
126 #endif
127
128                 case 'f':       /* read config file */
129                         configfile = strdup( optarg );
130                         break;
131
132                 case 'i':       /* run from inetd */
133                         inetd = 1;
134                         break;
135
136                 case 'p':       /* port on which to listen */
137                         port = atoi( optarg );
138                         break;
139
140                 case 's':       /* set syslog level */
141                         ldap_syslog = atoi( optarg );
142                         break;
143
144                 case 'u':       /* do udp */
145                         udp = 1;
146                         break;
147
148                 default:
149                         usage( argv[0] );
150                         exit( 1 );
151                 }
152         }
153
154         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
155
156         if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
157                 myname = strdup( argv[0] );
158         } else {
159                 myname = strdup( myname + 1 );
160         }
161
162         if ( ! inetd ) {
163                 /* pre-open config file before detach in case it is a relative path */
164                 fp = fopen( configfile, "r" );
165 #ifdef LDAP_DEBUG
166                 lutil_detach( ldap_debug, 0 );
167 #else
168                 lutil_detach( 0, 0 );
169 #endif
170         }
171 #ifdef LOG_LOCAL4
172         openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
173 #else
174         openlog( myname, OPENLOG_OPTIONS );
175 #endif
176
177         init();
178         read_config( configfile, &be, fp );
179
180         if ( ! inetd ) {
181                 pthread_attr_t  attr;
182                 int             status;
183
184                 time( &starttime );
185                 pthread_attr_init( &attr );
186                 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
187
188 #if !defined(HAVE_PTHREADS_D4) && !defined(HAVE_DCE)
189                 /* POSIX_THREADS or compatible
190                  * This is a draft 10 or standard pthreads implementation
191                  */
192                 if ( pthread_create( &listener_tid, &attr, slapd_daemon,
193                     (void *) port ) != 0 ) {
194                         Debug( LDAP_DEBUG_ANY,
195                             "listener pthread_create failed\n", 0, 0, 0 );
196                         exit( 1 );
197                 }
198 #else   /* draft4 */
199                 /*
200                  * This is a draft 4 or earlier pthreads implementation
201                  */
202                 if ( pthread_create( &listener_tid, attr, slapd_daemon,
203                     (void *) port ) != 0 ) {
204                         Debug( LDAP_DEBUG_ANY,
205                             "listener pthread_create failed\n", 0, 0, 0 );
206                         exit( 1 );
207                 }
208 #endif  /* !draft4 */
209                 pthread_attr_destroy( &attr );
210                 pthread_join( listener_tid, (void *) &status );
211                 pthread_exit( 0 );
212         } else {
213                 Connection              c;
214                 Operation               *o;
215                 BerElement              ber;
216                 unsigned long           len, tag;
217                 long                    msgid;
218                 int                     flen;
219                 struct sockaddr_in      from;
220                 struct hostent          *hp;
221
222                 c.c_dn = NULL;
223                 c.c_ops = NULL;
224                 c.c_sb.sb_sd = 0;
225                 c.c_sb.sb_options = 0;
226                 c.c_sb.sb_naddr = udp ? 1 : 0;
227                 c.c_sb.sb_ber.ber_buf = NULL;
228                 c.c_sb.sb_ber.ber_ptr = NULL;
229                 c.c_sb.sb_ber.ber_end = NULL;
230                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
231                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
232                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
233 #ifdef notdefcldap
234                 c.c_sb.sb_addrs = (void **) saddrlist;
235                 c.c_sb.sb_fromaddr = &faddr;
236                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
237 #endif
238                 flen = sizeof(from);
239                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
240 #ifdef SLAPD_RLOOKUPS
241                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
242                             sizeof(from.sin_addr.s_addr), AF_INET );
243 #else
244                         hp = NULL;
245 #endif
246
247                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
248                             hp == NULL ? "unknown" : hp->h_name,
249                             inet_ntoa( from.sin_addr ), 0 );
250
251                         c.c_addr = inet_ntoa( from.sin_addr );
252                         c.c_domain = strdup( hp == NULL ? "" : hp->h_name );
253                 } else {
254                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
255                             0, 0, 0 );
256                 }
257
258                 ber_init( &ber, 0 );
259                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
260                     == LDAP_TAG_MESSAGE ) {
261                         pthread_mutex_lock( &currenttime_mutex );
262                         time( &currenttime );
263                         pthread_mutex_unlock( &currenttime_mutex );
264
265                         if ( (tag = ber_get_int( &ber, &msgid ))
266                             != LDAP_TAG_MSGID ) {
267                                 /* log and send error */
268                                 Debug( LDAP_DEBUG_ANY,
269                                     "ber_get_int returns 0x%x\n", tag, 0, 0 );
270                                 return;
271                         }
272
273                         if ( (tag = ber_peek_tag( &ber, &len ))
274                             == LBER_ERROR ) {
275                                 /* log, close and send error */
276                                 Debug( LDAP_DEBUG_ANY,
277                                     "ber_peek_tag returns 0x%x\n", tag, 0, 0 );
278                                 ber_free( &ber, 1 );
279                                 close( c.c_sb.sb_sd );
280                                 c.c_sb.sb_sd = -1;
281                                 return;
282                         }
283
284                         connection_activity( &c );
285
286                         ber_free( &ber, 1 );
287                 }
288         }
289 }