2 Unix SMB/CIFS implementation.
3 client message handling routines
4 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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/>.
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "async_smb.h"
23 #include "libsmb/libsmb.h"
24 #include "../libcli/smb/smbXcli_base.h"
26 struct cli_message_start_state {
30 static void cli_message_start_done(struct tevent_req *subreq);
32 static struct tevent_req *cli_message_start_send(TALLOC_CTX *mem_ctx,
33 struct tevent_context *ev,
34 struct cli_state *cli,
38 struct tevent_req *req, *subreq;
39 struct cli_message_start_state *state;
45 req = tevent_req_create(mem_ctx, &state,
46 struct cli_message_start_state);
51 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_DOS,
52 username, strlen(username)+1,
56 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_DOS,
62 bytes = talloc_array(state, uint8_t, ulen+hlen+2);
69 memcpy(p, utmp, ulen);
72 memcpy(p, htmp, hlen);
77 subreq = cli_smb_send(state, ev, cli, SMBsendstrt, 0, 0, NULL,
78 talloc_get_size(bytes), bytes);
79 if (tevent_req_nomem(subreq, req)) {
80 return tevent_req_post(req, ev);
82 tevent_req_set_callback(subreq, cli_message_start_done, req);
87 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
88 return tevent_req_post(req, ev);
91 static void cli_message_start_done(struct tevent_req *subreq)
93 struct tevent_req *req = tevent_req_callback_data(
94 subreq, struct tevent_req);
95 struct cli_message_start_state *state = tevent_req_data(
96 req, struct cli_message_start_state);
102 status = cli_smb_recv(subreq, state, &inbuf, 0, &wct, &vwv,
105 if (!NT_STATUS_IS_OK(status)) {
107 tevent_req_nterror(req, status);
111 state->grp = SVAL(vwv+0, 0);
115 tevent_req_done(req);
118 static NTSTATUS cli_message_start_recv(struct tevent_req *req,
121 struct cli_message_start_state *state = tevent_req_data(
122 req, struct cli_message_start_state);
125 if (tevent_req_is_nterror(req, &status)) {
132 struct cli_message_text_state {
136 static void cli_message_text_done(struct tevent_req *subreq);
138 static struct tevent_req *cli_message_text_send(TALLOC_CTX *mem_ctx,
139 struct tevent_context *ev,
140 struct cli_state *cli,
145 struct tevent_req *req, *subreq;
146 struct cli_message_text_state *state;
151 req = tevent_req_create(mem_ctx, &state,
152 struct cli_message_text_state);
157 SSVAL(&state->vwv, 0, grp);
159 if (convert_string_talloc(talloc_tos(), CH_UNIX, CH_DOS, msg, msglen,
164 DEBUG(3, ("Conversion failed, sending message in UNIX "
169 bytes = talloc_array(state, uint8_t, msglen+3);
170 if (tevent_req_nomem(bytes, req)) {
172 return tevent_req_post(req, ev);
174 SCVAL(bytes, 0, 1); /* pad */
175 SSVAL(bytes+1, 0, msglen);
176 memcpy(bytes+3, msg, msglen);
179 subreq = cli_smb_send(state, ev, cli, SMBsendtxt, 0, 1, &state->vwv,
180 talloc_get_size(bytes), bytes);
181 if (tevent_req_nomem(subreq, req)) {
182 return tevent_req_post(req, ev);
184 tevent_req_set_callback(subreq, cli_message_text_done, req);
188 static void cli_message_text_done(struct tevent_req *subreq)
190 struct tevent_req *req = tevent_req_callback_data(
191 subreq, struct tevent_req);
194 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
196 if (!NT_STATUS_IS_OK(status)) {
197 tevent_req_nterror(req, status);
200 tevent_req_done(req);
203 static NTSTATUS cli_message_text_recv(struct tevent_req *req)
205 return tevent_req_simple_recv_ntstatus(req);
208 struct cli_message_end_state {
212 static void cli_message_end_done(struct tevent_req *subreq);
214 static struct tevent_req *cli_message_end_send(TALLOC_CTX *mem_ctx,
215 struct tevent_context *ev,
216 struct cli_state *cli,
219 struct tevent_req *req, *subreq;
220 struct cli_message_end_state *state;
222 req = tevent_req_create(mem_ctx, &state,
223 struct cli_message_end_state);
228 SSVAL(&state->vwv, 0, grp);
230 subreq = cli_smb_send(state, ev, cli, SMBsendend, 0, 1, &state->vwv,
232 if (tevent_req_nomem(subreq, req)) {
233 return tevent_req_post(req, ev);
235 tevent_req_set_callback(subreq, cli_message_end_done, req);
239 static void cli_message_end_done(struct tevent_req *subreq)
241 struct tevent_req *req = tevent_req_callback_data(
242 subreq, struct tevent_req);
245 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
247 if (!NT_STATUS_IS_OK(status)) {
248 tevent_req_nterror(req, status);
251 tevent_req_done(req);
254 static NTSTATUS cli_message_end_recv(struct tevent_req *req)
256 return tevent_req_simple_recv_ntstatus(req);
259 struct cli_message_state {
260 struct tevent_context *ev;
261 struct cli_state *cli;
267 static void cli_message_started(struct tevent_req *subreq);
268 static void cli_message_sent(struct tevent_req *subreq);
269 static void cli_message_done(struct tevent_req *subreq);
271 struct tevent_req *cli_message_send(TALLOC_CTX *mem_ctx,
272 struct tevent_context *ev,
273 struct cli_state *cli,
274 const char *host, const char *username,
277 struct tevent_req *req, *subreq;
278 struct cli_message_state *state;
280 req = tevent_req_create(mem_ctx, &state, struct cli_message_state);
287 state->message = message;
289 subreq = cli_message_start_send(state, ev, cli, host, username);
290 if (tevent_req_nomem(subreq, req)) {
291 return tevent_req_post(req, ev);
293 tevent_req_set_callback(subreq, cli_message_started, req);
297 static void cli_message_started(struct tevent_req *subreq)
299 struct tevent_req *req = tevent_req_callback_data(
300 subreq, struct tevent_req);
301 struct cli_message_state *state = tevent_req_data(
302 req, struct cli_message_state);
306 status = cli_message_start_recv(subreq, &state->grp);
308 if (!NT_STATUS_IS_OK(status)) {
309 tevent_req_nterror(req, status);
313 thistime = MIN(127, strlen(state->message));
315 subreq = cli_message_text_send(state, state->ev, state->cli,
316 state->grp, state->message, thistime);
317 if (tevent_req_nomem(subreq, req)) {
320 state->sent += thistime;
321 tevent_req_set_callback(subreq, cli_message_sent, req);
324 static void cli_message_sent(struct tevent_req *subreq)
326 struct tevent_req *req = tevent_req_callback_data(
327 subreq, struct tevent_req);
328 struct cli_message_state *state = tevent_req_data(
329 req, struct cli_message_state);
331 size_t left, thistime;
333 status = cli_message_text_recv(subreq);
335 if (!NT_STATUS_IS_OK(status)) {
336 tevent_req_nterror(req, status);
340 if (state->sent >= strlen(state->message)) {
341 subreq = cli_message_end_send(state, state->ev, state->cli,
343 if (tevent_req_nomem(subreq, req)) {
346 tevent_req_set_callback(subreq, cli_message_done, req);
350 left = strlen(state->message) - state->sent;
351 thistime = MIN(127, left);
353 subreq = cli_message_text_send(state, state->ev, state->cli,
355 state->message + state->sent,
357 if (tevent_req_nomem(subreq, req)) {
360 state->sent += thistime;
361 tevent_req_set_callback(subreq, cli_message_sent, req);
364 static void cli_message_done(struct tevent_req *subreq)
366 struct tevent_req *req = tevent_req_callback_data(
367 subreq, struct tevent_req);
370 status = cli_message_end_recv(subreq);
372 if (!NT_STATUS_IS_OK(status)) {
373 tevent_req_nterror(req, status);
376 tevent_req_done(req);
379 NTSTATUS cli_message_recv(struct tevent_req *req)
381 return tevent_req_simple_recv_ntstatus(req);
384 NTSTATUS cli_message(struct cli_state *cli, const char *host,
385 const char *username, const char *message)
387 TALLOC_CTX *frame = talloc_stackframe();
388 struct event_context *ev;
389 struct tevent_req *req;
390 NTSTATUS status = NT_STATUS_OK;
392 if (smbXcli_conn_has_async_calls(cli->conn)) {
394 * Can't use sync call while an async call is in flight
396 status = NT_STATUS_INVALID_PARAMETER;
400 ev = event_context_init(frame);
402 status = NT_STATUS_NO_MEMORY;
406 req = cli_message_send(frame, ev, cli, host, username, message);
408 status = NT_STATUS_NO_MEMORY;
412 if (!tevent_req_poll(req, ev)) {
413 status = map_nt_error_from_unix(errno);
417 status = cli_message_recv(req);