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