Merged Volkers (correct) fix from 2.2 for crash on unable to connect.
[Samba/gebeck_regimport.git] / source3 / libsmb / clientgen.c
blob6b6a2acd3b8a42eaf3697f4fd3ff7892b49d6248
1 /*
2 Unix SMB/CIFS implementation.
3 SMB client generic functions
4 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
21 #define NO_SYSLOG
23 #include "includes.h"
26 * Change the port number used to call on
28 int cli_set_port(struct cli_state *cli, int port)
30 cli->port = port;
31 return port;
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
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 ****************************************************************************/
45 static BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
47 BOOL ret;
49 for(;;) {
50 ret = receive_smb(fd, buffer, timeout);
52 if (!ret) {
53 DEBUG(10,("client_receive_smb failed\n"));
54 show_msg(buffer);
55 return ret;
58 /* Ignore session keepalive packets. */
59 if(CVAL(buffer,0) != SMBkeepalive)
60 break;
62 show_msg(buffer);
63 return ret;
66 /****************************************************************************
67 Recv an smb.
68 ****************************************************************************/
70 BOOL cli_receive_smb(struct cli_state *cli)
72 BOOL ret;
74 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
75 if (cli->fd == -1)
76 return False;
78 again:
79 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
81 if (ret) {
82 /* it might be an oplock break request */
83 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
84 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
85 SVAL(cli->inbuf,smb_vwv6) == 0 &&
86 SVAL(cli->inbuf,smb_vwv7) == 0) {
87 if (cli->oplock_handler) {
88 int fnum = SVAL(cli->inbuf,smb_vwv2);
89 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
90 if (!cli->oplock_handler(cli, fnum, level)) return False;
92 /* try to prevent loops */
93 SCVAL(cli->inbuf,smb_com,0xFF);
94 goto again;
98 /* If the server is not responding, note that now */
100 if (!ret) {
101 close(cli->fd);
102 cli->fd = -1;
105 return ret;
108 /****************************************************************************
109 Send an smb to a fd.
110 ****************************************************************************/
112 BOOL cli_send_smb(struct cli_state *cli)
114 size_t len;
115 size_t nwritten=0;
116 ssize_t ret;
118 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
119 if (cli->fd == -1)
120 return False;
122 cli_caclulate_sign_mac(cli);
124 len = smb_len(cli->outbuf) + 4;
126 while (nwritten < len) {
127 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
128 if (ret <= 0) {
129 close(cli->fd);
130 cli->fd = -1;
131 DEBUG(0,("Error writing %d bytes to client. %d\n", (int)len,(int)ret));
132 return False;
134 nwritten += ret;
136 return True;
139 /****************************************************************************
140 Setup basics in a outgoing packet.
141 ****************************************************************************/
143 void cli_setup_packet(struct cli_state *cli)
145 cli->rap_error = 0;
146 SSVAL(cli->outbuf,smb_pid,cli->pid);
147 SSVAL(cli->outbuf,smb_uid,cli->vuid);
148 SSVAL(cli->outbuf,smb_mid,cli->mid);
149 if (cli->protocol > PROTOCOL_CORE) {
150 uint16 flags2;
151 SCVAL(cli->outbuf,smb_flg,0x8);
152 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
153 if (cli->capabilities & CAP_UNICODE)
154 flags2 |= FLAGS2_UNICODE_STRINGS;
155 if (cli->capabilities & CAP_STATUS32)
156 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
157 if (cli->use_spnego)
158 flags2 |= FLAGS2_EXTENDED_SECURITY;
159 if (cli->sign_info.use_smb_signing
160 || cli->sign_info.temp_smb_signing)
161 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
162 SSVAL(cli->outbuf,smb_flg2, flags2);
166 /****************************************************************************
167 Setup the bcc length of the packet from a pointer to the end of the data.
168 ****************************************************************************/
170 void cli_setup_bcc(struct cli_state *cli, void *p)
172 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
175 /****************************************************************************
176 Initialise credentials of a client structure.
177 ****************************************************************************/
179 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
181 /* copy_nt_creds(&cli->usr, usr); */
182 safe_strcpy(cli->domain , usr->domain , sizeof(usr->domain )-1);
183 safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
184 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
185 cli->ntlmssp_flags = usr->ntlmssp_flags;
186 cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
188 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
189 cli->user_name, cli->domain,
190 cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
193 /****************************************************************************
194 Initialise a client structure.
195 ****************************************************************************/
197 struct cli_state *cli_initialise(struct cli_state *cli)
199 BOOL alloced_cli = False;
201 /* Check the effective uid - make sure we are not setuid */
202 if (is_setuid_root()) {
203 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
204 return NULL;
207 if (!cli) {
208 cli = (struct cli_state *)malloc(sizeof(*cli));
209 if (!cli)
210 return NULL;
211 ZERO_STRUCTP(cli);
212 alloced_cli = True;
215 if (cli->initialised)
216 cli_close_connection(cli);
218 ZERO_STRUCTP(cli);
220 cli->port = 0;
221 cli->fd = -1;
222 cli->cnum = -1;
223 cli->pid = (uint16)sys_getpid();
224 cli->mid = 1;
225 cli->vuid = UID_FIELD_INVALID;
226 cli->protocol = PROTOCOL_NT1;
227 cli->timeout = 20000; /* Timeout is in milliseconds. */
228 cli->bufsize = CLI_BUFFER_SIZE+4;
229 cli->max_xmit = cli->bufsize;
230 cli->outbuf = (char *)malloc(cli->bufsize);
231 cli->inbuf = (char *)malloc(cli->bufsize);
232 cli->oplock_handler = cli_oplock_ack;
233 if (lp_use_spnego()) {
234 cli->use_spnego = True;
237 /* Set the CLI_FORCE_DOSERR environment variable to test
238 client routines using DOS errors instead of STATUS32
239 ones. This intended only as a temporary hack. */
240 if (getenv("CLI_FORCE_DOSERR")) {
241 cli->force_dos_errors = True;
244 /* A way to attempt to force SMB signing */
245 if (getenv("CLI_FORCE_SMB_SIGNING"))
246 cli->sign_info.negotiated_smb_signing = True;
248 if (!cli->outbuf || !cli->inbuf)
249 goto error;
251 if ((cli->mem_ctx = talloc_init_named("cli based talloc")) == NULL)
252 goto error;
254 memset(cli->outbuf, 0, cli->bufsize);
255 memset(cli->inbuf, 0, cli->bufsize);
257 cli->nt_pipe_fnum = 0;
259 cli->initialised = 1;
260 cli->allocated = alloced_cli;
262 return cli;
264 /* Clean up after malloc() error */
266 error:
268 SAFE_FREE(cli->inbuf);
269 SAFE_FREE(cli->outbuf);
271 if (alloced_cli)
272 SAFE_FREE(cli);
274 return NULL;
277 /****************************************************************************
278 Close a client connection and free the memory without destroying cli itself.
279 ****************************************************************************/
281 void cli_close_connection(struct cli_state *cli)
283 SAFE_FREE(cli->outbuf);
284 SAFE_FREE(cli->inbuf);
286 data_blob_free(&cli->secblob);
288 if (cli->mem_ctx) {
289 talloc_destroy(cli->mem_ctx);
290 cli->mem_ctx = NULL;
293 if (cli->fd != -1)
294 close(cli->fd);
295 cli->fd = -1;
298 /****************************************************************************
299 Shutdown a client structure.
300 ****************************************************************************/
302 void cli_shutdown(struct cli_state *cli)
304 BOOL allocated = cli->allocated;
305 cli_close_connection(cli);
306 ZERO_STRUCTP(cli);
307 if (allocated) {
308 free(cli);
312 /****************************************************************************
313 Set socket options on a open connection.
314 ****************************************************************************/
316 void cli_sockopt(struct cli_state *cli, char *options)
318 set_socket_options(cli->fd, options);
321 /****************************************************************************
322 Set the PID to use for smb messages. Return the old pid.
323 ****************************************************************************/
325 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
327 uint16 ret = cli->pid;
328 cli->pid = pid;
329 return ret;