r15: syncing for 3.0.3pre2
[Samba.git] / source / libsmb / clientgen.c
blob66edc3ce38bbc81d34a3e412b1da8e8c4a22e94c
1 /*
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.
21 #define NO_SYSLOG
23 #include "includes.h"
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;
33 return old_timeout;
36 /****************************************************************************
37 Change the port number used to call on.
38 ****************************************************************************/
40 int cli_set_port(struct cli_state *cli, int port)
42 cli->port = port;
43 return 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 never returns
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(int fd,char *buffer, unsigned int timeout)
59 BOOL ret;
61 for(;;) {
62 ret = receive_smb_raw(fd, buffer, timeout);
64 if (!ret) {
65 DEBUG(10,("client_receive_smb failed\n"));
66 show_msg(buffer);
67 return ret;
70 /* Ignore session keepalive packets. */
71 if(CVAL(buffer,0) != SMBkeepalive)
72 break;
74 show_msg(buffer);
75 return ret;
78 /****************************************************************************
79 Recv an smb.
80 ****************************************************************************/
82 BOOL cli_receive_smb(struct cli_state *cli)
84 extern int smb_read_error;
85 BOOL ret;
87 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
88 if (cli->fd == -1)
89 return False;
91 again:
92 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
94 if (ret) {
95 /* it might be an oplock break request */
96 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
97 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
98 SVAL(cli->inbuf,smb_vwv6) == 0 &&
99 SVAL(cli->inbuf,smb_vwv7) == 0) {
100 if (cli->oplock_handler) {
101 int fnum = SVAL(cli->inbuf,smb_vwv2);
102 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
103 if (!cli->oplock_handler(cli, fnum, level)) return False;
105 /* try to prevent loops */
106 SCVAL(cli->inbuf,smb_com,0xFF);
107 goto again;
111 /* If the server is not responding, note that now */
113 if (!ret) {
114 cli->smb_rw_error = smb_read_error;
115 close(cli->fd);
116 cli->fd = -1;
117 return ret;
120 if (!cli_check_sign_mac(cli, True)) {
121 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
122 cli->smb_rw_error = READ_BAD_SIG;
123 close(cli->fd);
124 cli->fd = -1;
125 return False;
127 return True;
130 /****************************************************************************
131 Send an smb to a fd.
132 ****************************************************************************/
134 BOOL cli_send_smb(struct cli_state *cli)
136 size_t len;
137 size_t nwritten=0;
138 ssize_t ret;
140 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
141 if (cli->fd == -1)
142 return False;
144 cli_calculate_sign_mac(cli);
146 len = smb_len(cli->outbuf) + 4;
148 while (nwritten < len) {
149 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
150 if (ret <= 0) {
151 close(cli->fd);
152 cli->fd = -1;
153 cli->smb_rw_error = WRITE_ERROR;
154 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
155 (int)len,(int)ret, strerror(errno) ));
156 return False;
158 nwritten += ret;
160 /* Increment the mid so we can tell between responses. */
161 cli->mid++;
162 if (!cli->mid)
163 cli->mid++;
164 return True;
167 /****************************************************************************
168 Setup basics in a outgoing packet.
169 ****************************************************************************/
171 void cli_setup_packet(struct cli_state *cli)
173 cli->rap_error = 0;
174 SSVAL(cli->outbuf,smb_pid,cli->pid);
175 SSVAL(cli->outbuf,smb_uid,cli->vuid);
176 SSVAL(cli->outbuf,smb_mid,cli->mid);
177 if (cli->protocol > PROTOCOL_CORE) {
178 uint16 flags2;
179 SCVAL(cli->outbuf,smb_flg,0x8);
180 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
181 if (cli->capabilities & CAP_UNICODE)
182 flags2 |= FLAGS2_UNICODE_STRINGS;
183 if (cli->capabilities & CAP_STATUS32)
184 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
185 if (cli->use_spnego)
186 flags2 |= FLAGS2_EXTENDED_SECURITY;
187 SSVAL(cli->outbuf,smb_flg2, flags2);
191 /****************************************************************************
192 Setup the bcc length of the packet from a pointer to the end of the data.
193 ****************************************************************************/
195 void cli_setup_bcc(struct cli_state *cli, void *p)
197 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
200 /****************************************************************************
201 Initialise credentials of a client structure.
202 ****************************************************************************/
204 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
206 /* copy_nt_creds(&cli->usr, usr); */
207 fstrcpy(cli->domain , usr->domain);
208 fstrcpy(cli->user_name, usr->user_name);
209 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
211 DEBUG(10,("cli_init_creds: user %s domain %s\n",
212 cli->user_name, cli->domain));
215 /****************************************************************************
216 Set the signing state (used from the command line).
217 ****************************************************************************/
219 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
221 if (signing_state == Undefined)
222 return;
224 if (signing_state == False) {
225 cli->sign_info.allow_smb_signing = False;
226 cli->sign_info.mandatory_signing = False;
227 return;
230 cli->sign_info.allow_smb_signing = True;
232 if (signing_state == Required)
233 cli->sign_info.mandatory_signing = True;
236 /****************************************************************************
237 Initialise a client structure.
238 ****************************************************************************/
240 struct cli_state *cli_initialise(struct cli_state *cli)
242 BOOL alloced_cli = False;
244 /* Check the effective uid - make sure we are not setuid */
245 if (is_setuid_root()) {
246 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
247 return NULL;
250 if (!cli) {
251 cli = (struct cli_state *)malloc(sizeof(*cli));
252 if (!cli)
253 return NULL;
254 ZERO_STRUCTP(cli);
255 alloced_cli = True;
258 if (cli->initialised)
259 cli_close_connection(cli);
261 ZERO_STRUCTP(cli);
263 cli->port = 0;
264 cli->fd = -1;
265 cli->cnum = -1;
266 cli->pid = (uint16)sys_getpid();
267 cli->mid = 1;
268 cli->vuid = UID_FIELD_INVALID;
269 cli->protocol = PROTOCOL_NT1;
270 cli->timeout = 20000; /* Timeout is in milliseconds. */
271 cli->bufsize = CLI_BUFFER_SIZE+4;
272 cli->max_xmit = cli->bufsize;
273 cli->outbuf = (char *)malloc(cli->bufsize+SAFETY_MARGIN);
274 cli->inbuf = (char *)malloc(cli->bufsize+SAFETY_MARGIN);
275 cli->oplock_handler = cli_oplock_ack;
277 cli->use_spnego = lp_client_use_spnego();
279 cli->capabilities = CAP_UNICODE | CAP_STATUS32;
281 /* Set the CLI_FORCE_DOSERR environment variable to test
282 client routines using DOS errors instead of STATUS32
283 ones. This intended only as a temporary hack. */
284 if (getenv("CLI_FORCE_DOSERR"))
285 cli->force_dos_errors = True;
287 if (lp_client_signing())
288 cli->sign_info.allow_smb_signing = True;
290 if (lp_client_signing() == Required)
291 cli->sign_info.mandatory_signing = True;
293 if (!cli->outbuf || !cli->inbuf)
294 goto error;
296 if ((cli->mem_ctx = talloc_init("cli based talloc")) == NULL)
297 goto error;
299 memset(cli->outbuf, 0, cli->bufsize);
300 memset(cli->inbuf, 0, cli->bufsize);
303 #if defined(DEVELOPER)
304 /* just because we over-allocate, doesn't mean it's right to use it */
305 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
306 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
307 #endif
309 /* initialise signing */
310 cli_null_set_signing(cli);
312 cli->nt_pipe_fnum = 0;
313 cli->saved_netlogon_pipe_fnum = 0;
315 cli->initialised = 1;
316 cli->allocated = alloced_cli;
318 cli->pipe_idx = -1;
320 return cli;
322 /* Clean up after malloc() error */
324 error:
326 SAFE_FREE(cli->inbuf);
327 SAFE_FREE(cli->outbuf);
329 if (alloced_cli)
330 SAFE_FREE(cli);
332 return NULL;
335 /****************************************************************************
336 close the session
337 ****************************************************************************/
339 void cli_nt_session_close(struct cli_state *cli)
341 if (cli->ntlmssp_pipe_state) {
342 ntlmssp_end(&cli->ntlmssp_pipe_state);
345 if (cli->nt_pipe_fnum != 0)
346 cli_close(cli, cli->nt_pipe_fnum);
348 cli->nt_pipe_fnum = 0;
349 cli->pipe_idx = -1;
352 /****************************************************************************
353 close the NETLOGON session holding the session key for NETSEC
354 ****************************************************************************/
356 void cli_nt_netlogon_netsec_session_close(struct cli_state *cli)
358 if (cli->saved_netlogon_pipe_fnum != 0) {
359 cli_close(cli, cli->saved_netlogon_pipe_fnum);
360 cli->saved_netlogon_pipe_fnum = 0;
364 /****************************************************************************
365 Close a client connection and free the memory without destroying cli itself.
366 ****************************************************************************/
368 void cli_close_connection(struct cli_state *cli)
371 * tell our peer to free his resources. Wihtout this, when an
372 * application attempts to do a graceful shutdown and calls
373 * smbc_free_context() to clean up all connections, some connections
374 * can remain active on the peer end, until some (long) timeout period
375 * later. This tree disconnect forces the peer to clean up, since the
376 * connection will be going away.
378 if ( cli->cnum != (uint16)-1 )
379 cli_tdis(cli);
381 cli_nt_session_close(cli);
382 cli_nt_netlogon_netsec_session_close(cli);
384 SAFE_FREE(cli->outbuf);
385 SAFE_FREE(cli->inbuf);
387 cli_free_signing_context(cli);
388 data_blob_free(&cli->secblob);
389 data_blob_free(&cli->user_session_key);
391 if (cli->ntlmssp_pipe_state)
392 ntlmssp_end(&cli->ntlmssp_pipe_state);
394 if (cli->mem_ctx) {
395 talloc_destroy(cli->mem_ctx);
396 cli->mem_ctx = NULL;
399 if (cli->fd != -1)
400 close(cli->fd);
401 cli->fd = -1;
402 cli->smb_rw_error = 0;
406 /****************************************************************************
407 Shutdown a client structure.
408 ****************************************************************************/
410 void cli_shutdown(struct cli_state *cli)
412 BOOL allocated = cli->allocated;
413 cli_close_connection(cli);
414 ZERO_STRUCTP(cli);
415 if (allocated)
416 free(cli);
419 /****************************************************************************
420 Set socket options on a open connection.
421 ****************************************************************************/
423 void cli_sockopt(struct cli_state *cli, const char *options)
425 set_socket_options(cli->fd, options);
428 /****************************************************************************
429 Set the PID to use for smb messages. Return the old pid.
430 ****************************************************************************/
432 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
434 uint16 ret = cli->pid;
435 cli->pid = pid;
436 return ret;
439 /****************************************************************************
440 Send a keepalive packet to the server
441 ****************************************************************************/
442 BOOL cli_send_keepalive(struct cli_state *cli)
444 if (cli->fd == -1) {
445 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
446 return False;
448 if (!send_keepalive(cli->fd)) {
449 close(cli->fd);
450 cli->fd = -1;
451 DEBUG(0,("Error sending keepalive packet to client.\n"));
452 return False;
454 return True;