2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
35 #include <ac/unistd.h>
38 #include "ldap_pvt_thread.h"
43 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
44 # include <sys/epoll.h>
49 int allow_severity = LOG_INFO;
50 int deny_severity = LOG_NOTICE;
52 # define SLAP_STRING_UNKNOWN STRING_UNKNOWN
53 #else /* ! TCP Wrappers */
54 # define SLAP_STRING_UNKNOWN "unknown"
55 #endif /* ! TCP Wrappers */
58 # include <sys/stat.h>
59 /* this should go in <ldap.h> as soon as it is accepted */
60 # define LDAPI_MOD_URLEXT "x-mod"
61 #endif /* LDAP_PF_LOCAL */
64 int slap_inet4or6 = AF_UNSPEC;
66 int slap_inet4or6 = AF_INET;
71 ber_socket_t dtblsize;
72 slap_ssf_t local_ssf = LDAP_PVT_SASL_LOCAL_SSF;
73 struct runqueue_s slapd_rq;
75 Listener **slap_listeners = NULL;
77 #ifndef SLAPD_LISTEN_BACKLOG
78 #define SLAPD_LISTEN_BACKLOG 1024
81 static ber_socket_t wake_sds[2];
85 #define WAKE_LISTENER(w) do { \
86 if ((w) && waking < 5) { \
88 tcp_write( wake_sds[1], "0", 1 ); \
92 volatile sig_atomic_t slapd_shutdown = 0;
93 volatile sig_atomic_t slapd_gentle_shutdown = 0;
94 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
96 static struct slap_daemon {
97 ldap_pvt_thread_mutex_t sd_mutex;
99 ber_socket_t sd_nactives;
103 struct epoll_event *sd_epolls;
108 # ifdef SLAP_LIGHTWEIGHT_LISTENER
109 int *sd_suspend; /* 0: suspended, 1: not suspended */
114 /* In winsock, accept() returns values higher than dtblsize
115 so don't bother with this optimization */
121 # ifdef SLAP_LIGHTWEIGHT_LISTENER
122 fd_set sd_suspend; /* unset: suspended, set: not suspended */
128 # define SLAP_EVENTS_ARE_INDEXED 0
129 # define SLAP_SOCK_IX(s) (slap_daemon.sd_index[(s)])
130 # define SLAP_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_SOCK_IX(s)])
131 # define SLAP_SOCK_EV(s) (SLAP_SOCK_EP(s).events)
132 # define SLAP_SOCK_IS_ACTIVE(s) (SLAP_SOCK_IX(s) != -1)
133 # define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_SOCK_IX(s) == -1)
134 # define SLAP_SOCK_IS_SET(s, mode) (SLAP_SOCK_EV(s) & (mode))
136 # define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), EPOLLIN)
137 # define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), EPOLLOUT)
139 # define SLAP_SET_SOCK(s, mode) do { \
140 if ((SLAP_SOCK_EV(s) & (mode)) != (mode)) { \
141 SLAP_SOCK_EV(s) |= (mode); \
142 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
147 # define SLAP_CLR_SOCK(s, mode) do { \
148 if ((SLAP_SOCK_EV(s) & (mode))) { \
149 SLAP_SOCK_EV(s) &= ~(mode); \
150 epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
155 # define SLAP_SOCK_SET_READ(s) SLAP_SET_SOCK(s, EPOLLIN)
156 # define SLAP_SOCK_SET_WRITE(s) SLAP_SET_SOCK(s, EPOLLOUT)
158 # ifdef SLAP_LIGHTWEIGHT_LISTENER
159 # define SLAP_SOCK_SET_SUSPEND(s) \
160 ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 1 )
161 # define SLAP_SOCK_CLR_SUSPEND(s) \
162 ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 0 )
163 # define SLAP_SOCK_IS_SUSPEND(s) \
164 ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] == 1 )
167 # define SLAP_SOCK_CLR_READ(s) SLAP_CLR_SOCK((s), EPOLLIN)
168 # define SLAP_SOCK_CLR_WRITE(s) SLAP_CLR_SOCK((s), EPOLLOUT)
170 # define SLAP_CLR_EVENT(i, mode) (revents[(i)].events &= ~(mode))
172 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
174 /* If a Listener address is provided, store that as the epoll data.
175 * Otherwise, store the address of this socket's slot in the
176 * index array. If we can't do this add, the system is out of
177 * resources and we need to shutdown.
179 # define SLAP_ADD_SOCK(s, l) do { \
181 SLAP_SOCK_IX((s)) = slap_daemon.sd_nfds; \
182 SLAP_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_SOCK_IX(s)); \
183 SLAP_SOCK_EV((s)) = EPOLLIN; \
184 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
185 (s), &SLAP_SOCK_EP((s))); \
187 slap_daemon.sd_nfds++; \
189 Debug( LDAP_DEBUG_ANY, \
190 "daemon: epoll_ctl ADD failed, errno %d, shutting down\n", \
192 slapd_shutdown = 2; \
196 # define SLAP_EV_LISTENER(ptr) (((int *)(ptr) >= slap_daemon.sd_index && \
197 (int *)(ptr) <= (slap_daemon.sd_index+dtblsize)) ? 0 : 1 )
199 # define SLAP_EV_PTRFD(ptr) (SLAP_EV_LISTENER(ptr) ? \
200 ((Listener *)ptr)->sl_sd : (int *)(ptr) - slap_daemon.sd_index)
202 # ifdef SLAP_LIGHTWEIGHT_LISTENER
203 # define SLAP_DEL_SOCK(s) do { \
204 int fd, rc, suspend, index = SLAP_SOCK_IX((s)); \
205 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
206 (s), &SLAP_SOCK_EP((s))); \
207 slap_daemon.sd_epolls[index] = \
208 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
209 fd = SLAP_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
210 slap_daemon.sd_suspend[index] = \
211 slap_daemon.sd_suspend[slap_daemon.sd_nfds-1]; \
212 slap_daemon.sd_suspend[slap_daemon.sd_nfds-1] = 0; \
213 slap_daemon.sd_index[fd] = index; \
214 slap_daemon.sd_index[(s)] = -1; \
215 slap_daemon.sd_nfds--; \
218 # define SLAP_DEL_SOCK(s) do { \
219 int fd, rc, index = SLAP_SOCK_IX((s)); \
220 rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
221 (s), &SLAP_SOCK_EP((s))); \
222 slap_daemon.sd_epolls[index] = \
223 slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
224 fd = SLAP_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
225 slap_daemon.sd_index[fd] = index; \
226 slap_daemon.sd_index[(s)] = -1; \
227 slap_daemon.sd_nfds--; \
231 # define SLAP_EVENT_CLR_READ(i) SLAP_CLR_EVENT((i), EPOLLIN)
232 # define SLAP_EVENT_CLR_WRITE(i) SLAP_CLR_EVENT((i), EPOLLOUT)
234 # define SLAP_CHK_EVENT(i, mode) (revents[(i)].events & mode)
236 # define SLAP_EVENT_IS_READ(i) SLAP_CHK_EVENT((i), EPOLLIN)
237 # define SLAP_EVENT_IS_WRITE(i) SLAP_CHK_EVENT((i), EPOLLOUT)
238 # define SLAP_EVENT_IS_LISTENER(i) SLAP_EV_LISTENER(revents[(i)].data.ptr)
239 # define SLAP_EVENT_LISTENER(i) (revents[(i)].data.ptr)
241 # define SLAP_EVENT_FD(i) SLAP_EV_PTRFD(revents[(i)].data.ptr)
242 # define SLAP_SOCK_SET_MUTE(s) SLAP_SOCK_CLR_READ((s))
243 # define SLAP_SOCK_CLR_MUTE(s) SLAP_SOCK_SET_READ((s))
244 # define SLAP_SOCK_IS_MUTE(s) (!SLAP_SOCK_IS_READ((s)))
246 # ifdef SLAP_LIGHTWEIGHT_LISTENER
247 # define SLAP_SOCK_SET_INIT do { \
248 slap_daemon.sd_epolls = \
249 ch_malloc( sizeof(struct epoll_event) * dtblsize * 2 ); \
250 slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \
251 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
252 for (i=0; i<dtblsize; i++) slap_daemon.sd_index[i] = -1; \
253 slap_daemon.sd_suspend = ch_malloc(sizeof(int) * dtblsize); \
254 for (i=0; i<dtblsize; i++) slap_daemon.sd_suspend[i] = 0; \
257 # define SLAP_SOCK_SET_INIT do { \
258 slap_daemon.sd_epolls = ch_calloc(1, \
259 sizeof(struct epoll_event) * dtblsize * 2); \
260 slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \
261 slap_daemon.sd_epfd = epoll_create( dtblsize ); \
262 for (i=0; i<dtblsize; i++) slap_daemon.sd_index[i] = -1 \
266 # define SLAP_EVENT_DECL struct epoll_event *revents
268 # define SLAP_EVENT_INIT do { \
269 revents = slap_daemon.sd_epolls + dtblsize; \
272 # define SLAP_EVENT_WAIT(tvp) \
273 epoll_wait( slap_daemon.sd_epfd, revents, \
274 dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 )
279 # define SLAP_EVENTS_ARE_INDEXED 1
280 # define SLAP_EVENT_DECL \
281 fd_set readfds, writefds
283 # define SLAP_EVENT_INIT do { \
284 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \
286 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
288 FD_ZERO( &writefds ); \
293 # define CHK_SETSIZE do { \
294 if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
297 # define CHK_SETSIZE do { ; } while (0)
300 # ifdef SLAP_LIGHTWEIGHT_LISTENER
301 # define SLAP_SOCK_SET_INIT do { \
303 FD_ZERO(&slap_daemon.sd_readers); \
304 FD_ZERO(&slap_daemon.sd_writers); \
305 FD_ZERO(&slap_daemon.sd_suspend); \
308 # define SLAP_SOCK_SET_INIT do { \
310 FD_ZERO(&slap_daemon.sd_readers); \
311 FD_ZERO(&slap_daemon.sd_writers); \
315 # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives)
316 # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers)
317 # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers)
319 # define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
320 !SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
322 # ifdef SLAP_LIGHTWEIGHT_LISTENER
323 # define SLAP_SOCK_SET_SUSPEND(s) FD_SET((s), &slap_daemon.sd_suspend)
324 # define SLAP_SOCK_CLR_SUSPEND(s) FD_CLR((s), &slap_daemon.sd_suspend)
325 # define SLAP_SOCK_IS_SUSPEND(s) FD_ISSET((s), &slap_daemon.sd_suspend)
329 # define SLAP_SOCK_SET_READ(fd) do { \
330 if (!SLAP_SOCK_IS_READ(fd)) { FD_SET((fd), &slap_daemon.sd_readers); } \
332 # define SLAP_SOCK_SET_WRITE(fd) do { \
333 if (!SLAP_SOCK_IS_WRITE(fd)) { FD_SET((fd), &slap_daemon.sd_writers); } \
336 # define SLAP_ADDTEST(s) do { } while 0
337 # define SLAP_EVENT_MAX dtblsize
339 # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
340 # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers)
342 # define SLAP_EVENT_MAX slap_daemon.sd_nfds
343 # define SLAP_ADDTEST(s) do { \
344 if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
348 # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers)
349 # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers)
351 # define SLAP_ADD_SOCK(s, l) do { \
353 FD_SET((s), &slap_daemon.sd_actives); \
354 FD_SET((s), &slap_daemon.sd_readers); \
357 # define SLAP_DEL_SOCK(s) do { \
358 FD_CLR((s), &slap_daemon.sd_actives); \
359 FD_CLR((s), &slap_daemon.sd_readers); \
360 FD_CLR((s), &slap_daemon.sd_writers); \
363 # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds)
364 # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds)
366 # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds)
367 # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds)
369 # define SLAP_EVENT_WAIT(tvp) \
370 select( SLAP_EVENT_MAX, &readfds, \
371 nwriters > 0 ? &writefds : NULL, NULL, (tvp) )
373 # define SLAP_SOCK_SET_MUTE(s) FD_CLR((s), &readfds)
374 # define SLAP_SOCK_CLR_MUTE(s) FD_SET((s), &readfds)
375 # define SLAP_SOCK_IS_MUTE(s) (!FD_ISSET((s), &readfds))
380 * SLP related functions
384 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
385 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
386 static char** slapd_srvurls = NULL;
387 static SLPHandle slapd_hslp = 0;
388 int slapd_register_slp = 0;
390 void slapd_slp_init( const char* urls ) {
393 slapd_srvurls = ldap_str2charray( urls, " " );
395 if( slapd_srvurls == NULL ) return;
397 /* find and expand INADDR_ANY URLs */
398 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
399 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
400 char *host = ldap_pvt_get_fqdn( NULL );
401 if ( host != NULL ) {
402 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
404 sizeof( LDAP_SRVTYPE_PREFIX ) );
405 strcpy( lutil_strcopy(slapd_srvurls[i],
406 LDAP_SRVTYPE_PREFIX ), host );
411 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
412 char *host = ldap_pvt_get_fqdn( NULL );
413 if ( host != NULL ) {
414 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
416 sizeof( LDAPS_SRVTYPE_PREFIX ) );
417 strcpy( lutil_strcopy(slapd_srvurls[i],
418 LDAPS_SRVTYPE_PREFIX ), host );
425 /* open the SLP handle */
426 SLPOpen( "en", 0, &slapd_hslp );
429 void slapd_slp_deinit() {
430 if( slapd_srvurls == NULL ) return;
432 ldap_charray_free( slapd_srvurls );
433 slapd_srvurls = NULL;
435 /* close the SLP handle */
436 SLPClose( slapd_hslp );
439 void slapd_slp_regreport(
447 void slapd_slp_reg() {
450 if( slapd_srvurls == NULL ) return;
452 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
453 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
454 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
455 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
456 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
460 SLP_LIFETIME_MAXIMUM,
470 void slapd_slp_dereg() {
473 if( slapd_srvurls == NULL ) return;
475 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
476 SLPDereg( slapd_hslp,
482 #endif /* HAVE_SLP */
485 * Add a descriptor to daemon control
487 * If isactive, the descriptor is a live server session and is subject
488 * to idletimeout control. Otherwise, the descriptor is a passive
489 * listener or an outbound client session, and not subject to
490 * idletimeout. The underlying event handler may record the Listener
491 * argument to differentiate Listener's from real sessions.
493 static void slapd_add(ber_socket_t s, int isactive, Listener *sl) {
494 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
496 assert( SLAP_SOCK_NOT_ACTIVE(s) );
498 if ( isactive ) slap_daemon.sd_nactives++;
500 SLAP_ADD_SOCK(s, sl);
502 Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr\n",
505 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
507 #ifdef SLAP_LIGHTWEIGHT_LISTENER
513 * Remove the descriptor from daemon control
522 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
524 if ( wasactive ) slap_daemon.sd_nactives--;
525 waswriter = SLAP_SOCK_IS_WRITE(s);
527 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
528 (long) s, SLAP_SOCK_IS_READ(s) ? "r" : "",
529 waswriter ? "w" : "" );
530 if ( waswriter ) slap_daemon.sd_nwriters--;
532 #ifdef SLAP_LIGHTWEIGHT_LISTENER
533 SLAP_SOCK_CLR_SUSPEND(s);
537 /* If we ran out of file descriptors, we dropped a listener from
538 * the select() loop. Now that we're removing a session from our
539 * control, we can try to resume a dropped listener to use.
543 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
544 if ( slap_listeners[i]->sl_sd != AC_SOCKET_INVALID ) {
545 if ( slap_listeners[i]->sl_sd == s ) continue;
546 if ( slap_listeners[i]->sl_is_mute ) {
547 slap_listeners[i]->sl_is_mute = 0;
553 /* Walked the entire list without enabling anything; emfile
554 * counter is stale. Reset it.
556 if ( slap_listeners[i] == NULL ) emfile = 0;
558 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
559 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
562 #ifdef SLAP_LIGHTWEIGHT_LISTENER
564 * Temporarily suspend submitting events on the descriptor to the pool.
565 * Reading on the descriptor will be resumed by a connection procseeing thread
566 * when data (LDAP requests) on it are read.
567 * slapd_suspend() returns 1 when it is suspended otherwise returns 0
569 int slapd_suspend(ber_socket_t s) {
572 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
574 if ( !SLAP_SOCK_IS_SUSPEND( s ) && SLAP_SOCK_IS_ACTIVE( s ) &&
575 SLAP_SOCK_IS_READ( s ) )
577 SLAP_SOCK_SET_SUSPEND( s );
578 SLAP_SOCK_CLR_READ( s );
582 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
586 void slapd_resume ( ber_socket_t s ) {
587 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
589 SLAP_SOCK_SET_READ( s );
591 assert( SLAP_SOCK_IS_SUSPEND( s ) );
593 SLAP_SOCK_CLR_SUSPEND ( s );
595 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
601 void slapd_clr_write(ber_socket_t s, int wake) {
602 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
604 assert( SLAP_SOCK_IS_ACTIVE( s ));
606 if ( SLAP_SOCK_IS_WRITE( s )) {
607 SLAP_SOCK_CLR_WRITE( s );
608 slap_daemon.sd_nwriters--;
611 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
615 void slapd_set_write(ber_socket_t s, int wake) {
616 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
618 assert( SLAP_SOCK_IS_ACTIVE( s ));
620 if ( !SLAP_SOCK_IS_WRITE( s )) {
621 SLAP_SOCK_SET_WRITE( s );
622 slap_daemon.sd_nwriters++;
625 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
629 void slapd_clr_read(ber_socket_t s, int wake) {
630 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
632 assert( SLAP_SOCK_IS_ACTIVE( s ));
633 SLAP_SOCK_CLR_READ( s );
635 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
639 void slapd_set_read(ber_socket_t s, int wake) {
640 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
642 assert( SLAP_SOCK_IS_ACTIVE( s ));
643 if (!SLAP_SOCK_IS_READ( s ))
644 SLAP_SOCK_SET_READ( s );
646 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
650 static void slapd_close(ber_socket_t s) {
651 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
656 static void slap_free_listener_addresses(struct sockaddr **sal)
658 struct sockaddr **sap;
660 if (sal == NULL) return;
662 for (sap = sal; *sap != NULL; sap++) {
669 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
670 static int get_url_perms(
677 assert( exts != NULL );
678 assert( perms != NULL );
679 assert( crit != NULL );
682 for ( i = 0; exts[ i ]; i++ ) {
683 char *type = exts[ i ];
686 if ( type[ 0 ] == '!' ) {
691 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=",
692 sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 )
694 char *value = type + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
698 switch (strlen(value)) {
700 /* skip leading '0' */
701 if ( value[ 0 ] != '0' ) return LDAP_OTHER;
705 for ( j = 0; j < 3; j++) {
708 v = value[ j ] - '0';
710 if ( v < 0 || v > 7 ) return LDAP_OTHER;
717 for ( j = 1; j < 10; j++ ) {
718 static mode_t m[] = { 0,
719 S_IRUSR, S_IWUSR, S_IXUSR,
720 S_IRGRP, S_IWGRP, S_IXGRP,
721 S_IROTH, S_IWOTH, S_IXOTH
723 static char c[] = "-rwxrwxrwx";
725 if ( value[ j ] == c[ j ] ) {
728 } else if ( value[ j ] != '-' ) {
747 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
749 /* port = 0 indicates AF_LOCAL */
750 static int slap_get_listener_addresses(
753 struct sockaddr ***sal)
755 struct sockaddr **sap;
759 *sal = ch_malloc(2 * sizeof(void *));
760 if (*sal == NULL) return -1;
763 *sap = ch_malloc(sizeof(struct sockaddr_un));
764 if (*sap == NULL) goto errexit;
768 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) )
770 Debug( LDAP_DEBUG_ANY,
771 "daemon: domain socket path (%s) too long in URL",
776 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
777 (*sap)->sa_family = AF_LOCAL;
778 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
782 #ifdef HAVE_GETADDRINFO
783 struct addrinfo hints, *res, *sai;
787 memset( &hints, '\0', sizeof(hints) );
788 hints.ai_flags = AI_PASSIVE;
789 hints.ai_socktype = SOCK_STREAM;
790 hints.ai_family = slap_inet4or6;
791 snprintf(serv, sizeof serv, "%d", port);
793 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
794 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
795 AC_GAI_STRERROR(err), 0, 0);
800 for (n=2; (sai = sai->ai_next) != NULL; n++) {
803 *sal = ch_calloc(n, sizeof(void *));
804 if (*sal == NULL) return -1;
809 for ( sai=res; sai; sai=sai->ai_next ) {
810 if( sai->ai_addr == NULL ) {
811 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
812 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
817 switch (sai->ai_family) {
818 # ifdef LDAP_PF_INET6
820 *sap = ch_malloc(sizeof(struct sockaddr_in6));
825 *(struct sockaddr_in6 *)*sap =
826 *((struct sockaddr_in6 *)sai->ai_addr);
830 *sap = ch_malloc(sizeof(struct sockaddr_in));
835 *(struct sockaddr_in *)*sap =
836 *((struct sockaddr_in *)sai->ai_addr);
844 (*sap)->sa_family = sai->ai_family;
854 struct hostent *he = NULL;
856 if ( host == NULL ) {
857 in.s_addr = htonl(INADDR_ANY);
859 } else if ( !inet_aton( host, &in ) ) {
860 he = gethostbyname( host );
862 Debug( LDAP_DEBUG_ANY,
863 "daemon: invalid host %s", host, 0, 0);
866 for (n = 0; he->h_addr_list[n]; n++) ;
869 *sal = ch_malloc((n+1) * sizeof(void *));
875 for ( i = 0; i<n; i++ ) {
876 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
877 if (*sap == NULL) goto errexit;
878 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
879 sap[i]->sa_family = AF_INET;
880 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
882 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
883 he->h_addr_list[i], sizeof(struct in_addr) );
885 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
886 &in, sizeof(struct in_addr) );
896 slap_free_listener_addresses(*sal);
900 static int slap_open_listener(
911 int err, addrlen = 0;
912 struct sockaddr **sal, **psal;
913 int socktype = SOCK_STREAM; /* default to COTS */
915 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
920 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
922 rc = ldap_url_parse( url, &lud );
924 if( rc != LDAP_URL_SUCCESS ) {
925 Debug( LDAP_DEBUG_ANY,
926 "daemon: listen URL \"%s\" parse error=%d\n",
931 l.sl_url.bv_val = NULL;
935 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
936 Debug( LDAP_DEBUG_ANY,
937 "daemon: TLS not supported (%s)\n",
939 ldap_free_urldesc( lud );
943 if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
946 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
948 if(! lud->lud_port ) {
949 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
953 port = (unsigned short) lud->lud_port;
955 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
956 if ( tmp == LDAP_PROTO_IPC ) {
958 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
959 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
961 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
965 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
967 ldap_free_urldesc( lud );
971 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
972 || strcmp(lud->lud_host, "*") == 0 )
974 err = slap_get_listener_addresses(NULL, port, &sal);
976 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
980 #ifdef LDAP_CONNECTIONLESS
981 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
984 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
985 if ( lud->lud_exts ) {
986 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
988 l.sl_perms = S_IRWXU | S_IRWXO;
990 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
992 ldap_free_urldesc( lud );
993 if ( err ) return -1;
995 /* If we got more than one address returned, we need to make space
996 * for it in the slap_listeners array.
998 for ( num=0; sal[num]; num++ ) /* empty */;
1000 *listeners += num-1;
1001 slap_listeners = ch_realloc( slap_listeners,
1002 (*listeners + 1) * sizeof(Listener *) );
1006 while ( *sal != NULL ) {
1008 switch( (*sal)->sa_family ) {
1012 #ifdef LDAP_PF_INET6
1017 #ifdef LDAP_PF_LOCAL
1027 #ifdef LDAP_CONNECTIONLESS
1028 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
1031 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
1032 if ( l.sl_sd == AC_SOCKET_INVALID ) {
1033 int err = sock_errno();
1034 Debug( LDAP_DEBUG_ANY,
1035 "daemon: %s socket() failed errno=%d (%s)\n",
1036 af, err, sock_errstr(err) );
1041 #ifndef HAVE_WINSOCK
1042 if ( l.sl_sd >= dtblsize ) {
1043 Debug( LDAP_DEBUG_ANY,
1044 "daemon: listener descriptor %ld is too great %ld\n",
1045 (long) l.sl_sd, (long) dtblsize, 0 );
1046 tcp_close( l.sl_sd );
1052 #ifdef LDAP_PF_LOCAL
1053 if ( (*sal)->sa_family == AF_LOCAL ) {
1054 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
1059 /* enable address reuse */
1061 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
1062 (char *) &tmp, sizeof(tmp) );
1063 if ( rc == AC_SOCKET_ERROR ) {
1064 int err = sock_errno();
1065 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1066 "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
1067 (long) l.sl_sd, err, sock_errstr(err) );
1072 switch( (*sal)->sa_family ) {
1074 addrlen = sizeof(struct sockaddr_in);
1076 #ifdef LDAP_PF_INET6
1079 /* Try to use IPv6 sockets for IPv6 only */
1081 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
1082 (char *) &tmp, sizeof(tmp) );
1083 if ( rc == AC_SOCKET_ERROR ) {
1084 int err = sock_errno();
1085 Debug( LDAP_DEBUG_ANY, "slapd(%ld): "
1086 "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
1087 (long) l.sl_sd, err, sock_errstr(err) );
1090 addrlen = sizeof(struct sockaddr_in6);
1094 #ifdef LDAP_PF_LOCAL
1099 setsockopt(l.sl_sd, 0, LOCAL_CREDS, &one, sizeof one);
1102 addrlen = sizeof(struct sockaddr_un);
1107 if (bind(l.sl_sd, *sal, addrlen)) {
1109 Debug( LDAP_DEBUG_ANY,
1110 "daemon: bind(%ld) failed errno=%d (%s)\n",
1111 (long) l.sl_sd, err, sock_errstr(err) );
1112 tcp_close( l.sl_sd );
1117 switch ( (*sal)->sa_family ) {
1118 #ifdef LDAP_PF_LOCAL
1120 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
1121 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
1122 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
1123 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
1126 #endif /* LDAP_PF_LOCAL */
1130 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
1131 char addr[INET_ADDRSTRLEN];
1132 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
1133 addr, sizeof(addr) );
1136 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
1138 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
1140 ber_memalloc( sizeof("IP=255.255.255.255:65535") );
1141 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
1143 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
1144 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1147 #ifdef LDAP_PF_INET6
1149 char addr[INET6_ADDRSTRLEN];
1150 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
1152 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
1153 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
1154 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
1155 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
1157 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
1159 #endif /* LDAP_PF_INET6 */
1162 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
1163 (int) (*sal)->sa_family, 0, 0 );
1167 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
1168 ber_str2bv( url, 0, 1, &l.sl_url);
1169 li = ch_malloc( sizeof( Listener ) );
1171 slap_listeners[*cur] = li;
1176 slap_free_listener_addresses(psal);
1178 if ( l.sl_url.bv_val == NULL ) {
1179 Debug( LDAP_DEBUG_TRACE,
1180 "slap_open_listener: failed on %s\n", url, 0, 0 );
1184 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
1185 l.sl_url.bv_val, 0, 0 );
1189 static int sockinit(void);
1190 static int sockdestroy(void);
1192 int slapd_daemon_init( const char *urls )
1197 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1198 urls ? urls : "<null>", 0, 0 );
1199 if( (rc = sockinit()) != 0 ) {
1204 dtblsize = sysconf( _SC_OPEN_MAX );
1205 #elif HAVE_GETDTABLESIZE
1206 dtblsize = getdtablesize();
1208 dtblsize = FD_SETSIZE;
1211 /* open a pipe (or something equivalent connected to itself).
1212 * we write a byte on this fd whenever we catch a signal. The main
1213 * loop will be select'ing on this socket, and will wake up when
1214 * this byte arrives.
1216 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1217 Debug( LDAP_DEBUG_ANY,
1218 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1224 if( urls == NULL ) {
1228 u = ldap_str2charray( urls, " " );
1230 if( u == NULL || u[0] == NULL ) {
1231 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1236 for( i=0; u[i] != NULL; i++ ) {
1237 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1242 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1244 ldap_charray_free( u );
1248 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1250 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1252 for(n = 0, j = 0; u[n]; n++ ) {
1253 if ( slap_open_listener( u[n], &i, &j ) ) {
1254 ldap_charray_free( u );
1258 slap_listeners[j] = NULL;
1260 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1264 if( slapd_register_slp ) {
1265 slapd_slp_init( urls );
1270 ldap_charray_free( u );
1271 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1278 slapd_daemon_destroy(void)
1280 connections_destroy();
1281 tcp_close( wake_sds[1] );
1282 tcp_close( wake_sds[0] );
1286 if( slapd_register_slp ) {
1302 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1303 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1304 if ( remove ) slapd_remove( slap_listeners[l]->sl_sd, 0, 0 );
1306 #ifdef LDAP_PF_LOCAL
1307 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1308 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1310 #endif /* LDAP_PF_LOCAL */
1312 slapd_close( slap_listeners[l]->sl_sd );
1315 if ( slap_listeners[l]->sl_url.bv_val ) {
1316 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1319 if ( slap_listeners[l]->sl_name.bv_val ) {
1320 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1323 free ( slap_listeners[l] );
1324 slap_listeners[l] = NULL;
1329 slapd_handle_listener(
1335 socklen_t len = sizeof(from);
1338 struct berval authid = BER_BVNULL;
1339 #ifdef SLAPD_RLOOKUPS
1340 char hbuf[NI_MAXHOST];
1343 char *dnsname = NULL;
1344 char *peeraddr = NULL;
1345 #ifdef LDAP_PF_LOCAL
1346 char peername[MAXPATHLEN + sizeof("PATH=")];
1347 #elif defined(LDAP_PF_INET6)
1348 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1350 char peername[sizeof("IP=255.255.255.255:65336")];
1351 #endif /* LDAP_PF_LOCAL */
1355 #ifdef LDAP_CONNECTIONLESS
1356 if ( sl->sl_is_udp ) {
1357 /* The first time we receive a query, we set this
1358 * up as a "connection". It remains open for the life
1361 if ( sl->sl_is_udp < 2 ) {
1362 id = connection_init( sl->sl_sd, sl, "", "",
1363 CONN_IS_UDP, ssf, NULL );
1370 # ifdef LDAP_PF_LOCAL
1371 /* FIXME: apparently accept doesn't fill
1372 * the sun_path sun_path member */
1373 from.sa_un_addr.sun_path[0] = '\0';
1374 # endif /* LDAP_PF_LOCAL */
1376 s = accept( sl->sl_sd, (struct sockaddr *) &from, &len );
1377 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1379 * As soon as a TCP connection is accepted, the listener FD is resumed
1380 * for concurrent-processing of incoming TCP connections.
1382 slapd_resume( sl->sl_sd );
1384 if ( s == AC_SOCKET_INVALID ) {
1385 int err = sock_errno();
1396 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1398 /* Stop listening until an existing session closes */
1400 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1403 Debug( LDAP_DEBUG_ANY,
1404 "daemon: accept(%ld) failed errno=%d (%s)\n",
1405 (long) sl->sl_sd, err,
1407 ldap_pvt_thread_yield();
1411 #ifndef HAVE_WINSOCK
1412 /* make sure descriptor number isn't too great */
1413 if ( s >= dtblsize ) {
1414 Debug( LDAP_DEBUG_ANY,
1415 "daemon: %ld beyond descriptor table size %ld\n",
1416 (long) s, (long) dtblsize, 0 );
1419 ldap_pvt_thread_yield();
1425 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1427 /* newly accepted stream should not be in any of the FD SETS */
1428 assert( SLAP_SOCK_NOT_ACTIVE( s ));
1430 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1433 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1434 #ifdef LDAP_PF_LOCAL
1435 /* for IPv4 and IPv6 sockets only */
1436 if ( from.sa_addr.sa_family != AF_LOCAL )
1437 #endif /* LDAP_PF_LOCAL */
1442 /* enable keep alives */
1444 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1445 (char *) &tmp, sizeof(tmp) );
1446 if ( rc == AC_SOCKET_ERROR ) {
1447 int err = sock_errno();
1448 Debug( LDAP_DEBUG_ANY,
1449 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1450 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1454 /* enable no delay */
1456 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1457 (char *)&tmp, sizeof(tmp) );
1458 if ( rc == AC_SOCKET_ERROR ) {
1459 int err = sock_errno();
1460 Debug( LDAP_DEBUG_ANY,
1461 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1462 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1468 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1470 switch ( from.sa_addr.sa_family ) {
1471 # ifdef LDAP_PF_LOCAL
1473 /* FIXME: apparently accept doesn't fill
1474 * the sun_path sun_path member */
1475 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1476 AC_MEMCPY( from.sa_un_addr.sun_path,
1477 sl->sl_sa.sa_un_addr.sun_path,
1478 sizeof( from.sa_un_addr.sun_path ) );
1481 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1487 if( getpeereid( s, &uid, &gid ) == 0 ) {
1488 authid.bv_val = ch_malloc(
1489 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1490 "cn=peercred,cn=external,cn=auth" ) + 1 );
1491 authid.bv_len = sprintf( authid.bv_val,
1492 "gidNumber=%d+uidNumber=%d,"
1493 "cn=peercred,cn=external,cn=auth",
1494 (int) gid, (int) uid );
1495 assert( authid.bv_len <=
1496 STRLENOF( "gidNumber=4294967295+uidNumber=4294967295,"
1497 "cn=peercred,cn=external,cn=auth" ) );
1502 #endif /* LDAP_PF_LOCAL */
1504 # ifdef LDAP_PF_INET6
1506 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1507 peeraddr = inet_ntoa( *((struct in_addr *)
1508 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1509 sprintf( peername, "IP=%s:%d",
1510 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1511 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1513 char addr[INET6_ADDRSTRLEN];
1515 peeraddr = (char *) inet_ntop( AF_INET6,
1516 &from.sa_in6_addr.sin6_addr,
1517 addr, sizeof addr );
1518 sprintf( peername, "IP=%s %d",
1519 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1520 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1523 # endif /* LDAP_PF_INET6 */
1526 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1527 sprintf( peername, "IP=%s:%d",
1528 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1529 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1537 if ( ( from.sa_addr.sa_family == AF_INET )
1538 #ifdef LDAP_PF_INET6
1539 || ( from.sa_addr.sa_family == AF_INET6 )
1544 #ifdef SLAPD_RLOOKUPS
1545 if ( use_reverse_lookup ) {
1547 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1548 sizeof(hbuf), &herr ) == 0) {
1549 ldap_pvt_str2lower( hbuf );
1553 #endif /* SLAPD_RLOOKUPS */
1556 if ( !hosts_ctl("slapd",
1557 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1558 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1559 SLAP_STRING_UNKNOWN ))
1562 Statslog( LDAP_DEBUG_STATS,
1563 "fd=%ld DENIED from %s (%s)\n",
1565 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1566 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1571 #endif /* HAVE_TCPD */
1574 id = connection_init(s, sl,
1575 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1578 sl->sl_is_tls ? CONN_IS_TLS : 0,
1583 authid.bv_val ? &authid : NULL );
1585 if( authid.bv_val ) ch_free(authid.bv_val);
1588 Debug( LDAP_DEBUG_ANY,
1589 "daemon: connection_init(%ld, %s, %s) failed.\n",
1590 (long) s, peername, sl->sl_name.bv_val );
1595 Statslog( LDAP_DEBUG_STATS,
1596 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1597 id, (long) s, peername, sl->sl_name.bv_val,
1600 slapd_add( s, 1, NULL );
1604 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1606 slapd_handle_listener_thread(
1612 rc = slapd_handle_listener( (Listener*)ptr );
1614 if( rc != LDAP_SUCCESS ) {
1615 Debug( LDAP_DEBUG_ANY,
1616 "slapd_handle_listener_thread: failed", 0, 0, 0 );
1623 new_connection_activate(
1628 if( !slapd_suspend( sl->sl_sd ) ) return (0);
1630 status = ldap_pvt_thread_pool_submit( &connection_pool,
1631 slapd_handle_listener_thread, (void *) sl );
1634 Debug( LDAP_DEBUG_ANY,
1635 "new_connection_activate: ldap_pvt_thread_pool_submit failed\n",
1649 time_t last_idle_check = 0;
1650 struct timeval idle;
1653 #define SLAPD_IDLE_CHECK_LIMIT 4
1655 if ( global_idletimeout > 0 ) {
1656 last_idle_check = slap_get_time();
1657 /* Set the select timeout.
1658 * Don't just truncate, preserve the fractions of
1659 * seconds to prevent sleeping for zero time.
1661 idle.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
1662 idle.tv_usec = global_idletimeout - \
1663 ( idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
1664 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1670 slapd_add( wake_sds[0], 0, NULL );
1672 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1673 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
1675 #ifdef LDAP_CONNECTIONLESS
1676 /* Since this is connectionless, the data port is the
1677 * listening port. The listen() and accept() calls
1680 if ( slap_listeners[l]->sl_is_udp ) {
1681 slapd_add( slap_listeners[l]->sl_sd, 1, slap_listeners[l] );
1686 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
1687 int err = sock_errno();
1689 #ifdef LDAP_PF_INET6
1690 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1691 * we are already listening to in6addr_any, then we want to ignore
1692 * this and continue.
1694 if ( err == EADDRINUSE ) {
1696 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1697 struct sockaddr_in6 sa6;
1699 if ( sa.sin_family == AF_INET &&
1700 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1701 for ( i = 0 ; i < l; i++ ) {
1702 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1703 if ( sa6.sin6_family == AF_INET6 &&
1704 !memcmp( &sa6.sin6_addr, &in6addr_any,
1705 sizeof(struct in6_addr) ) )
1712 /* We are already listening to in6addr_any */
1713 Debug( LDAP_DEBUG_CONNS,
1714 "daemon: Attempt to listen to 0.0.0.0 failed, "
1715 "already listening on ::, assuming IPv4 included\n",
1717 slapd_close( slap_listeners[l]->sl_sd );
1718 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1724 Debug( LDAP_DEBUG_ANY,
1725 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1726 slap_listeners[l]->sl_url.bv_val, err,
1731 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1732 /* make the listening socket non-blocking */
1733 if ( ber_pvt_socket_set_nonblock( slap_listeners[l]->sl_sd, 1 ) < 0 ) {
1734 Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
1735 "set nonblocking on a listening socket failed\n",
1742 slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
1745 #ifdef HAVE_NT_SERVICE_MANAGER
1746 if ( started_event != NULL ) }
1747 ldap_pvt_thread_cond_signal( &started_event );
1751 #ifdef SLAP_SEM_LOAD_CONTROL
1753 * initialize count and lazyness of a semaphore
1755 (void) ldap_lazy_sem_init(
1756 SLAP_MAX_WORKER_THREADS + 4 /* max workers + margin */,
1760 /* initialization complete. Here comes the loop. */
1762 while ( !slapd_shutdown ) {
1767 #if SLAP_EVENTS_ARE_INDEXED
1768 ber_socket_t nrfds, nwfds;
1770 #define SLAPD_EBADF_LIMIT 16
1777 struct timeval *tvp;
1779 struct timeval *cat;
1782 now = slap_get_time();
1784 if( ( global_idletimeout > 0 ) &&
1785 difftime( last_idle_check +
1786 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1787 connections_timeout_idle( now );
1788 last_idle_check = now;
1793 if( slapd_gentle_shutdown ) {
1794 ber_socket_t active;
1796 if( slapd_gentle_shutdown == 1 ) {
1797 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1798 close_listeners( 1 );
1799 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1800 slapd_gentle_shutdown = 2;
1803 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1804 active = slap_daemon.sd_nactives;
1805 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1815 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1817 nwriters = slap_daemon.sd_nwriters;
1820 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1821 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1823 if ( slap_listeners[l]->sl_is_mute )
1824 SLAP_SOCK_SET_MUTE( slap_listeners[l]->sl_sd );
1826 if ( SLAP_SOCK_IS_MUTE( slap_listeners[l]->sl_sd ))
1827 SLAP_SOCK_CLR_MUTE( slap_listeners[l]->sl_sd );
1830 nfds = SLAP_EVENT_MAX;
1832 if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
1834 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1837 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
1838 && ( tv.tv_sec || tv.tv_usec )
1847 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1848 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1849 while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
1850 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1851 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1853 ldap_pvt_runqueue_runtask( &slapd_rq, rtask );
1854 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1855 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1856 ldap_pvt_thread_pool_submit( &connection_pool,
1857 rtask->routine, (void *) rtask );
1858 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1860 rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
1862 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1864 if ( cat && cat->tv_sec ) {
1865 time_t diff = difftime( cat->tv_sec, now );
1866 if ( diff == 0 ) diff = tdelta;
1867 if ( tvp == NULL || diff < tv.tv_sec ) {
1874 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1875 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
1876 slap_listeners[l]->sl_is_mute )
1881 Debug( LDAP_DEBUG_CONNS,
1882 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1883 slap_listeners[l]->sl_sd, at,
1884 tvp == NULL ? "NULL" : "zero" );
1887 switch(ns = SLAP_EVENT_WAIT(tvp)) {
1888 case -1: { /* failure - try again */
1889 int err = sock_errno();
1893 /* you'd think this would be EBADF */
1894 || err == WSAENOTSOCK
1897 if (++ebadf < SLAPD_EBADF_LIMIT)
1901 if( err != EINTR ) {
1902 Debug( LDAP_DEBUG_CONNS,
1903 "daemon: select failed (%d): %s\n",
1904 err, sock_errstr(err), 0 );
1910 case 0: /* timeout - let threads run */
1912 #ifndef HAVE_YIELDING_SELECT
1913 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1916 ldap_pvt_thread_yield();
1920 default: /* something happened - deal with it */
1921 if( slapd_shutdown ) continue;
1924 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1929 #if SLAP_EVENTS_ARE_INDEXED
1930 if ( SLAP_EVENT_IS_READ( wake_sds[0] )) {
1932 tcp_read( wake_sds[0], c, sizeof(c) );
1935 SLAP_EVENT_CLR_READ( wake_sds[0] );
1939 /* The event slot equals the descriptor number - this is
1940 * true for Unix select and poll. We treat Windows select
1941 * like this too, even though it's a kludge.
1943 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1946 if ( ns <= 0 ) break;
1947 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
1948 if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd )) continue;
1951 #ifdef SLAP_LIGHTWEIGHT_LISTENER
1952 rc = new_connection_activate(slap_listeners[l]);
1954 rc = slapd_handle_listener(slap_listeners[l]);
1957 #ifdef LDAP_CONNECTIONLESS
1958 /* This is a UDP session, let the data loop process it */
1964 /* Don't need to look at this in the data loops */
1965 SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
1966 SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
1969 /* bypass the following tests if no descriptors left */
1971 #ifndef HAVE_YIELDING_SELECT
1972 ldap_pvt_thread_yield();
1977 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1979 nrfds = readfds.fd_count;
1980 nwfds = writefds.fd_count;
1981 for ( i = 0; i < readfds.fd_count; i++ ) {
1982 Debug( LDAP_DEBUG_CONNS, " %d%s",
1983 readfds.fd_array[i], "r", 0 );
1985 for ( i = 0; i < writefds.fd_count; i++ ) {
1986 Debug( LDAP_DEBUG_CONNS, " %d%s",
1987 writefds.fd_array[i], "w", 0 );
1993 for ( i = 0; i < nfds; i++ ) {
1996 r = SLAP_EVENT_IS_READ( i );
1997 /* writefds was not initialized if nwriters was zero */
1998 w = nwriters ? SLAP_EVENT_IS_WRITE( i ) : 0;
2000 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
2001 r ? "r" : "", w ? "w" : "" );
2011 if ( ns <= 0 ) break;
2014 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
2017 /* loop through the writers */
2018 for ( i = 0; nwfds > 0; i++ )
2022 wd = writefds.fd_array[i];
2024 if( ! SLAP_EVENT_IS_WRITE( i ) ) {
2031 Debug( LDAP_DEBUG_CONNS,
2032 "daemon: write active on %d\n",
2035 * NOTE: it is possible that the connection was closed
2036 * and that the stream is now inactive.
2037 * connection_write() must valid the stream is still
2041 if ( connection_write( wd ) < 0 ) {
2042 if ( SLAP_EVENT_IS_READ( wd )) {
2043 SLAP_EVENT_CLR_READ( (unsigned) wd );
2048 SLAP_EVENT_CLR_WRITE( wd );
2051 for ( i = 0; nrfds > 0; i++ )
2055 rd = readfds.fd_array[i];
2057 if( ! SLAP_EVENT_IS_READ( i ) ) {
2064 Debug ( LDAP_DEBUG_CONNS,
2065 "daemon: read activity on %d\n", rd, 0, 0 );
2067 * NOTE: it is possible that the connection was closed
2068 * and that the stream is now inactive.
2069 * connection_read() must valid the stream is still
2073 #ifdef SLAP_LIGHTWEIGHT_LISTENER
2074 connection_processing_activate( rd );
2076 if ( connection_read( rd ) < 0 ) {
2081 #else /* !SLAP_EVENTS_ARE_INDEXED */
2083 /* The events are returned in an arbitrary list. This is true
2084 * for /dev/poll, epoll and kqueue. In order to prioritize things
2085 * so that we can handle wake_sds first, listeners second, and then
2086 * all other connections last (as we do for select), we would need
2087 * to use multiple event handles and cascade them.
2089 * That seems like a bit of hassle. So the wake_sds check has been
2090 * skipped. For epoll and kqueue we can associate arbitrary data with
2091 * an event, so we could use pointers to the listener structure
2092 * instead of just the file descriptor. For /dev/poll we have to
2093 * search the listeners array for a matching descriptor.
2095 /* if waking is set and we woke up, we'll read whatever
2100 tcp_read( wake_sds[0], c, sizeof(c) );
2107 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
2109 for (i=0; i<ns; i++) {
2112 if ( SLAP_EVENT_IS_LISTENER(i)
2113 #ifdef LDAP_CONNECTIONLESS
2114 && !((SLAP_EVENT_LISTENER(i))->sl_is_udp)
2121 /* Don't log internal wake events */
2122 if ( SLAP_EVENT_FD( i ) == wake_sds[0] ) continue;
2124 r = SLAP_EVENT_IS_READ( i );
2125 w = SLAP_EVENT_IS_WRITE( i );
2127 Debug( LDAP_DEBUG_CONNS, " %d%s%s", SLAP_EVENT_FD(i),
2128 r ? "r" : "", w ? "w" : "" );
2133 for (i=0; i<ns; i++) {
2136 if ( SLAP_EVENT_IS_LISTENER(i) ) {
2137 #ifdef SLAP_LIGHTWEIGHT_LISTENER
2138 rc = new_connection_activate( SLAP_EVENT_LISTENER( i ));
2140 rc = slapd_handle_listener( SLAP_EVENT_LISTENER( i ));
2143 /* If we found a regular listener, rc is now zero, and we
2144 * can skip the data portion. But if it was a UDP listener
2145 * then rc is still 1, and we want to handle the data.
2148 fd = SLAP_EVENT_FD( i );
2150 /* Ignore wake events, they were handled above */
2151 if ( fd == wake_sds[0] ) continue;
2153 if( SLAP_EVENT_IS_WRITE( i ) ) {
2154 Debug( LDAP_DEBUG_CONNS,
2155 "daemon: write active on %d\n",
2158 * NOTE: it is possible that the connection was closed
2159 * and that the stream is now inactive.
2160 * connection_write() must valid the stream is still
2164 if ( connection_write( fd ) < 0 ) {
2169 if( SLAP_EVENT_IS_READ( i ) ) {
2170 Debug( LDAP_DEBUG_CONNS,
2171 "daemon: read active on %d\n",
2174 * NOTE: it is possible that the connection was closed
2175 * and that the stream is now inactive.
2176 * connection_read() must valid the stream is still
2180 #ifdef SLAP_LIGHTWEIGHT_LISTENER
2181 if ( fd != wake_sds[0] ) {
2182 connection_processing_activate( fd );
2185 if ( connection_read( fd ) < 0 ) slapd_close( fd );
2190 #endif /* SLAP_EVENTS_ARE_INDEXED */
2192 #ifndef HAVE_YIELDING_SELECT
2193 ldap_pvt_thread_yield();
2197 if( slapd_shutdown == 1 ) {
2198 Debug( LDAP_DEBUG_ANY,
2199 "daemon: shutdown requested and initiated.\n",
2202 } else if ( slapd_shutdown == 2 ) {
2203 #ifdef HAVE_NT_SERVICE_MANAGER
2204 Debug( LDAP_DEBUG_ANY,
2205 "daemon: shutdown initiated by Service Manager.\n",
2207 #else /* !HAVE_NT_SERVICE_MANAGER */
2208 Debug( LDAP_DEBUG_ANY,
2209 "daemon: abnormal condition, shutdown initiated.\n",
2211 #endif /* !HAVE_NT_SERVICE_MANAGER */
2213 Debug( LDAP_DEBUG_ANY,
2214 "daemon: no active streams, shutdown initiated.\n",
2218 if( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
2220 if( !slapd_gentle_shutdown ) {
2221 slapd_abrupt_shutdown = 1;
2222 connections_shutdown();
2225 Debug( LDAP_DEBUG_ANY,
2226 "slapd shutdown: waiting for %d threads to terminate\n",
2227 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
2228 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
2230 free ( slap_listeners );
2231 slap_listeners = NULL;
2237 int slapd_daemon( void )
2243 #define SLAPD_LISTENER_THREAD 1
2244 #if defined( SLAPD_LISTENER_THREAD )
2246 ldap_pvt_thread_t listener_tid;
2248 /* listener as a separate THREAD */
2249 rc = ldap_pvt_thread_create( &listener_tid,
2250 0, slapd_daemon_task, NULL );
2253 Debug( LDAP_DEBUG_ANY,
2254 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2258 /* wait for the listener thread to complete */
2259 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2262 /* experimental code */
2263 slapd_daemon_task( NULL );
2270 static int sockinit(void)
2272 #if defined( HAVE_WINSOCK2 )
2273 WORD wVersionRequested;
2277 wVersionRequested = MAKEWORD( 2, 0 );
2279 err = WSAStartup( wVersionRequested, &wsaData );
2281 /* Tell the user that we couldn't find a usable */
2286 /* Confirm that the WinSock DLL supports 2.0.*/
2287 /* Note that if the DLL supports versions greater */
2288 /* than 2.0 in addition to 2.0, it will still return */
2289 /* 2.0 in wVersion since that is the version we */
2292 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2293 HIBYTE( wsaData.wVersion ) != 0 )
2295 /* Tell the user that we couldn't find a usable */
2301 /* The WinSock DLL is acceptable. Proceed. */
2302 #elif defined( HAVE_WINSOCK )
2304 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
2310 static int sockdestroy(void)
2312 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2319 slap_sig_shutdown( int sig )
2322 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2326 * If the NT Service Manager is controlling the server, we don't
2327 * want SIGBREAK to kill the server. For some strange reason,
2328 * SIGBREAK is generated when a user logs out.
2331 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2332 if (is_NT_Service && sig == SIGBREAK) {
2337 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
2338 slapd_gentle_shutdown = 1;
2347 /* reinstall self */
2348 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2352 slap_sig_wake( int sig )
2356 /* reinstall self */
2357 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2361 void slapd_add_internal(ber_socket_t s, int isactive) {
2362 slapd_add(s, isactive, NULL);
2365 Listener ** slapd_get_listeners(void) {
2366 return slap_listeners;
2369 void slap_wake_listener() {