2 Unix SMB/CIFS implementation.
3 SMB client generic functions
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 extern int smb_read_error;
25 /****************************************************************************
26 Change the timeout (in milliseconds).
27 ****************************************************************************/
29 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
31 unsigned int old_timeout = cli->timeout;
32 cli->timeout = timeout;
36 /****************************************************************************
37 Change the port number used to call on.
38 ****************************************************************************/
40 int cli_set_port(struct cli_state *cli, int port)
46 /****************************************************************************
47 Read an smb from a fd ignoring all keepalive packets. Note that the buffer
48 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
49 The timeout is in milliseconds
51 This is exactly the same as receive_smb except that it can be set to never return
52 a session keepalive packet (just as receive_smb used to do).
53 receive_smb was changed to return keepalives as the oplock processing means this call
54 should never go into a blocking read.
55 ****************************************************************************/
57 static BOOL client_receive_smb(struct cli_state *cli, BOOL eat_keepalives)
61 char *buffer = cli->inbuf;
62 unsigned int timeout = cli->timeout;
65 ret = receive_smb_raw(fd, buffer, timeout);
68 DEBUG(10,("client_receive_smb failed\n"));
73 /* Ignore session keepalive packets. */
74 if (eat_keepalives && (CVAL(buffer,0) == SMBkeepalive)) {
80 if (cli_encryption_on(cli)) {
81 NTSTATUS status = cli_decrypt_message(cli);
82 if (!NT_STATUS_IS_OK(status)) {
83 DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
85 cli->smb_rw_error = READ_BAD_DECRYPT;
95 /****************************************************************************
97 ****************************************************************************/
99 BOOL cli_receive_smb_internal(struct cli_state *cli, BOOL eat_keepalives)
103 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
108 ret = client_receive_smb(cli, eat_keepalives);
110 if (ret && !eat_keepalives && (CVAL(cli->inbuf,0) == SMBkeepalive)) {
111 /* Give back the keepalive. */
116 /* it might be an oplock break request */
117 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
118 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
119 SVAL(cli->inbuf,smb_vwv6) == 0 &&
120 SVAL(cli->inbuf,smb_vwv7) == 0) {
121 if (cli->oplock_handler) {
122 int fnum = SVAL(cli->inbuf,smb_vwv2);
123 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
124 if (!cli->oplock_handler(cli, fnum, level)) return False;
126 /* try to prevent loops */
127 SCVAL(cli->inbuf,smb_com,0xFF);
132 /* If the server is not responding, note that now */
134 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
135 cli->smb_rw_error = smb_read_error;
141 if (!cli_check_sign_mac(cli)) {
143 * If we get a signature failure in sessionsetup, then
144 * the server sometimes just reflects the sent signature
145 * back to us. Detect this and allow the upper layer to
146 * retrieve the correct Windows error message.
148 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
149 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
150 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
151 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
155 * Reflected signature on login error.
156 * Set bad sig but don't close fd.
158 cli->smb_rw_error = READ_BAD_SIG;
162 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
163 cli->smb_rw_error = READ_BAD_SIG;
172 /****************************************************************************
173 Recv an smb - eat keepalives.
174 ****************************************************************************/
176 BOOL cli_receive_smb(struct cli_state *cli)
178 return cli_receive_smb_internal(cli, True);
181 /****************************************************************************
182 Recv an smb - return keepalives.
183 ****************************************************************************/
185 BOOL cli_receive_smb_return_keepalive(struct cli_state *cli)
187 return cli_receive_smb_internal(cli, False);
190 static ssize_t write_socket(int fd, const char *buf, size_t len)
194 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
195 ret = write_data(fd,buf,len);
197 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
199 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
200 (int)len, fd, strerror(errno) ));
205 /****************************************************************************
207 ****************************************************************************/
209 BOOL cli_send_smb(struct cli_state *cli)
214 char *buf_out = cli->outbuf;
215 BOOL enc_on = cli_encryption_on(cli);
217 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
222 cli_calculate_sign_mac(cli);
225 NTSTATUS status = cli_encrypt_message(cli, &buf_out);
226 if (!NT_STATUS_IS_OK(status)) {
229 cli->smb_rw_error = WRITE_ERROR;
230 DEBUG(0,("Error in encrypting client message. Error %s\n",
231 nt_errstr(status) ));
236 len = smb_len(buf_out) + 4;
238 while (nwritten < len) {
239 ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
242 cli_free_enc_buffer(cli, buf_out);
246 cli->smb_rw_error = WRITE_ERROR;
247 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
248 (int)len,(int)ret, strerror(errno) ));
254 cli_free_enc_buffer(cli, buf_out);
256 /* Increment the mid so we can tell between responses. */
264 /****************************************************************************
265 Setup basics in a outgoing packet.
266 ****************************************************************************/
268 void cli_setup_packet(struct cli_state *cli)
271 SSVAL(cli->outbuf,smb_pid,cli->pid);
272 SSVAL(cli->outbuf,smb_uid,cli->vuid);
273 SSVAL(cli->outbuf,smb_mid,cli->mid);
274 if (cli->protocol > PROTOCOL_CORE) {
276 if (cli->case_sensitive) {
277 SCVAL(cli->outbuf,smb_flg,0x0);
279 /* Default setting, case insensitive. */
280 SCVAL(cli->outbuf,smb_flg,0x8);
282 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
283 if (cli->capabilities & CAP_UNICODE)
284 flags2 |= FLAGS2_UNICODE_STRINGS;
285 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
286 flags2 |= FLAGS2_DFS_PATHNAMES;
287 if (cli->capabilities & CAP_STATUS32)
288 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
290 flags2 |= FLAGS2_EXTENDED_SECURITY;
291 SSVAL(cli->outbuf,smb_flg2, flags2);
295 /****************************************************************************
296 Setup the bcc length of the packet from a pointer to the end of the data.
297 ****************************************************************************/
299 void cli_setup_bcc(struct cli_state *cli, void *p)
301 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
304 /****************************************************************************
305 Initialise credentials of a client structure.
306 ****************************************************************************/
308 void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
310 fstrcpy(cli->domain, domain);
311 fstrcpy(cli->user_name, username);
312 pwd_set_cleartext(&cli->pwd, password);
314 cli->pwd.null_pwd = True;
317 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
320 /****************************************************************************
321 Set the signing state (used from the command line).
322 ****************************************************************************/
324 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
326 if (signing_state == Undefined)
329 if (signing_state == False) {
330 cli->sign_info.allow_smb_signing = False;
331 cli->sign_info.mandatory_signing = False;
335 cli->sign_info.allow_smb_signing = True;
337 if (signing_state == Required)
338 cli->sign_info.mandatory_signing = True;
341 /****************************************************************************
342 Initialise a client structure. Always returns a malloc'ed struct.
343 ****************************************************************************/
345 struct cli_state *cli_initialise(void)
347 struct cli_state *cli = NULL;
349 /* Check the effective uid - make sure we are not setuid */
350 if (is_setuid_root()) {
351 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
355 cli = SMB_MALLOC_P(struct cli_state);
365 cli->pid = (uint16)sys_getpid();
367 cli->vuid = UID_FIELD_INVALID;
368 cli->protocol = PROTOCOL_NT1;
369 cli->timeout = 20000; /* Timeout is in milliseconds. */
370 cli->bufsize = CLI_BUFFER_SIZE+4;
371 cli->max_xmit = cli->bufsize;
372 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
373 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
374 cli->oplock_handler = cli_oplock_ack;
375 cli->case_sensitive = False;
376 cli->smb_rw_error = 0;
378 cli->use_spnego = lp_client_use_spnego();
380 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
382 /* Set the CLI_FORCE_DOSERR environment variable to test
383 client routines using DOS errors instead of STATUS32
384 ones. This intended only as a temporary hack. */
385 if (getenv("CLI_FORCE_DOSERR"))
386 cli->force_dos_errors = True;
388 if (lp_client_signing())
389 cli->sign_info.allow_smb_signing = True;
391 if (lp_client_signing() == Required)
392 cli->sign_info.mandatory_signing = True;
394 if (!cli->outbuf || !cli->inbuf)
397 if ((cli->mem_ctx = talloc_init("cli based talloc")) == NULL)
400 memset(cli->outbuf, 0, cli->bufsize);
401 memset(cli->inbuf, 0, cli->bufsize);
404 #if defined(DEVELOPER)
405 /* just because we over-allocate, doesn't mean it's right to use it */
406 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
407 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
410 /* initialise signing */
411 cli_null_set_signing(cli);
413 cli->initialised = 1;
417 /* Clean up after malloc() error */
421 SAFE_FREE(cli->inbuf);
422 SAFE_FREE(cli->outbuf);
427 /****************************************************************************
429 Close an open named pipe over SMB. Free any authentication data.
430 Returns False if the cli_close call failed.
431 ****************************************************************************/
433 BOOL cli_rpc_pipe_close(struct rpc_pipe_client *cli)
441 ret = cli_close(cli->cli, cli->fnum);
444 DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
446 "to machine %s. Error was %s\n",
450 cli_errstr(cli->cli)));
453 if (cli->auth.cli_auth_data_free_func) {
454 (*cli->auth.cli_auth_data_free_func)(&cli->auth);
457 DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
458 cli->pipe_name, cli->cli->desthost ));
460 DLIST_REMOVE(cli->cli->pipe_list, cli);
461 talloc_destroy(cli->mem_ctx);
465 /****************************************************************************
466 Close all pipes open on this session.
467 ****************************************************************************/
469 void cli_nt_pipes_close(struct cli_state *cli)
471 struct rpc_pipe_client *cp, *next;
473 for (cp = cli->pipe_list; cp; cp = next) {
475 cli_rpc_pipe_close(cp);
479 /****************************************************************************
480 Shutdown a client structure.
481 ****************************************************************************/
483 void cli_shutdown(struct cli_state *cli)
485 cli_nt_pipes_close(cli);
488 * tell our peer to free his resources. Wihtout this, when an
489 * application attempts to do a graceful shutdown and calls
490 * smbc_free_context() to clean up all connections, some connections
491 * can remain active on the peer end, until some (long) timeout period
492 * later. This tree disconnect forces the peer to clean up, since the
493 * connection will be going away.
495 * Also, do not do tree disconnect when cli->smb_rw_error is DO_NOT_DO_TDIS
496 * the only user for this so far is smbmount which passes opened connection
497 * down to kernel's smbfs module.
499 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != DO_NOT_DO_TDIS ) ) {
503 SAFE_FREE(cli->outbuf);
504 SAFE_FREE(cli->inbuf);
506 cli_free_signing_context(cli);
507 cli_free_encryption_context(cli);
509 data_blob_free(&cli->secblob);
510 data_blob_free(&cli->user_session_key);
513 talloc_destroy(cli->mem_ctx);
521 cli->smb_rw_error = 0;
526 /****************************************************************************
527 Set socket options on a open connection.
528 ****************************************************************************/
530 void cli_sockopt(struct cli_state *cli, const char *options)
532 set_socket_options(cli->fd, options);
535 /****************************************************************************
536 Set the PID to use for smb messages. Return the old pid.
537 ****************************************************************************/
539 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
541 uint16 ret = cli->pid;
546 /****************************************************************************
547 Set the case sensitivity flag on the packets. Returns old state.
548 ****************************************************************************/
550 BOOL cli_set_case_sensitive(struct cli_state *cli, BOOL case_sensitive)
552 BOOL ret = cli->case_sensitive;
553 cli->case_sensitive = case_sensitive;
557 /****************************************************************************
558 Send a keepalive packet to the server
559 ****************************************************************************/
561 BOOL cli_send_keepalive(struct cli_state *cli)
564 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
567 if (!send_keepalive(cli->fd)) {
570 DEBUG(0,("Error sending keepalive packet to client.\n"));
576 /****************************************************************************
577 Send/receive a SMBecho command: ping the server
578 ****************************************************************************/
580 BOOL cli_echo(struct cli_state *cli, unsigned char *data, size_t length)
584 SMB_ASSERT(length < 1024);
586 memset(cli->outbuf,'\0',smb_size);
587 set_message(cli->outbuf,1,length,True);
588 SCVAL(cli->outbuf,smb_com,SMBecho);
589 SSVAL(cli->outbuf,smb_tid,65535);
590 SSVAL(cli->outbuf,smb_vwv0,1);
591 cli_setup_packet(cli);
592 p = smb_buf(cli->outbuf);
593 memcpy(p, data, length);
596 cli_setup_bcc(cli, p);
599 if (!cli_receive_smb(cli)) {
603 if (cli_is_error(cli)) {