View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.server.ldap.handlers.sasl;
21  
22  
23  import javax.security.sasl.SaslServer;
24  
25  import org.apache.commons.lang3.exception.ExceptionUtils;
26  import org.apache.directory.api.ldap.model.exception.LdapAuthenticationException;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.exception.LdapOperationException;
29  import org.apache.directory.api.ldap.model.message.BindRequest;
30  import org.apache.directory.api.ldap.model.message.BindResponse;
31  import org.apache.directory.api.ldap.model.message.LdapResult;
32  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
33  import org.apache.directory.api.ldap.model.name.Dn;
34  import org.apache.directory.server.core.api.CoreSession;
35  import org.apache.directory.server.core.api.OperationEnum;
36  import org.apache.directory.server.core.api.interceptor.context.BindOperationContext;
37  import org.apache.directory.server.ldap.LdapProtocolUtils;
38  import org.apache.directory.server.ldap.LdapSession;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  
43  /**
44   * A Dummy mechanism handler for Simple mechanism: not really used but needed
45   * for the mechanism map.
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class SimpleMechanismHandler implements MechanismHandler
50  {
51      /** The logger instance */
52      private static final Logger LOG = LoggerFactory.getLogger( SimpleMechanismHandler.class );
53  
54  
55      @Override
56      public SaslServer handleMechanism( LdapSession ldapSession, BindRequest bindRequest ) throws Exception
57      {
58          // create a new Bind context, with a null session, as we don't have
59          // any context yet.
60          BindOperationContextnterceptor/context/BindOperationContext.html#BindOperationContext">BindOperationContext bindContext = new BindOperationContext( null );
61  
62          // Stores the Dn of the user to check, and its password
63          bindContext.setDn( bindRequest.getDn() );
64          bindContext.setCredentials( bindRequest.getCredentials() );
65          bindContext.setInterceptors( ldapSession.getLdapServer().getDirectoryService()
66              .getInterceptors( OperationEnum.BIND ) );
67  
68          // Stores the request controls into the operation context
69          LdapProtocolUtils.setRequestControls( bindContext, bindRequest );
70  
71          try
72          {
73              CoreSession adminSession = ldapSession.getLdapServer().getDirectoryService().getAdminSession();
74  
75              // And call the OperationManager bind operation.
76              adminSession.getDirectoryService().getOperationManager().bind( bindContext );
77  
78              // As a result, store the created session in the Core Session
79              ldapSession.setCoreSession( bindContext.getSession() );
80  
81              // Return the successful response
82              BindResponse response = ( BindResponse ) bindRequest.getResultResponse();
83              response.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
84              LdapProtocolUtils.setResponseControls( bindContext, response );
85  
86              // Write it back to the client
87              ldapSession.getIoSession().write( response );
88              LOG.debug( "Returned SUCCESS message: {}.", response );
89          }
90          catch ( LdapException e )
91          {
92              // Something went wrong. Write back an error message
93              ResultCodeEnum code = null;
94              LdapResult result = bindRequest.getResultResponse().getLdapResult();
95  
96              if ( e instanceof LdapOperationException )
97              {
98                  code = ( ( LdapOperationException ) e ).getResultCode();
99                  result.setResultCode( code );
100             }
101             else
102             {
103                 code = ResultCodeEnum.getBestEstimate( e, bindRequest.getType() );
104                 result.setResultCode( code );
105             }
106 
107             String msg = "Bind failed: " + e.getLocalizedMessage();
108 
109             if ( LOG.isDebugEnabled() )
110             {
111                 msg += ":\n" + ExceptionUtils.getStackTrace( e );
112                 msg += "\n\nBindRequest = \n" + bindRequest.toString();
113             }
114 
115             Dn name = null;
116 
117             if ( e instanceof LdapAuthenticationException )
118             {
119                 name = ( ( LdapAuthenticationException ) e ).getResolvedDn();
120             }
121 
122             if ( ( name != null )
123                 && ( ( code == ResultCodeEnum.NO_SUCH_OBJECT ) || ( code == ResultCodeEnum.ALIAS_PROBLEM )
124                     || ( code == ResultCodeEnum.INVALID_DN_SYNTAX ) || ( code == ResultCodeEnum.ALIAS_DEREFERENCING_PROBLEM ) ) )
125             {
126                 result.setMatchedDn( name );
127             }
128 
129             result.setDiagnosticMessage( msg );
130             ldapSession.getIoSession().write( bindRequest.getResultResponse() );
131         }
132 
133         return null;
134     }
135 
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public void init( LdapSession ldapSession )
142     {
143         // Do nothing
144     }
145 
146 
147     /**
148      * {@inheritDoc}
149      */
150     @Override
151     public void cleanup( LdapSession ldapSession )
152     {
153         ldapSession.clearSaslProperties();
154     }
155 }