Add STREAMINFO op to vfs_full_audit
[Samba.git] / source / libsmb / clientgen.c
blob2af4383dcdc1f24941d2824fc76acd6a3bb903f3
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.
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)
71 size_t len;
73 for(;;) {
74 NTSTATUS status;
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"));
82 show_msg(cli->inbuf);
84 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
85 set_smb_read_error(&cli->smb_rw_error,
86 SMB_READ_EOF);
87 return -1;
90 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
91 set_smb_read_error(&cli->smb_rw_error,
92 SMB_READ_TIMEOUT);
93 return -1;
96 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
97 return -1;
100 if (len < 0) {
101 return len;
104 /* Ignore session keepalive packets. */
105 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
106 break;
110 if (cli_encryption_on(cli)) {
111 NTSTATUS status = cli_decrypt_message(cli);
112 if (!NT_STATUS_IS_OK(status)) {
113 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
114 nt_errstr(status)));
115 cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
116 return -1;
120 show_msg(cli->inbuf);
121 return len;
124 /****************************************************************************
125 Recv an smb.
126 ****************************************************************************/
128 bool cli_receive_smb(struct cli_state *cli)
130 ssize_t len;
132 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
133 if (cli->fd == -1)
134 return false;
136 again:
137 len = client_receive_smb(cli, 0);
139 if (len > 0) {
140 /* it might be an oplock break request */
141 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
142 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
143 SVAL(cli->inbuf,smb_vwv6) == 0 &&
144 SVAL(cli->inbuf,smb_vwv7) == 0) {
145 if (cli->oplock_handler) {
146 int fnum = SVAL(cli->inbuf,smb_vwv2);
147 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
148 if (!cli->oplock_handler(cli, fnum, level)) {
149 return false;
152 /* try to prevent loops */
153 SCVAL(cli->inbuf,smb_com,0xFF);
154 goto again;
158 /* If the server is not responding, note that now */
159 if (len < 0) {
160 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
161 close(cli->fd);
162 cli->fd = -1;
163 return false;
166 if (!cli_check_sign_mac(cli, cli->inbuf)) {
168 * If we get a signature failure in sessionsetup, then
169 * the server sometimes just reflects the sent signature
170 * back to us. Detect this and allow the upper layer to
171 * retrieve the correct Windows error message.
173 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
174 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
175 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
176 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
177 cli_is_error(cli)) {
180 * Reflected signature on login error.
181 * Set bad sig but don't close fd.
183 cli->smb_rw_error = SMB_READ_BAD_SIG;
184 return true;
187 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
188 cli->smb_rw_error = SMB_READ_BAD_SIG;
189 close(cli->fd);
190 cli->fd = -1;
191 return false;
193 return true;
196 /****************************************************************************
197 Read the data portion of a readX smb.
198 The timeout is in milliseconds
199 ****************************************************************************/
201 ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
203 NTSTATUS status;
205 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
207 status = read_socket_with_timeout(
208 cli->fd, buffer, len, len, cli->timeout, NULL);
209 if (NT_STATUS_IS_OK(status)) {
210 return len;
213 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
214 set_smb_read_error(&cli->smb_rw_error, SMB_READ_EOF);
215 return -1;
218 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
219 set_smb_read_error(&cli->smb_rw_error, SMB_READ_TIMEOUT);
220 return -1;
223 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
224 return -1;
227 static ssize_t write_socket(int fd, const char *buf, size_t len)
229 ssize_t ret=0;
231 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
232 ret = write_data(fd,buf,len);
234 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
235 if(ret <= 0)
236 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
237 (int)len, fd, strerror(errno) ));
239 return(ret);
242 /****************************************************************************
243 Send an smb to a fd.
244 ****************************************************************************/
246 bool cli_send_smb(struct cli_state *cli)
248 size_t len;
249 size_t nwritten=0;
250 ssize_t ret;
251 char *buf_out = cli->outbuf;
252 bool enc_on = cli_encryption_on(cli);
254 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
255 if (cli->fd == -1)
256 return false;
258 cli_calculate_sign_mac(cli, cli->outbuf);
260 if (enc_on) {
261 NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
262 &buf_out);
263 if (!NT_STATUS_IS_OK(status)) {
264 close(cli->fd);
265 cli->fd = -1;
266 cli->smb_rw_error = SMB_WRITE_ERROR;
267 DEBUG(0,("Error in encrypting client message. Error %s\n",
268 nt_errstr(status) ));
269 return false;
273 len = smb_len(buf_out) + 4;
275 while (nwritten < len) {
276 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
277 if (ret <= 0) {
278 if (enc_on) {
279 cli_free_enc_buffer(cli, buf_out);
281 close(cli->fd);
282 cli->fd = -1;
283 cli->smb_rw_error = SMB_WRITE_ERROR;
284 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
285 (int)len,(int)ret, strerror(errno) ));
286 return false;
288 nwritten += ret;
291 if (enc_on) {
292 cli_free_enc_buffer(cli, buf_out);
295 /* Increment the mid so we can tell between responses. */
296 cli->mid++;
297 if (!cli->mid)
298 cli->mid++;
299 return true;
302 /****************************************************************************
303 Send a "direct" writeX smb to a fd.
304 ****************************************************************************/
306 bool cli_send_smb_direct_writeX(struct cli_state *cli,
307 const char *p,
308 size_t extradata)
310 /* First length to send is the offset to the data. */
311 size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
312 size_t nwritten=0;
313 ssize_t ret;
315 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
316 if (cli->fd == -1) {
317 return false;
320 if (client_is_signing_on(cli)) {
321 DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
322 return false;
325 while (nwritten < len) {
326 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
327 if (ret <= 0) {
328 close(cli->fd);
329 cli->fd = -1;
330 cli->smb_rw_error = SMB_WRITE_ERROR;
331 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
332 (int)len,(int)ret, strerror(errno) ));
333 return false;
335 nwritten += ret;
338 /* Now write the extra data. */
339 nwritten=0;
340 while (nwritten < extradata) {
341 ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
342 if (ret <= 0) {
343 close(cli->fd);
344 cli->fd = -1;
345 cli->smb_rw_error = SMB_WRITE_ERROR;
346 DEBUG(0,("Error writing %d extradata "
347 "bytes to client. %d (%s)\n",
348 (int)extradata,(int)ret, strerror(errno) ));
349 return false;
351 nwritten += ret;
354 /* Increment the mid so we can tell between responses. */
355 cli->mid++;
356 if (!cli->mid)
357 cli->mid++;
358 return true;
361 /****************************************************************************
362 Setup basics in a outgoing packet.
363 ****************************************************************************/
365 void cli_setup_packet_buf(struct cli_state *cli, char *buf)
367 uint16 flags2;
368 cli->rap_error = 0;
369 SIVAL(buf,smb_rcls,0);
370 SSVAL(buf,smb_pid,cli->pid);
371 memset(buf+smb_pidhigh, 0, 12);
372 SSVAL(buf,smb_uid,cli->vuid);
373 SSVAL(buf,smb_mid,cli->mid);
375 if (cli->protocol <= PROTOCOL_CORE) {
376 return;
379 if (cli->case_sensitive) {
380 SCVAL(buf,smb_flg,0x0);
381 } else {
382 /* Default setting, case insensitive. */
383 SCVAL(buf,smb_flg,0x8);
385 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
386 if (cli->capabilities & CAP_UNICODE)
387 flags2 |= FLAGS2_UNICODE_STRINGS;
388 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
389 flags2 |= FLAGS2_DFS_PATHNAMES;
390 if (cli->capabilities & CAP_STATUS32)
391 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
392 if (cli->use_spnego)
393 flags2 |= FLAGS2_EXTENDED_SECURITY;
394 SSVAL(buf,smb_flg2, flags2);
397 void cli_setup_packet(struct cli_state *cli)
399 cli_setup_packet_buf(cli, cli->outbuf);
402 /****************************************************************************
403 Setup the bcc length of the packet from a pointer to the end of the data.
404 ****************************************************************************/
406 void cli_setup_bcc(struct cli_state *cli, void *p)
408 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
411 /****************************************************************************
412 Initialise credentials of a client structure.
413 ****************************************************************************/
415 void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
417 fstrcpy(cli->domain, domain);
418 fstrcpy(cli->user_name, username);
419 pwd_set_cleartext(&cli->pwd, password);
420 if (!*username) {
421 cli->pwd.null_pwd = true;
424 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
427 /****************************************************************************
428 Set the signing state (used from the command line).
429 ****************************************************************************/
431 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
433 if (signing_state == Undefined)
434 return;
436 if (signing_state == false) {
437 cli->sign_info.allow_smb_signing = false;
438 cli->sign_info.mandatory_signing = false;
439 return;
442 cli->sign_info.allow_smb_signing = true;
444 if (signing_state == Required)
445 cli->sign_info.mandatory_signing = true;
448 /****************************************************************************
449 Initialise a client structure. Always returns a malloc'ed struct.
450 ****************************************************************************/
452 struct cli_state *cli_initialise(void)
454 struct cli_state *cli = NULL;
456 /* Check the effective uid - make sure we are not setuid */
457 if (is_setuid_root()) {
458 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
459 return NULL;
462 cli = talloc(NULL, struct cli_state);
463 if (!cli) {
464 return NULL;
467 ZERO_STRUCTP(cli);
469 cli->port = 0;
470 cli->fd = -1;
471 cli->cnum = -1;
472 cli->pid = (uint16)sys_getpid();
473 cli->mid = 1;
474 cli->vuid = UID_FIELD_INVALID;
475 cli->protocol = PROTOCOL_NT1;
476 cli->timeout = 20000; /* Timeout is in milliseconds. */
477 cli->bufsize = CLI_BUFFER_SIZE+4;
478 cli->max_xmit = cli->bufsize;
479 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
480 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
481 cli->oplock_handler = cli_oplock_ack;
482 cli->case_sensitive = false;
483 cli->smb_rw_error = SMB_READ_OK;
485 cli->use_spnego = lp_client_use_spnego();
487 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
489 /* Set the CLI_FORCE_DOSERR environment variable to test
490 client routines using DOS errors instead of STATUS32
491 ones. This intended only as a temporary hack. */
492 if (getenv("CLI_FORCE_DOSERR"))
493 cli->force_dos_errors = true;
495 if (lp_client_signing())
496 cli->sign_info.allow_smb_signing = true;
498 if (lp_client_signing() == Required)
499 cli->sign_info.mandatory_signing = true;
501 if (!cli->outbuf || !cli->inbuf)
502 goto error;
504 memset(cli->outbuf, 0, cli->bufsize);
505 memset(cli->inbuf, 0, cli->bufsize);
508 #if defined(DEVELOPER)
509 /* just because we over-allocate, doesn't mean it's right to use it */
510 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
511 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
512 #endif
514 /* initialise signing */
515 cli_null_set_signing(cli);
517 cli->initialised = 1;
519 return cli;
521 /* Clean up after malloc() error */
523 error:
525 SAFE_FREE(cli->inbuf);
526 SAFE_FREE(cli->outbuf);
527 SAFE_FREE(cli);
528 return NULL;
531 /****************************************************************************
532 External interface.
533 Close an open named pipe over SMB. Free any authentication data.
534 Returns false if the cli_close call failed.
535 ****************************************************************************/
537 bool cli_rpc_pipe_close(struct rpc_pipe_client *cli)
539 bool ret;
541 if (!cli) {
542 return false;
545 ret = cli_close(cli->cli, cli->fnum);
547 if (!ret) {
548 DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
549 "fnum 0x%x "
550 "to machine %s. Error was %s\n",
551 cli->pipe_name,
552 (int) cli->fnum,
553 cli->cli->desthost,
554 cli_errstr(cli->cli)));
557 if (cli->auth.cli_auth_data_free_func) {
558 (*cli->auth.cli_auth_data_free_func)(&cli->auth);
561 DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
562 cli->pipe_name, cli->cli->desthost ));
564 DLIST_REMOVE(cli->cli->pipe_list, cli);
565 talloc_destroy(cli->mem_ctx);
566 return ret;
569 /****************************************************************************
570 Close all pipes open on this session.
571 ****************************************************************************/
573 void cli_nt_pipes_close(struct cli_state *cli)
575 struct rpc_pipe_client *cp, *next;
577 for (cp = cli->pipe_list; cp; cp = next) {
578 next = cp->next;
579 cli_rpc_pipe_close(cp);
583 /****************************************************************************
584 Shutdown a client structure.
585 ****************************************************************************/
587 void cli_shutdown(struct cli_state *cli)
589 cli_nt_pipes_close(cli);
592 * tell our peer to free his resources. Wihtout this, when an
593 * application attempts to do a graceful shutdown and calls
594 * smbc_free_context() to clean up all connections, some connections
595 * can remain active on the peer end, until some (long) timeout period
596 * later. This tree disconnect forces the peer to clean up, since the
597 * connection will be going away.
599 * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
600 * the only user for this so far is smbmount which passes opened connection
601 * down to kernel's smbfs module.
603 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
604 cli_tdis(cli);
607 SAFE_FREE(cli->outbuf);
608 SAFE_FREE(cli->inbuf);
610 cli_free_signing_context(cli);
611 data_blob_free(&cli->secblob);
612 data_blob_free(&cli->user_session_key);
614 if (cli->fd != -1) {
615 close(cli->fd);
617 cli->fd = -1;
618 cli->smb_rw_error = SMB_READ_OK;
620 TALLOC_FREE(cli);
623 /****************************************************************************
624 Set socket options on a open connection.
625 ****************************************************************************/
627 void cli_sockopt(struct cli_state *cli, const char *options)
629 set_socket_options(cli->fd, options);
632 /****************************************************************************
633 Set the PID to use for smb messages. Return the old pid.
634 ****************************************************************************/
636 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
638 uint16 ret = cli->pid;
639 cli->pid = pid;
640 return ret;
643 /****************************************************************************
644 Set the case sensitivity flag on the packets. Returns old state.
645 ****************************************************************************/
647 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
649 bool ret = cli->case_sensitive;
650 cli->case_sensitive = case_sensitive;
651 return ret;
654 /****************************************************************************
655 Send a keepalive packet to the server
656 ****************************************************************************/
658 bool cli_send_keepalive(struct cli_state *cli)
660 if (cli->fd == -1) {
661 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
662 return false;
664 if (!send_keepalive(cli->fd)) {
665 close(cli->fd);
666 cli->fd = -1;
667 DEBUG(0,("Error sending keepalive packet to client.\n"));
668 return false;
670 return true;
673 /****************************************************************************
674 Send/receive a SMBecho command: ping the server
675 ****************************************************************************/
677 bool cli_echo(struct cli_state *cli, uint16 num_echos,
678 unsigned char *data, size_t length)
680 char *p;
681 int i;
683 SMB_ASSERT(length < 1024);
685 memset(cli->outbuf,'\0',smb_size);
686 cli_set_message(cli->outbuf,1,length,true);
687 SCVAL(cli->outbuf,smb_com,SMBecho);
688 SSVAL(cli->outbuf,smb_tid,65535);
689 SSVAL(cli->outbuf,smb_vwv0,num_echos);
690 cli_setup_packet(cli);
691 p = smb_buf(cli->outbuf);
692 memcpy(p, data, length);
693 p += length;
695 cli_setup_bcc(cli, p);
697 cli_send_smb(cli);
699 for (i=0; i<num_echos; i++) {
700 if (!cli_receive_smb(cli)) {
701 return false;
704 if (cli_is_error(cli)) {
705 return false;
709 return true;