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.
25 extern int smb_read_error;
27 /****************************************************************************
28 Change the timeout (in milliseconds).
29 ****************************************************************************/
31 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
33 unsigned int old_timeout = cli->timeout;
34 cli->timeout = timeout;
38 /****************************************************************************
39 Change the port number used to call on.
40 ****************************************************************************/
42 int cli_set_port(struct cli_state *cli, int port)
48 /****************************************************************************
49 Read an smb from a fd ignoring all keepalive packets. Note that the buffer
50 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
51 The timeout is in milliseconds
53 This is exactly the same as receive_smb except that it never returns
54 a session keepalive packet (just as receive_smb used to do).
55 receive_smb was changed to return keepalives as the oplock processing means this call
56 should never go into a blocking read.
57 ****************************************************************************/
59 static BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
64 ret = receive_smb_raw(fd, buffer, timeout);
67 DEBUG(10,("client_receive_smb failed\n"));
72 /* Ignore session keepalive packets. */
73 if(CVAL(buffer,0) != SMBkeepalive)
80 /****************************************************************************
82 ****************************************************************************/
84 BOOL cli_receive_smb(struct cli_state *cli)
88 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
93 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
96 /* it might be an oplock break request */
97 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
98 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
99 SVAL(cli->inbuf,smb_vwv6) == 0 &&
100 SVAL(cli->inbuf,smb_vwv7) == 0) {
101 if (cli->oplock_handler) {
102 int fnum = SVAL(cli->inbuf,smb_vwv2);
103 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
104 if (!cli->oplock_handler(cli, fnum, level)) return False;
106 /* try to prevent loops */
107 SCVAL(cli->inbuf,smb_com,0xFF);
112 /* If the server is not responding, note that now */
115 cli->smb_rw_error = smb_read_error;
121 if (!cli_check_sign_mac(cli)) {
122 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
123 cli->smb_rw_error = READ_BAD_SIG;
131 /****************************************************************************
133 ****************************************************************************/
135 BOOL cli_send_smb(struct cli_state *cli)
141 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
145 cli_calculate_sign_mac(cli);
147 len = smb_len(cli->outbuf) + 4;
149 while (nwritten < len) {
150 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
154 cli->smb_rw_error = WRITE_ERROR;
155 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
156 (int)len,(int)ret, strerror(errno) ));
161 /* Increment the mid so we can tell between responses. */
168 /****************************************************************************
169 Setup basics in a outgoing packet.
170 ****************************************************************************/
172 void cli_setup_packet(struct cli_state *cli)
175 SSVAL(cli->outbuf,smb_pid,cli->pid);
176 SSVAL(cli->outbuf,smb_uid,cli->vuid);
177 SSVAL(cli->outbuf,smb_mid,cli->mid);
178 if (cli->protocol > PROTOCOL_CORE) {
180 if (cli->case_sensitive) {
181 SCVAL(cli->outbuf,smb_flg,0x0);
183 /* Default setting, case insensitive. */
184 SCVAL(cli->outbuf,smb_flg,0x8);
186 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
187 if (cli->capabilities & CAP_UNICODE)
188 flags2 |= FLAGS2_UNICODE_STRINGS;
189 if (cli->capabilities & CAP_DFS)
190 flags2 |= FLAGS2_DFS_PATHNAMES;
191 if (cli->capabilities & CAP_STATUS32)
192 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
194 flags2 |= FLAGS2_EXTENDED_SECURITY;
195 SSVAL(cli->outbuf,smb_flg2, flags2);
199 /****************************************************************************
200 Setup the bcc length of the packet from a pointer to the end of the data.
201 ****************************************************************************/
203 void cli_setup_bcc(struct cli_state *cli, void *p)
205 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
208 /****************************************************************************
209 Initialise credentials of a client structure.
210 ****************************************************************************/
212 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
214 /* copy_nt_creds(&cli->usr, usr); */
215 fstrcpy(cli->domain , usr->domain);
216 fstrcpy(cli->user_name, usr->user_name);
217 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
219 DEBUG(10,("cli_init_creds: user %s domain %s\n",
220 cli->user_name, cli->domain));
223 /****************************************************************************
224 Set the signing state (used from the command line).
225 ****************************************************************************/
227 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
229 if (signing_state == Undefined)
232 if (signing_state == False) {
233 cli->sign_info.allow_smb_signing = False;
234 cli->sign_info.mandatory_signing = False;
238 cli->sign_info.allow_smb_signing = True;
240 if (signing_state == Required)
241 cli->sign_info.mandatory_signing = True;
244 /****************************************************************************
245 Initialise a client structure.
246 ****************************************************************************/
248 struct cli_state *cli_initialise(struct cli_state *cli)
250 BOOL alloced_cli = False;
253 /* Check the effective uid - make sure we are not setuid */
254 if (is_setuid_root()) {
255 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
260 cli = SMB_MALLOC_P(struct cli_state);
267 if (cli->initialised)
268 cli_close_connection(cli);
275 cli->pid = (uint16)sys_getpid();
277 cli->vuid = UID_FIELD_INVALID;
278 cli->protocol = PROTOCOL_NT1;
279 cli->timeout = 20000; /* Timeout is in milliseconds. */
280 cli->bufsize = CLI_BUFFER_SIZE+4;
281 cli->max_xmit = cli->bufsize;
282 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
283 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
284 cli->oplock_handler = cli_oplock_ack;
285 cli->case_sensitive = False;
286 cli->smb_rw_error = 0;
288 cli->use_spnego = lp_client_use_spnego();
290 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
292 /* Set the CLI_FORCE_DOSERR environment variable to test
293 client routines using DOS errors instead of STATUS32
294 ones. This intended only as a temporary hack. */
295 if (getenv("CLI_FORCE_DOSERR"))
296 cli->force_dos_errors = True;
298 if (lp_client_signing())
299 cli->sign_info.allow_smb_signing = True;
301 if (lp_client_signing() == Required)
302 cli->sign_info.mandatory_signing = True;
304 if (!cli->outbuf || !cli->inbuf)
307 if ((cli->mem_ctx = talloc_init("cli based talloc")) == NULL)
310 memset(cli->outbuf, 0, cli->bufsize);
311 memset(cli->inbuf, 0, cli->bufsize);
314 #if defined(DEVELOPER)
315 /* just because we over-allocate, doesn't mean it's right to use it */
316 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
317 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
320 /* initialise signing */
321 cli_null_set_signing(cli);
323 for (i=0; i<PI_MAX_PIPES; i++)
324 cli->nt_pipe_fnum[i] = 0;
326 cli->saved_netlogon_pipe_fnum = 0;
328 cli->initialised = 1;
329 cli->allocated = alloced_cli;
335 /* Clean up after malloc() error */
339 SAFE_FREE(cli->inbuf);
340 SAFE_FREE(cli->outbuf);
348 /****************************************************************************
350 ****************************************************************************/
352 void cli_nt_session_close(struct cli_state *cli)
356 if (cli->ntlmssp_pipe_state) {
357 ntlmssp_end(&cli->ntlmssp_pipe_state);
360 for (i=0; i<PI_MAX_PIPES; i++) {
361 if (cli->nt_pipe_fnum[i] != 0)
362 cli_close(cli, cli->nt_pipe_fnum[i]);
363 cli->nt_pipe_fnum[i] = 0;
368 /****************************************************************************
369 close the NETLOGON session holding the session key for NETSEC
370 ****************************************************************************/
372 void cli_nt_netlogon_netsec_session_close(struct cli_state *cli)
374 if (cli->saved_netlogon_pipe_fnum != 0) {
375 cli_close(cli, cli->saved_netlogon_pipe_fnum);
376 cli->saved_netlogon_pipe_fnum = 0;
380 /****************************************************************************
381 Close a client connection and free the memory without destroying cli itself.
382 ****************************************************************************/
384 void cli_close_connection(struct cli_state *cli)
386 cli_nt_session_close(cli);
387 cli_nt_netlogon_netsec_session_close(cli);
390 * tell our peer to free his resources. Wihtout this, when an
391 * application attempts to do a graceful shutdown and calls
392 * smbc_free_context() to clean up all connections, some connections
393 * can remain active on the peer end, until some (long) timeout period
394 * later. This tree disconnect forces the peer to clean up, since the
395 * connection will be going away.
397 * Also, do not do tree disconnect when cli->smb_rw_error is DO_NOT_DO_TDIS
398 * the only user for this so far is smbmount which passes opened connection
399 * down to kernel's smbfs module.
401 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != DO_NOT_DO_TDIS ) )
404 SAFE_FREE(cli->outbuf);
405 SAFE_FREE(cli->inbuf);
407 cli_free_signing_context(cli);
408 data_blob_free(&cli->secblob);
409 data_blob_free(&cli->user_session_key);
411 if (cli->ntlmssp_pipe_state)
412 ntlmssp_end(&cli->ntlmssp_pipe_state);
415 talloc_destroy(cli->mem_ctx);
422 cli->smb_rw_error = 0;
426 /****************************************************************************
427 Shutdown a client structure.
428 ****************************************************************************/
430 void cli_shutdown(struct cli_state *cli)
432 BOOL allocated = cli->allocated;
433 cli_close_connection(cli);
439 /****************************************************************************
440 Set socket options on a open connection.
441 ****************************************************************************/
443 void cli_sockopt(struct cli_state *cli, const char *options)
445 set_socket_options(cli->fd, options);
448 /****************************************************************************
449 Set the PID to use for smb messages. Return the old pid.
450 ****************************************************************************/
452 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
454 uint16 ret = cli->pid;
459 /****************************************************************************
460 Set the case sensitivity flag on the packets. Returns old state.
461 ****************************************************************************/
463 BOOL cli_set_case_sensitive(struct cli_state *cli, BOOL case_sensitive)
465 BOOL ret = cli->case_sensitive;
466 cli->case_sensitive = case_sensitive;
470 /****************************************************************************
471 Send a keepalive packet to the server
472 ****************************************************************************/
473 BOOL cli_send_keepalive(struct cli_state *cli)
476 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
479 if (!send_keepalive(cli->fd)) {
482 DEBUG(0,("Error sending keepalive packet to client.\n"));