]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Changed FD_SETSIZE checks for consistency. Added checks where needed.
[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 #ifndef sunos5
60 pthread_mutex_t regex_mutex;
61 #endif
62
63 static
64 usage( name )
65     char        *name;
66 {
67         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
68 }
69
70 main( argc, argv )
71     int         argc;
72     char        **argv;
73 {
74         int             i;
75         int             inetd = 0;
76         int             port;
77         char            *myname;
78         Backend         *be = NULL;
79         FILE            *fp = NULL;
80         extern char     *optarg;
81
82         configfile = SLAPD_DEFAULT_CONFIGFILE;
83         port = LDAP_PORT;
84         g_argc = argc;
85         g_argv = argv;
86
87         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
88                 switch ( i ) {
89 #ifdef LDAP_DEBUG
90                 case 'd':       /* turn on debugging */
91                         if ( optarg[0] == '?' ) {
92                                 printf( "Debug levels:\n" );
93                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
94                                     LDAP_DEBUG_TRACE );
95                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
96                                     LDAP_DEBUG_PACKETS );
97                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
98                                     LDAP_DEBUG_ARGS );
99                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
100                                     LDAP_DEBUG_CONNS );
101                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
102                                     LDAP_DEBUG_BER );
103                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
104                                     LDAP_DEBUG_FILTER );
105                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
106                                     LDAP_DEBUG_CONFIG );
107                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
108                                     LDAP_DEBUG_ACL );
109                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
110                                     LDAP_DEBUG_STATS );
111                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
112                                     LDAP_DEBUG_STATS2 );
113                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
114                                     LDAP_DEBUG_SHELL );
115                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
116                                     LDAP_DEBUG_PARSE );
117                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
118                                     LDAP_DEBUG_ANY );
119                                 exit( 0 );
120                         } else {
121                                 ldap_debug |= atoi( optarg );
122                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
123                         }
124                         break;
125 #else
126                 case 'd':       /* turn on debugging */
127                         fprintf( stderr,
128                             "must compile with LDAP_DEBUG for debugging\n" );
129                         break;
130 #endif
131
132                 case 'f':       /* read config file */
133                         configfile = strdup( optarg );
134                         break;
135
136                 case 'i':       /* run from inetd */
137                         inetd = 1;
138                         break;
139
140                 case 'p':       /* port on which to listen */
141                         port = atoi( optarg );
142                         break;
143
144                 case 's':       /* set syslog level */
145                         ldap_syslog = atoi( optarg );
146                         break;
147
148                 case 'u':       /* do udp */
149                         udp = 1;
150                         break;
151
152                 default:
153                         usage( argv[0] );
154                         exit( 1 );
155                 }
156         }
157
158         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
159
160         if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
161                 myname = strdup( argv[0] );
162         } else {
163                 myname = strdup( myname + 1 );
164         }
165
166         if ( ! inetd ) {
167                 /* pre-open config file before detach in case it is a relative path */
168                 fp = fopen( configfile, "r" );
169                 detach();
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 #ifndef THREAD_MIT_PTHREADS
189                 /* POSIX_THREADS or compatible
190                  * This is a draft 10 or standard pthreads implementation
191                  */
192                 if ( pthread_create( &listener_tid, &attr, (void *) 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   /* !THREAD_MIT_PTHREADS */
199                 /*
200                  * This is a draft 4 or earlier pthreads implementation
201                  */
202                 if ( pthread_create( &listener_tid, attr, (void *) 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  /* !THREAD_MIT_PTHREADS */
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 REVERSE_LOOKUP
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 }