]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/dnssrv.c
prepare for ldap_bv2dn()
[openldap] / libraries / libldap / dnssrv.c
index d5597c57ecf7d7094be50c67cda75948f1cf59b4..55184676df3d468756c33c5c3d788abb1d78a986 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
@@ -37,8 +37,107 @@ int ldap_dn2domain(
        LDAP_CONST char *dn_in,
        char **domainp)
 {
-       /* not yet implemented */
-       return LDAP_NOT_SUPPORTED;
+       int i;
+       char *domain = NULL;
+       char **dn;
+
+       if( dn_in == NULL || domainp == NULL ) {
+               return -1;
+       }
+
+       dn = ldap_explode_dn( dn_in, 0 );
+
+       if( dn == NULL ) {
+               return -2;
+       }
+
+       for( i=0; dn[i] != NULL; i++ ) {
+               char ** rdn = ldap_explode_rdn( dn[i], 0 );
+
+               if( rdn == NULL || *rdn == NULL ) {
+                       LDAP_FREE( rdn );
+                       LDAP_FREE( domain );
+                       LDAP_VFREE( dn );
+                       return -3;
+               }
+
+
+               if( rdn[1] == NULL ) {
+                       /*
+                        * single-valued RDN
+                        */
+                       char *dc;
+
+#define LDAP_DC "dc="
+#define LDAP_DCOID "0.9.2342.19200300.100.1.25="
+
+                       if( strncasecmp( rdn[0],
+                               LDAP_DC, sizeof(LDAP_DC)-1 ) == 0 )
+                       {
+                               dc = &rdn[0][sizeof(LDAP_DC)-1];
+
+                       } else if( strncmp( rdn[0],
+                               LDAP_DCOID, sizeof(LDAP_DCOID)-1 ) == 0 )
+                       {
+                               dc = &rdn[0][sizeof(LDAP_DCOID)-1];
+
+                       } else {
+                               dc = NULL;
+                       }
+
+                       if( dc != NULL ) {
+                               char *ndomain;
+
+                               if( *dc == '\0' ) {
+                                       /* dc value is empty! */
+                                       LDAP_FREE( rdn );
+                                       LDAP_FREE( domain );
+                                       LDAP_VFREE( dn );
+                                       LDAP_VFREE( rdn );
+                                       return -4;
+                               }
+
+                               ndomain = LDAP_REALLOC( domain,
+                                       ( domain == NULL ? 0 : strlen(domain) )
+                                       + strlen(dc) + sizeof(".") );
+
+                               if( ndomain == NULL ) {
+                                       LDAP_FREE( rdn );
+                                       LDAP_FREE( domain );
+                                       LDAP_VFREE( dn );
+                                       LDAP_VFREE( rdn );
+                                       return -5;
+                               }
+
+                               if( domain == NULL ) {
+                                       ndomain[0] = '\0';
+                               } else {
+                                       strcat( ndomain, "." );
+                               }
+
+                               strcat( ndomain, dc );
+
+                               domain = ndomain;
+                               continue;
+                       }
+               }
+
+               /*
+                * multi-valued RDN or fall thru
+                */
+
+               LDAP_VFREE( rdn );
+               LDAP_FREE( domain );
+               domain = NULL;
+       } 
+
+       if( domain != NULL &&  *domain == '\0' ) {
+               LDAP_FREE( domain );
+               domain = NULL;
+       }
+
+       *domainp = domain;
+       return 0;
 }
 
 int ldap_domain2dn(
@@ -95,9 +194,8 @@ int ldap_domain2hostlist(
        LDAP_CONST char *domain,
        char **list )
 {
-#ifdef HAVE_RES_SEARCH
+#ifdef HAVE_RES_QUERY
     char *request;
-    char *dn;
     char *hostlist = NULL;
     int rc, len, cur = 0;
     unsigned char reply[1024];
@@ -112,8 +210,7 @@ int ldap_domain2hostlist(
 
     request = LDAP_MALLOC(strlen(domain) + sizeof("_ldap._tcp."));
     if (request == NULL) {
-       rc = LDAP_NO_MEMORY;
-       goto out;
+               return LDAP_NO_MEMORY;
     }
     sprintf(request, "_ldap._tcp.%s", domain);
 
@@ -121,7 +218,8 @@ int ldap_domain2hostlist(
     ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
 #endif
 
-    len = res_search(request, C_IN, T_SRV, reply, sizeof(reply));
+    rc = LDAP_UNAVAILABLE;
+    len = res_query(request, C_IN, T_SRV, reply, sizeof(reply));
     if (len >= 0) {
        unsigned char *p;
        char host[1024];
@@ -203,5 +301,5 @@ int ldap_domain2hostlist(
     return rc;
 #else
     return LDAP_NOT_SUPPORTED;
-#endif                         /* HAVE_RES_SEARCH */
+#endif /* HAVE_RES_QUERY */
 }