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.
26 * Change the port number used to call on
28 int cli_set_port(struct cli_state *cli, int 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)
51 ret = receive_smb(fd, buffer, timeout);
55 DEBUG(10,("client_receive_smb failed\n"));
60 /* Ignore session keepalive packets. */
61 if(CVAL(buffer,0) != SMBkeepalive)
69 /****************************************************************************
71 ****************************************************************************/
72 BOOL cli_receive_smb(struct cli_state *cli)
76 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
77 if (cli->fd == -1) return False;
80 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
83 /* it might be an oplock break request */
84 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
85 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
86 SVAL(cli->inbuf,smb_vwv6) == 0 &&
87 SVAL(cli->inbuf,smb_vwv7) == 0) {
88 if (cli->oplock_handler) {
89 int fnum = SVAL(cli->inbuf,smb_vwv2);
90 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
91 if (!cli->oplock_handler(cli, fnum, level)) return False;
93 /* try to prevent loops */
94 SCVAL(cli->inbuf,smb_com,0xFF);
99 /* If the server is not responding, note that now */
109 /****************************************************************************
111 ****************************************************************************/
113 BOOL cli_send_smb(struct cli_state *cli)
119 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
123 cli_caclulate_sign_mac(cli);
125 len = smb_len(cli->outbuf) + 4;
127 while (nwritten < len) {
128 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
132 DEBUG(0,("Error writing %d bytes to client. %d\n", (int)len,(int)ret));
140 /****************************************************************************
141 Setup basics in a outgoing packet.
142 ****************************************************************************/
144 void cli_setup_packet(struct cli_state *cli)
147 SSVAL(cli->outbuf,smb_pid,cli->pid);
148 SSVAL(cli->outbuf,smb_uid,cli->vuid);
149 SSVAL(cli->outbuf,smb_mid,cli->mid);
150 if (cli->protocol > PROTOCOL_CORE) {
152 SCVAL(cli->outbuf,smb_flg,0x8);
153 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
154 if (cli->capabilities & CAP_UNICODE) {
155 flags2 |= FLAGS2_UNICODE_STRINGS;
157 if (cli->capabilities & CAP_STATUS32) {
158 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
160 if (cli->use_spnego) {
161 flags2 |= FLAGS2_EXTENDED_SECURITY;
163 if (cli->sign_info.use_smb_signing)
164 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
165 SSVAL(cli->outbuf,smb_flg2, flags2);
169 /****************************************************************************
170 setup the bcc length of the packet from a pointer to the end of the data
171 ****************************************************************************/
172 void cli_setup_bcc(struct cli_state *cli, void *p)
174 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
179 /****************************************************************************
180 initialise credentials of a client structure
181 ****************************************************************************/
182 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
184 /* copy_nt_creds(&cli->usr, usr); */
185 safe_strcpy(cli->domain , usr->domain , sizeof(usr->domain )-1);
186 safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
187 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
188 cli->ntlmssp_flags = usr->ntlmssp_flags;
189 cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
191 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
192 cli->user_name, cli->domain,
193 cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
197 /****************************************************************************
198 initialise a client structure
199 ****************************************************************************/
200 struct cli_state *cli_initialise(struct cli_state *cli)
202 BOOL alloced_cli = False;
204 /* Check the effective uid - make sure we are not setuid */
205 if (is_setuid_root()) {
206 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
211 cli = (struct cli_state *)malloc(sizeof(*cli));
218 if (cli->initialised) {
227 cli->pid = (uint16)sys_getpid();
229 cli->vuid = UID_FIELD_INVALID;
230 cli->protocol = PROTOCOL_NT1;
231 cli->timeout = 20000; /* Timeout is in milliseconds. */
232 cli->bufsize = CLI_BUFFER_SIZE+4;
233 cli->max_xmit = cli->bufsize;
234 cli->outbuf = (char *)malloc(cli->bufsize);
235 cli->inbuf = (char *)malloc(cli->bufsize);
236 cli->oplock_handler = cli_oplock_ack;
237 if (lp_use_spnego()) {
238 cli->use_spnego = True;
241 /* Set the CLI_FORCE_DOSERR environment variable to test
242 client routines using DOS errors instead of STATUS32
243 ones. This intended only as a temporary hack. */
244 if (getenv("CLI_FORCE_DOSERR")) {
245 cli->force_dos_errors = True;
248 if (!cli->outbuf || !cli->inbuf)
251 if ((cli->mem_ctx = talloc_init_named("cli based talloc")) == NULL)
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;
264 /* Clean up after malloc() error */
268 SAFE_FREE(cli->inbuf);
269 SAFE_FREE(cli->outbuf);
277 /****************************************************************************
278 shutdown a client structure
279 ****************************************************************************/
280 void cli_shutdown(struct cli_state *cli)
283 SAFE_FREE(cli->outbuf);
284 SAFE_FREE(cli->inbuf);
286 data_blob_free(&cli->secblob);
289 talloc_destroy(cli->mem_ctx);
293 allocated = cli->allocated;
301 /****************************************************************************
302 set socket options on a open connection
303 ****************************************************************************/
304 void cli_sockopt(struct cli_state *cli, char *options)
306 set_socket_options(cli->fd, options);
309 /****************************************************************************
310 set the PID to use for smb messages. Return the old pid.
311 ****************************************************************************/
312 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
314 uint16 ret = cli->pid;