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