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 extern int smb_read_error
;
25 /****************************************************************************
26 Change the timeout (in milliseconds).
27 ****************************************************************************/
29 unsigned int cli_set_timeout(struct cli_state
*cli
, unsigned int timeout
)
31 unsigned int old_timeout
= cli
->timeout
;
32 cli
->timeout
= timeout
;
36 /****************************************************************************
37 Change the port number used to call on.
38 ****************************************************************************/
40 int cli_set_port(struct cli_state
*cli
, int port
)
46 /****************************************************************************
47 Read an smb from a fd ignoring all keepalive packets. Note that the buffer
48 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
49 The timeout is in milliseconds
51 This is exactly the same as receive_smb except that it never returns
52 a session keepalive packet (just as receive_smb used to do).
53 receive_smb was changed to return keepalives as the oplock processing means this call
54 should never go into a blocking read.
55 ****************************************************************************/
57 static ssize_t
client_receive_smb(int fd
,char *buffer
, unsigned int timeout
, size_t maxlen
)
62 len
= receive_smb_raw(fd
, buffer
, timeout
, maxlen
);
65 DEBUG(10,("client_receive_smb failed\n"));
70 /* Ignore session keepalive packets. */
71 if(CVAL(buffer
,0) != SMBkeepalive
)
78 /****************************************************************************
80 ****************************************************************************/
82 BOOL
cli_receive_smb(struct cli_state
*cli
)
86 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
91 len
= client_receive_smb(cli
->fd
,cli
->inbuf
,cli
->timeout
, 0);
94 /* it might be an oplock break request */
95 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) &&
96 CVAL(cli
->inbuf
,smb_com
) == SMBlockingX
&&
97 SVAL(cli
->inbuf
,smb_vwv6
) == 0 &&
98 SVAL(cli
->inbuf
,smb_vwv7
) == 0) {
99 if (cli
->oplock_handler
) {
100 int fnum
= SVAL(cli
->inbuf
,smb_vwv2
);
101 unsigned char level
= CVAL(cli
->inbuf
,smb_vwv3
+1);
102 if (!cli
->oplock_handler(cli
, fnum
, level
)) {
106 /* try to prevent loops */
107 SCVAL(cli
->inbuf
,smb_com
,0xFF);
112 /* If the server is not responding, note that now */
114 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
115 cli
->smb_rw_error
= smb_read_error
;
121 if (!cli_check_sign_mac(cli
)) {
123 * If we get a signature failure in sessionsetup, then
124 * the server sometimes just reflects the sent signature
125 * back to us. Detect this and allow the upper layer to
126 * retrieve the correct Windows error message.
128 if (CVAL(cli
->outbuf
,smb_com
) == SMBsesssetupX
&&
129 (smb_len(cli
->inbuf
) > (smb_ss_field
+ 8 - 4)) &&
130 (SVAL(cli
->inbuf
,smb_flg2
) & FLAGS2_SMB_SECURITY_SIGNATURES
) &&
131 memcmp(&cli
->outbuf
[smb_ss_field
],&cli
->inbuf
[smb_ss_field
],8) == 0 &&
135 * Reflected signature on login error.
136 * Set bad sig but don't close fd.
138 cli
->smb_rw_error
= READ_BAD_SIG
;
142 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
143 cli
->smb_rw_error
= READ_BAD_SIG
;
151 /****************************************************************************
152 Read the data portion of a readX smb.
153 The timeout is in milliseconds
154 ****************************************************************************/
156 ssize_t
cli_receive_smb_data(struct cli_state
*cli
, char *buffer
, size_t len
)
158 if (cli
->timeout
> 0) {
159 return read_socket_with_timeout(cli
->fd
, buffer
, len
, len
, cli
->timeout
);
161 return read_data(cli
->fd
, buffer
, len
);
165 /****************************************************************************
166 Read a smb readX header.
167 ****************************************************************************/
169 BOOL
cli_receive_smb_readX_header(struct cli_state
*cli
)
178 /* Read up to the size of a readX header reply. */
179 len
= client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
, (smb_size
- 4) + 24);
182 /* it might be an oplock break request */
183 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) &&
184 CVAL(cli
->inbuf
,smb_com
) == SMBlockingX
&&
185 SVAL(cli
->inbuf
,smb_vwv6
) == 0 &&
186 SVAL(cli
->inbuf
,smb_vwv7
) == 0) {
187 ssize_t total_len
= smb_len(cli
->inbuf
);
189 if (total_len
> CLI_SAMBA_MAX_LARGE_READX_SIZE
+SAFETY_MARGIN
) {
193 /* Read the rest of the data. */
194 if ((total_len
- len
> 0) &&
195 !cli_receive_smb_data(cli
,cli
->inbuf
+len
,total_len
- len
)) {
199 if (cli
->oplock_handler
) {
200 int fnum
= SVAL(cli
->inbuf
,smb_vwv2
);
201 unsigned char level
= CVAL(cli
->inbuf
,smb_vwv3
+1);
202 if (!cli
->oplock_handler(cli
, fnum
, level
)) return False
;
204 /* try to prevent loops */
205 SCVAL(cli
->inbuf
,smb_com
,0xFF);
210 /* If it's not the above size it probably was an error packet. */
212 if ((len
== (smb_size
- 4) + 24) && !cli_is_error(cli
)) {
213 /* Check it's a non-chained readX reply. */
214 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) ||
215 (CVAL(cli
->inbuf
,smb_vwv0
) != 0xFF) ||
216 (CVAL(cli
->inbuf
,smb_com
) != SMBreadX
)) {
218 * We're not coping here with asnyc replies to
219 * other calls. Punt here - we need async client
226 * We know it's a readX reply - ensure we've read the
227 * padding bytes also.
230 offset
= SVAL(cli
->inbuf
,smb_vwv6
);
233 size_t padbytes
= offset
- len
;
234 ret
= cli_receive_smb_data(cli
,smb_buf(cli
->inbuf
),padbytes
);
235 if (ret
!= padbytes
) {
245 cli
->smb_rw_error
= smb_read_error
= READ_ERROR
;
251 static ssize_t
write_socket(int fd
, const char *buf
, size_t len
)
255 DEBUG(6,("write_socket(%d,%d)\n",fd
,(int)len
));
256 ret
= write_data(fd
,buf
,len
);
258 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd
,(int)len
,(int)ret
));
260 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
261 (int)len
, fd
, strerror(errno
) ));
266 /****************************************************************************
268 ****************************************************************************/
270 BOOL
cli_send_smb(struct cli_state
*cli
)
276 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
280 cli_calculate_sign_mac(cli
);
282 len
= smb_len(cli
->outbuf
) + 4;
284 while (nwritten
< len
) {
285 ret
= write_socket(cli
->fd
,cli
->outbuf
+nwritten
,len
- nwritten
);
289 cli
->smb_rw_error
= WRITE_ERROR
;
290 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
291 (int)len
,(int)ret
, strerror(errno
) ));
296 /* Increment the mid so we can tell between responses. */
303 /****************************************************************************
304 Setup basics in a outgoing packet.
305 ****************************************************************************/
307 void cli_setup_packet(struct cli_state
*cli
)
310 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
311 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
312 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
313 if (cli
->protocol
> PROTOCOL_CORE
) {
315 if (cli
->case_sensitive
) {
316 SCVAL(cli
->outbuf
,smb_flg
,0x0);
318 /* Default setting, case insensitive. */
319 SCVAL(cli
->outbuf
,smb_flg
,0x8);
321 flags2
= FLAGS2_LONG_PATH_COMPONENTS
;
322 if (cli
->capabilities
& CAP_UNICODE
)
323 flags2
|= FLAGS2_UNICODE_STRINGS
;
324 if ((cli
->capabilities
& CAP_DFS
) && cli
->dfsroot
)
325 flags2
|= FLAGS2_DFS_PATHNAMES
;
326 if (cli
->capabilities
& CAP_STATUS32
)
327 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
329 flags2
|= FLAGS2_EXTENDED_SECURITY
;
330 SSVAL(cli
->outbuf
,smb_flg2
, flags2
);
334 /****************************************************************************
335 Setup the bcc length of the packet from a pointer to the end of the data.
336 ****************************************************************************/
338 void cli_setup_bcc(struct cli_state
*cli
, void *p
)
340 set_message_bcc(cli
->outbuf
, PTR_DIFF(p
, smb_buf(cli
->outbuf
)));
343 /****************************************************************************
344 Initialise credentials of a client structure.
345 ****************************************************************************/
347 void cli_init_creds(struct cli_state
*cli
, const char *username
, const char *domain
, const char *password
)
349 fstrcpy(cli
->domain
, domain
);
350 fstrcpy(cli
->user_name
, username
);
351 pwd_set_cleartext(&cli
->pwd
, password
);
353 cli
->pwd
.null_pwd
= True
;
356 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli
->user_name
, cli
->domain
));
359 /****************************************************************************
360 Set the signing state (used from the command line).
361 ****************************************************************************/
363 void cli_setup_signing_state(struct cli_state
*cli
, int signing_state
)
365 if (signing_state
== Undefined
)
368 if (signing_state
== False
) {
369 cli
->sign_info
.allow_smb_signing
= False
;
370 cli
->sign_info
.mandatory_signing
= False
;
374 cli
->sign_info
.allow_smb_signing
= True
;
376 if (signing_state
== Required
)
377 cli
->sign_info
.mandatory_signing
= True
;
380 /****************************************************************************
381 Initialise a client structure. Always returns a malloc'ed struct.
382 ****************************************************************************/
384 struct cli_state
*cli_initialise(void)
386 struct cli_state
*cli
= NULL
;
388 /* Check the effective uid - make sure we are not setuid */
389 if (is_setuid_root()) {
390 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
394 cli
= SMB_MALLOC_P(struct cli_state
);
404 cli
->pid
= (uint16
)sys_getpid();
406 cli
->vuid
= UID_FIELD_INVALID
;
407 cli
->protocol
= PROTOCOL_NT1
;
408 cli
->timeout
= 20000; /* Timeout is in milliseconds. */
409 cli
->bufsize
= CLI_BUFFER_SIZE
+4;
410 cli
->max_xmit
= cli
->bufsize
;
411 cli
->outbuf
= (char *)SMB_MALLOC(cli
->bufsize
+SAFETY_MARGIN
);
412 cli
->inbuf
= (char *)SMB_MALLOC(cli
->bufsize
+SAFETY_MARGIN
);
413 cli
->oplock_handler
= cli_oplock_ack
;
414 cli
->case_sensitive
= False
;
415 cli
->smb_rw_error
= 0;
417 cli
->use_spnego
= lp_client_use_spnego();
419 cli
->capabilities
= CAP_UNICODE
| CAP_STATUS32
| CAP_DFS
;
421 /* Set the CLI_FORCE_DOSERR environment variable to test
422 client routines using DOS errors instead of STATUS32
423 ones. This intended only as a temporary hack. */
424 if (getenv("CLI_FORCE_DOSERR"))
425 cli
->force_dos_errors
= True
;
427 if (lp_client_signing())
428 cli
->sign_info
.allow_smb_signing
= True
;
430 if (lp_client_signing() == Required
)
431 cli
->sign_info
.mandatory_signing
= True
;
433 if (!cli
->outbuf
|| !cli
->inbuf
)
436 if ((cli
->mem_ctx
= talloc_init("cli based talloc")) == NULL
)
439 memset(cli
->outbuf
, 0, cli
->bufsize
);
440 memset(cli
->inbuf
, 0, cli
->bufsize
);
443 #if defined(DEVELOPER)
444 /* just because we over-allocate, doesn't mean it's right to use it */
445 clobber_region(FUNCTION_MACRO
, __LINE__
, cli
->outbuf
+cli
->bufsize
, SAFETY_MARGIN
);
446 clobber_region(FUNCTION_MACRO
, __LINE__
, cli
->inbuf
+cli
->bufsize
, SAFETY_MARGIN
);
449 /* initialise signing */
450 cli_null_set_signing(cli
);
452 cli
->initialised
= 1;
456 /* Clean up after malloc() error */
460 SAFE_FREE(cli
->inbuf
);
461 SAFE_FREE(cli
->outbuf
);
466 /****************************************************************************
468 Close an open named pipe over SMB. Free any authentication data.
469 Returns False if the cli_close call failed.
470 ****************************************************************************/
472 BOOL
cli_rpc_pipe_close(struct rpc_pipe_client
*cli
)
480 ret
= cli_close(cli
->cli
, cli
->fnum
);
483 DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
485 "to machine %s. Error was %s\n",
489 cli_errstr(cli
->cli
)));
492 if (cli
->auth
.cli_auth_data_free_func
) {
493 (*cli
->auth
.cli_auth_data_free_func
)(&cli
->auth
);
496 DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
497 cli
->pipe_name
, cli
->cli
->desthost
));
499 DLIST_REMOVE(cli
->cli
->pipe_list
, cli
);
500 talloc_destroy(cli
->mem_ctx
);
504 /****************************************************************************
505 Close all pipes open on this session.
506 ****************************************************************************/
508 void cli_nt_pipes_close(struct cli_state
*cli
)
510 struct rpc_pipe_client
*cp
, *next
;
512 for (cp
= cli
->pipe_list
; cp
; cp
= next
) {
514 cli_rpc_pipe_close(cp
);
518 /****************************************************************************
519 Shutdown a client structure.
520 ****************************************************************************/
522 void cli_shutdown(struct cli_state
*cli
)
524 cli_nt_pipes_close(cli
);
527 * tell our peer to free his resources. Wihtout this, when an
528 * application attempts to do a graceful shutdown and calls
529 * smbc_free_context() to clean up all connections, some connections
530 * can remain active on the peer end, until some (long) timeout period
531 * later. This tree disconnect forces the peer to clean up, since the
532 * connection will be going away.
534 * Also, do not do tree disconnect when cli->smb_rw_error is DO_NOT_DO_TDIS
535 * the only user for this so far is smbmount which passes opened connection
536 * down to kernel's smbfs module.
538 if ( (cli
->cnum
!= (uint16
)-1) && (cli
->smb_rw_error
!= DO_NOT_DO_TDIS
) ) {
542 SAFE_FREE(cli
->outbuf
);
543 SAFE_FREE(cli
->inbuf
);
545 cli_free_signing_context(cli
);
546 data_blob_free(&cli
->secblob
);
547 data_blob_free(&cli
->user_session_key
);
550 talloc_destroy(cli
->mem_ctx
);
558 cli
->smb_rw_error
= 0;
563 /****************************************************************************
564 Set socket options on a open connection.
565 ****************************************************************************/
567 void cli_sockopt(struct cli_state
*cli
, const char *options
)
569 set_socket_options(cli
->fd
, options
);
572 /****************************************************************************
573 Set the PID to use for smb messages. Return the old pid.
574 ****************************************************************************/
576 uint16
cli_setpid(struct cli_state
*cli
, uint16 pid
)
578 uint16 ret
= cli
->pid
;
583 /****************************************************************************
584 Set the case sensitivity flag on the packets. Returns old state.
585 ****************************************************************************/
587 BOOL
cli_set_case_sensitive(struct cli_state
*cli
, BOOL case_sensitive
)
589 BOOL ret
= cli
->case_sensitive
;
590 cli
->case_sensitive
= case_sensitive
;
594 /****************************************************************************
595 Send a keepalive packet to the server
596 ****************************************************************************/
598 BOOL
cli_send_keepalive(struct cli_state
*cli
)
601 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
604 if (!send_keepalive(cli
->fd
)) {
607 DEBUG(0,("Error sending keepalive packet to client.\n"));
613 /****************************************************************************
614 Send/receive a SMBecho command: ping the server
615 ****************************************************************************/
617 BOOL
cli_echo(struct cli_state
*cli
, uint16 num_echos
,
618 unsigned char *data
, size_t length
)
623 SMB_ASSERT(length
< 1024);
625 memset(cli
->outbuf
,'\0',smb_size
);
626 set_message(cli
->outbuf
,1,length
,True
);
627 SCVAL(cli
->outbuf
,smb_com
,SMBecho
);
628 SSVAL(cli
->outbuf
,smb_tid
,65535);
629 SSVAL(cli
->outbuf
,smb_vwv0
,num_echos
);
630 cli_setup_packet(cli
);
631 p
= smb_buf(cli
->outbuf
);
632 memcpy(p
, data
, length
);
635 cli_setup_bcc(cli
, p
);
639 for (i
=0; i
<num_echos
; i
++) {
640 if (!cli_receive_smb(cli
)) {
644 if (cli_is_error(cli
)) {