updated the 3.0 branch from the head branch - ready for alpha18
[ira/wip.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                         flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
165                 SSVAL(cli->outbuf,smb_flg2, flags2);
166         }
167 }
168
169 /****************************************************************************
170 setup the bcc length of the packet from a pointer to the end of the data
171 ****************************************************************************/
172 void cli_setup_bcc(struct cli_state *cli, void *p)
173 {
174         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
175 }
176
177
178
179 /****************************************************************************
180 initialise credentials of a client structure
181 ****************************************************************************/
182 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
183 {
184         /* copy_nt_creds(&cli->usr, usr); */
185         safe_strcpy(cli->domain   , usr->domain   , sizeof(usr->domain   )-1);
186         safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
187         memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
188         cli->ntlmssp_flags = usr->ntlmssp_flags;
189         cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
190
191         DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
192                cli->user_name, cli->domain,
193                cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
194 }
195
196
197 /****************************************************************************
198 initialise a client structure
199 ****************************************************************************/
200 struct cli_state *cli_initialise(struct cli_state *cli)
201 {
202         BOOL alloced_cli = False;
203
204         /* Check the effective uid - make sure we are not setuid */
205         if (is_setuid_root()) {
206                 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
207                 return NULL;
208         }
209
210         if (!cli) {
211                 cli = (struct cli_state *)malloc(sizeof(*cli));
212                 if (!cli)
213                         return NULL;
214                 ZERO_STRUCTP(cli);
215                 alloced_cli = True;
216         }
217
218         if (cli->initialised) {
219                 cli_shutdown(cli);
220         }
221
222         ZERO_STRUCTP(cli);
223
224         cli->port = 0;
225         cli->fd = -1;
226         cli->cnum = -1;
227         cli->pid = (uint16)sys_getpid();
228         cli->mid = 1;
229         cli->vuid = UID_FIELD_INVALID;
230         cli->protocol = PROTOCOL_NT1;
231         cli->timeout = 20000; /* Timeout is in milliseconds. */
232         cli->bufsize = CLI_BUFFER_SIZE+4;
233         cli->max_xmit = cli->bufsize;
234         cli->outbuf = (char *)malloc(cli->bufsize);
235         cli->inbuf = (char *)malloc(cli->bufsize);
236         cli->oplock_handler = cli_oplock_ack;
237         cli->use_spnego = True;
238
239         /* Set the CLI_FORCE_DOSERR environment variable to test
240            client routines using DOS errors instead of STATUS32
241            ones.  This intended only as a temporary hack. */    
242         if (getenv("CLI_FORCE_DOSERR")) {
243                 cli->force_dos_errors = True;
244         }
245
246         if (!cli->outbuf || !cli->inbuf)
247                 goto error;
248
249         if ((cli->mem_ctx = talloc_init_named("cli based talloc")) == NULL)
250                 goto error;
251
252         memset(cli->outbuf, 0, cli->bufsize);
253         memset(cli->inbuf, 0, cli->bufsize);
254
255         cli->nt_pipe_fnum = 0;
256
257         cli->initialised = 1;
258         cli->allocated = alloced_cli;
259
260         return cli;
261
262         /* Clean up after malloc() error */
263
264  error:
265
266         SAFE_FREE(cli->inbuf);
267         SAFE_FREE(cli->outbuf);
268
269         if (alloced_cli)
270                 SAFE_FREE(cli);
271
272         return NULL;
273 }
274
275 /****************************************************************************
276 shutdown a client structure
277 ****************************************************************************/
278 void cli_shutdown(struct cli_state *cli)
279 {
280         BOOL allocated;
281         SAFE_FREE(cli->outbuf);
282         SAFE_FREE(cli->inbuf);
283
284         data_blob_free(&cli->secblob);
285
286         if (cli->mem_ctx)
287                 talloc_destroy(cli->mem_ctx);
288
289         if (cli->fd != -1) 
290                 close(cli->fd);
291         allocated = cli->allocated;
292         ZERO_STRUCTP(cli);
293         if (allocated) {
294                 free(cli);
295         } 
296 }
297
298
299 /****************************************************************************
300 set socket options on a open connection
301 ****************************************************************************/
302 void cli_sockopt(struct cli_state *cli, char *options)
303 {
304         set_socket_options(cli->fd, options);
305 }
306
307 /****************************************************************************
308 set the PID to use for smb messages. Return the old pid.
309 ****************************************************************************/
310 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
311 {
312         uint16 ret = cli->pid;
313         cli->pid = pid;
314         return ret;
315 }