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