source: branches/samba-3.0/source/libsmb/clientgen.c@ 815

Last change on this file since 815 was 135, checked in by Paul Smedley, 17 years ago

Update source to 3.0.30

File size: 16.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 SMB client generic functions
4 Copyright (C) Andrew Tridgell 1994-1998
5
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.
10
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.
15
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.
19*/
20
21#include "includes.h"
22
23extern int smb_read_error;
24
25/****************************************************************************
26 Change the timeout (in milliseconds).
27****************************************************************************/
28
29unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
30{
31 unsigned int old_timeout = cli->timeout;
32 cli->timeout = timeout;
33 return old_timeout;
34}
35
36/****************************************************************************
37 Change the port number used to call on.
38****************************************************************************/
39
40int cli_set_port(struct cli_state *cli, int port)
41{
42 cli->port = port;
43 return port;
44}
45
46/****************************************************************************
47 Read an smb from a fd ignoring all keepalive packets.
48 The timeout is in milliseconds
49
50 This is exactly the same as receive_smb except that it never returns
51 a session keepalive packet (just as receive_smb used to do).
52 receive_smb was changed to return keepalives as the oplock processing means this call
53 should never go into a blocking read.
54****************************************************************************/
55
56static BOOL client_receive_smb(int fd,char *buffer, size_t bufsize, unsigned int timeout)
57{
58 BOOL ret;
59
60 for(;;) {
61 ret = receive_smb_raw(fd, buffer, bufsize, timeout);
62
63 if (!ret) {
64 DEBUG(10,("client_receive_smb failed\n"));
65 show_msg(buffer);
66 return ret;
67 }
68
69 /* Ignore session keepalive packets. */
70 if(CVAL(buffer,0) != SMBkeepalive)
71 break;
72 }
73 show_msg(buffer);
74 return ret;
75}
76
77/****************************************************************************
78 Recv an smb.
79****************************************************************************/
80
81BOOL cli_receive_smb(struct cli_state *cli)
82{
83 BOOL ret;
84
85 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
86 if (cli->fd == -1)
87 return False;
88
89 again:
90 ret = client_receive_smb(cli->fd,cli->inbuf, cli->bufsize, cli->timeout);
91
92 if (ret) {
93 /* it might be an oplock break request */
94 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
95 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
96 SVAL(cli->inbuf,smb_vwv6) == 0 &&
97 SVAL(cli->inbuf,smb_vwv7) == 0) {
98 if (cli->oplock_handler) {
99 int fnum = SVAL(cli->inbuf,smb_vwv2);
100 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
101 if (!cli->oplock_handler(cli, fnum, level)) return False;
102 }
103 /* try to prevent loops */
104 SCVAL(cli->inbuf,smb_com,0xFF);
105 goto again;
106 }
107 }
108
109 /* If the server is not responding, note that now */
110 if (!ret) {
111 DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
112 cli->smb_rw_error = smb_read_error;
113 close(cli->fd);
114 cli->fd = -1;
115 return ret;
116 }
117
118 if (!cli_check_sign_mac(cli)) {
119 /*
120 * If we get a signature failure in sessionsetup, then
121 * the server sometimes just reflects the sent signature
122 * back to us. Detect this and allow the upper layer to
123 * retrieve the correct Windows error message.
124 */
125 if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
126 (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
127 (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
128 memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
129 cli_is_error(cli)) {
130
131 /*
132 * Reflected signature on login error.
133 * Set bad sig but don't close fd.
134 */
135 cli->smb_rw_error = READ_BAD_SIG;
136 return True;
137 }
138
139 DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
140 cli->smb_rw_error = READ_BAD_SIG;
141 close(cli->fd);
142 cli->fd = -1;
143 return False;
144 };
145 return True;
146}
147
148static ssize_t write_socket(int fd, const char *buf, size_t len)
149{
150 ssize_t ret=0;
151
152 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
153 ret = write_data(fd,buf,len);
154
155 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
156 if(ret <= 0)
157 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
158 (int)len, fd, strerror(errno) ));
159
160 return(ret);
161}
162
163/****************************************************************************
164 Send an smb to a fd.
165****************************************************************************/
166
167BOOL cli_send_smb(struct cli_state *cli)
168{
169 size_t len;
170 size_t nwritten=0;
171 ssize_t ret;
172
173 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
174 if (cli->fd == -1)
175 return False;
176
177 cli_calculate_sign_mac(cli);
178
179 len = smb_len(cli->outbuf) + 4;
180
181 while (nwritten < len) {
182 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
183 if (ret <= 0) {
184 close(cli->fd);
185 cli->fd = -1;
186 cli->smb_rw_error = WRITE_ERROR;
187 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
188 (int)len,(int)ret, strerror(errno) ));
189 return False;
190 }
191 nwritten += ret;
192 }
193 /* Increment the mid so we can tell between responses. */
194 cli->mid++;
195 if (!cli->mid)
196 cli->mid++;
197 return True;
198}
199
200/****************************************************************************
201 Setup basics in a outgoing packet.
202****************************************************************************/
203
204void cli_setup_packet(struct cli_state *cli)
205{
206 cli->rap_error = 0;
207 SSVAL(cli->outbuf,smb_pid,cli->pid);
208 SSVAL(cli->outbuf,smb_uid,cli->vuid);
209 SSVAL(cli->outbuf,smb_mid,cli->mid);
210 if (cli->protocol > PROTOCOL_CORE) {
211 uint16 flags2;
212 if (cli->case_sensitive) {
213 SCVAL(cli->outbuf,smb_flg,0x0);
214 } else {
215 /* Default setting, case insensitive. */
216 SCVAL(cli->outbuf,smb_flg,0x8);
217 }
218 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
219 if (cli->capabilities & CAP_UNICODE)
220 flags2 |= FLAGS2_UNICODE_STRINGS;
221 if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
222 flags2 |= FLAGS2_DFS_PATHNAMES;
223 if (cli->capabilities & CAP_STATUS32)
224 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
225 if (cli->use_spnego)
226 flags2 |= FLAGS2_EXTENDED_SECURITY;
227 SSVAL(cli->outbuf,smb_flg2, flags2);
228 }
229}
230
231/****************************************************************************
232 Setup the bcc length of the packet from a pointer to the end of the data.
233****************************************************************************/
234
235void cli_setup_bcc(struct cli_state *cli, void *p)
236{
237 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
238}
239
240/****************************************************************************
241 Initialise credentials of a client structure.
242****************************************************************************/
243
244void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
245{
246 fstrcpy(cli->domain, domain);
247 fstrcpy(cli->user_name, username);
248 pwd_set_cleartext(&cli->pwd, password);
249 if (!*username) {
250 cli->pwd.null_pwd = True;
251 }
252
253 DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
254}
255
256/****************************************************************************
257 Set the signing state (used from the command line).
258****************************************************************************/
259
260void cli_setup_signing_state(struct cli_state *cli, int signing_state)
261{
262 if (signing_state == Undefined)
263 return;
264
265 if (signing_state == False) {
266 cli->sign_info.allow_smb_signing = False;
267 cli->sign_info.mandatory_signing = False;
268 return;
269 }
270
271 cli->sign_info.allow_smb_signing = True;
272
273 if (signing_state == Required)
274 cli->sign_info.mandatory_signing = True;
275}
276
277/****************************************************************************
278 Initialise a client structure. Always returns a malloc'ed struct.
279****************************************************************************/
280
281struct cli_state *cli_initialise(void)
282{
283 struct cli_state *cli = NULL;
284
285#ifndef __OS2__
286 /* Check the effective uid - make sure we are not setuid */
287 if (is_setuid_root()) {
288 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
289 return NULL;
290 }
291#endif
292 cli = SMB_MALLOC_P(struct cli_state);
293 if (!cli) {
294 return NULL;
295 }
296
297 ZERO_STRUCTP(cli);
298
299 cli->port = 0;
300 cli->fd = -1;
301 cli->cnum = -1;
302 cli->pid = (uint16)sys_getpid();
303 cli->mid = 1;
304 cli->vuid = UID_FIELD_INVALID;
305 cli->protocol = PROTOCOL_NT1;
306 cli->timeout = 20000; /* Timeout is in milliseconds. */
307 cli->bufsize = CLI_BUFFER_SIZE+4;
308 cli->max_xmit = cli->bufsize;
309 cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
310 cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
311 cli->oplock_handler = cli_oplock_ack;
312 cli->case_sensitive = False;
313 cli->smb_rw_error = 0;
314
315 cli->use_spnego = lp_client_use_spnego();
316
317 cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
318
319 /* Set the CLI_FORCE_DOSERR environment variable to test
320 client routines using DOS errors instead of STATUS32
321 ones. This intended only as a temporary hack. */
322 if (getenv("CLI_FORCE_DOSERR"))
323 cli->force_dos_errors = True;
324
325 if (lp_client_signing())
326 cli->sign_info.allow_smb_signing = True;
327
328 if (lp_client_signing() == Required)
329 cli->sign_info.mandatory_signing = True;
330
331 if (!cli->outbuf || !cli->inbuf)
332 goto error;
333
334 if ((cli->mem_ctx = talloc_init("cli based talloc")) == NULL)
335 goto error;
336
337 memset(cli->outbuf, 0, cli->bufsize);
338 memset(cli->inbuf, 0, cli->bufsize);
339
340
341#if defined(DEVELOPER)
342 /* just because we over-allocate, doesn't mean it's right to use it */
343 clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
344 clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
345#endif
346
347 /* initialise signing */
348 cli_null_set_signing(cli);
349
350 cli->initialised = 1;
351
352 return cli;
353
354 /* Clean up after malloc() error */
355
356 error:
357
358 SAFE_FREE(cli->inbuf);
359 SAFE_FREE(cli->outbuf);
360 SAFE_FREE(cli);
361 return NULL;
362}
363
364/****************************************************************************
365 External interface.
366 Close an open named pipe over SMB. Free any authentication data.
367 Returns False if the cli_close call failed.
368 ****************************************************************************/
369
370BOOL cli_rpc_pipe_close(struct rpc_pipe_client *cli)
371{
372 BOOL ret;
373
374 if (!cli) {
375 return False;
376 }
377
378 ret = cli_close(cli->cli, cli->fnum);
379
380 if (!ret) {
381 DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
382 "fnum 0x%x "
383 "to machine %s. Error was %s\n",
384 cli->pipe_name,
385 (int) cli->fnum,
386 cli->cli->desthost,
387 cli_errstr(cli->cli)));
388 }
389
390 if (cli->auth.cli_auth_data_free_func) {
391 (*cli->auth.cli_auth_data_free_func)(&cli->auth);
392 }
393
394 DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
395 cli->pipe_name, cli->cli->desthost ));
396
397 DLIST_REMOVE(cli->cli->pipe_list, cli);
398 talloc_destroy(cli->mem_ctx);
399 return ret;
400}
401
402/****************************************************************************
403 Close all pipes open on this session.
404****************************************************************************/
405
406void cli_nt_pipes_close(struct cli_state *cli)
407{
408 struct rpc_pipe_client *cp, *next;
409
410 for (cp = cli->pipe_list; cp; cp = next) {
411 next = cp->next;
412 cli_rpc_pipe_close(cp);
413 }
414}
415
416/****************************************************************************
417 Shutdown a client structure.
418****************************************************************************/
419
420void cli_shutdown(struct cli_state *cli)
421{
422 cli_nt_pipes_close(cli);
423
424 /*
425 * tell our peer to free his resources. Wihtout this, when an
426 * application attempts to do a graceful shutdown and calls
427 * smbc_free_context() to clean up all connections, some connections
428 * can remain active on the peer end, until some (long) timeout period
429 * later. This tree disconnect forces the peer to clean up, since the
430 * connection will be going away.
431 *
432 * Also, do not do tree disconnect when cli->smb_rw_error is DO_NOT_DO_TDIS
433 * the only user for this so far is smbmount which passes opened connection
434 * down to kernel's smbfs module.
435 */
436 if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != DO_NOT_DO_TDIS ) ) {
437 cli_tdis(cli);
438 }
439
440 SAFE_FREE(cli->outbuf);
441 SAFE_FREE(cli->inbuf);
442
443 cli_free_signing_context(cli);
444 data_blob_free(&cli->secblob);
445 data_blob_free(&cli->user_session_key);
446
447 if (cli->mem_ctx) {
448 talloc_destroy(cli->mem_ctx);
449 cli->mem_ctx = NULL;
450 }
451
452 if (cli->fd != -1) {
453 close(cli->fd);
454 }
455 cli->fd = -1;
456 cli->smb_rw_error = 0;
457
458 SAFE_FREE(cli);
459}
460
461/****************************************************************************
462 Set socket options on a open connection.
463****************************************************************************/
464
465void cli_sockopt(struct cli_state *cli, const char *options)
466{
467 set_socket_options(cli->fd, options);
468}
469
470/****************************************************************************
471 Set the PID to use for smb messages. Return the old pid.
472****************************************************************************/
473
474uint16 cli_setpid(struct cli_state *cli, uint16 pid)
475{
476 uint16 ret = cli->pid;
477 cli->pid = pid;
478 return ret;
479}
480
481/****************************************************************************
482 Set the case sensitivity flag on the packets. Returns old state.
483****************************************************************************/
484
485BOOL cli_set_case_sensitive(struct cli_state *cli, BOOL case_sensitive)
486{
487 BOOL ret = cli->case_sensitive;
488 cli->case_sensitive = case_sensitive;
489 return ret;
490}
491
492/****************************************************************************
493Send a keepalive packet to the server
494****************************************************************************/
495
496BOOL cli_send_keepalive(struct cli_state *cli)
497{
498 if (cli->fd == -1) {
499 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
500 return False;
501 }
502 if (!send_keepalive(cli->fd)) {
503 close(cli->fd);
504 cli->fd = -1;
505 DEBUG(0,("Error sending keepalive packet to client.\n"));
506 return False;
507 }
508 return True;
509}
510
511/****************************************************************************
512 Send/receive a SMBecho command: ping the server
513****************************************************************************/
514
515BOOL cli_echo(struct cli_state *cli, unsigned char *data, size_t length)
516{
517 char *p;
518
519 SMB_ASSERT(length < 1024);
520
521 memset(cli->outbuf,'\0',smb_size);
522 set_message(cli->outbuf,1,length,True);
523 SCVAL(cli->outbuf,smb_com,SMBecho);
524 SSVAL(cli->outbuf,smb_tid,65535);
525 SSVAL(cli->outbuf,smb_vwv0,1);
526 cli_setup_packet(cli);
527 p = smb_buf(cli->outbuf);
528 memcpy(p, data, length);
529 p += length;
530
531 cli_setup_bcc(cli, p);
532
533 cli_send_smb(cli);
534 if (!cli_receive_smb(cli)) {
535 return False;
536 }
537
538 if (cli_is_error(cli)) {
539 return False;
540 }
541 return True;
542}
Note: See TracBrowser for help on using the repository browser.