>From 4807c5d5f641f561d02f3671da36227da181358c Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Thu, 23 Jul 2015 10:51:50 -0400
Subject: [PATCH 6/9] DYNDNS: use realm and server commands only as fallback

Resolves:
https://fedorahosted.org/sssd/ticket/2195
---
 src/providers/ad/ad_dyndns.c     |  9 ---------
 src/providers/dp_dyndns.c        |  5 +++++
 src/providers/ipa/ipa_dyndns.c   |  7 -------
 src/providers/ldap/sdap_dyndns.c | 37 ++++++++++++++++++++-----------------
 src/providers/ldap/sdap_dyndns.h |  1 -
 5 files changed, 25 insertions(+), 34 deletions(-)

diff --git a/src/providers/ad/ad_dyndns.c b/src/providers/ad/ad_dyndns.c
index 5f8638128b966f0981c85a44e50e3201c73d561e..a4d38c442a69ff0440e459da32d8852bda314484 100644
--- a/src/providers/ad/ad_dyndns.c
+++ b/src/providers/ad/ad_dyndns.c
@@ -159,7 +159,6 @@ static void ad_dyndns_nsupdate_done(struct tevent_req *req)
 
 struct ad_dyndns_update_state {
     struct ad_options *ad_ctx;
-    const char *servername;
 };
 
 static void ad_dyndns_sdap_update_done(struct tevent_req *subreq);
@@ -217,13 +216,6 @@ ad_dyndns_update_send(struct ad_options *ctx)
         goto done;
     }
 
-    state->servername = talloc_strdup(state, lud->lud_host);
-    ldap_free_urldesc(lud);
-    if (!state->servername) {
-        ret = ENOMEM;
-        goto done;
-    }
-
     subreq = sdap_dyndns_update_send(state, sdap_ctx->be->ev,
                                      sdap_ctx->be,
                                      ctx->dyndns_ctx->opts,
@@ -235,7 +227,6 @@ ad_dyndns_update_send(struct ad_options *ctx)
                                                        AD_HOSTNAME),
                                      dp_opt_get_string(ctx->basic,
                                                        AD_KRB5_REALM),
-                                     state->servername,
                                      dp_opt_get_int(ctx->dyndns_ctx->opts,
                                                     DP_OPT_DYNDNS_TTL),
                                      false);
diff --git a/src/providers/dp_dyndns.c b/src/providers/dp_dyndns.c
index ae3f913ee392a6513f75aab497e7f2d784784748..efa6d73dcf66cb685cf3f331b38e75b5435aead1 100644
--- a/src/providers/dp_dyndns.c
+++ b/src/providers/dp_dyndns.c
@@ -394,11 +394,16 @@ nsupdate_msg_create_common(TALLOC_CTX *mem_ctx, const char *realm,
     tmp_ctx = talloc_new(NULL);
     if (tmp_ctx == NULL) return NULL;
 
+    if (realm != NULL) {
 #ifdef HAVE_NSUPDATE_REALM
     realm_directive = talloc_asprintf(tmp_ctx, "realm %s\n", realm);
 #else
     realm_directive = talloc_asprintf(tmp_ctx, "\n");
 #endif
+    } else {
+        realm_directive = talloc_asprintf(tmp_ctx, "\n");
+    }
+
     if (!realm_directive) {
         goto fail;
     }
diff --git a/src/providers/ipa/ipa_dyndns.c b/src/providers/ipa/ipa_dyndns.c
index e7026eb275798f0ed21fb8490295b6e6d419d8ee..1a7014718366ce3c898eef24eabd6903101dc92e 100644
--- a/src/providers/ipa/ipa_dyndns.c
+++ b/src/providers/ipa/ipa_dyndns.c
@@ -153,7 +153,6 @@ ipa_dyndns_update_send(struct ipa_options *ctx)
     struct ipa_dyndns_update_state *state;
     struct tevent_req *req, *subreq;
     struct sdap_id_ctx *sdap_ctx = ctx->id_ctx->sdap_id_ctx;
-    const char *servername;
 
     DEBUG(SSSDBG_TRACE_FUNC, "Performing update\n");
 
@@ -179,11 +178,6 @@ ipa_dyndns_update_send(struct ipa_options *ctx)
         ret = EIO;
         goto done;
     }
-    servername = ctx->service->sdap->uri + 7;
-    if (servername[0] == '\0') {
-        ret = EIO;
-        goto done;
-    }
 
     subreq = sdap_dyndns_update_send(state, sdap_ctx->be->ev,
                                      sdap_ctx->be,
@@ -196,7 +190,6 @@ ipa_dyndns_update_send(struct ipa_options *ctx)
                                                        IPA_HOSTNAME),
                                      dp_opt_get_string(ctx->basic,
                                                        IPA_KRB5_REALM),
-                                     servername,
                                      dp_opt_get_int(ctx->dyndns_ctx->opts,
                                                     DP_OPT_DYNDNS_TTL),
                                      true);
diff --git a/src/providers/ldap/sdap_dyndns.c b/src/providers/ldap/sdap_dyndns.c
index 2a179fd1b5e88bdf2442657ff6fa1dcc55417467..8a8940c60334c71b50a92dcc2eb740a9ea5fc486 100644
--- a/src/providers/ldap/sdap_dyndns.c
+++ b/src/providers/ldap/sdap_dyndns.c
@@ -81,7 +81,6 @@ sdap_dyndns_update_send(TALLOC_CTX *mem_ctx,
                         const char *ifname,
                         const char *hostname,
                         const char *realm,
-                        const char *servername,
                         const int ttl,
                         bool check_diff)
 {
@@ -99,7 +98,7 @@ sdap_dyndns_update_send(TALLOC_CTX *mem_ctx,
     state->update_ptr = dp_opt_get_bool(opts, DP_OPT_DYNDNS_UPDATE_PTR);
     state->hostname = hostname;
     state->realm = realm;
-    state->servername = servername;
+    state->servername = NULL;
     state->fallback_mode = false;
     state->ttl = ttl;
     state->be_res = be_ctx->be_res;
@@ -311,18 +310,20 @@ sdap_dyndns_update_step(struct tevent_req *req)
     errno_t ret;
     struct sdap_dyndns_update_state *state;
     const char *servername;
+    const char *realm;
     struct tevent_req *subreq;
 
     state = tevent_req_data(req, struct sdap_dyndns_update_state);
 
     servername = NULL;
-    if (state->fallback_mode == true &&
-        state->servername) {
+    realm = NULL;
+    if (state->fallback_mode) {
         servername = state->servername;
+        realm = state->realm;
     }
 
-    ret = be_nsupdate_create_fwd_msg(state, state->realm,
-                                     servername, state->hostname,
+    ret = be_nsupdate_create_fwd_msg(state, realm, servername,
+                                     state->hostname,
                                      state->ttl, state->remove_af,
                                      state->addresses,
                                      &state->update_msg);
@@ -359,11 +360,12 @@ sdap_dyndns_update_done(struct tevent_req *subreq)
     talloc_zfree(subreq);
     if (ret != EOK) {
         /* If the update didn't succeed, we can retry using the server name */
-        if (state->fallback_mode == false && state->servername &&
-            WIFEXITED(child_status) && WEXITSTATUS(child_status) != 0) {
+        if (state->fallback_mode == false
+                && WIFEXITED(child_status)
+                && WEXITSTATUS(child_status) != 0) {
             state->fallback_mode = true;
             DEBUG(SSSDBG_MINOR_FAILURE,
-                   "nsupdate failed, retrying with server name\n");
+                  "nsupdate failed, retrying.\n");
             ret = sdap_dyndns_update_step(req);
             if (ret == EOK) {
                 return;
@@ -395,18 +397,19 @@ sdap_dyndns_update_ptr_step(struct tevent_req *req)
     errno_t ret;
     struct sdap_dyndns_update_state *state;
     const char *servername;
+    const char *realm;
     struct tevent_req *subreq;
 
     state = tevent_req_data(req, struct sdap_dyndns_update_state);
 
     servername = NULL;
-    if (state->fallback_mode == true &&
-        state->servername) {
+    realm = NULL;
+    if (state->fallback_mode == true) {
         servername = state->servername;
+        realm = state->realm;
     }
 
-    ret = be_nsupdate_create_ptr_msg(state, state->realm,
-                                     servername, state->hostname,
+    ret = be_nsupdate_create_ptr_msg(state, realm, servername, state->hostname,
                                      state->ttl, state->remove_af,
                                      state->addresses, state->dns_addrlist,
                                      &state->update_msg);
@@ -443,11 +446,11 @@ sdap_dyndns_update_ptr_done(struct tevent_req *subreq)
     talloc_zfree(subreq);
     if (ret != EOK) {
         /* If the update didn't succeed, we can retry using the server name */
-        if (state->fallback_mode == false && state->servername &&
-            WIFEXITED(child_status) && WEXITSTATUS(child_status) != 0) {
+        if (state->fallback_mode == false
+                && WIFEXITED(child_status)
+                && WEXITSTATUS(child_status) != 0) {
             state->fallback_mode = true;
-            DEBUG(SSSDBG_MINOR_FAILURE,
-                   "nsupdate failed, retrying with server name\n");
+            DEBUG(SSSDBG_MINOR_FAILURE, "nsupdate failed, retrying\n");
             ret = sdap_dyndns_update_ptr_step(req);
             if (ret == EOK) {
                 return;
diff --git a/src/providers/ldap/sdap_dyndns.h b/src/providers/ldap/sdap_dyndns.h
index a9481b7941be6af0f172afce5f4d54f57ef85e48..fda4530b61cfcfcf98207638efd4de70498d90dc 100644
--- a/src/providers/ldap/sdap_dyndns.h
+++ b/src/providers/ldap/sdap_dyndns.h
@@ -40,7 +40,6 @@ sdap_dyndns_update_send(TALLOC_CTX *mem_ctx,
                         const char *ifname,
                         const char *hostname,
                         const char *realm,
-                        const char *servername,
                         const int ttl,
                         bool check_diff);
 
-- 
2.4.3

