1 package org.jscsi.target.connection.phase;
2
3
4 import java.io.IOException;
5 import java.security.DigestException;
6
7 import javax.naming.OperationNotSupportedException;
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.LoginRequestParser;
13 import org.jscsi.parser.login.LoginStage;
14 import org.jscsi.target.connection.Connection;
15 import org.jscsi.target.connection.stage.login.LoginOperationalParameterNegotiationStage;
16 import org.jscsi.target.connection.stage.login.SecurityNegotiationStage;
17 import org.jscsi.target.connection.stage.login.TargetLoginStage;
18 import org.jscsi.target.settings.ConnectionSettingsNegotiator;
19 import org.jscsi.target.settings.SettingsException;
20
21
22
23
24
25
26
27
28 public final class TargetLoginPhase extends TargetPhase {
29
30
31
32
33 private TargetLoginStage stage;
34
35
36
37
38
39
40
41
42 private boolean authenticated = true;
43
44
45
46
47
48
49
50
51
52 private boolean firstPdu = true;
53
54
55
56
57
58
59 public TargetLoginPhase (Connection connection) {
60 super(connection);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75 @Override
76 public boolean execute (ProtocolDataUnit pdu) throws IOException , InterruptedException , InternetSCSIException , DigestException , SettingsException {
77
78
79 final ConnectionSettingsNegotiator negotiator = connection.getConnectionSettingsNegotiator();
80 while (!negotiator.beginNegotiation()) {
81
82 }
83
84 boolean loginSuccessful = true;
85
86
87 try {
88
89 BasicHeaderSegment bhs = pdu.getBasicHeaderSegment();
90 LoginRequestParser parser = (LoginRequestParser) bhs.getParser();
91
92 LoginStage nextStageNumber;
93
94
95
96 if (parser.getCurrentStageNumber() == LoginStage.SECURITY_NEGOTIATION) {
97
98 stage = new SecurityNegotiationStage(this);
99 stage.execute(pdu);
100 nextStageNumber = stage.getNextStageNumber();
101
102 if (nextStageNumber != null)
103 authenticated = true;
104 else {
105 loginSuccessful = false;
106 return false;
107 }
108
109 if (nextStageNumber == LoginStage.LOGIN_OPERATIONAL_NEGOTIATION) {
110
111 pdu = connection.receivePdu();
112 bhs = pdu.getBasicHeaderSegment();
113 parser = (LoginRequestParser) bhs.getParser();
114 } else if (nextStageNumber == LoginStage.FULL_FEATURE_PHASE) {
115
116 return true;
117 } else {
118
119 loginSuccessful = false;
120 return false;
121 }
122 }
123
124
125
126 if (parser != null && authenticated && parser.getCurrentStageNumber() == LoginStage.LOGIN_OPERATIONAL_NEGOTIATION) {
127 stage = new LoginOperationalParameterNegotiationStage(this);
128 stage.execute(pdu);
129 nextStageNumber = stage.getNextStageNumber();
130 if (nextStageNumber == LoginStage.FULL_FEATURE_PHASE) return true;
131 }
132
133 loginSuccessful = false;
134 return false;
135 } catch (DigestException e) {
136 loginSuccessful = false;
137 throw e;
138 } catch (IOException e) {
139 loginSuccessful = false;
140 throw e;
141 } catch (InterruptedException e) {
142 loginSuccessful = false;
143 throw e;
144 } catch (InternetSCSIException e) {
145 loginSuccessful = false;
146 throw e;
147 } catch (SettingsException e) {
148 loginSuccessful = false;
149 throw e;
150 } finally {
151
152 negotiator.finishNegotiation(loginSuccessful);
153 }
154 }
155
156
157
158
159
160
161
162 public boolean getFirstPduAndSetToFalse () {
163 if (!firstPdu) return false;
164 firstPdu = false;
165 return true;
166 }
167
168 public final boolean getAuthenticated () {
169 return authenticated;
170 }
171 }