]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
More ACL to dn="" bug fixing... and add test006-acl check
[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 #ifdef IPV6_V6ONLY
747                         /* Try to use IPv6 sockets for IPv6 only */
748                         tmp = 1;
749                         rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
750                                          (char *) &tmp, sizeof(tmp) );
751                         if ( rc == AC_SOCKET_ERROR ) {
752                                 int err = sock_errno();
753 #ifdef NEW_LOGGING
754                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
755                                            "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n",
756                                            (long)l.sl_sd, err, sock_errstr(err) ));
757 #else
758                                 Debug( LDAP_DEBUG_ANY,
759                                        "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
760                                        (long) l.sl_sd, err, sock_errstr(err) );
761 #endif
762                         }
763 #endif
764                         addrlen = sizeof(struct sockaddr_in6);
765                         break;
766 #endif
767 #ifdef LDAP_PF_LOCAL
768                 case AF_LOCAL:
769                         addrlen = sizeof(struct sockaddr_un);
770                         break;
771 #endif
772                 }
773
774                 if (bind(l.sl_sd, *sal, addrlen)) {
775                         err = sock_errno();
776 #ifdef NEW_LOGGING
777                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
778                            "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
779                            (long)l.sl_sd, err, sock_errstr(err) ));
780 #else
781                 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
782                        (long) l.sl_sd, err, sock_errstr(err) );
783 #endif
784                         tcp_close( l.sl_sd );
785                         sal++;
786                         continue;
787                 }
788
789         switch ( (*sal)->sa_family ) {
790 #ifdef LDAP_PF_LOCAL
791         case AF_LOCAL: {
792                 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
793                 if ( chmod( addr, perms ) < 0 && crit ) {
794                         int err = sock_errno();
795 #ifdef NEW_LOGGING
796                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
797                                    "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
798                                    (long)l.sl_sd, err, sock_errstr(err) ));
799 #else
800                         Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
801                                (long) l.sl_sd, err, sock_errstr(err) );
802 #endif
803                         tcp_close( l.sl_sd );
804                         slap_free_listener_addresses(psal);
805                         return -1;
806                 }
807                 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
808                 sprintf( l.sl_name, "PATH=%s", addr );
809         } break;
810 #endif /* LDAP_PF_LOCAL */
811
812         case AF_INET: {
813                 char *s;
814 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
815                 char addr[INET_ADDRSTRLEN];
816                 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
817                            addr, sizeof(addr) );
818                 s = addr;
819 #else
820                 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
821 #endif
822                 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
823                 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
824                 sprintf( l.sl_name, "IP=%s:%d",
825                          s != NULL ? s : "unknown" , port );
826         } break;
827
828 #ifdef LDAP_PF_INET6
829         case AF_INET6: {
830                 char addr[INET6_ADDRSTRLEN];
831                 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
832                            addr, sizeof addr);
833                 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
834                 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
835                 sprintf( l.sl_name, "IP=%s %d", addr, port );
836         } break;
837 #endif /* LDAP_PF_INET6 */
838
839         default:
840 #ifdef NEW_LOGGING
841                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
842                            "slap_open_listener: unsupported address family (%d)\n",
843                            (int)(*sal)->sa_family ));
844 #else
845                 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
846                         (int) (*sal)->sa_family, 0, 0 );
847 #endif
848                 break;
849         }
850
851         AC_MEMCPY(&l.sl_sa, *sal, addrlen);
852         l.sl_url = ch_strdup( url );
853         li = ch_malloc( sizeof( Listener ) );
854         *li = l;
855         slap_listeners[*cur] = li;
856         (*cur)++;
857         sal++;
858
859         } /* while ( *sal != NULL ) */
860
861         slap_free_listener_addresses(psal);
862
863
864 #ifdef NEW_LOGGING
865         LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
866                    "slap_open_listener: daemon initialized %s\n", l.sl_url ));
867 #else
868         Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
869                 l.sl_url, 0, 0 );
870 #endif
871         return 0;
872 }
873
874 static int sockinit(void);
875 static int sockdestroy(void);
876
877 int slapd_daemon_init( const char *urls )
878 {
879         int i, j, n, rc;
880         char **u;
881
882 #ifdef NEW_LOGGING
883         LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
884                    "slapd_daemon_init: %s\n",
885                    urls ? urls : "<null>" ));
886 #else
887         Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
888                 urls ? urls : "<null>", 0, 0 );
889 #endif
890         if( (rc = sockinit()) != 0 ) {
891                 return rc;
892         }
893
894 #ifdef HAVE_SYSCONF
895         dtblsize = sysconf( _SC_OPEN_MAX );
896 #elif HAVE_GETDTABLESIZE
897         dtblsize = getdtablesize();
898 #else
899         dtblsize = FD_SETSIZE;
900 #endif
901
902 #ifdef FD_SETSIZE
903         if(dtblsize > FD_SETSIZE) {
904                 dtblsize = FD_SETSIZE;
905         }
906 #endif  /* !FD_SETSIZE */
907
908         /* open a pipe (or something equivalent connected to itself).
909          * we write a byte on this fd whenever we catch a signal. The main
910          * loop will be select'ing on this socket, and will wake up when
911          * this byte arrives.
912          */
913         if( (rc = lutil_pair( wake_sds )) < 0 ) {
914 #ifdef NEW_LOGGING
915                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
916                            "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
917 #else
918                 Debug( LDAP_DEBUG_ANY,
919                         "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
920 #endif
921                 return rc;
922         }
923
924         FD_ZERO( &slap_daemon.sd_readers );
925         FD_ZERO( &slap_daemon.sd_writers );
926
927         if( urls == NULL ) {
928                 urls = "ldap:///";
929         }
930
931         u = str2charray( urls, " " );
932
933         if( u == NULL || u[0] == NULL ) {
934 #ifdef NEW_LOGGING
935                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
936                            "slap_daemon_init: no urls (%s) provided.\n", urls ));
937 #else
938                 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
939                         urls, 0, 0 );
940 #endif
941                 return -1;
942         }
943
944         for( i=0; u[i] != NULL; i++ ) {
945 #ifdef NEW_LOGGING
946                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
947                            "slap_daemon_init: listen on %s\n.", u[i] ));
948 #else
949                 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
950                         u[i], 0, 0 );
951 #endif
952         }
953
954         if( i == 0 ) {
955 #ifdef NEW_LOGGING
956                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
957                            "slap_daemon_init: no listeners to open (%s)\n", urls ));
958 #else
959                 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
960                         urls, 0, 0 );
961 #endif
962                 charray_free( u );
963                 return -1;
964         }
965
966 #ifdef NEW_LOGGING
967         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
968                    "slap_daemon_init: %d listeners to open...\n", i ));
969 #else
970         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
971                 i, 0, 0 );
972 #endif
973         slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
974
975         for(n = 0, j = 0; u[n]; n++ ) {
976                 if ( slap_open_listener( u[n], &i, &j ) ) {
977                         charray_free( u );
978                         return -1;
979                 }
980         }
981         slap_listeners[j] = NULL;
982
983 #ifdef NEW_LOGGING
984         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
985                    "slap_daemon_init: %d listeners opened\n", i ));
986 #else
987         Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
988                 i, 0, 0 );
989 #endif
990
991 #ifdef HAVE_SLP
992         slapd_slp_init( urls );
993         slapd_slp_reg();
994 #endif
995
996         charray_free( u );
997         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
998         return !i;
999 }
1000
1001
1002 int
1003 slapd_daemon_destroy(void)
1004 {
1005         connections_destroy();
1006         tcp_close( wake_sds[1] );
1007         tcp_close( wake_sds[0] );
1008         sockdestroy();
1009
1010 #ifdef HAVE_SLP
1011         slapd_slp_dereg();
1012         slapd_slp_deinit();
1013 #endif
1014
1015         return 0;
1016 }
1017
1018
1019 static void
1020 close_listeners(
1021         int remove
1022 )
1023 {
1024         int l;
1025
1026         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1027                 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1028                         if ( remove )
1029                                 slapd_remove( slap_listeners[l]->sl_sd, 0 );
1030 #ifdef LDAP_PF_LOCAL
1031                         if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1032                                 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1033                         }
1034 #endif /* LDAP_PF_LOCAL */
1035                         slapd_close( slap_listeners[l]->sl_sd );
1036                 }
1037                 if ( slap_listeners[l]->sl_url )
1038                         free ( slap_listeners[l]->sl_url );
1039                 if ( slap_listeners[l]->sl_name )
1040                         free ( slap_listeners[l]->sl_name );
1041                 free ( slap_listeners[l] );
1042                 slap_listeners[l] = NULL;
1043         }
1044 }
1045
1046
1047 static void *
1048 slapd_daemon_task(
1049         void *ptr
1050 )
1051 {
1052         int l;
1053         time_t  last_idle_check = 0;
1054         time( &starttime );
1055
1056         if ( global_idletimeout > 0 ) {
1057                 last_idle_check = slap_get_time();
1058         }
1059         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1060                 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1061                         continue;
1062 #ifdef LDAP_CONNECTIONLESS
1063                 /* Since this is connectionless, the data port is the
1064                  * listening port. The listen() and accept() calls
1065                  * are unnecessary.
1066                  */
1067                 if ( slap_listeners[l]->sl_is_udp )
1068                 {
1069                         slapd_add( slap_listeners[l]->sl_sd );
1070                         continue;
1071                 }
1072 #endif
1073
1074                 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1075                         int err = sock_errno();
1076
1077 #ifdef LDAP_PF_INET6
1078                         /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1079                          * we are already listening to in6addr_any, then we want to ignore
1080                          * this and continue.
1081                          */
1082                         if ( err == EADDRINUSE ) {
1083                                 int i;
1084                                 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1085                                 struct sockaddr_in6 sa6;
1086                                 
1087                                 if ( sa.sin_family == AF_INET &&
1088                                      sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1089                                         for ( i = 0 ; i < l; i++ ) {
1090                                                 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1091                                                 if ( sa6.sin6_family == AF_INET6 &&
1092                                                      !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1093                                                         break;
1094                                         }
1095
1096                                         if ( i < l ) {
1097                                                 /* We are already listening to in6addr_any */
1098 #ifdef NEW_LOGGING
1099                                                 LDAP_LOG(( "connection", LDAP_LEVEL_WARNING,
1100                                                            "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n" ));
1101 #else
1102                                                 Debug( LDAP_DEBUG_CONNS,
1103                                                        "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1104                                                        0, 0, 0 );
1105 #endif
1106                                                 slapd_close( slap_listeners[l]->sl_sd );
1107                                                 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1108                                                 continue;
1109                                         }
1110                                 }
1111                         }
1112 #endif                          
1113 #ifdef NEW_LOGGING
1114                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1115                                    "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1116                                    slap_listeners[l]->sl_url, err, sock_errstr(err) ));
1117 #else
1118                         Debug( LDAP_DEBUG_ANY,
1119                                 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1120                                         slap_listeners[l]->sl_url, err,
1121                                         sock_errstr(err) );
1122 #endif
1123                         return( (void*)-1 );
1124                 }
1125
1126                 slapd_add( slap_listeners[l]->sl_sd );
1127         }
1128
1129 #ifdef HAVE_NT_SERVICE_MANAGER
1130         if ( started_event != NULL ) {
1131                 ldap_pvt_thread_cond_signal( &started_event );
1132         }
1133 #endif
1134         /* initialization complete. Here comes the loop. */
1135
1136         while ( !slapd_shutdown ) {
1137                 ber_socket_t i;
1138                 int ns;
1139                 int at;
1140                 ber_socket_t nfds;
1141 #define SLAPD_EBADF_LIMIT 16
1142                 int ebadf = 0;
1143                 int emfile = 0;
1144
1145 #define SLAPD_IDLE_CHECK_LIMIT 4
1146                 time_t  now;
1147
1148
1149                 fd_set                  readfds;
1150                 fd_set                  writefds;
1151                 Sockaddr                from;
1152
1153 #if defined(SLAPD_RLOOKUPS)
1154                 struct hostent          *hp;
1155 #endif
1156                 struct timeval          zero;
1157                 struct timeval          *tvp;
1158
1159                 if( emfile ) {
1160                         now = slap_get_time();
1161                         connections_timeout_idle( now );
1162                 }
1163                 else if ( global_idletimeout > 0 ) {
1164                         now = slap_get_time();
1165                         if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1166                                 connections_timeout_idle( now );
1167                         }
1168                 }
1169
1170 #ifdef SIGHUP
1171                 if( slapd_gentle_shutdown ) {
1172                         ber_socket_t active;
1173
1174                         if( slapd_gentle_shutdown == 1 ) {
1175                                 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1176                                 close_listeners( 1 );
1177                                 global_restrictops |= SLAP_RESTRICT_OP_WRITES;
1178                                 slapd_gentle_shutdown = 2;
1179                         }
1180
1181                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1182                         active = slap_daemon.sd_nactives;
1183                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1184                         if( active == 0 ) {
1185                                 slapd_shutdown = 2;
1186                                 break;
1187                         }
1188                 }
1189 #endif
1190
1191                 FD_ZERO( &writefds );
1192                 FD_ZERO( &readfds );
1193
1194                 zero.tv_sec = 0;
1195                 zero.tv_usec = 0;
1196
1197                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1198
1199 #ifdef FD_SET_MANUAL_COPY
1200                 for( s = 0; s < nfds; s++ ) {
1201                         if(FD_ISSET( &slap_sd_readers, s )) {
1202                                 FD_SET( s, &readfds );
1203                         }
1204                         if(FD_ISSET( &slap_sd_writers, s )) {
1205                                 FD_SET( s, &writefds );
1206                         }
1207                 }
1208 #else
1209                 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1210                 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1211 #endif
1212                 assert(!FD_ISSET(wake_sds[0], &readfds));
1213                 FD_SET( wake_sds[0], &readfds );
1214
1215                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1216                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1217                                 continue;
1218                         if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1219                             FD_SET( slap_listeners[l]->sl_sd, &readfds );
1220                 }
1221
1222 #ifndef HAVE_WINSOCK
1223                 nfds = slap_daemon.sd_nfds;
1224 #else
1225                 nfds = dtblsize;
1226 #endif
1227
1228                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1229
1230                 at = ldap_pvt_thread_pool_backload(&connection_pool);
1231
1232 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1233                 tvp = NULL;
1234 #else
1235                 tvp = at ? &zero : NULL;
1236 #endif
1237
1238                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1239                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1240                                 continue;
1241
1242 #ifdef NEW_LOGGING
1243                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1244                                    "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1245                                    slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1246 #else
1247                         Debug( LDAP_DEBUG_CONNS,
1248                                 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1249                                         slap_listeners[l]->sl_sd, at,
1250                                         tvp == NULL ? "NULL" : "zero" );
1251 #endif
1252                 }
1253
1254                 switch(ns = select( nfds, &readfds,
1255 #ifdef HAVE_WINSOCK
1256                         /* don't pass empty fd_set */
1257                         ( writefds.fd_count > 0 ? &writefds : NULL ),
1258 #else
1259                         &writefds,
1260 #endif
1261                         NULL, tvp ))
1262                 {
1263                 case -1: {      /* failure - try again */
1264                                 int err = sock_errno();
1265
1266                                 if( err == EBADF
1267 #ifdef WSAENOTSOCK
1268                                         /* you'd think this would be EBADF */
1269                                         || err == WSAENOTSOCK
1270 #endif
1271                                 ) {
1272                                         if (++ebadf < SLAPD_EBADF_LIMIT)
1273                                                 continue;
1274                                 }
1275
1276                                 if( err != EINTR ) {
1277 #ifdef NEW_LOGGING
1278                                         LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1279                                                    "slapd_daemon_task: select failed (%d): %s\n",
1280                                                    err, sock_errstr(err) ));
1281 #else
1282                                         Debug( LDAP_DEBUG_CONNS,
1283                                                 "daemon: select failed (%d): %s\n",
1284                                                 err, sock_errstr(err), 0 );
1285 #endif
1286                                         slapd_shutdown = 2;
1287                                 }
1288                         }
1289                         continue;
1290
1291                 case 0:         /* timeout - let threads run */
1292                         ebadf = 0;
1293 #ifdef NEW_LOGGING
1294                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1295                                    "slapd_daemon_task: select timeout - yielding\n" ));
1296 #else
1297                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1298                             0, 0, 0 );
1299 #endif
1300                         ldap_pvt_thread_yield();
1301                         continue;
1302
1303                 default:        /* something happened - deal with it */
1304                         if( slapd_shutdown ) continue;
1305
1306                         ebadf = 0;
1307 #ifdef NEW_LOGGING
1308                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1309                                    "slapd_daemon_task: activity on %d descriptors\n", ns ));
1310 #else
1311                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1312                                 ns, 0, 0 );
1313 #endif
1314                         /* FALL THRU */
1315                 }
1316
1317                 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1318                         char c[BUFSIZ];
1319                         tcp_read( wake_sds[0], c, sizeof(c) );
1320 #ifdef NO_THREADS
1321                         waking = 0;
1322 #endif
1323                         continue;
1324                 }
1325
1326                 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1327                         ber_socket_t s;
1328                         socklen_t len = sizeof(from);
1329                         long id;
1330                         slap_ssf_t ssf = 0;
1331                         char *authid = NULL;
1332
1333                         char    *dnsname = NULL;
1334                         char    *peeraddr;
1335 #ifdef LDAP_PF_LOCAL
1336                         char    peername[MAXPATHLEN + sizeof("PATH=")];
1337 #elif defined(LDAP_PF_INET6)
1338                         char    peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1339 #else
1340                         char    peername[sizeof("IP=255.255.255.255:65336")];
1341 #endif /* LDAP_PF_LOCAL */
1342
1343                         peername[0] = '\0';
1344
1345                         if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1346                                 continue;
1347
1348                         if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1349                                 continue;
1350
1351 #ifdef LDAP_CONNECTIONLESS
1352                         if ( slap_listeners[l]->sl_is_udp )
1353                         {
1354                         /* The first time we receive a query, we set this
1355                          * up as a "connection". It remains open for the life
1356                          * of the slapd.
1357                          */
1358                                 if ( slap_listeners[l]->sl_is_udp < 2 )
1359                                 {
1360                                     id = connection_init(
1361                                         slap_listeners[l]->sl_sd,
1362                                         slap_listeners[l]->sl_url, "", "",
1363                                         slap_listeners[l]->sl_name,
1364                                         2, ssf, authid );
1365                                     slap_listeners[l]->sl_is_udp++;
1366                                 }
1367                                 continue;
1368                         }
1369 #endif
1370
1371                         s = accept( slap_listeners[l]->sl_sd,
1372                                 (struct sockaddr *) &from, &len );
1373                         if ( s == AC_SOCKET_INVALID ) {
1374                                 int err = sock_errno();
1375
1376 #ifdef EMFILE
1377                                 if( err == EMFILE ) {
1378                                         emfile++;
1379                                 } else
1380 #endif
1381 #ifdef ENFILE
1382                                 if( err == ENFILE ) {
1383                                         emfile++;
1384                                 } else 
1385 #endif
1386                                 {
1387                                         emfile=0;
1388                                 }
1389
1390                                 if( emfile < 3 ) {
1391 #ifdef NEW_LOGGING
1392                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1393                                                 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1394                                                 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1395 #else
1396                                         Debug( LDAP_DEBUG_ANY,
1397                                             "daemon: accept(%ld) failed errno=%d (%s)\n",
1398                                             (long) slap_listeners[l]->sl_sd, err,
1399                                             sock_errstr(err) );
1400 #endif
1401                                 } else {
1402                                         /* prevent busy loop */
1403 #  ifdef HAVE_USLEEP
1404                                         if( emfile % 4 == 3 ) usleep( 250 );
1405 #  else
1406                                         if( emfile % 8 == 7 ) sleep( 1 );
1407 #  endif
1408                                 }
1409
1410                                 ldap_pvt_thread_yield();
1411                                 continue;
1412                         }
1413                         emfile = 0;
1414
1415 #ifndef HAVE_WINSOCK
1416                         /* make sure descriptor number isn't too great */
1417                         if ( s >= dtblsize ) {
1418 #ifdef NEW_LOGGING
1419                                 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1420                                    "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1421                                    (long)s, (long)dtblsize ));
1422 #else
1423                                 Debug( LDAP_DEBUG_ANY,
1424                                         "daemon: %ld beyond descriptor table size %ld\n",
1425                                         (long) s, (long) dtblsize, 0 );
1426 #endif
1427
1428                                 slapd_close(s);
1429                                 ldap_pvt_thread_yield();
1430                                 continue;
1431                         }
1432 #endif
1433
1434 #ifdef LDAP_DEBUG
1435                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1436
1437                         /* newly accepted stream should not be in any of the FD SETS */
1438                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1439                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1440                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1441
1442                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1443 #endif
1444
1445 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1446 #ifdef LDAP_PF_LOCAL
1447                         /* for IPv4 and IPv6 sockets only */
1448                         if ( from.sa_addr.sa_family != AF_LOCAL )
1449 #endif /* LDAP_PF_LOCAL */
1450                         {
1451                                 int rc;
1452                                 int tmp;
1453 #ifdef SO_KEEPALIVE
1454                                 /* enable keep alives */
1455                                 tmp = 1;
1456                                 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1457                                         (char *) &tmp, sizeof(tmp) );
1458                                 if ( rc == AC_SOCKET_ERROR ) {
1459                                         int err = sock_errno();
1460 #ifdef NEW_LOGGING
1461                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1462                                                    "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1463                                                    (long)s, err, sock_errstr(err) ));
1464 #else
1465                                         Debug( LDAP_DEBUG_ANY,
1466                                                 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1467                                                 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1468 #endif
1469                                 }
1470 #endif
1471 #ifdef TCP_NODELAY
1472                                 /* enable no delay */
1473                                 tmp = 1;
1474                                 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1475                                         (char *)&tmp, sizeof(tmp) );
1476                                 if ( rc == AC_SOCKET_ERROR ) {
1477                                         int err = sock_errno();
1478 #ifdef NEW_LOGGING
1479                                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1480                                                    "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1481                                                    (long)s, err, sock_errstr(err) ));
1482 #else
1483                                         Debug( LDAP_DEBUG_ANY,
1484                                                 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1485                                                 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1486 #endif
1487                                 }
1488 #endif
1489                         }
1490 #endif
1491
1492 #ifdef NEW_LOGGING
1493                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1494                                    "slapd_daemon_task: new connection on %ld\n", (long)s ));
1495 #else
1496                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1497                                 (long) s, 0, 0 );
1498 #endif
1499                         switch ( from.sa_addr.sa_family ) {
1500 #  ifdef LDAP_PF_LOCAL
1501                         case AF_LOCAL:
1502                                 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1503                                 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1504                                 dnsname = "local";
1505                                 break;
1506 #endif /* LDAP_PF_LOCAL */
1507
1508 #  ifdef LDAP_PF_INET6
1509                         case AF_INET6:
1510                         if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1511                                 peeraddr = inet_ntoa( *((struct in_addr *)
1512                                                         &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1513                                 sprintf( peername, "IP=%s:%d",
1514                                          peeraddr != NULL ? peeraddr : "unknown",
1515                                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1516                         } else {
1517                                 char addr[INET6_ADDRSTRLEN];
1518
1519                                 peeraddr = (char *) inet_ntop( AF_INET6,
1520                                                       &from.sa_in6_addr.sin6_addr,
1521                                                       addr, sizeof addr );
1522                                 sprintf( peername, "IP=%s %d",
1523                                          peeraddr != NULL ? peeraddr : "unknown",
1524                                          (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1525                         }
1526                         break;
1527 #  endif /* LDAP_PF_INET6 */
1528
1529                         case AF_INET:
1530                         peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1531                         sprintf( peername, "IP=%s:%d",
1532                                 peeraddr != NULL ? peeraddr : "unknown",
1533                                 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1534                                 break;
1535
1536                         default:
1537                                 slapd_close(s);
1538                                 continue;
1539                         }
1540
1541                         if ( ( from.sa_addr.sa_family == AF_INET )
1542 #ifdef LDAP_PF_INET6
1543                                 || ( from.sa_addr.sa_family == AF_INET6 )
1544 #endif
1545                         ) {
1546 #ifdef SLAPD_RLOOKUPS
1547                                 if ( use_reverse_lookup ) {
1548 #  ifdef LDAP_PF_INET6
1549                                         if ( from.sa_addr.sa_family == AF_INET6 )
1550                                                 hp = gethostbyaddr(
1551                                                         (char *)&(from.sa_in6_addr.sin6_addr),
1552                                                         sizeof(from.sa_in6_addr.sin6_addr),
1553                                                         AF_INET6 );
1554                                         else
1555 #  endif /* LDAP_PF_INET6 */
1556                                         hp = gethostbyaddr(
1557                                                 (char *) &(from.sa_in_addr.sin_addr),
1558                                                 sizeof(from.sa_in_addr.sin_addr),
1559                                                 AF_INET );
1560                                         dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1561                                 }
1562 #else
1563                                 dnsname = NULL;
1564 #endif /* SLAPD_RLOOKUPS */
1565
1566 #ifdef HAVE_TCPD
1567                                 if ( !hosts_ctl("slapd",
1568                                                 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1569                                                 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1570                                                 STRING_UNKNOWN ))
1571                                 {
1572                                         /* DENY ACCESS */
1573                                         Statslog( LDAP_DEBUG_ANY,
1574                                                 "fd=%ld host access from %s (%s) denied.\n",
1575                                                 (long) s,
1576                                                 dnsname != NULL ? dnsname : "unknown",
1577                                                 peeraddr != NULL ? peeraddr : "unknown",
1578                                                 0, 0 );
1579                                         slapd_close(s);
1580                                         continue;
1581                                 }
1582 #endif /* HAVE_TCPD */
1583                         }
1584
1585                         id = connection_init(s,
1586                                 slap_listeners[l]->sl_url,
1587                                 dnsname != NULL ? dnsname : "unknown",
1588                                 peername,
1589                                 slap_listeners[l]->sl_name,
1590 #ifdef HAVE_TLS
1591                                 slap_listeners[l]->sl_is_tls,
1592 #else
1593                                 0,
1594 #endif
1595                                 ssf,
1596                                 authid );
1597
1598                         if( authid ) ch_free(authid);
1599
1600                         if( id < 0 ) {
1601 #ifdef NEW_LOGGING
1602                                 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1603                                            "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1604                                            (long)s, peername, slap_listeners[l]->sl_name ));
1605 #else
1606                                 Debug( LDAP_DEBUG_ANY,
1607                                         "daemon: connection_init(%ld, %s, %s) failed.\n",
1608                                         (long) s,
1609                                         peername,
1610                                         slap_listeners[l]->sl_name );
1611 #endif
1612                                 slapd_close(s);
1613                                 continue;
1614                         }
1615
1616                         Statslog( LDAP_DEBUG_STATS,
1617                                 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1618                                 id, (long) s,
1619                                 peername,
1620                                 slap_listeners[l]->sl_name,
1621                                 0 );
1622
1623                         slapd_add( s );
1624                         continue;
1625                 }
1626
1627 #ifdef LDAP_DEBUG
1628 #ifdef NEW_LOGGING
1629                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1630                            "slapd_daemon_task: activity on " ));
1631 #else
1632                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1633 #endif
1634 #ifdef HAVE_WINSOCK
1635                 for ( i = 0; i < readfds.fd_count; i++ ) {
1636 #ifdef NEW_LOGGING
1637                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1638                                    " %d%s", readfds.fd_array[i], "r", 0 ));
1639 #else
1640                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1641                                 readfds.fd_array[i], "r", 0 );
1642 #endif
1643                 }
1644                 for ( i = 0; i < writefds.fd_count; i++ ) {
1645 #ifdef NEW_LOGGING
1646                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1647                                    " %d%s", writefds.fd_array[i], "w" ));
1648 #else
1649                         Debug( LDAP_DEBUG_CONNS, " %d%s",
1650                                 writefds.fd_array[i], "w", 0 );
1651 #endif
1652                 }
1653
1654 #else
1655                 for ( i = 0; i < nfds; i++ ) {
1656                         int     r, w;
1657                         int     is_listener = 0;
1658
1659                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1660                                 if ( i == slap_listeners[l]->sl_sd ) {
1661 #ifdef LDAP_CONNECTIONLESS
1662                                 /* The listener is the data port. Don't
1663                                  * skip it.
1664                                  */
1665                                         if (slap_listeners[l]->sl_is_udp)
1666                                                 continue;
1667 #endif
1668                                         is_listener = 1;
1669                                         break;
1670                                 }
1671                         }
1672                         if ( is_listener ) {
1673                                 continue;
1674                         }
1675                         r = FD_ISSET( i, &readfds );
1676                         w = FD_ISSET( i, &writefds );
1677                         if ( r || w ) {
1678 #ifdef NEW_LOGGING
1679                                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1680                                            " %d%s%s", i,
1681                                            r ? "r" : "", w ? "w" : "" ));
1682 #else
1683                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1684                                     r ? "r" : "", w ? "w" : "" );
1685 #endif
1686                         }
1687                 }
1688 #endif
1689 #ifdef NEW_LOGGING
1690                 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1691 #else
1692                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1693 #endif
1694
1695 #endif
1696
1697                 /* loop through the writers */
1698 #ifdef HAVE_WINSOCK
1699                 for ( i = 0; i < writefds.fd_count; i++ )
1700 #else
1701                 for ( i = 0; i < nfds; i++ )
1702 #endif
1703                 {
1704                         ber_socket_t wd;
1705                         int is_listener = 0;
1706 #ifdef HAVE_WINSOCK
1707                         wd = writefds.fd_array[i];
1708 #else
1709                         if( ! FD_ISSET( i, &writefds ) ) {
1710                                 continue;
1711                         }
1712                         wd = i;
1713 #endif
1714
1715                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1716                                 if ( i == slap_listeners[l]->sl_sd ) {
1717 #ifdef LDAP_CONNECTIONLESS
1718                                         if (slap_listeners[l]->sl_is_udp)
1719                                                 continue;
1720 #endif
1721                                         is_listener = 1;
1722                                         break;
1723                                 }
1724                         }
1725                         if ( is_listener ) {
1726                                 continue;
1727                         }
1728 #ifdef NEW_LOGGING
1729                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1730                                    "slapd_daemon_task: write active on %d\n", wd ));
1731 #else
1732                         Debug( LDAP_DEBUG_CONNS,
1733                                 "daemon: write active on %d\n",
1734                                 wd, 0, 0 );
1735 #endif
1736                         /*
1737                          * NOTE: it is possible that the connection was closed
1738                          * and that the stream is now inactive.
1739                          * connection_write() must valid the stream is still
1740                          * active.
1741                          */
1742
1743                         if ( connection_write( wd ) < 0 ) {
1744                                 FD_CLR( (unsigned) wd, &readfds );
1745                                 slapd_close( wd );
1746                         }
1747                 }
1748
1749 #ifdef HAVE_WINSOCK
1750                 for ( i = 0; i < readfds.fd_count; i++ )
1751 #else
1752                 for ( i = 0; i < nfds; i++ )
1753 #endif
1754                 {
1755                         ber_socket_t rd;
1756                         int is_listener = 0;
1757
1758 #ifdef HAVE_WINSOCK
1759                         rd = readfds.fd_array[i];
1760 #else
1761                         if( ! FD_ISSET( i, &readfds ) ) {
1762                                 continue;
1763                         }
1764                         rd = i;
1765 #endif
1766
1767                         for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1768                                 if ( rd == slap_listeners[l]->sl_sd ) {
1769 #ifdef LDAP_CONNECTIONLESS
1770                                         if (slap_listeners[l]->sl_is_udp)
1771                                                 continue;
1772 #endif
1773                                         is_listener = 1;
1774                                         break;
1775                                 }
1776                         }
1777                         if ( is_listener ) {
1778                                 continue;
1779                         }
1780
1781 #ifdef NEW_LOGGING
1782                         LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1783                                    "slapd_daemon_task: read activity on %d\n", rd ));
1784 #else
1785                         Debug ( LDAP_DEBUG_CONNS,
1786                                 "daemon: read activity on %d\n", rd, 0, 0 );
1787 #endif
1788                         /*
1789                          * NOTE: it is possible that the connection was closed
1790                          * and that the stream is now inactive.
1791                          * connection_read() must valid the stream is still
1792                          * active.
1793                          */
1794
1795                         if ( connection_read( rd ) < 0 ) {
1796                                 slapd_close( rd );
1797                         }
1798                 }
1799                 ldap_pvt_thread_yield();
1800         }
1801
1802         if( slapd_shutdown == 1 ) {
1803 #ifdef NEW_LOGGING
1804                 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1805                            "slapd_daemon_task: shutdown requested and initiated.\n"));
1806 #else
1807                 Debug( LDAP_DEBUG_TRACE,
1808                         "daemon: shutdown requested and initiated.\n",
1809                         0, 0, 0 );
1810 #endif
1811
1812         } else if ( slapd_shutdown == 2 ) {
1813 #ifdef HAVE_NT_SERVICE_MANAGER
1814 #ifdef NEW_LOGGING
1815                         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1816                                    "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1817 #else
1818                         Debug( LDAP_DEBUG_TRACE,
1819                                "daemon: shutdown initiated by Service Manager.\n",
1820                                0, 0, 0);
1821 #endif
1822 #else /* !HAVE_NT_SERVICE_MANAGER */
1823 #ifdef NEW_LOGGING
1824                         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1825                                    "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1826 #else
1827                         Debug( LDAP_DEBUG_TRACE,
1828                                "daemon: abnormal condition, shutdown initiated.\n",
1829                                0, 0, 0 );
1830 #endif
1831 #endif /* !HAVE_NT_SERVICE_MANAGER */
1832         } else {
1833 #ifdef NEW_LOGGING
1834                 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1835                            "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1836 #else
1837                 Debug( LDAP_DEBUG_TRACE,
1838                        "daemon: no active streams, shutdown initiated.\n",
1839                        0, 0, 0 );
1840 #endif
1841         }
1842
1843         if( slapd_gentle_shutdown != 2 )
1844                 close_listeners ( 0 );
1845         free ( slap_listeners );
1846         slap_listeners = NULL;
1847
1848 #ifdef NEW_LOGGING
1849         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1850                    "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1851                    ldap_pvt_thread_pool_backload(&connection_pool) ));
1852 #else
1853         Debug( LDAP_DEBUG_ANY,
1854             "slapd shutdown: waiting for %d threads to terminate\n",
1855             ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1856 #endif
1857         ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1858
1859         return NULL;
1860 }
1861
1862
1863 int slapd_daemon( void )
1864 {
1865         int rc;
1866
1867         connections_init();
1868
1869 #define SLAPD_LISTENER_THREAD 1
1870 #if defined( SLAPD_LISTENER_THREAD )
1871         {
1872                 ldap_pvt_thread_t       listener_tid;
1873
1874                 /* listener as a separate THREAD */
1875                 rc = ldap_pvt_thread_create( &listener_tid,
1876                         0, slapd_daemon_task, NULL );
1877
1878                 if ( rc != 0 ) {
1879 #ifdef NEW_LOGGING
1880                         LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1881                                    "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1882 #else
1883                         Debug( LDAP_DEBUG_ANY,
1884                         "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1885 #endif
1886                         return rc;
1887                 }
1888  
1889                 /* wait for the listener thread to complete */
1890                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1891         }
1892 #else
1893         /* experimental code */
1894         slapd_daemon_task( NULL );
1895 #endif
1896
1897         return 0;
1898
1899 }
1900
1901 int sockinit(void)
1902 {
1903 #if defined( HAVE_WINSOCK2 )
1904     WORD wVersionRequested;
1905         WSADATA wsaData;
1906         int err;
1907
1908         wVersionRequested = MAKEWORD( 2, 0 );
1909
1910         err = WSAStartup( wVersionRequested, &wsaData );
1911         if ( err != 0 ) {
1912                 /* Tell the user that we couldn't find a usable */
1913                 /* WinSock DLL.                                  */
1914                 return -1;
1915         }
1916
1917         /* Confirm that the WinSock DLL supports 2.0.*/
1918         /* Note that if the DLL supports versions greater    */
1919         /* than 2.0 in addition to 2.0, it will still return */
1920         /* 2.0 in wVersion since that is the version we      */
1921         /* requested.                                        */
1922
1923         if ( LOBYTE( wsaData.wVersion ) != 2 ||
1924                 HIBYTE( wsaData.wVersion ) != 0 )
1925         {
1926             /* Tell the user that we couldn't find a usable */
1927             /* WinSock DLL.                                  */
1928             WSACleanup();
1929             return -1;
1930         }
1931
1932         /* The WinSock DLL is acceptable. Proceed. */
1933 #elif defined( HAVE_WINSOCK )
1934         WSADATA wsaData;
1935         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1936             return -1;
1937         }
1938 #endif
1939         return 0;
1940 }
1941
1942 int sockdestroy(void)
1943 {
1944 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1945         WSACleanup();
1946 #endif
1947         return 0;
1948 }
1949
1950 RETSIGTYPE
1951 slap_sig_shutdown( int sig )
1952 {
1953 #ifdef NEW_LOGGING
1954         LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1955                    "slap_sig_shutdown: signal %d\n", sig ));
1956 #else
1957         Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1958 #endif
1959
1960         /*
1961          * If the NT Service Manager is controlling the server, we don't
1962          * want SIGBREAK to kill the server. For some strange reason,
1963          * SIGBREAK is generated when a user logs out.
1964          */
1965
1966 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1967         if (is_NT_Service && sig == SIGBREAK)
1968 #ifdef NEW_LOGGING
1969             LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1970                        "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1971 #else
1972             Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1973                   0, 0, 0);
1974 #endif
1975         else
1976 #endif
1977 #ifdef SIGHUP
1978         if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
1979                 slapd_gentle_shutdown = 1;
1980         else
1981 #endif
1982         slapd_shutdown = 1;
1983
1984         WAKE_LISTENER(1);
1985
1986         /* reinstall self */
1987         (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1988 }
1989
1990 RETSIGTYPE
1991 slap_sig_wake( int sig )
1992 {
1993         WAKE_LISTENER(1);
1994
1995         /* reinstall self */
1996         (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1997 }
1998
1999
2000 void slapd_add_internal(ber_socket_t s) {
2001         slapd_add(s);
2002 }
2003
2004 Listener ** slapd_get_listeners(void) {
2005         return slap_listeners;
2006 }