midltests: add some usefull defines to midltests.idl
[Samba.git] / source3 / libsmb / clientgen.c
blob2c49a8b3f0e8771751a4aef16fc9d387d57139a6
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) {
251 char addr[INET6_ADDRSTRLEN];
253 print_sockaddr(addr, sizeof(addr), &cli->dest_ss);
254 DEBUG(0, ("Receiving SMB: Server %s stopped responding\n",
255 addr));
256 close(cli->fd);
257 cli->fd = -1;
258 return false;
261 mid = SVAL(cli->inbuf,smb_mid);
262 seqnum = cli_state_get_seqnum(cli, mid);
264 if (!cli_check_sign_mac(cli, cli->inbuf, seqnum+1)) {
266 * If we get a signature failure in sessionsetup, then
267 * the server sometimes just reflects the sent signature
268 * back to us. Detect this and allow the upper layer to
269 * retrieve the correct Windows error message.
271 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
272 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
273 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
274 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
275 cli_is_error(cli)) {
278 * Reflected signature on login error.
279 * Set bad sig but don't close fd.
281 cli->smb_rw_error = SMB_READ_BAD_SIG;
282 return true;
285 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
286 cli->smb_rw_error = SMB_READ_BAD_SIG;
287 close(cli->fd);
288 cli->fd = -1;
289 return false;
291 return true;
294 static ssize_t write_socket(int fd, const char *buf, size_t len)
296 ssize_t ret=0;
298 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
299 ret = write_data(fd,buf,len);
301 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
302 if(ret <= 0)
303 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
304 (int)len, fd, strerror(errno) ));
306 return(ret);
309 /****************************************************************************
310 Send an smb to a fd.
311 ****************************************************************************/
313 bool cli_send_smb(struct cli_state *cli)
315 size_t len;
316 size_t nwritten=0;
317 ssize_t ret;
318 char *buf_out = cli->outbuf;
319 bool enc_on = cli_encryption_on(cli);
320 uint32_t seqnum;
322 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
323 if (cli->fd == -1)
324 return false;
326 cli_calculate_sign_mac(cli, cli->outbuf, &seqnum);
328 if (!cli_state_set_seqnum(cli, cli->mid, seqnum)) {
329 DEBUG(0,("Failed to store mid[%u]/seqnum[%u]\n",
330 (unsigned int)cli->mid,
331 (unsigned int)seqnum));
332 return false;
335 if (enc_on) {
336 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
337 &buf_out);
338 if (!NT_STATUS_IS_OK(status)) {
339 close(cli->fd);
340 cli->fd = -1;
341 cli->smb_rw_error = SMB_WRITE_ERROR;
342 DEBUG(0,("Error in encrypting client message. Error %s\n",
343 nt_errstr(status) ));
344 return false;
348 len = smb_len(buf_out) + 4;
350 while (nwritten < len) {
351 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
352 if (ret <= 0) {
353 if (enc_on) {
354 cli_free_enc_buffer(cli, buf_out);
356 close(cli->fd);
357 cli->fd = -1;
358 cli->smb_rw_error = SMB_WRITE_ERROR;
359 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
360 (int)len,(int)ret, strerror(errno) ));
361 return false;
363 nwritten += ret;
366 if (enc_on) {
367 cli_free_enc_buffer(cli, buf_out);
370 /* Increment the mid so we can tell between responses. */
371 cli->mid++;
372 if (!cli->mid)
373 cli->mid++;
374 return true;
377 /****************************************************************************
378 Send a "direct" writeX smb to a fd.
379 ****************************************************************************/
381 bool cli_send_smb_direct_writeX(struct cli_state *cli,
382 const char *p,
383 size_t extradata)
385 /* First length to send is the offset to the data. */
386 size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
387 size_t nwritten=0;
388 struct iovec iov[2];
390 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
391 if (cli->fd == -1) {
392 return false;
395 if (client_is_signing_on(cli)) {
396 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
397 return false;
400 iov[0].iov_base = (void *)cli->outbuf;
401 iov[0].iov_len = len;
402 iov[1].iov_base = CONST_DISCARD(void *, p);
403 iov[1].iov_len = extradata;
405 nwritten = write_data_iov(cli->fd, iov, 2);
406 if (nwritten < (len + extradata)) {
407 close(cli->fd);
408 cli->fd = -1;
409 cli->smb_rw_error = SMB_WRITE_ERROR;
410 DEBUG(0,("Error writing %d bytes to client. (%s)\n",
411 (int)(len+extradata), strerror(errno)));
412 return false;
415 /* Increment the mid so we can tell between responses. */
416 cli->mid++;
417 if (!cli->mid)
418 cli->mid++;
419 return true;
422 /****************************************************************************
423 Setup basics in a outgoing packet.
424 ****************************************************************************/
426 void cli_setup_packet_buf(struct cli_state *cli, char *buf)
428 uint16 flags2;
429 cli->rap_error = 0;
430 SIVAL(buf,smb_rcls,0);
431 SSVAL(buf,smb_pid,cli->pid);
432 memset(buf+smb_pidhigh, 0, 12);
433 SSVAL(buf,smb_uid,cli->vuid);
434 SSVAL(buf,smb_mid,cli->mid);
436 if (cli->protocol <= PROTOCOL_CORE) {
437 return;
440 if (cli->case_sensitive) {
441 SCVAL(buf,smb_flg,0x0);
442 } else {
443 /* Default setting, case insensitive. */
444 SCVAL(buf,smb_flg,0x8);
446 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
447 if (cli->capabilities & CAP_UNICODE)
448 flags2 |= FLAGS2_UNICODE_STRINGS;
449 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
450 flags2 |= FLAGS2_DFS_PATHNAMES;
451 if (cli->capabilities & CAP_STATUS32)
452 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
453 if (cli->use_spnego)
454 flags2 |= FLAGS2_EXTENDED_SECURITY;
455 SSVAL(buf,smb_flg2, flags2);
458 void cli_setup_packet(struct cli_state *cli)
460 cli_setup_packet_buf(cli, cli->outbuf);
463 /****************************************************************************
464 Setup the bcc length of the packet from a pointer to the end of the data.
465 ****************************************************************************/
467 void cli_setup_bcc(struct cli_state *cli, void *p)
469 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
472 /****************************************************************************
473 Initialize Domain, user or password.
474 ****************************************************************************/
476 NTSTATUS cli_set_domain(struct cli_state *cli, const char *domain)
478 TALLOC_FREE(cli->domain);
479 cli->domain = talloc_strdup(cli, domain ? domain : "");
480 if (cli->domain == NULL) {
481 return NT_STATUS_NO_MEMORY;
483 return NT_STATUS_OK;
486 NTSTATUS cli_set_username(struct cli_state *cli, const char *username)
488 TALLOC_FREE(cli->user_name);
489 cli->user_name = talloc_strdup(cli, username ? username : "");
490 if (cli->user_name == NULL) {
491 return NT_STATUS_NO_MEMORY;
493 return NT_STATUS_OK;
496 NTSTATUS cli_set_password(struct cli_state *cli, const char *password)
498 TALLOC_FREE(cli->password);
500 /* Password can be NULL. */
501 if (password) {
502 cli->password = talloc_strdup(cli, password);
503 if (cli->password == NULL) {
504 return NT_STATUS_NO_MEMORY;
506 } else {
507 /* Use zero NTLMSSP hashes and session key. */
508 cli->password = NULL;
511 return NT_STATUS_OK;
514 /****************************************************************************
515 Initialise credentials of a client structure.
516 ****************************************************************************/
518 NTSTATUS cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
520 NTSTATUS status = cli_set_username(cli, username);
521 if (!NT_STATUS_IS_OK(status)) {
522 return status;
524 status = cli_set_domain(cli, domain);
525 if (!NT_STATUS_IS_OK(status)) {
526 return status;
528 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
530 return cli_set_password(cli, password);
533 /****************************************************************************
534 Initialise a client structure. Always returns a talloc'ed struct.
535 Set the signing state (used from the command line).
536 ****************************************************************************/
538 struct cli_state *cli_initialise_ex(int signing_state)
540 struct cli_state *cli = NULL;
541 bool allow_smb_signing = false;
542 bool mandatory_signing = false;
544 /* Check the effective uid - make sure we are not setuid */
545 if (is_setuid_root()) {
546 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
547 return NULL;
550 cli = TALLOC_ZERO_P(NULL, struct cli_state);
551 if (!cli) {
552 return NULL;
555 cli->dfs_mountpoint = talloc_strdup(cli, "");
556 if (!cli->dfs_mountpoint) {
557 goto error;
559 cli->port = 0;
560 cli->fd = -1;
561 cli->cnum = -1;
562 cli->pid = (uint16)sys_getpid();
563 cli->mid = 1;
564 cli->vuid = UID_FIELD_INVALID;
565 cli->protocol = PROTOCOL_NT1;
566 cli->timeout = 20000; /* Timeout is in milliseconds. */
567 cli->bufsize = CLI_BUFFER_SIZE+4;
568 cli->max_xmit = cli->bufsize;
569 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
570 cli->seqnum = 0;
571 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
572 cli->oplock_handler = cli_oplock_ack;
573 cli->case_sensitive = false;
574 cli->smb_rw_error = SMB_READ_OK;
576 cli->use_spnego = lp_client_use_spnego();
578 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
580 /* Set the CLI_FORCE_DOSERR environment variable to test
581 client routines using DOS errors instead of STATUS32
582 ones. This intended only as a temporary hack. */
583 if (getenv("CLI_FORCE_DOSERR"))
584 cli->force_dos_errors = true;
586 if (lp_client_signing()) {
587 allow_smb_signing = true;
590 if (lp_client_signing() == Required) {
591 mandatory_signing = true;
594 if (signing_state != Undefined) {
595 allow_smb_signing = true;
598 if (signing_state == false) {
599 allow_smb_signing = false;
600 mandatory_signing = false;
603 if (signing_state == Required) {
604 mandatory_signing = true;
607 if (!cli->outbuf || !cli->inbuf)
608 goto error;
610 memset(cli->outbuf, 0, cli->bufsize);
611 memset(cli->inbuf, 0, cli->bufsize);
614 #if defined(DEVELOPER)
615 /* just because we over-allocate, doesn't mean it's right to use it */
616 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
617 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
618 #endif
620 /* initialise signing */
621 cli->signing_state = smb_signing_init(cli,
622 allow_smb_signing,
623 mandatory_signing);
624 if (!cli->signing_state) {
625 goto error;
628 cli->outgoing = tevent_queue_create(cli, "cli_outgoing");
629 if (cli->outgoing == NULL) {
630 goto error;
632 cli->pending = NULL;
634 cli->initialised = 1;
636 return cli;
638 /* Clean up after malloc() error */
640 error:
642 SAFE_FREE(cli->inbuf);
643 SAFE_FREE(cli->outbuf);
644 TALLOC_FREE(cli);
645 return NULL;
648 struct cli_state *cli_initialise(void)
650 return cli_initialise_ex(Undefined);
653 /****************************************************************************
654 Close all pipes open on this session.
655 ****************************************************************************/
657 void cli_nt_pipes_close(struct cli_state *cli)
659 while (cli->pipe_list != NULL) {
661 * No TALLOC_FREE here!
663 talloc_free(cli->pipe_list);
667 /****************************************************************************
668 Shutdown a client structure.
669 ****************************************************************************/
671 static void _cli_shutdown(struct cli_state *cli)
673 cli_nt_pipes_close(cli);
676 * tell our peer to free his resources. Wihtout this, when an
677 * application attempts to do a graceful shutdown and calls
678 * smbc_free_context() to clean up all connections, some connections
679 * can remain active on the peer end, until some (long) timeout period
680 * later. This tree disconnect forces the peer to clean up, since the
681 * connection will be going away.
683 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
684 * the only user for this so far is smbmount which passes opened connection
685 * down to kernel's smbfs module.
687 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
688 cli_tdis(cli);
691 SAFE_FREE(cli->outbuf);
692 SAFE_FREE(cli->inbuf);
694 data_blob_free(&cli->secblob);
695 data_blob_free(&cli->user_session_key);
697 if (cli->fd != -1) {
698 close(cli->fd);
700 cli->fd = -1;
701 cli->smb_rw_error = SMB_READ_OK;
704 * Need to free pending first, they remove themselves
706 while (cli->pending) {
707 talloc_free(cli->pending[0]);
709 TALLOC_FREE(cli);
712 void cli_shutdown(struct cli_state *cli)
714 struct cli_state *cli_head;
715 if (cli == NULL) {
716 return;
718 DLIST_HEAD(cli, cli_head);
719 if (cli_head == cli) {
721 * head of a DFS list, shutdown all subsidiary DFS
722 * connections.
724 struct cli_state *p, *next;
726 for (p = cli_head->next; p; p = next) {
727 next = p->next;
728 DLIST_REMOVE(cli_head, p);
729 _cli_shutdown(p);
731 } else {
732 DLIST_REMOVE(cli_head, cli);
735 _cli_shutdown(cli);
738 /****************************************************************************
739 Set socket options on a open connection.
740 ****************************************************************************/
742 void cli_sockopt(struct cli_state *cli, const char *options)
744 set_socket_options(cli->fd, options);
747 /****************************************************************************
748 Set the PID to use for smb messages. Return the old pid.
749 ****************************************************************************/
751 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
753 uint16 ret = cli->pid;
754 cli->pid = pid;
755 return ret;
758 /****************************************************************************
759 Set the case sensitivity flag on the packets. Returns old state.
760 ****************************************************************************/
762 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
764 bool ret = cli->case_sensitive;
765 cli->case_sensitive = case_sensitive;
766 return ret;
769 /****************************************************************************
770 Send a keepalive packet to the server
771 ****************************************************************************/
773 bool cli_send_keepalive(struct cli_state *cli)
775 if (cli->fd == -1) {
776 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
777 return false;
779 if (!send_keepalive(cli->fd)) {
780 close(cli->fd);
781 cli->fd = -1;
782 DEBUG(0,("Error sending keepalive packet to client.\n"));
783 return false;
785 return true;
788 struct cli_echo_state {
789 uint16_t vwv[1];
790 DATA_BLOB data;
791 int num_echos;
794 static void cli_echo_done(struct tevent_req *subreq);
796 struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
797 struct cli_state *cli, uint16_t num_echos,
798 DATA_BLOB data)
800 struct tevent_req *req, *subreq;
801 struct cli_echo_state *state;
803 req = tevent_req_create(mem_ctx, &state, struct cli_echo_state);
804 if (req == NULL) {
805 return NULL;
807 SSVAL(state->vwv, 0, num_echos);
808 state->data = data;
809 state->num_echos = num_echos;
811 subreq = cli_smb_send(state, ev, cli, SMBecho, 0, 1, state->vwv,
812 data.length, data.data);
813 if (subreq == NULL) {
814 goto fail;
816 tevent_req_set_callback(subreq, cli_echo_done, req);
817 return req;
818 fail:
819 TALLOC_FREE(req);
820 return NULL;
823 static void cli_echo_done(struct tevent_req *subreq)
825 struct tevent_req *req = tevent_req_callback_data(
826 subreq, struct tevent_req);
827 struct cli_echo_state *state = tevent_req_data(
828 req, struct cli_echo_state);
829 NTSTATUS status;
830 uint32_t num_bytes;
831 uint8_t *bytes;
832 uint8_t *inbuf;
834 status = cli_smb_recv(subreq, state, &inbuf, 0, NULL, NULL,
835 &num_bytes, &bytes);
836 if (!NT_STATUS_IS_OK(status)) {
837 tevent_req_nterror(req, status);
838 return;
840 if ((num_bytes != state->data.length)
841 || (memcmp(bytes, state->data.data, num_bytes) != 0)) {
842 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
843 return;
846 state->num_echos -=1;
847 if (state->num_echos == 0) {
848 tevent_req_done(req);
849 return;
852 if (!cli_smb_req_set_pending(subreq)) {
853 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
854 return;
859 * Get the result out from an echo request
860 * @param[in] req The async_req from cli_echo_send
861 * @retval Did the server reply correctly?
864 NTSTATUS cli_echo_recv(struct tevent_req *req)
866 return tevent_req_simple_recv_ntstatus(req);
870 * @brief Send/Receive SMBEcho requests
871 * @param[in] mem_ctx The memory context to put the async_req on
872 * @param[in] ev The event context that will call us back
873 * @param[in] cli The connection to send the echo to
874 * @param[in] num_echos How many times do we want to get the reply?
875 * @param[in] data The data we want to get back
876 * @retval Did the server reply correctly?
879 NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
881 TALLOC_CTX *frame = talloc_stackframe();
882 struct event_context *ev;
883 struct tevent_req *req;
884 NTSTATUS status = NT_STATUS_OK;
886 if (cli_has_async_calls(cli)) {
888 * Can't use sync call while an async call is in flight
890 status = NT_STATUS_INVALID_PARAMETER;
891 goto fail;
894 ev = event_context_init(frame);
895 if (ev == NULL) {
896 status = NT_STATUS_NO_MEMORY;
897 goto fail;
900 req = cli_echo_send(frame, ev, cli, num_echos, data);
901 if (req == NULL) {
902 status = NT_STATUS_NO_MEMORY;
903 goto fail;
906 if (!tevent_req_poll(req, ev)) {
907 status = map_nt_error_from_unix(errno);
908 goto fail;
911 status = cli_echo_recv(req);
912 fail:
913 TALLOC_FREE(frame);
914 if (!NT_STATUS_IS_OK(status)) {
915 cli_set_error(cli, status);
917 return status;
921 * Is the SMB command able to hold an AND_X successor
922 * @param[in] cmd The SMB command in question
923 * @retval Can we add a chained request after "cmd"?
925 bool is_andx_req(uint8_t cmd)
927 switch (cmd) {
928 case SMBtconX:
929 case SMBlockingX:
930 case SMBopenX:
931 case SMBreadX:
932 case SMBwriteX:
933 case SMBsesssetupX:
934 case SMBulogoffX:
935 case SMBntcreateX:
936 return true;
937 break;
938 default:
939 break;
942 return false;
945 NTSTATUS cli_smb(TALLOC_CTX *mem_ctx, struct cli_state *cli,
946 uint8_t smb_command, uint8_t additional_flags,
947 uint8_t wct, uint16_t *vwv,
948 uint32_t num_bytes, const uint8_t *bytes,
949 struct tevent_req **result_parent,
950 uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
951 uint32_t *pnum_bytes, uint8_t **pbytes)
953 struct tevent_context *ev;
954 struct tevent_req *req = NULL;
955 NTSTATUS status = NT_STATUS_NO_MEMORY;
957 if (cli_has_async_calls(cli)) {
958 return NT_STATUS_INVALID_PARAMETER;
960 ev = tevent_context_init(mem_ctx);
961 if (ev == NULL) {
962 goto fail;
964 req = cli_smb_send(mem_ctx, ev, cli, smb_command, additional_flags,
965 wct, vwv, num_bytes, bytes);
966 if (req == NULL) {
967 goto fail;
969 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
970 goto fail;
972 status = cli_smb_recv(req, NULL, NULL, min_wct, pwct, pvwv,
973 pnum_bytes, pbytes);
974 fail:
975 TALLOC_FREE(ev);
976 if (NT_STATUS_IS_OK(status)) {
977 *result_parent = req;
979 return status;