diff options
Diffstat (limited to 'source/libsmb/clientgen.c')
-rw-r--r-- | source/libsmb/clientgen.c | 164 |
1 files changed, 53 insertions, 111 deletions
diff --git a/source/libsmb/clientgen.c b/source/libsmb/clientgen.c index 4a102097ce8..dae9f4de11a 100644 --- a/source/libsmb/clientgen.c +++ b/source/libsmb/clientgen.c @@ -1,5 +1,6 @@ /* - Unix SMB/CIFS implementation. + Unix SMB/Netbios implementation. + Version 1.9. SMB client generic functions Copyright (C) Andrew Tridgell 1994-1998 @@ -22,10 +23,9 @@ #include "includes.h" -/**************************************************************************** - Change the port number used to call on. -****************************************************************************/ - +/* + * Change the port number used to call on + */ int cli_set_port(struct cli_state *cli, int port) { cli->port = port; @@ -33,52 +33,17 @@ int cli_set_port(struct cli_state *cli, int port) } /**************************************************************************** - Read an smb from a fd ignoring all keepalive packets. Note that the buffer - *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN. - The timeout is in milliseconds - - This is exactly the same as receive_smb except that it never returns - a session keepalive packet (just as receive_smb used to do). - receive_smb was changed to return keepalives as the oplock processing means this call - should never go into a blocking read. -****************************************************************************/ - -static BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout) -{ - BOOL ret; - - for(;;) { - ret = receive_smb(fd, buffer, timeout); - - if (!ret) { - DEBUG(10,("client_receive_smb failed\n")); - show_msg(buffer); - return ret; - } - - /* Ignore session keepalive packets. */ - if(CVAL(buffer,0) != SMBkeepalive) - break; - } - show_msg(buffer); - return ret; -} - -/**************************************************************************** - Recv an smb. +recv an smb ****************************************************************************/ - BOOL cli_receive_smb(struct cli_state *cli) { - extern int smb_read_error; BOOL ret; /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */ - if (cli->fd == -1) - return False; + if (cli->fd == -1) return False; again: - ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout); + ret = client_receive_smb(cli->fd,cli->inbuf,abs(cli->timeout)); if (ret) { /* it might be an oplock break request */ @@ -97,19 +62,18 @@ BOOL cli_receive_smb(struct cli_state *cli) } } - /* If the server is not responding, note that now */ + /* If the server is not responding, note that now */ - if (!ret) { - cli->smb_read_error = smb_read_error; - close(cli->fd); - cli->fd = -1; - } + if (!ret && cli->timeout > 0) { + close(cli->fd); + cli->fd = -1; + } return ret; } /**************************************************************************** - Send an smb to a fd. + send an smb to a fd. ****************************************************************************/ BOOL cli_send_smb(struct cli_state *cli) @@ -119,34 +83,31 @@ BOOL cli_send_smb(struct cli_state *cli) ssize_t ret; /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */ - if (cli->fd == -1) - return False; - - cli_caclulate_sign_mac(cli); + if (cli->fd == -1) return False; len = smb_len(cli->outbuf) + 4; while (nwritten < len) { ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten); if (ret <= 0) { - close(cli->fd); - cli->fd = -1; - DEBUG(0,("Error writing %d bytes to client. %d (%s)\n", - (int)len,(int)ret, strerror(errno) )); + close(cli->fd); + cli->fd = -1; + DEBUG(0,("Error writing %d bytes to client. %d\n", + (int)len,(int)ret)); return False; } nwritten += ret; } + return True; } /**************************************************************************** - Setup basics in a outgoing packet. +setup basics in a outgoing packet ****************************************************************************/ - void cli_setup_packet(struct cli_state *cli) { - cli->rap_error = 0; + cli->rap_error = 0; SSVAL(cli->outbuf,smb_pid,cli->pid); SSVAL(cli->outbuf,smb_uid,cli->vuid); SSVAL(cli->outbuf,smb_mid,cli->mid); @@ -154,32 +115,29 @@ void cli_setup_packet(struct cli_state *cli) uint16 flags2; SCVAL(cli->outbuf,smb_flg,0x8); flags2 = FLAGS2_LONG_PATH_COMPONENTS; - if (cli->capabilities & CAP_UNICODE) + if (cli->capabilities & CAP_UNICODE) { flags2 |= FLAGS2_UNICODE_STRINGS; - if (cli->capabilities & CAP_STATUS32) + } + if (cli->capabilities & CAP_STATUS32) { flags2 |= FLAGS2_32_BIT_ERROR_CODES; - if (cli->use_spnego) - flags2 |= FLAGS2_EXTENDED_SECURITY; - if (cli->sign_info.use_smb_signing - || cli->sign_info.temp_smb_signing) - flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES; + } SSVAL(cli->outbuf,smb_flg2, flags2); } } /**************************************************************************** - Setup the bcc length of the packet from a pointer to the end of the data. +setup the bcc length of the packet from a pointer to the end of the data ****************************************************************************/ - void cli_setup_bcc(struct cli_state *cli, void *p) { set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf))); } + + /**************************************************************************** - Initialise credentials of a client structure. +initialise a client structure ****************************************************************************/ - void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr) { /* copy_nt_creds(&cli->usr, usr); */ @@ -194,10 +152,10 @@ void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr) cli->ntlmssp_flags,cli->ntlmssp_cli_flgs)); } + /**************************************************************************** - Initialise a client structure. +initialise a client structure ****************************************************************************/ - struct cli_state *cli_initialise(struct cli_state *cli) { BOOL alloced_cli = False; @@ -216,8 +174,9 @@ struct cli_state *cli_initialise(struct cli_state *cli) alloced_cli = True; } - if (cli->initialised) - cli_close_connection(cli); + if (cli->initialised) { + cli_shutdown(cli); + } ZERO_STRUCTP(cli); @@ -234,23 +193,17 @@ struct cli_state *cli_initialise(struct cli_state *cli) cli->outbuf = (char *)malloc(cli->bufsize); cli->inbuf = (char *)malloc(cli->bufsize); cli->oplock_handler = cli_oplock_ack; - if (lp_use_spnego()) - cli->use_spnego = True; - /* Set the CLI_FORCE_DOSERR environment variable to test client routines using DOS errors instead of STATUS32 ones. This intended only as a temporary hack. */ - if (getenv("CLI_FORCE_DOSERR")) + if (getenv("CLI_FORCE_DOSERR")) { cli->force_dos_errors = True; + } - /* A way to attempt to force SMB signing */ - if (getenv("CLI_FORCE_SMB_SIGNING")) - cli->sign_info.negotiated_smb_signing = True; - if (!cli->outbuf || !cli->inbuf) goto error; - if ((cli->mem_ctx = talloc_init_named("cli based talloc")) == NULL) + if ((cli->mem_ctx = talloc_init()) == NULL) goto error; memset(cli->outbuf, 0, cli->bufsize); @@ -277,53 +230,42 @@ struct cli_state *cli_initialise(struct cli_state *cli) } /**************************************************************************** - Close a client connection and free the memory without destroying cli itself. +shutdown a client structure ****************************************************************************/ - -void cli_close_connection(struct cli_state *cli) +void cli_shutdown(struct cli_state *cli) { + BOOL allocated; SAFE_FREE(cli->outbuf); SAFE_FREE(cli->inbuf); - data_blob_free(&cli->secblob); - - if (cli->mem_ctx) { + if (cli->mem_ctx) talloc_destroy(cli->mem_ctx); - cli->mem_ctx = NULL; - } +#ifdef WITH_SSL + if (cli->fd != -1) + sslutil_disconnect(cli->fd); +#endif /* WITH_SSL */ if (cli->fd != -1) close(cli->fd); - cli->fd = -1; - cli->smb_read_error = 0; -} - -/**************************************************************************** - Shutdown a client structure. -****************************************************************************/ - -void cli_shutdown(struct cli_state *cli) -{ - BOOL allocated = cli->allocated; - cli_close_connection(cli); + allocated = cli->allocated; ZERO_STRUCTP(cli); - if (allocated) - free(cli); + if (allocated) { + SAFE_FREE(cli); + } } + /**************************************************************************** - Set socket options on a open connection. +set socket options on a open connection ****************************************************************************/ - void cli_sockopt(struct cli_state *cli, char *options) { set_socket_options(cli->fd, options); } /**************************************************************************** - Set the PID to use for smb messages. Return the old pid. +set the PID to use for smb messages. Return the old pid. ****************************************************************************/ - uint16 cli_setpid(struct cli_state *cli, uint16 pid) { uint16 ret = cli->pid; |