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