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
)
50 ret
= receive_smb(fd
, buffer
, timeout
);
53 DEBUG(10,("client_receive_smb failed\n"));
58 /* Ignore session keepalive packets. */
59 if(CVAL(buffer
,0) != SMBkeepalive
)
66 /****************************************************************************
68 ****************************************************************************/
70 BOOL
cli_receive_smb(struct cli_state
*cli
)
74 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
79 ret
= client_receive_smb(cli
->fd
,cli
->inbuf
,cli
->timeout
);
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);
98 /* If the server is not responding, note that now */
108 /****************************************************************************
110 ****************************************************************************/
112 BOOL
cli_send_smb(struct cli_state
*cli
)
118 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
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
);
131 DEBUG(0,("Error writing %d bytes to client. %d\n", (int)len
,(int)ret
));
139 /****************************************************************************
140 Setup basics in a outgoing packet.
141 ****************************************************************************/
143 void cli_setup_packet(struct cli_state
*cli
)
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
) {
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
;
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"));
208 cli
= (struct cli_state
*)malloc(sizeof(*cli
));
215 if (cli
->initialised
)
216 cli_close_connection(cli
);
223 cli
->pid
= (uint16
)sys_getpid();
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
)
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 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
);
289 talloc_destroy(cli
->mem_ctx
);
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
);
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
;
332 /****************************************************************************
333 Send a keepalive packet to the server
334 ****************************************************************************/
335 BOOL
cli_send_keepalive(struct cli_state
*cli
)
338 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
341 if (!send_keepalive(cli
->fd
)) {
344 DEBUG(0,("Error sending keepalive packet to client.\n"));