KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > ntlmssp > NtlmMessage


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 com.knowgate.jcifs.ntlmssp;
21
22 import com.knowgate.jcifs.Config;
23
24 /**
25  * Abstract superclass for all NTLMSSP messages.
26  */

27 public abstract class NtlmMessage implements NtlmFlags {
28
29     /**
30      * The NTLMSSP "preamble".
31      */

32     protected static final byte[] NTLMSSP_SIGNATURE = new byte[] {
33         (byte) 'N', (byte) 'T', (byte) 'L', (byte) 'M',
34         (byte) 'S', (byte) 'S', (byte) 'P', (byte) 0
35     };
36
37     private static final String JavaDoc OEM_ENCODING =
38                 Config.getProperty("jcifs.encoding",
39                         System.getProperty("file.encoding"));
40
41     private int flags;
42
43     /**
44      * Returns the flags currently in use for this message.
45      *
46      * @return An <code>int</code> containing the flags in use for this
47      * message.
48      */

49     public int getFlags() {
50         return flags;
51     }
52
53     /**
54      * Sets the flags for this message.
55      *
56      * @param flags The flags for this message.
57      */

58     public void setFlags(int flags) {
59         this.flags = flags;
60     }
61
62     /**
63      * Returns the status of the specified flag.
64      *
65      * @param flag The flag to test (i.e., <code>NTLMSSP_NEGOTIATE_OEM</code>).
66      * @return A <code>boolean</code> indicating whether the flag is set.
67      */

68     public boolean getFlag(int flag) {
69         return (getFlags() & flag) != 0;
70     }
71
72     /**
73      * Sets or clears the specified flag.
74      *
75      * @param flag The flag to set/clear (i.e.,
76      * <code>NTLMSSP_NEGOTIATE_OEM</code>).
77      * @param value Indicates whether to set (<code>true</code>) or
78      * clear (<code>false</code>) the specified flag.
79      */

80     public void setFlag(int flag, boolean value) {
81         setFlags(value ? (getFlags() | flag) :
82                 (getFlags() & (0xffffffff ^ flag)));
83     }
84
85     static int readULong(byte[] src, int index) {
86         return (src[index] & 0xff) |
87                 ((src[index + 1] & 0xff) << 8) |
88                 ((src[index + 2] & 0xff) << 16) |
89                 ((src[index + 3] & 0xff) << 24);
90     }
91
92     static int readUShort(byte[] src, int index) {
93         return (src[index] & 0xff) | ((src[index + 1] & 0xff) << 8);
94     }
95
96     static byte[] readSecurityBuffer(byte[] src, int index) {
97         int length = readUShort(src, index);
98         int offset = readULong(src, index + 4);
99         byte[] buffer = new byte[length];
100         System.arraycopy(src, offset, buffer, 0, length);
101         return buffer;
102     }
103
104     static void writeULong(byte[] dest, int offset, int ulong) {
105         dest[offset] = (byte) (ulong & 0xff);
106         dest[offset + 1] = (byte) (ulong >> 8 & 0xff);
107         dest[offset + 2] = (byte) (ulong >> 16 & 0xff);
108         dest[offset + 3] = (byte) (ulong >> 24 & 0xff);
109     }
110
111     static void writeUShort(byte[] dest, int offset, int ushort) {
112         dest[offset] = (byte) (ushort & 0xff);
113         dest[offset + 1] = (byte) (ushort >> 8 & 0xff);
114     }
115
116     static void writeSecurityBuffer(byte[] dest, int offset, int bodyOffset,
117             byte[] src) {
118         int length = (src != null) ? src.length : 0;
119         if (length == 0) return;
120         writeUShort(dest, offset, length);
121         writeUShort(dest, offset + 2, length);
122         writeULong(dest, offset + 4, bodyOffset);
123         System.arraycopy(src, 0, dest, bodyOffset, length);
124     }
125
126     static String JavaDoc getOEMEncoding() {
127         return OEM_ENCODING;
128     }
129
130     /**
131      * Returns the raw byte representation of this message.
132      *
133      * @return A <code>byte[]</code> containing the raw message material.
134      */

135     public abstract byte[] toByteArray();
136
137 }
138
Popular Tags