| 1 | |
package org.jscsi.target.connection.stage.login; |
| 2 | |
|
| 3 | |
|
| 4 | |
import java.io.IOException; |
| 5 | |
import java.security.DigestException; |
| 6 | |
import java.util.List; |
| 7 | |
import java.util.Vector; |
| 8 | |
|
| 9 | |
import org.jscsi.exception.InternetSCSIException; |
| 10 | |
import org.jscsi.parser.BasicHeaderSegment; |
| 11 | |
import org.jscsi.parser.ProtocolDataUnit; |
| 12 | |
import org.jscsi.parser.login.LoginStage; |
| 13 | |
import org.jscsi.parser.login.LoginStatus; |
| 14 | |
import org.jscsi.target.connection.phase.TargetLoginPhase; |
| 15 | |
import org.jscsi.target.settings.SettingsException; |
| 16 | |
import org.jscsi.target.settings.TextKeyword; |
| 17 | |
import org.jscsi.target.settings.TextParameter; |
| 18 | |
import org.slf4j.Logger; |
| 19 | |
import org.slf4j.LoggerFactory; |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
public final class SecurityNegotiationStage extends TargetLoginStage { |
| 28 | |
|
| 29 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(SecurityNegotiationStage.class); |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public SecurityNegotiationStage (TargetLoginPhase targetLoginPhase) { |
| 37 | 0 | super(targetLoginPhase, LoginStage.SECURITY_NEGOTIATION); |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
@Override |
| 41 | |
public void execute (ProtocolDataUnit initialPdu) throws IOException , InterruptedException , InternetSCSIException , DigestException , SettingsException { |
| 42 | |
|
| 43 | |
|
| 44 | 0 | BasicHeaderSegment bhs = initialPdu.getBasicHeaderSegment(); |
| 45 | 0 | initiatorTaskTag = bhs.getInitiatorTaskTag(); |
| 46 | |
|
| 47 | 0 | boolean authenticated = false; |
| 48 | |
|
| 49 | |
do { |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 0 | final String requestTextParameters = receivePduSequence(initialPdu); |
| 54 | |
|
| 55 | |
|
| 56 | 0 | final List<String> requestKeyValuePairs = TextParameter.tokenizeKeyValuePairs(requestTextParameters); |
| 57 | |
|
| 58 | |
|
| 59 | 0 | final List<String> authMethodKeyValuePairs = new Vector<String>(); |
| 60 | |
|
| 61 | |
|
| 62 | 0 | if (LOGGER.isDebugEnabled()) { |
| 63 | 0 | final StringBuilder sb = new StringBuilder(); |
| 64 | 0 | sb.append("request key value pairs:\n"); |
| 65 | 0 | for (String s : requestKeyValuePairs) |
| 66 | 0 | sb.append(" " + s + "\n"); |
| 67 | 0 | LOGGER.debug(sb.toString()); |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 0 | String authMethodValues = null; |
| 74 | 0 | if (!authenticated) { |
| 75 | 0 | for (int i = 0; i < requestKeyValuePairs.size(); ++i) { |
| 76 | 0 | final String[] split = TextParameter.splitKeyValuePair(requestKeyValuePairs.get(i)); |
| 77 | 0 | if (split == null) { |
| 78 | 0 | sendRejectPdu(LoginStatus.INITIATOR_ERROR); |
| 79 | 0 | throw new InternetSCSIException("key=value format error: " + requestKeyValuePairs.get(i)); |
| 80 | |
} |
| 81 | 0 | if (TextKeyword.AUTH_METHOD.equals(split[0])) { |
| 82 | 0 | authMethodValues = split[1]; |
| 83 | |
|
| 84 | 0 | requestKeyValuePairs.remove(i--); |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | 0 | } else if (isAuthenticationKey(split[0])) { |
| 89 | |
|
| 90 | 0 | authMethodKeyValuePairs.add(requestKeyValuePairs.remove(i--)); |
| 91 | |
|
| 92 | |
} |
| 93 | |
} |
| 94 | 0 | if (authMethodValues == null) { |
| 95 | 0 | sendRejectPdu(LoginStatus.MISSING_PARAMETER); |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | 0 | throw new InternetSCSIException("Missing AuthMethod key-value pair"); |
| 103 | |
} |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | 0 | final Vector<String> responseKeyValuePairs = new Vector<String>(); |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | 0 | if (!negotiator.negotiate(session.getTargetServer(), stageNumber, connection.isLeadingConnection(), ((TargetLoginPhase) targetPhase).getFirstPduAndSetToFalse(), requestKeyValuePairs, responseKeyValuePairs)) { |
| 113 | |
|
| 114 | 0 | sendRejectPdu(LoginStatus.INITIATOR_ERROR); |
| 115 | 0 | throw new InternetSCSIException("negotiation failure"); |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | 0 | if (!authenticated) { |
| 120 | 0 | if (authMethodValues.contains(TextKeyword.NONE)) { |
| 121 | |
|
| 122 | 0 | authenticated = true; |
| 123 | 0 | responseKeyValuePairs.add(TextParameter.toKeyValuePair(TextKeyword.AUTH_METHOD, |
| 124 | |
TextKeyword.NONE)); |
| 125 | |
|
| 126 | |
|
| 127 | 0 | final String responseString = TextParameter.concatenateKeyValuePairs(responseKeyValuePairs); |
| 128 | |
|
| 129 | 0 | if (LOGGER.isDebugEnabled()) LOGGER.debug("response: " + responseString); |
| 130 | |
|
| 131 | |
|
| 132 | 0 | sendPduSequence(responseString, requestedNextStageNumber); |
| 133 | |
|
| 134 | |
|
| 135 | 0 | if (requestedNextStageNumber == LoginStage.LOGIN_OPERATIONAL_NEGOTIATION || requestedNextStageNumber == LoginStage.FULL_FEATURE_PHASE) { |
| 136 | 0 | nextStageNumber = requestedNextStageNumber; |
| 137 | 0 | return; |
| 138 | |
} |
| 139 | 0 | } else { |
| 140 | |
|
| 141 | |
|
| 142 | 0 | LOGGER.error("initiator attempted CHAP authentication"); |
| 143 | |
|
| 144 | 0 | return; |
| 145 | |
} |
| 146 | |
|
| 147 | |
} |
| 148 | |
|
| 149 | 0 | } while (!bhs.isFinalFlag() && !authenticated); |
| 150 | 0 | } |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
private final boolean isAuthenticationKey (final String key) { |
| 166 | 0 | if (key == null || key.length() < 5) return false; |
| 167 | 0 | final String fourChars = key.substring(0, 4); |
| 168 | 0 | final String fiveChars = key.substring(0, 5); |
| 169 | 0 | if ("CHAP_".matches(fiveChars) || "KRB_".matches(fourChars) || "SPKM_".matches(fiveChars) || "SRP_".matches(fourChars) || (key.length() >= 10 && "TargetAuth".matches(key.substring(0, 10)))) return true; |
| 170 | 0 | return false; |
| 171 | |
} |
| 172 | |
} |