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;
30 * Change the port number used to call on
32 int cli_set_port(struct cli_state *cli, int port)
38 /****************************************************************************
40 ****************************************************************************/
41 BOOL cli_receive_smb(struct cli_state *cli)
45 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
48 /* it might be an oplock break request */
49 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
50 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
51 SVAL(cli->inbuf,smb_vwv6) == 0 &&
52 SVAL(cli->inbuf,smb_vwv7) == 0) {
53 if (cli->oplock_handler) {
54 int fnum = SVAL(cli->inbuf,smb_vwv2);
55 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
56 if (!cli->oplock_handler(cli, fnum, level)) return False;
58 /* try to prevent loops */
59 CVAL(cli->inbuf,smb_com) = 0xFF;
67 /****************************************************************************
68 send an smb to a fd and re-establish if necessary
69 ****************************************************************************/
70 BOOL cli_send_smb(struct cli_state *cli)
75 BOOL reestablished=False;
77 len = smb_len(cli->outbuf) + 4;
79 while (nwritten < len) {
80 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
81 if (ret <= 0 && errno == EPIPE && !reestablished) {
82 if (cli_reestablish_connection(cli)) {
89 DEBUG(0,("Error writing %d bytes to client. %d\n",
99 /****************************************************************************
100 setup basics in a outgoing packet
101 ****************************************************************************/
102 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 if (cli->capabilities & CAP_STATUS32) {
116 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
118 SSVAL(cli->outbuf,smb_flg2, flags2);
122 /****************************************************************************
123 setup the bcc length of the packet from a pointer to the end of the data
124 ****************************************************************************/
125 void cli_setup_bcc(struct cli_state *cli, void *p)
127 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
132 /****************************************************************************
133 initialise a client structure
134 ****************************************************************************/
135 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
137 /* copy_nt_creds(&cli->usr, usr); */
138 safe_strcpy(cli->domain , usr->domain , sizeof(usr->domain )-1);
139 safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
140 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
141 cli->ntlmssp_flags = usr->ntlmssp_flags;
142 cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
144 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
145 cli->user_name, cli->domain,
146 cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
150 /****************************************************************************
151 initialise a client structure
152 ****************************************************************************/
153 struct cli_state *cli_initialise(struct cli_state *cli)
155 BOOL alloced_cli = False;
158 cli = (struct cli_state *)malloc(sizeof(*cli));
165 if (cli->initialised) {
174 cli->pid = (uint16)sys_getpid();
176 cli->vuid = UID_FIELD_INVALID;
177 cli->protocol = PROTOCOL_NT1;
178 cli->timeout = 20000; /* Timeout is in milliseconds. */
179 cli->bufsize = CLI_BUFFER_SIZE+4;
180 cli->max_xmit = cli->bufsize;
181 cli->outbuf = (char *)malloc(cli->bufsize);
182 cli->inbuf = (char *)malloc(cli->bufsize);
183 cli->oplock_handler = cli_oplock_ack;
185 if (!cli->outbuf || !cli->inbuf)
188 if ((cli->mem_ctx = talloc_init()) == NULL)
191 memset(cli->outbuf, 0, cli->bufsize);
192 memset(cli->inbuf, 0, cli->bufsize);
194 cli->nt_pipe_fnum = 0;
196 cli->initialised = 1;
200 /* Clean up after malloc() error */
204 safe_free(cli->inbuf);
205 safe_free(cli->outbuf);
213 /****************************************************************************
214 shutdown a client structure
215 ****************************************************************************/
216 void cli_shutdown(struct cli_state *cli)
228 talloc_destroy(cli->mem_ctx);
232 sslutil_disconnect(cli->fd);
233 #endif /* WITH_SSL */
236 memset(cli, 0, sizeof(*cli));
240 /****************************************************************************
241 set socket options on a open connection
242 ****************************************************************************/
243 void cli_sockopt(struct cli_state *cli, char *options)
245 set_socket_options(cli->fd, options);
248 /****************************************************************************
249 set the PID to use for smb messages. Return the old pid.
250 ****************************************************************************/
251 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
253 uint16 ret = cli->pid;