]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Support attribute types list and only parameters on cn=config
[openldap] / servers / slapd / daemon.c
1
2 /* Revision history
3  *
4  * 5-Jun-96     hodges
5  *      Added locking of new_conn_mutex when traversing the c[] array.
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/signal.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
17 #include <ac/time.h>
18 #include <ac/unistd.h>
19
20 #include "ldapconfig.h"
21 #include "slap.h"
22
23 #ifdef HAVE_SYS_FILIO_H
24 #include <sys/filio.h>
25 #elif HAVE_SYS_IOCTL_H
26 #include <sys/ioctl.h>
27 #endif
28
29 #ifdef HAVE_TCPD
30 #include <tcpd.h>
31
32 int allow_severity = LOG_INFO;
33 int deny_severity = LOG_NOTICE;
34 #endif /* TCP Wrappers */
35
36 int             dtblsize;
37 Connection      *c;
38
39 static int slapd_shutdown = 0;
40
41 /* a link to the slapd.conf configuration parameters */
42 extern char *slapd_pid_file;
43 extern char *slapd_args_file;
44
45 void *
46 slapd_daemon(
47     void *port
48 )
49 {
50         Operation               *o;
51         BerElement              ber;
52         unsigned long           len, tag, msgid;
53         int                     i;
54         int                     tcps, ns;
55         struct sockaddr_in      addr;
56         fd_set                  readfds;
57         fd_set                  writefds;
58         FILE                    *fp;
59         int                     on = 1;
60
61 #ifdef HAVE_SYSCONF
62         dtblsize = sysconf( _SC_OPEN_MAX );
63 #elif HAVE_GETDTABLESIZE
64         dtblsize = getdtablesize();
65 #else
66         dtblsize = FD_SETSIZE
67 #endif
68
69 #ifdef FD_SETSIZE
70         if(dtblsize > FD_SETSIZE) {
71                 dtblsize = FD_SETSIZE;
72         }
73 #endif  /* !FD_SETSIZE */
74
75         c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
76
77         for ( i = 0; i < dtblsize; i++ ) {
78                 c[i].c_dn = NULL;
79                 c[i].c_cdn = NULL;
80                 c[i].c_addr = NULL;
81                 c[i].c_domain = NULL;
82                 c[i].c_ops = NULL;
83                 c[i].c_sb.sb_sd = -1;
84                 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
85                 c[i].c_sb.sb_naddr = 0;
86                 c[i].c_sb.sb_ber.ber_buf = NULL;
87                 c[i].c_sb.sb_ber.ber_ptr = NULL;
88                 c[i].c_sb.sb_ber.ber_end = NULL;
89                 c[i].c_writewaiter = 0;
90                 c[i].c_connid = 0;
91                 ldap_pvt_thread_mutex_init( &c[i].c_dnmutex );
92                 ldap_pvt_thread_mutex_init( &c[i].c_opsmutex );
93                 ldap_pvt_thread_mutex_init( &c[i].c_pdumutex );
94                 ldap_pvt_thread_cond_init( &c[i].c_wcv );
95         }
96
97         if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
98                 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
99                     errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
100                     "unknown", 0 );
101                 exit( 1 );
102         }
103
104         i = 1;
105         if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
106             sizeof(i) ) == -1 ) {
107                 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
108                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
109                     "unknown", 0 );
110         }
111
112         (void) memset( (void *) &addr, '\0', sizeof(addr) );
113         addr.sin_family = AF_INET;
114         addr.sin_addr.s_addr = INADDR_ANY;
115         addr.sin_port = htons( (int)port );
116         if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
117                 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
118                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
119                     "unknown", 0 );
120                 exit( 1 );
121         }
122
123         if ( listen( tcps, 5 ) == -1 ) {
124                 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
125                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
126                     "unknown", 0 );
127                 exit( 1 );
128         }
129
130         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
131
132         if (( slapd_pid_file != NULL ) &&
133                         (( fp = fopen( slapd_pid_file, "w" )) != NULL )) {
134                 fprintf( fp, "%d\n", (int) getpid() );
135                 fclose( fp );
136         }
137
138         if (( slapd_args_file != NULL ) &&
139                         (( fp = fopen( slapd_args_file, "w" )) != NULL )) {
140                 for ( i = 0; i < g_argc; i++ ) {
141                         fprintf( fp, "%s ", g_argv[i] );
142                 }
143                 fprintf( fp, "\n" );
144                 fclose( fp );
145         }
146
147         while ( !slapd_shutdown ) {
148                 struct sockaddr_in      from;
149                 struct hostent          *hp;
150                 struct timeval          zero;
151                 struct timeval          *tvp;
152                 int                     len, pid;
153
154                 char    *client_name;
155                 char    *client_addr;
156
157                 FD_ZERO( &writefds );
158                 FD_ZERO( &readfds );
159                 FD_SET( tcps, &readfds );
160
161                 zero.tv_sec = 0;
162                 zero.tv_usec = 0;
163
164                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
165                 Debug( LDAP_DEBUG_CONNS,
166                     "listening for connections on %d, activity on:",
167                     tcps, 0, 0 );
168
169                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
170                 for ( i = 0; i < dtblsize; i++ ) {
171                         if ( c[i].c_sb.sb_sd != -1 ) {
172                                 FD_SET( c[i].c_sb.sb_sd, &readfds );
173
174                                 if ( c[i].c_writewaiter ) {
175                                         FD_SET( c[i].c_sb.sb_sd, &writefds );
176                                 }
177                                 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
178                                     c[i].c_writewaiter ? "w" : "", 0 );
179                         }
180                 }
181                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
182                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
183
184                 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
185                     active_threads, 0, 0 );
186 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
187                 tvp = NULL;
188 #else
189                 tvp = active_threads ? &zero : NULL;
190 #endif
191                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
192
193                 switch ( i = select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
194                 case -1:        /* failure - try again */
195                         Debug( LDAP_DEBUG_CONNS,
196                             "select failed errno %d (%s)\n",
197                             errno, errno > -1 && errno < sys_nerr ?
198                             sys_errlist[errno] : "unknown", 0 );
199                         continue;
200
201                 case 0:         /* timeout - let threads run */
202                         Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
203                             0, 0, 0 );
204                         ldap_pvt_thread_yield();
205                         continue;
206
207                 default:        /* something happened - deal with it */
208                         Debug( LDAP_DEBUG_CONNS, "select activity on %d descriptors\n", i, 0, 0 );
209                         ;       /* FALL */
210                 }
211                 ldap_pvt_thread_mutex_lock( &currenttime_mutex );
212                 time( &currenttime );
213                 ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
214
215                 /* new connection */
216                 ldap_pvt_thread_mutex_lock( &new_conn_mutex );
217                 if ( FD_ISSET( tcps, &readfds ) ) {
218                         len = sizeof(from);
219                         if ( (ns = accept( tcps, (struct sockaddr *) &from,
220                             &len )) == -1 ) {
221                                 Debug( LDAP_DEBUG_ANY,
222                                     "accept() failed errno %d (%s)", errno,
223                                     errno > -1 && errno < sys_nerr ?
224                                     sys_errlist[errno] : "unknown", 0 );
225                                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
226                                 continue;
227                         }
228                         if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
229                                 Debug( LDAP_DEBUG_ANY,
230                                     "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
231                         }
232
233                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
234                             0, 0 );
235
236                         len = sizeof(from);
237
238                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
239                             == 0 ) {
240                                 char *s;
241                                 client_addr = inet_ntoa( from.sin_addr );
242
243 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
244                                 hp = gethostbyaddr( (char *)
245                                     &(from.sin_addr.s_addr),
246                                     sizeof(from.sin_addr.s_addr), AF_INET );
247
248                                 if(hp) {
249                                         client_name = hp->h_name;
250
251                                         /* normalize the domain */
252                                         for ( s = client_name; *s; s++ ) {
253                                                 *s = TOLOWER( (unsigned char) *s );
254                                         }
255
256                                 } else {
257                                         client_name = NULL;
258                                 }
259 #else
260                                 client_name = NULL;
261 #endif
262
263                         } else {
264                                 client_name = NULL;;
265                                 client_addr = NULL;
266                         }
267
268 #ifdef HAVE_TCPD
269                         if(!hosts_ctl("slapd",
270                                 client_name != NULL ? client_name : STRING_UNKNOWN,
271                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
272                                 STRING_UNKNOWN))
273                         {
274                                 /* DENY ACCESS */
275                                 Statslog( LDAP_DEBUG_ANY,
276                                  "fd=%d connection from %s (%s) denied.\n",
277                                         ns,
278                                                 client_name == NULL ? "unknown" : client_name,
279                                                 client_addr == NULL ? "unknown" : client_addr,
280                                   0, 0 );
281
282                                 close(ns);
283                                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
284                                 continue;
285                         }
286 #endif /* HAVE_TCPD */
287
288                         c[ns].c_sb.sb_sd = ns;
289                         ldap_pvt_thread_mutex_lock( &ops_mutex );
290                         c[ns].c_connid = num_conns++;
291                         ldap_pvt_thread_mutex_unlock( &ops_mutex );
292
293                         Statslog( LDAP_DEBUG_STATS,
294                             "conn=%d fd=%d connection from %s (%s) accepted.\n",
295                                 c[ns].c_connid, ns,
296                                         client_name == NULL ? "unknown" : client_name,
297                                         client_addr == NULL ? "unknown" : client_addr,
298                              0 );
299
300                         if ( c[ns].c_addr != NULL ) {
301                                 free( c[ns].c_addr );
302                         }
303                         c[ns].c_addr = ch_strdup( client_addr );
304
305                         if ( c[ns].c_domain != NULL ) {
306                                 free( c[ns].c_domain );
307                         }
308
309                         c[ns].c_domain = ch_strdup( client_name == NULL
310                                 ? "" : client_name );
311
312                         ldap_pvt_thread_mutex_lock( &c[ns].c_dnmutex );
313                         if ( c[ns].c_dn != NULL ) {
314                                 free( c[ns].c_dn );
315                                 c[ns].c_dn = NULL;
316                         }
317                         if ( c[ns].c_cdn != NULL ) {
318                                 free( c[ns].c_cdn );
319                                 c[ns].c_cdn = NULL;
320                         }
321                         ldap_pvt_thread_mutex_unlock( &c[ns].c_dnmutex );
322                         c[ns].c_starttime = currenttime;
323                         c[ns].c_opsinitiated = 0;
324                         c[ns].c_opscompleted = 0;
325                 }
326                 ldap_pvt_thread_mutex_unlock( &new_conn_mutex );
327
328                 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
329                 for ( i = 0; i < dtblsize; i++ ) {
330                         int     r, w;
331
332                         r = FD_ISSET( i, &readfds );
333                         w = FD_ISSET( i, &writefds );
334                         if ( i != tcps && (r || w) ) {
335                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
336                                     r ? "r" : "", w ? "w" : "" );
337                         }
338                 }
339                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
340
341                 for ( i = 0; i < dtblsize; i++ ) {
342                         if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
343                             ! FD_ISSET( i, &writefds )) ) {
344                                 continue;
345                         }
346
347                         if ( FD_ISSET( i, &writefds ) ) {
348                                 Debug( LDAP_DEBUG_CONNS,
349                                     "signaling write waiter on %d\n", i, 0, 0 );
350
351                                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
352                                 active_threads++;
353                                 c[i].c_writewaiter = 0;
354                                 ldap_pvt_thread_cond_signal( &c[i].c_wcv );
355                                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
356                         }
357
358                         if ( FD_ISSET( i, &readfds ) ) {
359                                 Debug( LDAP_DEBUG_CONNS,
360                                     "read activity on %d\n", i, 0, 0 );
361
362                                 connection_activity( &c[i] );
363                         }
364                 }
365
366                 ldap_pvt_thread_yield();
367         }
368
369         close( tcps );
370
371         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
372         Debug( LDAP_DEBUG_ANY,
373             "slapd shutting down - waiting for %d threads to terminate\n",
374             active_threads, 0, 0 );
375         while ( active_threads > 0 ) {
376                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
377         }
378         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
379
380         /* let backends do whatever cleanup they need to do */
381         Debug( LDAP_DEBUG_TRACE,
382             "slapd shutting down - waiting for backends to close down\n", 0, 0,
383             0 );
384         be_close();
385         Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
386
387         return NULL;
388 }
389
390 void
391 slap_set_shutdown( int sig )
392 {
393         slapd_shutdown = 1;
394         ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
395
396         /* reinstall self */
397         (void) SIGNAL( sig, slap_set_shutdown );
398 }
399
400 void
401 slap_do_nothing( int sig )
402 {
403         /* reinstall self */
404         (void) SIGNAL( sig, slap_do_nothing );
405 }