Updates!
[samba.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 port number used to call on 
27  */
28 int cli_set_port(struct cli_state *cli, int port)
29 {
30         cli->port = port;
31         return port;
32 }
33
34 /****************************************************************************
35   read an smb from a fd ignoring all keepalive packets. Note that the buffer 
36   *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
37   The timeout is in milliseconds
38
39   This is exactly the same as receive_smb except that it never returns
40   a session keepalive packet (just as receive_smb used to do).
41   receive_smb was changed to return keepalives as the oplock processing means this call
42   should never go into a blocking read.
43 ****************************************************************************/
44
45 static BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
46 {
47   BOOL ret;
48
49   for(;;)
50   {
51     ret = receive_smb(fd, buffer, timeout);
52
53     if (!ret)
54     {
55       DEBUG(10,("client_receive_smb failed\n"));
56       show_msg(buffer);
57       return ret;
58     }
59
60     /* Ignore session keepalive packets. */
61     if(CVAL(buffer,0) != SMBkeepalive)
62       break;
63   }
64   show_msg(buffer);
65   return ret;
66 }
67
68
69 /****************************************************************************
70 recv an smb
71 ****************************************************************************/
72 BOOL cli_receive_smb(struct cli_state *cli)
73 {
74         BOOL ret;
75
76         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
77         if (cli->fd == -1) return False; 
78
79  again:
80         ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
81         
82         if (ret) {
83                 /* it might be an oplock break request */
84                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
85                     CVAL(cli->inbuf,smb_com) == SMBlockingX &&
86                     SVAL(cli->inbuf,smb_vwv6) == 0 &&
87                     SVAL(cli->inbuf,smb_vwv7) == 0) {
88                         if (cli->oplock_handler) {
89                                 int fnum = SVAL(cli->inbuf,smb_vwv2);
90                                 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
91                                 if (!cli->oplock_handler(cli, fnum, level)) return False;
92                         }
93                         /* try to prevent loops */
94                         SCVAL(cli->inbuf,smb_com,0xFF);
95                         goto again;
96                 }
97         }
98
99         /* If the server is not responding, note that now */
100
101         if (!ret) {
102                 close(cli->fd);
103                 cli->fd = -1;
104         }
105
106         return ret;
107 }
108
109 /****************************************************************************
110  Send an smb to a fd.
111 ****************************************************************************/
112
113 BOOL cli_send_smb(struct cli_state *cli)
114 {
115         size_t len;
116         size_t nwritten=0;
117         ssize_t ret;
118
119         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
120         if (cli->fd == -1)
121                 return False;
122
123         cli_caclulate_sign_mac(cli);
124
125         len = smb_len(cli->outbuf) + 4;
126
127         while (nwritten < len) {
128                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
129                 if (ret <= 0) {
130                         close(cli->fd);
131                         cli->fd = -1;
132                         DEBUG(0,("Error writing %d bytes to client. %d\n", (int)len,(int)ret));
133                         return False;
134                 }
135                 nwritten += ret;
136         }
137         return True;
138 }
139
140 /****************************************************************************
141  Setup basics in a outgoing packet.
142 ****************************************************************************/
143
144 void cli_setup_packet(struct cli_state *cli)
145 {
146         cli->rap_error = 0;
147         SSVAL(cli->outbuf,smb_pid,cli->pid);
148         SSVAL(cli->outbuf,smb_uid,cli->vuid);
149         SSVAL(cli->outbuf,smb_mid,cli->mid);
150         if (cli->protocol > PROTOCOL_CORE) {
151                 uint16 flags2;
152                 SCVAL(cli->outbuf,smb_flg,0x8);
153                 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
154                 if (cli->capabilities & CAP_UNICODE) {
155                         flags2 |= FLAGS2_UNICODE_STRINGS;
156                 }
157                 if (cli->capabilities & CAP_STATUS32) {
158                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
159                 }
160                 if (cli->use_spnego) {
161                         flags2 |= FLAGS2_EXTENDED_SECURITY;
162                 }
163                 if (cli->sign_info.use_smb_signing 
164                     || cli->sign_info.temp_smb_signing)
165                         flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
166                 SSVAL(cli->outbuf,smb_flg2, flags2);
167         }
168 }
169
170 /****************************************************************************
171 setup the bcc length of the packet from a pointer to the end of the data
172 ****************************************************************************/
173 void cli_setup_bcc(struct cli_state *cli, void *p)
174 {
175         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
176 }
177
178
179
180 /****************************************************************************
181 initialise credentials of a client structure
182 ****************************************************************************/
183 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
184 {
185         /* copy_nt_creds(&cli->usr, usr); */
186         safe_strcpy(cli->domain   , usr->domain   , sizeof(usr->domain   )-1);
187         safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
188         memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
189         cli->ntlmssp_flags = usr->ntlmssp_flags;
190         cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
191
192         DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
193                cli->user_name, cli->domain,
194                cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
195 }
196
197
198 /****************************************************************************
199 initialise a client structure
200 ****************************************************************************/
201 struct cli_state *cli_initialise(struct cli_state *cli)
202 {
203         BOOL alloced_cli = False;
204
205         /* Check the effective uid - make sure we are not setuid */
206         if (is_setuid_root()) {
207                 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
208                 return NULL;
209         }
210
211         if (!cli) {
212                 cli = (struct cli_state *)malloc(sizeof(*cli));
213                 if (!cli)
214                         return NULL;
215                 ZERO_STRUCTP(cli);
216                 alloced_cli = True;
217         }
218
219         if (cli->initialised) {
220                 cli_shutdown(cli);
221         }
222
223         ZERO_STRUCTP(cli);
224
225         cli->port = 0;
226         cli->fd = -1;
227         cli->cnum = -1;
228         cli->pid = (uint16)sys_getpid();
229         cli->mid = 1;
230         cli->vuid = UID_FIELD_INVALID;
231         cli->protocol = PROTOCOL_NT1;
232         cli->timeout = 20000; /* Timeout is in milliseconds. */
233         cli->bufsize = CLI_BUFFER_SIZE+4;
234         cli->max_xmit = cli->bufsize;
235         cli->outbuf = (char *)malloc(cli->bufsize);
236         cli->inbuf = (char *)malloc(cli->bufsize);
237         cli->oplock_handler = cli_oplock_ack;
238         if (lp_use_spnego()) {
239                 cli->use_spnego = True;
240         }
241
242         /* Set the CLI_FORCE_DOSERR environment variable to test
243            client routines using DOS errors instead of STATUS32
244            ones.  This intended only as a temporary hack. */    
245         if (getenv("CLI_FORCE_DOSERR")) {
246                 cli->force_dos_errors = True;
247         }
248
249         /* A way to attempt to force SMB signing */
250         if (getenv("CLI_FORCE_SMB_SIGNING"))
251                 cli->sign_info.negotiated_smb_signing = True;
252                                    
253         if (!cli->outbuf || !cli->inbuf)
254                 goto error;
255
256         if ((cli->mem_ctx = talloc_init_named("cli based talloc")) == NULL)
257                 goto error;
258
259         memset(cli->outbuf, 0, cli->bufsize);
260         memset(cli->inbuf, 0, cli->bufsize);
261
262         cli->nt_pipe_fnum = 0;
263
264         cli->initialised = 1;
265         cli->allocated = alloced_cli;
266
267         return cli;
268
269         /* Clean up after malloc() error */
270
271  error:
272
273         SAFE_FREE(cli->inbuf);
274         SAFE_FREE(cli->outbuf);
275
276         if (alloced_cli)
277                 SAFE_FREE(cli);
278
279         return NULL;
280 }
281
282 /****************************************************************************
283 shutdown a client structure
284 ****************************************************************************/
285 void cli_shutdown(struct cli_state *cli)
286 {
287         BOOL allocated;
288         SAFE_FREE(cli->outbuf);
289         SAFE_FREE(cli->inbuf);
290
291         data_blob_free(&cli->secblob);
292
293         if (cli->mem_ctx)
294                 talloc_destroy(cli->mem_ctx);
295
296         if (cli->fd != -1) 
297                 close(cli->fd);
298         allocated = cli->allocated;
299         ZERO_STRUCTP(cli);
300         if (allocated) {
301                 free(cli);
302         } 
303 }
304
305
306 /****************************************************************************
307 set socket options on a open connection
308 ****************************************************************************/
309 void cli_sockopt(struct cli_state *cli, char *options)
310 {
311         set_socket_options(cli->fd, options);
312 }
313
314 /****************************************************************************
315 set the PID to use for smb messages. Return the old pid.
316 ****************************************************************************/
317 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
318 {
319         uint16 ret = cli->pid;
320         cli->pid = pid;
321         return ret;
322 }