2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
39 #include <netinet/in.h>
40 #include <openssl/err.h>
41 #include <openssl/md5.h>
42 #include <openssl/rand.h>
45 #include "iscsi_proto.h"
48 login_nsg(const struct pdu *response)
50 struct iscsi_bhs_login_response *bhslr;
52 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
54 return (bhslr->bhslr_flags & 0x03);
58 login_set_nsg(struct pdu *request, int nsg)
60 struct iscsi_bhs_login_request *bhslr;
62 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
63 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
64 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
66 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
68 bhslr->bhslr_flags &= 0xFC;
69 bhslr->bhslr_flags |= nsg;
73 login_set_csg(struct pdu *request, int csg)
75 struct iscsi_bhs_login_request *bhslr;
77 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
78 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
79 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
81 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
83 bhslr->bhslr_flags &= 0xF3;
84 bhslr->bhslr_flags |= csg << 2;
88 login_target_error_str(int class, int detail)
93 * RFC 3270, 10.13.5. Status-Class and Status-Detail
99 return ("Target moved temporarily");
101 return ("Target moved permanently");
103 snprintf(msg, sizeof(msg), "unknown redirection; "
104 "Status-Class 0x%x, Status-Detail 0x%x",
111 return ("Initiator error");
113 return ("Authentication failure");
115 return ("Authorization failure");
117 return ("Not found");
119 return ("Target removed");
121 return ("Unsupported version");
123 return ("Too many connections");
125 return ("Missing parameter");
127 return ("Can't include in session");
129 return ("Session type not supported");
131 return ("Session does not exist");
133 return ("Invalid during login");
135 snprintf(msg, sizeof(msg), "unknown initiator error; "
136 "Status-Class 0x%x, Status-Detail 0x%x",
143 return ("Target error");
145 return ("Service unavailable");
147 return ("Out of resources");
149 snprintf(msg, sizeof(msg), "unknown target error; "
150 "Status-Class 0x%x, Status-Detail 0x%x",
155 snprintf(msg, sizeof(msg), "unknown error; "
156 "Status-Class 0x%x, Status-Detail 0x%x",
163 kernel_modify(const struct connection *conn, const char *target_address)
165 struct iscsi_session_modify ism;
168 memset(&ism, 0, sizeof(ism));
169 ism.ism_session_id = conn->conn_session_id;
170 memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf));
171 strlcpy(ism.ism_conf.isc_target_addr, target_address,
172 sizeof(ism.ism_conf.isc_target));
173 error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism);
175 log_err(1, "failed to redirect to %s: ISCSISMODIFY",
181 * XXX: The way it works is suboptimal; what should happen is described
182 * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would
183 * be much more complicated: we would need to keep "dependencies"
184 * for sessions, so that, in case described in draft and using draft
185 * terminology, we would have three sessions: one for discovery,
186 * one for initial target portal, and one for redirect portal.
187 * This would allow us to "backtrack" on connection failure,
188 * as described in draft.
191 login_handle_redirection(struct connection *conn, struct pdu *response)
193 struct iscsi_bhs_login_response *bhslr;
194 struct keys *response_keys;
195 const char *target_address;
197 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
198 assert (bhslr->bhslr_status_class == 1);
200 response_keys = keys_new();
201 keys_load(response_keys, response);
203 target_address = keys_find(response_keys, "TargetAddress");
204 if (target_address == NULL)
205 log_errx(1, "received redirection without TargetAddress");
206 if (target_address[0] == '\0')
207 log_errx(1, "received redirection with empty TargetAddress");
208 if (strlen(target_address) >=
209 sizeof(conn->conn_conf.isc_target_addr) - 1)
210 log_errx(1, "received TargetAddress is too long");
212 log_debugx("received redirection to \"%s\"", target_address);
213 kernel_modify(conn, target_address);
217 login_receive(struct connection *conn, bool initial)
219 struct pdu *response;
220 struct iscsi_bhs_login_response *bhslr;
221 const char *errorstr;
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);
229 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
231 * XXX: Implement the C flag some day.
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");
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);
252 if (initial == false &&
253 ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
255 * It's a warning, not an error, to work around what seems
256 * to be bug in NetBSD iSCSI target.
258 log_warnx("received Login PDU with wrong StatSN: "
259 "is %d, should be %d", ntohl(bhslr->bhslr_statsn),
260 conn->conn_statsn + 1);
262 conn->conn_tsih = ntohs(bhslr->bhslr_tsih);
263 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
269 login_new_request(struct connection *conn)
272 struct iscsi_bhs_login_request *bhslr;
274 request = pdu_new(conn);
275 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
276 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_REQUEST |
277 ISCSI_BHS_OPCODE_IMMEDIATE;
278 bhslr->bhslr_flags = BHSLR_FLAGS_TRANSIT;
279 login_set_csg(request, BHSLR_STAGE_SECURITY_NEGOTIATION);
280 login_set_nsg(request, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
281 memcpy(bhslr->bhslr_isid, &conn->conn_isid, sizeof(bhslr->bhslr_isid));
282 bhslr->bhslr_tsih = htons(conn->conn_tsih);
283 bhslr->bhslr_initiator_task_tag = 0;
284 bhslr->bhslr_cmdsn = 0;
285 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
291 login_list_prefers(const char *list,
292 const char *choice1, const char *choice2)
294 char *tofree, *str, *token;
296 tofree = str = checked_strdup(list);
298 while ((token = strsep(&str, ",")) != NULL) {
299 if (strcmp(token, choice1) == 0) {
303 if (strcmp(token, choice2) == 0) {
313 login_hex2int(const char hex)
360 * XXX: Review this _carefully_.
363 login_hex2bin(const char *hex, char **binp, size_t *bin_lenp)
365 int i, hex_len, nibble;
366 bool lo = true; /* As opposed to 'hi'. */
368 size_t bin_off, bin_len;
370 if (strncasecmp(hex, "0x", strlen("0x")) != 0) {
371 log_warnx("malformed variable, should start with \"0x\"");
376 hex_len = strlen(hex);
378 log_warnx("malformed variable; doesn't contain anything "
383 bin_len = hex_len / 2 + hex_len % 2;
384 bin = calloc(bin_len, 1);
386 log_err(1, "calloc");
388 bin_off = bin_len - 1;
389 for (i = hex_len - 1; i >= 0; i--) {
390 nibble = login_hex2int(hex[i]);
392 log_warnx("malformed variable, invalid char \"%c\"",
397 assert(bin_off < bin_len);
399 bin[bin_off] = nibble;
402 bin[bin_off] |= nibble << 4;
414 login_bin2hex(const char *bin, size_t bin_len)
416 unsigned char *hex, *tmp, ch;
420 hex_len = bin_len * 2 + 3; /* +2 for "0x", +1 for '\0'. */
421 hex = malloc(hex_len);
423 log_err(1, "malloc");
426 tmp += sprintf(tmp, "0x");
427 for (i = 0; i < bin_len; i++) {
429 tmp += sprintf(tmp, "%02x", ch);
436 login_compute_md5(const char id, const char *secret,
437 const void *challenge, size_t challenge_len, void *response,
443 assert(response_len == MD5_DIGEST_LENGTH);
446 MD5_Update(&ctx, &id, sizeof(id));
447 MD5_Update(&ctx, secret, strlen(secret));
448 MD5_Update(&ctx, challenge, challenge_len);
449 rv = MD5_Final(response, &ctx);
451 log_errx(1, "MD5_Final");
455 login_negotiate_key(struct connection *conn, const char *name,
460 if (strcmp(name, "TargetAlias") == 0) {
461 strlcpy(conn->conn_target_alias, value,
462 sizeof(conn->conn_target_alias));
463 } else if (strcmp(value, "Irrelevant") == 0) {
465 } else if (strcmp(name, "HeaderDigest") == 0) {
466 which = login_list_prefers(value, "CRC32C", "None");
469 log_debugx("target prefers CRC32C "
470 "for header digest; we'll use it");
471 conn->conn_header_digest = CONN_DIGEST_CRC32C;
474 log_debugx("target prefers not to do "
475 "header digest; we'll comply");
478 log_warnx("target sent unrecognized "
479 "HeaderDigest value \"%s\"; will use None", value);
482 } else if (strcmp(name, "DataDigest") == 0) {
483 which = login_list_prefers(value, "CRC32C", "None");
486 log_debugx("target prefers CRC32C "
487 "for data digest; we'll use it");
488 conn->conn_data_digest = CONN_DIGEST_CRC32C;
491 log_debugx("target prefers not to do "
492 "data digest; we'll comply");
495 log_warnx("target sent unrecognized "
496 "DataDigest value \"%s\"; will use None", value);
499 } else if (strcmp(name, "MaxConnections") == 0) {
501 } else if (strcmp(name, "InitialR2T") == 0) {
502 if (strcmp(value, "Yes") == 0)
503 conn->conn_initial_r2t = true;
505 conn->conn_initial_r2t = false;
506 } else if (strcmp(name, "ImmediateData") == 0) {
507 if (strcmp(value, "Yes") == 0)
508 conn->conn_immediate_data = true;
510 conn->conn_immediate_data = false;
511 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
512 tmp = strtoul(value, NULL, 10);
514 log_errx(1, "received invalid "
515 "MaxRecvDataSegmentLength");
516 conn->conn_max_data_segment_length = tmp;
517 } else if (strcmp(name, "MaxBurstLength") == 0) {
518 if (conn->conn_immediate_data) {
519 tmp = strtoul(value, NULL, 10);
521 log_errx(1, "received invalid MaxBurstLength");
522 conn->conn_max_burst_length = tmp;
524 } else if (strcmp(name, "FirstBurstLength") == 0) {
525 tmp = strtoul(value, NULL, 10);
527 log_errx(1, "received invalid FirstBurstLength");
528 conn->conn_first_burst_length = tmp;
529 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
531 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
533 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
535 } else if (strcmp(name, "DataPDUInOrder") == 0) {
537 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
539 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
541 } else if (strcmp(name, "OFMarker") == 0) {
543 } else if (strcmp(name, "IFMarker") == 0) {
545 } else if (strcmp(name, "TargetPortalGroupTag") == 0) {
548 log_debugx("unknown key \"%s\"; ignoring", name);
553 login_negotiate(struct connection *conn)
555 struct pdu *request, *response;
556 struct keys *request_keys, *response_keys;
557 struct iscsi_bhs_login_response *bhslr;
560 log_debugx("beginning parameter negotiation");
561 request = login_new_request(conn);
562 login_set_csg(request, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
563 login_set_nsg(request, BHSLR_STAGE_FULL_FEATURE_PHASE);
564 request_keys = keys_new();
567 * The following keys are irrelevant for discovery sessions.
569 if (conn->conn_conf.isc_discovery == 0) {
570 if (conn->conn_conf.isc_header_digest != 0)
571 keys_add(request_keys, "HeaderDigest", "CRC32C");
573 keys_add(request_keys, "HeaderDigest", "None");
574 if (conn->conn_conf.isc_data_digest != 0)
575 keys_add(request_keys, "DataDigest", "CRC32C");
577 keys_add(request_keys, "DataDigest", "None");
579 keys_add(request_keys, "ImmediateData", "Yes");
580 keys_add_int(request_keys, "MaxBurstLength",
581 ISCSI_MAX_DATA_SEGMENT_LENGTH);
582 keys_add_int(request_keys, "FirstBurstLength",
583 ISCSI_MAX_DATA_SEGMENT_LENGTH);
584 keys_add(request_keys, "InitialR2T", "Yes");
586 keys_add(request_keys, "HeaderDigest", "None");
587 keys_add(request_keys, "DataDigest", "None");
590 keys_add_int(request_keys, "MaxRecvDataSegmentLength",
591 ISCSI_MAX_DATA_SEGMENT_LENGTH);
592 keys_add(request_keys, "DefaultTime2Wait", "0");
593 keys_add(request_keys, "DefaultTime2Retain", "0");
594 keys_add(request_keys, "ErrorRecoveryLevel", "0");
595 keys_add(request_keys, "MaxOutstandingR2T", "1");
596 keys_save(request_keys, request);
597 keys_delete(request_keys);
603 response = login_receive(conn, false);
604 response_keys = keys_new();
605 keys_load(response_keys, response);
606 for (i = 0; i < KEYS_MAX; i++) {
607 if (response_keys->keys_names[i] == NULL)
610 login_negotiate_key(conn,
611 response_keys->keys_names[i], response_keys->keys_values[i]);
614 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
615 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0)
616 log_warnx("received final login response "
617 "without the \"T\" flag");
618 else if (login_nsg(response) != BHSLR_STAGE_FULL_FEATURE_PHASE)
619 log_warnx("received final login response with wrong NSG 0x%x",
620 login_nsg(response));
622 log_debugx("parameter negotiation done; "
623 "transitioning to Full Feature phase");
625 keys_delete(response_keys);
626 pdu_delete(response);
630 login_send_chap_a(struct connection *conn)
633 struct keys *request_keys;
635 request = login_new_request(conn);
636 request_keys = keys_new();
637 keys_add(request_keys, "CHAP_A", "5");
638 keys_save(request_keys, request);
639 keys_delete(request_keys);
645 login_send_chap_r(struct pdu *response)
647 struct connection *conn;
649 struct keys *request_keys, *response_keys;
650 const char *chap_a, *chap_c, *chap_i;
651 char *chap_r, *challenge, response_bin[MD5_DIGEST_LENGTH];
652 size_t challenge_len;
655 char *mutual_chap_c, mutual_chap_i[4];
658 * As in the rest of the initiator, 'request' means
659 * 'initiator -> target', and 'response' means 'target -> initiator',
661 * So, here the 'response' from the target is the packet that contains
662 * CHAP challenge; our CHAP response goes into 'request'.
665 conn = response->pdu_connection;
667 response_keys = keys_new();
668 keys_load(response_keys, response);
671 * First, compute the response.
673 chap_a = keys_find(response_keys, "CHAP_A");
675 log_errx(1, "received CHAP packet without CHAP_A");
676 chap_c = keys_find(response_keys, "CHAP_C");
678 log_errx(1, "received CHAP packet without CHAP_C");
679 chap_i = keys_find(response_keys, "CHAP_I");
681 log_errx(1, "received CHAP packet without CHAP_I");
683 if (strcmp(chap_a, "5") != 0)
684 log_errx(1, "received CHAP packet "
685 "with unsupported CHAP_A \"%s\"", chap_a);
686 id = strtoul(chap_i, NULL, 10);
687 error = login_hex2bin(chap_c, &challenge, &challenge_len);
689 log_errx(1, "received CHAP packet with malformed CHAP_C");
690 login_compute_md5(id, conn->conn_conf.isc_secret,
691 challenge, challenge_len, response_bin, sizeof(response_bin));
693 chap_r = login_bin2hex(response_bin, sizeof(response_bin));
695 keys_delete(response_keys);
697 request = login_new_request(conn);
698 request_keys = keys_new();
699 keys_add(request_keys, "CHAP_N", conn->conn_conf.isc_user);
700 keys_add(request_keys, "CHAP_R", chap_r);
704 * If we want mutual authentication, we're expected to send
705 * our CHAP_I/CHAP_C now.
707 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
708 log_debugx("requesting mutual authentication; "
709 "binary challenge size is %zd bytes",
710 sizeof(conn->conn_mutual_challenge));
712 rv = RAND_bytes(conn->conn_mutual_challenge,
713 sizeof(conn->conn_mutual_challenge));
715 log_errx(1, "RAND_bytes failed: %s",
716 ERR_error_string(ERR_get_error(), NULL));
718 rv = RAND_bytes(&conn->conn_mutual_id,
719 sizeof(conn->conn_mutual_id));
721 log_errx(1, "RAND_bytes failed: %s",
722 ERR_error_string(ERR_get_error(), NULL));
724 mutual_chap_c = login_bin2hex(conn->conn_mutual_challenge,
725 sizeof(conn->conn_mutual_challenge));
726 snprintf(mutual_chap_i, sizeof(mutual_chap_i),
727 "%d", conn->conn_mutual_id);
728 keys_add(request_keys, "CHAP_I", mutual_chap_i);
729 keys_add(request_keys, "CHAP_C", mutual_chap_c);
733 keys_save(request_keys, request);
734 keys_delete(request_keys);
740 login_verify_mutual(const struct pdu *response)
742 struct connection *conn;
743 struct keys *response_keys;
744 const char *chap_n, *chap_r;
745 char *response_bin, expected_response_bin[MD5_DIGEST_LENGTH];
746 size_t response_bin_len;
749 conn = response->pdu_connection;
751 response_keys = keys_new();
752 keys_load(response_keys, response);
754 chap_n = keys_find(response_keys, "CHAP_N");
756 log_errx(1, "received CHAP Response PDU without CHAP_N");
757 chap_r = keys_find(response_keys, "CHAP_R");
759 log_errx(1, "received CHAP Response PDU without CHAP_R");
760 error = login_hex2bin(chap_r, &response_bin, &response_bin_len);
762 log_errx(1, "received CHAP Response PDU with malformed CHAP_R");
764 if (strcmp(chap_n, conn->conn_conf.isc_mutual_user) != 0) {
765 fail(conn, "Mutual CHAP failed");
766 log_errx(1, "mutual CHAP authentication failed: wrong user");
769 login_compute_md5(conn->conn_mutual_id,
770 conn->conn_conf.isc_mutual_secret, conn->conn_mutual_challenge,
771 sizeof(conn->conn_mutual_challenge), expected_response_bin,
772 sizeof(expected_response_bin));
774 if (memcmp(response_bin, expected_response_bin,
775 sizeof(expected_response_bin)) != 0) {
776 fail(conn, "Mutual CHAP failed");
777 log_errx(1, "mutual CHAP authentication failed: wrong secret");
780 keys_delete(response_keys);
783 log_debugx("mutual CHAP authentication succeeded");
787 login_chap(struct connection *conn)
789 struct pdu *response;
791 log_debugx("beginning CHAP authentication; sending CHAP_A");
792 login_send_chap_a(conn);
794 log_debugx("waiting for CHAP_A/CHAP_C/CHAP_I");
795 response = login_receive(conn, false);
797 log_debugx("sending CHAP_N/CHAP_R");
798 login_send_chap_r(response);
799 pdu_delete(response);
802 * XXX: Make sure this is not susceptible to MITM.
805 log_debugx("waiting for CHAP result");
806 response = login_receive(conn, false);
807 if (conn->conn_conf.isc_mutual_user[0] != '\0')
808 login_verify_mutual(response);
809 pdu_delete(response);
811 log_debugx("CHAP authentication done");
815 login(struct connection *conn)
817 struct pdu *request, *response;
818 struct keys *request_keys, *response_keys;
819 struct iscsi_bhs_login_request *bhslr;
820 struct iscsi_bhs_login_response *bhslr2;
821 const char *auth_method;
824 log_debugx("beginning Login phase; sending Login PDU");
825 request = login_new_request(conn);
827 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
829 request_keys = keys_new();
830 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
831 keys_add(request_keys, "AuthMethod", "CHAP");
832 } else if (conn->conn_conf.isc_user[0] != '\0') {
834 * Give target a chance to skip authentication if it
835 * doesn't feel like it.
837 * None is first, CHAP second; this is to work around
838 * what seems to be LIO (Linux target) bug: otherwise,
839 * if target is configured with no authentication,
840 * and we are configured to authenticate, the target
841 * will erroneously respond with AuthMethod=CHAP
842 * instead of AuthMethod=None, and will subsequently
843 * fail the connection. This usually happens with
844 * Discovery sessions, which default to no authentication.
846 keys_add(request_keys, "AuthMethod", "None,CHAP");
848 keys_add(request_keys, "AuthMethod", "None");
850 keys_add(request_keys, "InitiatorName",
851 conn->conn_conf.isc_initiator);
852 if (conn->conn_conf.isc_initiator_alias[0] != '\0') {
853 keys_add(request_keys, "InitiatorAlias",
854 conn->conn_conf.isc_initiator_alias);
856 if (conn->conn_conf.isc_discovery == 0) {
857 keys_add(request_keys, "SessionType", "Normal");
858 keys_add(request_keys,
859 "TargetName", conn->conn_conf.isc_target);
861 keys_add(request_keys, "SessionType", "Discovery");
863 keys_save(request_keys, request);
864 keys_delete(request_keys);
868 response = login_receive(conn, true);
870 response_keys = keys_new();
871 keys_load(response_keys, response);
873 for (i = 0; i < KEYS_MAX; i++) {
874 if (response_keys->keys_names[i] == NULL)
878 * Not interested in AuthMethod at this point; we only need
879 * to parse things such as TargetAlias.
881 * XXX: This is somewhat ugly. We should have a way to apply
882 * all the keys to the session and use that by default
883 * instead of discarding them.
885 if (strcmp(response_keys->keys_names[i], "AuthMethod") == 0)
888 login_negotiate_key(conn,
889 response_keys->keys_names[i], response_keys->keys_values[i]);
892 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
893 if ((bhslr2->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0 &&
894 login_nsg(response) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
895 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
896 log_errx(1, "target requested transition "
897 "to operational negotiation, but we require "
901 log_debugx("target requested transition "
902 "to operational negotiation");
903 keys_delete(response_keys);
904 pdu_delete(response);
905 login_negotiate(conn);
909 auth_method = keys_find(response_keys, "AuthMethod");
910 if (auth_method == NULL)
911 log_errx(1, "received response without AuthMethod");
912 if (strcmp(auth_method, "None") == 0) {
913 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
914 log_errx(1, "target does not require authantication, "
915 "but we require mutual CHAP");
918 log_debugx("target does not require authentication");
919 keys_delete(response_keys);
920 pdu_delete(response);
921 login_negotiate(conn);
925 if (strcmp(auth_method, "CHAP") != 0) {
926 fail(conn, "Unsupported AuthMethod");
927 log_errx(1, "received response "
928 "with unsupported AuthMethod \"%s\"", auth_method);
931 if (conn->conn_conf.isc_user[0] == '\0' ||
932 conn->conn_conf.isc_secret[0] == '\0') {
933 fail(conn, "Authentication required");
934 log_errx(1, "target requests CHAP authentication, but we don't "
935 "have user and secret");
938 keys_delete(response_keys);
939 response_keys = NULL;
940 pdu_delete(response);
944 login_negotiate(conn);