few edits
[Samba.git] / source / libsmb / clientgen.c
blob867b13fe8cb84a0e006d369a7329055a1ed17e03
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
22 #define NO_SYSLOG
24 #include "includes.h"
26 /****************************************************************************
27 Change the port number used to call on.
28 ****************************************************************************/
30 int cli_set_port(struct cli_state *cli, int port)
32 cli->port = port;
33 return port;
36 /****************************************************************************
37 Recv an smb.
38 ****************************************************************************/
40 BOOL cli_receive_smb(struct cli_state *cli)
42 extern int smb_read_error;
43 BOOL ret;
45 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
46 if (cli->fd == -1)
47 return False;
49 again:
50 ret = client_receive_smb(cli->fd,cli->inbuf,abs(cli->timeout));
52 if (ret) {
53 /* it might be an oplock break request */
54 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
55 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
56 SVAL(cli->inbuf,smb_vwv6) == 0 &&
57 SVAL(cli->inbuf,smb_vwv7) == 0) {
58 if (cli->oplock_handler) {
59 int fnum = SVAL(cli->inbuf,smb_vwv2);
60 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
61 if (!cli->oplock_handler(cli, fnum, level)) return False;
63 /* try to prevent loops */
64 SCVAL(cli->inbuf,smb_com,0xFF);
65 goto again;
69 /* If the server is not responding, note that now */
70 if (!ret) {
71 cli->smb_rw_error = smb_read_error;
72 close(cli->fd);
73 cli->fd = -1;
76 return ret;
79 /****************************************************************************
80 Send an smb to a fd.
81 ****************************************************************************/
83 BOOL cli_send_smb(struct cli_state *cli)
85 size_t len;
86 size_t nwritten=0;
87 ssize_t ret;
89 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
90 if (cli->fd == -1)
91 return False;
93 len = smb_len(cli->outbuf) + 4;
95 while (nwritten < len) {
96 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
97 if (ret <= 0) {
98 close(cli->fd);
99 cli->fd = -1;
100 cli->smb_rw_error = WRITE_ERROR;
101 DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
102 (int)len,(int)ret, strerror(errno) ));
103 return False;
105 nwritten += ret;
108 return True;
111 /****************************************************************************
112 Setup basics in a outgoing packet.
113 ****************************************************************************/
115 void cli_setup_packet(struct cli_state *cli)
117 cli->rap_error = 0;
118 SSVAL(cli->outbuf,smb_pid,cli->pid);
119 SSVAL(cli->outbuf,smb_uid,cli->vuid);
120 SSVAL(cli->outbuf,smb_mid,cli->mid);
121 if (cli->protocol > PROTOCOL_CORE) {
122 uint16 flags2;
123 SCVAL(cli->outbuf,smb_flg,0x8);
124 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
125 if (cli->capabilities & CAP_UNICODE) {
126 flags2 |= FLAGS2_UNICODE_STRINGS;
128 if (cli->capabilities & CAP_STATUS32) {
129 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
131 SSVAL(cli->outbuf,smb_flg2, flags2);
135 /****************************************************************************
136 Setup the bcc length of the packet from a pointer to the end of the data.
137 ****************************************************************************/
139 void cli_setup_bcc(struct cli_state *cli, void *p)
141 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
144 /****************************************************************************
145 Initialise a client structure.
146 ****************************************************************************/
148 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
150 /* copy_nt_creds(&cli->usr, usr); */
151 safe_strcpy(cli->domain , usr->domain , sizeof(usr->domain )-1);
152 safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
153 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
154 cli->ntlmssp_flags = usr->ntlmssp_flags;
155 cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
157 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
158 cli->user_name, cli->domain,
159 cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
162 /****************************************************************************
163 Initialise a client structure.
164 ****************************************************************************/
166 struct cli_state *cli_initialise(struct cli_state *cli)
168 BOOL alloced_cli = False;
170 /* Check the effective uid - make sure we are not setuid */
171 if (is_setuid_root()) {
172 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
173 return NULL;
176 if (!cli) {
177 cli = (struct cli_state *)malloc(sizeof(*cli));
178 if (!cli)
179 return NULL;
180 ZERO_STRUCTP(cli);
181 alloced_cli = True;
184 if (cli->initialised)
185 cli_close_connection(cli);
187 ZERO_STRUCTP(cli);
189 cli->port = 0;
190 cli->fd = -1;
191 cli->cnum = -1;
192 cli->pid = (uint16)sys_getpid();
193 cli->mid = 1;
194 cli->vuid = UID_FIELD_INVALID;
195 cli->protocol = PROTOCOL_NT1;
196 cli->timeout = 20000; /* Timeout is in milliseconds. */
197 cli->bufsize = CLI_BUFFER_SIZE+4;
198 cli->max_xmit = cli->bufsize;
199 cli->outbuf = (char *)malloc(cli->bufsize);
200 cli->inbuf = (char *)malloc(cli->bufsize);
201 cli->oplock_handler = cli_oplock_ack;
202 /* Set the CLI_FORCE_DOSERR environment variable to test
203 client routines using DOS errors instead of STATUS32
204 ones. This intended only as a temporary hack. */
205 if (getenv("CLI_FORCE_DOSERR"))
206 cli->force_dos_errors = True;
208 if (!cli->outbuf || !cli->inbuf)
209 goto error;
211 if ((cli->mem_ctx = talloc_init()) == NULL)
212 goto error;
214 memset(cli->outbuf, 0, cli->bufsize);
215 memset(cli->inbuf, 0, cli->bufsize);
217 cli->nt_pipe_fnum = 0;
219 cli->initialised = 1;
220 cli->allocated = alloced_cli;
222 return cli;
224 /* Clean up after malloc() error */
226 error:
228 SAFE_FREE(cli->inbuf);
229 SAFE_FREE(cli->outbuf);
231 if (alloced_cli)
232 SAFE_FREE(cli);
234 return NULL;
237 /****************************************************************************
238 Close a client connection and free the memory without destroying cli itself.
239 ****************************************************************************/
241 void cli_close_connection(struct cli_state *cli)
243 SAFE_FREE(cli->outbuf);
244 SAFE_FREE(cli->inbuf);
246 if (cli->mem_ctx) {
247 talloc_destroy(cli->mem_ctx);
248 cli->mem_ctx = NULL;
251 #ifdef WITH_SSL
252 if (cli->fd != -1)
253 sslutil_disconnect(cli->fd);
254 #endif /* WITH_SSL */
255 if (cli->fd != -1)
256 close(cli->fd);
257 cli->fd = -1;
258 cli->smb_rw_error = 0;
261 /****************************************************************************
262 Shutdown a client structure.
263 ****************************************************************************/
265 void cli_shutdown(struct cli_state *cli)
267 BOOL allocated = cli->allocated;
268 cli_close_connection(cli);
269 ZERO_STRUCTP(cli);
270 if (allocated)
271 SAFE_FREE(cli);
274 /****************************************************************************
275 Set socket options on a open connection.
276 ****************************************************************************/
278 void cli_sockopt(struct cli_state *cli, char *options)
280 set_socket_options(cli->fd, options);
283 /****************************************************************************
284 Set the PID to use for smb messages. Return the old pid.
285 ****************************************************************************/
287 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
289 uint16 ret = cli->pid;
290 cli->pid = pid;
291 return ret;
294 /****************************************************************************
295 Send a keepalive packet to the server.
296 ****************************************************************************/
298 BOOL cli_send_keepalive(struct cli_state *cli)
300 if (cli->fd == -1) {
301 DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
302 return False;
304 if (!send_keepalive(cli->fd)) {
305 close(cli->fd);
306 cli->fd = -1;
307 DEBUG(0,("Error sending keepalive packet to client. (%s)\n",
308 strerror(errno) ));
309 return False;
311 return True;