]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
More header work toward draft-ietf-ldapext-ldap-c-api-01.
[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                 detach();
166         }
167 #ifdef LOG_LOCAL4
168         openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
169 #else
170         openlog( myname, OPENLOG_OPTIONS );
171 #endif
172
173         init();
174         read_config( configfile, &be, fp );
175
176         if ( ! inetd ) {
177                 pthread_attr_t  attr;
178                 int             status;
179
180                 time( &starttime );
181                 pthread_attr_init( &attr );
182                 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
183
184 #if !defined(HAVE_PTHREADS_D4) && !defined(HAVE_DCE)
185                 /* POSIX_THREADS or compatible
186                  * This is a draft 10 or standard pthreads implementation
187                  */
188                 if ( pthread_create( &listener_tid, &attr, slapd_daemon,
189                     (void *) port ) != 0 ) {
190                         Debug( LDAP_DEBUG_ANY,
191                             "listener pthread_create failed\n", 0, 0, 0 );
192                         exit( 1 );
193                 }
194 #else   /* draft4 */
195                 /*
196                  * This is a draft 4 or earlier pthreads implementation
197                  */
198                 if ( pthread_create( &listener_tid, attr, slapd_daemon,
199                     (void *) port ) != 0 ) {
200                         Debug( LDAP_DEBUG_ANY,
201                             "listener pthread_create failed\n", 0, 0, 0 );
202                         exit( 1 );
203                 }
204 #endif  /* !draft4 */
205                 pthread_attr_destroy( &attr );
206                 pthread_join( listener_tid, (void *) &status );
207                 pthread_exit( 0 );
208         } else {
209                 Connection              c;
210                 Operation               *o;
211                 BerElement              ber;
212                 unsigned long           len, tag;
213                 long                    msgid;
214                 int                     flen;
215                 struct sockaddr_in      from;
216                 struct hostent          *hp;
217
218                 c.c_dn = NULL;
219                 c.c_ops = NULL;
220                 c.c_sb.sb_sd = 0;
221                 c.c_sb.sb_options = 0;
222                 c.c_sb.sb_naddr = udp ? 1 : 0;
223                 c.c_sb.sb_ber.ber_buf = NULL;
224                 c.c_sb.sb_ber.ber_ptr = NULL;
225                 c.c_sb.sb_ber.ber_end = NULL;
226                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
227                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
228                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
229 #ifdef notdefcldap
230                 c.c_sb.sb_addrs = (void **) saddrlist;
231                 c.c_sb.sb_fromaddr = &faddr;
232                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
233 #endif
234                 flen = sizeof(from);
235                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
236 #ifdef SLAPD_RLOOKUPS
237                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
238                             sizeof(from.sin_addr.s_addr), AF_INET );
239 #else
240                         hp = NULL;
241 #endif
242
243                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
244                             hp == NULL ? "unknown" : hp->h_name,
245                             inet_ntoa( from.sin_addr ), 0 );
246
247                         c.c_addr = inet_ntoa( from.sin_addr );
248                         c.c_domain = strdup( hp == NULL ? "" : hp->h_name );
249                 } else {
250                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
251                             0, 0, 0 );
252                 }
253
254                 ber_init( &ber, 0 );
255                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
256                     == LDAP_TAG_MESSAGE ) {
257                         pthread_mutex_lock( &currenttime_mutex );
258                         time( &currenttime );
259                         pthread_mutex_unlock( &currenttime_mutex );
260
261                         if ( (tag = ber_get_int( &ber, &msgid ))
262                             != LDAP_TAG_MSGID ) {
263                                 /* log and send error */
264                                 Debug( LDAP_DEBUG_ANY,
265                                     "ber_get_int returns 0x%x\n", tag, 0, 0 );
266                                 return;
267                         }
268
269                         if ( (tag = ber_peek_tag( &ber, &len ))
270                             == LBER_ERROR ) {
271                                 /* log, close and send error */
272                                 Debug( LDAP_DEBUG_ANY,
273                                     "ber_peek_tag returns 0x%x\n", tag, 0, 0 );
274                                 ber_free( &ber, 1 );
275                                 close( c.c_sb.sb_sd );
276                                 c.c_sb.sb_sd = -1;
277                                 return;
278                         }
279
280                         connection_activity( &c );
281
282                         ber_free( &ber, 1 );
283                 }
284         }
285 }