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.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   * A Dummy mechanism handler for Simple mechanism: not really used but needed
44   * for the mechanism map.
45   *
46   * @org.apache.xbean.XBean
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   * @version $$Rev$$
49   */
50  public class SimpleMechanismHandler implements MechanismHandler
51  {
52      /** The logger instance */
53      private static final Logger LOG = LoggerFactory.getLogger( SimpleMechanismHandler.class );
54  
55      
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          BindOperationContext opContext = new BindOperationContext( null );
61          
62          // Stores the DN of the user to check, and its password
63          opContext.setDn( bindRequest.getName() );
64          opContext.setCredentials( bindRequest.getCredentials() );
65  
66          // Stores the request controls into the operation context
67          LdapProtocolUtils.setRequestControls( opContext, bindRequest );
68          
69          try
70          {
71              CoreSession adminSession = ldapSession.getLdapServer().getDirectoryService().getAdminSession();
72  
73              // And call the OperationManager bind operation.
74              adminSession.getDirectoryService().getOperationManager().bind( opContext );
75              
76              // As a result, store the created session in the Core Session
77              ldapSession.setCoreSession( opContext.getSession() );
78              
79              // Return the successful response
80              BindResponse response = ( BindResponse ) bindRequest.getResultResponse();
81              response.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
82              LdapProtocolUtils.setResponseControls( opContext, response );
83              
84              // Write it back to the client
85              ldapSession.getIoSession().write( response );
86              LOG.debug( "Returned SUCCESS message: {}.", response );
87          }
88          catch ( Exception e )
89          {
90              // Something went wrong. Write back an error message            
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      * {@inheritDoc}
137      */
138     public void init( LdapSession ldapSession )
139     {
140         // Do nothing
141     }
142 
143 
144     /**
145      * {@inheritDoc}
146      */
147     public void cleanup( LdapSession ldapSession )
148     {
149         ldapSession.clearSaslProperties();
150     }
151 }