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.kerberos.changepwd.messages;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.DecoderException;
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.Asn1Decoder;
28  import org.apache.directory.api.util.Strings;
29  import org.apache.directory.server.kerberos.changepwd.exceptions.ChangePasswdErrorType;
30  import org.apache.directory.server.kerberos.changepwd.exceptions.ChangePasswordException;
31  import org.apache.directory.shared.kerberos.codec.krbError.KrbErrorContainer;
32  import org.apache.directory.shared.kerberos.messages.KrbError;
33  
34  
35  /**
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class ChangePasswordError extends AbstractPasswordMessage
40  {
41      private KrbError krbError;
42  
43      private short krbErrorLen;
44      private short messageLength;
45  
46      public ChangePasswordError( KrbError krbError )
47      {
48          this( PVNO, krbError );
49      }
50      
51      /**
52       * Creates a new instance of ChangePasswordError.
53       *
54       * @param versionNumber The version number 
55       * @param krbError The KRB-ERROR
56       */
57      public ChangePasswordError( short versionNumber, KrbError krbError )
58      {
59          super( versionNumber );
60  
61          this.krbError = krbError;
62      }
63  
64  
65      /**
66       * Returns the {@link KrbError}.
67       *
68       * @return The {@link KrbError}.
69       */
70      public KrbError getKrbError()
71      {
72          return krbError;
73      }
74  
75  
76      @Override
77      public short computeLength()
78      {
79          krbErrorLen = ( short ) krbError.computeLength();
80          messageLength = ( short ) ( HEADER_LENGTH + krbErrorLen );
81          
82          return messageLength;
83      }
84  
85      @Override
86      public ByteBuffer encode( ByteBuffer buf ) throws EncoderException
87      {
88          buf.putShort( messageLength );
89  
90          buf.putShort( getVersionNumber() );
91  
92          buf.putShort( ( short )0 ); // zero means, what follows is an error
93  
94          krbError.encode( buf );
95          
96          return buf;
97      }
98  
99      /**
100      * Decodes a {@link ByteBuffer} into a {@link ChangePasswordError}.
101      *
102      * @param buf The buffer containing the ChangePasswordError to decode
103      * @return The {@link ChangePasswordError}.
104      * @throws ChangePasswordException If the decoding failed
105      */
106     public static ChangePasswordError decode( ByteBuffer buf ) throws ChangePasswordException
107     {
108         short messageLength = buf.getShort();
109 
110         short pvno = buf.getShort();
111 
112         // AP_REQ length will be 0 for error messages
113         buf.getShort(); // authHeader length
114 
115         int errorLength = messageLength - HEADER_LENGTH;
116 
117         byte[] errorBytes = new byte[errorLength];
118 
119         buf.get( errorBytes );
120         ByteBuffer errorBuffer = ByteBuffer.wrap( errorBytes );
121 
122         KrbErrorContainerros/codec/krbError/KrbErrorContainer.html#KrbErrorContainer">KrbErrorContainer container = new KrbErrorContainer( errorBuffer );
123 
124         try
125         {
126             Asn1Decoder.decode( errorBuffer, container );
127         }
128         catch( DecoderException e )
129         {
130             throw new ChangePasswordException( ChangePasswdErrorType.KRB5_KPASSWD_MALFORMED, e );
131         }
132         
133         KrbError errorMessage = container.getKrbError();
134 
135         return new ChangePasswordError( pvno, errorMessage );
136     }
137 
138     public ChangePasswdErrorType getResultCode()
139     {
140         ByteBuffer buf = ByteBuffer.wrap( krbError.getEData() );
141         
142         return ChangePasswdErrorType.getTypeByValue( buf.getShort() );
143     }
144     
145     public String getResultString()
146     {
147         byte[] edata = krbError.getEData();
148 
149         // first two bytes contain the result code
150         return Strings.utf8ToString( edata, 2, edata.length - 2 );
151     }
152 }