[137] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | SMB client generic functions
|
---|
| 4 | Copyright (C) Andrew Tridgell 1994-1998
|
---|
| 5 | Copyright (C) Jeremy Allison 2007.
|
---|
| 6 |
|
---|
| 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 3 of the License, or
|
---|
| 10 | (at your option) any later version.
|
---|
| 11 |
|
---|
| 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.
|
---|
| 16 |
|
---|
| 17 | You should have received a copy of the GNU General Public License
|
---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | #include "includes.h"
|
---|
| 22 |
|
---|
| 23 | /*******************************************************************
|
---|
| 24 | Setup the word count and byte count for a client smb message.
|
---|
| 25 | ********************************************************************/
|
---|
| 26 |
|
---|
| 27 | int cli_set_message(char *buf,int num_words,int num_bytes,bool zero)
|
---|
| 28 | {
|
---|
| 29 | if (zero && (num_words || num_bytes)) {
|
---|
| 30 | memset(buf + smb_size,'\0',num_words*2 + num_bytes);
|
---|
| 31 | }
|
---|
| 32 | SCVAL(buf,smb_wct,num_words);
|
---|
| 33 | SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
|
---|
| 34 | smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
|
---|
| 35 | return (smb_size + num_words*2 + num_bytes);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /****************************************************************************
|
---|
| 39 | Change the timeout (in milliseconds).
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
|
---|
| 43 | {
|
---|
| 44 | unsigned int old_timeout = cli->timeout;
|
---|
| 45 | cli->timeout = timeout;
|
---|
| 46 | return old_timeout;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /****************************************************************************
|
---|
| 50 | Change the port number used to call on.
|
---|
| 51 | ****************************************************************************/
|
---|
| 52 |
|
---|
| 53 | int cli_set_port(struct cli_state *cli, int port)
|
---|
| 54 | {
|
---|
| 55 | cli->port = port;
|
---|
| 56 | return port;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /****************************************************************************
|
---|
| 60 | Read an smb from a fd ignoring all keepalive packets.
|
---|
| 61 | The timeout is in milliseconds
|
---|
| 62 |
|
---|
| 63 | This is exactly the same as receive_smb except that it never returns
|
---|
| 64 | a session keepalive packet (just as receive_smb used to do).
|
---|
| 65 | receive_smb was changed to return keepalives as the oplock processing means this call
|
---|
| 66 | should never go into a blocking read.
|
---|
| 67 | ****************************************************************************/
|
---|
| 68 |
|
---|
| 69 | static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
|
---|
| 70 | {
|
---|
| 71 | size_t len;
|
---|
| 72 |
|
---|
| 73 | for(;;) {
|
---|
| 74 | NTSTATUS status;
|
---|
| 75 |
|
---|
| 76 | set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
|
---|
| 77 |
|
---|
| 78 | status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize,
|
---|
| 79 | cli->timeout, maxlen, &len);
|
---|
| 80 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 81 | DEBUG(10,("client_receive_smb failed\n"));
|
---|
| 82 | show_msg(cli->inbuf);
|
---|
| 83 |
|
---|
| 84 | if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
|
---|
| 85 | set_smb_read_error(&cli->smb_rw_error,
|
---|
| 86 | SMB_READ_EOF);
|
---|
| 87 | return -1;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
|
---|
| 91 | set_smb_read_error(&cli->smb_rw_error,
|
---|
| 92 | SMB_READ_TIMEOUT);
|
---|
| 93 | return -1;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
|
---|
| 97 | return -1;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[141] | 100 | /*
|
---|
| 101 | * I don't believe len can be < 0 with NT_STATUS_OK
|
---|
| 102 | * returned above, but this check doesn't hurt. JRA.
|
---|
| 103 | */
|
---|
| 104 |
|
---|
| 105 | if ((ssize_t)len < 0) {
|
---|
[137] | 106 | return len;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /* Ignore session keepalive packets. */
|
---|
| 110 | if(CVAL(cli->inbuf,0) != SMBkeepalive) {
|
---|
| 111 | break;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if (cli_encryption_on(cli)) {
|
---|
| 116 | NTSTATUS status = cli_decrypt_message(cli);
|
---|
| 117 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 118 | DEBUG(0, ("SMB decryption failed on incoming packet! Error %s\n",
|
---|
| 119 | nt_errstr(status)));
|
---|
| 120 | cli->smb_rw_error = SMB_READ_BAD_DECRYPT;
|
---|
| 121 | return -1;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | show_msg(cli->inbuf);
|
---|
| 126 | return len;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /****************************************************************************
|
---|
| 130 | Recv an smb.
|
---|
| 131 | ****************************************************************************/
|
---|
| 132 |
|
---|
| 133 | bool cli_receive_smb(struct cli_state *cli)
|
---|
| 134 | {
|
---|
| 135 | ssize_t len;
|
---|
| 136 |
|
---|
| 137 | /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
|
---|
| 138 | if (cli->fd == -1)
|
---|
| 139 | return false;
|
---|
| 140 |
|
---|
| 141 | again:
|
---|
| 142 | len = client_receive_smb(cli, 0);
|
---|
| 143 |
|
---|
| 144 | if (len > 0) {
|
---|
| 145 | /* it might be an oplock break request */
|
---|
| 146 | if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
|
---|
| 147 | CVAL(cli->inbuf,smb_com) == SMBlockingX &&
|
---|
| 148 | SVAL(cli->inbuf,smb_vwv6) == 0 &&
|
---|
| 149 | SVAL(cli->inbuf,smb_vwv7) == 0) {
|
---|
| 150 | if (cli->oplock_handler) {
|
---|
| 151 | int fnum = SVAL(cli->inbuf,smb_vwv2);
|
---|
| 152 | unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
|
---|
| 153 | if (!cli->oplock_handler(cli, fnum, level)) {
|
---|
| 154 | return false;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | /* try to prevent loops */
|
---|
| 158 | SCVAL(cli->inbuf,smb_com,0xFF);
|
---|
| 159 | goto again;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /* If the server is not responding, note that now */
|
---|
| 164 | if (len < 0) {
|
---|
| 165 | DEBUG(0, ("Receiving SMB: Server stopped responding\n"));
|
---|
| 166 | close(cli->fd);
|
---|
| 167 | cli->fd = -1;
|
---|
| 168 | return false;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | if (!cli_check_sign_mac(cli, cli->inbuf)) {
|
---|
| 172 | /*
|
---|
| 173 | * If we get a signature failure in sessionsetup, then
|
---|
| 174 | * the server sometimes just reflects the sent signature
|
---|
| 175 | * back to us. Detect this and allow the upper layer to
|
---|
| 176 | * retrieve the correct Windows error message.
|
---|
| 177 | */
|
---|
| 178 | if (CVAL(cli->outbuf,smb_com) == SMBsesssetupX &&
|
---|
| 179 | (smb_len(cli->inbuf) > (smb_ss_field + 8 - 4)) &&
|
---|
| 180 | (SVAL(cli->inbuf,smb_flg2) & FLAGS2_SMB_SECURITY_SIGNATURES) &&
|
---|
| 181 | memcmp(&cli->outbuf[smb_ss_field],&cli->inbuf[smb_ss_field],8) == 0 &&
|
---|
| 182 | cli_is_error(cli)) {
|
---|
| 183 |
|
---|
| 184 | /*
|
---|
| 185 | * Reflected signature on login error.
|
---|
| 186 | * Set bad sig but don't close fd.
|
---|
| 187 | */
|
---|
| 188 | cli->smb_rw_error = SMB_READ_BAD_SIG;
|
---|
| 189 | return true;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | DEBUG(0, ("SMB Signature verification failed on incoming packet!\n"));
|
---|
| 193 | cli->smb_rw_error = SMB_READ_BAD_SIG;
|
---|
| 194 | close(cli->fd);
|
---|
| 195 | cli->fd = -1;
|
---|
| 196 | return false;
|
---|
| 197 | };
|
---|
| 198 | return true;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /****************************************************************************
|
---|
| 202 | Read the data portion of a readX smb.
|
---|
| 203 | The timeout is in milliseconds
|
---|
| 204 | ****************************************************************************/
|
---|
| 205 |
|
---|
| 206 | ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
|
---|
| 207 | {
|
---|
| 208 | NTSTATUS status;
|
---|
| 209 |
|
---|
| 210 | set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
|
---|
| 211 |
|
---|
| 212 | status = read_socket_with_timeout(
|
---|
| 213 | cli->fd, buffer, len, len, cli->timeout, NULL);
|
---|
| 214 | if (NT_STATUS_IS_OK(status)) {
|
---|
| 215 | return len;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
|
---|
| 219 | set_smb_read_error(&cli->smb_rw_error, SMB_READ_EOF);
|
---|
| 220 | return -1;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
|
---|
| 224 | set_smb_read_error(&cli->smb_rw_error, SMB_READ_TIMEOUT);
|
---|
| 225 | return -1;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
|
---|
| 229 | return -1;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | static ssize_t write_socket(int fd, const char *buf, size_t len)
|
---|
| 233 | {
|
---|
| 234 | ssize_t ret=0;
|
---|
| 235 |
|
---|
| 236 | DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
|
---|
| 237 | ret = write_data(fd,buf,len);
|
---|
| 238 |
|
---|
| 239 | DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
|
---|
| 240 | if(ret <= 0)
|
---|
| 241 | DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
|
---|
| 242 | (int)len, fd, strerror(errno) ));
|
---|
| 243 |
|
---|
| 244 | return(ret);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /****************************************************************************
|
---|
| 248 | Send an smb to a fd.
|
---|
| 249 | ****************************************************************************/
|
---|
| 250 |
|
---|
| 251 | bool cli_send_smb(struct cli_state *cli)
|
---|
| 252 | {
|
---|
| 253 | size_t len;
|
---|
| 254 | size_t nwritten=0;
|
---|
| 255 | ssize_t ret;
|
---|
| 256 | char *buf_out = cli->outbuf;
|
---|
| 257 | bool enc_on = cli_encryption_on(cli);
|
---|
| 258 |
|
---|
| 259 | /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
|
---|
| 260 | if (cli->fd == -1)
|
---|
| 261 | return false;
|
---|
| 262 |
|
---|
| 263 | cli_calculate_sign_mac(cli, cli->outbuf);
|
---|
| 264 |
|
---|
| 265 | if (enc_on) {
|
---|
| 266 | NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
|
---|
| 267 | &buf_out);
|
---|
| 268 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 269 | close(cli->fd);
|
---|
| 270 | cli->fd = -1;
|
---|
| 271 | cli->smb_rw_error = SMB_WRITE_ERROR;
|
---|
| 272 | DEBUG(0,("Error in encrypting client message. Error %s\n",
|
---|
| 273 | nt_errstr(status) ));
|
---|
| 274 | return false;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | len = smb_len(buf_out) + 4;
|
---|
| 279 |
|
---|
| 280 | while (nwritten < len) {
|
---|
| 281 | ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten);
|
---|
| 282 | if (ret <= 0) {
|
---|
| 283 | if (enc_on) {
|
---|
| 284 | cli_free_enc_buffer(cli, buf_out);
|
---|
| 285 | }
|
---|
| 286 | close(cli->fd);
|
---|
| 287 | cli->fd = -1;
|
---|
| 288 | cli->smb_rw_error = SMB_WRITE_ERROR;
|
---|
| 289 | DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
|
---|
| 290 | (int)len,(int)ret, strerror(errno) ));
|
---|
| 291 | return false;
|
---|
| 292 | }
|
---|
| 293 | nwritten += ret;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | if (enc_on) {
|
---|
| 297 | cli_free_enc_buffer(cli, buf_out);
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | /* Increment the mid so we can tell between responses. */
|
---|
| 301 | cli->mid++;
|
---|
| 302 | if (!cli->mid)
|
---|
| 303 | cli->mid++;
|
---|
| 304 | return true;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /****************************************************************************
|
---|
| 308 | Send a "direct" writeX smb to a fd.
|
---|
| 309 | ****************************************************************************/
|
---|
| 310 |
|
---|
| 311 | bool cli_send_smb_direct_writeX(struct cli_state *cli,
|
---|
| 312 | const char *p,
|
---|
| 313 | size_t extradata)
|
---|
| 314 | {
|
---|
| 315 | /* First length to send is the offset to the data. */
|
---|
| 316 | size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
|
---|
| 317 | size_t nwritten=0;
|
---|
[232] | 318 | struct iovec iov[2];
|
---|
[137] | 319 |
|
---|
| 320 | /* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
|
---|
| 321 | if (cli->fd == -1) {
|
---|
| 322 | return false;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | if (client_is_signing_on(cli)) {
|
---|
| 326 | DEBUG(0,("cli_send_smb_large: cannot send signed packet.\n"));
|
---|
| 327 | return false;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[232] | 330 | iov[0].iov_base = cli->outbuf;
|
---|
| 331 | iov[0].iov_len = len;
|
---|
| 332 | iov[1].iov_base = CONST_DISCARD(char *, p);
|
---|
| 333 | iov[1].iov_len = extradata;
|
---|
[137] | 334 |
|
---|
[232] | 335 | nwritten = write_data_iov(cli->fd, iov, 2);
|
---|
| 336 | if (nwritten < (len + extradata)) {
|
---|
| 337 | close(cli->fd);
|
---|
| 338 | cli->fd = -1;
|
---|
| 339 | cli->smb_rw_error = SMB_WRITE_ERROR;
|
---|
| 340 | DEBUG(0,("Error writing %d bytes to client. (%s)\n",
|
---|
| 341 | (int)(len+extradata), strerror(errno)));
|
---|
| 342 | return false;
|
---|
[137] | 343 | }
|
---|
| 344 |
|
---|
| 345 | /* Increment the mid so we can tell between responses. */
|
---|
| 346 | cli->mid++;
|
---|
| 347 | if (!cli->mid)
|
---|
| 348 | cli->mid++;
|
---|
| 349 | return true;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /****************************************************************************
|
---|
| 353 | Setup basics in a outgoing packet.
|
---|
| 354 | ****************************************************************************/
|
---|
| 355 |
|
---|
| 356 | void cli_setup_packet_buf(struct cli_state *cli, char *buf)
|
---|
| 357 | {
|
---|
| 358 | uint16 flags2;
|
---|
| 359 | cli->rap_error = 0;
|
---|
| 360 | SIVAL(buf,smb_rcls,0);
|
---|
| 361 | SSVAL(buf,smb_pid,cli->pid);
|
---|
| 362 | memset(buf+smb_pidhigh, 0, 12);
|
---|
| 363 | SSVAL(buf,smb_uid,cli->vuid);
|
---|
| 364 | SSVAL(buf,smb_mid,cli->mid);
|
---|
| 365 |
|
---|
| 366 | if (cli->protocol <= PROTOCOL_CORE) {
|
---|
| 367 | return;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | if (cli->case_sensitive) {
|
---|
| 371 | SCVAL(buf,smb_flg,0x0);
|
---|
| 372 | } else {
|
---|
| 373 | /* Default setting, case insensitive. */
|
---|
| 374 | SCVAL(buf,smb_flg,0x8);
|
---|
| 375 | }
|
---|
| 376 | flags2 = FLAGS2_LONG_PATH_COMPONENTS;
|
---|
| 377 | if (cli->capabilities & CAP_UNICODE)
|
---|
| 378 | flags2 |= FLAGS2_UNICODE_STRINGS;
|
---|
| 379 | if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
|
---|
| 380 | flags2 |= FLAGS2_DFS_PATHNAMES;
|
---|
| 381 | if (cli->capabilities & CAP_STATUS32)
|
---|
| 382 | flags2 |= FLAGS2_32_BIT_ERROR_CODES;
|
---|
| 383 | if (cli->use_spnego)
|
---|
| 384 | flags2 |= FLAGS2_EXTENDED_SECURITY;
|
---|
| 385 | SSVAL(buf,smb_flg2, flags2);
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | void cli_setup_packet(struct cli_state *cli)
|
---|
| 389 | {
|
---|
| 390 | cli_setup_packet_buf(cli, cli->outbuf);
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /****************************************************************************
|
---|
| 394 | Setup the bcc length of the packet from a pointer to the end of the data.
|
---|
| 395 | ****************************************************************************/
|
---|
| 396 |
|
---|
| 397 | void cli_setup_bcc(struct cli_state *cli, void *p)
|
---|
| 398 | {
|
---|
| 399 | set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | /****************************************************************************
|
---|
| 403 | Initialise credentials of a client structure.
|
---|
| 404 | ****************************************************************************/
|
---|
| 405 |
|
---|
| 406 | void cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
|
---|
| 407 | {
|
---|
| 408 | fstrcpy(cli->domain, domain);
|
---|
| 409 | fstrcpy(cli->user_name, username);
|
---|
| 410 | pwd_set_cleartext(&cli->pwd, password);
|
---|
| 411 | if (!*username) {
|
---|
| 412 | cli->pwd.null_pwd = true;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | /****************************************************************************
|
---|
| 419 | Set the signing state (used from the command line).
|
---|
| 420 | ****************************************************************************/
|
---|
| 421 |
|
---|
| 422 | void cli_setup_signing_state(struct cli_state *cli, int signing_state)
|
---|
| 423 | {
|
---|
| 424 | if (signing_state == Undefined)
|
---|
| 425 | return;
|
---|
| 426 |
|
---|
| 427 | if (signing_state == false) {
|
---|
| 428 | cli->sign_info.allow_smb_signing = false;
|
---|
| 429 | cli->sign_info.mandatory_signing = false;
|
---|
| 430 | return;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | cli->sign_info.allow_smb_signing = true;
|
---|
| 434 |
|
---|
| 435 | if (signing_state == Required)
|
---|
| 436 | cli->sign_info.mandatory_signing = true;
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | /****************************************************************************
|
---|
| 440 | Initialise a client structure. Always returns a malloc'ed struct.
|
---|
| 441 | ****************************************************************************/
|
---|
| 442 |
|
---|
| 443 | struct cli_state *cli_initialise(void)
|
---|
| 444 | {
|
---|
| 445 | struct cli_state *cli = NULL;
|
---|
| 446 |
|
---|
| 447 | #ifndef __OS2__
|
---|
| 448 | /* Check the effective uid - make sure we are not setuid */
|
---|
| 449 | if (is_setuid_root()) {
|
---|
| 450 | DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
|
---|
| 451 | return NULL;
|
---|
| 452 | }
|
---|
| 453 | #endif
|
---|
| 454 | cli = talloc(NULL, struct cli_state);
|
---|
| 455 | if (!cli) {
|
---|
| 456 | return NULL;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | ZERO_STRUCTP(cli);
|
---|
| 460 |
|
---|
| 461 | cli->port = 0;
|
---|
| 462 | cli->fd = -1;
|
---|
| 463 | cli->cnum = -1;
|
---|
| 464 | cli->pid = (uint16)sys_getpid();
|
---|
| 465 | cli->mid = 1;
|
---|
| 466 | cli->vuid = UID_FIELD_INVALID;
|
---|
| 467 | cli->protocol = PROTOCOL_NT1;
|
---|
| 468 | cli->timeout = 20000; /* Timeout is in milliseconds. */
|
---|
| 469 | cli->bufsize = CLI_BUFFER_SIZE+4;
|
---|
| 470 | cli->max_xmit = cli->bufsize;
|
---|
| 471 | cli->outbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
|
---|
| 472 | cli->inbuf = (char *)SMB_MALLOC(cli->bufsize+SAFETY_MARGIN);
|
---|
| 473 | cli->oplock_handler = cli_oplock_ack;
|
---|
| 474 | cli->case_sensitive = false;
|
---|
| 475 | cli->smb_rw_error = SMB_READ_OK;
|
---|
| 476 |
|
---|
| 477 | cli->use_spnego = lp_client_use_spnego();
|
---|
| 478 |
|
---|
| 479 | cli->capabilities = CAP_UNICODE | CAP_STATUS32 | CAP_DFS;
|
---|
| 480 |
|
---|
| 481 | /* Set the CLI_FORCE_DOSERR environment variable to test
|
---|
| 482 | client routines using DOS errors instead of STATUS32
|
---|
| 483 | ones. This intended only as a temporary hack. */
|
---|
| 484 | if (getenv("CLI_FORCE_DOSERR"))
|
---|
| 485 | cli->force_dos_errors = true;
|
---|
| 486 |
|
---|
| 487 | if (lp_client_signing())
|
---|
| 488 | cli->sign_info.allow_smb_signing = true;
|
---|
| 489 |
|
---|
| 490 | if (lp_client_signing() == Required)
|
---|
| 491 | cli->sign_info.mandatory_signing = true;
|
---|
| 492 |
|
---|
| 493 | if (!cli->outbuf || !cli->inbuf)
|
---|
| 494 | goto error;
|
---|
| 495 |
|
---|
| 496 | memset(cli->outbuf, 0, cli->bufsize);
|
---|
| 497 | memset(cli->inbuf, 0, cli->bufsize);
|
---|
| 498 |
|
---|
| 499 |
|
---|
| 500 | #if defined(DEVELOPER)
|
---|
| 501 | /* just because we over-allocate, doesn't mean it's right to use it */
|
---|
| 502 | clobber_region(FUNCTION_MACRO, __LINE__, cli->outbuf+cli->bufsize, SAFETY_MARGIN);
|
---|
| 503 | clobber_region(FUNCTION_MACRO, __LINE__, cli->inbuf+cli->bufsize, SAFETY_MARGIN);
|
---|
| 504 | #endif
|
---|
| 505 |
|
---|
| 506 | /* initialise signing */
|
---|
| 507 | cli_null_set_signing(cli);
|
---|
| 508 |
|
---|
| 509 | cli->initialised = 1;
|
---|
| 510 |
|
---|
| 511 | return cli;
|
---|
| 512 |
|
---|
| 513 | /* Clean up after malloc() error */
|
---|
| 514 |
|
---|
| 515 | error:
|
---|
| 516 |
|
---|
| 517 | SAFE_FREE(cli->inbuf);
|
---|
| 518 | SAFE_FREE(cli->outbuf);
|
---|
[233] | 519 | TALLOC_FREE(cli);
|
---|
[137] | 520 | return NULL;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | /****************************************************************************
|
---|
| 524 | External interface.
|
---|
| 525 | Close an open named pipe over SMB. Free any authentication data.
|
---|
| 526 | Returns false if the cli_close call failed.
|
---|
| 527 | ****************************************************************************/
|
---|
| 528 |
|
---|
| 529 | bool cli_rpc_pipe_close(struct rpc_pipe_client *cli)
|
---|
| 530 | {
|
---|
| 531 | bool ret;
|
---|
| 532 |
|
---|
| 533 | if (!cli) {
|
---|
| 534 | return false;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | ret = cli_close(cli->cli, cli->fnum);
|
---|
| 538 |
|
---|
| 539 | if (!ret) {
|
---|
| 540 | DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
|
---|
| 541 | "fnum 0x%x "
|
---|
| 542 | "to machine %s. Error was %s\n",
|
---|
| 543 | cli->pipe_name,
|
---|
| 544 | (int) cli->fnum,
|
---|
| 545 | cli->cli->desthost,
|
---|
| 546 | cli_errstr(cli->cli)));
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | if (cli->auth.cli_auth_data_free_func) {
|
---|
| 550 | (*cli->auth.cli_auth_data_free_func)(&cli->auth);
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
|
---|
| 554 | cli->pipe_name, cli->cli->desthost ));
|
---|
| 555 |
|
---|
| 556 | DLIST_REMOVE(cli->cli->pipe_list, cli);
|
---|
| 557 | talloc_destroy(cli->mem_ctx);
|
---|
| 558 | return ret;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | /****************************************************************************
|
---|
| 562 | Close all pipes open on this session.
|
---|
| 563 | ****************************************************************************/
|
---|
| 564 |
|
---|
| 565 | void cli_nt_pipes_close(struct cli_state *cli)
|
---|
| 566 | {
|
---|
| 567 | struct rpc_pipe_client *cp, *next;
|
---|
| 568 |
|
---|
| 569 | for (cp = cli->pipe_list; cp; cp = next) {
|
---|
| 570 | next = cp->next;
|
---|
| 571 | cli_rpc_pipe_close(cp);
|
---|
| 572 | }
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | /****************************************************************************
|
---|
| 576 | Shutdown a client structure.
|
---|
| 577 | ****************************************************************************/
|
---|
| 578 |
|
---|
| 579 | void cli_shutdown(struct cli_state *cli)
|
---|
| 580 | {
|
---|
| 581 | cli_nt_pipes_close(cli);
|
---|
| 582 |
|
---|
| 583 | /*
|
---|
| 584 | * tell our peer to free his resources. Wihtout this, when an
|
---|
| 585 | * application attempts to do a graceful shutdown and calls
|
---|
| 586 | * smbc_free_context() to clean up all connections, some connections
|
---|
| 587 | * can remain active on the peer end, until some (long) timeout period
|
---|
| 588 | * later. This tree disconnect forces the peer to clean up, since the
|
---|
| 589 | * connection will be going away.
|
---|
| 590 | *
|
---|
| 591 | * Also, do not do tree disconnect when cli->smb_rw_error is SMB_DO_NOT_DO_TDIS
|
---|
| 592 | * the only user for this so far is smbmount which passes opened connection
|
---|
| 593 | * down to kernel's smbfs module.
|
---|
| 594 | */
|
---|
| 595 | if ( (cli->cnum != (uint16)-1) && (cli->smb_rw_error != SMB_DO_NOT_DO_TDIS ) ) {
|
---|
| 596 | cli_tdis(cli);
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | SAFE_FREE(cli->outbuf);
|
---|
| 600 | SAFE_FREE(cli->inbuf);
|
---|
| 601 |
|
---|
| 602 | cli_free_signing_context(cli);
|
---|
| 603 | data_blob_free(&cli->secblob);
|
---|
| 604 | data_blob_free(&cli->user_session_key);
|
---|
| 605 |
|
---|
| 606 | if (cli->fd != -1) {
|
---|
| 607 | close(cli->fd);
|
---|
| 608 | }
|
---|
| 609 | cli->fd = -1;
|
---|
| 610 | cli->smb_rw_error = SMB_READ_OK;
|
---|
| 611 |
|
---|
| 612 | TALLOC_FREE(cli);
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | /****************************************************************************
|
---|
| 616 | Set socket options on a open connection.
|
---|
| 617 | ****************************************************************************/
|
---|
| 618 |
|
---|
| 619 | void cli_sockopt(struct cli_state *cli, const char *options)
|
---|
| 620 | {
|
---|
| 621 | set_socket_options(cli->fd, options);
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | /****************************************************************************
|
---|
| 625 | Set the PID to use for smb messages. Return the old pid.
|
---|
| 626 | ****************************************************************************/
|
---|
| 627 |
|
---|
| 628 | uint16 cli_setpid(struct cli_state *cli, uint16 pid)
|
---|
| 629 | {
|
---|
| 630 | uint16 ret = cli->pid;
|
---|
| 631 | cli->pid = pid;
|
---|
| 632 | return ret;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | /****************************************************************************
|
---|
| 636 | Set the case sensitivity flag on the packets. Returns old state.
|
---|
| 637 | ****************************************************************************/
|
---|
| 638 |
|
---|
| 639 | bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
|
---|
| 640 | {
|
---|
| 641 | bool ret = cli->case_sensitive;
|
---|
| 642 | cli->case_sensitive = case_sensitive;
|
---|
| 643 | return ret;
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | /****************************************************************************
|
---|
| 647 | Send a keepalive packet to the server
|
---|
| 648 | ****************************************************************************/
|
---|
| 649 |
|
---|
| 650 | bool cli_send_keepalive(struct cli_state *cli)
|
---|
| 651 | {
|
---|
| 652 | if (cli->fd == -1) {
|
---|
| 653 | DEBUG(3, ("cli_send_keepalive: fd == -1\n"));
|
---|
| 654 | return false;
|
---|
| 655 | }
|
---|
| 656 | if (!send_keepalive(cli->fd)) {
|
---|
| 657 | close(cli->fd);
|
---|
| 658 | cli->fd = -1;
|
---|
| 659 | DEBUG(0,("Error sending keepalive packet to client.\n"));
|
---|
| 660 | return false;
|
---|
| 661 | }
|
---|
| 662 | return true;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | /****************************************************************************
|
---|
| 666 | Send/receive a SMBecho command: ping the server
|
---|
| 667 | ****************************************************************************/
|
---|
| 668 |
|
---|
| 669 | bool cli_echo(struct cli_state *cli, uint16 num_echos,
|
---|
| 670 | unsigned char *data, size_t length)
|
---|
| 671 | {
|
---|
| 672 | char *p;
|
---|
| 673 | int i;
|
---|
| 674 |
|
---|
| 675 | SMB_ASSERT(length < 1024);
|
---|
| 676 |
|
---|
| 677 | memset(cli->outbuf,'\0',smb_size);
|
---|
| 678 | cli_set_message(cli->outbuf,1,length,true);
|
---|
| 679 | SCVAL(cli->outbuf,smb_com,SMBecho);
|
---|
| 680 | SSVAL(cli->outbuf,smb_tid,65535);
|
---|
| 681 | SSVAL(cli->outbuf,smb_vwv0,num_echos);
|
---|
| 682 | cli_setup_packet(cli);
|
---|
| 683 | p = smb_buf(cli->outbuf);
|
---|
| 684 | memcpy(p, data, length);
|
---|
| 685 | p += length;
|
---|
| 686 |
|
---|
| 687 | cli_setup_bcc(cli, p);
|
---|
| 688 |
|
---|
| 689 | cli_send_smb(cli);
|
---|
| 690 |
|
---|
| 691 | for (i=0; i<num_echos; i++) {
|
---|
| 692 | if (!cli_receive_smb(cli)) {
|
---|
| 693 | return false;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | if (cli_is_error(cli)) {
|
---|
| 697 | return false;
|
---|
| 698 | }
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | return true;
|
---|
| 702 | }
|
---|