preparing for release of 2.2.3a
[Samba.git] / source / libsmb / clientgen.c
blobca78ad8dde56632f8c2c07b8ee4d70b42320ae8d
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"
27 * Change the port number used to call on
29 int cli_set_port(struct cli_state *cli, int port)
31 cli->port = port;
32 return port;
35 /****************************************************************************
36 recv an smb
37 ****************************************************************************/
38 BOOL cli_receive_smb(struct cli_state *cli)
40 BOOL ret;
42 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
43 if (cli->fd == -1) return False;
45 again:
46 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
48 if (ret) {
49 /* it might be an oplock break request */
50 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
51 CVAL(cli->inbuf,smb_com) == SMBlockingX &&
52 SVAL(cli->inbuf,smb_vwv6) == 0 &&
53 SVAL(cli->inbuf,smb_vwv7) == 0) {
54 if (cli->oplock_handler) {
55 int fnum = SVAL(cli->inbuf,smb_vwv2);
56 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
57 if (!cli->oplock_handler(cli, fnum, level)) return False;
59 /* try to prevent loops */
60 SCVAL(cli->inbuf,smb_com,0xFF);
61 goto again;
65 /* If the server is not responding, note that now */
67 if (!ret) {
68 close(cli->fd);
69 cli->fd = -1;
72 return ret;
75 /****************************************************************************
76 send an smb to a fd.
77 ****************************************************************************/
79 BOOL cli_send_smb(struct cli_state *cli)
81 size_t len;
82 size_t nwritten=0;
83 ssize_t ret;
85 /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
86 if (cli->fd == -1) return False;
88 len = smb_len(cli->outbuf) + 4;
90 while (nwritten < len) {
91 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
92 if (ret <= 0) {
93 close(cli->fd);
94 cli->fd = -1;
95 DEBUG(0,("Error writing %d bytes to client. %d\n",
96 (int)len,(int)ret));
97 return False;
99 nwritten += ret;
102 return True;
105 /****************************************************************************
106 setup basics in a outgoing packet
107 ****************************************************************************/
108 void cli_setup_packet(struct cli_state *cli)
110 cli->rap_error = 0;
111 SSVAL(cli->outbuf,smb_pid,cli->pid);
112 SSVAL(cli->outbuf,smb_uid,cli->vuid);
113 SSVAL(cli->outbuf,smb_mid,cli->mid);
114 if (cli->protocol > PROTOCOL_CORE) {
115 uint16 flags2;
116 SCVAL(cli->outbuf,smb_flg,0x8);
117 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
118 if (cli->capabilities & CAP_UNICODE) {
119 flags2 |= FLAGS2_UNICODE_STRINGS;
121 if (cli->capabilities & CAP_STATUS32) {
122 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
124 SSVAL(cli->outbuf,smb_flg2, flags2);
128 /****************************************************************************
129 setup the bcc length of the packet from a pointer to the end of the data
130 ****************************************************************************/
131 void cli_setup_bcc(struct cli_state *cli, void *p)
133 set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
138 /****************************************************************************
139 initialise a client structure
140 ****************************************************************************/
141 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
143 /* copy_nt_creds(&cli->usr, usr); */
144 safe_strcpy(cli->domain , usr->domain , sizeof(usr->domain )-1);
145 safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
146 memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
147 cli->ntlmssp_flags = usr->ntlmssp_flags;
148 cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
150 DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
151 cli->user_name, cli->domain,
152 cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
156 /****************************************************************************
157 initialise a client structure
158 ****************************************************************************/
159 struct cli_state *cli_initialise(struct cli_state *cli)
161 BOOL alloced_cli = False;
163 /* Check the effective uid - make sure we are not setuid */
164 if (is_setuid_root()) {
165 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
166 return NULL;
169 if (!cli) {
170 cli = (struct cli_state *)malloc(sizeof(*cli));
171 if (!cli)
172 return NULL;
173 ZERO_STRUCTP(cli);
174 alloced_cli = True;
177 if (cli->initialised) {
178 cli_shutdown(cli);
181 ZERO_STRUCTP(cli);
183 cli->port = 0;
184 cli->fd = -1;
185 cli->cnum = -1;
186 cli->pid = (uint16)sys_getpid();
187 cli->mid = 1;
188 cli->vuid = UID_FIELD_INVALID;
189 cli->protocol = PROTOCOL_NT1;
190 cli->timeout = 20000; /* Timeout is in milliseconds. */
191 cli->bufsize = CLI_BUFFER_SIZE+4;
192 cli->max_xmit = cli->bufsize;
193 cli->outbuf = (char *)malloc(cli->bufsize);
194 cli->inbuf = (char *)malloc(cli->bufsize);
195 cli->oplock_handler = cli_oplock_ack;
196 /* Set the CLI_FORCE_DOSERR environment variable to test
197 client routines using DOS errors instead of STATUS32
198 ones. This intended only as a temporary hack. */
199 if (getenv("CLI_FORCE_DOSERR")) {
200 cli->force_dos_errors = True;
203 if (!cli->outbuf || !cli->inbuf)
204 goto error;
206 if ((cli->mem_ctx = talloc_init()) == NULL)
207 goto error;
209 memset(cli->outbuf, 0, cli->bufsize);
210 memset(cli->inbuf, 0, cli->bufsize);
212 cli->nt_pipe_fnum = 0;
214 cli->initialised = 1;
215 cli->allocated = alloced_cli;
217 return cli;
219 /* Clean up after malloc() error */
221 error:
223 SAFE_FREE(cli->inbuf);
224 SAFE_FREE(cli->outbuf);
226 if (alloced_cli)
227 SAFE_FREE(cli);
229 return NULL;
232 /****************************************************************************
233 shutdown a client structure
234 ****************************************************************************/
235 void cli_shutdown(struct cli_state *cli)
237 BOOL allocated;
238 SAFE_FREE(cli->outbuf);
239 SAFE_FREE(cli->inbuf);
241 if (cli->mem_ctx)
242 talloc_destroy(cli->mem_ctx);
244 #ifdef WITH_SSL
245 if (cli->fd != -1)
246 sslutil_disconnect(cli->fd);
247 #endif /* WITH_SSL */
248 if (cli->fd != -1)
249 close(cli->fd);
250 allocated = cli->allocated;
251 ZERO_STRUCTP(cli);
252 if (allocated) {
253 SAFE_FREE(cli);
258 /****************************************************************************
259 set socket options on a open connection
260 ****************************************************************************/
261 void cli_sockopt(struct cli_state *cli, char *options)
263 set_socket_options(cli->fd, options);
266 /****************************************************************************
267 set the PID to use for smb messages. Return the old pid.
268 ****************************************************************************/
269 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
271 uint16 ret = cli->pid;
272 cli->pid = pid;
273 return ret;