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 com.knowgate.jcifs.smb;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import com.knowgate.misc.Gadgets;
025:
026: class SmbComNTCreateAndX extends AndXServerMessageBlock {
027:
028: // access mask encoding
029: static final int FILE_READ_DATA = 0x00000001; // 1
030: static final int FILE_WRITE_DATA = 0x00000002; // 2
031: static final int FILE_APPEND_DATA = 0x00000004; // 3
032: static final int FILE_READ_EA = 0x00000008; // 4
033: static final int FILE_WRITE_EA = 0x00000010; // 5
034: static final int FILE_EXECUTE = 0x00000020; // 6
035: static final int FILE_DELETE = 0x00000040; // 7
036: static final int FILE_READ_ATTRIBUTES = 0x00000080; // 8
037: static final int FILE_WRITE_ATTRIBUTES = 0x00000100; // 9
038: static final int DELETE = 0x00010000; // 16
039: static final int READ_CONTROL = 0x00020000; // 17
040: static final int WRITE_DAC = 0x00040000; // 18
041: static final int WRITE_OWNER = 0x00080000; // 19
042: static final int SYNCHRONIZE = 0x00100000; // 20
043: static final int GENERIC_ALL = 0x10000000; // 28
044: static final int GENERIC_EXECUTE = 0x20000000; // 29
045: static final int GENERIC_WRITE = 0x40000000; // 30
046: static final int GENERIC_READ = 0x80000000; // 31
047:
048: // share access specified in SmbFile
049:
050: // create disposition
051:
052: /* Creates a new file or supersedes the existing one
053: */
054:
055: static final int FILE_SUPERSEDE = 0x0;
056:
057: /* Open the file or fail if it does not exist
058: * aka OPEN_EXISTING
059: */
060:
061: static final int FILE_OPEN = 0x1;
062:
063: /* Create the file or fail if it does not exist
064: * aka CREATE_NEW
065: */
066:
067: static final int FILE_CREATE = 0x2;
068:
069: /* Open the file or create it if it does not exist
070: * aka OPEN_ALWAYS
071: */
072:
073: static final int FILE_OPEN_IF = 0x3;
074:
075: /* Open the file and overwrite it's contents or fail if it does not exist
076: * aka TRUNCATE_EXISTING
077: */
078:
079: static final int FILE_OVERWRITE = 0x4;
080:
081: /* Open the file and overwrite it's contents or create it if it does not exist
082: * aka CREATE_ALWAYS (according to the wire when calling CreateFile)
083: */
084:
085: static final int FILE_OVERWRITE_IF = 0x5;
086:
087: // create options
088: static final int FILE_WRITE_THROUGH = 0x00000002;
089: static final int FILE_SEQUENTIAL_ONLY = 0x00000004;
090: static final int FILE_SYNCHRONOUS_IO_ALERT = 0x00000010;
091: static final int FILE_SYNCHRONOUS_IO_NONALERT = 0x00000020;
092:
093: // security flags
094: static final int SECURITY_CONTEXT_TRACKING = 0x01;
095: static final int SECURITY_EFFECTIVE_ONLY = 0x02;
096:
097: private int flags, rootDirectoryFid, desiredAccess,
098: extFileAttributes, shareAccess, createDisposition,
099: createOptions, impersonationLevel;
100: private long allocationSize;
101: private byte securityFlags;
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: writeInt2((useUnicode ? path.length() * 2 : path.length()),
159: dst, dstIndex);
160: dstIndex += 2;
161: writeInt4(flags, dst, dstIndex);
162: dstIndex += 4;
163: writeInt4(rootDirectoryFid, dst, dstIndex);
164: dstIndex += 4;
165: writeInt4(desiredAccess, dst, dstIndex);
166: dstIndex += 4;
167: writeInt8(allocationSize, dst, dstIndex);
168: dstIndex += 8;
169: writeInt4(extFileAttributes, dst, dstIndex);
170: dstIndex += 4;
171: writeInt4(shareAccess, dst, dstIndex);
172: dstIndex += 4;
173: writeInt4(createDisposition, dst, dstIndex);
174: dstIndex += 4;
175: writeInt4(createOptions, dst, dstIndex);
176: dstIndex += 4;
177: writeInt4(impersonationLevel, dst, dstIndex);
178: dstIndex += 4;
179: dst[dstIndex++] = securityFlags;
180:
181: return dstIndex - start;
182: }
183:
184: int writeBytesWireFormat(byte[] dst, int dstIndex) {
185: return writeString(path, dst, dstIndex);
186: }
187:
188: int readParameterWordsWireFormat(byte[] buffer, int bufferIndex) {
189: return 0;
190: }
191:
192: int readBytesWireFormat(byte[] buffer, int bufferIndex) {
193: return 0;
194: }
195:
196: int readBytesDirectWireFormat(InputStream in, int byteCount,
197: byte[] buffer, int bufferIndex) throws IOException {
198: return 0;
199: }
200:
201: public String toString() {
202: return new String("SmbComNTCreateAndX[" + super .toString()
203: + ",flags=0x" + Gadgets.toHexString(flags, 2)
204: + ",rootDirectoryFid=" + rootDirectoryFid
205: + ",desiredAccess=0x"
206: + Gadgets.toHexString(desiredAccess, 4)
207: + ",allocationSize=" + allocationSize
208: + ",extFileAttributes=0x"
209: + Gadgets.toHexString(extFileAttributes, 4)
210: + ",shareAccess=0x"
211: + Gadgets.toHexString(shareAccess, 4)
212: + ",createDisposition=0x"
213: + Gadgets.toHexString(createDisposition, 4)
214: + ",createOptions=0x"
215: + Gadgets.toHexString(createOptions, 8)
216: + ",impersonationLevel=0x"
217: + Gadgets.toHexString(impersonationLevel, 4)
218: + ",securityFlags=0x"
219: + Gadgets.toHexString(securityFlags, 2) + ",name="
220: + path + "]");
221: }
222: }
|