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 * Change the port number used to call on
29 int cli_set_port(struct cli_state *cli, int port)
35 /****************************************************************************
37 ****************************************************************************/
38 BOOL cli_receive_smb(struct cli_state *cli)
42 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
45 /* it might be an oplock break request */
46 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
47 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
48 SVAL(cli->inbuf,smb_vwv6) == 0 &&
49 SVAL(cli->inbuf,smb_vwv7) == 0) {
50 if (cli->oplock_handler) {
51 int fnum = SVAL(cli->inbuf,smb_vwv2);
52 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
53 if (!cli->oplock_handler(cli, fnum, level)) return False;
55 /* try to prevent loops */
56 CVAL(cli->inbuf,smb_com) = 0xFF;
64 /****************************************************************************
65 send an smb to a fd and re-establish if necessary
66 ****************************************************************************/
67 BOOL cli_send_smb(struct cli_state *cli)
72 BOOL reestablished=False;
74 len = smb_len(cli->outbuf) + 4;
76 while (nwritten < len) {
77 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
78 if (ret <= 0 && errno == EPIPE && !reestablished) {
79 if (cli_reestablish_connection(cli)) {
86 DEBUG(0,("Error writing %d bytes to client. %d\n",
96 /****************************************************************************
97 setup basics in a outgoing packet
98 ****************************************************************************/
99 void cli_setup_packet(struct cli_state *cli)
102 SSVAL(cli->outbuf,smb_pid,cli->pid);
103 SSVAL(cli->outbuf,smb_uid,cli->vuid);
104 SSVAL(cli->outbuf,smb_mid,cli->mid);
105 if (cli->protocol > PROTOCOL_CORE) {
107 SCVAL(cli->outbuf,smb_flg,0x8);
108 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
109 if (cli->capabilities & CAP_UNICODE) {
110 flags2 |= FLAGS2_UNICODE_STRINGS;
112 if (cli->capabilities & CAP_STATUS32) {
113 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
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)));
129 /****************************************************************************
130 initialise a client structure
131 ****************************************************************************/
132 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
134 /* copy_nt_creds(&cli->usr, usr); */
135 safe_strcpy(cli->domain , usr->domain , sizeof(usr->domain )-1);
136 safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
137 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
138 cli->ntlmssp_flags = usr->ntlmssp_flags;
139 cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
141 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
142 cli->user_name, cli->domain,
143 cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
147 /****************************************************************************
148 initialise a client structure
149 ****************************************************************************/
150 struct cli_state *cli_initialise(struct cli_state *cli)
152 BOOL alloced_cli = False;
155 cli = (struct cli_state *)malloc(sizeof(*cli));
162 if (cli->initialised) {
171 cli->pid = (uint16)sys_getpid();
173 cli->vuid = UID_FIELD_INVALID;
174 cli->protocol = PROTOCOL_NT1;
175 cli->timeout = 20000; /* Timeout is in milliseconds. */
176 cli->bufsize = CLI_BUFFER_SIZE+4;
177 cli->max_xmit = cli->bufsize;
178 cli->outbuf = (char *)malloc(cli->bufsize);
179 cli->inbuf = (char *)malloc(cli->bufsize);
180 cli->oplock_handler = cli_oplock_ack;
182 if (!cli->outbuf || !cli->inbuf)
185 if ((cli->mem_ctx = talloc_init()) == NULL)
188 memset(cli->outbuf, 0, cli->bufsize);
189 memset(cli->inbuf, 0, cli->bufsize);
191 cli->nt_pipe_fnum = 0;
193 cli->initialised = 1;
197 /* Clean up after malloc() error */
201 SAFE_FREE(cli->inbuf);
202 SAFE_FREE(cli->outbuf);
210 /****************************************************************************
211 shutdown a client structure
212 ****************************************************************************/
213 void cli_shutdown(struct cli_state *cli)
215 SAFE_FREE(cli->outbuf);
216 SAFE_FREE(cli->inbuf);
219 talloc_destroy(cli->mem_ctx);
223 sslutil_disconnect(cli->fd);
224 #endif /* WITH_SSL */
227 memset(cli, 0, sizeof(*cli));
231 /****************************************************************************
232 set socket options on a open connection
233 ****************************************************************************/
234 void cli_sockopt(struct cli_state *cli, char *options)
236 set_socket_options(cli->fd, options);
239 /****************************************************************************
240 set the PID to use for smb messages. Return the old pid.
241 ****************************************************************************/
242 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
244 uint16 ret = cli->pid;