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/>.
22 #include "smb_signing.h"
23 #include "async_smb.h"
25 /*******************************************************************
26 Setup the word count and byte count for a client smb message.
27 ********************************************************************/
29 int cli_set_message(char *buf,int num_words,int num_bytes,bool zero)
31 if (zero && (num_words || num_bytes)) {
32 memset(buf + smb_size,'\0',num_words*2 + num_bytes);
34 SCVAL(buf,smb_wct,num_words);
35 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
36 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
37 return (smb_size + num_words*2 + num_bytes);
40 /****************************************************************************
41 Change the timeout (in milliseconds).
42 ****************************************************************************/
44 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
46 unsigned int old_timeout = cli->timeout;
47 cli->timeout = timeout;
51 /****************************************************************************
52 Change the port number used to call on.
53 ****************************************************************************/
55 void cli_set_port(struct cli_state *cli, int port)
60 /****************************************************************************
61 convenience routine to find if we negotiated ucs2
62 ****************************************************************************/
64 bool cli_ucs2(struct cli_state *cli)
66 return ((cli->capabilities & CAP_UNICODE) != 0);
70 /****************************************************************************
71 Read an smb from a fd ignoring all keepalive packets.
72 The timeout is in milliseconds
74 This is exactly the same as receive_smb except that it never returns
75 a session keepalive packet (just as receive_smb used to do).
76 receive_smb was changed to return keepalives as the oplock processing means this call
77 should never go into a blocking read.
78 ****************************************************************************/
80 static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
87 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
89 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize,
90 cli->timeout, maxlen, &len);
91 if (!NT_STATUS_IS_OK(status)) {
92 DEBUG(10,("client_receive_smb failed\n"));
95 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
96 set_smb_read_error(&cli->smb_rw_error,
101 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
102 set_smb_read_error(&cli->smb_rw_error,
107 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
112 * I don't believe len can be < 0 with NT_STATUS_OK
113 * returned above, but this check doesn't hurt. JRA.
116 if ((ssize_t)len < 0) {
120 /* Ignore session keepalive packets. */
121 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
126 if (cli_encryption_on(cli)) {
127 NTSTATUS status = cli_decrypt_message(cli);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
131 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
136 show_msg(cli->inbuf);
140 static bool cli_state_set_seqnum(struct cli_state *cli, uint16_t mid, uint32_t seqnum)
142 struct cli_state_seqnum *c;
144 for (c = cli->seqnum; c; c = c->next) {
151 c = talloc_zero(cli, struct cli_state_seqnum);
158 c->persistent = false;
159 DLIST_ADD_END(cli->seqnum, c, struct cli_state_seqnum *);
164 bool cli_state_seqnum_persistent(struct cli_state *cli,
167 struct cli_state_seqnum *c;
169 for (c = cli->seqnum; c; c = c->next) {
171 c->persistent = true;
179 bool cli_state_seqnum_remove(struct cli_state *cli,
182 struct cli_state_seqnum *c;
184 for (c = cli->seqnum; c; c = c->next) {
186 DLIST_REMOVE(cli->seqnum, c);
195 static uint32_t cli_state_get_seqnum(struct cli_state *cli, uint16_t mid)
197 struct cli_state_seqnum *c;
199 for (c = cli->seqnum; c; c = c->next) {
201 uint32_t seqnum = c->seqnum;
202 if (!c->persistent) {
203 DLIST_REMOVE(cli->seqnum, c);
213 /****************************************************************************
215 ****************************************************************************/
217 bool cli_receive_smb(struct cli_state *cli)
223 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
228 len = client_receive_smb(cli, 0);
231 /* it might be an oplock break request */
232 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
233 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
234 SVAL(cli->inbuf,smb_vwv6) == 0 &&
235 SVAL(cli->inbuf,smb_vwv7) == 0) {
236 if (cli->oplock_handler) {
237 int fnum = SVAL(cli->inbuf,smb_vwv2);
238 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
239 if (!NT_STATUS_IS_OK(cli->oplock_handler(cli, fnum, level))) {
243 /* try to prevent loops */
244 SCVAL(cli->inbuf,smb_com,0xFF);
249 /* If the server is not responding, note that now */
252 * only log if the connection should still be open and not when
253 * the connection was closed due to a dropped ip message
256 char addr[INET6_ADDRSTRLEN];
257 print_sockaddr(addr, sizeof(addr), &cli->dest_ss);
258 DEBUG(0, ("Receiving SMB: Server %s stopped responding\n",
266 mid = SVAL(cli->inbuf,smb_mid);
267 seqnum = cli_state_get_seqnum(cli, mid);
269 if (!cli_check_sign_mac(cli, cli->inbuf, seqnum+1)) {
271 * If we get a signature failure in sessionsetup, then
272 * the server sometimes just reflects the sent signature
273 * back to us. Detect this and allow the upper layer to
274 * retrieve the correct Windows error message.
276 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
277 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
278 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
279 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
283 * Reflected signature on login error.
284 * Set bad sig but don't close fd.
286 cli->smb_rw_error = SMB_READ_BAD_SIG;
290 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
291 cli->smb_rw_error = SMB_READ_BAD_SIG;
299 static ssize_t write_socket(int fd, const char *buf, size_t len)
303 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
304 ret = write_data(fd,buf,len);
306 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
308 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
309 (int)len, fd, strerror(errno) ));
314 /****************************************************************************
316 ****************************************************************************/
318 bool cli_send_smb(struct cli_state *cli)
323 char *buf_out = cli->outbuf;
324 bool enc_on = cli_encryption_on(cli);
327 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
331 cli_calculate_sign_mac(cli, cli->outbuf, &seqnum);
333 if (!cli_state_set_seqnum(cli, cli->mid, seqnum)) {
334 DEBUG(0,("Failed to store mid[%u]/seqnum[%u]\n",
335 (unsigned int)cli->mid,
336 (unsigned int)seqnum));
341 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
343 if (!NT_STATUS_IS_OK(status)) {
346 cli->smb_rw_error = SMB_WRITE_ERROR;
347 DEBUG(0,("Error in encrypting client message. Error %s\n",
348 nt_errstr(status) ));
353 len = smb_len(buf_out) + 4;
355 while (nwritten < len) {
356 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
359 cli_free_enc_buffer(cli, buf_out);
363 cli->smb_rw_error = SMB_WRITE_ERROR;
364 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
365 (int)len,(int)ret, strerror(errno) ));
372 cli_free_enc_buffer(cli, buf_out);
375 /* Increment the mid so we can tell between responses. */
382 /****************************************************************************
383 Setup basics in a outgoing packet.
384 ****************************************************************************/
386 void cli_setup_packet_buf(struct cli_state *cli, char *buf)
390 SIVAL(buf,smb_rcls,0);
391 SSVAL(buf,smb_pid,cli->pid);
392 memset(buf+smb_pidhigh, 0, 12);
393 SSVAL(buf,smb_uid,cli->vuid);
394 SSVAL(buf,smb_mid,cli->mid);
396 if (cli->protocol <= PROTOCOL_CORE) {
400 if (cli->case_sensitive) {
401 SCVAL(buf,smb_flg,0x0);
403 /* Default setting, case insensitive. */
404 SCVAL(buf,smb_flg,0x8);
406 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
407 if (cli->capabilities & CAP_UNICODE)
408 flags2 |= FLAGS2_UNICODE_STRINGS;
409 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
410 flags2 |= FLAGS2_DFS_PATHNAMES;
411 if (cli->capabilities & CAP_STATUS32)
412 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
414 flags2 |= FLAGS2_EXTENDED_SECURITY;
415 SSVAL(buf,smb_flg2, flags2);
418 void cli_setup_packet(struct cli_state *cli)
420 cli_setup_packet_buf(cli, cli->outbuf);
423 /****************************************************************************
424 Setup the bcc length of the packet from a pointer to the end of the data.
425 ****************************************************************************/
427 void cli_setup_bcc(struct cli_state *cli, void *p)
429 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
432 /****************************************************************************
433 Initialize Domain, user or password.
434 ****************************************************************************/
436 NTSTATUS cli_set_domain(struct cli_state *cli, const char *domain)
438 TALLOC_FREE(cli->domain);
439 cli->domain = talloc_strdup(cli, domain ? domain : "");
440 if (cli->domain == NULL) {
441 return NT_STATUS_NO_MEMORY;
446 NTSTATUS cli_set_username(struct cli_state *cli, const char *username)
448 TALLOC_FREE(cli->user_name);
449 cli->user_name = talloc_strdup(cli, username ? username : "");
450 if (cli->user_name == NULL) {
451 return NT_STATUS_NO_MEMORY;
456 NTSTATUS cli_set_password(struct cli_state *cli, const char *password)
458 TALLOC_FREE(cli->password);
460 /* Password can be NULL. */
462 cli->password = talloc_strdup(cli, password);
463 if (cli->password == NULL) {
464 return NT_STATUS_NO_MEMORY;
467 /* Use zero NTLMSSP hashes and session key. */
468 cli->password = NULL;
474 /****************************************************************************
475 Initialise credentials of a client structure.
476 ****************************************************************************/
478 NTSTATUS cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
480 NTSTATUS status = cli_set_username(cli, username);
481 if (!NT_STATUS_IS_OK(status)) {
484 status = cli_set_domain(cli, domain);
485 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
490 return cli_set_password(cli, password);
493 /****************************************************************************
494 Initialise a client structure. Always returns a talloc'ed struct.
495 Set the signing state (used from the command line).
496 ****************************************************************************/
498 struct cli_state *cli_initialise_ex(int signing_state)
500 struct cli_state *cli = NULL;
501 bool allow_smb_signing = false;
502 bool mandatory_signing = false;
504 /* Check the effective uid - make sure we are not setuid */
505 if (is_setuid_root()) {
506 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
510 cli = TALLOC_ZERO_P(NULL, struct cli_state);
515 cli->dfs_mountpoint = talloc_strdup(cli, "");
516 if (!cli->dfs_mountpoint) {
522 cli->pid = (uint16)sys_getpid();
524 cli->vuid = UID_FIELD_INVALID;
525 cli->protocol = PROTOCOL_NT1;
526 cli->timeout = 20000; /* Timeout is in milliseconds. */
527 cli->bufsize = CLI_BUFFER_SIZE+4;
528 cli->max_xmit = cli->bufsize;
529 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
531 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
532 cli->oplock_handler = cli_oplock_ack;
533 cli->case_sensitive = false;
534 cli->smb_rw_error = SMB_READ_OK;
536 cli->use_spnego = lp_client_use_spnego();
538 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
540 /* Set the CLI_FORCE_DOSERR environment variable to test
541 client routines using DOS errors instead of STATUS32
542 ones. This intended only as a temporary hack. */
543 if (getenv("CLI_FORCE_DOSERR"))
544 cli->force_dos_errors = true;
546 if (lp_client_signing()) {
547 allow_smb_signing = true;
550 if (lp_client_signing() == Required) {
551 mandatory_signing = true;
554 if (signing_state != Undefined) {
555 allow_smb_signing = true;
558 if (signing_state == false) {
559 allow_smb_signing = false;
560 mandatory_signing = false;
563 if (signing_state == Required) {
564 mandatory_signing = true;
567 if (!cli->outbuf || !cli->inbuf)
570 memset(cli->outbuf, 0, cli->bufsize);
571 memset(cli->inbuf, 0, cli->bufsize);
573 /* initialise signing */
574 cli->signing_state = smb_signing_init(cli,
577 if (!cli->signing_state) {
581 cli->outgoing = tevent_queue_create(cli, "cli_outgoing");
582 if (cli->outgoing == NULL) {
587 cli->initialised = 1;
591 /* Clean up after malloc() error */
595 SAFE_FREE(cli->inbuf);
596 SAFE_FREE(cli->outbuf);
601 struct cli_state *cli_initialise(void)
603 return cli_initialise_ex(Undefined);
606 /****************************************************************************
607 Close all pipes open on this session.
608 ****************************************************************************/
610 void cli_nt_pipes_close(struct cli_state *cli)
612 while (cli->pipe_list != NULL) {
614 * No TALLOC_FREE here!
616 talloc_free(cli->pipe_list);
620 /****************************************************************************
621 Shutdown a client structure.
622 ****************************************************************************/
624 static void _cli_shutdown(struct cli_state *cli)
626 cli_nt_pipes_close(cli);
629 * tell our peer to free his resources. Wihtout this, when an
630 * application attempts to do a graceful shutdown and calls
631 * smbc_free_context() to clean up all connections, some connections
632 * can remain active on the peer end, until some (long) timeout period
633 * later. This tree disconnect forces the peer to clean up, since the
634 * connection will be going away.
636 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
637 * the only user for this so far is smbmount which passes opened connection
638 * down to kernel's smbfs module.
640 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
644 SAFE_FREE(cli->outbuf);
645 SAFE_FREE(cli->inbuf);
647 data_blob_free(&cli->secblob);
648 data_blob_free(&cli->user_session_key);
654 cli->smb_rw_error = SMB_READ_OK;
657 * Need to free pending first, they remove themselves
659 while (cli->pending) {
660 talloc_free(cli->pending[0]);
665 void cli_shutdown(struct cli_state *cli)
667 struct cli_state *cli_head;
671 DLIST_HEAD(cli, cli_head);
672 if (cli_head == cli) {
674 * head of a DFS list, shutdown all subsidiary DFS
677 struct cli_state *p, *next;
679 for (p = cli_head->next; p; p = next) {
681 DLIST_REMOVE(cli_head, p);
685 DLIST_REMOVE(cli_head, cli);
691 /****************************************************************************
692 Set socket options on a open connection.
693 ****************************************************************************/
695 void cli_sockopt(struct cli_state *cli, const char *options)
697 set_socket_options(cli->fd, options);
700 /****************************************************************************
701 Set the PID to use for smb messages. Return the old pid.
702 ****************************************************************************/
704 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
706 uint16 ret = cli->pid;
711 /****************************************************************************
712 Set the case sensitivity flag on the packets. Returns old state.
713 ****************************************************************************/
715 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
717 bool ret = cli->case_sensitive;
718 cli->case_sensitive = case_sensitive;
722 struct cli_echo_state {
728 static void cli_echo_done(struct tevent_req *subreq);
730 struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
731 struct cli_state *cli, uint16_t num_echos,
734 struct tevent_req *req, *subreq;
735 struct cli_echo_state *state;
737 req = tevent_req_create(mem_ctx, &state, struct cli_echo_state);
741 SSVAL(state->vwv, 0, num_echos);
743 state->num_echos = num_echos;
745 subreq = cli_smb_send(state, ev, cli, SMBecho, 0, 1, state->vwv,
746 data.length, data.data);
747 if (subreq == NULL) {
750 tevent_req_set_callback(subreq, cli_echo_done, req);
757 static void cli_echo_done(struct tevent_req *subreq)
759 struct tevent_req *req = tevent_req_callback_data(
760 subreq, struct tevent_req);
761 struct cli_echo_state *state = tevent_req_data(
762 req, struct cli_echo_state);
768 status = cli_smb_recv(subreq, state, &inbuf, 0, NULL, NULL,
770 if (!NT_STATUS_IS_OK(status)) {
771 tevent_req_nterror(req, status);
774 if ((num_bytes != state->data.length)
775 || (memcmp(bytes, state->data.data, num_bytes) != 0)) {
776 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
780 state->num_echos -=1;
781 if (state->num_echos == 0) {
782 tevent_req_done(req);
786 if (!cli_smb_req_set_pending(subreq)) {
787 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
793 * Get the result out from an echo request
794 * @param[in] req The async_req from cli_echo_send
795 * @retval Did the server reply correctly?
798 NTSTATUS cli_echo_recv(struct tevent_req *req)
800 return tevent_req_simple_recv_ntstatus(req);
804 * @brief Send/Receive SMBEcho requests
805 * @param[in] mem_ctx The memory context to put the async_req on
806 * @param[in] ev The event context that will call us back
807 * @param[in] cli The connection to send the echo to
808 * @param[in] num_echos How many times do we want to get the reply?
809 * @param[in] data The data we want to get back
810 * @retval Did the server reply correctly?
813 NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
815 TALLOC_CTX *frame = talloc_stackframe();
816 struct event_context *ev;
817 struct tevent_req *req;
818 NTSTATUS status = NT_STATUS_OK;
820 if (cli_has_async_calls(cli)) {
822 * Can't use sync call while an async call is in flight
824 status = NT_STATUS_INVALID_PARAMETER;
828 ev = event_context_init(frame);
830 status = NT_STATUS_NO_MEMORY;
834 req = cli_echo_send(frame, ev, cli, num_echos, data);
836 status = NT_STATUS_NO_MEMORY;
840 if (!tevent_req_poll(req, ev)) {
841 status = map_nt_error_from_unix(errno);
845 status = cli_echo_recv(req);
848 if (!NT_STATUS_IS_OK(status)) {
849 cli_set_error(cli, status);
855 * Is the SMB command able to hold an AND_X successor
856 * @param[in] cmd The SMB command in question
857 * @retval Can we add a chained request after "cmd"?
859 bool is_andx_req(uint8_t cmd)
879 NTSTATUS cli_smb(TALLOC_CTX *mem_ctx, struct cli_state *cli,
880 uint8_t smb_command, uint8_t additional_flags,
881 uint8_t wct, uint16_t *vwv,
882 uint32_t num_bytes, const uint8_t *bytes,
883 struct tevent_req **result_parent,
884 uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
885 uint32_t *pnum_bytes, uint8_t **pbytes)
887 struct tevent_context *ev;
888 struct tevent_req *req = NULL;
889 NTSTATUS status = NT_STATUS_NO_MEMORY;
891 if (cli_has_async_calls(cli)) {
892 return NT_STATUS_INVALID_PARAMETER;
894 ev = tevent_context_init(mem_ctx);
898 req = cli_smb_send(mem_ctx, ev, cli, smb_command, additional_flags,
899 wct, vwv, num_bytes, bytes);
903 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
906 status = cli_smb_recv(req, NULL, NULL, min_wct, pwct, pvwv,
910 if (NT_STATUS_IS_OK(status) && (result_parent != NULL)) {
911 *result_parent = req;