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