summaryrefslogtreecommitdiff
path: root/source3/torture/test_chain3.c
blob: d957e5145d40699e79e3c9a74da971d8154cb5e6 (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
/*
   Unix SMB/CIFS implementation.
   Test smbd chain routines

   Copyright (C) Volker Lendecke 2012

   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 "torture/proto.h"
#include "libsmb/libsmb.h"
#include "system/filesys.h"
#include "async_smb.h"
#include "lib/util/tevent_ntstatus.h"
#include "libcli/security/security.h"
#include "libcli/smb/smbXcli_base.h"

struct chain3_andx_state {
	uint16_t fnum;
	size_t written;
	char str[6];
};

static void chain3_andx_open_done(struct tevent_req *subreq);
static void chain3_andx_write_done(struct tevent_req *subreq);
static void chain3_andx_close_done(struct tevent_req *subreq);

static struct tevent_req *chain3_andx_send(TALLOC_CTX *mem_ctx,
					   struct tevent_context *ev,
					   struct cli_state *cli,
					   const char *fname)
{
	struct tevent_req *req, *subreq;
	struct tevent_req *smbreqs[3];
	struct chain3_andx_state *state;
	NTSTATUS status;

	req = tevent_req_create(mem_ctx, &state, struct chain3_andx_state);
	if (req == NULL) {
		return NULL;
	}

	strlcpy(state->str, "hello", sizeof(state->str));

	subreq = cli_openx_create(state, ev, cli, fname,
				  O_CREAT|O_RDWR, 0, &smbreqs[0]);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, chain3_andx_open_done, req);

	subreq = cli_write_andx_create(state, ev, cli, 0, 0,
				       (const uint8_t *)state->str, 0,
				       strlen(state->str)+1,
				       smbreqs, 1, &smbreqs[1]);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, chain3_andx_write_done, req);

	subreq = cli_smb1_close_create(state, ev, cli, 0, &smbreqs[2]);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, chain3_andx_close_done, req);

	status = smb1cli_req_chain_submit(smbreqs, ARRAY_SIZE(smbreqs));
	if (tevent_req_nterror(req, status)) {
		return tevent_req_post(req, ev);
	}
	return req;
}

static void chain3_andx_open_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct chain3_andx_state *state = tevent_req_data(
		req, struct chain3_andx_state);
	NTSTATUS status;

	status = cli_openx_recv(subreq, &state->fnum);
	printf("cli_openx returned %s, fnum=%u\n", nt_errstr(status),
	       (unsigned)state->fnum);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}
}

static void chain3_andx_write_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct chain3_andx_state *state = tevent_req_data(
		req, struct chain3_andx_state);
	NTSTATUS status;

	status = cli_write_andx_recv(subreq, &state->written);
	printf("cli_write_andx returned %s, written=%u\n", nt_errstr(status),
	       (unsigned)state->written);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}
}

static void chain3_andx_close_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	NTSTATUS status;

	status = cli_close_recv(subreq);
	printf("cli_close returned %s\n", nt_errstr(status));
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}
	tevent_req_done(req);
}

static NTSTATUS chain3_andx_recv(struct tevent_req *req)
{
	return tevent_req_simple_recv_ntstatus(req);
}

struct chain3_state {
	struct tevent_context *ev;
	struct cli_state *cli;
	const char *fname;
	uint16_t fnum;
};

static void chain3_got_break(struct tevent_req *subreq);
static void chain3_ntcreate_done(struct tevent_req *subreq);
static void chain3_break_close_done(struct tevent_req *subreq);
static void chain3_andx_done(struct tevent_req *subreq);

static struct tevent_req *chain3_send(TALLOC_CTX *mem_ctx,
				      struct tevent_context *ev)
{
	struct tevent_req *req, *subreq;
	struct chain3_state *state;

	req = tevent_req_create(mem_ctx, &state, struct chain3_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->fname = "chain3.txt";

	if (!torture_open_connection(&state->cli, 0)) {
		tevent_req_nterror(req, NT_STATUS_UNSUCCESSFUL);
		return tevent_req_post(req, ev);
	}

	subreq = cli_smb_oplock_break_waiter_send(
		state, state->ev, state->cli);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, chain3_got_break, req);

	subreq = cli_ntcreate_send(
		state, state->ev, state->cli, state->fname,
		REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK,
		GENERIC_READ_ACCESS|GENERIC_WRITE_ACCESS,
		FILE_ATTRIBUTE_NORMAL,
		FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
		FILE_OVERWRITE_IF, 0,
		SMB2_IMPERSONATION_IMPERSONATION, 0);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, chain3_ntcreate_done, req);
	return req;
}

static void chain3_got_break(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct chain3_state *state = tevent_req_data(
		req, struct chain3_state);
	uint16_t fnum;
	uint8_t level;
	NTSTATUS status;

	status = cli_smb_oplock_break_waiter_recv(subreq, &fnum, &level);
	TALLOC_FREE(subreq);
	printf("cli_smb_oplock_break_waiter_recv returned %s\n",
	       nt_errstr(status));
	if (tevent_req_nterror(req, status)) {
		return;
	}
	subreq = cli_close_send(state, state->ev, state->cli, fnum);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, chain3_break_close_done, req);
}

static void chain3_break_close_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	NTSTATUS status;

	status = cli_close_recv(subreq);
	TALLOC_FREE(subreq);
	printf("cli_close_recv returned %s\n", nt_errstr(status));
	if (tevent_req_nterror(req, status)) {
		return;
	}
}

static void chain3_ntcreate_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct chain3_state *state = tevent_req_data(
		req, struct chain3_state);
	NTSTATUS status;

	status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
	TALLOC_FREE(subreq);
	printf("cli_ntcreate returned %s, fnum=%u\n", nt_errstr(status),
	       (unsigned)state->fnum);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	subreq = chain3_andx_send(state, state->ev, state->cli, state->fname);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, chain3_andx_done, req);
}

static void chain3_andx_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	NTSTATUS status;

	status = chain3_andx_recv(subreq);
	TALLOC_FREE(subreq);
	printf("chain3_andx_recv returned %s\n", nt_errstr(status));
	if (tevent_req_nterror(req, status)) {
		return;
	}
	tevent_req_done(req);
}

static NTSTATUS chain3_recv(struct tevent_req *req)
{
	return tevent_req_simple_recv_ntstatus(req);
}

bool run_chain3(int dummy)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev;
	struct tevent_req *req;
	NTSTATUS status = NT_STATUS_NO_MEMORY;

	ev = samba_tevent_context_init(frame);
	if (ev == NULL) {
		goto fail;
	}
	req = chain3_send(frame, ev);
	if (req == NULL) {
		goto fail;
	}
	if (!tevent_req_poll_ntstatus(req, ev, &status)) {
		goto fail;
	}
	status = chain3_recv(req);
fail:
	TALLOC_FREE(frame);
	printf("run_chain3 returns %s\n", nt_errstr(status));
	return NT_STATUS_IS_OK(status);
}