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