]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Suck in changes from HEAD
[openldap] / servers / slapd / daemon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/ctype.h>
12 #include <ac/errno.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16 #include <ac/unistd.h>
17
18 #include "ldap_pvt.h"
19 #include "ldap_pvt_thread.h"
20 #include "lutil.h"
21 #include "slap.h"
22
23 #ifdef HAVE_TCPD
24 #include <tcpd.h>
25
26 int allow_severity = LOG_INFO;
27 int deny_severity = LOG_NOTICE;
28 #endif /* TCP Wrappers */
29
30 #ifdef LDAP_PF_LOCAL
31 #include <sys/stat.h>
32 /* this should go in <ldap.h> as soon as it is accepted */
33 #define LDAPI_MOD_URLEXT                "x-mod"
34 #endif /* LDAP_PF_LOCAL */
35
36 int slap_inet4or6 = AF_UNSPEC;
37
38 /* globals */
39 time_t starttime;
40 ber_socket_t dtblsize;
41
42 Listener **slap_listeners = NULL;
43
44 #define SLAPD_LISTEN 10
45
46 static ber_socket_t wake_sds[2];
47
48 #ifdef NO_THREADS
49 static int waking;
50 #define WAKE_LISTENER(w) \
51 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
52 #else
53 #define WAKE_LISTENER(w) \
54 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
55 #endif
56
57 #ifndef HAVE_WINSOCK
58 static
59 #endif
60 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
61
62 static struct slap_daemon {
63         ldap_pvt_thread_mutex_t sd_mutex;
64
65         ber_socket_t sd_nactives;
66
67 #ifndef HAVE_WINSOCK
68         /* In winsock, accept() returns values higher than dtblsize
69                 so don't bother with this optimization */
70         int sd_nfds;
71 #endif
72
73         fd_set sd_actives;
74         fd_set sd_readers;
75         fd_set sd_writers;
76 } slap_daemon;
77
78
79
80 #ifdef HAVE_SLP
81 /*
82  * SLP related functions
83  */
84 #include <slp.h>
85
86 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
87 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
88 static char** slapd_srvurls = NULL;
89 static SLPHandle slapd_hslp = 0;
90
91 void slapd_slp_init( const char* urls ) {
92         int i;
93
94         slapd_srvurls = str2charray( urls, " " );
95
96         if( slapd_srvurls == NULL ) return;
97
98         /* find and expand INADDR_ANY URLs */
99         for( i=0; slapd_srvurls[i] != NULL; i++ ) {
100                 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
101                         char *host = ldap_pvt_get_fqdn( NULL );
102                         if ( host != NULL ) {
103                                 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
104                                         strlen( host ) +
105                                         sizeof( LDAP_SRVTYPE_PREFIX ) );
106                                 strcpy( slap_strcopy(slapd_srvurls[i],
107                                         LDAP_SRVTYPE_PREFIX ), host );
108
109                                 ch_free( host );
110                         }
111
112                 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
113                         char *host = ldap_pvt_get_fqdn( NULL );
114                         if ( host != NULL ) {
115                                 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
116                                         strlen( host ) +
117                                         sizeof( LDAPS_SRVTYPE_PREFIX ) );
118                                 strcpy( slap_strcopy(slapd_srvurls[i],
119                                         LDAPS_SRVTYPE_PREFIX ), host );
120
121                                 ch_free( host );
122                         }
123                 }
124         }
125
126         /* open the SLP handle */
127         SLPOpen( "en", 0, &slapd_hslp );
128 }
129
130 void slapd_slp_deinit() {
131         if( slapd_srvurls == NULL ) return;
132
133         charray_free( slapd_srvurls );
134         slapd_srvurls = NULL;
135
136         /* close the SLP handle */
137         SLPClose( slapd_hslp );
138 }
139
140 void slapd_slp_regreport(
141         SLPHandle hslp,
142         SLPError errcode,
143         void* cookie )
144 {
145         /* empty report */
146 }
147
148 void slapd_slp_reg() {
149         int i;
150
151         for( i=0; slapd_srvurls[i] != NULL; i++ ) {
152                 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
153                                 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
154                     strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
155                                 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
156                 {
157                         SLPReg( slapd_hslp,
158                                 slapd_srvurls[i],
159                                 SLP_LIFETIME_MAXIMUM,
160                                 "ldap",
161                                 "",
162                                 1,
163                                 slapd_slp_regreport,
164                                 NULL );
165                 }
166         }
167 }
168
169 void slapd_slp_dereg() {
170         int i;
171
172         for( i=0; slapd_srvurls[i] != NULL; i++ ) {
173                 SLPDereg( slapd_hslp,
174                         slapd_srvurls[i],
175                         slapd_slp_regreport,
176                         NULL );
177         }
178 }
179 #endif /* HAVE_SLP */
180
181 /*
182  * Add a descriptor to daemon control
183  */
184 static void slapd_add(ber_socket_t s) {
185         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
186
187         assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
188         assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
189         assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
190
191 #ifndef HAVE_WINSOCK
192         if (s >= slap_daemon.sd_nfds) {
193                 slap_daemon.sd_nfds = s + 1;
194         }
195 #endif
196
197         slap_daemon.sd_nactives++;
198
199         FD_SET( s, &slap_daemon.sd_actives );
200         FD_SET( s, &slap_daemon.sd_readers );
201
202 #ifdef NEW_LOGGING
203         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
204                    "slapd_add: added %ld%s%s\n",
205                    (long)s,
206                    FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
207                    FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
208 #else
209         Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
210                 (long) s,
211             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
212                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
213 #endif
214         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
215 }
216
217 /*
218  * Remove the descriptor from daemon control
219  */
220 void slapd_remove(ber_socket_t s, int wake) {
221         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
222
223         slap_daemon.sd_nactives--;
224
225 #ifdef NEW_LOGGING
226         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
227                    "slapd_remove: removing %ld%s%s\n",
228                    (long) s,
229                    FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
230                    FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : ""  ));
231 #else
232         Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
233                 (long) s,
234             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
235                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
236 #endif
237         FD_CLR( s, &slap_daemon.sd_actives );
238         FD_CLR( s, &slap_daemon.sd_readers );
239         FD_CLR( s, &slap_daemon.sd_writers );
240
241         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
242         WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
243 }
244
245 void slapd_clr_write(ber_socket_t s, int wake) {
246         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
247
248         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
249         FD_CLR( s, &slap_daemon.sd_writers );
250
251         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
252         WAKE_LISTENER(wake);
253 }
254
255 void slapd_set_write(ber_socket_t s, int wake) {
256         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
257
258         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
259         if (!FD_ISSET(s, &slap_daemon.sd_writers))
260             FD_SET( (unsigned) s, &slap_daemon.sd_writers );
261
262         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
263         WAKE_LISTENER(wake);
264 }
265
266 void slapd_clr_read(ber_socket_t s, int wake) {
267         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
268
269         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
270         FD_CLR( s, &slap_daemon.sd_readers );
271
272         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
273         WAKE_LISTENER(wake);
274 }
275
276 void slapd_set_read(ber_socket_t s, int wake) {
277         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
278
279         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
280         if (!FD_ISSET(s, &slap_daemon.sd_readers))
281             FD_SET( s, &slap_daemon.sd_readers );
282
283         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
284         WAKE_LISTENER(wake);
285 }
286
287 static void slapd_close(ber_socket_t s) {
288 #ifdef NEW_LOGGING
289         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
290                    "slapd_close: closing %ld\n", (long)s ));
291 #else
292         Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
293                 (long) s, 0, 0 );
294 #endif
295         tcp_close(s);
296 }
297
298 static void slap_free_listener_addresses(struct sockaddr **sal)
299 {
300         struct sockaddr **sap;
301
302         if (sal == NULL) {
303                 return;
304         }
305
306         for (sap = sal; *sap != NULL; sap++) {
307                 ch_free(*sap);
308         }
309
310         ch_free(sal);
311 }
312
313 #ifdef LDAP_PF_LOCAL
314 static int get_url_perms(
315         char    **exts,
316         mode_t  *perms,
317         int     *crit )
318 {
319         int     i;
320
321         assert( exts );
322         assert( perms );
323         assert( crit );
324
325         *crit = 0;
326         for ( i = 0; exts[ i ]; i++ ) {
327                 char    *type = exts[ i ];
328                 int     c = 0;
329
330                 if ( type[ 0 ] == '!' ) {
331                         c = 1;
332                         type++;
333                 }
334
335                 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
336                         char    *value = type + sizeof(LDAPI_MOD_URLEXT "=") - 1;
337                         mode_t  p = 0;
338                         int     j;
339
340                         if ( strlen(value) != 3 ) {
341                                 return LDAP_OTHER;
342                         } 
343
344                         for ( j = 0; j < 3; j++ ) {
345                                 static mode_t   m[ 3 ] 
346                                         = { S_IRWXU, S_IRWXG, S_IRWXO };
347
348                                 switch ( value[ j ] ) {
349                                 case 'w':
350                                         p |= m[ j ];
351                                         break;
352                                 case '-':
353                                         break;
354                                 default:
355                                         return LDAP_OTHER;
356                                 }
357                         } 
358
359                         *crit = c;
360                         *perms = p;
361
362                         return LDAP_SUCCESS;
363                 }
364         }
365
366         return LDAP_OTHER;
367 }
368 #endif /* LDAP_PF_LOCAL */
369
370 /* port = 0 indicates AF_LOCAL */
371 static int slap_get_listener_addresses(
372         const char *host,
373         unsigned short port,
374         struct sockaddr ***sal)
375 {
376         struct sockaddr **sap;
377
378 #ifdef LDAP_PF_LOCAL
379         if ( port == 0 ) {
380                 *sal = ch_malloc(2 * sizeof(void *));
381                 if (*sal == NULL) {
382                         return -1;
383                 }
384
385                 sap = *sal;
386                 *sap = ch_malloc(sizeof(struct sockaddr_un));
387                 if (*sap == NULL)
388                         goto errexit;
389                 sap[1] = NULL;
390
391                 if ( strlen(host) >
392                      (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
393 #ifdef NEW_LOGGING
394                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
395                                    "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
396                                    host ));
397 #else
398                         Debug( LDAP_DEBUG_ANY,
399                                "daemon: domain socket path (%s) too long in URL",
400                                host, 0, 0);
401 #endif
402                         goto errexit;
403                 }
404
405                 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
406                 (*sap)->sa_family = AF_LOCAL;
407                 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
408         } else
409 #endif
410         {
411 #ifdef HAVE_GETADDRINFO
412                 struct addrinfo hints, *res, *sai;
413                 int n, err;
414                 char serv[7];
415
416                 memset( &hints, '\0', sizeof(hints) );
417                 hints.ai_flags = AI_PASSIVE;
418                 hints.ai_socktype = SOCK_STREAM;
419                 hints.ai_family = slap_inet4or6;
420                 snprintf(serv, sizeof serv, "%d", port);
421
422                 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
423 #ifdef NEW_LOGGING
424                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
425                                    "slap_get_listener_addresses: getaddrinfo failed: %s\n",
426                                    AC_GAI_STRERROR(err) ));
427 #else
428                         Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
429                                 AC_GAI_STRERROR(err), 0, 0);
430 #endif
431                         return -1;
432                 }
433
434                 sai = res;
435                 for (n=2; (sai = sai->ai_next) != NULL; n++) {
436                         /* EMPTY */ ;
437                 }
438                 *sal = ch_calloc(n, sizeof(void *));
439                 if (*sal == NULL) {
440                         return -1;
441                 }
442
443                 sap = *sal;
444                 *sap = NULL;
445
446                 for ( sai=res; sai; sai=sai->ai_next ) {
447                         if( sai->ai_addr == NULL ) {
448 #ifdef NEW_LOGGING
449                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
450                                         "slap_get_listener_addresses: "
451                                         "getaddrinfo ai_addr is NULL?\n" ));
452 #else
453                                 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
454                                         "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
455 #endif
456                                 freeaddrinfo(res);
457                                 goto errexit;
458                         }
459
460                         switch (sai->ai_family) {
461 #  ifdef LDAP_PF_INET6
462                         case AF_INET6:
463                                 *sap = ch_malloc(sizeof(struct sockaddr_in6));
464                                 if (*sap == NULL) {
465                                         freeaddrinfo(res);
466                                         goto errexit;
467                                 }
468                                 *(struct sockaddr_in6 *)*sap =
469                                         *((struct sockaddr_in6 *)sai->ai_addr);
470                                 break;
471 #  endif
472                         case AF_INET:
473                                 *sap = ch_malloc(sizeof(struct sockaddr_in));
474                                 if (*sap == NULL) {
475                                         freeaddrinfo(res);
476                                         goto errexit;
477                                 }
478                                 *(struct sockaddr_in *)*sap =
479                                         *((struct sockaddr_in *)sai->ai_addr);
480                                 break;
481                         default:
482                                 *sap = NULL;
483                                 break;
484                         }
485
486                         if (*sap != NULL) {
487                                 (*sap)->sa_family = sai->ai_family;
488                                 sap++;
489                                 *sap = NULL;
490                         }
491                 }
492
493                 freeaddrinfo(res);
494 #else
495                 int i, n = 1;
496                 struct in_addr in;
497                 struct hostent *he = NULL;
498
499                 if ( host == NULL ) {
500                         in.s_addr = htonl(INADDR_ANY);
501
502                 } else if ( !inet_aton( host, &in ) ) {
503                         he = gethostbyname( host );
504                         if( he == NULL ) {
505 #ifdef NEW_LOGGING
506                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
507                                            "slap_get_listener_addresses: invalid host %s\n",
508                                            host ));
509 #else
510                                 Debug( LDAP_DEBUG_ANY,
511                                        "daemon: invalid host %s", host, 0, 0);
512 #endif
513                                 return -1;
514                         }
515                         for (n = 0; he->h_addr_list[n]; n++) ;
516                 }
517
518                 *sal = ch_malloc((n+1) * sizeof(void *));
519                 if (*sal == NULL) {
520                         return -1;
521                 }
522
523                 sap = *sal;
524                 for ( i = 0; i<n; i++ ) {
525                         sap[i] = ch_malloc(sizeof(struct sockaddr_in));
526                         if (*sap == NULL) {
527                                 goto errexit;
528                         }
529                         (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
530                         sap[i]->sa_family = AF_INET;
531                         ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
532                         if (he) {
533                                 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
534                         } else {
535                                 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
536                         }
537                 }
538                 sap[i] = NULL;
539 #endif
540         }
541
542         return 0;
543
544 errexit:
545         slap_free_listener_addresses(*sal);
546         return -1;
547 }
548
549 static int slap_open_listener(
550         const char* url,
551         int *listeners,
552         int *cur
553         )
554 {
555         int     num, tmp, rc;
556         Listener l;
557         Listener *li;
558         LDAPURLDesc *lud;
559         unsigned short port;
560         int err, addrlen = 0;
561         struct sockaddr **sal, **psal;
562         int socktype = SOCK_STREAM;     /* default to COTS */
563 #ifdef LDAP_PF_LOCAL
564         mode_t  perms = S_IRWXU;
565         int     crit = 1;
566 #endif
567
568         rc = ldap_url_parse( url, &lud );
569
570         if( rc != LDAP_URL_SUCCESS ) {
571 #ifdef NEW_LOGGING
572                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
573                            "slap_open_listener: listen URL \"%s\" parse error %d\n",
574                            url, rc ));
575 #else
576                 Debug( LDAP_DEBUG_ANY,
577                         "daemon: listen URL \"%s\" parse error=%d\n",
578                         url, rc, 0 );
579 #endif
580                 return rc;
581         }
582
583 #ifndef HAVE_TLS
584         if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
585 #ifdef NEW_LOGGING
586                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
587                            "slap_open_listener: TLS is not supported (%s)\n",
588                            url ));
589 #else
590                 Debug( LDAP_DEBUG_ANY,
591                         "daemon: TLS not supported (%s)\n",
592                         url, 0, 0 );
593 #endif
594                 ldap_free_urldesc( lud );
595                 return -1;
596         }
597
598         if(! lud->lud_port ) {
599                 lud->lud_port = LDAP_PORT;
600         }
601
602 #else
603         l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
604
605         if(! lud->lud_port ) {
606                 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
607         }
608 #endif
609
610         port = (unsigned short) lud->lud_port;
611
612         tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
613         if ( tmp == LDAP_PROTO_IPC ) {
614 #ifdef LDAP_PF_LOCAL
615                 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
616                         err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
617                 } else {
618                         err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
619                 }
620
621                 if ( lud->lud_exts ) {
622                         err = get_url_perms( lud->lud_exts, &perms, &crit );
623                 }
624 #else
625
626 #ifdef NEW_LOGGING
627                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
628                            "slap_open_listener: URL scheme is not supported: %s\n",
629                            url ));
630 #else
631                 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
632                         url, 0, 0);
633 #endif
634                 ldap_free_urldesc( lud );
635                 return -1;
636 #endif
637         } else {
638 #ifdef LDAP_CONNECTIONLESS
639                 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
640 #endif
641                 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
642                         || strcmp(lud->lud_host, "*") == 0 )
643                 {
644                         err = slap_get_listener_addresses(NULL, port, &sal);
645                 } else {
646                         err = slap_get_listener_addresses(lud->lud_host, port, &sal);
647                 }
648         }
649
650         ldap_free_urldesc( lud );
651         if ( err ) {
652                 return -1;
653         }
654
655         /* If we got more than one address returned, we need to make space
656          * for it in the slap_listeners array.
657          */
658         for ( num=0; sal[num]; num++ );
659         if ( num > 1 ) {
660                 *listeners += num-1;
661                 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
662         }
663
664         psal = sal;
665         while ( *sal != NULL ) {
666                 switch( (*sal)->sa_family ) {
667                 case AF_INET:
668 #ifdef LDAP_PF_INET6
669                 case AF_INET6:
670 #endif
671 #ifdef LDAP_PF_LOCAL
672                 case AF_LOCAL:
673 #endif
674                         break;
675                 default:
676                         sal++;
677                         continue;
678                 }
679 #ifdef LDAP_CONNECTIONLESS
680                 if (l.sl_is_udp)
681                     socktype = SOCK_DGRAM;
682 #endif
683                 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
684                 if ( l.sl_sd == AC_SOCKET_INVALID ) {
685                         int err = sock_errno();
686 #ifdef NEW_LOGGING
687                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
688                                    "slap_open_listener: socket() failed errno=%d (%s)\n",
689                                    err, sock_errstr(err) ));
690 #else
691                         Debug( LDAP_DEBUG_ANY,
692                                 "daemon: socket() failed errno=%d (%s)\n", err,
693                                 sock_errstr(err), 0 );
694 #endif
695                         sal++;
696                         continue;
697                 }
698 #ifndef HAVE_WINSOCK
699                 if ( l.sl_sd >= dtblsize ) {
700 #ifdef NEW_LOGGING
701                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
702                                    "slap_open_listener: listener descriptor %ld is too great %ld\n",
703                                    (long)l.sl_sd, (long)dtblsize ));
704 #else
705                         Debug( LDAP_DEBUG_ANY,
706                                "daemon: listener descriptor %ld is too great %ld\n",
707                                (long) l.sl_sd, (long) dtblsize, 0 );
708 #endif
709                         tcp_close( l.sl_sd );
710                         sal++;
711                         continue;
712                 }
713 #endif
714 #ifdef LDAP_PF_LOCAL
715                 if ( (*sal)->sa_family == AF_LOCAL ) {
716                         unlink ( ((struct sockaddr_un *)*sal)->sun_path );
717                 } else
718 #endif
719                 {
720 #ifdef SO_REUSEADDR
721                         /* enable address reuse */
722                         tmp = 1;
723                         rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
724                                          (char *) &tmp, sizeof(tmp) );
725                         if ( rc == AC_SOCKET_ERROR ) {
726                                 int err = sock_errno();
727 #ifdef NEW_LOGGING
728                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
729                                            "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
730                                            (long)l.sl_sd, err, sock_errstr(err) ));
731 #else
732                                 Debug( LDAP_DEBUG_ANY,
733                                        "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
734                                        (long) l.sl_sd, err, sock_errstr(err) );
735 #endif
736                         }
737 #endif
738                 }
739
740                 switch( (*sal)->sa_family ) {
741                 case AF_INET:
742                         addrlen = sizeof(struct sockaddr_in);
743                         break;
744 #ifdef LDAP_PF_INET6
745                 case AF_INET6:
746                         addrlen = sizeof(struct sockaddr_in6);
747                         break;
748 #endif
749 #ifdef LDAP_PF_LOCAL
750                 case AF_LOCAL:
751                         addrlen = sizeof(struct sockaddr_un);
752                         break;
753 #endif
754                 }
755
756                 if (bind(l.sl_sd, *sal, addrlen)) {
757                         err = sock_errno();
758 #ifdef NEW_LOGGING
759                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
760                            "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
761                            (long)l.sl_sd, err, sock_errstr(err) ));
762 #else
763                 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
764                        (long) l.sl_sd, err, sock_errstr(err) );
765 #endif
766                         tcp_close( l.sl_sd );
767                         sal++;
768                         continue;
769                 }
770
771         switch ( (*sal)->sa_family ) {
772 #ifdef LDAP_PF_LOCAL
773         case AF_LOCAL: {
774                 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
775                 if ( chmod( addr, perms ) < 0 && crit ) {
776                         int err = sock_errno();
777 #ifdef NEW_LOGGING
778                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
779                                    "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
780                                    (long)l.sl_sd, err, sock_errstr(err) ));
781 #else
782                         Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
783                                (long) l.sl_sd, err, sock_errstr(err) );
784 #endif
785                         tcp_close( l.sl_sd );
786                         slap_free_listener_addresses(psal);
787                         return -1;
788                 }
789                 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
790                 sprintf( l.sl_name, "PATH=%s", addr );
791         } break;
792 #endif /* LDAP_PF_LOCAL */
793
794         case AF_INET: {
795                 char *s;
796 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
797                 char addr[INET_ADDRSTRLEN];
798                 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
799                            addr, sizeof(addr) );
800                 s = addr;
801 #else
802                 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
803 #endif
804                 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
805                 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
806                 sprintf( l.sl_name, "IP=%s:%d",
807                          s != NULL ? s : "unknown" , port );
808         } break;
809
810 #ifdef LDAP_PF_INET6
811         case AF_INET6: {
812                 char addr[INET6_ADDRSTRLEN];
813                 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
814                            addr, sizeof addr);
815                 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
816                 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
817                 sprintf( l.sl_name, "IP=%s %d", addr, port );
818         } break;
819 #endif /* LDAP_PF_INET6 */
820
821         default:
822 #ifdef NEW_LOGGING
823                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
824                            "slap_open_listener: unsupported address family (%d)\n",
825                            (int)(*sal)->sa_family ));
826 #else
827                 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
828                         (int) (*sal)->sa_family, 0, 0 );
829 #endif
830                 break;
831         }
832
833         AC_MEMCPY(&l.sl_sa, *sal, addrlen);
834         l.sl_url = ch_strdup( url );
835         li = ch_malloc( sizeof( Listener ) );
836         *li = l;
837         slap_listeners[*cur] = li;
838         (*cur)++;
839         sal++;
840
841         } /* while ( *sal != NULL ) */
842
843         slap_free_listener_addresses(psal);
844
845
846 #ifdef NEW_LOGGING
847         LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
848                    "slap_open_listener: daemon initialized %s\n", l.sl_url ));
849 #else
850         Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
851                 l.sl_url, 0, 0 );
852 #endif
853         return 0;
854 }
855
856 static int sockinit(void);
857 static int sockdestroy(void);
858
859 int slapd_daemon_init( const char *urls )
860 {
861         int i, j, n, rc;
862         char **u;
863
864 #ifdef NEW_LOGGING
865         LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
866                    "slapd_daemon_init: %s\n",
867                    urls ? urls : "<null>" ));
868 #else
869         Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
870                 urls ? urls : "<null>", 0, 0 );
871 #endif
872         if( (rc = sockinit()) != 0 ) {
873                 return rc;
874         }
875
876 #ifdef HAVE_SYSCONF
877         dtblsize = sysconf( _SC_OPEN_MAX );
878 #elif HAVE_GETDTABLESIZE
879         dtblsize = getdtablesize();
880 #else
881         dtblsize = FD_SETSIZE;
882 #endif
883
884 #ifdef FD_SETSIZE
885         if(dtblsize > FD_SETSIZE) {
886                 dtblsize = FD_SETSIZE;
887         }
888 #endif  /* !FD_SETSIZE */
889
890         /* open a pipe (or something equivalent connected to itself).
891          * we write a byte on this fd whenever we catch a signal. The main
892          * loop will be select'ing on this socket, and will wake up when
893          * this byte arrives.
894          */
895         if( (rc = lutil_pair( wake_sds )) < 0 ) {
896 #ifdef NEW_LOGGING
897                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
898                            "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
899 #else
900                 Debug( LDAP_DEBUG_ANY,
901                         "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
902 #endif
903                 return rc;
904         }
905
906         FD_ZERO( &slap_daemon.sd_readers );
907         FD_ZERO( &slap_daemon.sd_writers );
908
909         if( urls == NULL ) {
910                 urls = "ldap:///";
911         }
912
913         u = str2charray( urls, " " );
914
915         if( u == NULL || u[0] == NULL ) {
916 #ifdef NEW_LOGGING
917                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
918                            "slap_daemon_init: no urls (%s) provided.\n", urls ));
919 #else
920                 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
921                         urls, 0, 0 );
922 #endif
923                 return -1;
924         }
925
926         for( i=0; u[i] != NULL; i++ ) {
927 #ifdef NEW_LOGGING
928                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
929                            "slap_daemon_init: listen on %s\n.", u[i] ));
930 #else
931                 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
932                         u[i], 0, 0 );
933 #endif
934         }
935
936         if( i == 0 ) {
937 #ifdef NEW_LOGGING
938                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
939                            "slap_daemon_init: no listeners to open (%s)\n", urls ));
940 #else
941                 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
942                         urls, 0, 0 );
943 #endif
944                 charray_free( u );
945                 return -1;
946         }
947
948 #ifdef NEW_LOGGING
949         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
950                    "slap_daemon_init: %d listeners to open...\n", i ));
951 #else
952         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
953                 i, 0, 0 );
954 #endif
955         slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
956
957         for(n = 0, j = 0; u[n]; n++ ) {
958                 if ( slap_open_listener( u[n], &i, &j ) ) {
959                         charray_free( u );
960                         return -1;
961                 }
962         }
963         slap_listeners[j] = NULL;
964
965 #ifdef NEW_LOGGING
966         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
967                    "slap_daemon_init: %d listeners opened\n", i ));
968 #else
969         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
970                 i, 0, 0 );
971 #endif
972
973 #ifdef HAVE_SLP
974         slapd_slp_init( urls );
975         slapd_slp_reg();
976 #endif
977
978         charray_free( u );
979         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
980         return !i;
981 }
982
983
984 int
985 slapd_daemon_destroy(void)
986 {
987         connections_destroy();
988         tcp_close( wake_sds[1] );
989         tcp_close( wake_sds[0] );
990         sockdestroy();
991
992 #ifdef HAVE_SLP
993         slapd_slp_dereg();
994         slapd_slp_deinit();
995 #endif
996
997         return 0;
998 }
999
1000
1001 static void
1002 close_listeners(
1003         int remove
1004 )
1005 {
1006         int l;
1007
1008         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1009                 if ( remove )
1010                         slapd_remove( slap_listeners[l]->sl_sd, 0 );
1011                 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1012 #ifdef LDAP_PF_LOCAL
1013                         if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1014                                 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1015                         }
1016 #endif /* LDAP_PF_LOCAL */
1017                         slapd_close( slap_listeners[l]->sl_sd );
1018                 }
1019                 if ( slap_listeners[l]->sl_url )
1020                         free ( slap_listeners[l]->sl_url );
1021                 if ( slap_listeners[l]->sl_name )
1022                         free ( slap_listeners[l]->sl_name );
1023                 free ( slap_listeners[l] );
1024                 slap_listeners[l] = NULL;
1025         }
1026 }
1027
1028
1029 static void *
1030 slapd_daemon_task(
1031         void *ptr
1032 )
1033 {
1034         int l;
1035         time_t  last_idle_check = 0;
1036         time( &starttime );
1037
1038         if ( global_idletimeout > 0 ) {
1039                 last_idle_check = slap_get_time();
1040         }
1041         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1042                 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1043                         continue;
1044 #ifdef LDAP_CONNECTIONLESS
1045                 /* Since this is connectionless, the data port is the
1046                  * listening port. The listen() and accept() calls
1047                  * are unnecessary.
1048                  */
1049                 if ( slap_listeners[l]->sl_is_udp )
1050                 {
1051                         slapd_add( slap_listeners[l]->sl_sd );
1052                         continue;
1053                 }
1054 #endif
1055
1056                 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1057                         int err = sock_errno();
1058 #ifdef NEW_LOGGING
1059                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1060                                    "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1061                                    slap_listeners[l]->sl_url, err, sock_errstr(err) ));
1062 #else
1063                         Debug( LDAP_DEBUG_ANY,
1064                                 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1065                                         slap_listeners[l]->sl_url, err,
1066                                         sock_errstr(err) );
1067 #endif
1068                         return( (void*)-1 );
1069                 }
1070
1071                 slapd_add( slap_listeners[l]->sl_sd );
1072         }
1073
1074 #ifdef HAVE_NT_SERVICE_MANAGER
1075         if ( started_event != NULL ) {
1076                 ldap_pvt_thread_cond_signal( &started_event );
1077         }
1078 #endif
1079         /* initialization complete. Here comes the loop. */
1080
1081         while ( !slapd_shutdown ) {
1082                 ber_socket_t i;
1083                 int ns;
1084                 int at;
1085                 ber_socket_t nfds;
1086 #define SLAPD_EBADF_LIMIT 16
1087                 int ebadf = 0;
1088                 int emfile = 0;
1089
1090 #define SLAPD_IDLE_CHECK_LIMIT 4
1091                 time_t  now;
1092
1093
1094                 fd_set                  readfds;
1095                 fd_set                  writefds;
1096                 Sockaddr                from;
1097
1098 #if defined(SLAPD_RLOOKUPS)
1099                 struct hostent          *hp;
1100 #endif
1101                 struct timeval          zero;
1102                 struct timeval          *tvp;
1103
1104                 if( emfile ) {
1105                         now = slap_get_time();
1106                         connections_timeout_idle( now );
1107                 }
1108                 else if ( global_idletimeout > 0 ) {
1109                         now = slap_get_time();
1110                         if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1111                                 connections_timeout_idle( now );
1112                         }
1113                 }
1114
1115 #ifdef SIGHUP
1116                 if( slapd_gentle_shutdown ) {
1117                         ber_socket_t active;
1118
1119                         if( slapd_gentle_shutdown == 1 ) {
1120                                 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1121                                 close_listeners( 1 );
1122                                 global_restrictops |= SLAP_RESTRICT_OP_WRITES;
1123                                 slapd_gentle_shutdown = 2;
1124                         }
1125
1126                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1127                         active = slap_daemon.sd_nactives;
1128                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1129                         if( active == 0 ) {
1130                                 slapd_shutdown = 2;
1131                                 break;
1132                         }
1133                 }
1134 #endif
1135
1136                 FD_ZERO( &writefds );
1137                 FD_ZERO( &readfds );
1138
1139                 zero.tv_sec = 0;
1140                 zero.tv_usec = 0;
1141
1142                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1143
1144 #ifdef FD_SET_MANUAL_COPY
1145                 for( s = 0; s < nfds; s++ ) {
1146                         if(FD_ISSET( &slap_sd_readers, s )) {
1147                                 FD_SET( s, &readfds );
1148                         }
1149                         if(FD_ISSET( &slap_sd_writers, s )) {
1150                                 FD_SET( s, &writefds );
1151                         }
1152                 }
1153 #else
1154                 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1155                 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1156 #endif
1157                 assert(!FD_ISSET(wake_sds[0], &readfds));
1158                 FD_SET( wake_sds[0], &readfds );
1159
1160                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1161                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1162                                 continue;
1163                         if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1164                             FD_SET( slap_listeners[l]->sl_sd, &readfds );
1165                 }
1166
1167 #ifndef HAVE_WINSOCK
1168                 nfds = slap_daemon.sd_nfds;
1169 #else
1170                 nfds = dtblsize;
1171 #endif
1172
1173                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1174
1175                 at = ldap_pvt_thread_pool_backload(&connection_pool);
1176
1177 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1178                 tvp = NULL;
1179 #else
1180                 tvp = at ? &zero : NULL;
1181 #endif
1182
1183                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1184                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1185                                 continue;
1186
1187 #ifdef NEW_LOGGING
1188                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1189                                    "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1190                                    slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1191 #else
1192                         Debug( LDAP_DEBUG_CONNS,
1193                                 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1194                                         slap_listeners[l]->sl_sd, at,
1195                                         tvp == NULL ? "NULL" : "zero" );
1196 #endif
1197                 }
1198
1199                 switch(ns = select( nfds, &readfds,
1200 #ifdef HAVE_WINSOCK
1201                         /* don't pass empty fd_set */
1202                         ( writefds.fd_count > 0 ? &writefds : NULL ),
1203 #else
1204                         &writefds,
1205 #endif
1206                         NULL, tvp ))
1207                 {
1208                 case -1: {      /* failure - try again */
1209                                 int err = sock_errno();
1210
1211                                 if( err == EBADF
1212 #ifdef WSAENOTSOCK
1213                                         /* you'd think this would be EBADF */
1214                                         || err == WSAENOTSOCK
1215 #endif
1216                                 ) {
1217                                         if (++ebadf < SLAPD_EBADF_LIMIT)
1218                                                 continue;
1219                                 }
1220
1221                                 if( err != EINTR ) {
1222 #ifdef NEW_LOGGING
1223                                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1224                                                    "slapd_daemon_task: select failed (%d): %s\n",
1225                                                    err, sock_errstr(err) ));
1226 #else
1227                                         Debug( LDAP_DEBUG_CONNS,
1228                                                 "daemon: select failed (%d): %s\n",
1229                                                 err, sock_errstr(err), 0 );
1230 #endif
1231                                         slapd_shutdown = 2;
1232                                 }
1233                         }
1234                         continue;
1235
1236                 case 0:         /* timeout - let threads run */
1237                         ebadf = 0;
1238 #ifdef NEW_LOGGING
1239                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1240                                    "slapd_daemon_task: select timeout - yielding\n" ));
1241 #else
1242                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1243                             0, 0, 0 );
1244 #endif
1245                         ldap_pvt_thread_yield();
1246                         continue;
1247
1248                 default:        /* something happened - deal with it */
1249                         if( slapd_shutdown ) continue;
1250
1251                         ebadf = 0;
1252 #ifdef NEW_LOGGING
1253                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1254                                    "slapd_daemon_task: activity on %d descriptors\n", ns ));
1255 #else
1256                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1257                                 ns, 0, 0 );
1258 #endif
1259                         /* FALL THRU */
1260                 }
1261
1262                 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1263                         char c[BUFSIZ];
1264                         tcp_read( wake_sds[0], c, sizeof(c) );
1265 #ifdef NO_THREADS
1266                         waking = 0;
1267 #endif
1268                         continue;
1269                 }
1270
1271                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1272                         ber_socket_t s;
1273                         socklen_t len = sizeof(from);
1274                         long id;
1275                         slap_ssf_t ssf = 0;
1276                         char *authid = NULL;
1277
1278                         char    *dnsname = NULL;
1279                         char    *peeraddr;
1280 #ifdef LDAP_PF_LOCAL
1281                         char    peername[MAXPATHLEN + sizeof("PATH=")];
1282 #elif defined(LDAP_PF_INET6)
1283                         char    peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1284 #else
1285                         char    peername[sizeof("IP=255.255.255.255:65336")];
1286 #endif /* LDAP_PF_LOCAL */
1287
1288                         peername[0] = '\0';
1289
1290                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1291                                 continue;
1292
1293                         if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1294                                 continue;
1295
1296 #ifdef LDAP_CONNECTIONLESS
1297                         if ( slap_listeners[l]->sl_is_udp )
1298                         {
1299                         /* The first time we receive a query, we set this
1300                          * up as a "connection". It remains open for the life
1301                          * of the slapd.
1302                          */
1303                                 if ( slap_listeners[l]->sl_is_udp < 2 )
1304                                 {
1305                                     id = connection_init(
1306                                         slap_listeners[l]->sl_sd,
1307                                         slap_listeners[l]->sl_url, "", "",
1308                                         slap_listeners[l]->sl_name,
1309                                         2, ssf, authid );
1310                                     slap_listeners[l]->sl_is_udp++;
1311                                 }
1312                                 continue;
1313                         }
1314 #endif
1315
1316                         s = accept( slap_listeners[l]->sl_sd,
1317                                 (struct sockaddr *) &from, &len );
1318                         if ( s == AC_SOCKET_INVALID ) {
1319                                 int err = sock_errno();
1320
1321 #ifdef EMFILE
1322                                 if( err == EMFILE ) {
1323                                         emfile++;
1324                                 } else
1325 #endif
1326 #ifdef ENFILE
1327                                 if( err == ENFILE ) {
1328                                         emfile++;
1329                                 } else 
1330 #endif
1331                                 {
1332                                         emfile=0;
1333                                 }
1334
1335                                 if( emfile < 3 ) {
1336 #ifdef NEW_LOGGING
1337                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1338                                                 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1339                                                 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1340 #else
1341                                         Debug( LDAP_DEBUG_ANY,
1342                                             "daemon: accept(%ld) failed errno=%d (%s)\n",
1343                                             (long) slap_listeners[l]->sl_sd, err,
1344                                             sock_errstr(err) );
1345 #endif
1346                                 } else {
1347                                         /* prevent busy loop */
1348 #  ifdef HAVE_USLEEP
1349                                         if( emfile % 4 == 3 ) usleep( 250 );
1350 #  else
1351                                         if( emfile % 8 == 7 ) sleep( 1 );
1352 #  endif
1353                                 }
1354
1355                                 ldap_pvt_thread_yield();
1356                                 continue;
1357                         }
1358                         emfile = 0;
1359
1360 #ifndef HAVE_WINSOCK
1361                         /* make sure descriptor number isn't too great */
1362                         if ( s >= dtblsize ) {
1363 #ifdef NEW_LOGGING
1364                                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1365                                    "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1366                                    (long)s, (long)dtblsize ));
1367 #else
1368                                 Debug( LDAP_DEBUG_ANY,
1369                                         "daemon: %ld beyond descriptor table size %ld\n",
1370                                         (long) s, (long) dtblsize, 0 );
1371 #endif
1372
1373                                 slapd_close(s);
1374                                 ldap_pvt_thread_yield();
1375                                 continue;
1376                         }
1377 #endif
1378
1379 #ifdef LDAP_DEBUG
1380                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1381
1382                         /* newly accepted stream should not be in any of the FD SETS */
1383                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1384                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1385                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1386
1387                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1388 #endif
1389
1390 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1391 #ifdef LDAP_PF_LOCAL
1392                         /* for IPv4 and IPv6 sockets only */
1393                         if ( from.sa_addr.sa_family != AF_LOCAL )
1394 #endif /* LDAP_PF_LOCAL */
1395                         {
1396                                 int rc;
1397                                 int tmp;
1398 #ifdef SO_KEEPALIVE
1399                                 /* enable keep alives */
1400                                 tmp = 1;
1401                                 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1402                                         (char *) &tmp, sizeof(tmp) );
1403                                 if ( rc == AC_SOCKET_ERROR ) {
1404                                         int err = sock_errno();
1405 #ifdef NEW_LOGGING
1406                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1407                                                    "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1408                                                    (long)s, err, sock_errstr(err) ));
1409 #else
1410                                         Debug( LDAP_DEBUG_ANY,
1411                                                 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1412                                                 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1413 #endif
1414                                 }
1415 #endif
1416 #ifdef TCP_NODELAY
1417                                 /* enable no delay */
1418                                 tmp = 1;
1419                                 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1420                                         (char *)&tmp, sizeof(tmp) );
1421                                 if ( rc == AC_SOCKET_ERROR ) {
1422                                         int err = sock_errno();
1423 #ifdef NEW_LOGGING
1424                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1425                                                    "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1426                                                    (long)s, err, sock_errstr(err) ));
1427 #else
1428                                         Debug( LDAP_DEBUG_ANY,
1429                                                 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1430                                                 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1431 #endif
1432                                 }
1433 #endif
1434                         }
1435 #endif
1436
1437 #ifdef NEW_LOGGING
1438                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1439                                    "slapd_daemon_task: new connection on %ld\n", (long)s ));
1440 #else
1441                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1442                                 (long) s, 0, 0 );
1443 #endif
1444                         switch ( from.sa_addr.sa_family ) {
1445 #  ifdef LDAP_PF_LOCAL
1446                         case AF_LOCAL:
1447                                 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1448                                 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1449                                 dnsname = "local";
1450                                 break;
1451 #endif /* LDAP_PF_LOCAL */
1452
1453 #  ifdef LDAP_PF_INET6
1454                         case AF_INET6:
1455                         if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1456                                 peeraddr = inet_ntoa( *((struct in_addr *)
1457                                                         &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1458                                 sprintf( peername, "IP=%s:%d",
1459                                          peeraddr != NULL ? peeraddr : "unknown",
1460                                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1461                         } else {
1462                                 char addr[INET6_ADDRSTRLEN];
1463                                 sprintf( peername, "IP=%s %d",
1464                                          inet_ntop( AF_INET6,
1465                                                     &from.sa_in6_addr.sin6_addr,
1466                                                     addr, sizeof addr) ? addr : "unknown",
1467                                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1468                         }
1469                         break;
1470 #  endif /* LDAP_PF_INET6 */
1471
1472                         case AF_INET:
1473                         peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1474                         sprintf( peername, "IP=%s:%d",
1475                                 peeraddr != NULL ? peeraddr : "unknown",
1476                                 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1477                                 break;
1478
1479                         default:
1480                                 slapd_close(s);
1481                                 continue;
1482                         }
1483
1484                         if ( ( from.sa_addr.sa_family == AF_INET )
1485 #ifdef LDAP_PF_INET6
1486                                 || ( from.sa_addr.sa_family == AF_INET6 )
1487 #endif
1488                         ) {
1489 #ifdef SLAPD_RLOOKUPS
1490                                 if ( use_reverse_lookup ) {
1491 #  ifdef LDAP_PF_INET6
1492                                         if ( from.sa_addr.sa_family == AF_INET6 )
1493                                                 hp = gethostbyaddr(
1494                                                         (char *)&(from.sa_in6_addr.sin6_addr),
1495                                                         sizeof(from.sa_in6_addr.sin6_addr),
1496                                                         AF_INET6 );
1497                                         else
1498 #  endif /* LDAP_PF_INET6 */
1499                                         hp = gethostbyaddr(
1500                                                 (char *) &(from.sa_in_addr.sin_addr),
1501                                                 sizeof(from.sa_in_addr.sin_addr),
1502                                                 AF_INET );
1503                                         dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1504                                 }
1505 #else
1506                                 dnsname = NULL;
1507 #endif /* SLAPD_RLOOKUPS */
1508
1509 #ifdef HAVE_TCPD
1510                                 if ( !hosts_ctl("slapd",
1511                                                 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1512                                                 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1513                                                 STRING_UNKNOWN ))
1514                                 {
1515                                         /* DENY ACCESS */
1516                                         Statslog( LDAP_DEBUG_ANY,
1517                                                 "fd=%ld host access from %s (%s) denied.\n",
1518                                                 (long) s,
1519                                                 dnsname != NULL ? dnsname : "unknown",
1520                                                 peeraddr != NULL ? peeraddr : "unknown",
1521                                                 0, 0 );
1522                                         slapd_close(s);
1523                                         continue;
1524                                 }
1525 #endif /* HAVE_TCPD */
1526                         }
1527
1528                         id = connection_init(s,
1529                                 slap_listeners[l]->sl_url,
1530                                 dnsname != NULL ? dnsname : "unknown",
1531                                 peername,
1532                                 slap_listeners[l]->sl_name,
1533 #ifdef HAVE_TLS
1534                                 slap_listeners[l]->sl_is_tls,
1535 #else
1536                                 0,
1537 #endif
1538                                 ssf,
1539                                 authid );
1540
1541                         if( authid ) ch_free(authid);
1542
1543                         if( id < 0 ) {
1544 #ifdef NEW_LOGGING
1545                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1546                                            "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1547                                            (long)s, peername, slap_listeners[l]->sl_name ));
1548 #else
1549                                 Debug( LDAP_DEBUG_ANY,
1550                                         "daemon: connection_init(%ld, %s, %s) failed.\n",
1551                                         (long) s,
1552                                         peername,
1553                                         slap_listeners[l]->sl_name );
1554 #endif
1555                                 slapd_close(s);
1556                                 continue;
1557                         }
1558
1559                         Statslog( LDAP_DEBUG_STATS,
1560                                 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1561                                 id, (long) s,
1562                                 peername,
1563                                 slap_listeners[l]->sl_name,
1564                                 0 );
1565
1566                         slapd_add( s );
1567                         continue;
1568                 }
1569
1570 #ifdef LDAP_DEBUG
1571 #ifdef NEW_LOGGING
1572                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1573                            "slapd_daemon_task: activity on " ));
1574 #else
1575                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1576 #endif
1577 #ifdef HAVE_WINSOCK
1578                 for ( i = 0; i < readfds.fd_count; i++ ) {
1579 #ifdef NEW_LOGGING
1580                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1581                                    " %d%s", readfds.fd_array[i], "r", 0 ));
1582 #else
1583                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1584                                 readfds.fd_array[i], "r", 0 );
1585 #endif
1586                 }
1587                 for ( i = 0; i < writefds.fd_count; i++ ) {
1588 #ifdef NEW_LOGGING
1589                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1590                                    " %d%s", writefds.fd_array[i], "w" ));
1591 #else
1592                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1593                                 writefds.fd_array[i], "w", 0 );
1594 #endif
1595                 }
1596
1597 #else
1598                 for ( i = 0; i < nfds; i++ ) {
1599                         int     r, w;
1600                         int     is_listener = 0;
1601
1602                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1603                                 if ( i == slap_listeners[l]->sl_sd ) {
1604 #ifdef LDAP_CONNECTIONLESS
1605                                 /* The listener is the data port. Don't
1606                                  * skip it.
1607                                  */
1608                                         if (slap_listeners[l]->sl_is_udp)
1609                                                 continue;
1610 #endif
1611                                         is_listener = 1;
1612                                         break;
1613                                 }
1614                         }
1615                         if ( is_listener ) {
1616                                 continue;
1617                         }
1618                         r = FD_ISSET( i, &readfds );
1619                         w = FD_ISSET( i, &writefds );
1620                         if ( r || w ) {
1621 #ifdef NEW_LOGGING
1622                                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1623                                            " %d%s%s", i,
1624                                            r ? "r" : "", w ? "w" : "" ));
1625 #else
1626                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1627                                     r ? "r" : "", w ? "w" : "" );
1628 #endif
1629                         }
1630                 }
1631 #endif
1632 #ifdef NEW_LOGGING
1633                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1634 #else
1635                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1636 #endif
1637
1638 #endif
1639
1640                 /* loop through the writers */
1641 #ifdef HAVE_WINSOCK
1642                 for ( i = 0; i < writefds.fd_count; i++ )
1643 #else
1644                 for ( i = 0; i < nfds; i++ )
1645 #endif
1646                 {
1647                         ber_socket_t wd;
1648                         int is_listener = 0;
1649 #ifdef HAVE_WINSOCK
1650                         wd = writefds.fd_array[i];
1651 #else
1652                         if( ! FD_ISSET( i, &writefds ) ) {
1653                                 continue;
1654                         }
1655                         wd = i;
1656 #endif
1657
1658                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1659                                 if ( i == slap_listeners[l]->sl_sd ) {
1660 #ifdef LDAP_CONNECTIONLESS
1661                                         if (slap_listeners[l]->sl_is_udp)
1662                                                 continue;
1663 #endif
1664                                         is_listener = 1;
1665                                         break;
1666                                 }
1667                         }
1668                         if ( is_listener ) {
1669                                 continue;
1670                         }
1671 #ifdef NEW_LOGGING
1672                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1673                                    "slapd_daemon_task: write active on %d\n", wd ));
1674 #else
1675                         Debug( LDAP_DEBUG_CONNS,
1676                                 "daemon: write active on %d\n",
1677                                 wd, 0, 0 );
1678 #endif
1679                         /*
1680                          * NOTE: it is possible that the connection was closed
1681                          * and that the stream is now inactive.
1682                          * connection_write() must valid the stream is still
1683                          * active.
1684                          */
1685
1686                         if ( connection_write( wd ) < 0 ) {
1687                                 FD_CLR( (unsigned) wd, &readfds );
1688                                 slapd_close( wd );
1689                         }
1690                 }
1691
1692 #ifdef HAVE_WINSOCK
1693                 for ( i = 0; i < readfds.fd_count; i++ )
1694 #else
1695                 for ( i = 0; i < nfds; i++ )
1696 #endif
1697                 {
1698                         ber_socket_t rd;
1699                         int is_listener = 0;
1700
1701 #ifdef HAVE_WINSOCK
1702                         rd = readfds.fd_array[i];
1703 #else
1704                         if( ! FD_ISSET( i, &readfds ) ) {
1705                                 continue;
1706                         }
1707                         rd = i;
1708 #endif
1709
1710                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1711                                 if ( rd == slap_listeners[l]->sl_sd ) {
1712 #ifdef LDAP_CONNECTIONLESS
1713                                         if (slap_listeners[l]->sl_is_udp)
1714                                                 continue;
1715 #endif
1716                                         is_listener = 1;
1717                                         break;
1718                                 }
1719                         }
1720                         if ( is_listener ) {
1721                                 continue;
1722                         }
1723
1724 #ifdef NEW_LOGGING
1725                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1726                                    "slapd_daemon_task: read activity on %d\n", rd ));
1727 #else
1728                         Debug ( LDAP_DEBUG_CONNS,
1729                                 "daemon: read activity on %d\n", rd, 0, 0 );
1730 #endif
1731                         /*
1732                          * NOTE: it is possible that the connection was closed
1733                          * and that the stream is now inactive.
1734                          * connection_read() must valid the stream is still
1735                          * active.
1736                          */
1737
1738                         if ( connection_read( rd ) < 0 ) {
1739                                 slapd_close( rd );
1740                         }
1741                 }
1742                 ldap_pvt_thread_yield();
1743         }
1744
1745         if( slapd_shutdown == 1 ) {
1746 #ifdef NEW_LOGGING
1747                 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1748                            "slapd_daemon_task: shutdown requested and initiated.\n"));
1749 #else
1750                 Debug( LDAP_DEBUG_TRACE,
1751                         "daemon: shutdown requested and initiated.\n",
1752                         0, 0, 0 );
1753 #endif
1754
1755         } else if ( slapd_shutdown == 2 ) {
1756 #ifdef HAVE_NT_SERVICE_MANAGER
1757 #ifdef NEW_LOGGING
1758                         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1759                                    "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1760 #else
1761                         Debug( LDAP_DEBUG_TRACE,
1762                                "daemon: shutdown initiated by Service Manager.\n",
1763                                0, 0, 0);
1764 #endif
1765 #else /* !HAVE_NT_SERVICE_MANAGER */
1766 #ifdef NEW_LOGGING
1767                         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1768                                    "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1769 #else
1770                         Debug( LDAP_DEBUG_TRACE,
1771                                "daemon: abnormal condition, shutdown initiated.\n",
1772                                0, 0, 0 );
1773 #endif
1774 #endif /* !HAVE_NT_SERVICE_MANAGER */
1775         } else {
1776 #ifdef NEW_LOGGING
1777                 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1778                            "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1779 #else
1780                 Debug( LDAP_DEBUG_TRACE,
1781                        "daemon: no active streams, shutdown initiated.\n",
1782                        0, 0, 0 );
1783 #endif
1784         }
1785
1786         if( slapd_gentle_shutdown != 2 )
1787                 close_listeners ( 0 );
1788         free ( slap_listeners );
1789         slap_listeners = NULL;
1790
1791 #ifdef NEW_LOGGING
1792         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1793                    "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1794                    ldap_pvt_thread_pool_backload(&connection_pool) ));
1795 #else
1796         Debug( LDAP_DEBUG_ANY,
1797             "slapd shutdown: waiting for %d threads to terminate\n",
1798             ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1799 #endif
1800         ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1801
1802         return NULL;
1803 }
1804
1805
1806 int slapd_daemon( void )
1807 {
1808         int rc;
1809
1810         connections_init();
1811
1812 #define SLAPD_LISTENER_THREAD 1
1813 #if defined( SLAPD_LISTENER_THREAD )
1814         {
1815                 ldap_pvt_thread_t       listener_tid;
1816
1817                 /* listener as a separate THREAD */
1818                 rc = ldap_pvt_thread_create( &listener_tid,
1819                         0, slapd_daemon_task, NULL );
1820
1821                 if ( rc != 0 ) {
1822 #ifdef NEW_LOGGING
1823                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1824                                    "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1825 #else
1826                         Debug( LDAP_DEBUG_ANY,
1827                         "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1828 #endif
1829                         return rc;
1830                 }
1831  
1832                 /* wait for the listener thread to complete */
1833                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1834         }
1835 #else
1836         /* experimental code */
1837         slapd_daemon_task( NULL );
1838 #endif
1839
1840         return 0;
1841
1842 }
1843
1844 int sockinit(void)
1845 {
1846 #if defined( HAVE_WINSOCK2 )
1847     WORD wVersionRequested;
1848         WSADATA wsaData;
1849         int err;
1850
1851         wVersionRequested = MAKEWORD( 2, 0 );
1852
1853         err = WSAStartup( wVersionRequested, &wsaData );
1854         if ( err != 0 ) {
1855                 /* Tell the user that we couldn't find a usable */
1856                 /* WinSock DLL.                                  */
1857                 return -1;
1858         }
1859
1860         /* Confirm that the WinSock DLL supports 2.0.*/
1861         /* Note that if the DLL supports versions greater    */
1862         /* than 2.0 in addition to 2.0, it will still return */
1863         /* 2.0 in wVersion since that is the version we      */
1864         /* requested.                                        */
1865
1866         if ( LOBYTE( wsaData.wVersion ) != 2 ||
1867                 HIBYTE( wsaData.wVersion ) != 0 )
1868         {
1869             /* Tell the user that we couldn't find a usable */
1870             /* WinSock DLL.                                  */
1871             WSACleanup();
1872             return -1;
1873         }
1874
1875         /* The WinSock DLL is acceptable. Proceed. */
1876 #elif defined( HAVE_WINSOCK )
1877         WSADATA wsaData;
1878         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1879             return -1;
1880         }
1881 #endif
1882         return 0;
1883 }
1884
1885 int sockdestroy(void)
1886 {
1887 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1888         WSACleanup();
1889 #endif
1890         return 0;
1891 }
1892
1893 RETSIGTYPE
1894 slap_sig_shutdown( int sig )
1895 {
1896 #ifdef NEW_LOGGING
1897         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1898                    "slap_sig_shutdown: signal %d\n", sig ));
1899 #else
1900         Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1901 #endif
1902
1903         /*
1904          * If the NT Service Manager is controlling the server, we don't
1905          * want SIGBREAK to kill the server. For some strange reason,
1906          * SIGBREAK is generated when a user logs out.
1907          */
1908
1909 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1910         if (is_NT_Service && sig == SIGBREAK)
1911 #ifdef NEW_LOGGING
1912             LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1913                        "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1914 #else
1915             Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1916                   0, 0, 0);
1917 #endif
1918         else
1919 #endif
1920 #ifdef SIGHUP
1921         if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
1922                 slapd_gentle_shutdown = 1;
1923         else
1924 #endif
1925         slapd_shutdown = 1;
1926
1927         WAKE_LISTENER(1);
1928
1929         /* reinstall self */
1930         (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1931 }
1932
1933 RETSIGTYPE
1934 slap_sig_wake( int sig )
1935 {
1936         WAKE_LISTENER(1);
1937
1938         /* reinstall self */
1939         (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1940 }
1941
1942
1943 void slapd_add_internal(ber_socket_t s) {
1944         slapd_add(s);
1945 }
1946
1947 Listener ** slapd_get_listeners(void) {
1948         return slap_listeners;
1949 }