merge from 2.2 in password_ok() to ensure that we check the
[Samba/gbeck.git] / source / libsmb / clientgen.c
blob8e00bca82a9a6909ccab3efc5d332e767456039e
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 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)
35 if (port > 0)
36 cli->port = port;
38 return cli->port;
41 /****************************************************************************
42 recv an smb
43 ****************************************************************************/
44 BOOL cli_receive_smb(struct cli_state *cli)
46 BOOL ret;
47 again:
48 ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
50 if (ret) {
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;
59 goto again;
63 return ret;
66 /****************************************************************************
67 send an smb to a fd and re-establish if necessary
68 ****************************************************************************/
69 BOOL cli_send_smb(struct cli_state *cli)
71 size_t len;
72 size_t nwritten=0;
73 ssize_t ret;
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)) {
82 reestablished = True;
83 nwritten=0;
84 continue;
87 if (ret <= 0) {
88 DEBUG(0,("Error writing %d bytes to client. %d\n",
89 (int)len,(int)ret));
90 return False;
92 nwritten += ret;
95 return True;
98 /****************************************************************************
99 setup basics in a outgoing packet
100 ****************************************************************************/
101 void cli_setup_packet(struct cli_state *cli)
103 cli->rap_error = 0;
104 cli->nt_error = 0;
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) {
109 uint16 flags2;
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;
134 pstring buf;
135 int fnum;
136 unsigned char level;
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)
148 return;
150 cli->outbuf = buf;
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 */
163 else
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 */
169 cli_send_smb(cli);
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)
197 if (!cli) {
198 cli = (struct cli_state *)malloc(sizeof(*cli));
199 if (!cli)
200 return NULL;
201 ZERO_STRUCTP(cli);
204 if (cli->initialised) {
205 cli_shutdown(cli);
208 ZERO_STRUCTP(cli);
210 cli->port = 0;
211 cli->fd = -1;
212 cli->cnum = -1;
213 cli->pid = (uint16)sys_getpid();
214 cli->mid = 1;
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)
224 return NULL;
227 if ((cli->mem_ctx = talloc_init()) == NULL) {
228 free(cli->outbuf);
229 free(cli->inbuf);
230 return 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;
240 return cli;
243 /****************************************************************************
244 shutdown a client structure
245 ****************************************************************************/
246 void cli_shutdown(struct cli_state *cli)
248 if (cli->outbuf)
250 free(cli->outbuf);
252 if (cli->inbuf)
254 free(cli->inbuf);
257 if (cli->mem_ctx)
258 talloc_destroy(cli->mem_ctx);
260 #ifdef WITH_SSL
261 if (cli->fd != -1)
262 sslutil_disconnect(cli->fd);
263 #endif /* WITH_SSL */
264 if (cli->fd != -1)
265 close(cli->fd);
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;
284 cli->pid = pid;
285 return ret;