X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Fdnssrv.c;h=f43d4f4b0d6af3ef16257284dcab330beb694a10;hb=e22dc83ac096d1239a6903199f5dc3231b0f444b;hp=e2a9c0af291d87ee94ab212c74582940af60a393;hpb=472484725f862cc0ee27877c59e11ea4a54e7fcf;p=openldap diff --git a/libraries/libldap/dnssrv.c b/libraries/libldap/dnssrv.c index e2a9c0af29..f43d4f4b0d 100644 --- a/libraries/libldap/dnssrv.c +++ b/libraries/libldap/dnssrv.c @@ -1,7 +1,16 @@ /* $OpenLDAP$ */ -/* - * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved. - * COPYING RESTRICTIONS APPLY, see COPYRIGHT file +/* This work is part of OpenLDAP Software . + * + * Copyright 1998-2009 The OpenLDAP Foundation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. + * + * A copy of this license is available in the file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . */ /* @@ -28,41 +37,39 @@ #include #endif -/* Sometimes this is not defined. */ -#ifndef T_SRV -#define T_SRV 33 -#endif /* T_SRV */ - int ldap_dn2domain( LDAP_CONST char *dn_in, char **domainp) { int i, j; char *ndomain; - LDAPDN *dn = NULL; - LDAPRDN *rdn = NULL; + LDAPDN dn = NULL; + LDAPRDN rdn = NULL; LDAPAVA *ava = NULL; - struct berval domain = { 0, NULL }; - static struct berval DC = BER_BVC("DC"); - static struct berval DCOID = BER_BVC("0.9.2342.19200300.100.1.25"); + struct berval domain = BER_BVNULL; + static const struct berval DC = BER_BVC("DC"); + static const struct berval DCOID = BER_BVC("0.9.2342.19200300.100.1.25"); assert( dn_in != NULL ); assert( domainp != NULL ); + *domainp = NULL; + if ( ldap_str2dn( dn_in, &dn, LDAP_DN_FORMAT_LDAP ) != LDAP_SUCCESS ) { return -2; } - if( dn ) for( i=0; (*dn)[i] != NULL; i++ ) { - rdn = (*dn)[i]; + if( dn ) for( i=0; dn[i] != NULL; i++ ) { + rdn = dn[i]; - for( j=0; (*rdn)[j] != NULL; j++ ) { - ava = (*rdn)[j]; + for( j=0; rdn[j] != NULL; j++ ) { + ava = rdn[j]; - if( (*dn)[i][j][1] == NULL && - !ava->la_flags && ava->la_value.bv_len && + if( rdn[j+1] == NULL && + (ava->la_flags & LDAP_AVA_STRING) && + ava->la_value.bv_len && ( ber_bvstrcasecmp( &ava->la_attr, &DC ) == 0 - || ber_bvstrcasecmp( &ava->la_attr, &DCOID ) == 0 ) ) + || ber_bvcmp( &ava->la_attr, &DCOID ) == 0 ) ) { if( domain.bv_len == 0 ) { ndomain = LDAP_REALLOC( domain.bv_val, @@ -121,46 +128,50 @@ int ldap_domain2dn( LDAP_CONST char *domain_in, char **dnp) { - char *domain, *s, *tok_r, *dn; - size_t loc; + char *domain, *s, *tok_r, *dn, *dntmp; + size_t loc; assert( domain_in != NULL ); assert( dnp != NULL ); - domain = LDAP_STRDUP(domain_in); - if (domain == NULL) { + domain = LDAP_STRDUP(domain_in); + if (domain == NULL) { return LDAP_NO_MEMORY; - } - dn = NULL; - loc = 0; - - for (s = ldap_pvt_strtok(domain, ".", &tok_r); - s != NULL; - s = ldap_pvt_strtok(NULL, ".", &tok_r)) { - size_t len = strlen(s); - - dn = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len ); - if (dn == NULL) { - LDAP_FREE(domain); - return LDAP_NO_MEMORY; - } - if (loc > 0) { - /* not first time. */ - strcpy(dn + loc, ","); - loc++; } - strcpy(dn + loc, "dc="); - loc += sizeof("dc=")-1; + dn = NULL; + loc = 0; + + for (s = ldap_pvt_strtok(domain, ".", &tok_r); + s != NULL; + s = ldap_pvt_strtok(NULL, ".", &tok_r)) + { + size_t len = strlen(s); + + dntmp = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len ); + if (dntmp == NULL) { + if (dn != NULL) + LDAP_FREE(dn); + LDAP_FREE(domain); + return LDAP_NO_MEMORY; + } - strcpy(dn + loc, s); - loc += len; - } + dn = dntmp; - LDAP_FREE(domain); + if (loc > 0) { + /* not first time. */ + strcpy(dn + loc, ","); + loc++; + } + strcpy(dn + loc, "dc="); + loc += sizeof("dc=")-1; - *dnp = dn; + strcpy(dn + loc, s); + loc += len; + } - return LDAP_SUCCESS; + LDAP_FREE(domain); + *dnp = dn; + return LDAP_SUCCESS; } /* @@ -172,10 +183,11 @@ int ldap_domain2hostlist( char **list ) { #ifdef HAVE_RES_QUERY +#define DNSBUFSIZ (64*1024) char *request; char *hostlist = NULL; int rc, len, cur = 0; - unsigned char reply[1024]; + unsigned char reply[DNSBUFSIZ]; assert( domain != NULL ); assert( list != NULL ); @@ -195,17 +207,41 @@ int ldap_domain2hostlist( #endif rc = LDAP_UNAVAILABLE; +#ifdef NS_HFIXEDSZ + /* Bind 8/9 interface */ + len = res_query(request, ns_c_in, ns_t_srv, reply, sizeof(reply)); +# ifndef T_SRV +# define T_SRV ns_t_srv +# endif +#else + /* Bind 4 interface */ +# ifndef T_SRV +# define T_SRV 33 +# endif + len = res_query(request, C_IN, T_SRV, reply, sizeof(reply)); +#endif if (len >= 0) { unsigned char *p; - char host[1024]; + char host[DNSBUFSIZ]; int status; u_short port; /* int priority, weight; */ /* Parse out query */ p = reply; + +#ifdef NS_HFIXEDSZ + /* Bind 8/9 interface */ + p += NS_HFIXEDSZ; +#elif defined(HFIXEDSZ) + /* Bind 4 interface w/ HFIXEDSZ */ + p += HFIXEDSZ; +#else + /* Bind 4 interface w/o HFIXEDSZ */ p += sizeof(HEADER); +#endif + status = dn_expand(reply, reply + len, p, host, sizeof(host)); if (status < 0) { goto out; @@ -239,8 +275,12 @@ int ldap_domain2hostlist( /* weight = (p[2] << 8) | p[3]; */ port = (p[4] << 8) | p[5]; - buflen = strlen(host) + sizeof(":65355 "); - hostlist = (char *) LDAP_REALLOC(hostlist, cur + buflen); + if ( port == 0 || host[ 0 ] == '\0' ) { + goto add_size; + } + + buflen = strlen(host) + STRLENOF(":65355 "); + hostlist = (char *) LDAP_REALLOC(hostlist, cur + buflen + 1); if (hostlist == NULL) { rc = LDAP_NO_MEMORY; goto out; @@ -251,6 +291,7 @@ int ldap_domain2hostlist( } cur += sprintf(&hostlist[cur], "%s:%hd", host, port); } +add_size:; p += size; } }