source: trunk/samba-3.0.25pre1/source/libsmb/clientgen.c@ 1

Last change on this file since 1 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

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