1 19 20 package jcifs.ntlmssp; 21 22 import java.io.IOException ; 23 24 import java.net.UnknownHostException ; 25 26 import jcifs.netbios.NbtAddress; 27 28 import jcifs.Config; 29 30 33 public class Type1Message extends NtlmMessage { 34 35 private static final int DEFAULT_FLAGS; 36 37 private static final String DEFAULT_DOMAIN; 38 39 private static final String DEFAULT_WORKSTATION; 40 41 private String suppliedDomain; 42 43 private String 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 defaultWorkstation = null; 51 try { 52 defaultWorkstation = NbtAddress.getLocalHost().getHostName(); 53 } catch (UnknownHostException ex) { } 54 DEFAULT_WORKSTATION = defaultWorkstation; 55 } 56 57 61 public Type1Message() { 62 this(getDefaultFlags(), getDefaultDomain(), getDefaultWorkstation()); 63 } 64 65 72 public Type1Message(int flags, String suppliedDomain, 73 String suppliedWorkstation) { 74 setFlags(flags); 75 setSuppliedDomain(suppliedDomain); 76 setSuppliedWorkstation(suppliedWorkstation); 77 } 78 79 85 public Type1Message(byte[] material) throws IOException { 86 parse(material); 87 } 88 89 94 public String getSuppliedDomain() { 95 return suppliedDomain; 96 } 97 98 103 public void setSuppliedDomain(String suppliedDomain) { 104 this.suppliedDomain = suppliedDomain; 105 } 106 107 112 public String getSuppliedWorkstation() { 113 return suppliedWorkstation; 114 } 115 116 121 public void setSuppliedWorkstation(String suppliedWorkstation) { 122 this.suppliedWorkstation = suppliedWorkstation; 123 } 124 125 public byte[] toByteArray() { 126 try { 127 String suppliedDomain = getSuppliedDomain(); 128 String 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 ex) { 163 throw new IllegalStateException (ex.getMessage()); 164 } 165 } 166 167 public String toString() { 168 String suppliedDomain = getSuppliedDomain(); 169 String suppliedWorkstation = getSuppliedWorkstation(); 170 int flags = getFlags(); 171 StringBuffer buffer = new StringBuffer (); 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 201 public static int getDefaultFlags() { 202 return DEFAULT_FLAGS; 203 } 204 205 210 public static String getDefaultDomain() { 211 return DEFAULT_DOMAIN; 212 } 213 214 219 public static String getDefaultWorkstation() { 220 return DEFAULT_WORKSTATION; 221 } 222 223 private void parse(byte[] material) throws IOException { 224 for (int i = 0; i < 8; i++) { 225 if (material[i] != NTLMSSP_SIGNATURE[i]) { 226 throw new IOException ("Not an NTLMSSP message."); 227 } 228 } 229 if (readULong(material, 8) != 1) { 230 throw new IOException ("Not a Type 1 message."); 231 } 232 int flags = readULong(material, 12); 233 String suppliedDomain = null; 234 if ((flags & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED) != 0) { 235 byte[] domain = readSecurityBuffer(material, 16); 236 suppliedDomain = new String (domain, getOEMEncoding()); 237 } 238 String suppliedWorkstation = null; 239 if ((flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED) != 0) { 240 byte[] workstation = readSecurityBuffer(material, 24); 241 suppliedWorkstation = new String (workstation, getOEMEncoding()); 242 } 243 setFlags(flags); 244 setSuppliedDomain(suppliedDomain); 245 setSuppliedWorkstation(suppliedWorkstation); 246 } 247 248 } 249 | Popular Tags |