View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.localhandler.packet;
22  
23  import goldengate.common.digest.FilesystemBasedDigest;
24  import openr66.protocol.configuration.Configuration;
25  import openr66.protocol.exception.OpenR66ProtocolNoSslException;
26  import openr66.protocol.exception.OpenR66ProtocolPacketException;
27  
28  import org.jboss.netty.buffer.ChannelBuffer;
29  import org.jboss.netty.buffer.ChannelBuffers;
30  
31  /**
32   * Request Authentication class
33   *
34   * header = "hostId" middle = "key bytes" end = localId + way
35   *
36   * @author frederic bregier
37   */
38  public class AuthentPacket extends AbstractLocalPacket {
39      private static final byte ASKVALIDATE = 0;
40  
41      private static final byte ANSWERVALIDATE = 1;
42  
43      private final Integer localId;
44  
45      private byte way;
46  
47      private String hostId;
48  
49      private byte[] key;
50  
51      /**
52       * @param headerLength
53       * @param middleLength
54       * @param endLength
55       * @param buf
56       * @return the new AuthentPacket from buffer
57       * @throws OpenR66ProtocolPacketException
58       */
59      public static AuthentPacket createFromBuffer(int headerLength,
60              int middleLength, int endLength, ChannelBuffer buf)
61              throws OpenR66ProtocolPacketException {
62          if (headerLength - 1 <= 0) {
63              throw new OpenR66ProtocolPacketException("Not enough data");
64          }
65          if (middleLength <= 0) {
66              throw new OpenR66ProtocolPacketException("Not enough data");
67          }
68          if (endLength < 5) {
69              throw new OpenR66ProtocolPacketException("Not enough data");
70          }
71          final byte[] bheader = new byte[headerLength - 1];
72          final byte[] bmiddle = new byte[middleLength];
73          if (headerLength - 1 > 0) {
74              buf.readBytes(bheader);
75          }
76          if (middleLength > 0) {
77              buf.readBytes(bmiddle);
78          }
79          Integer newId = buf.readInt();
80          byte valid = buf.readByte();
81          final String sheader = new String(bheader);
82          return new AuthentPacket(sheader, bmiddle, newId, valid);
83      }
84  
85      /**
86       * @param hostId
87       * @param key
88       * @param newId
89       * @param valid
90       */
91      private AuthentPacket(String hostId, byte[] key, Integer newId, byte valid) {
92          this.hostId = hostId;
93          this.key = key;
94          localId = newId;
95          way = valid;
96      }
97  
98      /**
99       * @param hostId
100      * @param key
101      * @param newId
102      */
103     public AuthentPacket(String hostId, byte[] key, Integer newId) {
104         this.hostId = hostId;
105         this.key = key;
106         localId = newId;
107         way = ASKVALIDATE;
108     }
109 
110     /*
111      * (non-Javadoc)
112      *
113      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#createEnd()
114      */
115     @Override
116     public void createEnd() throws OpenR66ProtocolPacketException {
117         end = ChannelBuffers.buffer(5);
118         end.writeInt(localId);
119         end.writeByte(way);
120     }
121 
122     /*
123      * (non-Javadoc)
124      *
125      * @see
126      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createHeader()
127      */
128     @Override
129     public void createHeader() throws OpenR66ProtocolPacketException {
130         if (hostId == null) {
131             throw new OpenR66ProtocolPacketException("Not enough data");
132         }
133         header = ChannelBuffers.wrappedBuffer(hostId.getBytes());
134     }
135 
136     /*
137      * (non-Javadoc)
138      *
139      * @see
140      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createMiddle()
141      */
142     @Override
143     public void createMiddle() throws OpenR66ProtocolPacketException {
144         if (key == null) {
145             throw new OpenR66ProtocolPacketException("Not enough data");
146         }
147         middle = ChannelBuffers.wrappedBuffer(key);
148     }
149 
150     @Override
151     public byte getType() {
152         return LocalPacketFactory.AUTHENTPACKET;
153     }
154 
155     /*
156      * (non-Javadoc)
157      *
158      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#toString()
159      */
160     @Override
161     public String toString() {
162         return "AuthentPacket: " + hostId + " " + localId + " " + way;
163     }
164 
165     /**
166      * @return the hostId
167      */
168     public String getHostId() {
169         return hostId;
170     }
171 
172     /**
173      * @return the key
174      */
175     public byte[] getKey() {
176         return key;
177     }
178 
179     /**
180      * @return the localId
181      */
182     public Integer getLocalId() {
183         return localId;
184     }
185 
186     /**
187      * @return True if this packet is to be validated
188      */
189     public boolean isToValidate() {
190         return way == ASKVALIDATE;
191     }
192 
193     /**
194      * Validate the connection
195      */
196     public void validate(boolean isSSL) {
197         way = ANSWERVALIDATE;
198         try {
199             hostId = Configuration.configuration.getHostId(isSSL);
200         } catch (OpenR66ProtocolNoSslException e) {
201             hostId = Configuration.configuration.HOST_ID;
202         }
203         key = FilesystemBasedDigest.passwdCrypt(Configuration.configuration.HOST_AUTH.getHostkey());
204         header = null;
205         middle = null;
206         end = null;
207     }
208 }