Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[nivanova/samba-autobuild/.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  * Change the port number used to call on 
28  */
29 int cli_set_port(struct cli_state *cli, int port)
30 {
31         cli->port = port;
32         return port;
33 }
34
35 /****************************************************************************
36 recv an smb
37 ****************************************************************************/
38 BOOL cli_receive_smb(struct cli_state *cli)
39 {
40         BOOL ret;
41  again:
42         ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
43         
44         if (ret) {
45                 /* it might be an oplock break request */
46                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
47                     CVAL(cli->inbuf,smb_com) == SMBlockingX &&
48                     SVAL(cli->inbuf,smb_vwv6) == 0 &&
49                     SVAL(cli->inbuf,smb_vwv7) == 0) {
50                         if (cli->oplock_handler) {
51                                 int fnum = SVAL(cli->inbuf,smb_vwv2);
52                                 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
53                                 if (!cli->oplock_handler(cli, fnum, level)) return False;
54                         }
55                         /* try to prevent loops */
56                         CVAL(cli->inbuf,smb_com) = 0xFF;
57                         goto again;
58                 }
59         }
60
61         return ret;
62 }
63
64 /****************************************************************************
65   send an smb to a fd and re-establish if necessary
66 ****************************************************************************/
67 BOOL cli_send_smb(struct cli_state *cli)
68 {
69         size_t len;
70         size_t nwritten=0;
71         ssize_t ret;
72         BOOL reestablished=False;
73
74         len = smb_len(cli->outbuf) + 4;
75
76         while (nwritten < len) {
77                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
78                 if (ret <= 0 && errno == EPIPE && !reestablished) {
79                         if (cli_reestablish_connection(cli)) {
80                                 reestablished = True;
81                                 nwritten=0;
82                                 continue;
83                         }
84                 }
85                 if (ret <= 0) {
86                         DEBUG(0,("Error writing %d bytes to client. %d\n",
87                                  (int)len,(int)ret));
88                         return False;
89                 }
90                 nwritten += ret;
91         }
92         
93         return True;
94 }
95
96 /****************************************************************************
97 setup basics in a outgoing packet
98 ****************************************************************************/
99 void cli_setup_packet(struct cli_state *cli)
100 {
101         cli->rap_error = 0;
102         SSVAL(cli->outbuf,smb_pid,cli->pid);
103         SSVAL(cli->outbuf,smb_uid,cli->vuid);
104         SSVAL(cli->outbuf,smb_mid,cli->mid);
105         if (cli->protocol > PROTOCOL_CORE) {
106                 uint16 flags2;
107                 SCVAL(cli->outbuf,smb_flg,0x8);
108                 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
109                 if (cli->capabilities & CAP_UNICODE) {
110                         flags2 |= FLAGS2_UNICODE_STRINGS;
111                 }
112                 if (cli->capabilities & CAP_STATUS32) {
113                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
114                 }
115                 SSVAL(cli->outbuf,smb_flg2, flags2);
116         }
117 }
118
119 /****************************************************************************
120 setup the bcc length of the packet from a pointer to the end of the data
121 ****************************************************************************/
122 void cli_setup_bcc(struct cli_state *cli, void *p)
123 {
124         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
125 }
126
127
128
129 /****************************************************************************
130 initialise a client structure
131 ****************************************************************************/
132 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
133 {
134         /* copy_nt_creds(&cli->usr, usr); */
135         safe_strcpy(cli->domain   , usr->domain   , sizeof(usr->domain   )-1);
136         safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
137         memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
138         cli->ntlmssp_flags = usr->ntlmssp_flags;
139         cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
140
141         DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
142                cli->user_name, cli->domain,
143                cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
144 }
145
146
147 /****************************************************************************
148 initialise a client structure
149 ****************************************************************************/
150 struct cli_state *cli_initialise(struct cli_state *cli)
151 {
152         BOOL alloced_cli = False;
153
154         if (!cli) {
155                 cli = (struct cli_state *)malloc(sizeof(*cli));
156                 if (!cli)
157                         return NULL;
158                 ZERO_STRUCTP(cli);
159                 alloced_cli = True;
160         }
161
162         if (cli->initialised) {
163                 cli_shutdown(cli);
164         }
165
166         ZERO_STRUCTP(cli);
167
168         cli->port = 0;
169         cli->fd = -1;
170         cli->cnum = -1;
171         cli->pid = (uint16)sys_getpid();
172         cli->mid = 1;
173         cli->vuid = UID_FIELD_INVALID;
174         cli->protocol = PROTOCOL_NT1;
175         cli->timeout = 20000; /* Timeout is in milliseconds. */
176         cli->bufsize = CLI_BUFFER_SIZE+4;
177         cli->max_xmit = cli->bufsize;
178         cli->outbuf = (char *)malloc(cli->bufsize);
179         cli->inbuf = (char *)malloc(cli->bufsize);
180         cli->oplock_handler = cli_oplock_ack;
181
182         if (!cli->outbuf || !cli->inbuf)
183                 goto error;
184
185         if ((cli->mem_ctx = talloc_init()) == NULL)
186                 goto error;
187
188         memset(cli->outbuf, 0, cli->bufsize);
189         memset(cli->inbuf, 0, cli->bufsize);
190
191         cli->nt_pipe_fnum = 0;
192
193         cli->initialised = 1;
194
195         return cli;
196
197         /* Clean up after malloc() error */
198
199  error:
200
201         SAFE_FREE(cli->inbuf);
202         SAFE_FREE(cli->outbuf);
203
204         if (alloced_cli)
205                 SAFE_FREE(cli);
206
207         return NULL;
208 }
209
210 /****************************************************************************
211 shutdown a client structure
212 ****************************************************************************/
213 void cli_shutdown(struct cli_state *cli)
214 {
215         SAFE_FREE(cli->outbuf);
216         SAFE_FREE(cli->inbuf);
217
218         if (cli->mem_ctx)
219                 talloc_destroy(cli->mem_ctx);
220
221 #ifdef WITH_SSL
222     if (cli->fd != -1)
223       sslutil_disconnect(cli->fd);
224 #endif /* WITH_SSL */
225         if (cli->fd != -1) 
226       close(cli->fd);
227         memset(cli, 0, sizeof(*cli));
228 }
229
230
231 /****************************************************************************
232 set socket options on a open connection
233 ****************************************************************************/
234 void cli_sockopt(struct cli_state *cli, char *options)
235 {
236         set_socket_options(cli->fd, options);
237 }
238
239 /****************************************************************************
240 set the PID to use for smb messages. Return the old pid.
241 ****************************************************************************/
242 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
243 {
244         uint16 ret = cli->pid;
245         cli->pid = pid;
246         return ret;
247 }