2 Unix SMB/Netbios implementation.
4 SMB client generic functions
5 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
27 extern int DEBUGLEVEL
;
28 static void cli_process_oplock(struct cli_state
*cli
);
31 * Change the port number used to call on
33 int cli_set_port(struct cli_state
*cli
, int port
)
41 /****************************************************************************
43 ****************************************************************************/
44 BOOL
cli_receive_smb(struct cli_state
*cli
)
48 ret
= client_receive_smb(cli
->fd
,cli
->inbuf
,cli
->timeout
);
51 /* it might be an oplock break request */
52 if (!(CVAL(cli
->inbuf
, smb_flg
) & FLAG_REPLY
) &&
53 CVAL(cli
->inbuf
,smb_com
) == SMBlockingX
&&
54 SVAL(cli
->inbuf
,smb_vwv6
) == 0 &&
55 SVAL(cli
->inbuf
,smb_vwv7
) == 0) {
56 if (cli
->use_oplocks
) cli_process_oplock(cli
);
57 /* try to prevent loops */
58 CVAL(cli
->inbuf
,smb_com
) = 0xFF;
66 /****************************************************************************
67 send an smb to a fd and re-establish if necessary
68 ****************************************************************************/
69 BOOL
cli_send_smb(struct cli_state
*cli
)
74 BOOL reestablished
=False
;
76 len
= smb_len(cli
->outbuf
) + 4;
78 while (nwritten
< len
) {
79 ret
= write_socket(cli
->fd
,cli
->outbuf
+nwritten
,len
- nwritten
);
80 if (ret
<= 0 && errno
== EPIPE
&& !reestablished
) {
81 if (cli_reestablish_connection(cli
)) {
88 DEBUG(0,("Error writing %d bytes to client. %d\n",
98 /****************************************************************************
99 setup basics in a outgoing packet
100 ****************************************************************************/
101 void cli_setup_packet(struct cli_state
*cli
)
105 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
106 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
107 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
108 if (cli
->protocol
> PROTOCOL_CORE
) {
110 SCVAL(cli
->outbuf
,smb_flg
,0x8);
111 flags2
= FLAGS2_LONG_PATH_COMPONENTS
;
112 if (cli
->capabilities
& CAP_UNICODE
) {
113 flags2
|= FLAGS2_UNICODE_STRINGS
;
115 SSVAL(cli
->outbuf
,smb_flg2
, flags2
);
119 /****************************************************************************
120 setup the bcc length of the packet from a pointer to the end of the data
121 ****************************************************************************/
122 void cli_setup_bcc(struct cli_state
*cli
, void *p
)
124 set_message_bcc(cli
->outbuf
, PTR_DIFF(p
, smb_buf(cli
->outbuf
)));
128 /****************************************************************************
129 process an oplock break request from the server
130 ****************************************************************************/
131 static void cli_process_oplock(struct cli_state
*cli
)
133 char *oldbuf
= cli
->outbuf
;
138 fnum
= SVAL(cli
->inbuf
,smb_vwv2
);
139 level
= CVAL(cli
->inbuf
,smb_vwv3
+1);
141 /* damn, we really need to keep a record of open files so we
142 can detect a oplock break and a close crossing on the
143 wire. for now this swallows the errors */
144 if (fnum
== 0) return;
146 /* Ignore level II break to none's. */
147 if (level
== OPLOCKLEVEL_NONE
)
152 memset(buf
,'\0',smb_size
);
153 set_message(buf
,8,0,True
);
155 CVAL(buf
,smb_com
) = SMBlockingX
;
156 SSVAL(buf
,smb_tid
, cli
->cnum
);
157 cli_setup_packet(cli
);
158 SSVAL(buf
,smb_vwv0
,0xFF);
159 SSVAL(buf
,smb_vwv1
,0);
160 SSVAL(buf
,smb_vwv2
,fnum
);
161 if (cli
->use_level_II_oplocks
)
162 SSVAL(buf
,smb_vwv3
,0x102); /* levelII oplock break ack */
164 SSVAL(buf
,smb_vwv3
,2); /* exclusive oplock break ack */
165 SIVAL(buf
,smb_vwv4
,0); /* timoeut */
166 SSVAL(buf
,smb_vwv6
,0); /* unlockcount */
167 SSVAL(buf
,smb_vwv7
,0); /* lockcount */
171 cli
->outbuf
= oldbuf
;
174 /****************************************************************************
175 initialise a client structure
176 ****************************************************************************/
177 void cli_init_creds(struct cli_state
*cli
, const struct ntuser_creds
*usr
)
179 /* copy_nt_creds(&cli->usr, usr); */
180 safe_strcpy(cli
->domain
, usr
->domain
, sizeof(usr
->domain
)-1);
181 safe_strcpy(cli
->user_name
, usr
->user_name
, sizeof(usr
->user_name
)-1);
182 memcpy(&cli
->pwd
, &usr
->pwd
, sizeof(usr
->pwd
));
183 cli
->ntlmssp_flags
= usr
->ntlmssp_flags
;
184 cli
->ntlmssp_cli_flgs
= usr
!= NULL
? usr
->ntlmssp_flags
: 0;
186 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
187 cli
->user_name
, cli
->domain
,
188 cli
->ntlmssp_flags
,cli
->ntlmssp_cli_flgs
));
192 /****************************************************************************
193 initialise a client structure
194 ****************************************************************************/
195 struct cli_state
*cli_initialise(struct cli_state
*cli
)
198 cli
= (struct cli_state
*)malloc(sizeof(*cli
));
204 if (cli
->initialised
) {
213 cli
->pid
= (uint16
)sys_getpid();
215 cli
->vuid
= UID_FIELD_INVALID
;
216 cli
->protocol
= PROTOCOL_NT1
;
217 cli
->timeout
= 20000; /* Timeout is in milliseconds. */
218 cli
->bufsize
= CLI_BUFFER_SIZE
+4;
219 cli
->max_xmit
= cli
->bufsize
;
220 cli
->outbuf
= (char *)malloc(cli
->bufsize
);
221 cli
->inbuf
= (char *)malloc(cli
->bufsize
);
222 if (!cli
->outbuf
|| !cli
->inbuf
)
227 if ((cli
->mem_ctx
= talloc_init()) == NULL
) {
233 memset(cli
->outbuf
, '\0', cli
->bufsize
);
234 memset(cli
->inbuf
, '\0', cli
->bufsize
);
236 cli
->nt_pipe_fnum
= 0;
238 cli
->initialised
= 1;
243 /****************************************************************************
244 shutdown a client structure
245 ****************************************************************************/
246 void cli_shutdown(struct cli_state
*cli
)
258 talloc_destroy(cli
->mem_ctx
);
262 sslutil_disconnect(cli
->fd
);
263 #endif /* WITH_SSL */
266 memset(cli
, 0, sizeof(*cli
));
270 /****************************************************************************
271 set socket options on a open connection
272 ****************************************************************************/
273 void cli_sockopt(struct cli_state
*cli
, char *options
)
275 set_socket_options(cli
->fd
, options
);
278 /****************************************************************************
279 set the PID to use for smb messages. Return the old pid.
280 ****************************************************************************/
281 uint16
cli_setpid(struct cli_state
*cli
, uint16 pid
)
283 uint16 ret
= cli
->pid
;