LCOV - code coverage report
Current view: top level - source4/nbt_server/dgram - request.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 34 53 64.2 %
Date: 2021-09-23 10:06:22 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    NBT datagram server
       5             : 
       6             :    Copyright (C) Andrew Tridgell        2005
       7             :    
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "nbt_server/nbt_server.h"
      24             : #include "samba/service_task.h"
      25             : #include "lib/socket/socket.h"
      26             : #include "libcli/resolve/resolve.h"
      27             : #include "nbt_server/dgram/proto.h"
      28             : #include "librpc/gen_ndr/ndr_nbt.h"
      29             : #include "param/param.h"
      30             : 
      31             : /*
      32             :   a list of mailslots that we have static handlers for
      33             : */
      34             : static const struct {
      35             :         const char *mailslot_name;
      36             :         dgram_mailslot_handler_t handler;
      37             : } mailslot_handlers[] = {
      38             :         /* Handle both NTLOGON and NETLOGON in the same function, as
      39             :          * they are very similar */
      40             :         { NBT_MAILSLOT_NETLOGON, nbtd_mailslot_netlogon_handler },
      41             :         { NBT_MAILSLOT_NTLOGON,  nbtd_mailslot_netlogon_handler },
      42             :         { NBT_MAILSLOT_BROWSE,   nbtd_mailslot_browse_handler }
      43             : };
      44             : 
      45             : /*
      46             :   receive an incoming dgram request. This is used for general datagram
      47             :   requests. Mailslot requests for our listening mailslots
      48             :   are handled in the specific mailslot handlers
      49             : */
      50           0 : void dgram_request_handler(struct nbt_dgram_socket *dgmsock, 
      51             :                            struct nbt_dgram_packet *packet,
      52             :                            struct socket_address *src)
      53             : {
      54           0 :         DEBUG(0,("General datagram request from %s:%d\n", src->addr, src->port));
      55           0 :         NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
      56           0 : }
      57             : 
      58             : 
      59             : /*
      60             :   setup the port 138 datagram listener for a given interface
      61             : */
      62         126 : NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
      63             : {
      64         126 :         struct nbt_dgram_socket *bcast_dgmsock = NULL;
      65         126 :         struct nbtd_server *nbtsrv = iface->nbtsrv;
      66             :         struct socket_address *bcast_addr, *bind_addr;
      67             :         NTSTATUS status;
      68         126 :         TALLOC_CTX *tmp_ctx = talloc_new(iface);
      69             :         /* the list of mailslots that we are interested in */
      70             :         size_t i;
      71             : 
      72         126 :         if (!tmp_ctx) {
      73           0 :                 return NT_STATUS_NO_MEMORY;
      74             :         }
      75             : 
      76         126 :         if (strcmp("0.0.0.0", iface->netmask) != 0) {
      77             :                 /* listen for broadcasts on port 138 */
      78          63 :                 bcast_dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
      79          63 :                 if (!bcast_dgmsock) {
      80           0 :                         talloc_free(tmp_ctx);
      81           0 :                         return NT_STATUS_NO_MEMORY;
      82             :                 }
      83             :         
      84          63 :                 bcast_addr = socket_address_from_strings(tmp_ctx, bcast_dgmsock->sock->backend_name, 
      85             :                                                          iface->bcast_address, 
      86          63 :                                                          lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx));
      87          63 :                 if (!bcast_addr) {
      88           0 :                         talloc_free(tmp_ctx);
      89           0 :                         return NT_STATUS_NO_MEMORY;
      90             :                 }
      91             : 
      92          63 :                 status = socket_listen(bcast_dgmsock->sock, bcast_addr, 0, 0);
      93          63 :                 if (!NT_STATUS_IS_OK(status)) {
      94           0 :                         talloc_free(tmp_ctx);
      95           0 :                         DEBUG(0,("Failed to bind to %s:%d - %s\n", 
      96             :                                  iface->bcast_address, lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx),
      97             :                                  nt_errstr(status)));
      98           0 :                         return status;
      99             :                 }
     100             :         
     101          63 :                 dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
     102             :         }
     103             : 
     104             :         /* listen for unicasts on port 138 */
     105         126 :         iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
     106         126 :         if (!iface->dgmsock) {
     107           0 :                 talloc_free(tmp_ctx);
     108           0 :                 return NT_STATUS_NO_MEMORY;
     109             :         }
     110             : 
     111         126 :         bind_addr = socket_address_from_strings(tmp_ctx, iface->dgmsock->sock->backend_name, 
     112         126 :                                                 bind_address, lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx));
     113         126 :         if (!bind_addr) {
     114           0 :                 talloc_free(tmp_ctx);
     115           0 :                 return NT_STATUS_NO_MEMORY;
     116             :         }
     117             : 
     118         126 :         status = socket_listen(iface->dgmsock->sock, bind_addr, 0, 0);
     119         126 :         if (!NT_STATUS_IS_OK(status)) {
     120           0 :                 talloc_free(tmp_ctx);
     121           0 :                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
     122             :                          bind_address, lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx), nt_errstr(status)));
     123           0 :                 return status;
     124             :         }
     125             : 
     126         126 :         dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
     127             : 
     128         126 :         talloc_free(tmp_ctx);
     129             : 
     130         504 :         for (i=0;i<ARRAY_SIZE(mailslot_handlers);i++) {
     131             :                 /* note that we don't need to keep the pointer
     132             :                    to the dgmslot around - the callback is all
     133             :                    we need */
     134             :                 struct dgram_mailslot_handler *dgmslot;
     135             : 
     136         378 :                 if (bcast_dgmsock) {
     137         189 :                         dgmslot = dgram_mailslot_listen(bcast_dgmsock, 
     138          57 :                                                 mailslot_handlers[i].mailslot_name,
     139          57 :                                                 mailslot_handlers[i].handler, iface);
     140         189 :                         NT_STATUS_HAVE_NO_MEMORY(dgmslot);
     141             :                 }
     142             : 
     143         378 :                 dgmslot = dgram_mailslot_listen(iface->dgmsock, 
     144         114 :                                                 mailslot_handlers[i].mailslot_name,
     145         114 :                                                 mailslot_handlers[i].handler, iface);
     146         378 :                 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
     147             :         }
     148             : 
     149         126 :         return NT_STATUS_OK;
     150             : }

Generated by: LCOV version 1.13