~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/libsmb/clientgen.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

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