Forward port the change to talloc_init() to make all talloc contexts
[kai/samba-autobuild/.git] / source3 / 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 /****************************************************************************
26  Change the timeout (in milliseconds).
27 ****************************************************************************/
28
29 unsigned 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
40 int 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
57 static BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
58 {
59         BOOL ret;
60
61         for(;;) {
62                 ret = receive_smb(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
82 BOOL cli_receive_smb(struct cli_state *cli)
83 {
84         extern int smb_read_error;
85         BOOL ret;
86
87         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
88         if (cli->fd == -1)
89                 return False; 
90
91  again:
92         ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
93         
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;
104                         }
105                         /* try to prevent loops */
106                         SCVAL(cli->inbuf,smb_com,0xFF);
107                         goto again;
108                 }
109         }
110
111         /* If the server is not responding, note that now */
112
113         if (!ret) {
114                 cli->smb_rw_error = smb_read_error;
115                 close(cli->fd);
116                 cli->fd = -1;
117         }
118
119         return ret;
120 }
121
122 /****************************************************************************
123  Send an smb to a fd.
124 ****************************************************************************/
125
126 BOOL cli_send_smb(struct cli_state *cli)
127 {
128         size_t len;
129         size_t nwritten=0;
130         ssize_t ret;
131
132         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
133         if (cli->fd == -1)
134                 return False;
135
136         cli_caclulate_sign_mac(cli);
137
138         len = smb_len(cli->outbuf) + 4;
139
140         while (nwritten < len) {
141                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
142                 if (ret <= 0) {
143                         close(cli->fd);
144                         cli->fd = -1;
145                         cli->smb_rw_error = WRITE_ERROR;
146                         DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
147                                 (int)len,(int)ret, strerror(errno) ));
148                         return False;
149                 }
150                 nwritten += ret;
151         }
152         return True;
153 }
154
155 /****************************************************************************
156  Setup basics in a outgoing packet.
157 ****************************************************************************/
158
159 void cli_setup_packet(struct cli_state *cli)
160 {
161         cli->rap_error = 0;
162         SSVAL(cli->outbuf,smb_pid,cli->pid);
163         SSVAL(cli->outbuf,smb_uid,cli->vuid);
164         SSVAL(cli->outbuf,smb_mid,cli->mid);
165         if (cli->protocol > PROTOCOL_CORE) {
166                 uint16 flags2;
167                 SCVAL(cli->outbuf,smb_flg,0x8);
168                 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
169                 if (cli->capabilities & CAP_UNICODE)
170                         flags2 |= FLAGS2_UNICODE_STRINGS;
171                 if (cli->capabilities & CAP_STATUS32)
172                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
173                 if (cli->use_spnego)
174                         flags2 |= FLAGS2_EXTENDED_SECURITY;
175                 if (cli->sign_info.use_smb_signing 
176                     || cli->sign_info.temp_smb_signing)
177                         flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
178                 SSVAL(cli->outbuf,smb_flg2, flags2);
179         }
180 }
181
182 /****************************************************************************
183  Setup the bcc length of the packet from a pointer to the end of the data.
184 ****************************************************************************/
185
186 void cli_setup_bcc(struct cli_state *cli, void *p)
187 {
188         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
189 }
190
191 /****************************************************************************
192  Initialise credentials of a client structure.
193 ****************************************************************************/
194
195 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
196 {
197         /* copy_nt_creds(&cli->usr, usr); */
198         safe_strcpy(cli->domain   , usr->domain   , sizeof(usr->domain   )-1);
199         safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
200         memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
201         cli->ntlmssp_flags = usr->ntlmssp_flags;
202         cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
203
204         DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
205                cli->user_name, cli->domain,
206                cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
207 }
208
209 /****************************************************************************
210  Initialise a client structure.
211 ****************************************************************************/
212
213 struct cli_state *cli_initialise(struct cli_state *cli)
214 {
215         BOOL alloced_cli = False;
216
217         /* Check the effective uid - make sure we are not setuid */
218         if (is_setuid_root()) {
219                 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
220                 return NULL;
221         }
222
223         if (!cli) {
224                 cli = (struct cli_state *)malloc(sizeof(*cli));
225                 if (!cli)
226                         return NULL;
227                 ZERO_STRUCTP(cli);
228                 alloced_cli = True;
229         }
230
231         if (cli->initialised)
232                 cli_close_connection(cli);
233
234         ZERO_STRUCTP(cli);
235
236         cli->port = 0;
237         cli->fd = -1;
238         cli->cnum = -1;
239         cli->pid = (uint16)sys_getpid();
240         cli->mid = 1;
241         cli->vuid = UID_FIELD_INVALID;
242         cli->protocol = PROTOCOL_NT1;
243         cli->timeout = 20000; /* Timeout is in milliseconds. */
244         cli->bufsize = CLI_BUFFER_SIZE+4;
245         cli->max_xmit = cli->bufsize;
246         cli->outbuf = (char *)malloc(cli->bufsize);
247         cli->inbuf = (char *)malloc(cli->bufsize);
248         cli->oplock_handler = cli_oplock_ack;
249         if (lp_use_spnego())
250                 cli->use_spnego = True;
251
252         /* Set the CLI_FORCE_DOSERR environment variable to test
253            client routines using DOS errors instead of STATUS32
254            ones.  This intended only as a temporary hack. */    
255         if (getenv("CLI_FORCE_DOSERR"))
256                 cli->force_dos_errors = True;
257
258         /* A way to attempt to force SMB signing */
259         if (getenv("CLI_FORCE_SMB_SIGNING"))
260                 cli->sign_info.negotiated_smb_signing = True;
261                                    
262         if (!cli->outbuf || !cli->inbuf)
263                 goto error;
264
265         if ((cli->mem_ctx = talloc_init("cli based talloc")) == NULL)
266                 goto error;
267
268         memset(cli->outbuf, 0, cli->bufsize);
269         memset(cli->inbuf, 0, cli->bufsize);
270
271         cli->nt_pipe_fnum = 0;
272
273         cli->initialised = 1;
274         cli->allocated = alloced_cli;
275
276         return cli;
277
278         /* Clean up after malloc() error */
279
280  error:
281
282         SAFE_FREE(cli->inbuf);
283         SAFE_FREE(cli->outbuf);
284
285         if (alloced_cli)
286                 SAFE_FREE(cli);
287
288         return NULL;
289 }
290
291 /****************************************************************************
292  Close a client connection and free the memory without destroying cli itself.
293 ****************************************************************************/
294
295 void cli_close_connection(struct cli_state *cli)
296 {
297         SAFE_FREE(cli->outbuf);
298         SAFE_FREE(cli->inbuf);
299
300         data_blob_free(&cli->secblob);
301
302         if (cli->mem_ctx) {
303                 talloc_destroy(cli->mem_ctx);
304                 cli->mem_ctx = NULL;
305         }
306
307         if (cli->fd != -1) 
308                 close(cli->fd);
309         cli->fd = -1;
310         cli->smb_rw_error = 0;
311 }
312
313 /****************************************************************************
314  Shutdown a client structure.
315 ****************************************************************************/
316
317 void cli_shutdown(struct cli_state *cli)
318 {
319         BOOL allocated = cli->allocated;
320         cli_close_connection(cli);
321         ZERO_STRUCTP(cli);
322         if (allocated)
323                 free(cli);
324 }
325
326 /****************************************************************************
327  Set socket options on a open connection.
328 ****************************************************************************/
329
330 void cli_sockopt(struct cli_state *cli, char *options)
331 {
332         set_socket_options(cli->fd, options);
333 }
334
335 /****************************************************************************
336  Set the PID to use for smb messages. Return the old pid.
337 ****************************************************************************/
338
339 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
340 {
341         uint16 ret = cli->pid;
342         cli->pid = pid;
343         return ret;
344 }
345
346 /****************************************************************************
347 Send a keepalive packet to the server
348 ****************************************************************************/
349 BOOL cli_send_keepalive(struct cli_state *cli)
350 {
351         if (cli->fd == -1) {
352                 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
353                 return False;
354         }
355         if (!send_keepalive(cli->fd)) {
356                 close(cli->fd);
357                 cli->fd = -1;
358                 DEBUG(0,("Error sending keepalive packet to client.\n"));
359                 return False;
360         }
361         return True;
362 }