login.c revision 274870
1/*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: stable/10/usr.sbin/iscsid/login.c 274870 2014-11-22 17:50:14Z trasz $"); 33 34#include <sys/types.h> 35#include <sys/ioctl.h> 36#include <assert.h> 37#include <stdbool.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <netinet/in.h> 42 43#include "iscsid.h" 44#include "iscsi_proto.h" 45 46static int 47login_nsg(const struct pdu *response) 48{ 49 struct iscsi_bhs_login_response *bhslr; 50 51 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 52 53 return (bhslr->bhslr_flags & 0x03); 54} 55 56static void 57login_set_nsg(struct pdu *request, int nsg) 58{ 59 struct iscsi_bhs_login_request *bhslr; 60 61 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION || 62 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || 63 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE); 64 65 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 66 67 bhslr->bhslr_flags &= 0xFC; 68 bhslr->bhslr_flags |= nsg; 69} 70 71static void 72login_set_csg(struct pdu *request, int csg) 73{ 74 struct iscsi_bhs_login_request *bhslr; 75 76 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION || 77 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || 78 csg == BHSLR_STAGE_FULL_FEATURE_PHASE); 79 80 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 81 82 bhslr->bhslr_flags &= 0xF3; 83 bhslr->bhslr_flags |= csg << 2; 84} 85 86static const char * 87login_target_error_str(int class, int detail) 88{ 89 static char msg[128]; 90 91 /* 92 * RFC 3270, 10.13.5. Status-Class and Status-Detail 93 */ 94 switch (class) { 95 case 0x01: 96 switch (detail) { 97 case 0x01: 98 return ("Target moved temporarily"); 99 case 0x02: 100 return ("Target moved permanently"); 101 default: 102 snprintf(msg, sizeof(msg), "unknown redirection; " 103 "Status-Class 0x%x, Status-Detail 0x%x", 104 class, detail); 105 return (msg); 106 } 107 case 0x02: 108 switch (detail) { 109 case 0x00: 110 return ("Initiator error"); 111 case 0x01: 112 return ("Authentication failure"); 113 case 0x02: 114 return ("Authorization failure"); 115 case 0x03: 116 return ("Not found"); 117 case 0x04: 118 return ("Target removed"); 119 case 0x05: 120 return ("Unsupported version"); 121 case 0x06: 122 return ("Too many connections"); 123 case 0x07: 124 return ("Missing parameter"); 125 case 0x08: 126 return ("Can't include in session"); 127 case 0x09: 128 return ("Session type not supported"); 129 case 0x0a: 130 return ("Session does not exist"); 131 case 0x0b: 132 return ("Invalid during login"); 133 default: 134 snprintf(msg, sizeof(msg), "unknown initiator error; " 135 "Status-Class 0x%x, Status-Detail 0x%x", 136 class, detail); 137 return (msg); 138 } 139 case 0x03: 140 switch (detail) { 141 case 0x00: 142 return ("Target error"); 143 case 0x01: 144 return ("Service unavailable"); 145 case 0x02: 146 return ("Out of resources"); 147 default: 148 snprintf(msg, sizeof(msg), "unknown target error; " 149 "Status-Class 0x%x, Status-Detail 0x%x", 150 class, detail); 151 return (msg); 152 } 153 default: 154 snprintf(msg, sizeof(msg), "unknown error; " 155 "Status-Class 0x%x, Status-Detail 0x%x", 156 class, detail); 157 return (msg); 158 } 159} 160 161static void 162kernel_modify(const struct connection *conn, const char *target_address) 163{ 164 struct iscsi_session_modify ism; 165 int error; 166 167 memset(&ism, 0, sizeof(ism)); 168 ism.ism_session_id = conn->conn_session_id; 169 memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf)); 170 strlcpy(ism.ism_conf.isc_target_addr, target_address, 171 sizeof(ism.ism_conf.isc_target)); 172 error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism); 173 if (error != 0) { 174 log_err(1, "failed to redirect to %s: ISCSISMODIFY", 175 target_address); 176 } 177} 178 179/* 180 * XXX: The way it works is suboptimal; what should happen is described 181 * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would 182 * be much more complicated: we would need to keep "dependencies" 183 * for sessions, so that, in case described in draft and using draft 184 * terminology, we would have three sessions: one for discovery, 185 * one for initial target portal, and one for redirect portal. 186 * This would allow us to "backtrack" on connection failure, 187 * as described in draft. 188 */ 189static void 190login_handle_redirection(struct connection *conn, struct pdu *response) 191{ 192 struct iscsi_bhs_login_response *bhslr; 193 struct keys *response_keys; 194 const char *target_address; 195 196 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 197 assert (bhslr->bhslr_status_class == 1); 198 199 response_keys = keys_new(); 200 keys_load(response_keys, response); 201 202 target_address = keys_find(response_keys, "TargetAddress"); 203 if (target_address == NULL) 204 log_errx(1, "received redirection without TargetAddress"); 205 if (target_address[0] == '\0') 206 log_errx(1, "received redirection with empty TargetAddress"); 207 if (strlen(target_address) >= 208 sizeof(conn->conn_conf.isc_target_addr) - 1) 209 log_errx(1, "received TargetAddress is too long"); 210 211 log_debugx("received redirection to \"%s\"", target_address); 212 kernel_modify(conn, target_address); 213} 214 215static struct pdu * 216login_receive(struct connection *conn) 217{ 218 struct pdu *response; 219 struct iscsi_bhs_login_response *bhslr; 220 const char *errorstr; 221 static bool initial = true; 222 223 response = pdu_new(conn); 224 pdu_receive(response); 225 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGIN_RESPONSE) { 226 log_errx(1, "protocol error: received invalid opcode 0x%x", 227 response->pdu_bhs->bhs_opcode); 228 } 229 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 230 /* 231 * XXX: Implement the C flag some day. 232 */ 233 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) 234 log_errx(1, "received Login PDU with unsupported \"C\" flag"); 235 if (bhslr->bhslr_version_max != 0x00) 236 log_errx(1, "received Login PDU with unsupported " 237 "Version-max 0x%x", bhslr->bhslr_version_max); 238 if (bhslr->bhslr_version_active != 0x00) 239 log_errx(1, "received Login PDU with unsupported " 240 "Version-active 0x%x", bhslr->bhslr_version_active); 241 if (bhslr->bhslr_status_class == 1) { 242 login_handle_redirection(conn, response); 243 log_debugx("redirection handled; exiting"); 244 exit(0); 245 } 246 if (bhslr->bhslr_status_class != 0) { 247 errorstr = login_target_error_str(bhslr->bhslr_status_class, 248 bhslr->bhslr_status_detail); 249 fail(conn, errorstr); 250 log_errx(1, "target returned error: %s", errorstr); 251 } 252 if (initial == false && 253 ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) { 254 /* 255 * It's a warning, not an error, to work around what seems 256 * to be bug in NetBSD iSCSI target. 257 */ 258 log_warnx("received Login PDU with wrong StatSN: " 259 "is %d, should be %d", ntohl(bhslr->bhslr_statsn), 260 conn->conn_statsn + 1); 261 } 262 conn->conn_tsih = ntohs(bhslr->bhslr_tsih); 263 conn->conn_statsn = ntohl(bhslr->bhslr_statsn); 264 265 initial = false; 266 267 return (response); 268} 269 270static struct pdu * 271login_new_request(struct connection *conn, int csg) 272{ 273 struct pdu *request; 274 struct iscsi_bhs_login_request *bhslr; 275 int nsg; 276 277 request = pdu_new(conn); 278 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 279 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_REQUEST | 280 ISCSI_BHS_OPCODE_IMMEDIATE; 281 282 bhslr->bhslr_flags = BHSLR_FLAGS_TRANSIT; 283 switch (csg) { 284 case BHSLR_STAGE_SECURITY_NEGOTIATION: 285 nsg = BHSLR_STAGE_OPERATIONAL_NEGOTIATION; 286 break; 287 case BHSLR_STAGE_OPERATIONAL_NEGOTIATION: 288 nsg = BHSLR_STAGE_FULL_FEATURE_PHASE; 289 break; 290 default: 291 assert(!"invalid csg"); 292 log_errx(1, "invalid csg %d", csg); 293 } 294 login_set_csg(request, csg); 295 login_set_nsg(request, nsg); 296 297 memcpy(bhslr->bhslr_isid, &conn->conn_isid, sizeof(bhslr->bhslr_isid)); 298 bhslr->bhslr_tsih = htons(conn->conn_tsih); 299 bhslr->bhslr_initiator_task_tag = 0; 300 bhslr->bhslr_cmdsn = 0; 301 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1); 302 303 return (request); 304} 305 306static int 307login_list_prefers(const char *list, 308 const char *choice1, const char *choice2) 309{ 310 char *tofree, *str, *token; 311 312 tofree = str = checked_strdup(list); 313 314 while ((token = strsep(&str, ",")) != NULL) { 315 if (strcmp(token, choice1) == 0) { 316 free(tofree); 317 return (1); 318 } 319 if (strcmp(token, choice2) == 0) { 320 free(tofree); 321 return (2); 322 } 323 } 324 free(tofree); 325 return (-1); 326} 327 328static void 329login_negotiate_key(struct connection *conn, const char *name, 330 const char *value) 331{ 332 int which, tmp; 333 334 if (strcmp(name, "TargetAlias") == 0) { 335 strlcpy(conn->conn_target_alias, value, 336 sizeof(conn->conn_target_alias)); 337 } else if (strcmp(value, "Irrelevant") == 0) { 338 /* Ignore. */ 339 } else if (strcmp(name, "HeaderDigest") == 0) { 340 which = login_list_prefers(value, "CRC32C", "None"); 341 switch (which) { 342 case 1: 343 log_debugx("target prefers CRC32C " 344 "for header digest; we'll use it"); 345 conn->conn_header_digest = CONN_DIGEST_CRC32C; 346 break; 347 case 2: 348 log_debugx("target prefers not to do " 349 "header digest; we'll comply"); 350 break; 351 default: 352 log_warnx("target sent unrecognized " 353 "HeaderDigest value \"%s\"; will use None", value); 354 break; 355 } 356 } else if (strcmp(name, "DataDigest") == 0) { 357 which = login_list_prefers(value, "CRC32C", "None"); 358 switch (which) { 359 case 1: 360 log_debugx("target prefers CRC32C " 361 "for data digest; we'll use it"); 362 conn->conn_data_digest = CONN_DIGEST_CRC32C; 363 break; 364 case 2: 365 log_debugx("target prefers not to do " 366 "data digest; we'll comply"); 367 break; 368 default: 369 log_warnx("target sent unrecognized " 370 "DataDigest value \"%s\"; will use None", value); 371 break; 372 } 373 } else if (strcmp(name, "MaxConnections") == 0) { 374 /* Ignore. */ 375 } else if (strcmp(name, "InitialR2T") == 0) { 376 if (strcmp(value, "Yes") == 0) 377 conn->conn_initial_r2t = true; 378 else 379 conn->conn_initial_r2t = false; 380 } else if (strcmp(name, "ImmediateData") == 0) { 381 if (strcmp(value, "Yes") == 0) 382 conn->conn_immediate_data = true; 383 else 384 conn->conn_immediate_data = false; 385 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) { 386 tmp = strtoul(value, NULL, 10); 387 if (tmp <= 0) 388 log_errx(1, "received invalid " 389 "MaxRecvDataSegmentLength"); 390 conn->conn_max_data_segment_length = tmp; 391 } else if (strcmp(name, "MaxBurstLength") == 0) { 392 if (conn->conn_immediate_data) { 393 tmp = strtoul(value, NULL, 10); 394 if (tmp <= 0) 395 log_errx(1, "received invalid MaxBurstLength"); 396 conn->conn_max_burst_length = tmp; 397 } 398 } else if (strcmp(name, "FirstBurstLength") == 0) { 399 tmp = strtoul(value, NULL, 10); 400 if (tmp <= 0) 401 log_errx(1, "received invalid FirstBurstLength"); 402 conn->conn_first_burst_length = tmp; 403 } else if (strcmp(name, "DefaultTime2Wait") == 0) { 404 /* Ignore */ 405 } else if (strcmp(name, "DefaultTime2Retain") == 0) { 406 /* Ignore */ 407 } else if (strcmp(name, "MaxOutstandingR2T") == 0) { 408 /* Ignore */ 409 } else if (strcmp(name, "DataPDUInOrder") == 0) { 410 /* Ignore */ 411 } else if (strcmp(name, "DataSequenceInOrder") == 0) { 412 /* Ignore */ 413 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) { 414 /* Ignore */ 415 } else if (strcmp(name, "OFMarker") == 0) { 416 /* Ignore */ 417 } else if (strcmp(name, "IFMarker") == 0) { 418 /* Ignore */ 419 } else if (strcmp(name, "TargetPortalGroupTag") == 0) { 420 /* Ignore */ 421 } else { 422 log_debugx("unknown key \"%s\"; ignoring", name); 423 } 424} 425 426static void 427login_negotiate(struct connection *conn) 428{ 429 struct pdu *request, *response; 430 struct keys *request_keys, *response_keys; 431 struct iscsi_bhs_login_response *bhslr; 432 int i, nrequests = 0; 433 434 log_debugx("beginning operational parameter negotiation"); 435 request = login_new_request(conn, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 436 request_keys = keys_new(); 437 438 /* 439 * The following keys are irrelevant for discovery sessions. 440 */ 441 if (conn->conn_conf.isc_discovery == 0) { 442 if (conn->conn_conf.isc_header_digest != 0) 443 keys_add(request_keys, "HeaderDigest", "CRC32C"); 444 else 445 keys_add(request_keys, "HeaderDigest", "None"); 446 if (conn->conn_conf.isc_data_digest != 0) 447 keys_add(request_keys, "DataDigest", "CRC32C"); 448 else 449 keys_add(request_keys, "DataDigest", "None"); 450 451 keys_add(request_keys, "ImmediateData", "Yes"); 452 keys_add_int(request_keys, "MaxBurstLength", 453 ISCSI_MAX_DATA_SEGMENT_LENGTH); 454 keys_add_int(request_keys, "FirstBurstLength", 455 ISCSI_MAX_DATA_SEGMENT_LENGTH); 456 keys_add(request_keys, "InitialR2T", "Yes"); 457 } else { 458 keys_add(request_keys, "HeaderDigest", "None"); 459 keys_add(request_keys, "DataDigest", "None"); 460 } 461 462 keys_add_int(request_keys, "MaxRecvDataSegmentLength", 463 ISCSI_MAX_DATA_SEGMENT_LENGTH); 464 keys_add(request_keys, "DefaultTime2Wait", "0"); 465 keys_add(request_keys, "DefaultTime2Retain", "0"); 466 keys_add(request_keys, "ErrorRecoveryLevel", "0"); 467 keys_add(request_keys, "MaxOutstandingR2T", "1"); 468 keys_save(request_keys, request); 469 keys_delete(request_keys); 470 request_keys = NULL; 471 pdu_send(request); 472 pdu_delete(request); 473 request = NULL; 474 475 response = login_receive(conn); 476 response_keys = keys_new(); 477 keys_load(response_keys, response); 478 for (i = 0; i < KEYS_MAX; i++) { 479 if (response_keys->keys_names[i] == NULL) 480 break; 481 482 login_negotiate_key(conn, 483 response_keys->keys_names[i], response_keys->keys_values[i]); 484 } 485 486 keys_delete(response_keys); 487 response_keys = NULL; 488 489 for (;;) { 490 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 491 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0) 492 break; 493 494 nrequests++; 495 if (nrequests > 5) { 496 log_warnx("received login response " 497 "without the \"T\" flag too many times; giving up"); 498 break; 499 } 500 501 log_debugx("received login response " 502 "without the \"T\" flag; sending another request"); 503 504 pdu_delete(response); 505 506 request = login_new_request(conn, 507 BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 508 pdu_send(request); 509 pdu_delete(request); 510 511 response = login_receive(conn); 512 } 513 514 if (login_nsg(response) != BHSLR_STAGE_FULL_FEATURE_PHASE) 515 log_warnx("received final login response with wrong NSG 0x%x", 516 login_nsg(response)); 517 pdu_delete(response); 518 519 log_debugx("operational parameter negotiation done; " 520 "transitioning to Full Feature phase"); 521} 522 523static void 524login_send_chap_a(struct connection *conn) 525{ 526 struct pdu *request; 527 struct keys *request_keys; 528 529 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION); 530 request_keys = keys_new(); 531 keys_add(request_keys, "CHAP_A", "5"); 532 keys_save(request_keys, request); 533 keys_delete(request_keys); 534 pdu_send(request); 535 pdu_delete(request); 536} 537 538static void 539login_send_chap_r(struct pdu *response) 540{ 541 struct connection *conn; 542 struct pdu *request; 543 struct keys *request_keys, *response_keys; 544 struct rchap *rchap; 545 const char *chap_a, *chap_c, *chap_i; 546 char *chap_r; 547 int error; 548 char *mutual_chap_c, *mutual_chap_i; 549 550 /* 551 * As in the rest of the initiator, 'request' means 552 * 'initiator -> target', and 'response' means 'target -> initiator', 553 * 554 * So, here the 'response' from the target is the packet that contains 555 * CHAP challenge; our CHAP response goes into 'request'. 556 */ 557 558 conn = response->pdu_connection; 559 560 response_keys = keys_new(); 561 keys_load(response_keys, response); 562 563 /* 564 * First, compute the response. 565 */ 566 chap_a = keys_find(response_keys, "CHAP_A"); 567 if (chap_a == NULL) 568 log_errx(1, "received CHAP packet without CHAP_A"); 569 chap_c = keys_find(response_keys, "CHAP_C"); 570 if (chap_c == NULL) 571 log_errx(1, "received CHAP packet without CHAP_C"); 572 chap_i = keys_find(response_keys, "CHAP_I"); 573 if (chap_i == NULL) 574 log_errx(1, "received CHAP packet without CHAP_I"); 575 576 if (strcmp(chap_a, "5") != 0) { 577 log_errx(1, "received CHAP packet " 578 "with unsupported CHAP_A \"%s\"", chap_a); 579 } 580 581 rchap = rchap_new(conn->conn_conf.isc_secret); 582 error = rchap_receive(rchap, chap_i, chap_c); 583 if (error != 0) { 584 log_errx(1, "received CHAP packet " 585 "with malformed CHAP_I or CHAP_C"); 586 } 587 chap_r = rchap_get_response(rchap); 588 rchap_delete(rchap); 589 590 keys_delete(response_keys); 591 592 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION); 593 request_keys = keys_new(); 594 keys_add(request_keys, "CHAP_N", conn->conn_conf.isc_user); 595 keys_add(request_keys, "CHAP_R", chap_r); 596 free(chap_r); 597 598 /* 599 * If we want mutual authentication, we're expected to send 600 * our CHAP_I/CHAP_C now. 601 */ 602 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 603 log_debugx("requesting mutual authentication; " 604 "binary challenge size is %zd bytes", 605 sizeof(conn->conn_mutual_chap->chap_challenge)); 606 607 assert(conn->conn_mutual_chap == NULL); 608 conn->conn_mutual_chap = chap_new(); 609 mutual_chap_i = chap_get_id(conn->conn_mutual_chap); 610 mutual_chap_c = chap_get_challenge(conn->conn_mutual_chap); 611 keys_add(request_keys, "CHAP_I", mutual_chap_i); 612 keys_add(request_keys, "CHAP_C", mutual_chap_c); 613 free(mutual_chap_i); 614 free(mutual_chap_c); 615 } 616 617 keys_save(request_keys, request); 618 keys_delete(request_keys); 619 pdu_send(request); 620 pdu_delete(request); 621} 622 623static void 624login_verify_mutual(const struct pdu *response) 625{ 626 struct connection *conn; 627 struct keys *response_keys; 628 const char *chap_n, *chap_r; 629 int error; 630 631 conn = response->pdu_connection; 632 633 response_keys = keys_new(); 634 keys_load(response_keys, response); 635 636 chap_n = keys_find(response_keys, "CHAP_N"); 637 if (chap_n == NULL) 638 log_errx(1, "received CHAP Response PDU without CHAP_N"); 639 chap_r = keys_find(response_keys, "CHAP_R"); 640 if (chap_r == NULL) 641 log_errx(1, "received CHAP Response PDU without CHAP_R"); 642 643 error = chap_receive(conn->conn_mutual_chap, chap_r); 644 if (error != 0) 645 log_errx(1, "received CHAP Response PDU with invalid CHAP_R"); 646 647 if (strcmp(chap_n, conn->conn_conf.isc_mutual_user) != 0) { 648 fail(conn, "Mutual CHAP failed"); 649 log_errx(1, "mutual CHAP authentication failed: wrong user"); 650 } 651 652 error = chap_authenticate(conn->conn_mutual_chap, 653 conn->conn_conf.isc_mutual_secret); 654 if (error != 0) { 655 fail(conn, "Mutual CHAP failed"); 656 log_errx(1, "mutual CHAP authentication failed: wrong secret"); 657 } 658 659 keys_delete(response_keys); 660 chap_delete(conn->conn_mutual_chap); 661 conn->conn_mutual_chap = NULL; 662 663 log_debugx("mutual CHAP authentication succeeded"); 664} 665 666static void 667login_chap(struct connection *conn) 668{ 669 struct pdu *response; 670 671 log_debugx("beginning CHAP authentication; sending CHAP_A"); 672 login_send_chap_a(conn); 673 674 log_debugx("waiting for CHAP_A/CHAP_C/CHAP_I"); 675 response = login_receive(conn); 676 677 log_debugx("sending CHAP_N/CHAP_R"); 678 login_send_chap_r(response); 679 pdu_delete(response); 680 681 /* 682 * XXX: Make sure this is not susceptible to MITM. 683 */ 684 685 log_debugx("waiting for CHAP result"); 686 response = login_receive(conn); 687 if (conn->conn_conf.isc_mutual_user[0] != '\0') 688 login_verify_mutual(response); 689 pdu_delete(response); 690 691 log_debugx("CHAP authentication done"); 692} 693 694void 695login(struct connection *conn) 696{ 697 struct pdu *request, *response; 698 struct keys *request_keys, *response_keys; 699 struct iscsi_bhs_login_response *bhslr2; 700 const char *auth_method; 701 int i; 702 703 log_debugx("beginning Login phase; sending Login PDU"); 704 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION); 705 request_keys = keys_new(); 706 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 707 keys_add(request_keys, "AuthMethod", "CHAP"); 708 } else if (conn->conn_conf.isc_user[0] != '\0') { 709 /* 710 * Give target a chance to skip authentication if it 711 * doesn't feel like it. 712 * 713 * None is first, CHAP second; this is to work around 714 * what seems to be LIO (Linux target) bug: otherwise, 715 * if target is configured with no authentication, 716 * and we are configured to authenticate, the target 717 * will erroneously respond with AuthMethod=CHAP 718 * instead of AuthMethod=None, and will subsequently 719 * fail the connection. This usually happens with 720 * Discovery sessions, which default to no authentication. 721 */ 722 keys_add(request_keys, "AuthMethod", "None,CHAP"); 723 } else { 724 keys_add(request_keys, "AuthMethod", "None"); 725 } 726 keys_add(request_keys, "InitiatorName", 727 conn->conn_conf.isc_initiator); 728 if (conn->conn_conf.isc_initiator_alias[0] != '\0') { 729 keys_add(request_keys, "InitiatorAlias", 730 conn->conn_conf.isc_initiator_alias); 731 } 732 if (conn->conn_conf.isc_discovery == 0) { 733 keys_add(request_keys, "SessionType", "Normal"); 734 keys_add(request_keys, 735 "TargetName", conn->conn_conf.isc_target); 736 } else { 737 keys_add(request_keys, "SessionType", "Discovery"); 738 } 739 keys_save(request_keys, request); 740 keys_delete(request_keys); 741 pdu_send(request); 742 pdu_delete(request); 743 744 response = login_receive(conn); 745 746 response_keys = keys_new(); 747 keys_load(response_keys, response); 748 749 for (i = 0; i < KEYS_MAX; i++) { 750 if (response_keys->keys_names[i] == NULL) 751 break; 752 753 /* 754 * Not interested in AuthMethod at this point; we only need 755 * to parse things such as TargetAlias. 756 * 757 * XXX: This is somewhat ugly. We should have a way to apply 758 * all the keys to the session and use that by default 759 * instead of discarding them. 760 */ 761 if (strcmp(response_keys->keys_names[i], "AuthMethod") == 0) 762 continue; 763 764 login_negotiate_key(conn, 765 response_keys->keys_names[i], response_keys->keys_values[i]); 766 } 767 768 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 769 if ((bhslr2->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0 && 770 login_nsg(response) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) { 771 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 772 log_errx(1, "target requested transition " 773 "to operational parameter negotiation, " 774 "but we require mutual CHAP"); 775 } 776 777 log_debugx("target requested transition " 778 "to operational parameter negotiation"); 779 keys_delete(response_keys); 780 pdu_delete(response); 781 login_negotiate(conn); 782 return; 783 } 784 785 auth_method = keys_find(response_keys, "AuthMethod"); 786 if (auth_method == NULL) 787 log_errx(1, "received response without AuthMethod"); 788 if (strcmp(auth_method, "None") == 0) { 789 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 790 log_errx(1, "target does not require authantication, " 791 "but we require mutual CHAP"); 792 } 793 794 log_debugx("target does not require authentication"); 795 keys_delete(response_keys); 796 pdu_delete(response); 797 login_negotiate(conn); 798 return; 799 } 800 801 if (strcmp(auth_method, "CHAP") != 0) { 802 fail(conn, "Unsupported AuthMethod"); 803 log_errx(1, "received response " 804 "with unsupported AuthMethod \"%s\"", auth_method); 805 } 806 807 if (conn->conn_conf.isc_user[0] == '\0' || 808 conn->conn_conf.isc_secret[0] == '\0') { 809 fail(conn, "Authentication required"); 810 log_errx(1, "target requests CHAP authentication, but we don't " 811 "have user and secret"); 812 } 813 814 keys_delete(response_keys); 815 response_keys = NULL; 816 pdu_delete(response); 817 response = NULL; 818 819 login_chap(conn); 820 login_negotiate(conn); 821} 822