s3-utils: Use ads_do_search_retry in net ads search
[ddiss/samba.git] / source3 / utils / net_ads.c
index 48df56479232c89639d90193e04ee21107d3757c..f751d688aa5219bdfb53f894b7e58bb636a44d4b 100644 (file)
@@ -22,8 +22,9 @@
 
 #include "includes.h"
 #include "utils/net.h"
+#include "rpc_client/cli_pipe.h"
 #include "librpc/gen_ndr/ndr_krb5pac.h"
-#include "../librpc/gen_ndr/cli_spoolss.h"
+#include "../librpc/gen_ndr/ndr_spoolss.h"
 #include "nsswitch/libwbclient/wbclient.h"
 #include "ads.h"
 #include "libads/cldap.h"
@@ -35,6 +36,7 @@
 #include "secrets.h"
 #include "krb5_env.h"
 #include "../libcli/security/security.h"
+#include "libsmb/libsmb.h"
 
 #ifdef HAVE_ADS
 
@@ -1120,7 +1122,7 @@ static WERROR check_ads_config( void )
 *******************************************************************/
 
 #if defined(WITH_DNS_UPDATES)
-#include "dns.h"
+#include "../lib/addns/dns.h"
 DNS_ERROR DoDNSUpdate(char *pszServerName,
                      const char *pszDomainName, const char *pszHostName,
                      const struct sockaddr_storage *sslist,
@@ -1132,7 +1134,7 @@ static NTSTATUS net_update_dns_internal(TALLOC_CTX *ctx, ADS_STRUCT *ads,
                                        int num_addrs)
 {
        struct dns_rr_ns *nameservers = NULL;
-       int ns_count = 0;
+       int ns_count = 0, i;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        DNS_ERROR dns_err;
        fstring dns_server;
@@ -1197,16 +1199,31 @@ static NTSTATUS net_update_dns_internal(TALLOC_CTX *ctx, ADS_STRUCT *ads,
 
        }
 
-       /* Now perform the dns update - we'll try non-secure and if we fail,
-          we'll follow it up with a secure update */
+       for (i=0; i < ns_count; i++) {
 
-       fstrcpy( dns_server, nameservers[0].hostname );
+               /* Now perform the dns update - we'll try non-secure and if we fail,
+                  we'll follow it up with a secure update */
+
+               fstrcpy( dns_server, nameservers[i].hostname );
+
+               dns_err = DoDNSUpdate(dns_server, dnsdomain, machine_name, addrs, num_addrs);
+               if (ERR_DNS_IS_OK(dns_err)) {
+                       status = NT_STATUS_OK;
+                       goto done;
+               }
+
+               if (ERR_DNS_EQUAL(dns_err, ERROR_DNS_INVALID_NAME_SERVER) ||
+                   ERR_DNS_EQUAL(dns_err, ERROR_DNS_CONNECTION_FAILED) ||
+                   ERR_DNS_EQUAL(dns_err, ERROR_DNS_SOCKET_ERROR)) {
+                       DEBUG(1,("retrying DNS update with next nameserver after receiving %s\n",
+                               dns_errstr(dns_err)));
+                       continue;
+               }
 
-       dns_err = DoDNSUpdate(dns_server, dnsdomain, machine_name, addrs, num_addrs);
-       if (!ERR_DNS_IS_OK(dns_err)) {
                d_printf(_("DNS Update for %s failed: %s\n"),
                        machine_name, dns_errstr(dns_err));
                status = NT_STATUS_UNSUCCESSFUL;
+               goto done;
        }
 
 done:
@@ -1396,6 +1413,11 @@ int net_ads_join(struct net_context *c, int argc, const char **argv)
        r->in.msg_ctx           = c->msg_ctx;
 
        werr = libnet_Join(ctx, r);
+       if (W_ERROR_EQUAL(werr, WERR_DCNOTFOUND) &&
+           strequal(domain, lp_realm())) {
+               r->in.domain_name = lp_workgroup();
+               werr = libnet_Join(ctx, r);
+       }
        if (!W_ERROR_IS_OK(werr)) {
                goto fail;
        }
@@ -1814,7 +1836,7 @@ static int net_ads_printer_publish(struct net_context *c, int argc, const char *
                                        Undefined);
 
        if (NT_STATUS_IS_ERR(nt_status)) {
-               d_fprintf(stderr, _("Unable to open a connnection to %s to "
+               d_fprintf(stderr, _("Unable to open a connection to %s to "
                                    "obtain data for %s\n"),
                          servername, printername);
                ads_destroy(&ads);
@@ -1863,7 +1885,7 @@ static int net_ads_printer_publish(struct net_context *c, int argc, const char *
 
        nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_spoolss.syntax_id, &pipe_hnd);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               d_fprintf(stderr, _("Unable to open a connnection to the spoolss pipe on %s\n"),
+               d_fprintf(stderr, _("Unable to open a connection to the spoolss pipe on %s\n"),
                         servername);
                SAFE_FREE(prt_dn);
                ads_destroy(&ads);
@@ -2185,7 +2207,7 @@ static int net_ads_search(struct net_context *c, int argc, const char **argv)
        ldap_exp = argv[0];
        attrs = (argv + 1);
 
-       rc = ads_do_search_all(ads, ads->config.bind_path,
+       rc = ads_do_search_retry(ads, ads->config.bind_path,
                               LDAP_SCOPE_SUBTREE,
                               ldap_exp, attrs, &res);
        if (!ADS_ERR_OK(rc)) {