KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > ntlm > Type2NTLMMessage


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.ntlm;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.filesys.util.HexDump;
23
24 /**
25  * Type 2 NTLM Message Class
26  *
27  * @author GKSpencer
28  */

29 public class Type2NTLMMessage extends NTLMMessage
30 {
31     // Minimal type 2 message length
32

33     public static final int MinimalMessageLength = 32;
34     
35     // Type 2 field offsets
36

37     public static final int OffsetTarget = 12;
38     public static final int OffsetFlags = 20;
39     public static final int OffsetChallenge = 24;
40     public static final int OffsetContext = 32;
41     public static final int OffsetTargetInfo = 40; // optional
42

43     /**
44      * Default constructor
45      */

46     public Type2NTLMMessage()
47     {
48         super();
49     }
50     
51     /**
52      * Class constructor
53      *
54      * @param buf byte[]
55      */

56     public Type2NTLMMessage(byte[] buf)
57     {
58         super(buf, 0, buf.length);
59     }
60     
61     /**
62      * Class constructor
63      *
64      * @param buf byte[]
65      * @param offset int
66      * @param len int
67      */

68     public Type2NTLMMessage(byte[] buf, int offset, int len)
69     {
70         super(buf, offset, len);
71     }
72
73     /**
74      * Return the flags value
75      *
76      * @return int
77      */

78     public int getFlags()
79     {
80         return getIntValue(OffsetFlags);
81     }
82     
83     /**
84      * Check if the target name has been set
85      *
86      * @return boolean
87      */

88     public final boolean hasTarget()
89     {
90         return hasFlag(NTLM.FlagRequestTarget);
91     }
92     
93     /**
94      * Return the target name
95      *
96      * @return String
97      */

98     public final String JavaDoc getTarget()
99     {
100         return getStringValue(OffsetTarget, hasFlag(NTLM.FlagNegotiateUnicode));
101     }
102     
103     /**
104      * Return the challenge
105      *
106      * @return byte[]
107      */

108     public final byte[] getChallenge()
109     {
110         return getRawBytes(OffsetChallenge, 8);
111     }
112     
113     /**
114      * Check if the optional context field is present
115      *
116      * @return boolean
117      */

118     public final boolean hasContext()
119     {
120         return hasFlag(NTLM.FlagLocalCall);
121     }
122     
123     /**
124      * Return the context values
125      *
126      * @return int[]
127      */

128     public final int[] getContext()
129     {
130         if ( hasContext() == false)
131             return null;
132         
133         int[] ctx = new int[2];
134         
135         ctx[0] = getIntValue(OffsetContext);
136         ctx[1] = getIntValue(OffsetContext + 4);
137         
138         return ctx;
139     }
140     
141     /**
142      * Check if target information is present
143      *
144      * @return boolean
145      */

146     public final boolean hasTargetInformation()
147     {
148         return hasFlag(NTLM.FlagTargetInfo);
149     }
150     
151     /**
152      * Return the target information
153      *
154      * @return List<TargetInfo>
155      */

156     public final List JavaDoc<TargetInfo> getTargetInformation()
157     {
158         if ( hasTargetInformation() == false)
159             return null;
160         
161         // Get the target information block length and offset
162

163         int tLen = getStringLength(OffsetTargetInfo);
164         int tOff = getStringOffset(OffsetTargetInfo);
165         
166         List JavaDoc<TargetInfo> tList = new ArrayList JavaDoc<TargetInfo>();
167         if ( tLen == 0)
168             return tList;
169         
170         // Unpack the target information structures
171

172         int typ = -1;
173         int slen = -1;
174         String JavaDoc name = null;
175         
176         while ( typ != 0)
177         {
178             // Unpack the details for the current target
179

180             typ = getShortValue(tOff);
181             slen = getShortValue(tOff + 2);
182             
183             if ( slen > 0)
184                 name = getRawString(tOff + 4, slen/2, true);
185             else
186                 name = null;
187             
188             // Add the details to the list
189

190             if ( typ != 0)
191                 tList.add( new TargetInfo(typ, name));
192             
193             // Update the data offset
194

195             tOff += slen + 4;
196         }
197         
198         // Return the target list
199

200         return tList;
201     }
202     
203     /**
204      * Build a type 2 message
205      *
206      * @param flags int
207      * @param target String
208      * @param challenge byte[]
209      * @param ctx byte[]
210      * @param tList List<TargetInfo>
211      */

212     public final void buildType2(int flags, String JavaDoc target, byte[] challenge, int[] ctx, List JavaDoc<TargetInfo> tList)
213     {
214         // Initialize the header/flags
215

216         initializeHeader(NTLM.Type2, flags);
217
218         // Determine if strings are ASCII or Unicode
219

220         boolean isUni = hasFlag(NTLM.FlagNegotiateUnicode);
221         
222         int strOff = OffsetTargetInfo;
223         if ( tList != null)
224             strOff += 8;
225         
226         // Pack the target name
227

228         strOff = setStringValue(OffsetTarget, target, strOff, isUni);
229         
230         // Pack the challenge and context
231

232         if ( challenge != null)
233             setRawBytes(OffsetChallenge, challenge);
234         else
235             zeroBytes(OffsetChallenge, 8);
236         
237         if ( ctx != null)
238             setRawInts(OffsetContext, ctx);
239         else
240             zeroBytes(OffsetContext, 8);
241         
242         // Pack the target information, if specified
243

244         if ( tList != null)
245         {
246             // Clear the target information length and set the data offset
247

248             setIntValue(OffsetTargetInfo, 0);
249             setIntValue(OffsetTargetInfo+4, strOff);
250             
251             int startOff = strOff;
252             
253             // Pack the target information structures
254

255             for ( TargetInfo tInfo : tList)
256             {
257                 // Pack the target information structure
258

259                 setShortValue(strOff, tInfo.isType());
260                 
261                 int tLen = tInfo.getName().length();
262                 if ( isUni)
263                     tLen *= 2;
264                 setShortValue(strOff+2, tLen);
265                 strOff = setRawString(strOff+4, tInfo.getName(), isUni);
266             }
267             
268             // Add the list terminator
269

270             zeroBytes(strOff, 4);
271             strOff += 4;
272             
273             // Set the target information block length
274

275             setShortValue(OffsetTargetInfo, strOff - startOff);
276             setShortValue(OffsetTargetInfo+2, strOff - startOff);
277         }
278         
279         // Set the message length
280

281         setLength(strOff);
282     }
283     
284     /**
285      * Set the message flags
286      *
287      * @param flags int
288      */

289     protected void setFlags(int flags)
290     {
291         setIntValue( OffsetFlags, flags);
292     }
293     
294     /**
295      * Return the type 2 message as a string
296      *
297      * @return String
298      */

299     public String JavaDoc toString()
300     {
301         StringBuilder JavaDoc str = new StringBuilder JavaDoc();
302         
303         str.append("[Type2:0x");
304         str.append(Integer.toHexString(getFlags()));
305         str.append(",Target:");
306         str.append(getTarget());
307         str.append(",Ch:");
308         str.append(HexDump.hexString(getChallenge()));
309         
310         if ( hasTargetInformation())
311         {
312             List JavaDoc<TargetInfo> targets = getTargetInformation();
313             
314             str.append(",TargInf:");
315             for ( TargetInfo target : targets)
316             {
317                 str.append(target);
318             }
319         }
320         str.append("]");
321         
322         return str.toString();
323     }
324 }
325
Popular Tags