s3-build __FUNCTION__ is always available, always use it
[Samba.git] / source3 / libsmb / clientgen.c
blob096dd2f3fde05cda64a5ab9855221d831d9bd6a3
1 /*
2 Unix SMB/CIFS implementation.
3 SMB client generic functions
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2007.
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.
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.
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/>.
21 #include "includes.h"
22 #include "smb_signing.h"
23 #include "async_smb.h"
25 /*******************************************************************
26 Setup the word count and byte count for a client smb message.
27 ********************************************************************/
29 int cli_set_message(char *buf,int num_words,int num_bytes,bool zero)
31 if (zero && (num_words || num_bytes)) {
32 memset(buf + smb_size,'\0',num_words*2 + num_bytes);
34 SCVAL(buf,smb_wct,num_words);
35 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
36 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
37 return (smb_size + num_words*2 + num_bytes);
40 /****************************************************************************
41 Change the timeout (in milliseconds).
42 ****************************************************************************/
44 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
46 unsigned int old_timeout = cli->timeout;
47 cli->timeout = timeout;
48 return old_timeout;
51 /****************************************************************************
52 Change the port number used to call on.
53 ****************************************************************************/
55 void cli_set_port(struct cli_state *cli, int port)
57 cli->port = port;
60 /****************************************************************************
61 convenience routine to find if we negotiated ucs2
62 ****************************************************************************/
64 bool cli_ucs2(struct cli_state *cli)
66 return ((cli->capabilities & CAP_UNICODE) != 0);
70 /****************************************************************************
71 Read an smb from a fd ignoring all keepalive packets.
72 The timeout is in milliseconds
74 This is exactly the same as receive_smb except that it never returns
75 a session keepalive packet (just as receive_smb used to do).
76 receive_smb was changed to return keepalives as the oplock processing means this call
77 should never go into a blocking read.
78 ****************************************************************************/
80 static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
82 size_t len;
84 for(;;) {
85 NTSTATUS status;
87 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
89 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize,
90 cli->timeout, maxlen, &len);
91 if (!NT_STATUS_IS_OK(status)) {
92 DEBUG(10,("client_receive_smb failed\n"));
93 show_msg(cli->inbuf);
95 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
96 set_smb_read_error(&cli->smb_rw_error,
97 SMB_READ_EOF);
98 return -1;
101 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
102 set_smb_read_error(&cli->smb_rw_error,
103 SMB_READ_TIMEOUT);
104 return -1;
107 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
108 return -1;
112 * I don't believe len can be < 0 with NT_STATUS_OK
113 * returned above, but this check doesn't hurt. JRA.
116 if ((ssize_t)len < 0) {
117 return len;
120 /* Ignore session keepalive packets. */
121 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
122 break;
126 if (cli_encryption_on(cli)) {
127 NTSTATUS status = cli_decrypt_message(cli);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
130 nt_errstr(status)));
131 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
132 return -1;
136 show_msg(cli->inbuf);
137 return len;
140 static bool cli_state_set_seqnum(struct cli_state *cli, uint16_t mid, uint32_t seqnum)
142 struct cli_state_seqnum *c;
144 for (c = cli->seqnum; c; c = c->next) {
145 if (c->mid == mid) {
146 c->seqnum = seqnum;
147 return true;
151 c = talloc_zero(cli, struct cli_state_seqnum);
152 if (!c) {
153 return false;
156 c->mid = mid;
157 c->seqnum = seqnum;
158 c->persistent = false;
159 DLIST_ADD_END(cli->seqnum, c, struct cli_state_seqnum *);
161 return true;
164 bool cli_state_seqnum_persistent(struct cli_state *cli,
165 uint16_t mid)
167 struct cli_state_seqnum *c;
169 for (c = cli->seqnum; c; c = c->next) {
170 if (c->mid == mid) {
171 c->persistent = true;
172 return true;
176 return false;
179 bool cli_state_seqnum_remove(struct cli_state *cli,
180 uint16_t mid)
182 struct cli_state_seqnum *c;
184 for (c = cli->seqnum; c; c = c->next) {
185 if (c->mid == mid) {
186 DLIST_REMOVE(cli->seqnum, c);
187 TALLOC_FREE(c);
188 return true;
192 return false;
195 static uint32_t cli_state_get_seqnum(struct cli_state *cli, uint16_t mid)
197 struct cli_state_seqnum *c;
199 for (c = cli->seqnum; c; c = c->next) {
200 if (c->mid == mid) {
201 uint32_t seqnum = c->seqnum;
202 if (!c->persistent) {
203 DLIST_REMOVE(cli->seqnum, c);
204 TALLOC_FREE(c);
206 return seqnum;
210 return 0;
213 /****************************************************************************
214 Recv an smb.
215 ****************************************************************************/
217 bool cli_receive_smb(struct cli_state *cli)
219 ssize_t len;
220 uint16_t mid;
221 uint32_t seqnum;
223 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
224 if (cli->fd == -1)
225 return false;
227 again:
228 len = client_receive_smb(cli, 0);
230 if (len > 0) {
231 /* it might be an oplock break request */
232 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
233 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
234 SVAL(cli->inbuf,smb_vwv6) == 0 &&
235 SVAL(cli->inbuf,smb_vwv7) == 0) {
236 if (cli->oplock_handler) {
237 int fnum = SVAL(cli->inbuf,smb_vwv2);
238 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
239 if (!NT_STATUS_IS_OK(cli->oplock_handler(cli, fnum, level))) {
240 return false;
243 /* try to prevent loops */
244 SCVAL(cli->inbuf,smb_com,0xFF);
245 goto again;
249 /* If the server is not responding, note that now */
250 if (len < 0) {
252 * only log if the connection should still be open and not when
253 * the connection was closed due to a dropped ip message
255 if (cli->fd != -1) {
256 char addr[INET6_ADDRSTRLEN];
257 print_sockaddr(addr, sizeof(addr), &cli->dest_ss);
258 DEBUG(0, ("Receiving SMB: Server %s stopped responding\n",
259 addr));
260 close(cli->fd);
261 cli->fd = -1;
263 return false;
266 mid = SVAL(cli->inbuf,smb_mid);
267 seqnum = cli_state_get_seqnum(cli, mid);
269 if (!cli_check_sign_mac(cli, cli->inbuf, seqnum+1)) {
271 * If we get a signature failure in sessionsetup, then
272 * the server sometimes just reflects the sent signature
273 * back to us. Detect this and allow the upper layer to
274 * retrieve the correct Windows error message.
276 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
277 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
278 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
279 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
280 cli_is_error(cli)) {
283 * Reflected signature on login error.
284 * Set bad sig but don't close fd.
286 cli->smb_rw_error = SMB_READ_BAD_SIG;
287 return true;
290 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
291 cli->smb_rw_error = SMB_READ_BAD_SIG;
292 close(cli->fd);
293 cli->fd = -1;
294 return false;
296 return true;
299 static ssize_t write_socket(int fd, const char *buf, size_t len)
301 ssize_t ret=0;
303 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
304 ret = write_data(fd,buf,len);
306 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
307 if(ret <= 0)
308 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
309 (int)len, fd, strerror(errno) ));
311 return(ret);
314 /****************************************************************************
315 Send an smb to a fd.
316 ****************************************************************************/
318 bool cli_send_smb(struct cli_state *cli)
320 size_t len;
321 size_t nwritten=0;
322 ssize_t ret;
323 char *buf_out = cli->outbuf;
324 bool enc_on = cli_encryption_on(cli);
325 uint32_t seqnum;
327 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
328 if (cli->fd == -1)
329 return false;
331 cli_calculate_sign_mac(cli, cli->outbuf, &seqnum);
333 if (!cli_state_set_seqnum(cli, cli->mid, seqnum)) {
334 DEBUG(0,("Failed to store mid[%u]/seqnum[%u]\n",
335 (unsigned int)cli->mid,
336 (unsigned int)seqnum));
337 return false;
340 if (enc_on) {
341 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
342 &buf_out);
343 if (!NT_STATUS_IS_OK(status)) {
344 close(cli->fd);
345 cli->fd = -1;
346 cli->smb_rw_error = SMB_WRITE_ERROR;
347 DEBUG(0,("Error in encrypting client message. Error %s\n",
348 nt_errstr(status) ));
349 return false;
353 len = smb_len(buf_out) + 4;
355 while (nwritten < len) {
356 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
357 if (ret <= 0) {
358 if (enc_on) {
359 cli_free_enc_buffer(cli, buf_out);
361 close(cli->fd);
362 cli->fd = -1;
363 cli->smb_rw_error = SMB_WRITE_ERROR;
364 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
365 (int)len,(int)ret, strerror(errno) ));
366 return false;
368 nwritten += ret;
371 if (enc_on) {
372 cli_free_enc_buffer(cli, buf_out);
375 /* Increment the mid so we can tell between responses. */
376 cli->mid++;
377 if (!cli->mid)
378 cli->mid++;
379 return true;
382 /****************************************************************************
383 Send a "direct" writeX smb to a fd.
384 ****************************************************************************/
386 bool cli_send_smb_direct_writeX(struct cli_state *cli,
387 const char *p,
388 size_t extradata)
390 /* First length to send is the offset to the data. */
391 size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
392 size_t nwritten=0;
393 struct iovec iov[2];
395 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
396 if (cli->fd == -1) {
397 return false;
400 if (client_is_signing_on(cli)) {
401 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
402 return false;
405 iov[0].iov_base = (void *)cli->outbuf;
406 iov[0].iov_len = len;
407 iov[1].iov_base = CONST_DISCARD(void *, p);
408 iov[1].iov_len = extradata;
410 nwritten = write_data_iov(cli->fd, iov, 2);
411 if (nwritten < (len + extradata)) {
412 close(cli->fd);
413 cli->fd = -1;
414 cli->smb_rw_error = SMB_WRITE_ERROR;
415 DEBUG(0,("Error writing %d bytes to client. (%s)\n",
416 (int)(len+extradata), strerror(errno)));
417 return false;
420 /* Increment the mid so we can tell between responses. */
421 cli->mid++;
422 if (!cli->mid)
423 cli->mid++;
424 return true;
427 /****************************************************************************
428 Setup basics in a outgoing packet.
429 ****************************************************************************/
431 void cli_setup_packet_buf(struct cli_state *cli, char *buf)
433 uint16 flags2;
434 cli->rap_error = 0;
435 SIVAL(buf,smb_rcls,0);
436 SSVAL(buf,smb_pid,cli->pid);
437 memset(buf+smb_pidhigh, 0, 12);
438 SSVAL(buf,smb_uid,cli->vuid);
439 SSVAL(buf,smb_mid,cli->mid);
441 if (cli->protocol <= PROTOCOL_CORE) {
442 return;
445 if (cli->case_sensitive) {
446 SCVAL(buf,smb_flg,0x0);
447 } else {
448 /* Default setting, case insensitive. */
449 SCVAL(buf,smb_flg,0x8);
451 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
452 if (cli->capabilities & CAP_UNICODE)
453 flags2 |= FLAGS2_UNICODE_STRINGS;
454 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
455 flags2 |= FLAGS2_DFS_PATHNAMES;
456 if (cli->capabilities & CAP_STATUS32)
457 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
458 if (cli->use_spnego)
459 flags2 |= FLAGS2_EXTENDED_SECURITY;
460 SSVAL(buf,smb_flg2, flags2);
463 void cli_setup_packet(struct cli_state *cli)
465 cli_setup_packet_buf(cli, cli->outbuf);
468 /****************************************************************************
469 Setup the bcc length of the packet from a pointer to the end of the data.
470 ****************************************************************************/
472 void cli_setup_bcc(struct cli_state *cli, void *p)
474 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
477 /****************************************************************************
478 Initialize Domain, user or password.
479 ****************************************************************************/
481 NTSTATUS cli_set_domain(struct cli_state *cli, const char *domain)
483 TALLOC_FREE(cli->domain);
484 cli->domain = talloc_strdup(cli, domain ? domain : "");
485 if (cli->domain == NULL) {
486 return NT_STATUS_NO_MEMORY;
488 return NT_STATUS_OK;
491 NTSTATUS cli_set_username(struct cli_state *cli, const char *username)
493 TALLOC_FREE(cli->user_name);
494 cli->user_name = talloc_strdup(cli, username ? username : "");
495 if (cli->user_name == NULL) {
496 return NT_STATUS_NO_MEMORY;
498 return NT_STATUS_OK;
501 NTSTATUS cli_set_password(struct cli_state *cli, const char *password)
503 TALLOC_FREE(cli->password);
505 /* Password can be NULL. */
506 if (password) {
507 cli->password = talloc_strdup(cli, password);
508 if (cli->password == NULL) {
509 return NT_STATUS_NO_MEMORY;
511 } else {
512 /* Use zero NTLMSSP hashes and session key. */
513 cli->password = NULL;
516 return NT_STATUS_OK;
519 /****************************************************************************
520 Initialise credentials of a client structure.
521 ****************************************************************************/
523 NTSTATUS cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
525 NTSTATUS status = cli_set_username(cli, username);
526 if (!NT_STATUS_IS_OK(status)) {
527 return status;
529 status = cli_set_domain(cli, domain);
530 if (!NT_STATUS_IS_OK(status)) {
531 return status;
533 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
535 return cli_set_password(cli, password);
538 /****************************************************************************
539 Initialise a client structure. Always returns a talloc'ed struct.
540 Set the signing state (used from the command line).
541 ****************************************************************************/
543 struct cli_state *cli_initialise_ex(int signing_state)
545 struct cli_state *cli = NULL;
546 bool allow_smb_signing = false;
547 bool mandatory_signing = false;
549 /* Check the effective uid - make sure we are not setuid */
550 if (is_setuid_root()) {
551 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
552 return NULL;
555 cli = TALLOC_ZERO_P(NULL, struct cli_state);
556 if (!cli) {
557 return NULL;
560 cli->dfs_mountpoint = talloc_strdup(cli, "");
561 if (!cli->dfs_mountpoint) {
562 goto error;
564 cli->port = 0;
565 cli->fd = -1;
566 cli->cnum = -1;
567 cli->pid = (uint16)sys_getpid();
568 cli->mid = 1;
569 cli->vuid = UID_FIELD_INVALID;
570 cli->protocol = PROTOCOL_NT1;
571 cli->timeout = 20000; /* Timeout is in milliseconds. */
572 cli->bufsize = CLI_BUFFER_SIZE+4;
573 cli->max_xmit = cli->bufsize;
574 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
575 cli->seqnum = 0;
576 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
577 cli->oplock_handler = cli_oplock_ack;
578 cli->case_sensitive = false;
579 cli->smb_rw_error = SMB_READ_OK;
581 cli->use_spnego = lp_client_use_spnego();
583 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
585 /* Set the CLI_FORCE_DOSERR environment variable to test
586 client routines using DOS errors instead of STATUS32
587 ones. This intended only as a temporary hack. */
588 if (getenv("CLI_FORCE_DOSERR"))
589 cli->force_dos_errors = true;
591 if (lp_client_signing()) {
592 allow_smb_signing = true;
595 if (lp_client_signing() == Required) {
596 mandatory_signing = true;
599 if (signing_state != Undefined) {
600 allow_smb_signing = true;
603 if (signing_state == false) {
604 allow_smb_signing = false;
605 mandatory_signing = false;
608 if (signing_state == Required) {
609 mandatory_signing = true;
612 if (!cli->outbuf || !cli->inbuf)
613 goto error;
615 memset(cli->outbuf, 0, cli->bufsize);
616 memset(cli->inbuf, 0, cli->bufsize);
619 #if defined(DEVELOPER)
620 /* just because we over-allocate, doesn't mean it's right to use it */
621 clobber_region(__FUNCTION__, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
622 clobber_region(__FUNCTION__, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
623 #endif
625 /* initialise signing */
626 cli->signing_state = smb_signing_init(cli,
627 allow_smb_signing,
628 mandatory_signing);
629 if (!cli->signing_state) {
630 goto error;
633 cli->outgoing = tevent_queue_create(cli, "cli_outgoing");
634 if (cli->outgoing == NULL) {
635 goto error;
637 cli->pending = NULL;
639 cli->initialised = 1;
641 return cli;
643 /* Clean up after malloc() error */
645 error:
647 SAFE_FREE(cli->inbuf);
648 SAFE_FREE(cli->outbuf);
649 TALLOC_FREE(cli);
650 return NULL;
653 struct cli_state *cli_initialise(void)
655 return cli_initialise_ex(Undefined);
658 /****************************************************************************
659 Close all pipes open on this session.
660 ****************************************************************************/
662 void cli_nt_pipes_close(struct cli_state *cli)
664 while (cli->pipe_list != NULL) {
666 * No TALLOC_FREE here!
668 talloc_free(cli->pipe_list);
672 /****************************************************************************
673 Shutdown a client structure.
674 ****************************************************************************/
676 static void _cli_shutdown(struct cli_state *cli)
678 cli_nt_pipes_close(cli);
681 * tell our peer to free his resources. Wihtout this, when an
682 * application attempts to do a graceful shutdown and calls
683 * smbc_free_context() to clean up all connections, some connections
684 * can remain active on the peer end, until some (long) timeout period
685 * later. This tree disconnect forces the peer to clean up, since the
686 * connection will be going away.
688 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
689 * the only user for this so far is smbmount which passes opened connection
690 * down to kernel's smbfs module.
692 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
693 cli_tdis(cli);
696 SAFE_FREE(cli->outbuf);
697 SAFE_FREE(cli->inbuf);
699 data_blob_free(&cli->secblob);
700 data_blob_free(&cli->user_session_key);
702 if (cli->fd != -1) {
703 close(cli->fd);
705 cli->fd = -1;
706 cli->smb_rw_error = SMB_READ_OK;
709 * Need to free pending first, they remove themselves
711 while (cli->pending) {
712 talloc_free(cli->pending[0]);
714 TALLOC_FREE(cli);
717 void cli_shutdown(struct cli_state *cli)
719 struct cli_state *cli_head;
720 if (cli == NULL) {
721 return;
723 DLIST_HEAD(cli, cli_head);
724 if (cli_head == cli) {
726 * head of a DFS list, shutdown all subsidiary DFS
727 * connections.
729 struct cli_state *p, *next;
731 for (p = cli_head->next; p; p = next) {
732 next = p->next;
733 DLIST_REMOVE(cli_head, p);
734 _cli_shutdown(p);
736 } else {
737 DLIST_REMOVE(cli_head, cli);
740 _cli_shutdown(cli);
743 /****************************************************************************
744 Set socket options on a open connection.
745 ****************************************************************************/
747 void cli_sockopt(struct cli_state *cli, const char *options)
749 set_socket_options(cli->fd, options);
752 /****************************************************************************
753 Set the PID to use for smb messages. Return the old pid.
754 ****************************************************************************/
756 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
758 uint16 ret = cli->pid;
759 cli->pid = pid;
760 return ret;
763 /****************************************************************************
764 Set the case sensitivity flag on the packets. Returns old state.
765 ****************************************************************************/
767 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
769 bool ret = cli->case_sensitive;
770 cli->case_sensitive = case_sensitive;
771 return ret;
774 struct cli_echo_state {
775 uint16_t vwv[1];
776 DATA_BLOB data;
777 int num_echos;
780 static void cli_echo_done(struct tevent_req *subreq);
782 struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
783 struct cli_state *cli, uint16_t num_echos,
784 DATA_BLOB data)
786 struct tevent_req *req, *subreq;
787 struct cli_echo_state *state;
789 req = tevent_req_create(mem_ctx, &state, struct cli_echo_state);
790 if (req == NULL) {
791 return NULL;
793 SSVAL(state->vwv, 0, num_echos);
794 state->data = data;
795 state->num_echos = num_echos;
797 subreq = cli_smb_send(state, ev, cli, SMBecho, 0, 1, state->vwv,
798 data.length, data.data);
799 if (subreq == NULL) {
800 goto fail;
802 tevent_req_set_callback(subreq, cli_echo_done, req);
803 return req;
804 fail:
805 TALLOC_FREE(req);
806 return NULL;
809 static void cli_echo_done(struct tevent_req *subreq)
811 struct tevent_req *req = tevent_req_callback_data(
812 subreq, struct tevent_req);
813 struct cli_echo_state *state = tevent_req_data(
814 req, struct cli_echo_state);
815 NTSTATUS status;
816 uint32_t num_bytes;
817 uint8_t *bytes;
818 uint8_t *inbuf;
820 status = cli_smb_recv(subreq, state, &inbuf, 0, NULL, NULL,
821 &num_bytes, &bytes);
822 if (!NT_STATUS_IS_OK(status)) {
823 tevent_req_nterror(req, status);
824 return;
826 if ((num_bytes != state->data.length)
827 || (memcmp(bytes, state->data.data, num_bytes) != 0)) {
828 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
829 return;
832 state->num_echos -=1;
833 if (state->num_echos == 0) {
834 tevent_req_done(req);
835 return;
838 if (!cli_smb_req_set_pending(subreq)) {
839 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
840 return;
845 * Get the result out from an echo request
846 * @param[in] req The async_req from cli_echo_send
847 * @retval Did the server reply correctly?
850 NTSTATUS cli_echo_recv(struct tevent_req *req)
852 return tevent_req_simple_recv_ntstatus(req);
856 * @brief Send/Receive SMBEcho requests
857 * @param[in] mem_ctx The memory context to put the async_req on
858 * @param[in] ev The event context that will call us back
859 * @param[in] cli The connection to send the echo to
860 * @param[in] num_echos How many times do we want to get the reply?
861 * @param[in] data The data we want to get back
862 * @retval Did the server reply correctly?
865 NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
867 TALLOC_CTX *frame = talloc_stackframe();
868 struct event_context *ev;
869 struct tevent_req *req;
870 NTSTATUS status = NT_STATUS_OK;
872 if (cli_has_async_calls(cli)) {
874 * Can't use sync call while an async call is in flight
876 status = NT_STATUS_INVALID_PARAMETER;
877 goto fail;
880 ev = event_context_init(frame);
881 if (ev == NULL) {
882 status = NT_STATUS_NO_MEMORY;
883 goto fail;
886 req = cli_echo_send(frame, ev, cli, num_echos, data);
887 if (req == NULL) {
888 status = NT_STATUS_NO_MEMORY;
889 goto fail;
892 if (!tevent_req_poll(req, ev)) {
893 status = map_nt_error_from_unix(errno);
894 goto fail;
897 status = cli_echo_recv(req);
898 fail:
899 TALLOC_FREE(frame);
900 if (!NT_STATUS_IS_OK(status)) {
901 cli_set_error(cli, status);
903 return status;
907 * Is the SMB command able to hold an AND_X successor
908 * @param[in] cmd The SMB command in question
909 * @retval Can we add a chained request after "cmd"?
911 bool is_andx_req(uint8_t cmd)
913 switch (cmd) {
914 case SMBtconX:
915 case SMBlockingX:
916 case SMBopenX:
917 case SMBreadX:
918 case SMBwriteX:
919 case SMBsesssetupX:
920 case SMBulogoffX:
921 case SMBntcreateX:
922 return true;
923 break;
924 default:
925 break;
928 return false;
931 NTSTATUS cli_smb(TALLOC_CTX *mem_ctx, struct cli_state *cli,
932 uint8_t smb_command, uint8_t additional_flags,
933 uint8_t wct, uint16_t *vwv,
934 uint32_t num_bytes, const uint8_t *bytes,
935 struct tevent_req **result_parent,
936 uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
937 uint32_t *pnum_bytes, uint8_t **pbytes)
939 struct tevent_context *ev;
940 struct tevent_req *req = NULL;
941 NTSTATUS status = NT_STATUS_NO_MEMORY;
943 if (cli_has_async_calls(cli)) {
944 return NT_STATUS_INVALID_PARAMETER;
946 ev = tevent_context_init(mem_ctx);
947 if (ev == NULL) {
948 goto fail;
950 req = cli_smb_send(mem_ctx, ev, cli, smb_command, additional_flags,
951 wct, vwv, num_bytes, bytes);
952 if (req == NULL) {
953 goto fail;
955 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
956 goto fail;
958 status = cli_smb_recv(req, NULL, NULL, min_wct, pwct, pvwv,
959 pnum_bytes, pbytes);
960 fail:
961 TALLOC_FREE(ev);
962 if (NT_STATUS_IS_OK(status) && (result_parent != NULL)) {
963 *result_parent = req;
965 return status;