r6225: get rid of warnings from my compiler about nested externs
[samba.git] / source / libsmb / clientgen.c
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 #define NO_SYSLOG
22
23 #include "includes.h"
24
25 extern int smb_read_error;
26
27 /****************************************************************************
28  Change the timeout (in milliseconds).
29 ****************************************************************************/
30
31 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
32 {
33         unsigned int old_timeout = cli->timeout;
34         cli->timeout = timeout;
35         return old_timeout;
36 }
37
38 /****************************************************************************
39  Change the port number used to call on.
40 ****************************************************************************/
41
42 int cli_set_port(struct cli_state *cli, int port)
43 {
44         cli->port = port;
45         return port;
46 }
47
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
52
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 ****************************************************************************/
58
59 static BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
60 {
61         BOOL ret;
62
63         for(;;) {
64                 ret = receive_smb_raw(fd, buffer, timeout);
65
66                 if (!ret) {
67                         DEBUG(10,("client_receive_smb failed\n"));
68                         show_msg(buffer);
69                         return ret;
70                 }
71
72                 /* Ignore session keepalive packets. */
73                 if(CVAL(buffer,0) != SMBkeepalive)
74                         break;
75         }
76         show_msg(buffer);
77         return ret;
78 }
79
80 /****************************************************************************
81  Recv an smb.
82 ****************************************************************************/
83
84 BOOL cli_receive_smb(struct cli_state *cli)
85 {
86         BOOL ret;
87
88         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
89         if (cli->fd == -1)
90                 return False; 
91
92  again:
93         ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
94         
95         if (ret) {
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;
105                         }
106                         /* try to prevent loops */
107                         SCVAL(cli->inbuf,smb_com,0xFF);
108                         goto again;
109                 }
110         }
111
112         /* If the server is not responding, note that now */
113
114         if (!ret) {
115                 cli->smb_rw_error = smb_read_error;
116                 close(cli->fd);
117                 cli->fd = -1;
118                 return ret;
119         }
120
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;
124                 close(cli->fd);
125                 cli->fd = -1;
126                 return False;
127         };
128         return True;
129 }
130
131 /****************************************************************************
132  Send an smb to a fd.
133 ****************************************************************************/
134
135 BOOL cli_send_smb(struct cli_state *cli)
136 {
137         size_t len;
138         size_t nwritten=0;
139         ssize_t ret;
140
141         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
142         if (cli->fd == -1)
143                 return False;
144
145         cli_calculate_sign_mac(cli);
146
147         len = smb_len(cli->outbuf) + 4;
148
149         while (nwritten < len) {
150                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
151                 if (ret <= 0) {
152                         close(cli->fd);
153                         cli->fd = -1;
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) ));
157                         return False;
158                 }
159                 nwritten += ret;
160         }
161         /* Increment the mid so we can tell between responses. */
162         cli->mid++;
163         if (!cli->mid)
164                 cli->mid++;
165         return True;
166 }
167
168 /****************************************************************************
169  Setup basics in a outgoing packet.
170 ****************************************************************************/
171
172 void cli_setup_packet(struct cli_state *cli)
173 {
174         cli->rap_error = 0;
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) {
179                 uint16 flags2;
180                 if (cli->case_sensitive) {
181                         SCVAL(cli->outbuf,smb_flg,0x0);
182                 } else {
183                         /* Default setting, case insensitive. */
184                         SCVAL(cli->outbuf,smb_flg,0x8);
185                 }
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;
193                 if (cli->use_spnego)
194                         flags2 |= FLAGS2_EXTENDED_SECURITY;
195                 SSVAL(cli->outbuf,smb_flg2, flags2);
196         }
197 }
198
199 /****************************************************************************
200  Setup the bcc length of the packet from a pointer to the end of the data.
201 ****************************************************************************/
202
203 void cli_setup_bcc(struct cli_state *cli, void *p)
204 {
205         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
206 }
207
208 /****************************************************************************
209  Initialise credentials of a client structure.
210 ****************************************************************************/
211
212 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
213 {
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));
218
219         DEBUG(10,("cli_init_creds: user %s domain %s\n",
220                cli->user_name, cli->domain));
221 }
222
223 /****************************************************************************
224  Set the signing state (used from the command line).
225 ****************************************************************************/
226
227 void cli_setup_signing_state(struct cli_state *cli, int signing_state)
228 {
229         if (signing_state == Undefined)
230                 return;
231
232         if (signing_state == False) {
233                 cli->sign_info.allow_smb_signing = False;
234                 cli->sign_info.mandatory_signing = False;
235                 return;
236         }
237
238         cli->sign_info.allow_smb_signing = True;
239
240         if (signing_state == Required) 
241                 cli->sign_info.mandatory_signing = True;
242 }
243
244 /****************************************************************************
245  Initialise a client structure.
246 ****************************************************************************/
247
248 struct cli_state *cli_initialise(struct cli_state *cli)
249 {
250         BOOL alloced_cli = False;
251         int i;
252
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"));
256                 return NULL;
257         }
258
259         if (!cli) {
260                 cli = SMB_MALLOC_P(struct cli_state);
261                 if (!cli)
262                         return NULL;
263                 ZERO_STRUCTP(cli);
264                 alloced_cli = True;
265         }
266
267         if (cli->initialised)
268                 cli_close_connection(cli);
269
270         ZERO_STRUCTP(cli);
271
272         cli->port = 0;
273         cli->fd = -1;
274         cli->cnum = -1;
275         cli->pid = (uint16)sys_getpid();
276         cli->mid = 1;
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;
287
288         cli->use_spnego = lp_client_use_spnego();
289
290         cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
291
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;
297
298         if (lp_client_signing()) 
299                 cli->sign_info.allow_smb_signing = True;
300
301         if (lp_client_signing() == Required) 
302                 cli->sign_info.mandatory_signing = True;
303                                    
304         if (!cli->outbuf || !cli->inbuf)
305                 goto error;
306
307         if ((cli->mem_ctx = talloc_init("cli based talloc")) == NULL)
308                 goto error;
309
310         memset(cli->outbuf, 0, cli->bufsize);
311         memset(cli->inbuf, 0, cli->bufsize);
312
313
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);
318 #endif
319
320         /* initialise signing */
321         cli_null_set_signing(cli);
322
323         for (i=0; i<PI_MAX_PIPES; i++)
324                 cli->nt_pipe_fnum[i] = 0;
325
326         cli->saved_netlogon_pipe_fnum = 0;
327
328         cli->initialised = 1;
329         cli->allocated = alloced_cli;
330
331         cli->pipe_idx = -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
342         if (alloced_cli)
343                 SAFE_FREE(cli);
344
345         return NULL;
346 }
347
348 /****************************************************************************
349 close the session
350 ****************************************************************************/
351
352 void cli_nt_session_close(struct cli_state *cli)
353 {
354         int i;
355
356         if (cli->ntlmssp_pipe_state) {
357                 ntlmssp_end(&cli->ntlmssp_pipe_state);
358         }
359
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;
364         }
365         cli->pipe_idx = -1;
366 }
367
368 /****************************************************************************
369 close the NETLOGON session holding the session key for NETSEC
370 ****************************************************************************/
371
372 void cli_nt_netlogon_netsec_session_close(struct cli_state *cli)
373 {
374         if (cli->saved_netlogon_pipe_fnum != 0) {
375                 cli_close(cli, cli->saved_netlogon_pipe_fnum);
376                 cli->saved_netlogon_pipe_fnum = 0;
377         }
378 }
379
380 /****************************************************************************
381  Close a client connection and free the memory without destroying cli itself.
382 ****************************************************************************/
383
384 void cli_close_connection(struct cli_state *cli)
385 {
386         cli_nt_session_close(cli);
387         cli_nt_netlogon_netsec_session_close(cli);
388
389         /*
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.
396          *
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.
400          */
401         if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != DO_NOT_DO_TDIS ) )
402                 cli_tdis(cli);
403         
404         SAFE_FREE(cli->outbuf);
405         SAFE_FREE(cli->inbuf);
406
407         cli_free_signing_context(cli);
408         data_blob_free(&cli->secblob);
409         data_blob_free(&cli->user_session_key);
410
411         if (cli->ntlmssp_pipe_state) 
412                 ntlmssp_end(&cli->ntlmssp_pipe_state);
413
414         if (cli->mem_ctx) {
415                 talloc_destroy(cli->mem_ctx);
416                 cli->mem_ctx = NULL;
417         }
418
419         if (cli->fd != -1) 
420                 close(cli->fd);
421         cli->fd = -1;
422         cli->smb_rw_error = 0;
423
424 }
425
426 /****************************************************************************
427  Shutdown a client structure.
428 ****************************************************************************/
429
430 void cli_shutdown(struct cli_state *cli)
431 {
432         BOOL allocated = cli->allocated;
433         cli_close_connection(cli);
434         ZERO_STRUCTP(cli);
435         if (allocated)
436                 free(cli);
437 }
438
439 /****************************************************************************
440  Set socket options on a open connection.
441 ****************************************************************************/
442
443 void cli_sockopt(struct cli_state *cli, const char *options)
444 {
445         set_socket_options(cli->fd, options);
446 }
447
448 /****************************************************************************
449  Set the PID to use for smb messages. Return the old pid.
450 ****************************************************************************/
451
452 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
453 {
454         uint16 ret = cli->pid;
455         cli->pid = pid;
456         return ret;
457 }
458
459 /****************************************************************************
460  Set the case sensitivity flag on the packets. Returns old state.
461 ****************************************************************************/
462
463 BOOL cli_set_case_sensitive(struct cli_state *cli, BOOL case_sensitive)
464 {
465         BOOL ret = cli->case_sensitive;
466         cli->case_sensitive = case_sensitive;
467         return ret;
468 }
469
470 /****************************************************************************
471 Send a keepalive packet to the server
472 ****************************************************************************/
473 BOOL cli_send_keepalive(struct cli_state *cli)
474 {
475         if (cli->fd == -1) {
476                 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
477                 return False;
478         }
479         if (!send_keepalive(cli->fd)) {
480                 close(cli->fd);
481                 cli->fd = -1;
482                 DEBUG(0,("Error sending keepalive packet to client.\n"));
483                 return False;
484         }
485         return True;
486 }