Fix bogus uninitialized variable warnings
[Samba/gbeck.git] / source3 / libsmb / clientgen.c
blob042b3bdfb041cbe7b53b65e13e7d9ac87921db73
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 ssize_t len;
74 for(;;) {
75 len = receive_smb_raw(cli->fd, cli->inbuf, cli->timeout,
76 maxlen, &cli->smb_rw_error);
78 if (len < 0) {
79 DEBUG(10,("client_receive_smb failed\n"));
80 show_msg(cli->inbuf);
81 return len;
84 /* Ignore session keepalive packets. */
85 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
86 break;
90 if (cli_encryption_on(cli)) {
91 NTSTATUS status = cli_decrypt_message(cli);
92 if (!NT_STATUS_IS_OK(status)) {
93 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
94 nt_errstr(status)));
95 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
96 return -1;
100 show_msg(cli->inbuf);
101 return len;
104 /****************************************************************************
105 Recv an smb.
106 ****************************************************************************/
108 bool cli_receive_smb(struct cli_state *cli)
110 ssize_t len;
112 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
113 if (cli->fd == -1)
114 return false;
116 again:
117 len = client_receive_smb(cli, 0);
119 if (len > 0) {
120 /* it might be an oplock break request */
121 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
122 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
123 SVAL(cli->inbuf,smb_vwv6) == 0 &&
124 SVAL(cli->inbuf,smb_vwv7) == 0) {
125 if (cli->oplock_handler) {
126 int fnum = SVAL(cli->inbuf,smb_vwv2);
127 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
128 if (!cli->oplock_handler(cli, fnum, level)) {
129 return false;
132 /* try to prevent loops */
133 SCVAL(cli->inbuf,smb_com,0xFF);
134 goto again;
138 /* If the server is not responding, note that now */
139 if (len < 0) {
140 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
141 close(cli->fd);
142 cli->fd = -1;
143 return false;
146 if (!cli_check_sign_mac(cli)) {
148 * If we get a signature failure in sessionsetup, then
149 * the server sometimes just reflects the sent signature
150 * back to us. Detect this and allow the upper layer to
151 * retrieve the correct Windows error message.
153 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
154 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
155 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
156 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
157 cli_is_error(cli)) {
160 * Reflected signature on login error.
161 * Set bad sig but don't close fd.
163 cli->smb_rw_error = SMB_READ_BAD_SIG;
164 return true;
167 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
168 cli->smb_rw_error = SMB_READ_BAD_SIG;
169 close(cli->fd);
170 cli->fd = -1;
171 return false;
173 return true;
176 /****************************************************************************
177 Read the data portion of a readX smb.
178 The timeout is in milliseconds
179 ****************************************************************************/
181 ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
183 return read_socket_with_timeout(cli->fd, buffer, len, len,
184 cli->timeout, &cli->smb_rw_error);
187 /****************************************************************************
188 Read a smb readX header.
189 We can only use this if encryption and signing are off.
190 ****************************************************************************/
192 bool cli_receive_smb_readX_header(struct cli_state *cli)
194 ssize_t len, offset;
196 if (cli->fd == -1)
197 return false;
199 again:
201 /* Read up to the size of a readX header reply. */
202 len = client_receive_smb(cli, (smb_size - 4) + 24);
204 if (len > 0) {
205 /* it might be an oplock break request */
206 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
207 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
208 SVAL(cli->inbuf,smb_vwv6) == 0 &&
209 SVAL(cli->inbuf,smb_vwv7) == 0) {
210 ssize_t total_len = smb_len(cli->inbuf);
212 if (total_len > CLI_SAMBA_MAX_LARGE_READX_SIZE+SAFETY_MARGIN) {
213 goto read_err;
216 /* Read the rest of the data. */
217 if ((total_len - len > 0) &&
218 !cli_receive_smb_data(cli,cli->inbuf+len,total_len - len)) {
219 goto read_err;
222 if (cli->oplock_handler) {
223 int fnum = SVAL(cli->inbuf,smb_vwv2);
224 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
225 if (!cli->oplock_handler(cli, fnum, level)) return false;
227 /* try to prevent loops */
228 SCVAL(cli->inbuf,smb_com,0xFF);
229 goto again;
233 /* If it's not the above size it probably was an error packet. */
235 if ((len == (smb_size - 4) + 24) && !cli_is_error(cli)) {
236 /* Check it's a non-chained readX reply. */
237 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) ||
238 (CVAL(cli->inbuf,smb_vwv0) != 0xFF) ||
239 (CVAL(cli->inbuf,smb_com) != SMBreadX)) {
241 * We're not coping here with asnyc replies to
242 * other calls. Punt here - we need async client
243 * libs for this.
245 goto read_err;
249 * We know it's a readX reply - ensure we've read the
250 * padding bytes also.
253 offset = SVAL(cli->inbuf,smb_vwv6);
254 if (offset > len) {
255 ssize_t ret;
256 size_t padbytes = offset - len;
257 ret = cli_receive_smb_data(cli,smb_buf(cli->inbuf),padbytes);
258 if (ret != padbytes) {
259 goto read_err;
264 return true;
266 read_err:
268 cli->smb_rw_error = SMB_READ_ERROR;
269 close(cli->fd);
270 cli->fd = -1;
271 return false;
274 static ssize_t write_socket(int fd, const char *buf, size_t len)
276 ssize_t ret=0;
278 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
279 ret = write_data(fd,buf,len);
281 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
282 if(ret <= 0)
283 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
284 (int)len, fd, strerror(errno) ));
286 return(ret);
289 /****************************************************************************
290 Send an smb to a fd.
291 ****************************************************************************/
293 bool cli_send_smb(struct cli_state *cli)
295 size_t len;
296 size_t nwritten=0;
297 ssize_t ret;
298 char *buf_out = cli->outbuf;
299 bool enc_on = cli_encryption_on(cli);
301 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
302 if (cli->fd == -1)
303 return false;
305 cli_calculate_sign_mac(cli);
307 if (enc_on) {
308 NTSTATUS status = cli_encrypt_message(cli, &buf_out);
309 if (!NT_STATUS_IS_OK(status)) {
310 close(cli->fd);
311 cli->fd = -1;
312 cli->smb_rw_error = SMB_WRITE_ERROR;
313 DEBUG(0,("Error in encrypting client message. Error %s\n",
314 nt_errstr(status) ));
315 return false;
319 len = smb_len(buf_out) + 4;
321 while (nwritten < len) {
322 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
323 if (ret <= 0) {
324 if (enc_on) {
325 cli_free_enc_buffer(cli, buf_out);
327 close(cli->fd);
328 cli->fd = -1;
329 cli->smb_rw_error = SMB_WRITE_ERROR;
330 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
331 (int)len,(int)ret, strerror(errno) ));
332 return false;
334 nwritten += ret;
337 if (enc_on) {
338 cli_free_enc_buffer(cli, buf_out);
341 /* Increment the mid so we can tell between responses. */
342 cli->mid++;
343 if (!cli->mid)
344 cli->mid++;
345 return true;
348 /****************************************************************************
349 Send a "direct" writeX smb to a fd.
350 ****************************************************************************/
352 bool cli_send_smb_direct_writeX(struct cli_state *cli,
353 const char *p,
354 size_t extradata)
356 /* First length to send is the offset to the data. */
357 size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
358 size_t nwritten=0;
359 ssize_t ret;
361 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
362 if (cli->fd == -1) {
363 return false;
366 if (client_is_signing_on(cli)) {
367 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
368 return false;
371 while (nwritten < len) {
372 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
373 if (ret <= 0) {
374 close(cli->fd);
375 cli->fd = -1;
376 cli->smb_rw_error = SMB_WRITE_ERROR;
377 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
378 (int)len,(int)ret, strerror(errno) ));
379 return false;
381 nwritten += ret;
384 /* Now write the extra data. */
385 nwritten=0;
386 while (nwritten < extradata) {
387 ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
388 if (ret <= 0) {
389 close(cli->fd);
390 cli->fd = -1;
391 cli->smb_rw_error = SMB_WRITE_ERROR;
392 DEBUG(0,("Error writing %d extradata "
393 "bytes to client. %d (%s)\n",
394 (int)extradata,(int)ret, strerror(errno) ));
395 return false;
397 nwritten += ret;
400 /* Increment the mid so we can tell between responses. */
401 cli->mid++;
402 if (!cli->mid)
403 cli->mid++;
404 return true;
407 /****************************************************************************
408 Setup basics in a outgoing packet.
409 ****************************************************************************/
411 void cli_setup_packet(struct cli_state *cli)
413 cli->rap_error = 0;
414 SSVAL(cli->outbuf,smb_pid,cli->pid);
415 SSVAL(cli->outbuf,smb_uid,cli->vuid);
416 SSVAL(cli->outbuf,smb_mid,cli->mid);
417 if (cli->protocol > PROTOCOL_CORE) {
418 uint16 flags2;
419 if (cli->case_sensitive) {
420 SCVAL(cli->outbuf,smb_flg,0x0);
421 } else {
422 /* Default setting, case insensitive. */
423 SCVAL(cli->outbuf,smb_flg,0x8);
425 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
426 if (cli->capabilities & CAP_UNICODE)
427 flags2 |= FLAGS2_UNICODE_STRINGS;
428 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
429 flags2 |= FLAGS2_DFS_PATHNAMES;
430 if (cli->capabilities & CAP_STATUS32)
431 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
432 if (cli->use_spnego)
433 flags2 |= FLAGS2_EXTENDED_SECURITY;
434 SSVAL(cli->outbuf,smb_flg2, flags2);
438 /****************************************************************************
439 Setup the bcc length of the packet from a pointer to the end of the data.
440 ****************************************************************************/
442 void cli_setup_bcc(struct cli_state *cli, void *p)
444 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
447 /****************************************************************************
448 Initialise credentials of a client structure.
449 ****************************************************************************/
451 void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
453 fstrcpy(cli->domain, domain);
454 fstrcpy(cli->user_name, username);
455 pwd_set_cleartext(&cli->pwd, password);
456 if (!*username) {
457 cli->pwd.null_pwd = true;
460 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
463 /****************************************************************************
464 Set the signing state (used from the command line).
465 ****************************************************************************/
467 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
469 if (signing_state == Undefined)
470 return;
472 if (signing_state == false) {
473 cli->sign_info.allow_smb_signing = false;
474 cli->sign_info.mandatory_signing = false;
475 return;
478 cli->sign_info.allow_smb_signing = true;
480 if (signing_state == Required)
481 cli->sign_info.mandatory_signing = true;
484 /****************************************************************************
485 Initialise a client structure. Always returns a malloc'ed struct.
486 ****************************************************************************/
488 struct cli_state *cli_initialise(void)
490 struct cli_state *cli = NULL;
492 /* Check the effective uid - make sure we are not setuid */
493 if (is_setuid_root()) {
494 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
495 return NULL;
498 cli = SMB_MALLOC_P(struct cli_state);
499 if (!cli) {
500 return NULL;
503 ZERO_STRUCTP(cli);
505 cli->port = 0;
506 cli->fd = -1;
507 cli->cnum = -1;
508 cli->pid = (uint16)sys_getpid();
509 cli->mid = 1;
510 cli->vuid = UID_FIELD_INVALID;
511 cli->protocol = PROTOCOL_NT1;
512 cli->timeout = 20000; /* Timeout is in milliseconds. */
513 cli->bufsize = CLI_BUFFER_SIZE+4;
514 cli->max_xmit = cli->bufsize;
515 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
516 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
517 cli->oplock_handler = cli_oplock_ack;
518 cli->case_sensitive = false;
519 cli->smb_rw_error = SMB_READ_OK;
521 cli->use_spnego = lp_client_use_spnego();
523 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
525 /* Set the CLI_FORCE_DOSERR environment variable to test
526 client routines using DOS errors instead of STATUS32
527 ones. This intended only as a temporary hack. */
528 if (getenv("CLI_FORCE_DOSERR"))
529 cli->force_dos_errors = true;
531 if (lp_client_signing())
532 cli->sign_info.allow_smb_signing = true;
534 if (lp_client_signing() == Required)
535 cli->sign_info.mandatory_signing = true;
537 if (!cli->outbuf || !cli->inbuf)
538 goto error;
540 memset(cli->outbuf, 0, cli->bufsize);
541 memset(cli->inbuf, 0, cli->bufsize);
544 #if defined(DEVELOPER)
545 /* just because we over-allocate, doesn't mean it's right to use it */
546 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
547 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
548 #endif
550 /* initialise signing */
551 cli_null_set_signing(cli);
553 cli->initialised = 1;
555 return cli;
557 /* Clean up after malloc() error */
559 error:
561 SAFE_FREE(cli->inbuf);
562 SAFE_FREE(cli->outbuf);
563 SAFE_FREE(cli);
564 return NULL;
567 /****************************************************************************
568 External interface.
569 Close an open named pipe over SMB. Free any authentication data.
570 Returns false if the cli_close call failed.
571 ****************************************************************************/
573 bool cli_rpc_pipe_close(struct rpc_pipe_client *cli)
575 bool ret;
577 if (!cli) {
578 return false;
581 ret = cli_close(cli->cli, cli->fnum);
583 if (!ret) {
584 DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
585 "fnum 0x%x "
586 "to machine %s. Error was %s\n",
587 cli->pipe_name,
588 (int) cli->fnum,
589 cli->cli->desthost,
590 cli_errstr(cli->cli)));
593 if (cli->auth.cli_auth_data_free_func) {
594 (*cli->auth.cli_auth_data_free_func)(&cli->auth);
597 DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
598 cli->pipe_name, cli->cli->desthost ));
600 DLIST_REMOVE(cli->cli->pipe_list, cli);
601 talloc_destroy(cli->mem_ctx);
602 return ret;
605 /****************************************************************************
606 Close all pipes open on this session.
607 ****************************************************************************/
609 void cli_nt_pipes_close(struct cli_state *cli)
611 struct rpc_pipe_client *cp, *next;
613 for (cp = cli->pipe_list; cp; cp = next) {
614 next = cp->next;
615 cli_rpc_pipe_close(cp);
619 /****************************************************************************
620 Shutdown a client structure.
621 ****************************************************************************/
623 void cli_shutdown(struct cli_state *cli)
625 cli_nt_pipes_close(cli);
628 * tell our peer to free his resources. Wihtout this, when an
629 * application attempts to do a graceful shutdown and calls
630 * smbc_free_context() to clean up all connections, some connections
631 * can remain active on the peer end, until some (long) timeout period
632 * later. This tree disconnect forces the peer to clean up, since the
633 * connection will be going away.
635 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
636 * the only user for this so far is smbmount which passes opened connection
637 * down to kernel's smbfs module.
639 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
640 cli_tdis(cli);
643 SAFE_FREE(cli->outbuf);
644 SAFE_FREE(cli->inbuf);
646 cli_free_signing_context(cli);
647 data_blob_free(&cli->secblob);
648 data_blob_free(&cli->user_session_key);
650 if (cli->fd != -1) {
651 close(cli->fd);
653 cli->fd = -1;
654 cli->smb_rw_error = SMB_READ_OK;
656 SAFE_FREE(cli);
659 /****************************************************************************
660 Set socket options on a open connection.
661 ****************************************************************************/
663 void cli_sockopt(struct cli_state *cli, const char *options)
665 set_socket_options(cli->fd, options);
668 /****************************************************************************
669 Set the PID to use for smb messages. Return the old pid.
670 ****************************************************************************/
672 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
674 uint16 ret = cli->pid;
675 cli->pid = pid;
676 return ret;
679 /****************************************************************************
680 Set the case sensitivity flag on the packets. Returns old state.
681 ****************************************************************************/
683 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
685 bool ret = cli->case_sensitive;
686 cli->case_sensitive = case_sensitive;
687 return ret;
690 /****************************************************************************
691 Send a keepalive packet to the server
692 ****************************************************************************/
694 bool cli_send_keepalive(struct cli_state *cli)
696 if (cli->fd == -1) {
697 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
698 return false;
700 if (!send_keepalive(cli->fd)) {
701 close(cli->fd);
702 cli->fd = -1;
703 DEBUG(0,("Error sending keepalive packet to client.\n"));
704 return false;
706 return true;
709 /****************************************************************************
710 Send/receive a SMBecho command: ping the server
711 ****************************************************************************/
713 bool cli_echo(struct cli_state *cli, uint16 num_echos,
714 unsigned char *data, size_t length)
716 char *p;
717 int i;
719 SMB_ASSERT(length < 1024);
721 memset(cli->outbuf,'\0',smb_size);
722 cli_set_message(cli->outbuf,1,length,true);
723 SCVAL(cli->outbuf,smb_com,SMBecho);
724 SSVAL(cli->outbuf,smb_tid,65535);
725 SSVAL(cli->outbuf,smb_vwv0,num_echos);
726 cli_setup_packet(cli);
727 p = smb_buf(cli->outbuf);
728 memcpy(p, data, length);
729 p += length;
731 cli_setup_bcc(cli, p);
733 cli_send_smb(cli);
735 for (i=0; i<num_echos; i++) {
736 if (!cli_receive_smb(cli)) {
737 return false;
740 if (cli_is_error(cli)) {
741 return false;
745 return true;