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 int cli_set_port(struct cli_state *cli, int port)
59 /****************************************************************************
60 Read an smb from a fd ignoring all keepalive packets.
61 The timeout is in milliseconds
63 This is exactly the same as receive_smb except that it never returns
64 a session keepalive packet (just as receive_smb used to do).
65 receive_smb was changed to return keepalives as the oplock processing means this call
66 should never go into a blocking read.
67 ****************************************************************************/
69 static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
76 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
78 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize,
79 cli->timeout, maxlen, &len);
80 if (!NT_STATUS_IS_OK(status)) {
81 DEBUG(10,("client_receive_smb failed\n"));
84 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
85 set_smb_read_error(&cli->smb_rw_error,
90 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
91 set_smb_read_error(&cli->smb_rw_error,
96 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
101 * I don't believe len can be < 0 with NT_STATUS_OK
102 * returned above, but this check doesn't hurt. JRA.
105 if ((ssize_t)len < 0) {
109 /* Ignore session keepalive packets. */
110 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
115 if (cli_encryption_on(cli)) {
116 NTSTATUS status = cli_decrypt_message(cli);
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
120 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
125 show_msg(cli->inbuf);
129 /****************************************************************************
131 ****************************************************************************/
133 bool cli_receive_smb(struct cli_state *cli)
137 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
142 len = client_receive_smb(cli, 0);
145 /* it might be an oplock break request */
146 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
147 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
148 SVAL(cli->inbuf,smb_vwv6) == 0 &&
149 SVAL(cli->inbuf,smb_vwv7) == 0) {
150 if (cli->oplock_handler) {
151 int fnum = SVAL(cli->inbuf,smb_vwv2);
152 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
153 if (!cli->oplock_handler(cli, fnum, level)) {
157 /* try to prevent loops */
158 SCVAL(cli->inbuf,smb_com,0xFF);
163 /* If the server is not responding, note that now */
165 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
171 if (!cli_check_sign_mac(cli, cli->inbuf)) {
173 * If we get a signature failure in sessionsetup, then
174 * the server sometimes just reflects the sent signature
175 * back to us. Detect this and allow the upper layer to
176 * retrieve the correct Windows error message.
178 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
179 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
180 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
181 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
185 * Reflected signature on login error.
186 * Set bad sig but don't close fd.
188 cli->smb_rw_error = SMB_READ_BAD_SIG;
192 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
193 cli->smb_rw_error = SMB_READ_BAD_SIG;
201 /****************************************************************************
202 Read the data portion of a readX smb.
203 The timeout is in milliseconds
204 ****************************************************************************/
206 ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
210 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
212 status = read_socket_with_timeout(
213 cli->fd, buffer, len, len, cli->timeout, NULL);
214 if (NT_STATUS_IS_OK(status)) {
218 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
219 set_smb_read_error(&cli->smb_rw_error, SMB_READ_EOF);
223 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
224 set_smb_read_error(&cli->smb_rw_error, SMB_READ_TIMEOUT);
228 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
232 static ssize_t write_socket(int fd, const char *buf, size_t len)
236 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
237 ret = write_data(fd,buf,len);
239 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
241 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
242 (int)len, fd, strerror(errno) ));
247 /****************************************************************************
249 ****************************************************************************/
251 bool cli_send_smb(struct cli_state *cli)
256 char *buf_out = cli->outbuf;
257 bool enc_on = cli_encryption_on(cli);
259 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
263 cli_calculate_sign_mac(cli, cli->outbuf);
266 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
268 if (!NT_STATUS_IS_OK(status)) {
271 cli->smb_rw_error = SMB_WRITE_ERROR;
272 DEBUG(0,("Error in encrypting client message. Error %s\n",
273 nt_errstr(status) ));
278 len = smb_len(buf_out) + 4;
280 while (nwritten < len) {
281 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
284 cli_free_enc_buffer(cli, buf_out);
288 cli->smb_rw_error = SMB_WRITE_ERROR;
289 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
290 (int)len,(int)ret, strerror(errno) ));
297 cli_free_enc_buffer(cli, buf_out);
300 /* Increment the mid so we can tell between responses. */
307 /****************************************************************************
308 Send a "direct" writeX smb to a fd.
309 ****************************************************************************/
311 bool cli_send_smb_direct_writeX(struct cli_state *cli,
315 /* First length to send is the offset to the data. */
316 size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
320 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
325 if (client_is_signing_on(cli)) {
326 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
330 while (nwritten < len) {
331 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
335 cli->smb_rw_error = SMB_WRITE_ERROR;
336 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
337 (int)len,(int)ret, strerror(errno) ));
343 /* Now write the extra data. */
345 while (nwritten < extradata) {
346 ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
350 cli->smb_rw_error = SMB_WRITE_ERROR;
351 DEBUG(0,("Error writing %d extradata "
352 "bytes to client. %d (%s)\n",
353 (int)extradata,(int)ret, strerror(errno) ));
359 /* Increment the mid so we can tell between responses. */
366 /****************************************************************************
367 Setup basics in a outgoing packet.
368 ****************************************************************************/
370 void cli_setup_packet_buf(struct cli_state *cli, char *buf)
374 SIVAL(buf,smb_rcls,0);
375 SSVAL(buf,smb_pid,cli->pid);
376 memset(buf+smb_pidhigh, 0, 12);
377 SSVAL(buf,smb_uid,cli->vuid);
378 SSVAL(buf,smb_mid,cli->mid);
380 if (cli->protocol <= PROTOCOL_CORE) {
384 if (cli->case_sensitive) {
385 SCVAL(buf,smb_flg,0x0);
387 /* Default setting, case insensitive. */
388 SCVAL(buf,smb_flg,0x8);
390 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
391 if (cli->capabilities & CAP_UNICODE)
392 flags2 |= FLAGS2_UNICODE_STRINGS;
393 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
394 flags2 |= FLAGS2_DFS_PATHNAMES;
395 if (cli->capabilities & CAP_STATUS32)
396 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
398 flags2 |= FLAGS2_EXTENDED_SECURITY;
399 SSVAL(buf,smb_flg2, flags2);
402 void cli_setup_packet(struct cli_state *cli)
404 cli_setup_packet_buf(cli, cli->outbuf);
407 /****************************************************************************
408 Setup the bcc length of the packet from a pointer to the end of the data.
409 ****************************************************************************/
411 void cli_setup_bcc(struct cli_state *cli, void *p)
413 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
416 /****************************************************************************
417 Initialise credentials of a client structure.
418 ****************************************************************************/
420 void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
422 fstrcpy(cli->domain, domain);
423 fstrcpy(cli->user_name, username);
424 pwd_set_cleartext(&cli->pwd, password);
426 cli->pwd.null_pwd = true;
429 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
432 /****************************************************************************
433 Set the signing state (used from the command line).
434 ****************************************************************************/
436 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
438 if (signing_state == Undefined)
441 if (signing_state == false) {
442 cli->sign_info.allow_smb_signing = false;
443 cli->sign_info.mandatory_signing = false;
447 cli->sign_info.allow_smb_signing = true;
449 if (signing_state == Required)
450 cli->sign_info.mandatory_signing = true;
453 /****************************************************************************
454 Initialise a client structure. Always returns a malloc'ed struct.
455 ****************************************************************************/
457 struct cli_state *cli_initialise(void)
459 struct cli_state *cli = NULL;
461 /* Check the effective uid - make sure we are not setuid */
462 if (is_setuid_root()) {
463 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
467 cli = TALLOC_ZERO_P(NULL, struct cli_state);
475 cli->pid = (uint16)sys_getpid();
477 cli->vuid = UID_FIELD_INVALID;
478 cli->protocol = PROTOCOL_NT1;
479 cli->timeout = 20000; /* Timeout is in milliseconds. */
480 cli->bufsize = CLI_BUFFER_SIZE+4;
481 cli->max_xmit = cli->bufsize;
482 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
483 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
484 cli->oplock_handler = cli_oplock_ack;
485 cli->case_sensitive = false;
486 cli->smb_rw_error = SMB_READ_OK;
488 cli->use_spnego = lp_client_use_spnego();
490 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
492 /* Set the CLI_FORCE_DOSERR environment variable to test
493 client routines using DOS errors instead of STATUS32
494 ones. This intended only as a temporary hack. */
495 if (getenv("CLI_FORCE_DOSERR"))
496 cli->force_dos_errors = true;
498 if (lp_client_signing())
499 cli->sign_info.allow_smb_signing = true;
501 if (lp_client_signing() == Required)
502 cli->sign_info.mandatory_signing = true;
504 if (!cli->outbuf || !cli->inbuf)
507 memset(cli->outbuf, 0, cli->bufsize);
508 memset(cli->inbuf, 0, cli->bufsize);
511 #if defined(DEVELOPER)
512 /* just because we over-allocate, doesn't mean it's right to use it */
513 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
514 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
517 /* initialise signing */
518 cli_null_set_signing(cli);
520 cli->initialised = 1;
524 /* Clean up after malloc() error */
528 SAFE_FREE(cli->inbuf);
529 SAFE_FREE(cli->outbuf);
534 /****************************************************************************
535 Close all pipes open on this session.
536 ****************************************************************************/
538 void cli_nt_pipes_close(struct cli_state *cli)
540 while (cli->pipe_list != NULL) {
542 * No TALLOC_FREE here!
544 talloc_free(cli->pipe_list);
548 /****************************************************************************
549 Shutdown a client structure.
550 ****************************************************************************/
552 void cli_shutdown(struct cli_state *cli)
554 cli_nt_pipes_close(cli);
557 * tell our peer to free his resources. Wihtout this, when an
558 * application attempts to do a graceful shutdown and calls
559 * smbc_free_context() to clean up all connections, some connections
560 * can remain active on the peer end, until some (long) timeout period
561 * later. This tree disconnect forces the peer to clean up, since the
562 * connection will be going away.
564 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
565 * the only user for this so far is smbmount which passes opened connection
566 * down to kernel's smbfs module.
568 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
572 SAFE_FREE(cli->outbuf);
573 SAFE_FREE(cli->inbuf);
575 cli_free_signing_context(cli);
576 data_blob_free(&cli->secblob);
577 data_blob_free(&cli->user_session_key);
583 cli->smb_rw_error = SMB_READ_OK;
588 /****************************************************************************
589 Set socket options on a open connection.
590 ****************************************************************************/
592 void cli_sockopt(struct cli_state *cli, const char *options)
594 set_socket_options(cli->fd, options);
597 /****************************************************************************
598 Set the PID to use for smb messages. Return the old pid.
599 ****************************************************************************/
601 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
603 uint16 ret = cli->pid;
608 /****************************************************************************
609 Set the case sensitivity flag on the packets. Returns old state.
610 ****************************************************************************/
612 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
614 bool ret = cli->case_sensitive;
615 cli->case_sensitive = case_sensitive;
619 /****************************************************************************
620 Send a keepalive packet to the server
621 ****************************************************************************/
623 bool cli_send_keepalive(struct cli_state *cli)
626 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
629 if (!send_keepalive(cli->fd)) {
632 DEBUG(0,("Error sending keepalive packet to client.\n"));
639 * @brief: Collect a echo reply
640 * @param[in] req The corresponding async request
642 * There might be more than one echo reply. This helper pulls the reply out of
643 * the data stream. If all expected replies have arrived, declare the
647 static void cli_echo_recv_helper(struct async_req *req)
649 struct cli_request *cli_req;
656 status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
657 if (!NT_STATUS_IS_OK(status)) {
658 async_req_error(req, status);
662 cli_req = talloc_get_type_abort(req->private_data, struct cli_request);
664 if ((num_bytes != cli_req->data.echo.data.length)
665 || (memcmp(cli_req->data.echo.data.data, bytes,
667 async_req_error(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
671 cli_req->data.echo.num_echos -= 1;
673 if (cli_req->data.echo.num_echos == 0) {
674 client_set_trans_sign_state_off(cli_req->cli, cli_req->mid);
683 * @brief Send SMBEcho requests
684 * @param[in] mem_ctx The memory context to put the async_req on
685 * @param[in] ev The event context that will call us back
686 * @param[in] cli The connection to send the echo to
687 * @param[in] num_echos How many times do we want to get the reply?
688 * @param[in] data The data we want to get back
689 * @retval The async request
692 struct async_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
693 struct cli_state *cli, uint16_t num_echos,
698 struct async_req *result;
699 struct cli_request *req;
701 SSVAL(vwv, 0, num_echos);
703 data_copy = (uint8_t *)talloc_memdup(mem_ctx, data.data, data.length);
704 if (data_copy == NULL) {
708 result = cli_request_send(mem_ctx, ev, cli, SMBecho, 0, 1, vwv,
709 data.length, data.data);
710 if (result == NULL) {
711 TALLOC_FREE(data_copy);
714 req = talloc_get_type_abort(result->private_data, struct cli_request);
716 client_set_trans_sign_state_on(cli, req->mid);
718 req->data.echo.num_echos = num_echos;
719 req->data.echo.data.data = talloc_move(req, &data_copy);
720 req->data.echo.data.length = data.length;
722 req->recv_helper.fn = cli_echo_recv_helper;
728 * Get the result out from an echo request
729 * @param[in] req The async_req from cli_echo_send
730 * @retval Did the server reply correctly?
733 NTSTATUS cli_echo_recv(struct async_req *req)
735 SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
736 if (req->state == ASYNC_REQ_ERROR) {
744 * @brief Send/Receive SMBEcho requests
745 * @param[in] mem_ctx The memory context to put the async_req on
746 * @param[in] ev The event context that will call us back
747 * @param[in] cli The connection to send the echo to
748 * @param[in] num_echos How many times do we want to get the reply?
749 * @param[in] data The data we want to get back
750 * @retval Did the server reply correctly?
753 NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
755 TALLOC_CTX *frame = talloc_stackframe();
756 struct event_context *ev;
757 struct async_req *req;
758 NTSTATUS status = NT_STATUS_NO_MEMORY;
760 if (cli->fd_event != NULL) {
762 * Can't use sync call while an async call is in flight
764 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
768 ev = event_context_init(frame);
773 req = cli_echo_send(frame, ev, cli, num_echos, data);
778 while (req->state < ASYNC_REQ_DONE) {
782 status = cli_echo_recv(req);