summaryrefslogtreecommitdiff
path: root/source3/libsmb/async_smb.c
blob: 62aaa5646c8ab8d98e3a32df8e364ad25eb70f4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
   Unix SMB/CIFS implementation.
   Infrastructure for async SMB client requests
   Copyright (C) Volker Lendecke 2008

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libsmb/libsmb.h"
#include "../lib/util/tevent_ntstatus.h"
#include "async_smb.h"
#include "../libcli/smb/smbXcli_base.h"

/*
 * Fetch a smb request's mid. Only valid after the request has been sent by
 * cli_smb_req_send().
 */
uint16_t cli_smb_req_mid(struct tevent_req *req)
{
	return smb1cli_req_mid(req);
}

void cli_smb_req_set_mid(struct tevent_req *req, uint16_t mid)
{
	smb1cli_req_set_mid(req, mid);
}

uint32_t cli_smb_req_seqnum(struct tevent_req *req)
{
	return smb1cli_req_seqnum(req);
}

void cli_smb_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
{
	smb1cli_req_set_seqnum(req, seqnum);
}

struct cli_smb_req_state {
	struct cli_state *cli;
	uint8_t smb_command;
	struct tevent_req *req;
	struct cli_smb_req_state **ptr;
};

static int cli_smb_req_state_destructor(struct cli_smb_req_state *state)
{
	talloc_set_destructor(state->ptr, NULL);
	talloc_free(state->ptr);
	return 0;
}

static int cli_smb_req_state_ptr_destructor(struct cli_smb_req_state **ptr)
{
	struct cli_smb_req_state *state = *ptr;
	void *parent = talloc_parent(state);

	talloc_set_destructor(state, NULL);

	talloc_reparent(state, parent, state->req);
	talloc_free(state);
	return 0;
}

struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
				      struct tevent_context *ev,
				      struct cli_state *cli,
				      uint8_t smb_command,
				      uint8_t additional_flags,
				      uint8_t wct, uint16_t *vwv,
				      int iov_count,
				      struct iovec *bytes_iov)
{
	struct cli_smb_req_state *state;
	uint8_t clear_flags = 0;
	uint16_t additional_flags2 = 0;
	uint16_t clear_flags2 = 0;

	state = talloc_zero(mem_ctx, struct cli_smb_req_state);
	if (state == NULL) {
		return NULL;
	}
	state->cli = cli;
	state->smb_command = smb_command;
	state->ptr = talloc(state, struct cli_smb_req_state *);
	if (state->ptr == NULL) {
		talloc_free(state);
		return NULL;
	}
	*state->ptr = state;

	if (cli->case_sensitive) {
		clear_flags |= FLAG_CASELESS_PATHNAMES;
	} else {
		/* Default setting, case insensitive. */
		additional_flags |= FLAG_CASELESS_PATHNAMES;
	}

	if ((smb1cli_conn_capabilities(cli->conn) & CAP_DFS) && cli->dfsroot) {
		additional_flags2 |= FLAGS2_DFS_PATHNAMES;
	}

	state->req = smb1cli_req_create(state, ev, cli->conn, smb_command,
					additional_flags, clear_flags,
					additional_flags2, clear_flags2,
					cli->timeout,
					cli->smb1.pid,
					cli->smb1.tid,
					cli->smb1.uid,
					wct, vwv, iov_count, bytes_iov);
	if (state->req == NULL) {
		talloc_free(state);
		return NULL;
	}

	talloc_reparent(state, state->req, state->ptr);
	talloc_set_destructor(state, cli_smb_req_state_destructor);
	talloc_set_destructor(state->ptr, cli_smb_req_state_ptr_destructor);

	return state->req;
}

NTSTATUS cli_smb_req_send(struct tevent_req *req)
{
	return smb1cli_req_chain_submit(&req, 1);
}

struct tevent_req *cli_smb_send(TALLOC_CTX *mem_ctx,
				struct tevent_context *ev,
				struct cli_state *cli,
				uint8_t smb_command,
				uint8_t additional_flags,
				uint8_t wct, uint16_t *vwv,
				uint32_t num_bytes,
				const uint8_t *bytes)
{
	struct cli_smb_req_state *state;
	uint8_t clear_flags = 0;
	uint16_t additional_flags2 = 0;
	uint16_t clear_flags2 = 0;

	state = talloc_zero(mem_ctx, struct cli_smb_req_state);
	if (state == NULL) {
		return NULL;
	}
	state->cli = cli;
	state->smb_command = smb_command;
	state->ptr = talloc(state, struct cli_smb_req_state *);
	if (state->ptr == NULL) {
		talloc_free(state);
		return NULL;
	}
	*state->ptr = state;

	if (cli->case_sensitive) {
		clear_flags |= FLAG_CASELESS_PATHNAMES;
	} else {
		/* Default setting, case insensitive. */
		additional_flags |= FLAG_CASELESS_PATHNAMES;
	}

	if ((smb1cli_conn_capabilities(cli->conn) & CAP_DFS) && cli->dfsroot) {
		additional_flags2 |= FLAGS2_DFS_PATHNAMES;
	}

	state->req = smb1cli_req_send(state, ev, cli->conn, smb_command,
				additional_flags, clear_flags,
				additional_flags2, clear_flags2,
				cli->timeout,
				cli->smb1.pid,
				cli->smb1.tid,
				cli->smb1.uid,
				wct, vwv, num_bytes, bytes);
	if (state->req == NULL) {
		talloc_free(state);
		return NULL;
	}

	talloc_reparent(state, state->req, state->ptr);
	talloc_set_destructor(state, cli_smb_req_state_destructor);
	talloc_set_destructor(state->ptr, cli_smb_req_state_ptr_destructor);

	return state->req;
}

NTSTATUS cli_smb_recv(struct tevent_req *req,
		      TALLOC_CTX *mem_ctx, uint8_t **pinbuf,
		      uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
		      uint32_t *pnum_bytes, uint8_t **pbytes)
{
	NTSTATUS status;
	void *parent = talloc_parent(req);
	struct cli_smb_req_state *state =
		talloc_get_type(parent,
		struct cli_smb_req_state);
	struct iovec *recv_iov = NULL;
	uint8_t wct = 0;
	uint16_t *vwv = NULL;
	uint32_t num_bytes;
	uint8_t *bytes = NULL;
	uint8_t *inbuf;
	bool is_expected = false;
	bool map_dos_errors = true;

	if (pinbuf != NULL) {
		*pinbuf = NULL;
	}
	if (pwct != NULL) {
		*pwct = 0;
	}
	if (pvwv != NULL) {
		*pvwv = NULL;
	}
	if (pnum_bytes != NULL) {
		*pnum_bytes = 0;
	}
	if (pbytes != NULL) {
		*pbytes = NULL;
	}

	status = smb1cli_req_recv(req, req,
				  &recv_iov,
				  NULL, /* phdr */
				  &wct,
				  &vwv,
				  NULL, /* pvwv_offset */
				  &num_bytes,
				  &bytes,
				  NULL, /* pbytes_offset */
				  &inbuf,
				  NULL, 0); /* expected */

	if (state) {
		if ((state->smb_command == SMBsesssetupX) &&
		     NT_STATUS_EQUAL(status,
				NT_STATUS_MORE_PROCESSING_REQUIRED)) {
			/*
			 * NT_STATUS_MORE_PROCESSING_REQUIRED is a
			 * valid return code for session setup
			 */
			is_expected = true;
		}

		map_dos_errors = state->cli->map_dos_errors;
		state->cli->raw_status = status;
		talloc_free(state->ptr);
		state = NULL;
	}

	if (NT_STATUS_IS_DOS(status) && map_dos_errors) {
		uint8_t eclass = NT_STATUS_DOS_CLASS(status);
		uint16_t ecode = NT_STATUS_DOS_CODE(status);
		/*
		 * TODO: is it really a good idea to do a mapping here?
		 *
		 * The old cli_pull_error() also does it, so I do not change
		 * the behavior yet.
		 */
		status = dos_to_ntstatus(eclass, ecode);
	}

	if (!NT_STATUS_IS_ERR(status)) {
		is_expected = true;
	}

	if (!is_expected) {
		TALLOC_FREE(recv_iov);
		return status;
	}

	if (wct < min_wct) {
		TALLOC_FREE(recv_iov);
		return NT_STATUS_INVALID_NETWORK_RESPONSE;
	}

	if (pwct != NULL) {
		*pwct = wct;
	}
	if (pvwv != NULL) {
		*pvwv = vwv;
	}
	if (pnum_bytes != NULL) {
		*pnum_bytes = num_bytes;
	}
	if (pbytes != NULL) {
		*pbytes = bytes;
	}

	if (pinbuf != NULL && mem_ctx != NULL) {
		if (talloc_reference_count(inbuf) == 0) {
			*pinbuf = talloc_move(mem_ctx, &inbuf);
			TALLOC_FREE(recv_iov);
		} else {
			*pinbuf = inbuf;
		}
	}

	return status;
}

size_t cli_smb_wct_ofs(struct tevent_req **reqs, int num_reqs)
{
	return smb1cli_req_wct_ofs(reqs, num_reqs);
}

NTSTATUS cli_smb_chain_send(struct tevent_req **reqs, int num_reqs)
{
	return smb1cli_req_chain_submit(reqs, num_reqs);
}