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/>.
23 /*******************************************************************
24 Setup the word count and byte count for a client smb message.
25 ********************************************************************/
27 int cli_set_message(char *buf
,int num_words
,int num_bytes
,bool zero
)
29 if (zero
&& (num_words
|| num_bytes
)) {
30 memset(buf
+ smb_size
,'\0',num_words
*2 + num_bytes
);
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
);
38 /****************************************************************************
39 Change the timeout (in milliseconds).
40 ****************************************************************************/
42 unsigned int cli_set_timeout(struct cli_state
*cli
, unsigned int timeout
)
44 unsigned int old_timeout
= cli
->timeout
;
45 cli
->timeout
= timeout
;
49 /****************************************************************************
50 Change the port number used to call on.
51 ****************************************************************************/
53 int cli_set_port(struct cli_state
*cli
, int port
)
59 /****************************************************************************
60 Read an smb from a fd ignoring all keepalive packets. Note that the buffer
61 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
62 The timeout is in milliseconds
64 This is exactly the same as receive_smb except that it never returns
65 a session keepalive packet (just as receive_smb used to do).
66 receive_smb was changed to return keepalives as the oplock processing means this call
67 should never go into a blocking read.
68 ****************************************************************************/
70 static ssize_t
client_receive_smb(struct cli_state
*cli
, size_t maxlen
)
75 len
= receive_smb_raw(cli
->fd
, cli
->inbuf
, cli
->timeout
,
76 maxlen
, &cli
->smb_rw_error
);
79 DEBUG(10,("client_receive_smb failed\n"));
84 /* Ignore session keepalive packets. */
85 if(CVAL(cli
->inbuf
,0) != SMBkeepalive
) {
90 if (cli_encryption_on(cli
)) {
91 NTSTATUS status
= cli_decrypt_message(cli
);
92 if (!NT_STATUS_IS_OK(status
)) {
93 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
95 cli
->smb_rw_error
= SMB_READ_BAD_DECRYPT
;
100 show_msg(cli
->inbuf
);
104 /****************************************************************************
106 ****************************************************************************/
108 bool cli_receive_smb(struct cli_state
*cli
)
112 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
117 len
= client_receive_smb(cli
, 0);
120 /* it might be an oplock break request */
121 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) &&
122 CVAL(cli
->inbuf
,smb_com
) == SMBlockingX
&&
123 SVAL(cli
->inbuf
,smb_vwv6
) == 0 &&
124 SVAL(cli
->inbuf
,smb_vwv7
) == 0) {
125 if (cli
->oplock_handler
) {
126 int fnum
= SVAL(cli
->inbuf
,smb_vwv2
);
127 unsigned char level
= CVAL(cli
->inbuf
,smb_vwv3
+1);
128 if (!cli
->oplock_handler(cli
, fnum
, level
)) {
132 /* try to prevent loops */
133 SCVAL(cli
->inbuf
,smb_com
,0xFF);
138 /* If the server is not responding, note that now */
140 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
146 if (!cli_check_sign_mac(cli
)) {
148 * If we get a signature failure in sessionsetup, then
149 * the server sometimes just reflects the sent signature
150 * back to us. Detect this and allow the upper layer to
151 * retrieve the correct Windows error message.
153 if (CVAL(cli
->outbuf
,smb_com
) == SMBsesssetupX
&&
154 (smb_len(cli
->inbuf
) > (smb_ss_field
+ 8 - 4)) &&
155 (SVAL(cli
->inbuf
,smb_flg2
) & FLAGS2_SMB_SECURITY_SIGNATURES
) &&
156 memcmp(&cli
->outbuf
[smb_ss_field
],&cli
->inbuf
[smb_ss_field
],8) == 0 &&
160 * Reflected signature on login error.
161 * Set bad sig but don't close fd.
163 cli
->smb_rw_error
= SMB_READ_BAD_SIG
;
167 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
168 cli
->smb_rw_error
= SMB_READ_BAD_SIG
;
176 /****************************************************************************
177 Read the data portion of a readX smb.
178 The timeout is in milliseconds
179 ****************************************************************************/
181 ssize_t
cli_receive_smb_data(struct cli_state
*cli
, char *buffer
, size_t len
)
183 return read_socket_with_timeout(cli
->fd
, buffer
, len
, len
,
184 cli
->timeout
, &cli
->smb_rw_error
);
187 /****************************************************************************
188 Read a smb readX header.
189 We can only use this if encryption and signing are off.
190 ****************************************************************************/
192 bool cli_receive_smb_readX_header(struct cli_state
*cli
)
201 /* Read up to the size of a readX header reply. */
202 len
= client_receive_smb(cli
, (smb_size
- 4) + 24);
205 /* it might be an oplock break request */
206 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) &&
207 CVAL(cli
->inbuf
,smb_com
) == SMBlockingX
&&
208 SVAL(cli
->inbuf
,smb_vwv6
) == 0 &&
209 SVAL(cli
->inbuf
,smb_vwv7
) == 0) {
210 ssize_t total_len
= smb_len(cli
->inbuf
);
212 if (total_len
> CLI_SAMBA_MAX_LARGE_READX_SIZE
+SAFETY_MARGIN
) {
216 /* Read the rest of the data. */
217 if ((total_len
- len
> 0) &&
218 !cli_receive_smb_data(cli
,cli
->inbuf
+len
,total_len
- len
)) {
222 if (cli
->oplock_handler
) {
223 int fnum
= SVAL(cli
->inbuf
,smb_vwv2
);
224 unsigned char level
= CVAL(cli
->inbuf
,smb_vwv3
+1);
225 if (!cli
->oplock_handler(cli
, fnum
, level
)) return false;
227 /* try to prevent loops */
228 SCVAL(cli
->inbuf
,smb_com
,0xFF);
233 /* If it's not the above size it probably was an error packet. */
235 if ((len
== (smb_size
- 4) + 24) && !cli_is_error(cli
)) {
236 /* Check it's a non-chained readX reply. */
237 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) ||
238 (CVAL(cli
->inbuf
,smb_vwv0
) != 0xFF) ||
239 (CVAL(cli
->inbuf
,smb_com
) != SMBreadX
)) {
241 * We're not coping here with asnyc replies to
242 * other calls. Punt here - we need async client
249 * We know it's a readX reply - ensure we've read the
250 * padding bytes also.
253 offset
= SVAL(cli
->inbuf
,smb_vwv6
);
256 size_t padbytes
= offset
- len
;
257 ret
= cli_receive_smb_data(cli
,smb_buf(cli
->inbuf
),padbytes
);
258 if (ret
!= padbytes
) {
268 cli
->smb_rw_error
= SMB_READ_ERROR
;
274 static ssize_t
write_socket(int fd
, const char *buf
, size_t len
)
278 DEBUG(6,("write_socket(%d,%d)\n",fd
,(int)len
));
279 ret
= write_data(fd
,buf
,len
);
281 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd
,(int)len
,(int)ret
));
283 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
284 (int)len
, fd
, strerror(errno
) ));
289 /****************************************************************************
291 ****************************************************************************/
293 bool cli_send_smb(struct cli_state
*cli
)
298 char *buf_out
= cli
->outbuf
;
299 bool enc_on
= cli_encryption_on(cli
);
301 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
305 cli_calculate_sign_mac(cli
);
308 NTSTATUS status
= cli_encrypt_message(cli
, &buf_out
);
309 if (!NT_STATUS_IS_OK(status
)) {
312 cli
->smb_rw_error
= SMB_WRITE_ERROR
;
313 DEBUG(0,("Error in encrypting client message. Error %s\n",
314 nt_errstr(status
) ));
319 len
= smb_len(buf_out
) + 4;
321 while (nwritten
< len
) {
322 ret
= write_socket(cli
->fd
,buf_out
+nwritten
,len
- nwritten
);
325 cli_free_enc_buffer(cli
, buf_out
);
329 cli
->smb_rw_error
= SMB_WRITE_ERROR
;
330 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
331 (int)len
,(int)ret
, strerror(errno
) ));
338 cli_free_enc_buffer(cli
, buf_out
);
341 /* Increment the mid so we can tell between responses. */
348 /****************************************************************************
349 Send a "direct" writeX smb to a fd.
350 ****************************************************************************/
352 bool cli_send_smb_direct_writeX(struct cli_state
*cli
,
356 /* First length to send is the offset to the data. */
357 size_t len
= SVAL(cli
->outbuf
,smb_vwv11
) + 4;
361 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
366 if (client_is_signing_on(cli
)) {
367 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
371 while (nwritten
< len
) {
372 ret
= write_socket(cli
->fd
,cli
->outbuf
+nwritten
,len
- nwritten
);
376 cli
->smb_rw_error
= SMB_WRITE_ERROR
;
377 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
378 (int)len
,(int)ret
, strerror(errno
) ));
384 /* Now write the extra data. */
386 while (nwritten
< extradata
) {
387 ret
= write_socket(cli
->fd
,p
+nwritten
,extradata
- nwritten
);
391 cli
->smb_rw_error
= SMB_WRITE_ERROR
;
392 DEBUG(0,("Error writing %d extradata "
393 "bytes to client. %d (%s)\n",
394 (int)extradata
,(int)ret
, strerror(errno
) ));
400 /* Increment the mid so we can tell between responses. */
407 /****************************************************************************
408 Setup basics in a outgoing packet.
409 ****************************************************************************/
411 void cli_setup_packet(struct cli_state
*cli
)
414 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
415 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
416 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
417 if (cli
->protocol
> PROTOCOL_CORE
) {
419 if (cli
->case_sensitive
) {
420 SCVAL(cli
->outbuf
,smb_flg
,0x0);
422 /* Default setting, case insensitive. */
423 SCVAL(cli
->outbuf
,smb_flg
,0x8);
425 flags2
= FLAGS2_LONG_PATH_COMPONENTS
;
426 if (cli
->capabilities
& CAP_UNICODE
)
427 flags2
|= FLAGS2_UNICODE_STRINGS
;
428 if ((cli
->capabilities
& CAP_DFS
) && cli
->dfsroot
)
429 flags2
|= FLAGS2_DFS_PATHNAMES
;
430 if (cli
->capabilities
& CAP_STATUS32
)
431 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
433 flags2
|= FLAGS2_EXTENDED_SECURITY
;
434 SSVAL(cli
->outbuf
,smb_flg2
, flags2
);
438 /****************************************************************************
439 Setup the bcc length of the packet from a pointer to the end of the data.
440 ****************************************************************************/
442 void cli_setup_bcc(struct cli_state
*cli
, void *p
)
444 set_message_bcc(cli
->outbuf
, PTR_DIFF(p
, smb_buf(cli
->outbuf
)));
447 /****************************************************************************
448 Initialise credentials of a client structure.
449 ****************************************************************************/
451 void cli_init_creds(struct cli_state
*cli
, const char *username
, const char *domain
, const char *password
)
453 fstrcpy(cli
->domain
, domain
);
454 fstrcpy(cli
->user_name
, username
);
455 pwd_set_cleartext(&cli
->pwd
, password
);
457 cli
->pwd
.null_pwd
= true;
460 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli
->user_name
, cli
->domain
));
463 /****************************************************************************
464 Set the signing state (used from the command line).
465 ****************************************************************************/
467 void cli_setup_signing_state(struct cli_state
*cli
, int signing_state
)
469 if (signing_state
== Undefined
)
472 if (signing_state
== false) {
473 cli
->sign_info
.allow_smb_signing
= false;
474 cli
->sign_info
.mandatory_signing
= false;
478 cli
->sign_info
.allow_smb_signing
= true;
480 if (signing_state
== Required
)
481 cli
->sign_info
.mandatory_signing
= true;
484 /****************************************************************************
485 Initialise a client structure. Always returns a malloc'ed struct.
486 ****************************************************************************/
488 struct cli_state
*cli_initialise(void)
490 struct cli_state
*cli
= NULL
;
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"));
498 cli
= SMB_MALLOC_P(struct cli_state
);
508 cli
->pid
= (uint16
)sys_getpid();
510 cli
->vuid
= UID_FIELD_INVALID
;
511 cli
->protocol
= PROTOCOL_NT1
;
512 cli
->timeout
= 20000; /* Timeout is in milliseconds. */
513 cli
->bufsize
= CLI_BUFFER_SIZE
+4;
514 cli
->max_xmit
= cli
->bufsize
;
515 cli
->outbuf
= (char *)SMB_MALLOC(cli
->bufsize
+SAFETY_MARGIN
);
516 cli
->inbuf
= (char *)SMB_MALLOC(cli
->bufsize
+SAFETY_MARGIN
);
517 cli
->oplock_handler
= cli_oplock_ack
;
518 cli
->case_sensitive
= false;
519 cli
->smb_rw_error
= SMB_READ_OK
;
521 cli
->use_spnego
= lp_client_use_spnego();
523 cli
->capabilities
= CAP_UNICODE
| CAP_STATUS32
| CAP_DFS
;
525 /* Set the CLI_FORCE_DOSERR environment variable to test
526 client routines using DOS errors instead of STATUS32
527 ones. This intended only as a temporary hack. */
528 if (getenv("CLI_FORCE_DOSERR"))
529 cli
->force_dos_errors
= true;
531 if (lp_client_signing())
532 cli
->sign_info
.allow_smb_signing
= true;
534 if (lp_client_signing() == Required
)
535 cli
->sign_info
.mandatory_signing
= true;
537 if (!cli
->outbuf
|| !cli
->inbuf
)
540 memset(cli
->outbuf
, 0, cli
->bufsize
);
541 memset(cli
->inbuf
, 0, cli
->bufsize
);
544 #if defined(DEVELOPER)
545 /* just because we over-allocate, doesn't mean it's right to use it */
546 clobber_region(FUNCTION_MACRO
, __LINE__
, cli
->outbuf
+cli
->bufsize
, SAFETY_MARGIN
);
547 clobber_region(FUNCTION_MACRO
, __LINE__
, cli
->inbuf
+cli
->bufsize
, SAFETY_MARGIN
);
550 /* initialise signing */
551 cli_null_set_signing(cli
);
553 cli
->initialised
= 1;
557 /* Clean up after malloc() error */
561 SAFE_FREE(cli
->inbuf
);
562 SAFE_FREE(cli
->outbuf
);
567 /****************************************************************************
569 Close an open named pipe over SMB. Free any authentication data.
570 Returns false if the cli_close call failed.
571 ****************************************************************************/
573 bool cli_rpc_pipe_close(struct rpc_pipe_client
*cli
)
581 ret
= cli_close(cli
->cli
, cli
->fnum
);
584 DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
586 "to machine %s. Error was %s\n",
590 cli_errstr(cli
->cli
)));
593 if (cli
->auth
.cli_auth_data_free_func
) {
594 (*cli
->auth
.cli_auth_data_free_func
)(&cli
->auth
);
597 DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
598 cli
->pipe_name
, cli
->cli
->desthost
));
600 DLIST_REMOVE(cli
->cli
->pipe_list
, cli
);
601 talloc_destroy(cli
->mem_ctx
);
605 /****************************************************************************
606 Close all pipes open on this session.
607 ****************************************************************************/
609 void cli_nt_pipes_close(struct cli_state
*cli
)
611 struct rpc_pipe_client
*cp
, *next
;
613 for (cp
= cli
->pipe_list
; cp
; cp
= next
) {
615 cli_rpc_pipe_close(cp
);
619 /****************************************************************************
620 Shutdown a client structure.
621 ****************************************************************************/
623 void cli_shutdown(struct cli_state
*cli
)
625 cli_nt_pipes_close(cli
);
628 * tell our peer to free his resources. Wihtout this, when an
629 * application attempts to do a graceful shutdown and calls
630 * smbc_free_context() to clean up all connections, some connections
631 * can remain active on the peer end, until some (long) timeout period
632 * later. This tree disconnect forces the peer to clean up, since the
633 * connection will be going away.
635 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
636 * the only user for this so far is smbmount which passes opened connection
637 * down to kernel's smbfs module.
639 if ( (cli
->cnum
!= (uint16
)-1) && (cli
->smb_rw_error
!= SMB_DO_NOT_DO_TDIS
) ) {
643 SAFE_FREE(cli
->outbuf
);
644 SAFE_FREE(cli
->inbuf
);
646 cli_free_signing_context(cli
);
647 data_blob_free(&cli
->secblob
);
648 data_blob_free(&cli
->user_session_key
);
654 cli
->smb_rw_error
= SMB_READ_OK
;
659 /****************************************************************************
660 Set socket options on a open connection.
661 ****************************************************************************/
663 void cli_sockopt(struct cli_state
*cli
, const char *options
)
665 set_socket_options(cli
->fd
, options
);
668 /****************************************************************************
669 Set the PID to use for smb messages. Return the old pid.
670 ****************************************************************************/
672 uint16
cli_setpid(struct cli_state
*cli
, uint16 pid
)
674 uint16 ret
= cli
->pid
;
679 /****************************************************************************
680 Set the case sensitivity flag on the packets. Returns old state.
681 ****************************************************************************/
683 bool cli_set_case_sensitive(struct cli_state
*cli
, bool case_sensitive
)
685 bool ret
= cli
->case_sensitive
;
686 cli
->case_sensitive
= case_sensitive
;
690 /****************************************************************************
691 Send a keepalive packet to the server
692 ****************************************************************************/
694 bool cli_send_keepalive(struct cli_state
*cli
)
697 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
700 if (!send_keepalive(cli
->fd
)) {
703 DEBUG(0,("Error sending keepalive packet to client.\n"));
709 /****************************************************************************
710 Send/receive a SMBecho command: ping the server
711 ****************************************************************************/
713 bool cli_echo(struct cli_state
*cli
, uint16 num_echos
,
714 unsigned char *data
, size_t length
)
719 SMB_ASSERT(length
< 1024);
721 memset(cli
->outbuf
,'\0',smb_size
);
722 cli_set_message(cli
->outbuf
,1,length
,true);
723 SCVAL(cli
->outbuf
,smb_com
,SMBecho
);
724 SSVAL(cli
->outbuf
,smb_tid
,65535);
725 SSVAL(cli
->outbuf
,smb_vwv0
,num_echos
);
726 cli_setup_packet(cli
);
727 p
= smb_buf(cli
->outbuf
);
728 memcpy(p
, data
, length
);
731 cli_setup_bcc(cli
, p
);
735 for (i
=0; i
<num_echos
; i
++) {
736 if (!cli_receive_smb(cli
)) {
740 if (cli_is_error(cli
)) {