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;
21
22
23 import org.apache.directory.server.core.CoreSession;
24 import org.apache.directory.server.core.interceptor.context.BindOperationContext;
25 import org.apache.directory.server.ldap.LdapProtocolUtils;
26 import org.apache.directory.server.ldap.LdapSession;
27 import org.apache.directory.shared.ldap.exception.LdapAuthenticationException;
28 import org.apache.directory.shared.ldap.exception.LdapException;
29 import org.apache.directory.shared.ldap.message.BindRequest;
30 import org.apache.directory.shared.ldap.message.BindResponse;
31 import org.apache.directory.shared.ldap.message.LdapResult;
32 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
33 import org.apache.directory.shared.ldap.name.LdapDN;
34 import org.apache.directory.shared.ldap.util.ExceptionUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import javax.naming.Name;
39 import javax.security.sasl.SaslServer;
40
41
42
43
44
45
46
47
48
49
50 public class SimpleMechanismHandler implements MechanismHandler
51 {
52
53 private static final Logger LOG = LoggerFactory.getLogger( SimpleMechanismHandler.class );
54
55
56 public SaslServer handleMechanism( LdapSession ldapSession, BindRequest bindRequest ) throws Exception
57 {
58
59
60 BindOperationContext opContext = new BindOperationContext( null );
61
62
63 opContext.setDn( bindRequest.getName() );
64 opContext.setCredentials( bindRequest.getCredentials() );
65
66
67 LdapProtocolUtils.setRequestControls( opContext, bindRequest );
68
69 try
70 {
71 CoreSession adminSession = ldapSession.getLdapServer().getDirectoryService().getAdminSession();
72
73
74 adminSession.getDirectoryService().getOperationManager().bind( opContext );
75
76
77 ldapSession.setCoreSession( opContext.getSession() );
78
79
80 BindResponse response = ( BindResponse ) bindRequest.getResultResponse();
81 response.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
82 LdapProtocolUtils.setResponseControls( opContext, response );
83
84
85 ldapSession.getIoSession().write( response );
86 LOG.debug( "Returned SUCCESS message: {}.", response );
87 }
88 catch ( Exception e )
89 {
90
91 ResultCodeEnum code = null;
92 LdapResult result = bindRequest.getResultResponse().getLdapResult();
93
94 if ( e instanceof LdapException )
95 {
96 code = ( ( LdapException ) e ).getResultCode();
97 result.setResultCode( code );
98 }
99 else
100 {
101 code = ResultCodeEnum.getBestEstimate( e, bindRequest.getType() );
102 result.setResultCode( code );
103 }
104
105 String msg = "Bind failed: " + e.getMessage();
106
107 if ( LOG.isDebugEnabled() )
108 {
109 msg += ":\n" + ExceptionUtils.getStackTrace( e );
110 msg += "\n\nBindRequest = \n" + bindRequest.toString();
111 }
112
113 Name name = null;
114
115 if ( e instanceof LdapAuthenticationException )
116 {
117 name = ((LdapAuthenticationException)e).getResolvedName();
118 }
119
120 if ( ( name != null )
121 && ( ( code == ResultCodeEnum.NO_SUCH_OBJECT ) || ( code == ResultCodeEnum.ALIAS_PROBLEM )
122 || ( code == ResultCodeEnum.INVALID_DN_SYNTAX ) || ( code == ResultCodeEnum.ALIAS_DEREFERENCING_PROBLEM ) ) )
123 {
124 result.setMatchedDn( new LdapDN( name ) );
125 }
126
127 result.setErrorMessage( msg );
128 ldapSession.getIoSession().write( bindRequest.getResultResponse() );
129 }
130
131 return null;
132 }
133
134
135
136
137
138 public void init( LdapSession ldapSession )
139 {
140
141 }
142
143
144
145
146
147 public void cleanup( LdapSession ldapSession )
148 {
149 ldapSession.clearSaslProperties();
150 }
151 }