s4:dcesrv_netr_DsRAddressToSitenamesExW - fix the detection of the address family...
[kamenim/samba.git] / source4 / rpc_server / netlogon / dcerpc_netlogon.c
index d7aa4252d2e5bffed51037bc9d2227cf61e51d78..28e517f0c945778efc767cde20007ce22823cdca 100644 (file)
@@ -33,7 +33,7 @@
 #include "lib/messaging/irpc.h"
 #include "librpc/gen_ndr/ndr_irpc.h"
 #include "cldap_server/cldap_server.h"
-#include "lib/socket/socket.h"
+#include "lib/tsocket/tsocket.h"
 
 struct netlogon_server_pipe_state {
        struct netr_Credential client_challenge;
@@ -249,7 +249,6 @@ static NTSTATUS dcesrv_netr_ServerAuthenticate3(struct dcesrv_call_state *dce_ca
        creds->sid = samdb_result_dom_sid(creds, msgs[0], "objectSid");
 
        nt_status = schannel_save_creds_state(mem_ctx,
-                                             lp_iconv_convenience(dce_call->conn->dce_ctx->lp_ctx),
                                              lp_private_dir(dce_call->conn->dce_ctx->lp_ctx),
                                              creds);
 
@@ -362,7 +361,6 @@ static NTSTATUS dcesrv_netr_creds_server_step_check(struct dcesrv_call_state *dc
        }
 
        nt_status = schannel_check_creds_state(mem_ctx,
-                                              lp_iconv_convenience(dce_call->conn->dce_ctx->lp_ctx),
                                               lp_private_dir(dce_call->conn->dce_ctx->lp_ctx),
                                               computer_name,
                                               received_authenticator,
@@ -714,7 +712,6 @@ static NTSTATUS dcesrv_netr_LogonSamLogonEx(struct dcesrv_call_state *dce_call,
        struct netlogon_creds_CredentialState *creds;
 
        nt_status = schannel_get_creds_state(mem_ctx,
-                                            lp_iconv_convenience(dce_call->conn->dce_ctx->lp_ctx),
                                             lp_private_dir(dce_call->conn->dce_ctx->lp_ctx),
                                             r->in.computer_name, &creds);
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -988,6 +985,10 @@ static WERROR dcesrv_netr_LogonControl2(struct dcesrv_call_state *dce_call, TALL
        return werr;
 }
 
+static WERROR fill_trusted_domains_array(TALLOC_CTX *mem_ctx,
+                                        struct ldb_context *sam_ctx,
+                                        struct netr_DomainTrustList *trusts,
+                                        uint32_t trust_flags);
 
 /*
   netr_GetAnyDCName
@@ -995,18 +996,62 @@ static WERROR dcesrv_netr_LogonControl2(struct dcesrv_call_state *dce_call, TALL
 static WERROR dcesrv_netr_GetAnyDCName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                       struct netr_GetAnyDCName *r)
 {
-       struct netr_GetDcName r2;
+       struct netr_DomainTrustList *trusts;
+       struct ldb_context *sam_ctx;
+       struct loadparm_context *lp_ctx = dce_call->conn->dce_ctx->lp_ctx;
+       uint32_t i;
        WERROR werr;
 
-       ZERO_STRUCT(r2);
+       *r->out.dcname = NULL;
+
+       if ((r->in.domainname == NULL) || (r->in.domainname[0] == '\0')) {
+               /* if the domainname parameter wasn't set assume our domain */
+               r->in.domainname = lp_workgroup(lp_ctx);
+       }
+
+       sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, lp_ctx,
+                               dce_call->conn->auth_state.session_info);
+       if (sam_ctx == NULL) {
+               return WERR_DS_UNAVAILABLE;
+       }
 
-       r2.in.logon_server      = r->in.logon_server;
-       r2.in.domainname        = r->in.domainname;
-       r2.out.dcname           = r->out.dcname;
+       if (strcasecmp(r->in.domainname, lp_workgroup(lp_ctx)) == 0) {
+               /* well we asked for a DC of our own domain */
+               if (samdb_is_pdc(sam_ctx)) {
+                       /* we are the PDC of the specified domain */
+                       return WERR_NO_SUCH_DOMAIN;
+               }
 
-       werr = dcesrv_netr_GetDcName(dce_call, mem_ctx, &r2);
+               *r->out.dcname = talloc_asprintf(mem_ctx, "\\%s",
+                                               lp_netbios_name(lp_ctx));
+               W_ERROR_HAVE_NO_MEMORY(*r->out.dcname);
 
-       return werr;
+               return WERR_OK;
+       }
+
+       /* Okay, now we have to consider the trusted domains */
+
+       trusts = talloc_zero(mem_ctx, struct netr_DomainTrustList);
+       W_ERROR_HAVE_NO_MEMORY(trusts);
+
+       trusts->count = 0;
+
+       werr = fill_trusted_domains_array(mem_ctx, sam_ctx, trusts,
+                                         NETR_TRUST_FLAG_INBOUND
+                                         | NETR_TRUST_FLAG_OUTBOUND);
+       W_ERROR_NOT_OK_RETURN(werr);
+
+       for (i = 0; i < trusts->count; i++) {
+               if (strcasecmp(r->in.domainname, trusts->array[i].netbios_name) == 0) {
+                       /* FIXME: Here we need to find a DC for the specified
+                        * trusted domain. */
+
+                       /* return WERR_OK; */
+                       return WERR_NO_SUCH_DOMAIN;
+               }
+       }
+
+       return WERR_NO_SUCH_DOMAIN;
 }
 
 
@@ -1021,9 +1066,9 @@ static NTSTATUS dcesrv_netr_DatabaseRedo(struct dcesrv_call_state *dce_call, TAL
 
 
 /*
-  netr_NetrEnumerateTurstedDomains
+  netr_NetrEnumerateTrustedDomains
 */
-static WERROR dcesrv_netr_NetrEnumerateTrustedDomains(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
+static NTSTATUS dcesrv_netr_NetrEnumerateTrustedDomains(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                       struct netr_NetrEnumerateTrustedDomains *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
@@ -1088,7 +1133,19 @@ static WERROR dcesrv_netr_NETRLOGONCOMPUTECLIENTDIGEST(struct dcesrv_call_state
 static WERROR dcesrv_netr_DsRGetSiteName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                                  struct netr_DsRGetSiteName *r)
 {
-       DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
+       struct ldb_context *sam_ctx;
+       struct loadparm_context *lp_ctx = dce_call->conn->dce_ctx->lp_ctx;
+
+       sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, lp_ctx,
+                               dce_call->conn->auth_state.session_info);
+       if (sam_ctx == NULL) {
+               return WERR_DS_UNAVAILABLE;
+       }
+
+       *r->out.site = samdb_server_site_name(sam_ctx, mem_ctx);
+       W_ERROR_HAVE_NO_MEMORY(*r->out.site);
+
+       return WERR_OK;
 }
 
 
@@ -1163,11 +1220,10 @@ static NTSTATUS dcesrv_netr_LogonGetDomainInfo(struct dcesrv_call_state *dce_cal
                "securityIdentifier", "trustPartner", NULL };
        const char * const attrs2[] = { "dNSHostName",
                "msDS-SupportedEncryptionTypes", NULL };
-       const char * const attrs3[] = { NULL };
        const char *temp_str, *temp_str2;
        const char *old_dns_hostname;
        struct ldb_context *sam_ctx;
-       struct ldb_message **res0, **res1, **res2, **res3, *new_msg;
+       struct ldb_message **res1, **res2, **res3, *new_msg;
        struct ldb_dn *workstation_dn;
        struct netr_DomainInformation *domain_info;
        struct netr_LsaPolicyInformation *lsa_policy_info;
@@ -1189,8 +1245,8 @@ static NTSTATUS dcesrv_netr_LogonGetDomainInfo(struct dcesrv_call_state *dce_cal
        NT_STATUS_NOT_OK_RETURN(status);
 
        sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx,
-               dce_call->conn->dce_ctx->lp_ctx,
-               system_session(dce_call->conn->dce_ctx->lp_ctx));
+                               dce_call->conn->dce_ctx->lp_ctx,
+                               system_session(dce_call->conn->dce_ctx->lp_ctx));
        if (sam_ctx == NULL) {
                return NT_STATUS_INVALID_SYSTEM_SERVICE;
        }
@@ -1198,15 +1254,8 @@ static NTSTATUS dcesrv_netr_LogonGetDomainInfo(struct dcesrv_call_state *dce_cal
        switch (r->in.level) {
        case 1: /* Domain information */
 
-               /*
-                * Updates the DNS hostname when the client wishes that the
-                * server should handle this for him
-                * ("NETR_WS_FLAG_HANDLES_SPN_UPDATE" not set).
-                * See MS-NRPC section 3.5.4.3.9
-                */
-               if ((r->in.query->workstation_info->workstation_flags
-                   & NETR_WS_FLAG_HANDLES_SPN_UPDATE) != 0) {
-                       update_dns_hostname = false;
+               if (r->in.query->workstation_info == NULL) {
+                       return NT_STATUS_INVALID_PARAMETER;
                }
 
                /*
@@ -1226,23 +1275,6 @@ static NTSTATUS dcesrv_netr_LogonGetDomainInfo(struct dcesrv_call_state *dce_cal
                        update_dns_hostname = false;
                }
 
-               /*
-                * Check that the DNS hostname when it should be updated
-                * will be used only by maximum one host.
-                */
-               ret = gendb_search(sam_ctx, mem_ctx,
-                                  ldb_get_default_basedn(sam_ctx),
-                                  &res0, attrs3, "(dNSHostName=%s)",
-                                  r->in.query->workstation_info->dns_hostname);
-               if (ret < 0) {
-                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
-               }
-               if (ret >= 1) {
-                       update_dns_hostname = false;
-               }
-
-               talloc_free(res0);
-
                /* Prepare the workstation DN */
                workstation_dn = ldb_dn_new_fmt(mem_ctx, sam_ctx, "<SID=%s>",
                        dom_sid_string(mem_ctx, creds->sid));
@@ -1259,6 +1291,20 @@ static NTSTATUS dcesrv_netr_LogonGetDomainInfo(struct dcesrv_call_state *dce_cal
                old_dns_hostname = samdb_result_string(res1[0], "dNSHostName",
                        NULL);
 
+               /*
+                * Updates the DNS hostname when the client wishes that the
+                * server should handle this for him
+                * ("NETR_WS_FLAG_HANDLES_SPN_UPDATE" not set). And this is
+                * obviously only checked when we do already have a
+                * "dNSHostName".
+                * See MS-NRPC section 3.5.4.3.9
+                */
+               if ((old_dns_hostname != NULL) &&
+                   (r->in.query->workstation_info->workstation_flags
+                   & NETR_WS_FLAG_HANDLES_SPN_UPDATE) != 0) {
+                       update_dns_hostname = false;
+               }
+
                /* Gets host informations and put them in our directory */
                new_msg = ldb_msg_new(mem_ctx);
                NT_STATUS_HAVE_NO_MEMORY(new_msg);
@@ -1455,16 +1501,6 @@ static WERROR dcesrv_netr_NETRLOGONSENDTOSAM(struct dcesrv_call_state *dce_call,
 }
 
 
-/*
-  netr_DsRAddressToSitenamesW
-*/
-static WERROR dcesrv_netr_DsRAddressToSitenamesW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
-                      struct netr_DsRAddressToSitenamesW *r)
-{
-       DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
-}
-
-
 /*
   netr_DsRGetDCNameEx2
 */
@@ -1475,7 +1511,8 @@ static WERROR dcesrv_netr_DsRGetDCNameEx2(struct dcesrv_call_state *dce_call,
        struct ldb_context *sam_ctx;
        struct netr_DsRGetDCNameInfo *info;
        struct loadparm_context *lp_ctx = dce_call->conn->dce_ctx->lp_ctx;
-       struct socket_address *addr;
+       const struct tsocket_address *remote_address;
+       char *addr = NULL;
        const char *server_site_name;
        char *guid_str;
        struct netlogon_samlogon_response response;
@@ -1489,8 +1526,11 @@ static WERROR dcesrv_netr_DsRGetDCNameEx2(struct dcesrv_call_state *dce_call,
                return WERR_DS_UNAVAILABLE;
        }
 
-       addr = dce_call->conn->transport.get_peer_addr(dce_call->conn, mem_ctx);
-       W_ERROR_HAVE_NO_MEMORY(addr);
+       remote_address = dcesrv_connection_get_remote_address(dce_call->conn);
+       if (tsocket_address_is_inet(remote_address, "ip")) {
+               addr = tsocket_address_inet_addr_string(remote_address, mem_ctx);
+               W_ERROR_HAVE_NO_MEMORY(addr);
+       }
 
        /* "server_unc" is ignored by w2k3 */
 
@@ -1512,9 +1552,9 @@ static WERROR dcesrv_netr_DsRGetDCNameEx2(struct dcesrv_call_state *dce_call,
                                                 r->in.domain_name,
                                                 NULL, guid_str,
                                                 r->in.client_account,
-                                                r->in.mask, addr->addr,
+                                                r->in.mask, addr,
                                                 NETLOGON_NT_VERSION_5EX_WITH_IP,
-                                                lp_ctx, &response);
+                                                lp_ctx, &response, true);
        if (!NT_STATUS_IS_OK(status)) {
                return ntstatus_to_werror(status);
        }
@@ -1523,10 +1563,11 @@ static WERROR dcesrv_netr_DsRGetDCNameEx2(struct dcesrv_call_state *dce_call,
        W_ERROR_HAVE_NO_MEMORY(info);
        info->dc_unc           = talloc_asprintf(mem_ctx, "\\\\%s",
                                                 response.data.nt5_ex.pdc_dns_name);
+       W_ERROR_HAVE_NO_MEMORY(info->dc_unc);
        info->dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
                                           response.data.nt5_ex.sockaddr.pdc_ip);
-       info->dc_address_type = DS_ADDRESS_TYPE_INET;
        W_ERROR_HAVE_NO_MEMORY(info->dc_address);
+       info->dc_address_type  = DS_ADDRESS_TYPE_INET; /* TODO: make this dynamic? for ipv6 */
        info->domain_guid      = response.data.nt5_ex.domain_uuid;
        info->domain_name      = response.data.nt5_ex.dns_domain;
        info->forest_name      = response.data.nt5_ex.forest;
@@ -1615,12 +1656,27 @@ static WERROR dcesrv_netr_NetrEnumerateTrustedDomainsEx(struct dcesrv_call_state
 static WERROR dcesrv_netr_DsRAddressToSitenamesExW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                                                   struct netr_DsRAddressToSitenamesExW *r)
 {
+       struct ldb_context *sam_ctx;
        struct netr_DsRAddressToSitenamesExWCtr *ctr;
-       int i;
+       struct loadparm_context *lp_ctx = dce_call->conn->dce_ctx->lp_ctx;
+       sa_family_t sin_family;
+       struct sockaddr_in *addr;
+#ifdef HAVE_IPV6
+       struct sockaddr_in6 *addr6;
+       char addr_str[INET6_ADDRSTRLEN];
+#else
+       char addr_str[INET_ADDRSTRLEN];
+#endif
+       char *subnet_name;
+       const char *res;
+       uint32_t i;
+
+       sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, lp_ctx,
+                               dce_call->conn->auth_state.session_info);
+       if (sam_ctx == NULL) {
+               return WERR_DS_UNAVAILABLE;
+       }
 
-       /* we should map the provided IPs to site names, once we have
-        * sites support
-        */
        ctr = talloc(mem_ctx, struct netr_DsRAddressToSitenamesExWCtr);
        W_ERROR_HAVE_NO_MEMORY(ctr);
 
@@ -1633,22 +1689,123 @@ static WERROR dcesrv_netr_DsRAddressToSitenamesExW(struct dcesrv_call_state *dce
        W_ERROR_HAVE_NO_MEMORY(ctr->subnetname);
 
        for (i=0; i<ctr->count; i++) {
-               /* FIXME: Hardcoded site name */
-               ctr->sitename[i].string   = "Default-First-Site-Name";
+               ctr->sitename[i].string = NULL;
                ctr->subnetname[i].string = NULL;
+
+               if (r->in.addresses[i].size < sizeof(sa_family_t)) {
+                       continue;
+               }
+               /* The first two byte of the buffer are reserved for the
+                * "sin_family" but for now only the first one is used. */
+               sin_family = r->in.addresses[i].buffer[0];
+
+               switch (sin_family) {
+               case AF_INET:
+                       if (r->in.addresses[i].size < sizeof(struct sockaddr_in)) {
+                               continue;
+                       }
+                       addr = (struct sockaddr_in *) r->in.addresses[i].buffer;
+                       res = inet_ntop(AF_INET, &addr->sin_addr,
+                                       addr_str, sizeof(addr_str));
+                       break;
+#ifdef HAVE_IPV6
+               case AF_INET6:
+                       if (r->in.addresses[i].size < sizeof(struct sockaddr_in6)) {
+                               continue;
+                       }
+                       addr6 = (struct sockaddr_in6 *) r->in.addresses[i].buffer;
+                       res = inet_ntop(AF_INET6, &addr6->sin6_addr,
+                                       addr_str, sizeof(addr_str));
+                       break;
+#endif
+               default:
+                       continue;
+               }
+
+               if (res == NULL) {
+                       continue;
+               }
+
+               ctr->sitename[i].string   = samdb_client_site_name(sam_ctx,
+                                                                  mem_ctx,
+                                                                  addr_str,
+                                                                  &subnet_name);
+               W_ERROR_HAVE_NO_MEMORY(ctr->sitename[i].string);
+               ctr->subnetname[i].string = subnet_name;
        }
 
        return WERR_OK;
 }
 
 
+/*
+  netr_DsRAddressToSitenamesW
+*/
+static WERROR dcesrv_netr_DsRAddressToSitenamesW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
+                      struct netr_DsRAddressToSitenamesW *r)
+{
+       struct netr_DsRAddressToSitenamesExW r2;
+       struct netr_DsRAddressToSitenamesWCtr *ctr;
+       uint32_t i;
+       WERROR werr;
+
+       ZERO_STRUCT(r2);
+
+       r2.in.server_name = r->in.server_name;
+       r2.in.count = r->in.count;
+       r2.in.addresses = r->in.addresses;
+
+       r2.out.ctr = talloc(mem_ctx, struct netr_DsRAddressToSitenamesExWCtr *);
+       W_ERROR_HAVE_NO_MEMORY(r2.out.ctr);
+
+       ctr = talloc(mem_ctx, struct netr_DsRAddressToSitenamesWCtr);
+       W_ERROR_HAVE_NO_MEMORY(ctr);
+
+       *r->out.ctr = ctr;
+
+       ctr->count = r->in.count;
+       ctr->sitename = talloc_array(ctr, struct lsa_String, ctr->count);
+       W_ERROR_HAVE_NO_MEMORY(ctr->sitename);
+
+       werr = dcesrv_netr_DsRAddressToSitenamesExW(dce_call, mem_ctx, &r2);
+
+       for (i=0; i<ctr->count; i++) {
+               ctr->sitename[i].string   = (*r2.out.ctr)->sitename[i].string;
+       }
+
+       return werr;
+}
+
+
 /*
   netr_DsrGetDcSiteCoverageW
 */
 static WERROR dcesrv_netr_DsrGetDcSiteCoverageW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                       struct netr_DsrGetDcSiteCoverageW *r)
 {
-       DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
+       struct ldb_context *sam_ctx;
+       struct DcSitesCtr *ctr;
+       struct loadparm_context *lp_ctx = dce_call->conn->dce_ctx->lp_ctx;
+
+       sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, lp_ctx,
+                               dce_call->conn->auth_state.session_info);
+       if (sam_ctx == NULL) {
+               return WERR_DS_UNAVAILABLE;
+       }
+
+       ctr = talloc(mem_ctx, struct DcSitesCtr);
+       W_ERROR_HAVE_NO_MEMORY(ctr);
+
+       *r->out.ctr = ctr;
+
+       /* For now only return our default site */
+       ctr->num_sites = 1;
+       ctr->sites = talloc_array(ctr, struct lsa_String, ctr->num_sites);
+       W_ERROR_HAVE_NO_MEMORY(ctr->sites);
+       ctr->sites[0].string = samdb_server_site_name(sam_ctx, mem_ctx);
+       W_ERROR_HAVE_NO_MEMORY(ctr->sites[0].string);
+
+       return WERR_OK;
 }
 
 
@@ -1677,7 +1834,8 @@ static WERROR fill_trusted_domains_array(TALLOC_CTX *mem_ctx,
        const char *trust_attrs[] = { "flatname", "trustPartner",
                                      "securityIdentifier", "trustDirection",
                                      "trustType", "trustAttributes", NULL };
-       int i, n;
+       uint32_t n;
+       int i;
        int ret;
 
        if (!(trust_flags & (NETR_TRUST_FLAG_INBOUND |
@@ -1824,7 +1982,7 @@ static WERROR dcesrv_netr_DsrEnumerateDomainTrusts(struct dcesrv_call_state *dce
 
        /* NOTE: we currently are always the root of the forest */
        if (r->in.trust_flags & NETR_TRUST_FLAG_IN_FOREST) {
-               int n = trusts->count;
+               uint32_t n = trusts->count;
 
                ret = gendb_search_dn(sam_ctx, mem_ctx, NULL,
                                      &dom_res, dom_attrs);
@@ -1954,10 +2112,6 @@ static WERROR dcesrv_netr_DsRGetForestTrustInformation(struct dcesrv_call_state
        struct ldb_context *sam_ctx;
        WERROR werr;
 
-       if (lp_server_role(lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
-               return WERR_CALL_NOT_IMPLEMENTED;
-       }
-
        if (r->in.flags & 0xFFFFFFFE) {
                return WERR_INVALID_FLAGS;
        }
@@ -2017,10 +2171,6 @@ static NTSTATUS dcesrv_netr_GetForestTrustInformation(struct dcesrv_call_state *
        NTSTATUS status;
        WERROR werr;
 
-       if (lp_server_role(lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
-               return NT_STATUS_NOT_IMPLEMENTED;
-       }
-
        status = dcesrv_netr_creds_server_step_check(dce_call,
                                                     mem_ctx,
                                                     r->in.computer_name,