Janitorial duties to make autogen.sh portable.
[Samba/gebeck_regimport.git] / source3 / libsmb / clientgen.c
blob81b3bbcab553ad4fba45061c3ddf7b2c9534363f
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"
25 /****************************************************************************
26 Change the timeout (in milliseconds).
27 ****************************************************************************/
29 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
31 unsigned int old_timeout = cli->timeout;
32 cli->timeout = timeout;
33 return old_timeout;
36 /****************************************************************************
37 Change the port number used to call on.
38 ****************************************************************************/
40 int cli_set_port(struct cli_state *cli, int port)
42 cli->port = port;
43 return port;
46 /****************************************************************************
47 Read an smb from a fd ignoring all keepalive packets. Note that the buffer
48 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
49 The timeout is in milliseconds
51 This is exactly the same as receive_smb except that it never returns
52 a session keepalive packet (just as receive_smb used to do).
53 receive_smb was changed to return keepalives as the oplock processing means this call
54 should never go into a blocking read.
55 ****************************************************************************/
57 static BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
59 BOOL ret;
61 for(;;) {
62 ret = receive_smb(fd, buffer, timeout);
64 if (!ret) {
65 DEBUG(10,("client_receive_smb failed\n"));
66 show_msg(buffer);
67 return ret;
70 /* Ignore session keepalive packets. */
71 if(CVAL(buffer,0) != SMBkeepalive)
72 break;
74 show_msg(buffer);
75 return ret;
78 /****************************************************************************
79 Recv an smb.
80 ****************************************************************************/
82 BOOL cli_receive_smb(struct cli_state *cli)
84 extern int smb_read_error;
85 BOOL ret;
87 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
88 if (cli->fd == -1)
89 return False;
91 again:
92 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
94 if (ret) {
95 /* it might be an oplock break request */
96 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
97 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
98 SVAL(cli->inbuf,smb_vwv6) == 0 &&
99 SVAL(cli->inbuf,smb_vwv7) == 0) {
100 if (cli->oplock_handler) {
101 int fnum = SVAL(cli->inbuf,smb_vwv2);
102 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
103 if (!cli->oplock_handler(cli, fnum, level)) return False;
105 /* try to prevent loops */
106 SCVAL(cli->inbuf,smb_com,0xFF);
107 goto again;
111 /* If the server is not responding, note that now */
113 if (!ret) {
114 cli->smb_rw_error = smb_read_error;
115 close(cli->fd);
116 cli->fd = -1;
117 return ret;
120 if (!cli_check_sign_mac(cli)) {
121 DEBUG(0, ("SMB Signiture verification failed on incoming packet!\n"));
122 return False;
124 return True;
127 /****************************************************************************
128 Send an smb to a fd.
129 ****************************************************************************/
131 BOOL cli_send_smb(struct cli_state *cli)
133 size_t len;
134 size_t nwritten=0;
135 ssize_t ret;
137 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
138 if (cli->fd == -1)
139 return False;
141 cli_caclulate_sign_mac(cli);
143 len = smb_len(cli->outbuf) + 4;
145 while (nwritten < len) {
146 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
147 if (ret <= 0) {
148 close(cli->fd);
149 cli->fd = -1;
150 cli->smb_rw_error = WRITE_ERROR;
151 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
152 (int)len,(int)ret, strerror(errno) ));
153 return False;
155 nwritten += ret;
157 return True;
160 /****************************************************************************
161 Setup basics in a outgoing packet.
162 ****************************************************************************/
164 void cli_setup_packet(struct cli_state *cli)
166 cli->rap_error = 0;
167 SSVAL(cli->outbuf,smb_pid,cli->pid);
168 SSVAL(cli->outbuf,smb_uid,cli->vuid);
169 SSVAL(cli->outbuf,smb_mid,cli->mid);
170 if (cli->protocol > PROTOCOL_CORE) {
171 uint16 flags2;
172 SCVAL(cli->outbuf,smb_flg,0x8);
173 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
174 if (cli->capabilities & CAP_UNICODE)
175 flags2 |= FLAGS2_UNICODE_STRINGS;
176 if (cli->capabilities & CAP_STATUS32)
177 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
178 if (cli->use_spnego)
179 flags2 |= FLAGS2_EXTENDED_SECURITY;
180 SSVAL(cli->outbuf,smb_flg2, flags2);
184 /****************************************************************************
185 Setup the bcc length of the packet from a pointer to the end of the data.
186 ****************************************************************************/
188 void cli_setup_bcc(struct cli_state *cli, void *p)
190 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
193 /****************************************************************************
194 Initialise credentials of a client structure.
195 ****************************************************************************/
197 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
199 /* copy_nt_creds(&cli->usr, usr); */
200 fstrcpy(cli->domain , usr->domain);
201 fstrcpy(cli->user_name, usr->user_name);
202 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
203 cli->ntlmssp_flags = usr->ntlmssp_flags;
204 cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
206 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
207 cli->user_name, cli->domain,
208 cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
211 /****************************************************************************
212 Initialise a client structure.
213 ****************************************************************************/
215 struct cli_state *cli_initialise(struct cli_state *cli)
217 BOOL alloced_cli = False;
219 /* Check the effective uid - make sure we are not setuid */
220 if (is_setuid_root()) {
221 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
222 return NULL;
225 if (!cli) {
226 cli = (struct cli_state *)malloc(sizeof(*cli));
227 if (!cli)
228 return NULL;
229 ZERO_STRUCTP(cli);
230 alloced_cli = True;
233 if (cli->initialised)
234 cli_close_connection(cli);
236 ZERO_STRUCTP(cli);
238 cli->port = 0;
239 cli->fd = -1;
240 cli->cnum = -1;
241 cli->pid = (uint16)sys_getpid();
242 cli->mid = 1;
243 cli->vuid = UID_FIELD_INVALID;
244 cli->protocol = PROTOCOL_NT1;
245 cli->timeout = 20000; /* Timeout is in milliseconds. */
246 cli->bufsize = CLI_BUFFER_SIZE+4;
247 cli->max_xmit = cli->bufsize;
248 cli->outbuf = (char *)malloc(cli->bufsize+SAFETY_MARGIN);
249 cli->inbuf = (char *)malloc(cli->bufsize+SAFETY_MARGIN);
250 cli->oplock_handler = cli_oplock_ack;
252 cli->use_spnego = lp_client_use_spnego();
254 cli->capabilities = CAP_UNICODE | CAP_STATUS32;
256 /* Set the CLI_FORCE_DOSERR environment variable to test
257 client routines using DOS errors instead of STATUS32
258 ones. This intended only as a temporary hack. */
259 if (getenv("CLI_FORCE_DOSERR"))
260 cli->force_dos_errors = True;
262 /* initialise signing */
263 cli_null_set_signing(cli);
265 if (lp_client_signing())
266 cli->sign_info.allow_smb_signing = True;
268 if (!cli->outbuf || !cli->inbuf)
269 goto error;
271 if ((cli->mem_ctx = talloc_init("cli based talloc")) == NULL)
272 goto error;
274 memset(cli->outbuf, 0, cli->bufsize);
275 memset(cli->inbuf, 0, cli->bufsize);
277 cli->nt_pipe_fnum = 0;
278 cli->saved_netlogon_pipe_fnum = 0;
280 cli->initialised = 1;
281 cli->allocated = alloced_cli;
283 return cli;
285 /* Clean up after malloc() error */
287 error:
289 SAFE_FREE(cli->inbuf);
290 SAFE_FREE(cli->outbuf);
292 if (alloced_cli)
293 SAFE_FREE(cli);
295 return NULL;
298 /****************************************************************************
299 Close a client connection and free the memory without destroying cli itself.
300 ****************************************************************************/
302 void cli_close_connection(struct cli_state *cli)
304 SAFE_FREE(cli->outbuf);
305 SAFE_FREE(cli->inbuf);
307 cli_free_signing_context(cli);
308 data_blob_free(&cli->secblob);
310 if (cli->mem_ctx) {
311 talloc_destroy(cli->mem_ctx);
312 cli->mem_ctx = NULL;
315 if (cli->fd != -1)
316 close(cli->fd);
317 cli->fd = -1;
318 cli->smb_rw_error = 0;
322 /****************************************************************************
323 Shutdown a client structure.
324 ****************************************************************************/
326 void cli_shutdown(struct cli_state *cli)
328 BOOL allocated = cli->allocated;
329 cli_close_connection(cli);
330 ZERO_STRUCTP(cli);
331 if (allocated)
332 free(cli);
335 /****************************************************************************
336 Set socket options on a open connection.
337 ****************************************************************************/
339 void cli_sockopt(struct cli_state *cli, const char *options)
341 set_socket_options(cli->fd, options);
344 /****************************************************************************
345 Set the PID to use for smb messages. Return the old pid.
346 ****************************************************************************/
348 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
350 uint16 ret = cli->pid;
351 cli->pid = pid;
352 return ret;
355 /****************************************************************************
356 Send a keepalive packet to the server
357 ****************************************************************************/
358 BOOL cli_send_keepalive(struct cli_state *cli)
360 if (cli->fd == -1) {
361 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
362 return False;
364 if (!send_keepalive(cli->fd)) {
365 close(cli->fd);
366 cli->fd = -1;
367 DEBUG(0,("Error sending keepalive packet to client.\n"));
368 return False;
370 return True;