1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.ldap.handlers.bind.plain;
21
22
23 import java.io.IOException;
24
25 import org.apache.directory.server.core.CoreSession;
26 import org.apache.directory.server.core.interceptor.context.BindOperationContext;
27 import org.apache.directory.server.ldap.LdapSession;
28 import org.apache.directory.server.ldap.handlers.bind.AbstractSaslServer;
29 import org.apache.directory.shared.ldap.constants.SupportedSaslMechanisms;
30 import org.apache.directory.shared.ldap.message.BindRequest;
31 import org.apache.directory.shared.ldap.name.LdapDN;
32 import org.apache.directory.shared.ldap.schema.PrepareString;
33 import org.apache.directory.shared.ldap.util.StringTools;
34
35 import javax.naming.InvalidNameException;
36 import javax.security.sasl.SaslException;
37
38
39
40
41
42
43
44
45
46
47 public class PlainSaslServer extends AbstractSaslServer
48 {
49
50 public static final String SASL_PLAIN_AUTHZID = "authzid";
51
52
53 public static final String SASL_PLAIN_AUTHCID = "authcid";
54
55
56 public static final String SASL_PLAIN_PASSWORD = "password";
57
58
59
60
61
62 private enum NegotiationState
63 {
64 INITIALIZED,
65 MECH_RECEIVED,
66 COMPLETED
67 }
68
69
70
71
72
73 private enum InitialResponse
74 {
75 AUTHZID_EXPECTED,
76 AUTHCID_EXPECTED,
77 PASSWORD_EXPECTED
78 }
79
80
81 private NegotiationState state;
82
83
84
85
86
87
88
89
90
91 public PlainSaslServer( LdapSession ldapSession, CoreSession adminSession, BindRequest bindRequest )
92 {
93 super( ldapSession, adminSession, bindRequest );
94 state = NegotiationState.INITIALIZED;
95
96
97 getLdapSession().removeSaslProperty( SASL_PLAIN_AUTHZID );
98 getLdapSession().removeSaslProperty( SASL_PLAIN_AUTHCID );
99 getLdapSession().removeSaslProperty( SASL_PLAIN_PASSWORD );
100 }
101
102
103
104
105
106 public String getMechanismName()
107 {
108 return SupportedSaslMechanisms.PLAIN;
109 }
110
111
112
113
114
115 public byte[] evaluateResponse( byte[] initialResponse ) throws SaslException
116 {
117 if ( StringTools.isEmpty( initialResponse ) )
118 {
119 state = NegotiationState.MECH_RECEIVED;
120 return null;
121 }
122 else
123 {
124
125
126
127
128 InitialResponse element = InitialResponse.AUTHZID_EXPECTED;
129 String authzId = null;
130 String authcId = null;
131 String password = null;
132
133 int start = 0;
134 int end = 0;
135
136 try
137 {
138 for ( byte b:initialResponse )
139 {
140 if ( b == '\0' )
141 {
142 if ( start - end == 0 )
143 {
144
145 if ( element == InitialResponse.AUTHZID_EXPECTED )
146 {
147
148
149 element = InitialResponse.AUTHCID_EXPECTED;
150 continue;
151 }
152 else
153 {
154
155 throw new IllegalArgumentException( "response with no auhcid or no password" );
156 }
157 }
158 else
159 {
160 start++;
161 String value = new String( initialResponse, start, end - start + 1, "UTF-8" );
162
163 switch ( element )
164 {
165 case AUTHZID_EXPECTED :
166 element = InitialResponse.AUTHCID_EXPECTED;
167 authzId = PrepareString.normalize( value, PrepareString.StringType.CASE_EXACT_IA5 );
168 end++;
169 start = end;
170 break;
171
172 case AUTHCID_EXPECTED :
173 element = InitialResponse.PASSWORD_EXPECTED;
174 authcId = PrepareString.normalize( value, PrepareString.StringType.DIRECTORY_STRING );
175 end++;
176 start = end;
177 break;
178
179
180 default :
181
182 throw new IllegalArgumentException( "'\0' chars are not allowed in authcid or no password" );
183 }
184 }
185 }
186 else
187 {
188 end++;
189 }
190 }
191
192 if ( start == end )
193 {
194 throw new IllegalArgumentException( "response with no auhcid or no password" );
195 }
196
197 start++;
198 String value = StringTools.utf8ToString( initialResponse, start, end - start + 1 );
199
200 password = PrepareString.normalize( value, PrepareString.StringType.CASE_EXACT_IA5 );
201
202 if ( ( authcId == null ) || ( password == null ) )
203 {
204 throw new IllegalArgumentException( "response with no auhcid or no password" );
205 }
206
207
208 CoreSession userSession = authenticate( authcId, password );
209
210 getLdapSession().setCoreSession( userSession );
211
212 state = NegotiationState.COMPLETED;
213 }
214 catch ( IOException ioe )
215 {
216 throw new IllegalArgumentException( "The given InitialReponse is incorrect" );
217 }
218 catch ( InvalidNameException ine )
219 {
220 throw new IllegalArgumentException( "Cannot authenticate an invalid authcid DN" );
221 }
222 catch ( Exception e )
223 {
224 throw new SaslException( "Cannot authenticate the user " + authcId );
225 }
226 }
227
228 return StringTools.EMPTY_BYTES;
229 }
230
231
232 public boolean isComplete()
233 {
234 return state == NegotiationState.COMPLETED;
235 }
236
237
238
239
240
241 private CoreSession authenticate( String user, String password ) throws InvalidNameException, Exception
242 {
243 BindOperationContext bindContext = new BindOperationContext( getLdapSession().getCoreSession() );
244 bindContext.setDn( new LdapDN( user ) );
245 bindContext.setCredentials( StringTools.getBytesUtf8( password ) );
246
247 getAdminSession().getDirectoryService().getOperationManager().bind( bindContext );
248
249 return bindContext.getSession();
250 }
251 }