aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO/SharpCifs/Smb/SmbComSessionSetupAndX.cs
blob: a1642391ca54f23bdd6d62bce9817e53094e9b11 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// This code is derived from jcifs smb client library <jcifs at samba dot org>
//  
// 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
// 
// Ported to C# by J. Arturo <webmaster at komodosoft.net>
using System;
using SharpCifs.Util.Sharpen;

namespace SharpCifs.Smb
{
	internal class SmbComSessionSetupAndX : AndXServerMessageBlock
	{
		private static readonly int BatchLimit = Config.GetInt("jcifs.smb.client.SessionSetupAndX.TreeConnectAndX"
			, 1);

		private static readonly bool DisablePlainTextPasswords = Config.GetBoolean("jcifs.smb.client.disablePlainTextPasswords"
			, true);

		private byte[] _lmHash;

		private byte[] _ntHash;

		private byte[] _blob;

		private int _sessionKey;

		private int _capabilities;

		private string _accountName;

		private string _primaryDomain;

		internal SmbSession Session;

		internal object Cred;

		/// <exception cref="SharpCifs.Smb.SmbException"></exception>
		internal SmbComSessionSetupAndX(SmbSession session, ServerMessageBlock andx, object
			 cred) : base(andx)
		{
			Command = SmbComSessionSetupAndx;
			this.Session = session;
			this.Cred = cred;
			_sessionKey = session.transport.SessionKey;
			_capabilities = session.transport.Capabilities;
            if (session.transport.Server.Security == SmbConstants.SecurityUser)
			{
				if (cred is NtlmPasswordAuthentication)
				{
					NtlmPasswordAuthentication auth = (NtlmPasswordAuthentication)cred;
					if (auth == NtlmPasswordAuthentication.Anonymous)
					{
						_lmHash = new byte[0];
						_ntHash = new byte[0];
						_capabilities &= ~SmbConstants.CapExtendedSecurity;
					}
					else
					{
						if (session.transport.Server.EncryptedPasswords)
						{
							_lmHash = auth.GetAnsiHash(session.transport.Server.EncryptionKey);
							_ntHash = auth.GetUnicodeHash(session.transport.Server.EncryptionKey);
							// prohibit HTTP auth attempts for the null session
							if (_lmHash.Length == 0 && _ntHash.Length == 0)
							{
								throw new RuntimeException("Null setup prohibited.");
							}
						}
						else
						{
						    if (DisablePlainTextPasswords)
							{
								throw new RuntimeException("Plain text passwords are disabled");
							}
						    if (UseUnicode)
						    {
						        // plain text
						        string password = auth.GetPassword();
						        _lmHash = new byte[0];
						        _ntHash = new byte[(password.Length + 1) * 2];
						        WriteString(password, _ntHash, 0);
						    }
						    else
						    {
						        // plain text
						        string password = auth.GetPassword();
						        _lmHash = new byte[(password.Length + 1) * 2];
						        _ntHash = new byte[0];
						        WriteString(password, _lmHash, 0);
						    }
						}
					}
					_accountName = auth.Username;
					if (UseUnicode)
					{
						_accountName = _accountName.ToUpper();
					}
					_primaryDomain = auth.Domain.ToUpper();
				}
				else
				{
					if (cred is byte[])
					{
						_blob = (byte[])cred;
					}
					else
					{
						throw new SmbException("Unsupported credential type");
					}
				}
			}
			else
			{
                if (session.transport.Server.Security == SmbConstants.SecurityShare)
				{
					if (cred is NtlmPasswordAuthentication)
					{
						NtlmPasswordAuthentication auth = (NtlmPasswordAuthentication)cred;
						_lmHash = new byte[0];
						_ntHash = new byte[0];
						_accountName = auth.Username;
						if (UseUnicode)
						{
							_accountName = _accountName.ToUpper();
						}
						_primaryDomain = auth.Domain.ToUpper();
					}
					else
					{
						throw new SmbException("Unsupported credential type");
					}
				}
				else
				{
					throw new SmbException("Unsupported");
				}
			}
		}

		internal override int GetBatchLimit(byte command)
		{
			return command == SmbComTreeConnectAndx ? BatchLimit : 0;
		}

		internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
		{
			int start = dstIndex;
			WriteInt2(Session.transport.SndBufSize, dst, dstIndex);
			dstIndex += 2;
			WriteInt2(Session.transport.MaxMpxCount, dst, dstIndex);
			dstIndex += 2;
            WriteInt2(SmbConstants.VcNumber, dst, dstIndex);
			dstIndex += 2;
			WriteInt4(_sessionKey, dst, dstIndex);
			dstIndex += 4;
			if (_blob != null)
			{
				WriteInt2(_blob.Length, dst, dstIndex);
				dstIndex += 2;
			}
			else
			{
				WriteInt2(_lmHash.Length, dst, dstIndex);
				dstIndex += 2;
				WriteInt2(_ntHash.Length, dst, dstIndex);
				dstIndex += 2;
			}
			dst[dstIndex++] = unchecked(unchecked(0x00));
			dst[dstIndex++] = unchecked(unchecked(0x00));
			dst[dstIndex++] = unchecked(unchecked(0x00));
			dst[dstIndex++] = unchecked(unchecked(0x00));
			WriteInt4(_capabilities, dst, dstIndex);
			dstIndex += 4;
			return dstIndex - start;
		}

		internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
		{
			int start = dstIndex;
			if (_blob != null)
			{
				Array.Copy(_blob, 0, dst, dstIndex, _blob.Length);
				dstIndex += _blob.Length;
			}
			else
			{
				Array.Copy(_lmHash, 0, dst, dstIndex, _lmHash.Length);
				dstIndex += _lmHash.Length;
				Array.Copy(_ntHash, 0, dst, dstIndex, _ntHash.Length);
				dstIndex += _ntHash.Length;
				dstIndex += WriteString(_accountName, dst, dstIndex);
				dstIndex += WriteString(_primaryDomain, dst, dstIndex);
			}
            dstIndex += WriteString(SmbConstants.NativeOs, dst, dstIndex);
            dstIndex += WriteString(SmbConstants.NativeLanman, dst, dstIndex);
			return dstIndex - start;
		}

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

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

		public override string ToString()
		{
			string result = "SmbComSessionSetupAndX[" + base.ToString() + ",snd_buf_size="
				 + Session.transport.SndBufSize + ",maxMpxCount=" + Session.transport.MaxMpxCount
                 + ",VC_NUMBER=" + SmbConstants.VcNumber + ",sessionKey=" + _sessionKey + ",lmHash.length="
				 + (_lmHash == null ? 0 : _lmHash.Length) + ",ntHash.length=" + (_ntHash == null ? 
				0 : _ntHash.Length) + ",capabilities=" + _capabilities + ",accountName=" + _accountName
                 + ",primaryDomain=" + _primaryDomain + ",NATIVE_OS=" + SmbConstants.NativeOs
                 + ",NATIVE_LANMAN=" + SmbConstants.NativeLanman + "]";
			return result;
		}
	}
}