LCOV - code coverage report
Current view: top level - source4/libcli/smb_composite - connect_nego.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 91 97 93.8 %
Date: 2024-04-21 15:09:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Copyright (C) Stefan Metzmacher 2018
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "lib/util/tevent_ntstatus.h"
      22             : #include "libcli/composite/composite.h"
      23             : #include "libcli/raw/libcliraw.h"
      24             : #include "libcli/raw/raw_proto.h"
      25             : #include "libcli/smb_composite/smb_composite.h"
      26             : #include "lib/socket/socket.h"
      27             : #include "libcli/resolve/resolve.h"
      28             : #include "librpc/gen_ndr/ndr_nbt.h"
      29             : #include "libcli/smb/smbXcli_base.h"
      30             : 
      31             : struct smb_connect_nego_state {
      32             :         struct tevent_context *ev;
      33             :         struct resolve_context *resolve_ctx;
      34             :         const char *socket_options;
      35             :         struct smbcli_options options;
      36             :         const char *dest_hostname;
      37             :         const char *dest_address;
      38             :         const char **dest_ports;
      39             :         const char *target_hostname;
      40             :         struct nbt_name calling, called;
      41             :         struct smbXcli_conn *conn;
      42             : };
      43             : 
      44             : static void smb_connect_nego_connect_done(struct composite_context *creq);
      45             : static void smb_connect_nego_nego_done(struct tevent_req *subreq);
      46             : 
      47        6677 : struct tevent_req *smb_connect_nego_send(TALLOC_CTX *mem_ctx,
      48             :                                          struct tevent_context *ev,
      49             :                                          struct resolve_context *resolve_ctx,
      50             :                                          const struct smbcli_options *options,
      51             :                                          const char *socket_options,
      52             :                                          const char *dest_hostname,
      53             :                                          const char *dest_address, /* optional */
      54             :                                          const char **dest_ports,
      55             :                                          const char *target_hostname,
      56             :                                          const char *called_name,
      57             :                                          const char *calling_name)
      58             : {
      59        6677 :         struct tevent_req *req = NULL;
      60        6677 :         struct smb_connect_nego_state *state = NULL;
      61        6677 :         struct composite_context *creq = NULL;
      62             : 
      63        6677 :         req = tevent_req_create(mem_ctx, &state,
      64             :                                 struct smb_connect_nego_state);
      65        6677 :         if (req == NULL) {
      66           0 :                 return NULL;
      67             :         }
      68        6677 :         state->ev = ev;
      69        6677 :         state->resolve_ctx= resolve_ctx;
      70        6677 :         state->options = *options;
      71        6677 :         state->socket_options = socket_options;
      72        6677 :         state->dest_hostname = dest_hostname;
      73        6677 :         state->dest_address = dest_address;
      74        6677 :         state->dest_ports = dest_ports;
      75        6677 :         state->target_hostname = target_hostname;
      76             : 
      77        6677 :         make_nbt_name_client(&state->calling, calling_name);
      78             : 
      79        6677 :         nbt_choose_called_name(state, &state->called,
      80             :                                called_name, NBT_NAME_SERVER);
      81        6677 :         if (tevent_req_nomem(state->called.name, req)) {
      82           0 :                 return tevent_req_post(req, ev);
      83             :         }
      84             : 
      85        7067 :         creq = smbcli_sock_connect_send(state,
      86        6287 :                                         state->dest_address,
      87        6287 :                                         state->dest_ports,
      88        6287 :                                         state->dest_hostname,
      89        6287 :                                         state->resolve_ctx,
      90        6287 :                                         state->ev,
      91        6287 :                                         state->socket_options,
      92        6287 :                                         &state->calling,
      93        6677 :                                         &state->called);
      94        6677 :         if (tevent_req_nomem(creq, req)) {
      95           0 :                 return tevent_req_post(req, ev);
      96             :         }
      97        6677 :         creq->async.private_data = req;
      98        6677 :         creq->async.fn = smb_connect_nego_connect_done;
      99             : 
     100        6677 :         return req;
     101             : }
     102             : 
     103        6677 : static void smb_connect_nego_connect_done(struct composite_context *creq)
     104             : {
     105         390 :         struct tevent_req *req =
     106        6677 :                 talloc_get_type_abort(creq->async.private_data,
     107             :                 struct tevent_req);
     108         390 :         struct smb_connect_nego_state *state =
     109        6677 :                 tevent_req_data(req,
     110             :                 struct smb_connect_nego_state);
     111        6677 :         struct tevent_req *subreq = NULL;
     112        6677 :         struct smbcli_socket *sock = NULL;
     113         390 :         uint32_t smb1_capabilities;
     114        6677 :         uint32_t timeout_msec = state->options.request_timeout * 1000;
     115         390 :         NTSTATUS status;
     116             : 
     117        6677 :         status = smbcli_sock_connect_recv(creq, state, &sock);
     118        6677 :         creq = NULL;
     119        6677 :         if (tevent_req_nterror(req, status)) {
     120           1 :                 return;
     121             :         }
     122             : 
     123        6676 :         TALLOC_FREE(sock->event.fde);
     124        6676 :         TALLOC_FREE(sock->event.te);
     125             : 
     126        6676 :         smb1_capabilities = 0;
     127        6676 :         smb1_capabilities |= CAP_LARGE_FILES;
     128        6676 :         smb1_capabilities |= CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
     129        6676 :         smb1_capabilities |= CAP_LOCK_AND_READ | CAP_NT_FIND;
     130        6676 :         smb1_capabilities |= CAP_DFS | CAP_W2K_SMBS;
     131        6676 :         smb1_capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX;
     132        6676 :         smb1_capabilities |= CAP_LWIO;
     133             : 
     134        6676 :         if (state->options.ntstatus_support) {
     135        6676 :                 smb1_capabilities |= CAP_STATUS32;
     136             :         }
     137             : 
     138        6676 :         if (state->options.unicode) {
     139        6676 :                 smb1_capabilities |= CAP_UNICODE;
     140             :         }
     141             : 
     142        6676 :         if (state->options.use_spnego) {
     143        6528 :                 smb1_capabilities |= CAP_EXTENDED_SECURITY;
     144             :         }
     145             : 
     146        6676 :         if (state->options.use_level2_oplocks) {
     147        6676 :                 smb1_capabilities |= CAP_LEVEL_II_OPLOCKS;
     148             :         }
     149             : 
     150       13352 :         state->conn = smbXcli_conn_create(state,
     151        6676 :                                           sock->sock->fd,
     152             :                                           state->target_hostname,
     153             :                                           state->options.signing,
     154             :                                           smb1_capabilities,
     155             :                                           &state->options.client_guid,
     156             :                                           state->options.smb2_capabilities,
     157        6676 :                                           &state->options.smb3_capabilities);
     158        6676 :         if (tevent_req_nomem(state->conn, req)) {
     159           0 :                 return;
     160             :         }
     161        6676 :         sock->sock->fd = -1;
     162        6676 :         TALLOC_FREE(sock);
     163             : 
     164        7066 :         subreq = smbXcli_negprot_send(state,
     165             :                                       state->ev,
     166             :                                       state->conn,
     167             :                                       timeout_msec,
     168        6676 :                                       state->options.min_protocol,
     169        6676 :                                       state->options.max_protocol,
     170        6676 :                                       state->options.max_credits,
     171             :                                       NULL);
     172        6676 :         if (tevent_req_nomem(subreq, req)) {
     173           0 :                 return;
     174             :         }
     175        6676 :         tevent_req_set_callback(subreq, smb_connect_nego_nego_done, req);
     176             : }
     177             : 
     178        6676 : static void smb_connect_nego_nego_done(struct tevent_req *subreq)
     179             : {
     180         390 :         struct tevent_req *req =
     181        6676 :                 tevent_req_callback_data(subreq,
     182             :                 struct tevent_req);
     183         390 :         NTSTATUS status;
     184             : 
     185        6676 :         status = smbXcli_negprot_recv(subreq, NULL, NULL);
     186        6676 :         TALLOC_FREE(subreq);
     187        6676 :         if (tevent_req_nterror(req, status)) {
     188           0 :                 return;
     189             :         }
     190             : 
     191        6676 :         tevent_req_done(req);
     192             : }
     193             : 
     194        6677 : NTSTATUS smb_connect_nego_recv(struct tevent_req *req,
     195             :                                TALLOC_CTX *mem_ctx,
     196             :                                struct smbXcli_conn **_conn)
     197             : {
     198         390 :         struct smb_connect_nego_state *state =
     199        6677 :                 tevent_req_data(req,
     200             :                 struct smb_connect_nego_state);
     201         390 :         NTSTATUS status;
     202             : 
     203        6677 :         if (tevent_req_is_nterror(req, &status)) {
     204           1 :                 tevent_req_received(req);
     205           1 :                 return status;
     206             :         }
     207             : 
     208        6676 :         *_conn = talloc_move(mem_ctx, &state->conn);
     209        6676 :         tevent_req_received(req);
     210        6676 :         return NT_STATUS_OK;
     211             : }

Generated by: LCOV version 1.14