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 98b443d9 Lines: 91 97 93.8 %
Date: 2024-05-31 13:13:24 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        6431 : 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        6431 :         struct tevent_req *req = NULL;
      60        6431 :         struct smb_connect_nego_state *state = NULL;
      61        6431 :         struct composite_context *creq = NULL;
      62             : 
      63        6431 :         req = tevent_req_create(mem_ctx, &state,
      64             :                                 struct smb_connect_nego_state);
      65        6431 :         if (req == NULL) {
      66           0 :                 return NULL;
      67             :         }
      68        6431 :         state->ev = ev;
      69        6431 :         state->resolve_ctx= resolve_ctx;
      70        6431 :         state->options = *options;
      71        6431 :         state->socket_options = socket_options;
      72        6431 :         state->dest_hostname = dest_hostname;
      73        6431 :         state->dest_address = dest_address;
      74        6431 :         state->dest_ports = dest_ports;
      75        6431 :         state->target_hostname = target_hostname;
      76             : 
      77        6431 :         make_nbt_name_client(&state->calling, calling_name);
      78             : 
      79        6431 :         nbt_choose_called_name(state, &state->called,
      80             :                                called_name, NBT_NAME_SERVER);
      81        6431 :         if (tevent_req_nomem(state->called.name, req)) {
      82           0 :                 return tevent_req_post(req, ev);
      83             :         }
      84             : 
      85        6821 :         creq = smbcli_sock_connect_send(state,
      86        6041 :                                         state->dest_address,
      87        6041 :                                         state->dest_ports,
      88        6041 :                                         state->dest_hostname,
      89        6041 :                                         state->resolve_ctx,
      90        6041 :                                         state->ev,
      91        6041 :                                         state->socket_options,
      92        6041 :                                         &state->calling,
      93        6431 :                                         &state->called);
      94        6431 :         if (tevent_req_nomem(creq, req)) {
      95           0 :                 return tevent_req_post(req, ev);
      96             :         }
      97        6431 :         creq->async.private_data = req;
      98        6431 :         creq->async.fn = smb_connect_nego_connect_done;
      99             : 
     100        6431 :         return req;
     101             : }
     102             : 
     103        6431 : static void smb_connect_nego_connect_done(struct composite_context *creq)
     104             : {
     105         390 :         struct tevent_req *req =
     106        6431 :                 talloc_get_type_abort(creq->async.private_data,
     107             :                 struct tevent_req);
     108         390 :         struct smb_connect_nego_state *state =
     109        6431 :                 tevent_req_data(req,
     110             :                 struct smb_connect_nego_state);
     111        6431 :         struct tevent_req *subreq = NULL;
     112        6431 :         struct smbcli_socket *sock = NULL;
     113         390 :         uint32_t smb1_capabilities;
     114        6431 :         uint32_t timeout_msec = state->options.request_timeout * 1000;
     115         390 :         NTSTATUS status;
     116             : 
     117        6431 :         status = smbcli_sock_connect_recv(creq, state, &sock);
     118        6431 :         creq = NULL;
     119        6431 :         if (tevent_req_nterror(req, status)) {
     120           1 :                 return;
     121             :         }
     122             : 
     123        6430 :         TALLOC_FREE(sock->event.fde);
     124        6430 :         TALLOC_FREE(sock->event.te);
     125             : 
     126        6430 :         smb1_capabilities = 0;
     127        6430 :         smb1_capabilities |= CAP_LARGE_FILES;
     128        6430 :         smb1_capabilities |= CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
     129        6430 :         smb1_capabilities |= CAP_LOCK_AND_READ | CAP_NT_FIND;
     130        6430 :         smb1_capabilities |= CAP_DFS | CAP_W2K_SMBS;
     131        6430 :         smb1_capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX;
     132        6430 :         smb1_capabilities |= CAP_LWIO;
     133             : 
     134        6430 :         if (state->options.ntstatus_support) {
     135        6430 :                 smb1_capabilities |= CAP_STATUS32;
     136             :         }
     137             : 
     138        6430 :         if (state->options.unicode) {
     139        6430 :                 smb1_capabilities |= CAP_UNICODE;
     140             :         }
     141             : 
     142        6430 :         if (state->options.use_spnego) {
     143        6282 :                 smb1_capabilities |= CAP_EXTENDED_SECURITY;
     144             :         }
     145             : 
     146        6430 :         if (state->options.use_level2_oplocks) {
     147        6430 :                 smb1_capabilities |= CAP_LEVEL_II_OPLOCKS;
     148             :         }
     149             : 
     150       12860 :         state->conn = smbXcli_conn_create(state,
     151        6430 :                                           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        6430 :                                           &state->options.smb3_capabilities);
     158        6430 :         if (tevent_req_nomem(state->conn, req)) {
     159           0 :                 return;
     160             :         }
     161        6430 :         sock->sock->fd = -1;
     162        6430 :         TALLOC_FREE(sock);
     163             : 
     164        6820 :         subreq = smbXcli_negprot_send(state,
     165             :                                       state->ev,
     166             :                                       state->conn,
     167             :                                       timeout_msec,
     168        6430 :                                       state->options.min_protocol,
     169        6430 :                                       state->options.max_protocol,
     170        6430 :                                       state->options.max_credits,
     171             :                                       NULL);
     172        6430 :         if (tevent_req_nomem(subreq, req)) {
     173           0 :                 return;
     174             :         }
     175        6430 :         tevent_req_set_callback(subreq, smb_connect_nego_nego_done, req);
     176             : }
     177             : 
     178        6430 : static void smb_connect_nego_nego_done(struct tevent_req *subreq)
     179             : {
     180         390 :         struct tevent_req *req =
     181        6430 :                 tevent_req_callback_data(subreq,
     182             :                 struct tevent_req);
     183         390 :         NTSTATUS status;
     184             : 
     185        6430 :         status = smbXcli_negprot_recv(subreq, NULL, NULL);
     186        6430 :         TALLOC_FREE(subreq);
     187        6430 :         if (tevent_req_nterror(req, status)) {
     188           0 :                 return;
     189             :         }
     190             : 
     191        6430 :         tevent_req_done(req);
     192             : }
     193             : 
     194        6431 : 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        6431 :                 tevent_req_data(req,
     200             :                 struct smb_connect_nego_state);
     201         390 :         NTSTATUS status;
     202             : 
     203        6431 :         if (tevent_req_is_nterror(req, &status)) {
     204           1 :                 tevent_req_received(req);
     205           1 :                 return status;
     206             :         }
     207             : 
     208        6430 :         *_conn = talloc_move(mem_ctx, &state->conn);
     209        6430 :         tevent_req_received(req);
     210        6430 :         return NT_STATUS_OK;
     211             : }

Generated by: LCOV version 1.14