s3-libnet: Make sure we do not overwrite precreated SPNs.
authorGünther Deschner <gd@samba.org>
Fri, 26 Sep 2014 01:35:43 +0000 (03:35 +0200)
committerKarolin Seeger <kseeger@samba.org>
Thu, 9 Oct 2014 19:23:05 +0000 (21:23 +0200)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=9984

Signed-off-by: Günther Deschner <gd@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
Autobuild-User(master): Günther Deschner <gd@samba.org>
Autobuild-Date(master): Fri Sep 26 08:22:45 CEST 2014 on sn-devel-104

(cherry picked from commit 0aacbe78bb40d76b65087c2a197c92b0101e625e)

source3/libnet/libnet_join.c

index 3611cc720fcc85fdb79cd53a1663fa0597a961f6..aa7b5cb83e948ea5c579f3362c6b3659ce662675 100644 (file)
@@ -388,8 +388,10 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
        ADS_STATUS status;
        ADS_MODLIST mods;
        fstring my_fqdn;
-       const char *spn_array[3] = {NULL, NULL, NULL};
+       const char **spn_array = NULL;
+       size_t num_spns = 0;
        char *spn = NULL;
+       bool ok;
 
        /* Find our DN */
 
@@ -398,6 +400,14 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
                return status;
        }
 
+       status = libnet_join_get_machine_spns(mem_ctx,
+                                             r,
+                                             discard_const_p(char **, &spn_array),
+                                             &num_spns);
+       if (!ADS_ERR_OK(status)) {
+               DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
+       }
+
        /* Windows only creates HOST/shortname & HOST/fqdn. */
 
        spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
@@ -407,7 +417,15 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
        if (!strupper_m(spn)) {
                return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
        }
-       spn_array[0] = spn;
+
+       ok = ads_element_in_array(spn_array, num_spns, spn);
+       if (!ok) {
+               ok = add_string_to_array(spn_array, spn,
+                                        &spn_array, (int *)&num_spns);
+               if (!ok) {
+                       return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
+               }
+       }
 
        if (!name_to_fqdn(my_fqdn, r->in.machine_name)
            || (strchr(my_fqdn, '.') == NULL)) {
@@ -424,8 +442,23 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
                if (!spn) {
                        return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
                }
-               spn_array[1] = spn;
+
+               ok = ads_element_in_array(spn_array, num_spns, spn);
+               if (!ok) {
+                       ok = add_string_to_array(spn_array, spn,
+                                                &spn_array, (int *)&num_spns);
+                       if (!ok) {
+                               return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
+                       }
+               }
+       }
+
+       /* make sure to NULL terminate the array */
+       spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
+       if (spn_array == NULL) {
+               return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
        }
+       spn_array[num_spns] = NULL;
 
        mods = ads_init_mods(mem_ctx);
        if (!mods) {