001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package jcifs.smb;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import jcifs.util.Hexdump;
024:
025: class SmbComNTCreateAndX extends AndXServerMessageBlock {
026:
027: // access mask encoding
028: static final int FILE_READ_DATA = 0x00000001; // 1
029: static final int FILE_WRITE_DATA = 0x00000002; // 2
030: static final int FILE_APPEND_DATA = 0x00000004; // 3
031: static final int FILE_READ_EA = 0x00000008; // 4
032: static final int FILE_WRITE_EA = 0x00000010; // 5
033: static final int FILE_EXECUTE = 0x00000020; // 6
034: static final int FILE_DELETE = 0x00000040; // 7
035: static final int FILE_READ_ATTRIBUTES = 0x00000080; // 8
036: static final int FILE_WRITE_ATTRIBUTES = 0x00000100; // 9
037: static final int DELETE = 0x00010000; // 16
038: static final int READ_CONTROL = 0x00020000; // 17
039: static final int WRITE_DAC = 0x00040000; // 18
040: static final int WRITE_OWNER = 0x00080000; // 19
041: static final int SYNCHRONIZE = 0x00100000; // 20
042: static final int GENERIC_ALL = 0x10000000; // 28
043: static final int GENERIC_EXECUTE = 0x20000000; // 29
044: static final int GENERIC_WRITE = 0x40000000; // 30
045: static final int GENERIC_READ = 0x80000000; // 31
046:
047: // share access specified in SmbFile
048:
049: // create disposition
050:
051: /* Creates a new file or supersedes the existing one
052: */
053:
054: static final int FILE_SUPERSEDE = 0x0;
055:
056: /* Open the file or fail if it does not exist
057: * aka OPEN_EXISTING
058: */
059:
060: static final int FILE_OPEN = 0x1;
061:
062: /* Create the file or fail if it does not exist
063: * aka CREATE_NEW
064: */
065:
066: static final int FILE_CREATE = 0x2;
067:
068: /* Open the file or create it if it does not exist
069: * aka OPEN_ALWAYS
070: */
071:
072: static final int FILE_OPEN_IF = 0x3;
073:
074: /* Open the file and overwrite it's contents or fail if it does not exist
075: * aka TRUNCATE_EXISTING
076: */
077:
078: static final int FILE_OVERWRITE = 0x4;
079:
080: /* Open the file and overwrite it's contents or create it if it does not exist
081: * aka CREATE_ALWAYS (according to the wire when calling CreateFile)
082: */
083:
084: static final int FILE_OVERWRITE_IF = 0x5;
085:
086: // create options
087: static final int FILE_WRITE_THROUGH = 0x00000002;
088: static final int FILE_SEQUENTIAL_ONLY = 0x00000004;
089: static final int FILE_SYNCHRONOUS_IO_ALERT = 0x00000010;
090: static final int FILE_SYNCHRONOUS_IO_NONALERT = 0x00000020;
091:
092: // security flags
093: static final int SECURITY_CONTEXT_TRACKING = 0x01;
094: static final int SECURITY_EFFECTIVE_ONLY = 0x02;
095:
096: private int flags, rootDirectoryFid, desiredAccess,
097: extFileAttributes, shareAccess, createDisposition,
098: createOptions, impersonationLevel;
099: private long allocationSize;
100: private byte securityFlags;
101: private int namelen_index;
102:
103: SmbComNTCreateAndX(String name, int flags, int shareAccess,
104: int extFileAttributes, int createOptions,
105: ServerMessageBlock andx) {
106: super (andx);
107: this .path = name;
108: command = SMB_COM_NT_CREATE_ANDX;
109:
110: // desiredAccess
111: desiredAccess = (flags >>> 16) & 0xFFFF;
112: desiredAccess |= FILE_READ_EA | FILE_READ_ATTRIBUTES;
113:
114: // extFileAttributes
115: this .extFileAttributes = extFileAttributes;
116:
117: // shareAccess
118: this .shareAccess = shareAccess;
119:
120: // createDisposition
121: if ((flags & SmbFile.O_TRUNC) == SmbFile.O_TRUNC) {
122: // truncate the file
123: if ((flags & SmbFile.O_CREAT) == SmbFile.O_CREAT) {
124: // create it if necessary
125: createDisposition = FILE_OVERWRITE_IF;
126: } else {
127: createDisposition = FILE_OVERWRITE;
128: }
129: } else {
130: // don't truncate the file
131: if ((flags & SmbFile.O_CREAT) == SmbFile.O_CREAT) {
132: // create it if necessary
133: if ((flags & SmbFile.O_EXCL) == SmbFile.O_EXCL) {
134: // fail if already exists
135: createDisposition = FILE_CREATE;
136: } else {
137: createDisposition = FILE_OPEN_IF;
138: }
139: } else {
140: createDisposition = FILE_OPEN;
141: }
142: }
143:
144: if ((createOptions & 0x01) == 0) {
145: this .createOptions = createOptions | 0x0040;
146: } else {
147: this .createOptions = createOptions;
148: }
149: impersonationLevel = 0x02; // As seen on NT :~)
150: securityFlags = (byte) 0x03; // SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY
151: }
152:
153: int writeParameterWordsWireFormat(byte[] dst, int dstIndex) {
154: int start = dstIndex;
155:
156: dst[dstIndex++] = (byte) 0x00;
157: // name length without counting null termination
158: namelen_index = dstIndex;
159: dstIndex += 2;
160: writeInt4(flags, dst, dstIndex);
161: dstIndex += 4;
162: writeInt4(rootDirectoryFid, dst, dstIndex);
163: dstIndex += 4;
164: writeInt4(desiredAccess, dst, dstIndex);
165: dstIndex += 4;
166: writeInt8(allocationSize, dst, dstIndex);
167: dstIndex += 8;
168: writeInt4(extFileAttributes, dst, dstIndex);
169: dstIndex += 4;
170: writeInt4(shareAccess, dst, dstIndex);
171: dstIndex += 4;
172: writeInt4(createDisposition, dst, dstIndex);
173: dstIndex += 4;
174: writeInt4(createOptions, dst, dstIndex);
175: dstIndex += 4;
176: writeInt4(impersonationLevel, dst, dstIndex);
177: dstIndex += 4;
178: dst[dstIndex++] = securityFlags;
179:
180: return dstIndex - start;
181: }
182:
183: int writeBytesWireFormat(byte[] dst, int dstIndex) {
184: int n;
185: n = writeString(path, dst, dstIndex);
186: writeInt2((useUnicode ? path.length() * 2 : n), dst,
187: namelen_index);
188: return n;
189: }
190:
191: int readParameterWordsWireFormat(byte[] buffer, int bufferIndex) {
192: return 0;
193: }
194:
195: int readBytesWireFormat(byte[] buffer, int bufferIndex) {
196: return 0;
197: }
198:
199: int readBytesDirectWireFormat(InputStream in, int byteCount,
200: byte[] buffer, int bufferIndex) throws IOException {
201: return 0;
202: }
203:
204: public String toString() {
205: return new String("SmbComNTCreateAndX[" + super .toString()
206: + ",flags=0x" + Hexdump.toHexString(flags, 2)
207: + ",rootDirectoryFid=" + rootDirectoryFid
208: + ",desiredAccess=0x"
209: + Hexdump.toHexString(desiredAccess, 4)
210: + ",allocationSize=" + allocationSize
211: + ",extFileAttributes=0x"
212: + Hexdump.toHexString(extFileAttributes, 4)
213: + ",shareAccess=0x"
214: + Hexdump.toHexString(shareAccess, 4)
215: + ",createDisposition=0x"
216: + Hexdump.toHexString(createDisposition, 4)
217: + ",createOptions=0x"
218: + Hexdump.toHexString(createOptions, 8)
219: + ",impersonationLevel=0x"
220: + Hexdump.toHexString(impersonationLevel, 4)
221: + ",securityFlags=0x"
222: + Hexdump.toHexString(securityFlags, 2) + ",name="
223: + path + "]");
224: }
225: }
|