KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > ntlmssp > Type1Message


1 /* jcifs smb client library in Java
2  * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
3  * "Eric Glass" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.ntlmssp;
21
22 import java.io.IOException JavaDoc;
23
24 import java.net.UnknownHostException JavaDoc;
25
26 import jcifs.netbios.NbtAddress;
27
28 import jcifs.Config;
29
30 /**
31  * Represents an NTLMSSP Type-1 message.
32  */

33 public class Type1Message extends NtlmMessage {
34
35     private static final int DEFAULT_FLAGS;
36
37     private static final String JavaDoc DEFAULT_DOMAIN;
38
39     private static final String JavaDoc DEFAULT_WORKSTATION;
40
41     private String JavaDoc suppliedDomain;
42
43     private String JavaDoc suppliedWorkstation;
44
45     static {
46         DEFAULT_FLAGS = NTLMSSP_NEGOTIATE_NTLM |
47                 (Config.getBoolean("jcifs.smb.client.useUnicode", true) ?
48                         NTLMSSP_NEGOTIATE_UNICODE : NTLMSSP_NEGOTIATE_OEM);
49         DEFAULT_DOMAIN = Config.getProperty("jcifs.smb.client.domain", null);
50         String JavaDoc defaultWorkstation = null;
51         try {
52             defaultWorkstation = NbtAddress.getLocalHost().getHostName();
53         } catch (UnknownHostException JavaDoc ex) { }
54         DEFAULT_WORKSTATION = defaultWorkstation;
55     }
56
57     /**
58      * Creates a Type-1 message using default values from the current
59      * environment.
60      */

61     public Type1Message() {
62         this(getDefaultFlags(), getDefaultDomain(), getDefaultWorkstation());
63     }
64
65     /**
66      * Creates a Type-1 message with the specified parameters.
67      *
68      * @param flags The flags to apply to this message.
69      * @param suppliedDomain The supplied authentication domain.
70      * @param suppliedWorkstation The supplied workstation name.
71      */

72     public Type1Message(int flags, String JavaDoc suppliedDomain,
73             String JavaDoc suppliedWorkstation) {
74         setFlags(flags);
75         setSuppliedDomain(suppliedDomain);
76         setSuppliedWorkstation(suppliedWorkstation);
77     }
78
79     /**
80      * Creates a Type-1 message using the given raw Type-1 material.
81      *
82      * @param material The raw Type-1 material used to construct this message.
83      * @throws IOException If an error occurs while parsing the material.
84      */

85     public Type1Message(byte[] material) throws IOException JavaDoc {
86         parse(material);
87     }
88
89     /**
90      * Returns the supplied authentication domain.
91      *
92      * @return A <code>String</code> containing the supplied domain.
93      */

94     public String JavaDoc getSuppliedDomain() {
95         return suppliedDomain;
96     }
97
98     /**
99      * Sets the supplied authentication domain for this message.
100      *
101      * @param suppliedDomain The supplied domain for this message.
102      */

103     public void setSuppliedDomain(String JavaDoc suppliedDomain) {
104         this.suppliedDomain = suppliedDomain;
105     }
106
107     /**
108      * Returns the supplied workstation name.
109      *
110      * @return A <code>String</code> containing the supplied workstation name.
111      */

112     public String JavaDoc getSuppliedWorkstation() {
113         return suppliedWorkstation;
114     }
115
116     /**
117      * Sets the supplied workstation name for this message.
118      *
119      * @param suppliedWorkstation The supplied workstation for this message.
120      */

121     public void setSuppliedWorkstation(String JavaDoc suppliedWorkstation) {
122         this.suppliedWorkstation = suppliedWorkstation;
123     }
124
125     public byte[] toByteArray() {
126         try {
127             String JavaDoc suppliedDomain = getSuppliedDomain();
128             String JavaDoc suppliedWorkstation = getSuppliedWorkstation();
129             int flags = getFlags();
130             boolean hostInfo = false;
131             byte[] domain = new byte[0];
132             if (suppliedDomain != null && suppliedDomain.length() != 0) {
133                 hostInfo = true;
134                 flags |= NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED;
135                 domain = suppliedDomain.toUpperCase().getBytes(
136                         getOEMEncoding());
137             } else {
138                 flags &= (NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED ^ 0xffffffff);
139             }
140             byte[] workstation = new byte[0];
141             if (suppliedWorkstation != null &&
142                     suppliedWorkstation.length() != 0) {
143                 hostInfo = true;
144                 flags |= NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED;
145                 workstation =
146                         suppliedWorkstation.toUpperCase().getBytes(
147                                 getOEMEncoding());
148             } else {
149                 flags &= (NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED ^
150                         0xffffffff);
151             }
152             byte[] type1 = new byte[hostInfo ?
153                     (32 + domain.length + workstation.length) : 16];
154             System.arraycopy(NTLMSSP_SIGNATURE, 0, type1, 0, 8);
155             writeULong(type1, 8, 1);
156             writeULong(type1, 12, flags);
157             if (hostInfo) {
158                 writeSecurityBuffer(type1, 16, 32, domain);
159                 writeSecurityBuffer(type1, 24, 32 + domain.length, workstation);
160             }
161             return type1;
162         } catch (IOException JavaDoc ex) {
163             throw new IllegalStateException JavaDoc(ex.getMessage());
164         }
165     }
166
167     public String JavaDoc toString() {
168         String JavaDoc suppliedDomain = getSuppliedDomain();
169         String JavaDoc suppliedWorkstation = getSuppliedWorkstation();
170         int flags = getFlags();
171         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
172         if (suppliedDomain != null) {
173             buffer.append("suppliedDomain: ").append(suppliedDomain);
174         }
175         if (suppliedWorkstation != null) {
176             if (buffer.length() > 0) buffer.append("; ");
177             buffer.append("suppliedWorkstation: ").append(suppliedWorkstation);
178         }
179         if (flags != 0) {
180             if (buffer.length() > 0) buffer.append("; ");
181             buffer.append("flags: ");
182             buffer.append("0x");
183             buffer.append(Integer.toHexString((flags >> 28) & 0x0f));
184             buffer.append(Integer.toHexString((flags >> 24) & 0x0f));
185             buffer.append(Integer.toHexString((flags >> 20) & 0x0f));
186             buffer.append(Integer.toHexString((flags >> 16) & 0x0f));
187             buffer.append(Integer.toHexString((flags >> 12) & 0x0f));
188             buffer.append(Integer.toHexString((flags >> 8) & 0x0f));
189             buffer.append(Integer.toHexString((flags >> 4) & 0x0f));
190             buffer.append(Integer.toHexString(flags & 0x0f));
191         }
192         return buffer.toString();
193     }
194
195     /**
196      * Returns the default flags for a generic Type-1 message in the
197      * current environment.
198      *
199      * @return An <code>int</code> containing the default flags.
200      */

201     public static int getDefaultFlags() {
202         return DEFAULT_FLAGS;
203     }
204
205     /**
206      * Returns the default domain from the current environment.
207      *
208      * @return A <code>String</code> containing the default domain.
209      */

210     public static String JavaDoc getDefaultDomain() {
211         return DEFAULT_DOMAIN;
212     }
213
214     /**
215      * Returns the default workstation from the current environment.
216      *
217      * @return A <code>String</code> containing the default workstation.
218      */

219     public static String JavaDoc getDefaultWorkstation() {
220         return DEFAULT_WORKSTATION;
221     }
222
223     private void parse(byte[] material) throws IOException JavaDoc {
224         for (int i = 0; i < 8; i++) {
225             if (material[i] != NTLMSSP_SIGNATURE[i]) {
226                 throw new IOException JavaDoc("Not an NTLMSSP message.");
227             }
228         }
229         if (readULong(material, 8) != 1) {
230             throw new IOException JavaDoc("Not a Type 1 message.");
231         }
232         int flags = readULong(material, 12);
233         String JavaDoc suppliedDomain = null;
234         if ((flags & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED) != 0) {
235             byte[] domain = readSecurityBuffer(material, 16);
236             suppliedDomain = new String JavaDoc(domain, getOEMEncoding());
237         }
238         String JavaDoc suppliedWorkstation = null;
239         if ((flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED) != 0) {
240             byte[] workstation = readSecurityBuffer(material, 24);
241             suppliedWorkstation = new String JavaDoc(workstation, getOEMEncoding());
242         }
243         setFlags(flags);
244         setSuppliedDomain(suppliedDomain);
245         setSuppliedWorkstation(suppliedWorkstation);
246     }
247
248 }
249
Popular Tags