root/source3/libsmb/clientgen.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cli_set_message
  2. cli_set_timeout
  3. cli_set_port
  4. cli_ucs2
  5. client_receive_smb
  6. cli_receive_smb
  7. cli_receive_smb_data
  8. write_socket
  9. cli_send_smb
  10. cli_send_smb_direct_writeX
  11. cli_setup_packet_buf
  12. cli_setup_packet
  13. cli_setup_bcc
  14. cli_set_domain
  15. cli_set_username
  16. cli_set_password
  17. cli_init_creds
  18. cli_initialise_ex
  19. cli_initialise
  20. cli_nt_pipes_close
  21. cli_shutdown
  22. cli_sockopt
  23. cli_setpid
  24. cli_set_case_sensitive
  25. cli_send_keepalive
  26. cli_echo_recv_helper
  27. cli_echo_send
  28. cli_echo_recv
  29. cli_echo
  30. is_andx_req

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

/* [<][>][^][v][top][bottom][index][help] */