aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO/SharpCifs/Smb/SmbComNTCreateAndX.cs
blob: 26b5ba63aff4367a6471a1b94109ab03773ee076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// This code is derived from jcifs smb client library <jcifs at samba dot org>
// Ported by J. Arturo <webmaster at komodosoft dot net>
//  
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
using SharpCifs.Util;

namespace SharpCifs.Smb
{
	internal class SmbComNtCreateAndX : AndXServerMessageBlock
	{
		internal const int FileSupersede = unchecked(0x0);

		internal const int FileOpen = unchecked(0x1);

		internal const int FileCreate = unchecked(0x2);

		internal const int FileOpenIf = unchecked(0x3);

		internal const int FileOverwrite = unchecked(0x4);

		internal const int FileOverwriteIf = unchecked(0x5);

		internal const int FileWriteThrough = unchecked(0x00000002);

		internal const int FileSequentialOnly = unchecked(0x00000004);

		internal const int FileSynchronousIoAlert = unchecked(0x00000010);

		internal const int FileSynchronousIoNonalert = unchecked(0x00000020);

		internal const int SecurityContextTracking = unchecked(0x01);

		internal const int SecurityEffectiveOnly = unchecked(0x02);

		private int _rootDirectoryFid;

		private int _extFileAttributes;

		private int _shareAccess;

		private int _createDisposition;

		private int _createOptions;

		private int _impersonationLevel;

		private long _allocationSize;

		private byte _securityFlags;

		private int _namelenIndex;

		internal int Flags0;

		internal int DesiredAccess;

		internal SmbComNtCreateAndX(string name, int flags, int access, int shareAccess, 
			int extFileAttributes, int createOptions, ServerMessageBlock andx) : base(andx)
		{
			// share access specified in SmbFile
			// create disposition
			// create options
			// security flags
			Path = name;
			Command = SmbComNtCreateAndx;
			DesiredAccess = access;
            DesiredAccess |= SmbConstants.FileReadData | SmbConstants.FileReadEa | SmbConstants.FileReadAttributes;
			// extFileAttributes
			this._extFileAttributes = extFileAttributes;
			// shareAccess
			this._shareAccess = shareAccess;
			// createDisposition
			if ((flags & SmbFile.OTrunc) == SmbFile.OTrunc)
			{
				// truncate the file
				if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
				{
					// create it if necessary
					_createDisposition = FileOverwriteIf;
				}
				else
				{
					_createDisposition = FileOverwrite;
				}
			}
			else
			{
				// don't truncate the file
				if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
				{
					// create it if necessary
					if ((flags & SmbFile.OExcl) == SmbFile.OExcl)
					{
						// fail if already exists
						_createDisposition = FileCreate;
					}
					else
					{
						_createDisposition = FileOpenIf;
					}
				}
				else
				{
					_createDisposition = FileOpen;
				}
			}
			if ((createOptions & unchecked(0x0001)) == 0)
			{
				this._createOptions = createOptions | unchecked(0x0040);
			}
			else
			{
				this._createOptions = createOptions;
			}
			_impersonationLevel = unchecked(0x02);
			// As seen on NT :~)
			_securityFlags = unchecked(unchecked(0x03));
		}

		// SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY
		internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
		{
			int start = dstIndex;
			dst[dstIndex++] = unchecked(unchecked(0x00));
			// name length without counting null termination
			_namelenIndex = dstIndex;
			dstIndex += 2;
			WriteInt4(Flags0, dst, dstIndex);
			dstIndex += 4;
			WriteInt4(_rootDirectoryFid, dst, dstIndex);
			dstIndex += 4;
			WriteInt4(DesiredAccess, dst, dstIndex);
			dstIndex += 4;
			WriteInt8(_allocationSize, dst, dstIndex);
			dstIndex += 8;
			WriteInt4(_extFileAttributes, dst, dstIndex);
			dstIndex += 4;
			WriteInt4(_shareAccess, dst, dstIndex);
			dstIndex += 4;
			WriteInt4(_createDisposition, dst, dstIndex);
			dstIndex += 4;
			WriteInt4(_createOptions, dst, dstIndex);
			dstIndex += 4;
			WriteInt4(_impersonationLevel, dst, dstIndex);
			dstIndex += 4;
			dst[dstIndex++] = _securityFlags;
			return dstIndex - start;
		}

		internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
		{
			int n;
			n = WriteString(Path, dst, dstIndex);
			WriteInt2((UseUnicode ? Path.Length * 2 : n), dst, _namelenIndex);
			return n;
		}

		internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex
			)
		{
			return 0;
		}

		internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
		{
			return 0;
		}

		public override string ToString()
		{
			return "SmbComNTCreateAndX[" + base.ToString() + ",flags=0x" + Hexdump
				.ToHexString(Flags0, 2) + ",rootDirectoryFid=" + _rootDirectoryFid + ",desiredAccess=0x"
				 + Hexdump.ToHexString(DesiredAccess, 4) + ",allocationSize=" + _allocationSize +
				 ",extFileAttributes=0x" + Hexdump.ToHexString(_extFileAttributes, 4) + ",shareAccess=0x"
				 + Hexdump.ToHexString(_shareAccess, 4) + ",createDisposition=0x" + Hexdump.ToHexString
				(_createDisposition, 4) + ",createOptions=0x" + Hexdump.ToHexString(_createOptions
				, 8) + ",impersonationLevel=0x" + Hexdump.ToHexString(_impersonationLevel, 4) + ",securityFlags=0x"
				 + Hexdump.ToHexString(_securityFlags, 2) + ",name=" + Path + "]";
		}
	}
}