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 void cli_set_port(struct cli_state *cli, int port)
58 /****************************************************************************
59 convenience routine to find if we negotiated ucs2
60 ****************************************************************************/
62 bool cli_ucs2(struct cli_state *cli)
64 return ((cli->capabilities & CAP_UNICODE) != 0);
68 /****************************************************************************
69 Read an smb from a fd ignoring all keepalive packets.
70 The timeout is in milliseconds
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 ****************************************************************************/
78 static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
85 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
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"));
93 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
94 set_smb_read_error(&cli->smb_rw_error,
99 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
100 set_smb_read_error(&cli->smb_rw_error,
105 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
110 * I don't believe len can be < 0 with NT_STATUS_OK
111 * returned above, but this check doesn't hurt. JRA.
114 if ((ssize_t)len < 0) {
118 /* Ignore session keepalive packets. */
119 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
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",
129 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
134 show_msg(cli->inbuf);
138 static bool cli_state_set_seqnum(struct cli_state *cli, uint16_t mid, uint32_t seqnum)
140 struct cli_state_seqnum *c;
142 for (c = cli->seqnum; c; c = c->next) {
149 c = talloc_zero(cli, struct cli_state_seqnum);
156 c->persistent = false;
157 DLIST_ADD_END(cli->seqnum, c, struct cli_state_seqnum *);
162 bool cli_state_seqnum_persistent(struct cli_state *cli,
165 struct cli_state_seqnum *c;
167 for (c = cli->seqnum; c; c = c->next) {
169 c->persistent = true;
177 bool cli_state_seqnum_remove(struct cli_state *cli,
180 struct cli_state_seqnum *c;
182 for (c = cli->seqnum; c; c = c->next) {
184 DLIST_REMOVE(cli->seqnum, c);
193 static uint32_t cli_state_get_seqnum(struct cli_state *cli, uint16_t mid)
195 struct cli_state_seqnum *c;
197 for (c = cli->seqnum; c; c = c->next) {
199 uint32_t seqnum = c->seqnum;
200 if (!c->persistent) {
201 DLIST_REMOVE(cli->seqnum, c);
211 /****************************************************************************
213 ****************************************************************************/
215 bool cli_receive_smb(struct cli_state *cli)
221 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
226 len = client_receive_smb(cli, 0);
229 /* it might be an oplock break request */
230 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
231 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
232 SVAL(cli->inbuf,smb_vwv6) == 0 &&
233 SVAL(cli->inbuf,smb_vwv7) == 0) {
234 if (cli->oplock_handler) {
235 int fnum = SVAL(cli->inbuf,smb_vwv2);
236 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
237 if (!NT_STATUS_IS_OK(cli->oplock_handler(cli, fnum, level))) {
241 /* try to prevent loops */
242 SCVAL(cli->inbuf,smb_com,0xFF);
247 /* If the server is not responding, note that now */
249 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
255 mid = SVAL(cli->inbuf,smb_mid);
256 seqnum = cli_state_get_seqnum(cli, mid);
258 if (!cli_check_sign_mac(cli, cli->inbuf, seqnum+1)) {
260 * If we get a signature failure in sessionsetup, then
261 * the server sometimes just reflects the sent signature
262 * back to us. Detect this and allow the upper layer to
263 * retrieve the correct Windows error message.
265 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
266 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
267 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
268 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
272 * Reflected signature on login error.
273 * Set bad sig but don't close fd.
275 cli->smb_rw_error = SMB_READ_BAD_SIG;
279 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
280 cli->smb_rw_error = SMB_READ_BAD_SIG;
288 /****************************************************************************
289 Read the data portion of a readX smb.
290 The timeout is in milliseconds
291 ****************************************************************************/
293 ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
297 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
299 status = read_fd_with_timeout(
300 cli->fd, buffer, len, len, cli->timeout, NULL);
301 if (NT_STATUS_IS_OK(status)) {
305 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
306 set_smb_read_error(&cli->smb_rw_error, SMB_READ_EOF);
310 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
311 set_smb_read_error(&cli->smb_rw_error, SMB_READ_TIMEOUT);
315 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
319 static ssize_t write_socket(int fd, const char *buf, size_t len)
323 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
324 ret = write_data(fd,buf,len);
326 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
328 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
329 (int)len, fd, strerror(errno) ));
334 /****************************************************************************
336 ****************************************************************************/
338 bool cli_send_smb(struct cli_state *cli)
343 char *buf_out = cli->outbuf;
344 bool enc_on = cli_encryption_on(cli);
347 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
351 cli_calculate_sign_mac(cli, cli->outbuf, &seqnum);
353 if (!cli_state_set_seqnum(cli, cli->mid, seqnum)) {
354 DEBUG(0,("Failed to store mid[%u]/seqnum[%u]\n",
355 (unsigned int)cli->mid,
356 (unsigned int)seqnum));
361 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
363 if (!NT_STATUS_IS_OK(status)) {
366 cli->smb_rw_error = SMB_WRITE_ERROR;
367 DEBUG(0,("Error in encrypting client message. Error %s\n",
368 nt_errstr(status) ));
373 len = smb_len(buf_out) + 4;
375 while (nwritten < len) {
376 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
379 cli_free_enc_buffer(cli, buf_out);
383 cli->smb_rw_error = SMB_WRITE_ERROR;
384 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
385 (int)len,(int)ret, strerror(errno) ));
392 cli_free_enc_buffer(cli, buf_out);
395 /* Increment the mid so we can tell between responses. */
402 /****************************************************************************
403 Send a "direct" writeX smb to a fd.
404 ****************************************************************************/
406 bool cli_send_smb_direct_writeX(struct cli_state *cli,
410 /* First length to send is the offset to the data. */
411 size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
415 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
420 if (client_is_signing_on(cli)) {
421 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
425 iov[0].iov_base = (void *)cli->outbuf;
426 iov[0].iov_len = len;
427 iov[1].iov_base = CONST_DISCARD(void *, p);
428 iov[1].iov_len = extradata;
430 nwritten = write_data_iov(cli->fd, iov, 2);
431 if (nwritten < (len + extradata)) {
434 cli->smb_rw_error = SMB_WRITE_ERROR;
435 DEBUG(0,("Error writing %d bytes to client. (%s)\n",
436 (int)(len+extradata), strerror(errno)));
440 /* Increment the mid so we can tell between responses. */
447 /****************************************************************************
448 Setup basics in a outgoing packet.
449 ****************************************************************************/
451 void cli_setup_packet_buf(struct cli_state *cli, char *buf)
455 SIVAL(buf,smb_rcls,0);
456 SSVAL(buf,smb_pid,cli->pid);
457 memset(buf+smb_pidhigh, 0, 12);
458 SSVAL(buf,smb_uid,cli->vuid);
459 SSVAL(buf,smb_mid,cli->mid);
461 if (cli->protocol <= PROTOCOL_CORE) {
465 if (cli->case_sensitive) {
466 SCVAL(buf,smb_flg,0x0);
468 /* Default setting, case insensitive. */
469 SCVAL(buf,smb_flg,0x8);
471 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
472 if (cli->capabilities & CAP_UNICODE)
473 flags2 |= FLAGS2_UNICODE_STRINGS;
474 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
475 flags2 |= FLAGS2_DFS_PATHNAMES;
476 if (cli->capabilities & CAP_STATUS32)
477 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
479 flags2 |= FLAGS2_EXTENDED_SECURITY;
480 SSVAL(buf,smb_flg2, flags2);
483 void cli_setup_packet(struct cli_state *cli)
485 cli_setup_packet_buf(cli, cli->outbuf);
488 /****************************************************************************
489 Setup the bcc length of the packet from a pointer to the end of the data.
490 ****************************************************************************/
492 void cli_setup_bcc(struct cli_state *cli, void *p)
494 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
497 /****************************************************************************
498 Initialize Domain, user or password.
499 ****************************************************************************/
501 NTSTATUS cli_set_domain(struct cli_state *cli, const char *domain)
503 TALLOC_FREE(cli->domain);
504 cli->domain = talloc_strdup(cli, domain ? domain : "");
505 if (cli->domain == NULL) {
506 return NT_STATUS_NO_MEMORY;
511 NTSTATUS cli_set_username(struct cli_state *cli, const char *username)
513 TALLOC_FREE(cli->user_name);
514 cli->user_name = talloc_strdup(cli, username ? username : "");
515 if (cli->user_name == NULL) {
516 return NT_STATUS_NO_MEMORY;
521 NTSTATUS cli_set_password(struct cli_state *cli, const char *password)
523 TALLOC_FREE(cli->password);
525 /* Password can be NULL. */
527 cli->password = talloc_strdup(cli, password);
528 if (cli->password == NULL) {
529 return NT_STATUS_NO_MEMORY;
532 /* Use zero NTLMSSP hashes and session key. */
533 cli->password = NULL;
539 /****************************************************************************
540 Initialise credentials of a client structure.
541 ****************************************************************************/
543 NTSTATUS cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
545 NTSTATUS status = cli_set_username(cli, username);
546 if (!NT_STATUS_IS_OK(status)) {
549 status = cli_set_domain(cli, domain);
550 if (!NT_STATUS_IS_OK(status)) {
553 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
555 return cli_set_password(cli, password);
558 /****************************************************************************
559 Initialise a client structure. Always returns a talloc'ed struct.
560 Set the signing state (used from the command line).
561 ****************************************************************************/
563 struct cli_state *cli_initialise_ex(int signing_state)
565 struct cli_state *cli = NULL;
566 bool allow_smb_signing = false;
567 bool mandatory_signing = false;
569 /* Check the effective uid - make sure we are not setuid */
570 if (is_setuid_root()) {
571 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
575 cli = TALLOC_ZERO_P(NULL, struct cli_state);
580 cli->dfs_mountpoint = talloc_strdup(cli, "");
581 if (!cli->dfs_mountpoint) {
587 cli->pid = (uint16)sys_getpid();
589 cli->vuid = UID_FIELD_INVALID;
590 cli->protocol = PROTOCOL_NT1;
591 cli->timeout = 20000; /* Timeout is in milliseconds. */
592 cli->bufsize = CLI_BUFFER_SIZE+4;
593 cli->max_xmit = cli->bufsize;
594 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
596 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
597 cli->oplock_handler = cli_oplock_ack;
598 cli->case_sensitive = false;
599 cli->smb_rw_error = SMB_READ_OK;
601 cli->use_spnego = lp_client_use_spnego();
603 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
605 /* Set the CLI_FORCE_DOSERR environment variable to test
606 client routines using DOS errors instead of STATUS32
607 ones. This intended only as a temporary hack. */
608 if (getenv("CLI_FORCE_DOSERR"))
609 cli->force_dos_errors = true;
611 if (lp_client_signing()) {
612 allow_smb_signing = true;
615 if (lp_client_signing() == Required) {
616 mandatory_signing = true;
619 if (signing_state != Undefined) {
620 allow_smb_signing = true;
623 if (signing_state == false) {
624 allow_smb_signing = false;
625 mandatory_signing = false;
628 if (signing_state == Required) {
629 mandatory_signing = true;
632 if (!cli->outbuf || !cli->inbuf)
635 memset(cli->outbuf, 0, cli->bufsize);
636 memset(cli->inbuf, 0, cli->bufsize);
639 #if defined(DEVELOPER)
640 /* just because we over-allocate, doesn't mean it's right to use it */
641 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
642 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
645 /* initialise signing */
646 cli->signing_state = smb_signing_init(cli,
649 if (!cli->signing_state) {
653 cli->outgoing = tevent_queue_create(cli, "cli_outgoing");
654 if (cli->outgoing == NULL) {
659 cli->initialised = 1;
663 /* Clean up after malloc() error */
667 SAFE_FREE(cli->inbuf);
668 SAFE_FREE(cli->outbuf);
673 struct cli_state *cli_initialise(void)
675 return cli_initialise_ex(Undefined);
678 /****************************************************************************
679 Close all pipes open on this session.
680 ****************************************************************************/
682 void cli_nt_pipes_close(struct cli_state *cli)
684 while (cli->pipe_list != NULL) {
686 * No TALLOC_FREE here!
688 talloc_free(cli->pipe_list);
692 /****************************************************************************
693 Shutdown a client structure.
694 ****************************************************************************/
696 void cli_shutdown(struct cli_state *cli)
698 if (cli->prev == NULL) {
700 * Possible head of a DFS list,
701 * shutdown all subsidiary DFS
704 struct cli_state *p, *next;
706 for (p = cli->next; p; p = next) {
712 * We're a subsidiary connection.
713 * Just remove ourselves from the
716 DLIST_REMOVE(cli->prev, cli);
719 cli_nt_pipes_close(cli);
722 * tell our peer to free his resources. Wihtout this, when an
723 * application attempts to do a graceful shutdown and calls
724 * smbc_free_context() to clean up all connections, some connections
725 * can remain active on the peer end, until some (long) timeout period
726 * later. This tree disconnect forces the peer to clean up, since the
727 * connection will be going away.
729 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
730 * the only user for this so far is smbmount which passes opened connection
731 * down to kernel's smbfs module.
733 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
737 SAFE_FREE(cli->outbuf);
738 SAFE_FREE(cli->inbuf);
740 data_blob_free(&cli->secblob);
741 data_blob_free(&cli->user_session_key);
747 cli->smb_rw_error = SMB_READ_OK;
750 * Need to free pending first, they remove themselves
752 while (cli->pending) {
753 talloc_free(cli->pending[0]);
758 /****************************************************************************
759 Set socket options on a open connection.
760 ****************************************************************************/
762 void cli_sockopt(struct cli_state *cli, const char *options)
764 set_socket_options(cli->fd, options);
767 /****************************************************************************
768 Set the PID to use for smb messages. Return the old pid.
769 ****************************************************************************/
771 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
773 uint16 ret = cli->pid;
778 /****************************************************************************
779 Set the case sensitivity flag on the packets. Returns old state.
780 ****************************************************************************/
782 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
784 bool ret = cli->case_sensitive;
785 cli->case_sensitive = case_sensitive;
789 /****************************************************************************
790 Send a keepalive packet to the server
791 ****************************************************************************/
793 bool cli_send_keepalive(struct cli_state *cli)
796 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
799 if (!send_keepalive(cli->fd)) {
802 DEBUG(0,("Error sending keepalive packet to client.\n"));
808 struct cli_echo_state {
814 static void cli_echo_done(struct tevent_req *subreq);
816 struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
817 struct cli_state *cli, uint16_t num_echos,
820 struct tevent_req *req, *subreq;
821 struct cli_echo_state *state;
823 req = tevent_req_create(mem_ctx, &state, struct cli_echo_state);
827 SSVAL(state->vwv, 0, num_echos);
829 state->num_echos = num_echos;
831 subreq = cli_smb_send(state, ev, cli, SMBecho, 0, 1, state->vwv,
832 data.length, data.data);
833 if (subreq == NULL) {
836 tevent_req_set_callback(subreq, cli_echo_done, req);
843 static void cli_echo_done(struct tevent_req *subreq)
845 struct tevent_req *req = tevent_req_callback_data(
846 subreq, struct tevent_req);
847 struct cli_echo_state *state = tevent_req_data(
848 req, struct cli_echo_state);
853 status = cli_smb_recv(subreq, 0, NULL, NULL, &num_bytes, &bytes);
854 if (!NT_STATUS_IS_OK(status)) {
855 tevent_req_nterror(req, status);
858 if ((num_bytes != state->data.length)
859 || (memcmp(bytes, state->data.data, num_bytes) != 0)) {
860 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
864 state->num_echos -=1;
865 if (state->num_echos == 0) {
866 tevent_req_done(req);
870 if (!cli_smb_req_set_pending(subreq)) {
871 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
877 * Get the result out from an echo request
878 * @param[in] req The async_req from cli_echo_send
879 * @retval Did the server reply correctly?
882 NTSTATUS cli_echo_recv(struct tevent_req *req)
884 return tevent_req_simple_recv_ntstatus(req);
888 * @brief Send/Receive SMBEcho requests
889 * @param[in] mem_ctx The memory context to put the async_req on
890 * @param[in] ev The event context that will call us back
891 * @param[in] cli The connection to send the echo to
892 * @param[in] num_echos How many times do we want to get the reply?
893 * @param[in] data The data we want to get back
894 * @retval Did the server reply correctly?
897 NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
899 TALLOC_CTX *frame = talloc_stackframe();
900 struct event_context *ev;
901 struct tevent_req *req;
902 NTSTATUS status = NT_STATUS_OK;
904 if (cli_has_async_calls(cli)) {
906 * Can't use sync call while an async call is in flight
908 status = NT_STATUS_INVALID_PARAMETER;
912 ev = event_context_init(frame);
914 status = NT_STATUS_NO_MEMORY;
918 req = cli_echo_send(frame, ev, cli, num_echos, data);
920 status = NT_STATUS_NO_MEMORY;
924 if (!tevent_req_poll(req, ev)) {
925 status = map_nt_error_from_unix(errno);
929 status = cli_echo_recv(req);
932 if (!NT_STATUS_IS_OK(status)) {
933 cli_set_error(cli, status);
939 * Is the SMB command able to hold an AND_X successor
940 * @param[in] cmd The SMB command in question
941 * @retval Can we add a chained request after "cmd"?
943 bool is_andx_req(uint8_t cmd)