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