]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/daemon.c
Remove lint
[openldap] / servers / slapd / daemon.c
index a3c45a9fe598b6d5191feb027d1cca75f2276158..57c3a89d502b087fef5d56c095179a14830172db 100644 (file)
@@ -107,42 +107,55 @@ static struct slap_daemon {
  */
 #include <slp.h>
 
-#define MAX_HOSTNAME_LEN 256
 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
-static char** slapd_srvurls = 0;
+#define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
+static char** slapd_srvurls = NULL;
 static SLPHandle slapd_hslp = 0;
 
 void slapd_slp_init( const char* urls ) {
        int i;
-       struct hostent* he;
-       char hn[MAX_HOSTNAME_LEN];
 
        slapd_srvurls = str2charray( urls, " " );
 
-       for( i=0; slapd_srvurls[i]!=NULL; i++ ) {
+       if( slapd_srvurls == NULL ) return;
+
+       /* find and expand INADDR_ANY URLs */
+       for( i=0; slapd_srvurls[i] != NULL; i++ ) {
                if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
-                       /* INADDR_ANY urls should be marked up with host.domainname */
-                       if ( gethostname( hn, MAX_HOSTNAME_LEN ) == 0) {
-                               he = gethostbyname( hn );
-                               if( he ) {
-                                       slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
-                                               strlen( he->h_name ) +
-                                               strlen( LDAP_SRVTYPE_PREFIX ) + 1);
-                                       strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
-                                       strcat( slapd_srvurls[i], he->h_name );
-                               }
+                       char *host = ldap_pvt_get_fqdn( NULL );
+                       if ( host != NULL ) {
+                               slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
+                                       strlen( host ) +
+                                       sizeof( LDAP_SRVTYPE_PREFIX ) );
+                               strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
+                               strcat( slapd_srvurls[i], host );
+
+                               ch_free( host );
+                       }
+
+               } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
+                       char *host = ldap_pvt_get_fqdn( NULL );
+                       if ( host != NULL ) {
+                               slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
+                                       strlen( host ) +
+                                       sizeof( LDAPS_SRVTYPE_PREFIX ) );
+                               strcpy( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX );
+                               strcat( slapd_srvurls[i], host );
+
+                               ch_free( host );
                        }
                }
        }
 
        /* open the SLP handle */
-       SLPOpen("en", 0, &slapd_hslp);
+       SLPOpen( "en", 0, &slapd_hslp );
 }
 
 void slapd_slp_deinit() {
-       if ( slapd_srvurls ) {
-               charray_free( slapd_srvurls );
-       }
+       if( slapd_srvurls == NULL ) return;
+
+       charray_free( slapd_srvurls );
+       slapd_srvurls = NULL;
 
        /* close the SLP handle */
        SLPClose( slapd_hslp );
@@ -160,14 +173,20 @@ void slapd_slp_reg() {
        int i;
 
        for( i=0; slapd_srvurls[i] != NULL; i++ ) {
-               SLPReg( slapd_hslp,
-                       slapd_srvurls[i],
-                       SLP_LIFETIME_MAXIMUM,
-                       "ldap",
-                       "",
-                       1,
-                       slapd_slp_regreport,
-                       NULL );
+               if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
+                               sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
+                   strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
+                               sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
+               {
+                       SLPReg( slapd_hslp,
+                               slapd_srvurls[i],
+                               SLP_LIFETIME_MAXIMUM,
+                               "ldap",
+                               "",
+                               1,
+                               slapd_slp_regreport,
+                               NULL );
+               }
        }
 }
 
@@ -318,35 +337,51 @@ static int slap_get_listener_addresses(
        struct sockaddr ***sal)
 {
        struct sockaddr **sap;
-#ifdef HAVE_GETADDRINFO
-       struct addrinfo hints, *res, *sai;
-       int n, err;
 
-       memset( &hints, '\0', sizeof(hints) );
-       hints.ai_flags = AI_PASSIVE;
-       hints.ai_socktype = SOCK_STREAM;
-#  ifdef LDAP_PF_LOCAL
+#ifdef LDAP_PF_LOCAL
        if ( port == 0 ) {
-               hints.ai_family = AF_LOCAL;
-               /* host specifies a service in this case */
-               if (err = getaddrinfo(NULL, host, &hints, &res)) {
+               *sal = ch_malloc(2 * sizeof(void *));
+               if (*sal == NULL) {
+                       return -1;
+               }
+
+               sap = *sal;
+               *sap = ch_malloc(sizeof(struct sockaddr_un));
+               if (*sap == NULL)
+                       goto errexit;
+               sap[1] = NULL;
+
+               if ( strlen(host) >
+                    (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
 #ifdef NEW_LOGGING
                        LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
-                                  "slap_get_listener_addresses: getaddrinfo failed: %s\n",
-                                  AC_GAI_STRERROR(err) ));
+                                  "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
+                                  host ));
 #else
-                       Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
-                               AC_GAI_STRERROR(err), 0, 0);
+                       Debug( LDAP_DEBUG_ANY,
+                              "daemon: domain socket path (%s) too long in URL",
+                              host, 0, 0);
 #endif
-                       return -1;
+                       goto errexit;
                }
+
+               (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
+               (*sap)->sa_family = AF_LOCAL;
+               strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
        } else
-#  endif
+#endif
        {
+#ifdef HAVE_GETADDRINFO
+               struct addrinfo hints, *res, *sai;
+               int n, err;
                char serv[7];
 
-               snprintf(serv, sizeof serv, "%d", port);
+               memset( &hints, '\0', sizeof(hints) );
+               hints.ai_flags = AI_PASSIVE;
+               hints.ai_socktype = SOCK_STREAM;
                hints.ai_family = AF_UNSPEC;
+               snprintf(serv, sizeof serv, "%d", port);
+
                if (err = getaddrinfo(host, serv, &hints, &res)) {
 #ifdef NEW_LOGGING
                        LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
@@ -358,94 +393,54 @@ static int slap_get_listener_addresses(
 #endif
                        return -1;
                }
-       }
 
-       sai = res;
-       for (n=2; (sai = sai->ai_next) != NULL; n++) {
-               /* EMPTY */ ;
-       }
-       *sal = ch_malloc(n * sizeof(*sal));
-       if (*sal == NULL) {
-               return -1;
-       }
-
-       sai = res;
-       sap = *sal;
-       do {
-               switch (sai->ai_family) {
-#  ifdef LDAP_PF_LOCAL
-               case AF_LOCAL: {
-                       *sap = ch_malloc(sizeof(struct sockaddr_un));
-                       if (*sap == NULL) {
-                               freeaddrinfo(res);
-                               goto errexit;
-                       }
-                       *(struct sockaddr_un *)*sap =
-                               *((struct sockaddr_un *)sai->ai_addr);
-               } break;
-#  endif
-#  ifdef LDAP_PF_INET6
-               case AF_INET6: {
-                       *sap = ch_malloc(sizeof(struct sockaddr_in6));
-                       if (*sap == NULL) {
-                               freeaddrinfo(res);
-                               goto errexit;
-                       }
-                       *(struct sockaddr_in6 *)*sap =
-                               *((struct sockaddr_in6 *)sai->ai_addr);
-               } break;
-#  endif
-               case AF_INET: {
-                       *sap = ch_malloc(sizeof(struct sockaddr_in));
-                       if (*sap == NULL) {
-                               freeaddrinfo(res);
-                               goto errexit;
-                       }
-                       *(struct sockaddr_in *)*sap =
-                               *((struct sockaddr_in *)sai->ai_addr);
-               } break;
-               default:
-                       *sap = NULL;
-                       break;
+               sai = res;
+               for (n=2; (sai = sai->ai_next) != NULL; n++) {
+                       /* EMPTY */ ;
                }
-               if (*sap != NULL) {
-                       (*sap)->sa_family = sai->ai_family;
-                       sap++;
-               }
-       } while ((sai = sai->ai_next) != NULL);
-
-       freeaddrinfo(res);
-
-#else
-#  ifdef LDAP_PF_LOCAL
-       if ( port == 0 ) {
-               *sal = ch_malloc(2 * sizeof(*sal));
+               *sal = ch_malloc(n * sizeof(void *));
                if (*sal == NULL) {
                        return -1;
                }
+
+               sai = res;
                sap = *sal;
-               *sap = ch_malloc(sizeof(struct sockaddr_un));
-               if (*sap == NULL)
-                       goto errexit;
-               (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
-               (*sap)->sa_family = AF_LOCAL;
-               if ( strlen(host) >
-                    (sizeof(((struct sockaddr_un *)*sal)->sun_path) - 1) ) {
-#ifdef NEW_LOGGING
-                       LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
-                                  "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
-                                  host ));
-#else
-                       Debug( LDAP_DEBUG_ANY,
-                              "daemon: domain socket path (%s) too long in URL",
-                              host, 0, 0);
-#endif
-                       goto errexit;
-               }
-               strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
-       } else
+
+               do {
+                       switch (sai->ai_family) {
+#  ifdef LDAP_PF_INET6
+                       case AF_INET6:
+                               *sap = ch_malloc(sizeof(struct sockaddr_in6));
+                               if (*sap == NULL) {
+                                       freeaddrinfo(res);
+                                       goto errexit;
+                               }
+                               *(struct sockaddr_in6 *)*sap =
+                                       *((struct sockaddr_in6 *)sai->ai_addr);
+                               break;
 #  endif
-       {
+                       case AF_INET:
+                               *sap = ch_malloc(sizeof(struct sockaddr_in));
+                               if (*sap == NULL) {
+                                       freeaddrinfo(res);
+                                       goto errexit;
+                               }
+                               *(struct sockaddr_in *)*sap =
+                                       *((struct sockaddr_in *)sai->ai_addr);
+                               break;
+                       default:
+                               *sap = NULL;
+                               break;
+                       }
+                       if (*sap != NULL) {
+                               (*sap)->sa_family = sai->ai_family;
+                               sap++;
+                       }
+               } while ((sai = sai->ai_next) != NULL);
+
+               *sap = NULL;
+               freeaddrinfo(res);
+#else
                struct in_addr in;
 
                if ( host == NULL ) {
@@ -467,7 +462,7 @@ static int slap_get_listener_addresses(
                        AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
                }
 
-               *sal = ch_malloc(2 * sizeof(*sal));
+               *sal = ch_malloc(2 * sizeof(void *));
                if (*sal == NULL) {
                        return -1;
                }
@@ -477,16 +472,15 @@ static int slap_get_listener_addresses(
                if (*sap == NULL) {
                        goto errexit;
                }
+               sap[1] = NULL;
 
                (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
                (*sap)->sa_family = AF_INET;
                ((struct sockaddr_in *)*sap)->sin_port = htons(port);
                ((struct sockaddr_in *)*sap)->sin_addr = in;
-       }
-       sap++;
 #endif
+       }
 
-       *sap = NULL;
        return 0;
 
 errexit:
@@ -724,13 +718,15 @@ static Listener * slap_open_listener(
 
        case AF_INET: {
                char *s;
-#ifdef HAVE_GETADDRINFO
+#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
                char addr[INET_ADDRSTRLEN];
                inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
                           addr, sizeof(addr) );
                s = addr;
+               port = ((struct sockaddr_in *)*sal) ->sin_port;
 #else
                s = inet_ntoa( l.sl_addr.sin_addr );
+               port = l.sl_addr.sin_port;
 #endif
                l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
                sprintf( l.sl_name, "IP=%s:%d",
@@ -742,6 +738,7 @@ static Listener * slap_open_listener(
                char addr[INET6_ADDRSTRLEN];
                inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
                           addr, sizeof addr);
+               port = ((struct sockaddr_in6 *)*sal)->sin6_port;
                l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
                sprintf( l.sl_name, "IP=%s %d", addr, port );
        } break;
@@ -1098,7 +1095,7 @@ slapd_daemon_task(
                        Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
                            0, 0, 0 );
 #endif
-               ldap_pvt_thread_yield();
+                       ldap_pvt_thread_yield();
                        continue;
 
                default:        /* something happened - deal with it */
@@ -1163,13 +1160,14 @@ slapd_daemon_task(
                                    (long) slap_listeners[l]->sl_sd, err,
                                    sock_errstr(err) );
 #endif
+                               ldap_pvt_thread_yield();
                                continue;
                        }
 
 #ifndef HAVE_WINSOCK
                        /* make sure descriptor number isn't too great */
                        if ( s >= dtblsize ) {
-#ifdef NEW_LGGING
+#ifdef NEW_LOGGING
                                LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
                                           "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
                                           (long)s, (long)dtblsize ));
@@ -1179,6 +1177,7 @@ slapd_daemon_task(
                                        (long) s, (long) dtblsize, 0 );
 #endif
                                slapd_close(s);
+                               ldap_pvt_thread_yield();
                                continue;
                        }
 #endif
@@ -1382,7 +1381,7 @@ slapd_daemon_task(
                for ( i = 0; i < readfds.fd_count; i++ ) {
 #ifdef NEW_LOGGING
                        LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
-                                  " %d%s", readfs.fd_array[i], "r", 0 );
+                                  " %d%s", readfds.fd_array[i], "r", 0 ));
 #else
                        Debug( LDAP_DEBUG_CONNS, " %d%s",
                                readfds.fd_array[i], "r", 0 );
@@ -1629,9 +1628,9 @@ int slapd_daemon( void )
 #endif
                        return rc;
                }
-
-               /* wait for the listener thread to complete */
-               ldap_pvt_thread_join( listener_tid, (void *) NULL );
+               /* wait for the listener thread to complete */
+               ldap_pvt_thread_join( listener_tid, (void *) NULL );
        }
 #else
        /* experimental code */