source: trunk/server/source3/libsmb/clientgen.c@ 888

Last change on this file since 888 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 24.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 SMB client generic functions
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2007.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "includes.h"
22#include "libsmb/libsmb.h"
23#include "../lib/util/tevent_ntstatus.h"
24#include "smb_signing.h"
25#include "async_smb.h"
26
27/*******************************************************************
28 Setup the word count and byte count for a client smb message.
29********************************************************************/
30
31int cli_set_message(char *buf,int num_words,int num_bytes,bool zero)
32{
33 if (zero && (num_words || num_bytes)) {
34 memset(buf + smb_size,'\0',num_words*2 + num_bytes);
35 }
36 SCVAL(buf,smb_wct,num_words);
37 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
38 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
39 return (smb_size + num_words*2 + num_bytes);
40}
41
42/****************************************************************************
43 Change the timeout (in milliseconds).
44****************************************************************************/
45
46unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
47{
48 unsigned int old_timeout = cli->timeout;
49 cli->timeout = timeout;
50 return old_timeout;
51}
52
53/****************************************************************************
54 Change the port number used to call on.
55****************************************************************************/
56
57void cli_set_port(struct cli_state *cli, int port)
58{
59 cli->port = port;
60}
61
62/****************************************************************************
63 convenience routine to find if we negotiated ucs2
64****************************************************************************/
65
66bool cli_ucs2(struct cli_state *cli)
67{
68 return ((cli->capabilities & CAP_UNICODE) != 0);
69}
70
71
72/****************************************************************************
73 Read an smb from a fd ignoring all keepalive packets.
74 The timeout is in milliseconds
75
76 This is exactly the same as receive_smb except that it never returns
77 a session keepalive packet (just as receive_smb used to do).
78 receive_smb was changed to return keepalives as the oplock processing means this call
79 should never go into a blocking read.
80****************************************************************************/
81
82static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
83{
84 size_t len;
85
86 for(;;) {
87 NTSTATUS status;
88
89 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
90
91 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize,
92 cli->timeout, maxlen, &len);
93 if (!NT_STATUS_IS_OK(status)) {
94 DEBUG(10,("client_receive_smb failed\n"));
95 show_msg(cli->inbuf);
96
97 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
98 set_smb_read_error(&cli->smb_rw_error,
99 SMB_READ_EOF);
100 return -1;
101 }
102
103 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
104 set_smb_read_error(&cli->smb_rw_error,
105 SMB_READ_TIMEOUT);
106 return -1;
107 }
108
109 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
110 return -1;
111 }
112
113 /*
114 * I don't believe len can be < 0 with NT_STATUS_OK
115 * returned above, but this check doesn't hurt. JRA.
116 */
117
118 if ((ssize_t)len < 0) {
119 return len;
120 }
121
122 /* Ignore session keepalive packets. */
123 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
124 break;
125 }
126 }
127
128 if (cli_encryption_on(cli)) {
129 NTSTATUS status = cli_decrypt_message(cli);
130 if (!NT_STATUS_IS_OK(status)) {
131 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
132 nt_errstr(status)));
133 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
134 return -1;
135 }
136 }
137
138 show_msg(cli->inbuf);
139 return len;
140}
141
142static bool cli_state_set_seqnum(struct cli_state *cli, uint16_t mid, uint32_t seqnum)
143{
144 struct cli_state_seqnum *c;
145
146 for (c = cli->seqnum; c; c = c->next) {
147 if (c->mid == mid) {
148 c->seqnum = seqnum;
149 return true;
150 }
151 }
152
153 c = talloc_zero(cli, struct cli_state_seqnum);
154 if (!c) {
155 return false;
156 }
157
158 c->mid = mid;
159 c->seqnum = seqnum;
160 c->persistent = false;
161 DLIST_ADD_END(cli->seqnum, c, struct cli_state_seqnum *);
162
163 return true;
164}
165
166bool cli_state_seqnum_persistent(struct cli_state *cli,
167 uint16_t mid)
168{
169 struct cli_state_seqnum *c;
170
171 for (c = cli->seqnum; c; c = c->next) {
172 if (c->mid == mid) {
173 c->persistent = true;
174 return true;
175 }
176 }
177
178 return false;
179}
180
181bool cli_state_seqnum_remove(struct cli_state *cli,
182 uint16_t mid)
183{
184 struct cli_state_seqnum *c;
185
186 for (c = cli->seqnum; c; c = c->next) {
187 if (c->mid == mid) {
188 DLIST_REMOVE(cli->seqnum, c);
189 TALLOC_FREE(c);
190 return true;
191 }
192 }
193
194 return false;
195}
196
197static uint32_t cli_state_get_seqnum(struct cli_state *cli, uint16_t mid)
198{
199 struct cli_state_seqnum *c;
200
201 for (c = cli->seqnum; c; c = c->next) {
202 if (c->mid == mid) {
203 uint32_t seqnum = c->seqnum;
204 if (!c->persistent) {
205 DLIST_REMOVE(cli->seqnum, c);
206 TALLOC_FREE(c);
207 }
208 return seqnum;
209 }
210 }
211
212 return 0;
213}
214
215/****************************************************************************
216 Recv an smb.
217****************************************************************************/
218
219bool cli_receive_smb(struct cli_state *cli)
220{
221 ssize_t len;
222 uint16_t mid;
223 uint32_t seqnum;
224
225 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
226 if (cli->fd == -1)
227 return false;
228
229 again:
230 len = client_receive_smb(cli, 0);
231
232 if (len > 0) {
233 /* it might be an oplock break request */
234 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
235 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
236 SVAL(cli->inbuf,smb_vwv6) == 0 &&
237 SVAL(cli->inbuf,smb_vwv7) == 0) {
238 if (cli->oplock_handler) {
239 int fnum = SVAL(cli->inbuf,smb_vwv2);
240 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
241 if (!NT_STATUS_IS_OK(cli->oplock_handler(cli, fnum, level))) {
242 return false;
243 }
244 }
245 /* try to prevent loops */
246 SCVAL(cli->inbuf,smb_com,0xFF);
247 goto again;
248 }
249 }
250
251 /* If the server is not responding, note that now */
252 if (len < 0) {
253 /*
254 * only log if the connection should still be open and not when
255 * the connection was closed due to a dropped ip message
256 */
257 if (cli->fd != -1) {
258 char addr[INET6_ADDRSTRLEN];
259 print_sockaddr(addr, sizeof(addr), &cli->dest_ss);
260 DEBUG(0, ("Receiving SMB: Server %s stopped responding\n",
261 addr));
262 close(cli->fd);
263 cli->fd = -1;
264 }
265 return false;
266 }
267
268 mid = SVAL(cli->inbuf,smb_mid);
269 seqnum = cli_state_get_seqnum(cli, mid);
270
271 if (!cli_check_sign_mac(cli, cli->inbuf, seqnum+1)) {
272 /*
273 * If we get a signature failure in sessionsetup, then
274 * the server sometimes just reflects the sent signature
275 * back to us. Detect this and allow the upper layer to
276 * retrieve the correct Windows error message.
277 */
278 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
279 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
280 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
281 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
282 cli_is_error(cli)) {
283
284 /*
285 * Reflected signature on login error.
286 * Set bad sig but don't close fd.
287 */
288 cli->smb_rw_error = SMB_READ_BAD_SIG;
289 return true;
290 }
291
292 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
293 cli->smb_rw_error = SMB_READ_BAD_SIG;
294 close(cli->fd);
295 cli->fd = -1;
296 return false;
297 };
298 return true;
299}
300
301static ssize_t write_socket(int fd, const char *buf, size_t len)
302{
303 ssize_t ret=0;
304
305 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
306 ret = write_data(fd,buf,len);
307
308 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
309 if(ret <= 0)
310 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
311 (int)len, fd, strerror(errno) ));
312
313 return(ret);
314}
315
316/****************************************************************************
317 Send an smb to a fd.
318****************************************************************************/
319
320bool cli_send_smb(struct cli_state *cli)
321{
322 size_t len;
323 size_t nwritten=0;
324 ssize_t ret;
325 char *buf_out = cli->outbuf;
326 bool enc_on = cli_encryption_on(cli);
327 uint32_t seqnum;
328
329 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
330 if (cli->fd == -1)
331 return false;
332
333 cli_calculate_sign_mac(cli, cli->outbuf, &seqnum);
334
335 if (!cli_state_set_seqnum(cli, cli->mid, seqnum)) {
336 DEBUG(0,("Failed to store mid[%u]/seqnum[%u]\n",
337 (unsigned int)cli->mid,
338 (unsigned int)seqnum));
339 return false;
340 }
341
342 if (enc_on) {
343 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
344 &buf_out);
345 if (!NT_STATUS_IS_OK(status)) {
346 close(cli->fd);
347 cli->fd = -1;
348 cli->smb_rw_error = SMB_WRITE_ERROR;
349 DEBUG(0,("Error in encrypting client message. Error %s\n",
350 nt_errstr(status) ));
351 return false;
352 }
353 }
354
355 len = smb_len(buf_out) + 4;
356
357 while (nwritten < len) {
358 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
359 if (ret <= 0) {
360 if (enc_on) {
361 cli_free_enc_buffer(cli, buf_out);
362 }
363 close(cli->fd);
364 cli->fd = -1;
365 cli->smb_rw_error = SMB_WRITE_ERROR;
366 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
367 (int)len,(int)ret, strerror(errno) ));
368 return false;
369 }
370 nwritten += ret;
371 }
372
373 if (enc_on) {
374 cli_free_enc_buffer(cli, buf_out);
375 }
376
377 /* Increment the mid so we can tell between responses. */
378 cli->mid++;
379 if (!cli->mid)
380 cli->mid++;
381 return true;
382}
383
384/****************************************************************************
385 Setup basics in a outgoing packet.
386****************************************************************************/
387
388void cli_setup_packet_buf(struct cli_state *cli, char *buf)
389{
390 uint16 flags2;
391 cli->rap_error = 0;
392 SIVAL(buf,smb_rcls,0);
393 SSVAL(buf,smb_pid,cli->pid);
394 memset(buf+smb_pidhigh, 0, 12);
395 SSVAL(buf,smb_uid,cli->vuid);
396 SSVAL(buf,smb_mid,cli->mid);
397
398 if (cli->protocol <= PROTOCOL_CORE) {
399 return;
400 }
401
402 if (cli->case_sensitive) {
403 SCVAL(buf,smb_flg,0x0);
404 } else {
405 /* Default setting, case insensitive. */
406 SCVAL(buf,smb_flg,0x8);
407 }
408 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
409 if (cli->capabilities & CAP_UNICODE)
410 flags2 |= FLAGS2_UNICODE_STRINGS;
411 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
412 flags2 |= FLAGS2_DFS_PATHNAMES;
413 if (cli->capabilities & CAP_STATUS32)
414 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
415 if (cli->use_spnego)
416 flags2 |= FLAGS2_EXTENDED_SECURITY;
417 SSVAL(buf,smb_flg2, flags2);
418}
419
420void cli_setup_packet(struct cli_state *cli)
421{
422 cli_setup_packet_buf(cli, cli->outbuf);
423}
424
425/****************************************************************************
426 Setup the bcc length of the packet from a pointer to the end of the data.
427****************************************************************************/
428
429void cli_setup_bcc(struct cli_state *cli, void *p)
430{
431 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
432}
433
434/****************************************************************************
435 Initialize Domain, user or password.
436****************************************************************************/
437
438NTSTATUS cli_set_domain(struct cli_state *cli, const char *domain)
439{
440 TALLOC_FREE(cli->domain);
441 cli->domain = talloc_strdup(cli, domain ? domain : "");
442 if (cli->domain == NULL) {
443 return NT_STATUS_NO_MEMORY;
444 }
445 return NT_STATUS_OK;
446}
447
448NTSTATUS cli_set_username(struct cli_state *cli, const char *username)
449{
450 TALLOC_FREE(cli->user_name);
451 cli->user_name = talloc_strdup(cli, username ? username : "");
452 if (cli->user_name == NULL) {
453 return NT_STATUS_NO_MEMORY;
454 }
455 return NT_STATUS_OK;
456}
457
458NTSTATUS cli_set_password(struct cli_state *cli, const char *password)
459{
460 TALLOC_FREE(cli->password);
461
462 /* Password can be NULL. */
463 if (password) {
464 cli->password = talloc_strdup(cli, password);
465 if (cli->password == NULL) {
466 return NT_STATUS_NO_MEMORY;
467 }
468 } else {
469 /* Use zero NTLMSSP hashes and session key. */
470 cli->password = NULL;
471 }
472
473 return NT_STATUS_OK;
474}
475
476/****************************************************************************
477 Initialise credentials of a client structure.
478****************************************************************************/
479
480NTSTATUS cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
481{
482 NTSTATUS status = cli_set_username(cli, username);
483 if (!NT_STATUS_IS_OK(status)) {
484 return status;
485 }
486 status = cli_set_domain(cli, domain);
487 if (!NT_STATUS_IS_OK(status)) {
488 return status;
489 }
490 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
491
492 return cli_set_password(cli, password);
493}
494
495/****************************************************************************
496 Initialise a client structure. Always returns a talloc'ed struct.
497 Set the signing state (used from the command line).
498****************************************************************************/
499
500struct cli_state *cli_initialise_ex(int signing_state)
501{
502 struct cli_state *cli = NULL;
503 bool allow_smb_signing = false;
504 bool mandatory_signing = false;
505
506#ifndef __OS2__
507 /* Check the effective uid - make sure we are not setuid */
508 if (is_setuid_root()) {
509 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
510 return NULL;
511 }
512#endif
513
514 cli = TALLOC_ZERO_P(NULL, struct cli_state);
515 if (!cli) {
516 return NULL;
517 }
518
519 cli->dfs_mountpoint = talloc_strdup(cli, "");
520 if (!cli->dfs_mountpoint) {
521 goto error;
522 }
523 cli->port = 0;
524 cli->fd = -1;
525 cli->cnum = -1;
526 cli->pid = (uint16)sys_getpid();
527 cli->mid = 1;
528 cli->vuid = UID_FIELD_INVALID;
529 cli->protocol = PROTOCOL_NT1;
530 cli->timeout = 20000; /* Timeout is in milliseconds. */
531 cli->bufsize = CLI_BUFFER_SIZE+4;
532 cli->max_xmit = cli->bufsize;
533 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
534 cli->seqnum = 0;
535 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
536 cli->oplock_handler = cli_oplock_ack;
537 cli->case_sensitive = false;
538 cli->smb_rw_error = SMB_READ_OK;
539
540 cli->use_spnego = lp_client_use_spnego();
541
542 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
543
544 /* Set the CLI_FORCE_DOSERR environment variable to test
545 client routines using DOS errors instead of STATUS32
546 ones. This intended only as a temporary hack. */
547 if (getenv("CLI_FORCE_DOSERR"))
548 cli->force_dos_errors = true;
549
550 if (lp_client_signing()) {
551 allow_smb_signing = true;
552 }
553
554 if (lp_client_signing() == Required) {
555 mandatory_signing = true;
556 }
557
558 if (signing_state != Undefined) {
559 allow_smb_signing = true;
560 }
561
562 if (signing_state == false) {
563 allow_smb_signing = false;
564 mandatory_signing = false;
565 }
566
567 if (signing_state == Required) {
568 mandatory_signing = true;
569 }
570
571 if (!cli->outbuf || !cli->inbuf)
572 goto error;
573
574 memset(cli->outbuf, 0, cli->bufsize);
575 memset(cli->inbuf, 0, cli->bufsize);
576
577
578#if defined(DEVELOPER)
579 /* just because we over-allocate, doesn't mean it's right to use it */
580 clobber_region(__FUNCTION__, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
581 clobber_region(__FUNCTION__, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
582#endif
583
584 /* initialise signing */
585 cli->signing_state = smb_signing_init(cli,
586 allow_smb_signing,
587 mandatory_signing);
588 if (!cli->signing_state) {
589 goto error;
590 }
591
592 cli->outgoing = tevent_queue_create(cli, "cli_outgoing");
593 if (cli->outgoing == NULL) {
594 goto error;
595 }
596 cli->pending = NULL;
597
598 cli->initialised = 1;
599
600 return cli;
601
602 /* Clean up after malloc() error */
603
604 error:
605
606 SAFE_FREE(cli->inbuf);
607 SAFE_FREE(cli->outbuf);
608 TALLOC_FREE(cli);
609 return NULL;
610}
611
612struct cli_state *cli_initialise(void)
613{
614 return cli_initialise_ex(Undefined);
615}
616
617/****************************************************************************
618 Close all pipes open on this session.
619****************************************************************************/
620
621void cli_nt_pipes_close(struct cli_state *cli)
622{
623 while (cli->pipe_list != NULL) {
624 /*
625 * No TALLOC_FREE here!
626 */
627 talloc_free(cli->pipe_list);
628 }
629}
630
631/****************************************************************************
632 Shutdown a client structure.
633****************************************************************************/
634
635static void _cli_shutdown(struct cli_state *cli)
636{
637 cli_nt_pipes_close(cli);
638
639 /*
640 * tell our peer to free his resources. Wihtout this, when an
641 * application attempts to do a graceful shutdown and calls
642 * smbc_free_context() to clean up all connections, some connections
643 * can remain active on the peer end, until some (long) timeout period
644 * later. This tree disconnect forces the peer to clean up, since the
645 * connection will be going away.
646 *
647 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
648 * the only user for this so far is smbmount which passes opened connection
649 * down to kernel's smbfs module.
650 */
651 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
652 cli_tdis(cli);
653 }
654
655 SAFE_FREE(cli->outbuf);
656 SAFE_FREE(cli->inbuf);
657
658 data_blob_free(&cli->secblob);
659 data_blob_free(&cli->user_session_key);
660
661 if (cli->fd != -1) {
662 close(cli->fd);
663 }
664 cli->fd = -1;
665 cli->smb_rw_error = SMB_READ_OK;
666
667 /*
668 * Need to free pending first, they remove themselves
669 */
670 while (cli->pending) {
671 talloc_free(cli->pending[0]);
672 }
673 TALLOC_FREE(cli);
674}
675
676void cli_shutdown(struct cli_state *cli)
677{
678 struct cli_state *cli_head;
679 if (cli == NULL) {
680 return;
681 }
682 DLIST_HEAD(cli, cli_head);
683 if (cli_head == cli) {
684 /*
685 * head of a DFS list, shutdown all subsidiary DFS
686 * connections.
687 */
688 struct cli_state *p, *next;
689
690 for (p = cli_head->next; p; p = next) {
691 next = p->next;
692 DLIST_REMOVE(cli_head, p);
693 _cli_shutdown(p);
694 }
695 } else {
696 DLIST_REMOVE(cli_head, cli);
697 }
698
699 _cli_shutdown(cli);
700}
701
702/****************************************************************************
703 Set socket options on a open connection.
704****************************************************************************/
705
706void cli_sockopt(struct cli_state *cli, const char *options)
707{
708 set_socket_options(cli->fd, options);
709}
710
711/****************************************************************************
712 Set the PID to use for smb messages. Return the old pid.
713****************************************************************************/
714
715uint16 cli_setpid(struct cli_state *cli, uint16 pid)
716{
717 uint16 ret = cli->pid;
718 cli->pid = pid;
719 return ret;
720}
721
722/****************************************************************************
723 Set the case sensitivity flag on the packets. Returns old state.
724****************************************************************************/
725
726bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
727{
728 bool ret = cli->case_sensitive;
729 cli->case_sensitive = case_sensitive;
730 return ret;
731}
732
733struct cli_echo_state {
734 uint16_t vwv[1];
735 DATA_BLOB data;
736 int num_echos;
737};
738
739static void cli_echo_done(struct tevent_req *subreq);
740
741struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
742 struct cli_state *cli, uint16_t num_echos,
743 DATA_BLOB data)
744{
745 struct tevent_req *req, *subreq;
746 struct cli_echo_state *state;
747
748 req = tevent_req_create(mem_ctx, &state, struct cli_echo_state);
749 if (req == NULL) {
750 return NULL;
751 }
752 SSVAL(state->vwv, 0, num_echos);
753 state->data = data;
754 state->num_echos = num_echos;
755
756 subreq = cli_smb_send(state, ev, cli, SMBecho, 0, 1, state->vwv,
757 data.length, data.data);
758 if (subreq == NULL) {
759 goto fail;
760 }
761 tevent_req_set_callback(subreq, cli_echo_done, req);
762 return req;
763 fail:
764 TALLOC_FREE(req);
765 return NULL;
766}
767
768static void cli_echo_done(struct tevent_req *subreq)
769{
770 struct tevent_req *req = tevent_req_callback_data(
771 subreq, struct tevent_req);
772 struct cli_echo_state *state = tevent_req_data(
773 req, struct cli_echo_state);
774 NTSTATUS status;
775 uint32_t num_bytes;
776 uint8_t *bytes;
777 uint8_t *inbuf;
778
779 status = cli_smb_recv(subreq, state, &inbuf, 0, NULL, NULL,
780 &num_bytes, &bytes);
781 if (!NT_STATUS_IS_OK(status)) {
782 tevent_req_nterror(req, status);
783 return;
784 }
785 if ((num_bytes != state->data.length)
786 || (memcmp(bytes, state->data.data, num_bytes) != 0)) {
787 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
788 return;
789 }
790
791 state->num_echos -=1;
792 if (state->num_echos == 0) {
793 tevent_req_done(req);
794 return;
795 }
796
797 if (!cli_smb_req_set_pending(subreq)) {
798 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
799 return;
800 }
801}
802
803/**
804 * Get the result out from an echo request
805 * @param[in] req The async_req from cli_echo_send
806 * @retval Did the server reply correctly?
807 */
808
809NTSTATUS cli_echo_recv(struct tevent_req *req)
810{
811 return tevent_req_simple_recv_ntstatus(req);
812}
813
814/**
815 * @brief Send/Receive SMBEcho requests
816 * @param[in] mem_ctx The memory context to put the async_req on
817 * @param[in] ev The event context that will call us back
818 * @param[in] cli The connection to send the echo to
819 * @param[in] num_echos How many times do we want to get the reply?
820 * @param[in] data The data we want to get back
821 * @retval Did the server reply correctly?
822 */
823
824NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
825{
826 TALLOC_CTX *frame = talloc_stackframe();
827 struct event_context *ev;
828 struct tevent_req *req;
829 NTSTATUS status = NT_STATUS_OK;
830
831 if (cli_has_async_calls(cli)) {
832 /*
833 * Can't use sync call while an async call is in flight
834 */
835 status = NT_STATUS_INVALID_PARAMETER;
836 goto fail;
837 }
838
839 ev = event_context_init(frame);
840 if (ev == NULL) {
841 status = NT_STATUS_NO_MEMORY;
842 goto fail;
843 }
844
845 req = cli_echo_send(frame, ev, cli, num_echos, data);
846 if (req == NULL) {
847 status = NT_STATUS_NO_MEMORY;
848 goto fail;
849 }
850
851 if (!tevent_req_poll(req, ev)) {
852 status = map_nt_error_from_unix(errno);
853 goto fail;
854 }
855
856 status = cli_echo_recv(req);
857 fail:
858 TALLOC_FREE(frame);
859 if (!NT_STATUS_IS_OK(status)) {
860 cli_set_error(cli, status);
861 }
862 return status;
863}
864
865/**
866 * Is the SMB command able to hold an AND_X successor
867 * @param[in] cmd The SMB command in question
868 * @retval Can we add a chained request after "cmd"?
869 */
870bool is_andx_req(uint8_t cmd)
871{
872 switch (cmd) {
873 case SMBtconX:
874 case SMBlockingX:
875 case SMBopenX:
876 case SMBreadX:
877 case SMBwriteX:
878 case SMBsesssetupX:
879 case SMBulogoffX:
880 case SMBntcreateX:
881 return true;
882 break;
883 default:
884 break;
885 }
886
887 return false;
888}
889
890NTSTATUS cli_smb(TALLOC_CTX *mem_ctx, struct cli_state *cli,
891 uint8_t smb_command, uint8_t additional_flags,
892 uint8_t wct, uint16_t *vwv,
893 uint32_t num_bytes, const uint8_t *bytes,
894 struct tevent_req **result_parent,
895 uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
896 uint32_t *pnum_bytes, uint8_t **pbytes)
897{
898 struct tevent_context *ev;
899 struct tevent_req *req = NULL;
900 NTSTATUS status = NT_STATUS_NO_MEMORY;
901
902 if (cli_has_async_calls(cli)) {
903 return NT_STATUS_INVALID_PARAMETER;
904 }
905 ev = tevent_context_init(mem_ctx);
906 if (ev == NULL) {
907 goto fail;
908 }
909 req = cli_smb_send(mem_ctx, ev, cli, smb_command, additional_flags,
910 wct, vwv, num_bytes, bytes);
911 if (req == NULL) {
912 goto fail;
913 }
914 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
915 goto fail;
916 }
917 status = cli_smb_recv(req, NULL, NULL, min_wct, pwct, pvwv,
918 pnum_bytes, pbytes);
919fail:
920 TALLOC_FREE(ev);
921 if (NT_STATUS_IS_OK(status) && (result_parent != NULL)) {
922 *result_parent = req;
923 }
924 return status;
925}
Note: See TracBrowser for help on using the repository browser.