build: fix the build.
[Samba/gebeck_regimport.git] / source3 / libsmb / clientgen.c
blobe64b6fa278a233f08ac24102c5ff23e33e90ff9b
1 /*
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/>.
21 #include "includes.h"
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;
46 return old_timeout;
49 /****************************************************************************
50 Change the port number used to call on.
51 ****************************************************************************/
53 int cli_set_port(struct cli_state *cli, int port)
55 cli->port = port;
56 return port;
59 /****************************************************************************
60 Read an smb from a fd ignoring all keepalive packets. Note that the buffer
61 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
62 The timeout is in milliseconds
64 This is exactly the same as receive_smb except that it never returns
65 a session keepalive packet (just as receive_smb used to do).
66 receive_smb was changed to return keepalives as the oplock processing means this call
67 should never go into a blocking read.
68 ****************************************************************************/
70 static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
72 size_t len;
74 for(;;) {
75 NTSTATUS status;
77 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
79 status = receive_smb_raw(cli->fd, cli->inbuf, cli->timeout,
80 maxlen, &len);
81 if (!NT_STATUS_IS_OK(status)) {
82 DEBUG(10,("client_receive_smb failed\n"));
83 show_msg(cli->inbuf);
85 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
86 set_smb_read_error(&cli->smb_rw_error,
87 SMB_READ_EOF);
88 return -1;
91 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
92 set_smb_read_error(&cli->smb_rw_error,
93 SMB_READ_TIMEOUT);
94 return -1;
97 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
98 return -1;
101 if (len < 0) {
102 return len;
105 /* Ignore session keepalive packets. */
106 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
107 break;
111 if (cli_encryption_on(cli)) {
112 NTSTATUS status = cli_decrypt_message(cli);
113 if (!NT_STATUS_IS_OK(status)) {
114 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
115 nt_errstr(status)));
116 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
117 return -1;
121 show_msg(cli->inbuf);
122 return len;
125 /****************************************************************************
126 Recv an smb.
127 ****************************************************************************/
129 bool cli_receive_smb(struct cli_state *cli)
131 ssize_t len;
133 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
134 if (cli->fd == -1)
135 return false;
137 again:
138 len = client_receive_smb(cli, 0);
140 if (len > 0) {
141 /* it might be an oplock break request */
142 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
143 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
144 SVAL(cli->inbuf,smb_vwv6) == 0 &&
145 SVAL(cli->inbuf,smb_vwv7) == 0) {
146 if (cli->oplock_handler) {
147 int fnum = SVAL(cli->inbuf,smb_vwv2);
148 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
149 if (!cli->oplock_handler(cli, fnum, level)) {
150 return false;
153 /* try to prevent loops */
154 SCVAL(cli->inbuf,smb_com,0xFF);
155 goto again;
159 /* If the server is not responding, note that now */
160 if (len < 0) {
161 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
162 close(cli->fd);
163 cli->fd = -1;
164 return false;
167 if (!cli_check_sign_mac(cli, cli->inbuf)) {
169 * If we get a signature failure in sessionsetup, then
170 * the server sometimes just reflects the sent signature
171 * back to us. Detect this and allow the upper layer to
172 * retrieve the correct Windows error message.
174 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
175 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
176 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
177 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
178 cli_is_error(cli)) {
181 * Reflected signature on login error.
182 * Set bad sig but don't close fd.
184 cli->smb_rw_error = SMB_READ_BAD_SIG;
185 return true;
188 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
189 cli->smb_rw_error = SMB_READ_BAD_SIG;
190 close(cli->fd);
191 cli->fd = -1;
192 return false;
194 return true;
197 /****************************************************************************
198 Read the data portion of a readX smb.
199 The timeout is in milliseconds
200 ****************************************************************************/
202 ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
204 NTSTATUS status;
206 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
208 status = read_socket_with_timeout(
209 cli->fd, buffer, len, len, cli->timeout, NULL);
210 if (NT_STATUS_IS_OK(status)) {
211 return len;
214 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
215 set_smb_read_error(&cli->smb_rw_error, SMB_READ_EOF);
216 return -1;
219 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
220 set_smb_read_error(&cli->smb_rw_error, SMB_READ_TIMEOUT);
221 return -1;
224 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
225 return -1;
228 /****************************************************************************
229 Read a smb readX header.
230 We can only use this if encryption and signing are off.
231 ****************************************************************************/
233 bool cli_receive_smb_readX_header(struct cli_state *cli)
235 ssize_t len, offset;
237 if (cli->fd == -1)
238 return false;
240 again:
242 /* Read up to the size of a readX header reply. */
243 len = client_receive_smb(cli, (smb_size - 4) + 24);
245 if (len > 0) {
246 /* it might be an oplock break request */
247 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
248 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
249 SVAL(cli->inbuf,smb_vwv6) == 0 &&
250 SVAL(cli->inbuf,smb_vwv7) == 0) {
251 ssize_t total_len = smb_len(cli->inbuf);
253 if (total_len > CLI_SAMBA_MAX_LARGE_READX_SIZE+SAFETY_MARGIN) {
254 goto read_err;
257 /* Read the rest of the data. */
258 if ((total_len - len > 0) &&
259 !cli_receive_smb_data(cli,cli->inbuf+len,total_len - len)) {
260 goto read_err;
263 if (cli->oplock_handler) {
264 int fnum = SVAL(cli->inbuf,smb_vwv2);
265 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
266 if (!cli->oplock_handler(cli, fnum, level)) return false;
268 /* try to prevent loops */
269 SCVAL(cli->inbuf,smb_com,0xFF);
270 goto again;
274 /* If it's not the above size it probably was an error packet. */
276 if ((len == (smb_size - 4) + 24) && !cli_is_error(cli)) {
277 /* Check it's a non-chained readX reply. */
278 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) ||
279 (CVAL(cli->inbuf,smb_vwv0) != 0xFF) ||
280 (CVAL(cli->inbuf,smb_com) != SMBreadX)) {
282 * We're not coping here with asnyc replies to
283 * other calls. Punt here - we need async client
284 * libs for this.
286 goto read_err;
290 * We know it's a readX reply - ensure we've read the
291 * padding bytes also.
294 offset = SVAL(cli->inbuf,smb_vwv6);
295 if (offset > len) {
296 ssize_t ret;
297 size_t padbytes = offset - len;
298 ret = cli_receive_smb_data(cli,smb_buf(cli->inbuf),padbytes);
299 if (ret != padbytes) {
300 goto read_err;
305 return true;
307 read_err:
309 cli->smb_rw_error = SMB_READ_ERROR;
310 close(cli->fd);
311 cli->fd = -1;
312 return false;
315 static ssize_t write_socket(int fd, const char *buf, size_t len)
317 ssize_t ret=0;
319 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
320 ret = write_data(fd,buf,len);
322 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
323 if(ret <= 0)
324 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
325 (int)len, fd, strerror(errno) ));
327 return(ret);
330 /****************************************************************************
331 Send an smb to a fd.
332 ****************************************************************************/
334 bool cli_send_smb(struct cli_state *cli)
336 size_t len;
337 size_t nwritten=0;
338 ssize_t ret;
339 char *buf_out = cli->outbuf;
340 bool enc_on = cli_encryption_on(cli);
342 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
343 if (cli->fd == -1)
344 return false;
346 cli_calculate_sign_mac(cli, cli->outbuf);
348 if (enc_on) {
349 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
350 &buf_out);
351 if (!NT_STATUS_IS_OK(status)) {
352 close(cli->fd);
353 cli->fd = -1;
354 cli->smb_rw_error = SMB_WRITE_ERROR;
355 DEBUG(0,("Error in encrypting client message. Error %s\n",
356 nt_errstr(status) ));
357 return false;
361 len = smb_len(buf_out) + 4;
363 while (nwritten < len) {
364 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
365 if (ret <= 0) {
366 if (enc_on) {
367 cli_free_enc_buffer(cli, buf_out);
369 close(cli->fd);
370 cli->fd = -1;
371 cli->smb_rw_error = SMB_WRITE_ERROR;
372 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
373 (int)len,(int)ret, strerror(errno) ));
374 return false;
376 nwritten += ret;
379 if (enc_on) {
380 cli_free_enc_buffer(cli, buf_out);
383 /* Increment the mid so we can tell between responses. */
384 cli->mid++;
385 if (!cli->mid)
386 cli->mid++;
387 return true;
390 /****************************************************************************
391 Send a "direct" writeX smb to a fd.
392 ****************************************************************************/
394 bool cli_send_smb_direct_writeX(struct cli_state *cli,
395 const char *p,
396 size_t extradata)
398 /* First length to send is the offset to the data. */
399 size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
400 size_t nwritten=0;
401 ssize_t ret;
403 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
404 if (cli->fd == -1) {
405 return false;
408 if (client_is_signing_on(cli)) {
409 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
410 return false;
413 while (nwritten < len) {
414 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
415 if (ret <= 0) {
416 close(cli->fd);
417 cli->fd = -1;
418 cli->smb_rw_error = SMB_WRITE_ERROR;
419 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
420 (int)len,(int)ret, strerror(errno) ));
421 return false;
423 nwritten += ret;
426 /* Now write the extra data. */
427 nwritten=0;
428 while (nwritten < extradata) {
429 ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
430 if (ret <= 0) {
431 close(cli->fd);
432 cli->fd = -1;
433 cli->smb_rw_error = SMB_WRITE_ERROR;
434 DEBUG(0,("Error writing %d extradata "
435 "bytes to client. %d (%s)\n",
436 (int)extradata,(int)ret, strerror(errno) ));
437 return false;
439 nwritten += ret;
442 /* Increment the mid so we can tell between responses. */
443 cli->mid++;
444 if (!cli->mid)
445 cli->mid++;
446 return true;
449 /****************************************************************************
450 Setup basics in a outgoing packet.
451 ****************************************************************************/
453 void cli_setup_packet_buf(struct cli_state *cli, char *buf)
455 uint16 flags2;
456 cli->rap_error = 0;
457 SIVAL(buf,smb_rcls,0);
458 SSVAL(buf,smb_pid,cli->pid);
459 memset(buf+smb_pidhigh, 0, 12);
460 SSVAL(buf,smb_uid,cli->vuid);
461 SSVAL(buf,smb_mid,cli->mid);
463 if (cli->protocol <= PROTOCOL_CORE) {
464 return;
467 if (cli->case_sensitive) {
468 SCVAL(buf,smb_flg,0x0);
469 } else {
470 /* Default setting, case insensitive. */
471 SCVAL(buf,smb_flg,0x8);
473 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
474 if (cli->capabilities & CAP_UNICODE)
475 flags2 |= FLAGS2_UNICODE_STRINGS;
476 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
477 flags2 |= FLAGS2_DFS_PATHNAMES;
478 if (cli->capabilities & CAP_STATUS32)
479 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
480 if (cli->use_spnego)
481 flags2 |= FLAGS2_EXTENDED_SECURITY;
482 SSVAL(buf,smb_flg2, flags2);
485 void cli_setup_packet(struct cli_state *cli)
487 cli_setup_packet_buf(cli, cli->outbuf);
490 /****************************************************************************
491 Setup the bcc length of the packet from a pointer to the end of the data.
492 ****************************************************************************/
494 void cli_setup_bcc(struct cli_state *cli, void *p)
496 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
499 /****************************************************************************
500 Initialise credentials of a client structure.
501 ****************************************************************************/
503 void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
505 fstrcpy(cli->domain, domain);
506 fstrcpy(cli->user_name, username);
507 pwd_set_cleartext(&cli->pwd, password);
508 if (!*username) {
509 cli->pwd.null_pwd = true;
512 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
515 /****************************************************************************
516 Set the signing state (used from the command line).
517 ****************************************************************************/
519 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
521 if (signing_state == Undefined)
522 return;
524 if (signing_state == false) {
525 cli->sign_info.allow_smb_signing = false;
526 cli->sign_info.mandatory_signing = false;
527 return;
530 cli->sign_info.allow_smb_signing = true;
532 if (signing_state == Required)
533 cli->sign_info.mandatory_signing = true;
536 /****************************************************************************
537 Initialise a client structure. Always returns a malloc'ed struct.
538 ****************************************************************************/
540 struct cli_state *cli_initialise(void)
542 struct cli_state *cli = NULL;
544 /* Check the effective uid - make sure we are not setuid */
545 if (is_setuid_root()) {
546 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
547 return NULL;
550 cli = talloc(NULL, struct cli_state);
551 if (!cli) {
552 return NULL;
555 ZERO_STRUCTP(cli);
557 cli->port = 0;
558 cli->fd = -1;
559 cli->cnum = -1;
560 cli->pid = (uint16)sys_getpid();
561 cli->mid = 1;
562 cli->vuid = UID_FIELD_INVALID;
563 cli->protocol = PROTOCOL_NT1;
564 cli->timeout = 20000; /* Timeout is in milliseconds. */
565 cli->bufsize = CLI_BUFFER_SIZE+4;
566 cli->max_xmit = cli->bufsize;
567 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
568 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
569 cli->oplock_handler = cli_oplock_ack;
570 cli->case_sensitive = false;
571 cli->smb_rw_error = SMB_READ_OK;
573 cli->use_spnego = lp_client_use_spnego();
575 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
577 /* Set the CLI_FORCE_DOSERR environment variable to test
578 client routines using DOS errors instead of STATUS32
579 ones. This intended only as a temporary hack. */
580 if (getenv("CLI_FORCE_DOSERR"))
581 cli->force_dos_errors = true;
583 if (lp_client_signing())
584 cli->sign_info.allow_smb_signing = true;
586 if (lp_client_signing() == Required)
587 cli->sign_info.mandatory_signing = true;
589 if (!cli->outbuf || !cli->inbuf)
590 goto error;
592 memset(cli->outbuf, 0, cli->bufsize);
593 memset(cli->inbuf, 0, cli->bufsize);
596 #if defined(DEVELOPER)
597 /* just because we over-allocate, doesn't mean it's right to use it */
598 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
599 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
600 #endif
602 /* initialise signing */
603 cli_null_set_signing(cli);
605 cli->initialised = 1;
607 return cli;
609 /* Clean up after malloc() error */
611 error:
613 SAFE_FREE(cli->inbuf);
614 SAFE_FREE(cli->outbuf);
615 SAFE_FREE(cli);
616 return NULL;
619 /****************************************************************************
620 Close all pipes open on this session.
621 ****************************************************************************/
623 void cli_nt_pipes_close(struct cli_state *cli)
625 while (cli->pipe_list != NULL) {
627 * No TALLOC_FREE here!
629 talloc_free(cli->pipe_list);
633 /****************************************************************************
634 Shutdown a client structure.
635 ****************************************************************************/
637 void cli_shutdown(struct cli_state *cli)
639 cli_nt_pipes_close(cli);
642 * tell our peer to free his resources. Wihtout this, when an
643 * application attempts to do a graceful shutdown and calls
644 * smbc_free_context() to clean up all connections, some connections
645 * can remain active on the peer end, until some (long) timeout period
646 * later. This tree disconnect forces the peer to clean up, since the
647 * connection will be going away.
649 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
650 * the only user for this so far is smbmount which passes opened connection
651 * down to kernel's smbfs module.
653 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
654 cli_tdis(cli);
657 SAFE_FREE(cli->outbuf);
658 SAFE_FREE(cli->inbuf);
660 cli_free_signing_context(cli);
661 data_blob_free(&cli->secblob);
662 data_blob_free(&cli->user_session_key);
664 if (cli->fd != -1) {
665 close(cli->fd);
667 cli->fd = -1;
668 cli->smb_rw_error = SMB_READ_OK;
670 TALLOC_FREE(cli);
673 /****************************************************************************
674 Set socket options on a open connection.
675 ****************************************************************************/
677 void cli_sockopt(struct cli_state *cli, const char *options)
679 set_socket_options(cli->fd, options);
682 /****************************************************************************
683 Set the PID to use for smb messages. Return the old pid.
684 ****************************************************************************/
686 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
688 uint16 ret = cli->pid;
689 cli->pid = pid;
690 return ret;
693 /****************************************************************************
694 Set the case sensitivity flag on the packets. Returns old state.
695 ****************************************************************************/
697 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
699 bool ret = cli->case_sensitive;
700 cli->case_sensitive = case_sensitive;
701 return ret;
704 /****************************************************************************
705 Send a keepalive packet to the server
706 ****************************************************************************/
708 bool cli_send_keepalive(struct cli_state *cli)
710 if (cli->fd == -1) {
711 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
712 return false;
714 if (!send_keepalive(cli->fd)) {
715 close(cli->fd);
716 cli->fd = -1;
717 DEBUG(0,("Error sending keepalive packet to client.\n"));
718 return false;
720 return true;
723 /****************************************************************************
724 Send/receive a SMBecho command: ping the server
725 ****************************************************************************/
727 bool cli_echo(struct cli_state *cli, uint16 num_echos,
728 unsigned char *data, size_t length)
730 char *p;
731 int i;
733 SMB_ASSERT(length < 1024);
735 memset(cli->outbuf,'\0',smb_size);
736 cli_set_message(cli->outbuf,1,length,true);
737 SCVAL(cli->outbuf,smb_com,SMBecho);
738 SSVAL(cli->outbuf,smb_tid,65535);
739 SSVAL(cli->outbuf,smb_vwv0,num_echos);
740 cli_setup_packet(cli);
741 p = smb_buf(cli->outbuf);
742 memcpy(p, data, length);
743 p += length;
745 cli_setup_bcc(cli, p);
747 cli_send_smb(cli);
749 for (i=0; i<num_echos; i++) {
750 if (!cli_receive_smb(cli)) {
751 return false;
754 if (cli_is_error(cli)) {
755 return false;
759 return true;