s4/libnet: Add "dNSHostName" attribute when joining the Site
[kamenim/samba.git] / source4 / libnet / libnet_site.c
index 4a32ab92ed58b4a1e6559ecfac473f254e29eac1..24311df5a2791c7b8d3f1603ddf689cb61970917 100644 (file)
@@ -22,9 +22,9 @@
 #include "libcli/cldap/cldap.h"
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
-#include "librpc/rpc/dcerpc.h"
 #include "libcli/resolve/resolve.h"
 #include "param/param.h"
+#include "lib/tsocket/tsocket.h"
 
 /**
  * 1. Setup a CLDAP socket.
@@ -41,6 +41,8 @@ NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_context *lctx, struct li
 
        struct cldap_socket *cldap = NULL;
        struct cldap_netlogon search;
+       int ret;
+       struct tsocket_address *dest_address;
 
        tmp_ctx = talloc_named(ctx, 0, "libnet_FindSite temp context");
        if (!tmp_ctx) {
@@ -50,14 +52,30 @@ NTSTATUS libnet_FindSite(TALLOC_CTX *ctx, struct libnet_context *lctx, struct li
 
        /* Resolve the site name. */
        ZERO_STRUCT(search);
-       search.in.dest_address = r->in.dest_address;
-       search.in.dest_port = r->in.cldap_port;
+       search.in.dest_address = NULL;
+       search.in.dest_port = 0;
        search.in.acct_control = -1;
        search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
        search.in.map_response = true;
 
-       cldap = cldap_socket_init(tmp_ctx, lctx->event_ctx, lp_iconv_convenience(lctx->lp_ctx));
-       status = cldap_netlogon(cldap, tmp_ctx, &search);
+       ret = tsocket_address_inet_from_strings(tmp_ctx, "ip",
+                                               r->in.dest_address,
+                                               r->in.cldap_port,
+                                               &dest_address);
+       if (ret != 0) {
+               r->out.error_string = NULL;
+               status = map_nt_error_from_unix(errno);
+               return status;
+       }
+
+       /* we want to use non async calls, so we're not passing an event context */
+       status = cldap_socket_init(tmp_ctx, NULL, NULL, dest_address, &cldap);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(tmp_ctx);
+               r->out.error_string = NULL;
+               return status;
+       }
+       status = cldap_netlogon(cldap, lp_iconv_convenience(lctx->lp_ctx), tmp_ctx, &search);
        if (!NT_STATUS_IS_OK(status)
            || !search.out.netlogon.data.nt5_ex.client_site) {
                /*
@@ -135,6 +153,7 @@ NTSTATUS libnet_JoinSite(struct libnet_context *ctx,
        const char *config_dn_str;
        struct nbt_name name;
        const char *dest_addr = NULL;
+       char *dns_host_name;
 
        tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
        if (!tmp_ctx) {
@@ -202,6 +221,15 @@ NTSTATUS libnet_JoinSite(struct libnet_context *ctx,
                talloc_free(tmp_ctx);
                return NT_STATUS_NO_MEMORY;
        }
+       dns_host_name = talloc_asprintf(tmp_ctx, "%s.%s",
+                                       libnet_r->in.netbios_name, libnet_r->out.realm);
+       if (!dns_host_name) {
+               r->out.error_string = NULL;
+               talloc_free(tmp_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+       strlower_m(dns_host_name);
+       rtn = ldb_msg_add_string(msg, "dNSHostName", dns_host_name);
 
        server_dn = ldb_dn_new(tmp_ctx, remote_ldb, server_dn_str);
        if ( ! ldb_dn_validate(server_dn)) {
@@ -216,7 +244,7 @@ NTSTATUS libnet_JoinSite(struct libnet_context *ctx,
 
        rtn = ldb_add(remote_ldb, msg);
        if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
-               int i;
+               unsigned int i;
 
                /* make a 'modify' msg, and only for serverReference */
                msg = ldb_msg_new(tmp_ctx);