| 1 |
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
Copyright (C) 2016 by Ronnie Sahlberg <[email protected]> |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as published by |
| 7 |
the Free Software Foundation; either version 2.1 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
#ifdef HAVE_CONFIG_H |
| 19 |
#include "config.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifndef _GNU_SOURCE |
| 23 |
#define _GNU_SOURCE |
| 24 |
#endif |
| 25 |
|
| 26 |
#ifdef HAVE_STDINT_H |
| 27 |
#include <stdint.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#ifdef HAVE_STDLIB_H |
| 31 |
#include <stdlib.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef HAVE_STRING_H |
| 35 |
#include <string.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef STDC_HEADERS |
| 39 |
#include <stddef.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef HAVE_TIME_H |
| 43 |
#include <time.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef HAVE_SYS_TIME_H |
| 47 |
#include <sys/time.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include <errno.h> |
| 51 |
|
| 52 |
#include "compat.h" |
| 53 |
|
| 54 |
#include "smb2.h" |
| 55 |
#include "libsmb2.h" |
| 56 |
#include "libsmb2-private.h" |
| 57 |
|
| 58 |
static int |
| 59 |
smb2_encode_preauth_context(struct smb2_context *smb2, struct smb2_pdu *pdu) |
| 60 |
{ |
| 61 |
uint8_t *buf; |
| 62 |
int len, i, data_len; |
| 63 |
struct smb2_iovec *iov; |
| 64 |
|
| 65 |
/* Preauth integrity capability */ |
| 66 |
data_len = 38; |
| 67 |
len = 8 + PAD_TO_64BIT(38); |
| 68 |
buf = malloc(len); |
| 69 |
if (buf == NULL) { |
| 70 |
smb2_set_error(smb2, "Failed to allocate preauth context"); |
| 71 |
return -1; |
| 72 |
} |
| 73 |
memset(buf, 0, len); |
| 74 |
|
| 75 |
iov = smb2_add_iovector(smb2, &pdu->out, buf, len, free); |
| 76 |
smb2_set_uint16(iov, 0, SMB2_PREAUTH_INTEGRITY_CAP); |
| 77 |
smb2_set_uint16(iov, 2, data_len); |
| 78 |
smb2_set_uint16(iov, 8, 1); |
| 79 |
smb2_set_uint16(iov, 10, 32); |
| 80 |
smb2_set_uint16(iov, 12, SMB2_HASH_SHA_512); |
| 81 |
|
| 82 |
for (i = 0; i < SMB2_SALT_SIZE; i++) { |
| 83 |
smb2_set_uint8(iov, 14 + i, smb2->salt[i]); |
| 84 |
} |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
static int |
| 89 |
smb2_encode_encryption_context(struct smb2_context *smb2, struct smb2_pdu *pdu) |
| 90 |
{ |
| 91 |
uint8_t *buf; |
| 92 |
int len, data_len; |
| 93 |
struct smb2_iovec *iov; |
| 94 |
|
| 95 |
data_len = PAD_TO_64BIT(4); |
| 96 |
len = 8 + data_len; |
| 97 |
len = PAD_TO_64BIT(len); |
| 98 |
buf = malloc(len); |
| 99 |
if (buf == NULL) { |
| 100 |
smb2_set_error(smb2, "Failed to allocate encryption context"); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
memset(buf, 0, len); |
| 104 |
|
| 105 |
iov = smb2_add_iovector(smb2, &pdu->out, buf, len, free); |
| 106 |
smb2_set_uint16(iov, 0, SMB2_ENCRYPTION_CAP); |
| 107 |
smb2_set_uint16(iov, 2, data_len); |
| 108 |
smb2_set_uint16(iov, 8, 1); |
| 109 |
smb2_set_uint16(iov, 10, SMB2_ENCRYPTION_AES_128_CCM); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
static int |
| 115 |
smb2_encode_negotiate_request(struct smb2_context *smb2, |
| 116 |
struct smb2_pdu *pdu, |
| 117 |
struct smb2_negotiate_request *req) |
| 118 |
{ |
| 119 |
uint8_t *buf; |
| 120 |
int i, len; |
| 121 |
struct smb2_iovec *iov; |
| 122 |
|
| 123 |
len = SMB2_NEGOTIATE_REQUEST_SIZE + |
| 124 |
req->dialect_count * sizeof(uint16_t); |
| 125 |
len = PAD_TO_32BIT(len); |
| 126 |
if (smb2->version == SMB2_VERSION_ANY || |
| 127 |
smb2->version == SMB2_VERSION_ANY3 || |
| 128 |
smb2->version == SMB2_VERSION_0311) { |
| 129 |
/* Negotiate contexts are aligned at 64bit boundaries */ |
| 130 |
if (len & 0x04) { |
| 131 |
len += 4; |
| 132 |
} |
| 133 |
} |
| 134 |
buf = calloc(len, sizeof(uint8_t)); |
| 135 |
if (buf == NULL) { |
| 136 |
smb2_set_error(smb2, "Failed to allocate negotiate buffer"); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
iov = smb2_add_iovector(smb2, &pdu->out, buf, len, free); |
| 141 |
|
| 142 |
if (smb2->version == SMB2_VERSION_ANY || |
| 143 |
smb2->version == SMB2_VERSION_ANY3 || |
| 144 |
smb2->version == SMB2_VERSION_0311) { |
| 145 |
req->negotiate_context_offset = len + SMB2_HEADER_SIZE; |
| 146 |
|
| 147 |
if (smb2_encode_preauth_context(smb2, pdu)) { |
| 148 |
return -1; |
| 149 |
} |
| 150 |
req->negotiate_context_count++; |
| 151 |
|
| 152 |
if (smb2_encode_encryption_context(smb2, pdu)) { |
| 153 |
return -1; |
| 154 |
} |
| 155 |
req->negotiate_context_count++; |
| 156 |
} |
| 157 |
|
| 158 |
smb2_set_uint16(iov, 0, SMB2_NEGOTIATE_REQUEST_SIZE); |
| 159 |
smb2_set_uint16(iov, 2, req->dialect_count); |
| 160 |
smb2_set_uint16(iov, 4, req->security_mode); |
| 161 |
smb2_set_uint32(iov, 8, req->capabilities); |
| 162 |
memcpy(iov->buf + 12, req->client_guid, SMB2_GUID_SIZE); |
| 163 |
smb2_set_uint32(iov, 28, req->negotiate_context_offset); |
| 164 |
smb2_set_uint16(iov, 32, req->negotiate_context_count); |
| 165 |
for (i = 0; i < req->dialect_count; i++) { |
| 166 |
smb2_set_uint16(iov, 36 + i * sizeof(uint16_t), |
| 167 |
req->dialects[i]); |
| 168 |
} |
| 169 |
|
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
struct smb2_pdu * |
| 174 |
smb2_cmd_negotiate_async(struct smb2_context *smb2, |
| 175 |
struct smb2_negotiate_request *req, |
| 176 |
smb2_command_cb cb, void *cb_data) |
| 177 |
{ |
| 178 |
struct smb2_pdu *pdu; |
| 179 |
|
| 180 |
pdu = smb2_allocate_pdu(smb2, SMB2_NEGOTIATE, cb, cb_data); |
| 181 |
if (pdu == NULL) { |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
if (smb2_encode_negotiate_request(smb2, pdu, req)) { |
| 186 |
smb2_free_pdu(smb2, pdu); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
if (smb2_pad_to_64bit(smb2, &pdu->out) != 0) { |
| 191 |
smb2_free_pdu(smb2, pdu); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
return pdu; |
| 196 |
} |
| 197 |
|
| 198 |
static int |
| 199 |
smb2_encode_negotiate_reply(struct smb2_context *smb2, |
| 200 |
struct smb2_pdu *pdu, |
| 201 |
struct smb2_negotiate_reply *rep) |
| 202 |
{ |
| 203 |
uint8_t *buf; |
| 204 |
int len, seclen; |
| 205 |
struct smb2_iovec *iov; |
| 206 |
|
| 207 |
len = SMB2_NEGOTIATE_REPLY_SIZE & 0xfffe; |
| 208 |
len = PAD_TO_32BIT(len); |
| 209 |
if (smb2->dialect == SMB2_VERSION_ANY || |
| 210 |
smb2->dialect == SMB2_VERSION_ANY3 || |
| 211 |
smb2->dialect == SMB2_VERSION_0311) { |
| 212 |
/* Negotiate contexts are aligned at 64bit boundaries */ |
| 213 |
if (len & 0x04) { |
| 214 |
len += 4; |
| 215 |
} |
| 216 |
} |
| 217 |
buf = calloc(len, sizeof(uint8_t)); |
| 218 |
if (buf == NULL) { |
| 219 |
smb2_set_error(smb2, "Failed to allocate negotiate reply buffer"); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
iov = smb2_add_iovector(smb2, &pdu->out, buf, len, free); |
| 224 |
|
| 225 |
if (rep->security_buffer_length) { |
| 226 |
seclen = rep->security_buffer_length; |
| 227 |
seclen = PAD_TO_64BIT(len); |
| 228 |
/* Security buffer */ |
| 229 |
buf = malloc(seclen); |
| 230 |
if (buf == NULL) { |
| 231 |
smb2_set_error(smb2, "Failed to allocate secbuf"); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
memcpy(buf, rep->security_buffer, rep->security_buffer_length); |
| 235 |
memset(buf + rep->security_buffer_length, 0, seclen - rep->security_buffer_length); |
| 236 |
smb2_add_iovector(smb2, &pdu->out, |
| 237 |
buf, |
| 238 |
len, |
| 239 |
free); |
| 240 |
} |
| 241 |
|
| 242 |
if (smb2->dialect == SMB2_VERSION_ANY || |
| 243 |
smb2->dialect == SMB2_VERSION_ANY3 || |
| 244 |
smb2->dialect == SMB2_VERSION_0311) { |
| 245 |
if (smb2_encode_preauth_context(smb2, pdu)) { |
| 246 |
return -1; |
| 247 |
} |
| 248 |
rep->negotiate_context_count++; |
| 249 |
|
| 250 |
if (smb2_encode_encryption_context(smb2, pdu)) { |
| 251 |
return -1; |
| 252 |
} |
| 253 |
rep->negotiate_context_count++; |
| 254 |
} |
| 255 |
|
| 256 |
smb2_set_uint16(iov, 0, SMB2_NEGOTIATE_REPLY_SIZE); |
| 257 |
smb2_set_uint16(iov, 2, rep->security_mode); |
| 258 |
smb2_set_uint16(iov, 4, rep->dialect_revision); |
| 259 |
smb2_set_uint16(iov, 6, rep->negotiate_context_count); |
| 260 |
memcpy(iov->buf + 8, rep->server_guid, SMB2_GUID_SIZE); |
| 261 |
smb2_set_uint32(iov, 24, rep->capabilities); |
| 262 |
smb2_set_uint32(iov, 28, rep->max_transact_size); |
| 263 |
smb2_set_uint32(iov, 32, rep->max_read_size); |
| 264 |
smb2_set_uint32(iov, 36, rep->max_write_size); |
| 265 |
smb2_set_uint64(iov, 40, rep->system_time); |
| 266 |
smb2_set_uint64(iov, 48, rep->server_start_time); |
| 267 |
|
| 268 |
rep->security_buffer_offset = len + SMB2_HEADER_SIZE; |
| 269 |
smb2_set_uint16(iov, 56, rep->security_buffer_offset); |
| 270 |
smb2_set_uint16(iov, 58, rep->security_buffer_length); |
| 271 |
rep->negotiate_context_offset = len + seclen + SMB2_HEADER_SIZE; |
| 272 |
smb2_set_uint32(iov, 60, rep->negotiate_context_offset); |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
struct smb2_pdu * |
| 278 |
smb2_cmd_negotiate_reply_async(struct smb2_context *smb2, |
| 279 |
struct smb2_negotiate_reply *rep, |
| 280 |
smb2_command_cb cb, void *cb_data) |
| 281 |
{ |
| 282 |
struct smb2_pdu *pdu; |
| 283 |
|
| 284 |
pdu = smb2_allocate_pdu(smb2, SMB2_NEGOTIATE, cb, cb_data); |
| 285 |
if (pdu == NULL) { |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
if (smb2_encode_negotiate_reply(smb2, pdu, rep)) { |
| 290 |
smb2_free_pdu(smb2, pdu); |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
|
| 294 |
if (smb2_pad_to_64bit(smb2, &pdu->out) != 0) { |
| 295 |
smb2_free_pdu(smb2, pdu); |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
|
| 299 |
return pdu; |
| 300 |
} |
| 301 |
|
| 302 |
#define IOV_OFFSET (rep->security_buffer_offset - SMB2_HEADER_SIZE - \ |
| 303 |
(SMB2_NEGOTIATE_REPLY_SIZE & 0xfffe)) |
| 304 |
|
| 305 |
static int |
| 306 |
smb2_parse_encryption_context(struct smb2_context *smb2, |
| 307 |
struct smb2_negotiate_reply *rep, |
| 308 |
struct smb2_iovec *iov, |
| 309 |
int offset) |
| 310 |
{ |
| 311 |
smb2_get_uint16(iov, offset, &rep->cypher); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
static int |
| 316 |
smb2_parse_negotiate_contexts(struct smb2_context *smb2, |
| 317 |
struct smb2_negotiate_reply *rep, |
| 318 |
struct smb2_iovec *iov, |
| 319 |
int offset, int count) |
| 320 |
{ |
| 321 |
uint16_t type, len; |
| 322 |
|
| 323 |
while (count--) { |
| 324 |
if (offset > (int)iov->len) { |
| 325 |
smb2_set_error(smb2, "Bad len in negotiate context\n"); |
| 326 |
return -1; |
| 327 |
} |
| 328 |
smb2_get_uint16(iov, offset, &type); |
| 329 |
smb2_get_uint16(iov, offset + 2, &len); |
| 330 |
|
| 331 |
switch (type) { |
| 332 |
case SMB2_PREAUTH_INTEGRITY_CAP: |
| 333 |
break; |
| 334 |
case SMB2_ENCRYPTION_CAP: |
| 335 |
if (smb2_parse_encryption_context(smb2, rep, |
| 336 |
iov, offset + 8)) { |
| 337 |
return -1; |
| 338 |
} |
| 339 |
break; |
| 340 |
case SMB2_SIGNING_CAP: |
| 341 |
case SMB2_COMPRESSION_CAP: |
| 342 |
case SMB2_NETNAME_NEGOTIATE_CONTEXT_ID: |
| 343 |
case SMB2_TRANSPORT_CAP: |
| 344 |
case SMB2_RDMA_TRANSFORM_CAP: |
| 345 |
break; |
| 346 |
default: |
| 347 |
smb2_set_error(smb2, "Unknown negotiate context " |
| 348 |
"type 0x%04x", type); |
| 349 |
return -1; |
| 350 |
} |
| 351 |
offset += PAD_TO_64BIT(len + 8); |
| 352 |
} |
| 353 |
|
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
int |
| 358 |
smb2_process_negotiate_fixed(struct smb2_context *smb2, |
| 359 |
struct smb2_pdu *pdu) |
| 360 |
{ |
| 361 |
struct smb2_negotiate_reply *rep; |
| 362 |
struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1]; |
| 363 |
uint16_t struct_size; |
| 364 |
|
| 365 |
smb2_get_uint16(iov, 0, &struct_size); |
| 366 |
if (struct_size != SMB2_NEGOTIATE_REPLY_SIZE || |
| 367 |
(struct_size & 0xfffe) != iov->len) { |
| 368 |
smb2_set_error(smb2, "Unexpected size of Negotiate " |
| 369 |
"reply. Expected %d, got %d", |
| 370 |
SMB2_NEGOTIATE_REPLY_SIZE, |
| 371 |
(int)iov->len); |
| 372 |
return -1; |
| 373 |
} |
| 374 |
|
| 375 |
rep = malloc(sizeof(*rep)); |
| 376 |
if (rep == NULL) { |
| 377 |
smb2_set_error(smb2, "Failed to allocate negotiate reply"); |
| 378 |
return -1; |
| 379 |
} |
| 380 |
|
| 381 |
pdu->payload = rep; |
| 382 |
|
| 383 |
smb2_get_uint16(iov, 2, &rep->security_mode); |
| 384 |
smb2_get_uint16(iov, 4, &rep->dialect_revision); |
| 385 |
memcpy(rep->server_guid, iov->buf + 8, SMB2_GUID_SIZE); |
| 386 |
smb2_get_uint32(iov, 24, &rep->capabilities); |
| 387 |
smb2_get_uint32(iov, 28, &rep->max_transact_size); |
| 388 |
smb2_get_uint32(iov, 32, &rep->max_read_size); |
| 389 |
smb2_get_uint32(iov, 36, &rep->max_write_size); |
| 390 |
smb2_get_uint64(iov, 40, &rep->system_time); |
| 391 |
smb2_get_uint64(iov, 48, &rep->server_start_time); |
| 392 |
smb2_get_uint16(iov, 56, &rep->security_buffer_offset); |
| 393 |
smb2_get_uint16(iov, 58, &rep->security_buffer_length); |
| 394 |
|
| 395 |
if (rep->security_buffer_length && |
| 396 |
(rep->security_buffer_offset + rep->security_buffer_length > (uint16_t)smb2->spl)) { |
| 397 |
smb2_set_error(smb2, "Security buffer extends beyond end of " |
| 398 |
"PDU"); |
| 399 |
pdu->payload = NULL; |
| 400 |
free(rep); |
| 401 |
return -1; |
| 402 |
} |
| 403 |
smb2_get_uint16(iov, 6, &rep->negotiate_context_count); |
| 404 |
smb2_get_uint32(iov, 60, &rep->negotiate_context_offset); |
| 405 |
|
| 406 |
if (rep->security_buffer_length == 0) { |
| 407 |
return 0; |
| 408 |
} |
| 409 |
if (rep->security_buffer_offset < SMB2_HEADER_SIZE + |
| 410 |
(SMB2_NEGOTIATE_REPLY_SIZE & 0xfffe)) { |
| 411 |
smb2_set_error(smb2, "Security buffer overlaps with " |
| 412 |
"negotiate reply header"); |
| 413 |
pdu->payload = NULL; |
| 414 |
free(rep); |
| 415 |
return -1; |
| 416 |
} |
| 417 |
|
| 418 |
/* |
| 419 |
* In SMB3.1.1 and later we have negotiate contexts at the end of the |
| 420 |
* blob but we can not compute how big they are from just |
| 421 |
* looking at the smb2 header of the fixed part of the negotiate reply |
| 422 |
* so just return all the remaining data as the variable size. |
| 423 |
* The contexts are technically where padding should be. |
| 424 |
*/ |
| 425 |
if (rep->dialect_revision >= SMB2_VERSION_0311) { |
| 426 |
return smb2->spl - SMB2_HEADER_SIZE - |
| 427 |
(SMB2_NEGOTIATE_REPLY_SIZE & 0xfffe); |
| 428 |
} else { |
| 429 |
return IOV_OFFSET + rep->security_buffer_length; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
int |
| 434 |
smb2_process_negotiate_variable(struct smb2_context *smb2, |
| 435 |
struct smb2_pdu *pdu) |
| 436 |
{ |
| 437 |
struct smb2_negotiate_reply *rep = pdu->payload; |
| 438 |
struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1]; |
| 439 |
int offset; |
| 440 |
|
| 441 |
rep->security_buffer = &iov->buf[IOV_OFFSET]; |
| 442 |
|
| 443 |
if (rep->dialect_revision < SMB2_VERSION_0311 || |
| 444 |
!rep->negotiate_context_count) { |
| 445 |
return 0; |
| 446 |
} |
| 447 |
|
| 448 |
offset = rep->negotiate_context_offset - SMB2_HEADER_SIZE - |
| 449 |
(SMB2_NEGOTIATE_REPLY_SIZE & 0xfffe); |
| 450 |
|
| 451 |
if (offset < 0 || offset > (int)iov->len) { |
| 452 |
return -1; |
| 453 |
} |
| 454 |
|
| 455 |
if (smb2_parse_negotiate_contexts(smb2, rep, iov, offset, |
| 456 |
rep->negotiate_context_count)) { |
| 457 |
return -1; |
| 458 |
} |
| 459 |
|
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
#define IOVREQ_OFFSET (req->security_buffer_offset - SMB2_HEADER_SIZE - \ |
| 464 |
(SMB2_NEGOTIATE_REQUEST_SIZE & 0xfffe)) |
| 465 |
|
| 466 |
int |
| 467 |
smb2_process_negotiate_request_fixed(struct smb2_context *smb2, |
| 468 |
struct smb2_pdu *pdu) |
| 469 |
{ |
| 470 |
struct smb2_negotiate_request *req; |
| 471 |
struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1]; |
| 472 |
uint16_t struct_size; |
| 473 |
|
| 474 |
smb2_get_uint16(iov, 0, &struct_size); |
| 475 |
if (struct_size != SMB2_NEGOTIATE_REQUEST_SIZE || |
| 476 |
(struct_size & 0xfffe) != iov->len) { |
| 477 |
smb2_set_error(smb2, "Unexpected size of Negotiate " |
| 478 |
"request. Expected %d, got %d", |
| 479 |
SMB2_NEGOTIATE_REQUEST_SIZE, |
| 480 |
(int)iov->len); |
| 481 |
return -1; |
| 482 |
} |
| 483 |
|
| 484 |
req = malloc(sizeof(*req)); |
| 485 |
if (req == NULL) { |
| 486 |
smb2_set_error(smb2, "Failed to allocate negotiate request"); |
| 487 |
return -1; |
| 488 |
} |
| 489 |
|
| 490 |
pdu->payload = req; |
| 491 |
|
| 492 |
smb2_get_uint16(iov, 2, &req->dialect_count); |
| 493 |
smb2_get_uint16(iov, 4, &req->security_mode); |
| 494 |
/* 2 bytes reserved */ |
| 495 |
smb2_get_uint32(iov, 8, &req->capabilities); |
| 496 |
memcpy(req->client_guid, iov->buf + 12, SMB2_GUID_SIZE); |
| 497 |
smb2_get_uint32(iov, 28, &req->negotiate_context_offset); |
| 498 |
smb2_get_uint16(iov, 32, &req->negotiate_context_count); |
| 499 |
|
| 500 |
/* |
| 501 |
* In SMB3.1.1 and later we have negotiate contexts at the end of the |
| 502 |
* blob but we can not compute how big they are from just |
| 503 |
* looking at the smb2 header of the fixed part of the negotiate reply |
| 504 |
* so just return all the remaining data as the variable size. |
| 505 |
* The contexts are technically where padding should be. |
| 506 |
*/ |
| 507 |
if (req->negotiate_context_count > 0) { |
| 508 |
return smb2->spl - SMB2_HEADER_SIZE - |
| 509 |
(SMB2_NEGOTIATE_REQUEST_SIZE & 0xfffe); |
| 510 |
} else { |
| 511 |
return req->dialect_count * sizeof(uint16_t); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
static int |
| 516 |
smb2_parse_encryption_request_context(struct smb2_context *smb2, |
| 517 |
struct smb2_negotiate_request *req, |
| 518 |
struct smb2_iovec *iov, |
| 519 |
int offset, int len) |
| 520 |
{ |
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
static int |
| 525 |
smb2_parse_netname_request_context(struct smb2_context *smb2, |
| 526 |
struct smb2_negotiate_request *req, |
| 527 |
struct smb2_iovec *iov, |
| 528 |
int offset, int len) |
| 529 |
{ |
| 530 |
char netname[128]; |
| 531 |
char *client; |
| 532 |
|
| 533 |
memcpy(netname, iov->buf + offset, len); |
| 534 |
client = discard_const(smb2_utf16_to_utf8((uint16_t *)netname, len)); |
| 535 |
free(client); |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
static int |
| 540 |
smb2_parse_negotiate_request_contexts(struct smb2_context *smb2, |
| 541 |
struct smb2_negotiate_request *req, |
| 542 |
struct smb2_iovec *iov, |
| 543 |
int offset, int count) |
| 544 |
{ |
| 545 |
uint16_t type, len; |
| 546 |
|
| 547 |
while (count--) { |
| 548 |
smb2_get_uint16(iov, offset, &type); |
| 549 |
smb2_get_uint16(iov, offset + 2, &len); |
| 550 |
|
| 551 |
if (offset > (int)iov->len) { |
| 552 |
smb2_set_error(smb2, "Bad len in negotiate context"); |
| 553 |
return -1; |
| 554 |
} |
| 555 |
switch (type) { |
| 556 |
case SMB2_PREAUTH_INTEGRITY_CAP: |
| 557 |
break; |
| 558 |
case SMB2_ENCRYPTION_CAP: |
| 559 |
if (smb2_parse_encryption_request_context(smb2, req, |
| 560 |
iov, offset + 8, len)) { |
| 561 |
return -1; |
| 562 |
} |
| 563 |
break; |
| 564 |
case SMB2_NETNAME_NEGOTIATE_CONTEXT_ID: |
| 565 |
if (smb2_parse_netname_request_context(smb2, req, |
| 566 |
iov, offset + 8, len)) { |
| 567 |
return -1; |
| 568 |
} |
| 569 |
break; |
| 570 |
case SMB2_SIGNING_CAP: |
| 571 |
case SMB2_COMPRESSION_CAP: |
| 572 |
case SMB2_TRANSPORT_CAP: |
| 573 |
case SMB2_RDMA_TRANSFORM_CAP: |
| 574 |
break; |
| 575 |
case SMB2_CONTEXTTYPE_RESERVED: |
| 576 |
/* used by samba for "posix extension cap" */ |
| 577 |
break; |
| 578 |
default: |
| 579 |
smb2_set_error(smb2, "Unknown negotiate context " |
| 580 |
"type 0x%04x", type); |
| 581 |
return -1; |
| 582 |
} |
| 583 |
offset += PAD_TO_64BIT(len + 8); |
| 584 |
} |
| 585 |
|
| 586 |
return 0; |
| 587 |
} |
| 588 |
|
| 589 |
int |
| 590 |
smb2_process_negotiate_request_variable(struct smb2_context *smb2, |
| 591 |
struct smb2_pdu *pdu) |
| 592 |
{ |
| 593 |
struct smb2_negotiate_request *req = (struct smb2_negotiate_request*)pdu->payload; |
| 594 |
struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1]; |
| 595 |
int offset; |
| 596 |
int has_0311 = 0; |
| 597 |
uint32_t d; |
| 598 |
|
| 599 |
for (d = 0; d < req->dialect_count && d < SMB2_NEGOTIATE_MAX_DIALECTS; d++) { |
| 600 |
smb2_get_uint16(iov, d * 2, &req->dialects[d]); |
| 601 |
if (req->dialects[d] == 0x0311) { |
| 602 |
has_0311 = 1; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
if (!req->negotiate_context_count) { |
| 607 |
return 0; |
| 608 |
} |
| 609 |
|
| 610 |
/* if dialects array does not contain 0311, there should be no interpretation of |
| 611 |
* negotiate contexts (its the client start time in non-0311 dialects) */ |
| 612 |
if (!has_0311) { |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
offset = req->negotiate_context_offset - SMB2_HEADER_SIZE - |
| 617 |
(SMB2_NEGOTIATE_REQUEST_SIZE & 0xfffe); |
| 618 |
|
| 619 |
if (offset < 0 || offset > (int)iov->len) { |
| 620 |
return -1; |
| 621 |
} |
| 622 |
|
| 623 |
if (smb2_parse_negotiate_request_contexts(smb2, req, iov, offset, |
| 624 |
req->negotiate_context_count)) { |
| 625 |
return -1; |
| 626 |
} |
| 627 |
|
| 628 |
return 0; |
| 629 |
} |
| 630 |
|