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