summaryrefslogtreecommitdiff
path: root/source3/libsmb/climessage.c
blob: 170447b2539fa53dfbba734ed83288000dba0f11 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
   Unix SMB/CIFS implementation.
   client message handling routines
   Copyright (C) Andrew Tridgell 1994-1998

   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"

struct cli_message_start_state {
	uint16_t grp;
};

static void cli_message_start_done(struct tevent_req *subreq);

static struct tevent_req *cli_message_start_send(TALLOC_CTX *mem_ctx,
						 struct tevent_context *ev,
						 struct cli_state *cli,
						 const char *host,
						 const char *username)
{
	struct tevent_req *req, *subreq;
	struct cli_message_start_state *state;
	char *htmp = NULL;
	char *utmp = NULL;
	size_t hlen, ulen;
	uint8_t *bytes, *p;

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

	if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_DOS,
				   username, strlen(username)+1,
				   &utmp, &ulen, true)) {
		goto fail;
	}
	if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_DOS,
				   host, strlen(host)+1,
				   &htmp, &hlen, true)) {
		goto fail;
	}

	bytes = talloc_array(state, uint8_t, ulen+hlen+2);
	if (bytes == NULL) {
		goto fail;
	}
	p = bytes;

	*p++ = 4;
	memcpy(p, utmp, ulen);
	*p++ = 4;
	memcpy(p, htmp, hlen);
	TALLOC_FREE(htmp);
	TALLOC_FREE(utmp);

	subreq = cli_smb_send(state, ev, cli, SMBsendstrt, 0, 0, NULL,
			      talloc_get_size(bytes), bytes);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, cli_message_start_done, req);
	return req;
fail:
	TALLOC_FREE(htmp);
	TALLOC_FREE(utmp);
	tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
	return tevent_req_post(req, ev);
}

static void cli_message_start_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct cli_message_start_state *state = tevent_req_data(
		req, struct cli_message_start_state);
	NTSTATUS status;
	uint8_t wct;
	uint16_t *vwv;

	status = cli_smb_recv(subreq, NULL, NULL, 0, &wct, &vwv, NULL, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(subreq);
		tevent_req_nterror(req, status);
		return;
	}
	if (wct >= 1) {
		state->grp = SVAL(vwv+0, 0);
	} else {
		state->grp = 0;
	}
	TALLOC_FREE(subreq);
	tevent_req_done(req);
}

static NTSTATUS cli_message_start_recv(struct tevent_req *req,
				       uint16_t *pgrp)
{
	struct cli_message_start_state *state = tevent_req_data(
		req, struct cli_message_start_state);
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		return status;
	}
	*pgrp = state->grp;
	return NT_STATUS_OK;
}

struct cli_message_text_state {
	uint16_t vwv;
};

static void cli_message_text_done(struct tevent_req *subreq);

static struct tevent_req *cli_message_text_send(TALLOC_CTX *mem_ctx,
						struct tevent_context *ev,
						struct cli_state *cli,
						uint16_t grp,
						const char *msg,
						int msglen)
{
	struct tevent_req *req, *subreq;
	struct cli_message_text_state *state;
	char *tmp;
	size_t tmplen;
	uint8_t *bytes;

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

	SSVAL(&state->vwv, 0, grp);

	if (convert_string_talloc(talloc_tos(), CH_UNIX, CH_DOS, msg, msglen,
				  &tmp, &tmplen, true)) {
		msg = tmp;
		msglen = tmplen;
	} else {
		DEBUG(3, ("Conversion failed, sending message in UNIX "
			  "charset\n"));
		tmp = NULL;
	}

	bytes = talloc_array(state, uint8_t, msglen+3);
	if (tevent_req_nomem(bytes, req)) {
		TALLOC_FREE(tmp);
		return tevent_req_post(req, ev);
	}
	SCVAL(bytes, 0, 0);	/* pad */
	SSVAL(bytes, 1, msglen);
	memcpy(bytes+3, msg, msglen);
	TALLOC_FREE(tmp);

	subreq = cli_smb_send(state, ev, cli, SMBsendtxt, 0, 1, &state->vwv,
			      talloc_get_size(bytes), bytes);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, cli_message_text_done, req);
	return req;
}

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

	status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	tevent_req_done(req);
}

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

struct cli_message_end_state {
	uint16_t vwv;
};

static void cli_message_end_done(struct tevent_req *subreq);

static struct tevent_req *cli_message_end_send(TALLOC_CTX *mem_ctx,
						struct tevent_context *ev,
						struct cli_state *cli,
						uint16_t grp)
{
	struct tevent_req *req, *subreq;
	struct cli_message_end_state *state;

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

	SSVAL(&state->vwv, 0, grp);

	subreq = cli_smb_send(state, ev, cli, SMBsendend, 0, 1, &state->vwv,
			      0, NULL);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, cli_message_end_done, req);
	return req;
}

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

	status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	tevent_req_done(req);
}

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

struct cli_message_state {
	struct tevent_context *ev;
	struct cli_state *cli;
	size_t sent;
	const char *message;
	uint16_t grp;
};

static void cli_message_started(struct tevent_req *subreq);
static void cli_message_sent(struct tevent_req *subreq);
static void cli_message_done(struct tevent_req *subreq);

struct tevent_req *cli_message_send(TALLOC_CTX *mem_ctx,
				    struct tevent_context *ev,
				    struct cli_state *cli,
				    const char *host, const char *username,
				    const char *message)
{
	struct tevent_req *req, *subreq;
	struct cli_message_state *state;

	req = tevent_req_create(mem_ctx, &state, struct cli_message_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->cli = cli;
	state->sent = 0;
	state->message = message;

	subreq = cli_message_start_send(state, ev, cli, host, username);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, cli_message_started, req);
	return req;
}

static void cli_message_started(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct cli_message_state *state = tevent_req_data(
		req, struct cli_message_state);
	NTSTATUS status;
	size_t thistime;

	status = cli_message_start_recv(subreq, &state->grp);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}

	thistime = MIN(127, strlen(state->message));

	subreq = cli_message_text_send(state, state->ev, state->cli,
				       state->grp, state->message, thistime);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	state->sent += thistime;
	tevent_req_set_callback(subreq, cli_message_sent, req);
}

static void cli_message_sent(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct cli_message_state *state = tevent_req_data(
		req, struct cli_message_state);
	NTSTATUS status;
	size_t left, thistime;

	status = cli_message_text_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}

	if (state->sent >= strlen(state->message)) {
		subreq = cli_message_end_send(state, state->ev, state->cli,
					      state->grp);
		if (tevent_req_nomem(subreq, req)) {
			return;
		}
		tevent_req_set_callback(subreq, cli_message_done, req);
		return;
	}

	left = strlen(state->message) - state->sent;
	thistime = MIN(127, left);

	subreq = cli_message_text_send(state, state->ev, state->cli,
				       state->grp,
				       state->message + state->sent,
				       thistime);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	state->sent += thistime;
	tevent_req_set_callback(subreq, cli_message_sent, req);
}

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

	status = cli_message_end_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	tevent_req_done(req);
}

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

NTSTATUS cli_message(struct cli_state *cli, const char *host,
		     const char *username, const char *message)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct event_context *ev;
	struct tevent_req *req;
	NTSTATUS status = NT_STATUS_OK;

	if (cli_has_async_calls(cli)) {
		/*
		 * Can't use sync call while an async call is in flight
		 */
		status = NT_STATUS_INVALID_PARAMETER;
		goto fail;
	}

	ev = event_context_init(frame);
	if (ev == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	req = cli_message_send(frame, ev, cli, host, username, message);
	if (req == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	if (!tevent_req_poll(req, ev)) {
		status = map_nt_error_from_unix(errno);
		goto fail;
	}

	status = cli_message_recv(req);
 fail:
	TALLOC_FREE(frame);
	return status;
}