LCOV - code coverage report
Current view: top level - source4/libcli/resolve - lmhosts.c (source / functions) Hit Total Coverage
Test: coverage report for master 70ed9daf Lines: 23 42 54.8 %
Date: 2024-01-11 09:59:51 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    lmhosts name resolution module
       5             : 
       6             :    Copyright (C) Andrew Tridgell 1994-1998,2005
       7             :    Copyright (C) Jeremy Allison 2007
       8             :    Copyright (C) Jelmer Vernooij 2007
       9             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009-2014
      10             :    This program is free software; you can redistribute it and/or modify
      11             :    it under the terms of the GNU General Public License as published by
      12             :    the Free Software Foundation; either version 3 of the License, or
      13             :    (at your option) any later version.
      14             : 
      15             :    This program is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :    GNU General Public License for more details.
      19             : 
      20             :    You should have received a copy of the GNU General Public License
      21             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      22             : */
      23             : 
      24             : #include "includes.h"
      25             : #include "libcli/composite/composite.h"
      26             : #include "libcli/resolve/resolve.h"
      27             : #include "lib/socket/socket.h"
      28             : #include "system/network.h"
      29             : #include "lib/socket/netif.h"
      30             : #include "param/param.h"
      31             : #include "lib/util/util_net.h"
      32             : #include "libcli/nbt/libnbt.h"
      33             : #include "dynconfig.h"
      34             : 
      35             : struct resolve_lmhosts_state {
      36             :         struct socket_address **addrs;
      37             :         char **names;
      38             : };
      39             : 
      40             : /**
      41             :   lmhosts name resolution method - async send
      42             :  */
      43             : /*
      44             :   general name resolution - async send
      45             :  */
      46       55129 : static struct composite_context *resolve_name_lmhosts_send(
      47             :         TALLOC_CTX *mem_ctx,
      48             :         struct tevent_context *event_ctx,
      49             :         void *userdata, uint32_t flags,
      50             :         uint16_t port,
      51             :         struct nbt_name *name)
      52             : {
      53        1242 :         struct composite_context *c;
      54        1242 :         struct resolve_lmhosts_state *state;
      55        1242 :         struct sockaddr_storage *resolved_iplist;
      56       55129 :         size_t resolved_count = 0, i;
      57             : 
      58       55129 :         if (event_ctx == NULL) {
      59           0 :                 return NULL;
      60             :         }
      61             : 
      62       55129 :         c = composite_create(mem_ctx, event_ctx);
      63       55129 :         if (c == NULL) return NULL;
      64             : 
      65       55129 :         if (composite_nomem(c->event_ctx, c)) return c;
      66             : 
      67       55129 :         state = talloc_zero(c, struct resolve_lmhosts_state);
      68       55129 :         if (composite_nomem(state, c)) return c;
      69       55129 :         c->private_data = state;
      70             : 
      71       55129 :         c->status = resolve_lmhosts_file_as_sockaddr(state,
      72             :                                                      dyn_LMHOSTSFILE,
      73             :                                                      name->name,
      74       55129 :                                                      name->type,
      75             :                                                      &resolved_iplist,
      76             :                                                      &resolved_count);
      77       55129 :         if (!composite_is_ok(c)) return c;
      78             : 
      79           0 :         for (i=0; i < resolved_count; i += 2) {
      80           0 :                 state->addrs = talloc_realloc(state, state->addrs, struct socket_address *, i+2);
      81           0 :                 if (composite_nomem(state->addrs, c)) return c;
      82             : 
      83           0 :                 set_sockaddr_port((struct sockaddr *)&resolved_iplist[i], port);
      84             : 
      85           0 :                 state->addrs[i] = socket_address_from_sockaddr(state->addrs, (struct sockaddr *)&resolved_iplist[i], sizeof(resolved_iplist[i]));
      86           0 :                 if (composite_nomem(state->addrs[i], c)) return c;
      87             : 
      88           0 :                 state->addrs[i+1] = NULL;
      89             : 
      90             : 
      91           0 :                 state->names = talloc_realloc(state, state->names, char *, i+2);
      92           0 :                 if (composite_nomem(state->addrs, c)) return c;
      93             : 
      94           0 :                 state->names[i] = talloc_strdup(state->names, name->name);
      95           0 :                 if (composite_nomem(state->names[i], c)) return c;
      96             : 
      97           0 :                 state->names[i+1] = NULL;
      98             : 
      99             :         }
     100             : 
     101           0 :         composite_done(c);
     102           0 :         return c;
     103             : }
     104             : 
     105             : /*
     106             :   general name resolution method - recv side
     107             :  */
     108       55129 : static NTSTATUS resolve_name_lmhosts_recv(struct composite_context *c,
     109             :                                           TALLOC_CTX *mem_ctx,
     110             :                                           struct socket_address ***addrs,
     111             :                                           char ***names)
     112             : {
     113        1242 :         NTSTATUS status;
     114             : 
     115       55129 :         status = composite_wait(c);
     116             : 
     117       55129 :         if (NT_STATUS_IS_OK(status)) {
     118           0 :                 struct resolve_lmhosts_state *state = talloc_get_type(c->private_data, struct resolve_lmhosts_state);
     119           0 :                 *addrs = talloc_steal(mem_ctx, state->addrs);
     120           0 :                 if (names) {
     121           0 :                         *names = talloc_steal(mem_ctx, state->names);
     122             :                 }
     123             :         }
     124             : 
     125       55129 :         talloc_free(c);
     126       55129 :         return status;
     127             : }
     128             : 
     129             : 
     130       68176 : bool resolve_context_add_lmhosts_method(struct resolve_context *ctx)
     131             : {
     132       68176 :         return resolve_context_add_method(ctx, resolve_name_lmhosts_send, resolve_name_lmhosts_recv, NULL);
     133             : }

Generated by: LCOV version 1.14