move to SAFE_FREE()
[samba.git] / source3 / libsmb / clientgen.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client generic functions
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26
27 extern int DEBUGLEVEL;
28
29 /*
30  * Change the port number used to call on 
31  */
32 int cli_set_port(struct cli_state *cli, int port)
33 {
34         cli->port = port;
35         return port;
36 }
37
38 /****************************************************************************
39 recv an smb
40 ****************************************************************************/
41 BOOL cli_receive_smb(struct cli_state *cli)
42 {
43         BOOL ret;
44  again:
45         ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
46         
47         if (ret) {
48                 /* it might be an oplock break request */
49                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
50                     CVAL(cli->inbuf,smb_com) == SMBlockingX &&
51                     SVAL(cli->inbuf,smb_vwv6) == 0 &&
52                     SVAL(cli->inbuf,smb_vwv7) == 0) {
53                         if (cli->oplock_handler) {
54                                 int fnum = SVAL(cli->inbuf,smb_vwv2);
55                                 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
56                                 if (!cli->oplock_handler(cli, fnum, level)) return False;
57                         }
58                         /* try to prevent loops */
59                         CVAL(cli->inbuf,smb_com) = 0xFF;
60                         goto again;
61                 }
62         }
63
64         return ret;
65 }
66
67 /****************************************************************************
68   send an smb to a fd and re-establish if necessary
69 ****************************************************************************/
70 BOOL cli_send_smb(struct cli_state *cli)
71 {
72         size_t len;
73         size_t nwritten=0;
74         ssize_t ret;
75         BOOL reestablished=False;
76
77         len = smb_len(cli->outbuf) + 4;
78
79         while (nwritten < len) {
80                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
81                 if (ret <= 0 && errno == EPIPE && !reestablished) {
82                         if (cli_reestablish_connection(cli)) {
83                                 reestablished = True;
84                                 nwritten=0;
85                                 continue;
86                         }
87                 }
88                 if (ret <= 0) {
89                         DEBUG(0,("Error writing %d bytes to client. %d\n",
90                                  (int)len,(int)ret));
91                         return False;
92                 }
93                 nwritten += ret;
94         }
95         
96         return True;
97 }
98
99 /****************************************************************************
100 setup basics in a outgoing packet
101 ****************************************************************************/
102 void cli_setup_packet(struct cli_state *cli)
103 {
104         cli->rap_error = 0;
105         SSVAL(cli->outbuf,smb_pid,cli->pid);
106         SSVAL(cli->outbuf,smb_uid,cli->vuid);
107         SSVAL(cli->outbuf,smb_mid,cli->mid);
108         if (cli->protocol > PROTOCOL_CORE) {
109                 uint16 flags2;
110                 SCVAL(cli->outbuf,smb_flg,0x8);
111                 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
112                 if (cli->capabilities & CAP_UNICODE) {
113                         flags2 |= FLAGS2_UNICODE_STRINGS;
114                 }
115                 if (cli->capabilities & CAP_STATUS32) {
116                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
117                 }
118                 SSVAL(cli->outbuf,smb_flg2, flags2);
119         }
120 }
121
122 /****************************************************************************
123 setup the bcc length of the packet from a pointer to the end of the data
124 ****************************************************************************/
125 void cli_setup_bcc(struct cli_state *cli, void *p)
126 {
127         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
128 }
129
130
131
132 /****************************************************************************
133 initialise a client structure
134 ****************************************************************************/
135 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
136 {
137         /* copy_nt_creds(&cli->usr, usr); */
138         safe_strcpy(cli->domain   , usr->domain   , sizeof(usr->domain   )-1);
139         safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
140         memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
141         cli->ntlmssp_flags = usr->ntlmssp_flags;
142         cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
143
144         DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
145                cli->user_name, cli->domain,
146                cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
147 }
148
149
150 /****************************************************************************
151 initialise a client structure
152 ****************************************************************************/
153 struct cli_state *cli_initialise(struct cli_state *cli)
154 {
155         BOOL alloced_cli = False;
156
157         if (!cli) {
158                 cli = (struct cli_state *)malloc(sizeof(*cli));
159                 if (!cli)
160                         return NULL;
161                 ZERO_STRUCTP(cli);
162                 alloced_cli = True;
163         }
164
165         if (cli->initialised) {
166                 cli_shutdown(cli);
167         }
168
169         ZERO_STRUCTP(cli);
170
171         cli->port = 0;
172         cli->fd = -1;
173         cli->cnum = -1;
174         cli->pid = (uint16)sys_getpid();
175         cli->mid = 1;
176         cli->vuid = UID_FIELD_INVALID;
177         cli->protocol = PROTOCOL_NT1;
178         cli->timeout = 20000; /* Timeout is in milliseconds. */
179         cli->bufsize = CLI_BUFFER_SIZE+4;
180         cli->max_xmit = cli->bufsize;
181         cli->outbuf = (char *)malloc(cli->bufsize);
182         cli->inbuf = (char *)malloc(cli->bufsize);
183         cli->oplock_handler = cli_oplock_ack;
184
185         if (!cli->outbuf || !cli->inbuf)
186                 goto error;
187
188         if ((cli->mem_ctx = talloc_init()) == NULL)
189                 goto error;
190
191         memset(cli->outbuf, 0, cli->bufsize);
192         memset(cli->inbuf, 0, cli->bufsize);
193
194         cli->nt_pipe_fnum = 0;
195
196         cli->initialised = 1;
197
198         return cli;
199
200         /* Clean up after malloc() error */
201
202  error:
203
204         SAFE_FREE(cli->inbuf);
205         SAFE_FREE(cli->outbuf);
206
207         if (alloced_cli)
208                 SAFE_FREE(cli);
209
210         return NULL;
211 }
212
213 /****************************************************************************
214 shutdown a client structure
215 ****************************************************************************/
216 void cli_shutdown(struct cli_state *cli)
217 {
218         SAFE_FREE(cli->outbuf);
219         SAFE_FREE(cli->inbuf);
220
221         if (cli->mem_ctx)
222                 talloc_destroy(cli->mem_ctx);
223
224 #ifdef WITH_SSL
225     if (cli->fd != -1)
226       sslutil_disconnect(cli->fd);
227 #endif /* WITH_SSL */
228         if (cli->fd != -1) 
229       close(cli->fd);
230         memset(cli, 0, sizeof(*cli));
231 }
232
233
234 /****************************************************************************
235 set socket options on a open connection
236 ****************************************************************************/
237 void cli_sockopt(struct cli_state *cli, char *options)
238 {
239         set_socket_options(cli->fd, options);
240 }
241
242 /****************************************************************************
243 set the PID to use for smb messages. Return the old pid.
244 ****************************************************************************/
245 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
246 {
247         uint16 ret = cli->pid;
248         cli->pid = pid;
249         return ret;
250 }
251