nbt_server: Factor out dgram sending from reply construction
authorVolker Lendecke <vl@samba.org>
Sat, 17 Feb 2018 16:05:09 +0000 (17:05 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 27 Feb 2018 08:14:17 +0000 (09:14 +0100)
Separation of concerns. Only one call to dgram_mailslot_netlogon_reply, which
does the UDP send.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
source4/nbt_server/dgram/netlogon.c

index 0954716c663a5e4f6625f0cd5f377d3009c03c3b..24265d9a4ddbd8a92ad0bbca94bd14c40ec94641 100644 (file)
 /*
   reply to a GETDC request
  */
-static void nbtd_netlogon_getdc(struct nbtd_interface *iface,
-                               struct nbt_dgram_packet *packet, 
-                               const struct socket_address *src,
-                               struct nbt_netlogon_packet *netlogon)
+static NTSTATUS nbtd_netlogon_getdc(struct nbtd_interface *iface,
+                                   struct nbt_dgram_packet *packet,
+                                   const struct socket_address *src,
+                                   struct nbt_netlogon_packet *netlogon,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct nbt_netlogon_response **presponse)
 {
        struct nbt_name *name = &packet->data.msg.dest_name;
-       struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
        struct nbt_netlogon_response_from_pdc *pdc;
        struct ldb_context *samctx;
-       struct nbt_netlogon_response netlogon_response;
+       struct nbt_netlogon_response *response;
 
        /* only answer getdc requests on the PDC or LOGON names */
        if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
-               return;
+               return NT_STATUS_NOT_SUPPORTED;
        }
 
        samctx = iface->nbtsrv->sam_ctx;
@@ -57,59 +58,75 @@ static void nbtd_netlogon_getdc(struct nbtd_interface *iface,
        if (lpcfg_server_role(iface->nbtsrv->task->lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC
            || !samdb_is_pdc(samctx)) {
                DEBUG(2, ("Not a PDC, so not processing LOGON_PRIMARY_QUERY\n"));
-               return;         
+               return NT_STATUS_NOT_SUPPORTED;
        }
 
        if (strcasecmp_m(name->name, lpcfg_workgroup(iface->nbtsrv->task->lp_ctx)) != 0) {
                DEBUG(5,("GetDC requested for a domian %s that we don't host\n", name->name));
-               return;
+               return NT_STATUS_NOT_SUPPORTED;
        }
 
        /* setup a GETDC reply */
-       ZERO_STRUCT(netlogon_response);
-       netlogon_response.response_type = NETLOGON_GET_PDC;
-       pdc = &netlogon_response.data.get_pdc;
+       response = talloc_zero(mem_ctx, struct nbt_netlogon_response);
+       if (response == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       response->response_type = NETLOGON_GET_PDC;
+       pdc = &response->data.get_pdc;
 
        pdc->command = NETLOGON_RESPONSE_FROM_PDC;
-       pdc->pdc_name         = lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx);
+
+       pdc->pdc_name = talloc_strdup(
+               response, lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx));
+       if (pdc->pdc_name == NULL) {
+               TALLOC_FREE(response);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        pdc->unicode_pdc_name = pdc->pdc_name;
-       pdc->domain_name      = lpcfg_workgroup(iface->nbtsrv->task->lp_ctx);
+
+       pdc->domain_name = talloc_strdup(
+               response, lpcfg_workgroup(iface->nbtsrv->task->lp_ctx));
+       if (pdc->domain_name == NULL) {
+               TALLOC_FREE(response);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        pdc->nt_version       = 1;
        pdc->lmnt_token       = 0xFFFF;
        pdc->lm20_token       = 0xFFFF;
 
-       dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
-                                     packet, 
-                                     lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx),
-                                     netlogon->req.pdc.mailslot_name,
-                                     &netlogon_response);
+       *presponse = response;
+       return NT_STATUS_OK;
 }
 
-
 /*
   reply to a ADS style GETDC request
  */
-static void nbtd_netlogon_samlogon(struct nbtd_interface *iface,
-                                  struct nbt_dgram_packet *packet, 
-                                  const struct socket_address *src,
-                                  struct nbt_netlogon_packet *netlogon)
+static NTSTATUS nbtd_netlogon_samlogon(
+       struct nbtd_interface *iface,
+       struct nbt_dgram_packet *packet,
+       const struct socket_address *src,
+       struct nbt_netlogon_packet *netlogon,
+       TALLOC_CTX *mem_ctx,
+       struct nbt_netlogon_response **presponse)
 {
        struct nbt_name *name = &packet->data.msg.dest_name;
-       struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
        struct ldb_context *samctx;
+       struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, false);
        const char *my_ip = reply_iface->ip_address; 
        struct dom_sid *sid;
-       struct nbt_netlogon_response netlogon_response;
+       struct nbt_netlogon_response *response;
        NTSTATUS status;
 
        if (!my_ip) {
                DEBUG(0, ("Could not obtain own IP address for datagram socket\n"));
-               return;
+               return NT_STATUS_NOT_SUPPORTED;
        }
 
        /* only answer getdc requests on the PDC or LOGON names */
        if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
-               return;
+               return NT_STATUS_NOT_SUPPORTED;
        }
 
        samctx = iface->nbtsrv->sam_ctx;
@@ -120,25 +137,29 @@ static void nbtd_netlogon_samlogon(struct nbtd_interface *iface,
                sid = NULL;
        }
 
-       status = fill_netlogon_samlogon_response(samctx, packet, NULL, name->name, sid, NULL, 
-                                                netlogon->req.logon.user_name, netlogon->req.logon.acct_control, src->addr, 
-                                                netlogon->req.logon.nt_version, iface->nbtsrv->task->lp_ctx, &netlogon_response.data.samlogon, false);
+       response = talloc_zero(mem_ctx, struct nbt_netlogon_response);
+       if (response == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       response->response_type = NETLOGON_SAMLOGON;
+
+       status = fill_netlogon_samlogon_response(
+               samctx, response, NULL, name->name, sid, NULL,
+               netlogon->req.logon.user_name,
+               netlogon->req.logon.acct_control, src->addr,
+               netlogon->req.logon.nt_version, iface->nbtsrv->task->lp_ctx,
+               &response->data.samlogon, false);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(2,("NBT netlogon query failed domain=%s sid=%s version=%d - %s\n",
                         name->name, dom_sid_string(packet, sid), netlogon->req.logon.nt_version, nt_errstr(status)));
-               return;
+               TALLOC_FREE(response);
+               return status;
        }
 
-       netlogon_response.response_type = NETLOGON_SAMLOGON;
-
-       dgram_mailslot_netlogon_reply(reply_iface->dgmsock, 
-                                     packet, 
-                                     lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx),
-                                     netlogon->req.logon.mailslot_name,
-                                     &netlogon_response);
+       *presponse = response;
+       return NT_STATUS_OK;
 }
 
-
 /*
   handle incoming netlogon mailslot requests
 */
@@ -151,8 +172,11 @@ void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
                talloc_get_type(dgmslot->private_data, struct nbtd_interface);
        struct nbt_netlogon_packet *netlogon = 
                talloc(dgmslot, struct nbt_netlogon_packet);
+       struct nbtd_interface *reply_iface = nbtd_find_reply_iface(
+               iface, src->addr, false);
        struct nbtd_iface_name *iname;
        struct nbt_name *name = &packet->data.msg.dest_name;
+       struct nbt_netlogon_response *response;
 
        if (netlogon == NULL) goto failed;
 
@@ -173,18 +197,28 @@ void nbtd_mailslot_netlogon_handler(struct dgram_mailslot_handler *dgmslot,
 
        switch (netlogon->command) {
        case LOGON_PRIMARY_QUERY:
-               nbtd_netlogon_getdc(iface, packet, src, netlogon);
+               status = nbtd_netlogon_getdc(iface, packet, src, netlogon,
+                                            netlogon, &response);
                break;
        case LOGON_SAM_LOGON_REQUEST:
-               nbtd_netlogon_samlogon(iface, packet, src, netlogon);
+               status = nbtd_netlogon_samlogon(iface, packet, src, netlogon,
+                                               netlogon, &response);
                break;
        default:
                DEBUG(2,("unknown netlogon op %d from %s:%d\n", 
                         netlogon->command, src->addr, src->port));
                NDR_PRINT_DEBUG(nbt_netlogon_packet, netlogon);
+               status = NT_STATUS_NOT_SUPPORTED;
                break;
        }
 
+       if (NT_STATUS_IS_OK(status)) {
+               dgram_mailslot_netlogon_reply(
+                       reply_iface->dgmsock, packet,
+                       lpcfg_netbios_name(iface->nbtsrv->task->lp_ctx),
+                       netlogon->req.logon.mailslot_name, response);
+       }
+
        talloc_free(netlogon);
        return;