2 Unix SMB/CIFS implementation.
3 SMB client generic functions
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 BOOL
client_receive_smb(int fd
,char *buffer
, unsigned int timeout
)
62 ret
= receive_smb_raw(fd
, buffer
, timeout
);
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
)
84 extern int smb_read_error
;
87 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
92 ret
= client_receive_smb(cli
->fd
,cli
->inbuf
,cli
->timeout
);
95 /* it might be an oplock break request */
96 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) &&
97 CVAL(cli
->inbuf
,smb_com
) == SMBlockingX
&&
98 SVAL(cli
->inbuf
,smb_vwv6
) == 0 &&
99 SVAL(cli
->inbuf
,smb_vwv7
) == 0) {
100 if (cli
->oplock_handler
) {
101 int fnum
= SVAL(cli
->inbuf
,smb_vwv2
);
102 unsigned char level
= CVAL(cli
->inbuf
,smb_vwv3
+1);
103 if (!cli
->oplock_handler(cli
, fnum
, level
)) return False
;
105 /* try to prevent loops */
106 SCVAL(cli
->inbuf
,smb_com
,0xFF);
111 /* If the server is not responding, note that now */
114 cli
->smb_rw_error
= smb_read_error
;
120 if (!cli_check_sign_mac(cli
)) {
121 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
122 cli
->smb_rw_error
= READ_BAD_SIG
;
130 /****************************************************************************
132 ****************************************************************************/
134 BOOL
cli_send_smb(struct cli_state
*cli
)
140 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
144 cli_calculate_sign_mac(cli
);
146 len
= smb_len(cli
->outbuf
) + 4;
148 while (nwritten
< len
) {
149 ret
= write_socket(cli
->fd
,cli
->outbuf
+nwritten
,len
- nwritten
);
153 cli
->smb_rw_error
= WRITE_ERROR
;
154 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
155 (int)len
,(int)ret
, strerror(errno
) ));
160 /* Increment the mid so we can tell between responses. */
167 /****************************************************************************
168 Setup basics in a outgoing packet.
169 ****************************************************************************/
171 void cli_setup_packet(struct cli_state
*cli
)
174 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
175 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
176 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
177 if (cli
->protocol
> PROTOCOL_CORE
) {
179 if (cli
->case_sensitive
) {
180 SCVAL(cli
->outbuf
,smb_flg
,0x0);
182 /* Default setting, case insensitive. */
183 SCVAL(cli
->outbuf
,smb_flg
,0x8);
185 flags2
= FLAGS2_LONG_PATH_COMPONENTS
;
186 if (cli
->capabilities
& CAP_UNICODE
)
187 flags2
|= FLAGS2_UNICODE_STRINGS
;
188 if (cli
->capabilities
& CAP_DFS
)
189 flags2
|= FLAGS2_DFS_PATHNAMES
;
190 if (cli
->capabilities
& CAP_STATUS32
)
191 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
193 flags2
|= FLAGS2_EXTENDED_SECURITY
;
194 SSVAL(cli
->outbuf
,smb_flg2
, flags2
);
198 /****************************************************************************
199 Setup the bcc length of the packet from a pointer to the end of the data.
200 ****************************************************************************/
202 void cli_setup_bcc(struct cli_state
*cli
, void *p
)
204 set_message_bcc(cli
->outbuf
, PTR_DIFF(p
, smb_buf(cli
->outbuf
)));
207 /****************************************************************************
208 Initialise credentials of a client structure.
209 ****************************************************************************/
211 void cli_init_creds(struct cli_state
*cli
, const struct ntuser_creds
*usr
)
213 /* copy_nt_creds(&cli->usr, usr); */
214 fstrcpy(cli
->domain
, usr
->domain
);
215 fstrcpy(cli
->user_name
, usr
->user_name
);
216 memcpy(&cli
->pwd
, &usr
->pwd
, sizeof(usr
->pwd
));
218 DEBUG(10,("cli_init_creds: user %s domain %s\n",
219 cli
->user_name
, cli
->domain
));
222 /****************************************************************************
223 Set the signing state (used from the command line).
224 ****************************************************************************/
226 void cli_setup_signing_state(struct cli_state
*cli
, int signing_state
)
228 if (signing_state
== Undefined
)
231 if (signing_state
== False
) {
232 cli
->sign_info
.allow_smb_signing
= False
;
233 cli
->sign_info
.mandatory_signing
= False
;
237 cli
->sign_info
.allow_smb_signing
= True
;
239 if (signing_state
== Required
)
240 cli
->sign_info
.mandatory_signing
= True
;
243 /****************************************************************************
244 Initialise a client structure.
245 ****************************************************************************/
247 struct cli_state
*cli_initialise(struct cli_state
*cli
)
249 BOOL alloced_cli
= False
;
252 /* Check the effective uid - make sure we are not setuid */
253 if (is_setuid_root()) {
254 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
259 cli
= SMB_MALLOC_P(struct cli_state
);
266 if (cli
->initialised
)
267 cli_close_connection(cli
);
274 cli
->pid
= (uint16
)sys_getpid();
276 cli
->vuid
= UID_FIELD_INVALID
;
277 cli
->protocol
= PROTOCOL_NT1
;
278 cli
->timeout
= 20000; /* Timeout is in milliseconds. */
279 cli
->bufsize
= CLI_BUFFER_SIZE
+4;
280 cli
->max_xmit
= cli
->bufsize
;
281 cli
->outbuf
= (char *)SMB_MALLOC(cli
->bufsize
+SAFETY_MARGIN
);
282 cli
->inbuf
= (char *)SMB_MALLOC(cli
->bufsize
+SAFETY_MARGIN
);
283 cli
->oplock_handler
= cli_oplock_ack
;
284 cli
->case_sensitive
= False
;
285 cli
->smb_rw_error
= 0;
287 cli
->use_spnego
= lp_client_use_spnego();
289 cli
->capabilities
= CAP_UNICODE
| CAP_STATUS32
| CAP_DFS
;
291 /* Set the CLI_FORCE_DOSERR environment variable to test
292 client routines using DOS errors instead of STATUS32
293 ones. This intended only as a temporary hack. */
294 if (getenv("CLI_FORCE_DOSERR"))
295 cli
->force_dos_errors
= True
;
297 if (lp_client_signing())
298 cli
->sign_info
.allow_smb_signing
= True
;
300 if (lp_client_signing() == Required
)
301 cli
->sign_info
.mandatory_signing
= True
;
303 if (!cli
->outbuf
|| !cli
->inbuf
)
306 if ((cli
->mem_ctx
= talloc_init("cli based talloc")) == NULL
)
309 memset(cli
->outbuf
, 0, cli
->bufsize
);
310 memset(cli
->inbuf
, 0, cli
->bufsize
);
313 #if defined(DEVELOPER)
314 /* just because we over-allocate, doesn't mean it's right to use it */
315 clobber_region(FUNCTION_MACRO
, __LINE__
, cli
->outbuf
+cli
->bufsize
, SAFETY_MARGIN
);
316 clobber_region(FUNCTION_MACRO
, __LINE__
, cli
->inbuf
+cli
->bufsize
, SAFETY_MARGIN
);
319 /* initialise signing */
320 cli_null_set_signing(cli
);
322 for (i
=0; i
<PI_MAX_PIPES
; i
++)
323 cli
->pipes
[i
].fnum
= 0;
325 cli
->netlogon_pipe
.fnum
= 0;
327 cli
->initialised
= 1;
328 cli
->allocated
= alloced_cli
;
334 /* Clean up after malloc() error */
338 SAFE_FREE(cli
->inbuf
);
339 SAFE_FREE(cli
->outbuf
);
347 /****************************************************************************
349 ****************************************************************************/
351 void cli_nt_session_close(struct cli_state
*cli
)
355 for (i
=0; i
<PI_MAX_PIPES
; i
++) {
356 if (cli
->pipes
[i
].pipe_auth_flags
& AUTH_PIPE_NTLMSSP
) {
357 ntlmssp_end(&cli
->pipes
[i
].ntlmssp_pipe_state
);
360 if (cli
->pipes
[i
].fnum
!= 0)
361 cli_close(cli
, cli
->pipes
[i
].fnum
);
362 cli
->pipes
[i
].fnum
= 0;
367 /****************************************************************************
368 close the NETLOGON session holding the session key for NETSEC
369 ****************************************************************************/
371 void cli_nt_netlogon_netsec_session_close(struct cli_state
*cli
)
373 if (cli
->netlogon_pipe
.fnum
!= 0) {
374 cli_close(cli
, cli
->netlogon_pipe
.fnum
);
375 cli
->netlogon_pipe
.fnum
= 0;
379 /****************************************************************************
380 Close a client connection and free the memory without destroying cli itself.
381 ****************************************************************************/
383 void cli_close_connection(struct cli_state
*cli
)
385 cli_nt_session_close(cli
);
386 cli_nt_netlogon_netsec_session_close(cli
);
389 * tell our peer to free his resources. Wihtout this, when an
390 * application attempts to do a graceful shutdown and calls
391 * smbc_free_context() to clean up all connections, some connections
392 * can remain active on the peer end, until some (long) timeout period
393 * later. This tree disconnect forces the peer to clean up, since the
394 * connection will be going away.
396 * Also, do not do tree disconnect when cli->smb_rw_error is DO_NOT_DO_TDIS
397 * the only user for this so far is smbmount which passes opened connection
398 * down to kernel's smbfs module.
400 if ( (cli
->cnum
!= (uint16
)-1) && (cli
->smb_rw_error
!= DO_NOT_DO_TDIS
) )
403 SAFE_FREE(cli
->outbuf
);
404 SAFE_FREE(cli
->inbuf
);
406 cli_free_signing_context(cli
);
407 data_blob_free(&cli
->secblob
);
408 data_blob_free(&cli
->user_session_key
);
410 if (cli
->pipes
[cli
->pipe_idx
].pipe_auth_flags
& AUTH_PIPE_NTLMSSP
)
411 ntlmssp_end(&cli
->pipes
[cli
->pipe_idx
].ntlmssp_pipe_state
);
414 talloc_destroy(cli
->mem_ctx
);
421 cli
->smb_rw_error
= 0;
425 /****************************************************************************
426 Shutdown a client structure.
427 ****************************************************************************/
429 void cli_shutdown(struct cli_state
*cli
)
431 BOOL allocated
= cli
->allocated
;
432 cli_close_connection(cli
);
438 /****************************************************************************
439 Set socket options on a open connection.
440 ****************************************************************************/
442 void cli_sockopt(struct cli_state
*cli
, const char *options
)
444 set_socket_options(cli
->fd
, options
);
447 /****************************************************************************
448 Set the PID to use for smb messages. Return the old pid.
449 ****************************************************************************/
451 uint16
cli_setpid(struct cli_state
*cli
, uint16 pid
)
453 uint16 ret
= cli
->pid
;
458 /****************************************************************************
459 Set the case sensitivity flag on the packets. Returns old state.
460 ****************************************************************************/
462 BOOL
cli_set_case_sensitive(struct cli_state
*cli
, BOOL case_sensitive
)
464 BOOL ret
= cli
->case_sensitive
;
465 cli
->case_sensitive
= case_sensitive
;
469 /****************************************************************************
470 Send a keepalive packet to the server
471 ****************************************************************************/
472 BOOL
cli_send_keepalive(struct cli_state
*cli
)
475 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
478 if (!send_keepalive(cli
->fd
)) {
481 DEBUG(0,("Error sending keepalive packet to client.\n"));