677a0f4220902fe8e059329bc82c6a6178ba10c3
[kai/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 recv an smb
36 ****************************************************************************/
37 BOOL cli_receive_smb(struct cli_state *cli)
38 {
39         BOOL ret;
40
41         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
42         if (cli->fd == -1) return False; 
43
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                         SCVAL(cli->inbuf,smb_com,0xFF);
60                         goto again;
61                 }
62         }
63
64         /* If the server is not responding, note that now */
65
66         if (!ret) {
67                 close(cli->fd);
68                 cli->fd = -1;
69         }
70
71         return ret;
72 }
73
74 /****************************************************************************
75   send an smb to a fd.
76 ****************************************************************************/
77
78 BOOL cli_send_smb(struct cli_state *cli)
79 {
80         size_t len;
81         size_t nwritten=0;
82         ssize_t ret;
83
84         /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
85         if (cli->fd == -1) return False;
86
87         len = smb_len(cli->outbuf) + 4;
88
89         while (nwritten < len) {
90                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
91                 if (ret <= 0) {
92                         close(cli->fd);
93                         cli->fd = -1;
94                         DEBUG(0,("Error writing %d bytes to client. %d\n",
95                                  (int)len,(int)ret));
96                         return False;
97                 }
98                 nwritten += ret;
99         }
100         
101         return True;
102 }
103
104 /****************************************************************************
105 setup basics in a outgoing packet
106 ****************************************************************************/
107 void cli_setup_packet(struct cli_state *cli)
108 {
109         cli->rap_error = 0;
110         SSVAL(cli->outbuf,smb_pid,cli->pid);
111         SSVAL(cli->outbuf,smb_uid,cli->vuid);
112         SSVAL(cli->outbuf,smb_mid,cli->mid);
113         if (cli->protocol > PROTOCOL_CORE) {
114                 uint16 flags2;
115                 SCVAL(cli->outbuf,smb_flg,0x8);
116                 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
117                 if (cli->capabilities & CAP_UNICODE) {
118                         flags2 |= FLAGS2_UNICODE_STRINGS;
119                 }
120                 if (cli->capabilities & CAP_STATUS32) {
121                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
122                 }
123                 if (cli->use_spnego) {
124                         flags2 |= FLAGS2_EXTENDED_SECURITY;
125                 }
126                 SSVAL(cli->outbuf,smb_flg2, flags2);
127         }
128 }
129
130 /****************************************************************************
131 setup the bcc length of the packet from a pointer to the end of the data
132 ****************************************************************************/
133 void cli_setup_bcc(struct cli_state *cli, void *p)
134 {
135         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
136 }
137
138
139
140 /****************************************************************************
141 initialise credentials of a client structure
142 ****************************************************************************/
143 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
144 {
145         /* copy_nt_creds(&cli->usr, usr); */
146         safe_strcpy(cli->domain   , usr->domain   , sizeof(usr->domain   )-1);
147         safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
148         memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
149         cli->ntlmssp_flags = usr->ntlmssp_flags;
150         cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
151
152         DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
153                cli->user_name, cli->domain,
154                cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
155 }
156
157
158 /****************************************************************************
159 initialise a client structure
160 ****************************************************************************/
161 struct cli_state *cli_initialise(struct cli_state *cli)
162 {
163         BOOL alloced_cli = False;
164
165         /* Check the effective uid - make sure we are not setuid */
166         if (is_setuid_root()) {
167                 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
168                 return NULL;
169         }
170
171         if (!cli) {
172                 cli = (struct cli_state *)malloc(sizeof(*cli));
173                 if (!cli)
174                         return NULL;
175                 ZERO_STRUCTP(cli);
176                 alloced_cli = True;
177         }
178
179         if (cli->initialised) {
180                 cli_shutdown(cli);
181         }
182
183         ZERO_STRUCTP(cli);
184
185         cli->port = 0;
186         cli->fd = -1;
187         cli->cnum = -1;
188         cli->pid = (uint16)sys_getpid();
189         cli->mid = 1;
190         cli->vuid = UID_FIELD_INVALID;
191         cli->protocol = PROTOCOL_NT1;
192         cli->timeout = 20000; /* Timeout is in milliseconds. */
193         cli->bufsize = CLI_BUFFER_SIZE+4;
194         cli->max_xmit = cli->bufsize;
195         cli->outbuf = (char *)malloc(cli->bufsize);
196         cli->inbuf = (char *)malloc(cli->bufsize);
197         cli->oplock_handler = cli_oplock_ack;
198         cli->use_spnego = True;
199
200         /* Set the CLI_FORCE_DOSERR environment variable to test
201            client routines using DOS errors instead of STATUS32
202            ones.  This intended only as a temporary hack. */    
203         if (getenv("CLI_FORCE_DOSERR")) {
204                 cli->force_dos_errors = True;
205         }
206
207         if (!cli->outbuf || !cli->inbuf)
208                 goto error;
209
210         if ((cli->mem_ctx = talloc_init_named("cli based talloc")) == NULL)
211                 goto error;
212
213         memset(cli->outbuf, 0, cli->bufsize);
214         memset(cli->inbuf, 0, cli->bufsize);
215
216         cli->nt_pipe_fnum = 0;
217
218         cli->initialised = 1;
219         cli->allocated = alloced_cli;
220
221         return cli;
222
223         /* Clean up after malloc() error */
224
225  error:
226
227         SAFE_FREE(cli->inbuf);
228         SAFE_FREE(cli->outbuf);
229
230         if (alloced_cli)
231                 SAFE_FREE(cli);
232
233         return NULL;
234 }
235
236 /****************************************************************************
237 shutdown a client structure
238 ****************************************************************************/
239 void cli_shutdown(struct cli_state *cli)
240 {
241         BOOL allocated;
242         SAFE_FREE(cli->outbuf);
243         SAFE_FREE(cli->inbuf);
244
245         data_blob_free(&cli->secblob);
246
247         if (cli->mem_ctx)
248                 talloc_destroy(cli->mem_ctx);
249
250         if (cli->fd != -1) 
251                 close(cli->fd);
252         allocated = cli->allocated;
253         ZERO_STRUCTP(cli);
254         if (allocated) {
255                 free(cli);
256         } 
257 }
258
259
260 /****************************************************************************
261 set socket options on a open connection
262 ****************************************************************************/
263 void cli_sockopt(struct cli_state *cli, char *options)
264 {
265         set_socket_options(cli->fd, options);
266 }
267
268 /****************************************************************************
269 set the PID to use for smb messages. Return the old pid.
270 ****************************************************************************/
271 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
272 {
273         uint16 ret = cli->pid;
274         cli->pid = pid;
275         return ret;
276 }