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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
// 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 System;
using SharpCifs.Util;
using SharpCifs.Util.Sharpen;
using SharpCifs.Util.Transport;
namespace SharpCifs.Smb
{
public abstract class ServerMessageBlock: Response
{
internal static LogStream Log = LogStream.GetInstance();
internal static long Ticks1601 = new DateTime(1601, 1, 1).Ticks;
internal static readonly byte[] Header = { 0xFF, (byte)('S'), (byte)('M'),
(byte)('B'), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
internal static void WriteInt2(long val, byte[] dst, int dstIndex)
{
dst[dstIndex] = unchecked((byte)(val));
dst[++dstIndex] = unchecked((byte)(val >> 8));
}
internal static void WriteInt4(long val, byte[] dst, int dstIndex)
{
dst[dstIndex] = unchecked((byte)(val));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >> 8));
}
internal static int ReadInt2(byte[] src, int srcIndex)
{
return unchecked(src[srcIndex] & 0xFF) + ((src[srcIndex + 1] & 0xFF) << 8);
}
internal static int ReadInt4(byte[] src, int srcIndex)
{
return unchecked(src[srcIndex] & 0xFF) + ((src[srcIndex + 1] & 0xFF) << 8) + ((src[srcIndex + 2]
& 0xFF) << 16) + ((src[srcIndex + 3] & 0xFF) << 24);
}
internal static long ReadInt8(byte[] src, int srcIndex)
{
return unchecked(ReadInt4(src, srcIndex) & unchecked(0xFFFFFFFFL)) + unchecked((long)(ReadInt4
(src, srcIndex + 4)) << 32);
}
internal static void WriteInt8(long val, byte[] dst, int dstIndex)
{
dst[dstIndex] = unchecked((byte)(val));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >>= 8));
dst[++dstIndex] = unchecked((byte)(val >> 8));
}
internal static long ReadTime(byte[] src, int srcIndex)
{
int low = ReadInt4(src, srcIndex);
int hi = ReadInt4(src, srcIndex + 4);
long t = ((long)hi << (int)32L) | (low & unchecked((long)(0xFFFFFFFFL)));
t = (t / 10000L - SmbConstants.MillisecondsBetween1970And1601);
return t;
}
internal static void WriteTime(long t, byte[] dst, int dstIndex)
{
if (t != 0L)
{
t = (t + SmbConstants.MillisecondsBetween1970And1601) * 10000L;
}
WriteInt8(t, dst, dstIndex);
}
internal static long ReadUTime(byte[] buffer, int bufferIndex)
{
return ReadInt4(buffer, bufferIndex) * 1000L;
}
internal static void WriteUTime(long t, byte[] dst, int dstIndex)
{
if (t == 0L || t == unchecked((long)(0xFFFFFFFFFFFFFFFFL)))
{
WriteInt4(unchecked((int)(0xFFFFFFFF)), dst, dstIndex);
return;
}
// t isn't in DST either
WriteInt4((int)(t / 1000L), dst, dstIndex);
}
internal const byte SmbComCreateDirectory = 0x00;
internal const byte SmbComDeleteDirectory = 0x01;
internal const byte SmbComClose = 0x04;
internal const byte SmbComDelete = 0x06;
internal const byte SmbComRename = 0x07;
internal const byte SmbComQueryInformation = 0x08;
internal const byte SmbComWrite = 0x0B;
internal const byte SmbComCheckDirectory = 0x10;
internal const byte SmbComTransaction = 0x25;
internal const byte SmbComTransactionSecondary = 0x26;
internal const byte SmbComMove = 0x2A;
internal const byte SmbComEcho = 0x2B;
internal const byte SmbComOpenAndx = 0x2D;
internal const byte SmbComReadAndx = 0x2E;
internal const byte SmbComWriteAndx = 0x2F;
internal const byte SmbComTransaction2 = 0x32;
internal const byte SmbComFindClose2 = 0x34;
internal const byte SmbComTreeDisconnect = 0x71;
internal const byte SmbComNegotiate = 0x72;
internal const byte SmbComSessionSetupAndx = 0x73;
internal const byte SmbComLogoffAndx = 0x74;
internal const byte SmbComTreeConnectAndx = 0x75;
internal const byte SmbComNtTransact = 0xA0;
internal const byte SmbComNtTransactSecondary = 0xA1;
internal const byte SmbComNtCreateAndx = 0xA2;
internal byte Command;
internal byte Flags;
internal int HeaderStart;
internal int Length;
internal int BatchLevel;
internal int ErrorCode;
internal int Flags2;
internal int Tid;
internal int Pid;
internal int Uid;
internal int Mid;
internal int WordCount;
internal int ByteCount;
internal bool UseUnicode;
internal bool Received;
internal bool ExtendedSecurity;
internal long ResponseTimeout = 1;
internal int SignSeq;
internal bool VerifyFailed;
internal NtlmPasswordAuthentication Auth = null;
internal string Path;
internal SigningDigest Digest;
internal ServerMessageBlock Response;
public ServerMessageBlock()
{
Flags = unchecked((byte)(SmbConstants.FlagsPathNamesCaseless | SmbConstants.FlagsPathNamesCanonicalized
));
Pid = SmbConstants.Pid;
BatchLevel = 0;
}
internal virtual void Reset()
{
Flags = unchecked((byte)(SmbConstants.FlagsPathNamesCaseless | SmbConstants.FlagsPathNamesCanonicalized
));
Flags2 = 0;
ErrorCode = 0;
Received = false;
Digest = null;
}
internal virtual int WriteString(string str, byte[] dst, int dstIndex)
{
return WriteString(str, dst, dstIndex, UseUnicode);
}
internal virtual int WriteString(string str, byte[] dst, int dstIndex, bool useUnicode
)
{
int start = dstIndex;
try
{
if (useUnicode)
{
// Unicode requires word alignment
if (((dstIndex - HeaderStart) % 2) != 0)
{
dst[dstIndex++] = (byte)('\0');
}
Array.Copy(Runtime.GetBytesForString(str, SmbConstants.UniEncoding), 0, dst, dstIndex
, str.Length * 2);
dstIndex += str.Length * 2;
dst[dstIndex++] = (byte)('\0');
dst[dstIndex++] = (byte)('\0');
}
else
{
byte[] b = Runtime.GetBytesForString(str, SmbConstants.OemEncoding);
Array.Copy(b, 0, dst, dstIndex, b.Length);
dstIndex += b.Length;
dst[dstIndex++] = (byte)('\0');
}
}
catch (UnsupportedEncodingException uee)
{
if (Log.Level > 1)
{
Runtime.PrintStackTrace(uee, Log);
}
}
return dstIndex - start;
}
internal virtual string ReadString(byte[] src, int srcIndex)
{
return ReadString(src, srcIndex, 256, UseUnicode);
}
internal virtual string ReadString(byte[] src, int srcIndex, int maxLen, bool useUnicode
)
{
int len = 0;
string str = null;
try
{
if (useUnicode)
{
// Unicode requires word alignment
if (((srcIndex - HeaderStart) % 2) != 0)
{
srcIndex++;
}
while (src[srcIndex + len] != 0x00 || src[srcIndex
+ len + 1] != 0x00)
{
len += 2;
if (len > maxLen)
{
if (Log.Level > 0)
{
Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 :
128);
}
throw new RuntimeException("zero termination not found");
}
}
str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.UniEncoding);
}
else
{
while (src[srcIndex + len] != 0x00)
{
len++;
if (len > maxLen)
{
if (Log.Level > 0)
{
Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 :
128);
}
throw new RuntimeException("zero termination not found");
}
}
str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.OemEncoding);
}
}
catch (UnsupportedEncodingException uee)
{
if (Log.Level > 1)
{
Runtime.PrintStackTrace(uee, Log);
}
}
return str;
}
internal virtual string ReadString(byte[] src, int srcIndex, int srcEnd, int maxLen
, bool useUnicode)
{
int len = 0;
string str = null;
try
{
if (useUnicode)
{
// Unicode requires word alignment
if (((srcIndex - HeaderStart) % 2) != 0)
{
srcIndex++;
}
for (len = 0; (srcIndex + len + 1) < srcEnd; len += 2)
{
if (src[srcIndex + len] == 0x00 && src[srcIndex
+ len + 1] == 0x00)
{
break;
}
if (len > maxLen)
{
if (Log.Level > 0)
{
Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 :
128);
}
throw new RuntimeException("zero termination not found");
}
}
str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.UniEncoding);
}
else
{
for (len = 0; srcIndex < srcEnd; len++)
{
if (src[srcIndex + len] == 0x00)
{
break;
}
if (len > maxLen)
{
if (Log.Level > 0)
{
Hexdump.ToHexdump(Console.Error, src, srcIndex, maxLen < 128 ? maxLen + 8 :
128);
}
throw new RuntimeException("zero termination not found");
}
}
str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.OemEncoding);
}
}
catch (UnsupportedEncodingException uee)
{
if (Log.Level > 1)
{
Runtime.PrintStackTrace(uee, Log);
}
}
return str;
}
internal virtual int StringWireLength(string str, int offset)
{
int len = str.Length + 1;
if (UseUnicode)
{
len = str.Length * 2 + 2;
len = (offset % 2) != 0 ? len + 1 : len;
}
return len;
}
internal virtual int ReadStringLength(byte[] src, int srcIndex, int max)
{
int len = 0;
while (src[srcIndex + len] != 0x00)
{
if (len++ > max)
{
throw new RuntimeException("zero termination not found: " + this);
}
}
return len;
}
internal virtual int Encode(byte[] dst, int dstIndex)
{
int start = HeaderStart = dstIndex;
dstIndex += WriteHeaderWireFormat(dst, dstIndex);
WordCount = WriteParameterWordsWireFormat(dst, dstIndex + 1);
dst[dstIndex++] = unchecked((byte)((WordCount / 2) & 0xFF));
dstIndex += WordCount;
WordCount /= 2;
ByteCount = WriteBytesWireFormat(dst, dstIndex + 2);
dst[dstIndex++] = unchecked((byte)(ByteCount & 0xFF));
dst[dstIndex++] = unchecked((byte)((ByteCount >> 8) & 0xFF));
dstIndex += ByteCount;
Length = dstIndex - start;
if (Digest != null)
{
Digest.Sign(dst, HeaderStart, Length, this, Response);
}
return Length;
}
internal virtual int Decode(byte[] buffer, int bufferIndex)
{
int start = HeaderStart = bufferIndex;
bufferIndex += ReadHeaderWireFormat(buffer, bufferIndex);
WordCount = buffer[bufferIndex++];
if (WordCount != 0)
{
int n;
if ((n = ReadParameterWordsWireFormat(buffer, bufferIndex)) != WordCount * 2)
{
if (Log.Level >= 5)
{
Log.WriteLine("wordCount * 2=" + (WordCount * 2) + " but readParameterWordsWireFormat returned "
+ n);
}
}
bufferIndex += WordCount * 2;
}
ByteCount = ReadInt2(buffer, bufferIndex);
bufferIndex += 2;
if (ByteCount != 0)
{
int n;
if ((n = ReadBytesWireFormat(buffer, bufferIndex)) != ByteCount)
{
if (Log.Level >= 5)
{
Log.WriteLine("byteCount=" + ByteCount + " but readBytesWireFormat returned " + n
);
}
}
// Don't think we can rely on n being correct here. Must use byteCount.
// Last paragraph of section 3.13.3 eludes to this.
bufferIndex += ByteCount;
}
Length = bufferIndex - start;
return Length;
}
internal virtual int WriteHeaderWireFormat(byte[] dst, int dstIndex)
{
Array.Copy(Header, 0, dst, dstIndex, Header.Length);
dst[dstIndex + SmbConstants.CmdOffset] = Command;
dst[dstIndex + SmbConstants.FlagsOffset] = Flags;
WriteInt2(Flags2, dst, dstIndex + SmbConstants.FlagsOffset + 1);
dstIndex += SmbConstants.TidOffset;
WriteInt2(Tid, dst, dstIndex);
WriteInt2(Pid, dst, dstIndex + 2);
WriteInt2(Uid, dst, dstIndex + 4);
WriteInt2(Mid, dst, dstIndex + 6);
return SmbConstants.HeaderLength;
}
internal virtual int ReadHeaderWireFormat(byte[] buffer, int bufferIndex)
{
Command = buffer[bufferIndex + SmbConstants.CmdOffset];
ErrorCode = ReadInt4(buffer, bufferIndex + SmbConstants.ErrorCodeOffset);
Flags = buffer[bufferIndex + SmbConstants.FlagsOffset];
Flags2 = ReadInt2(buffer, bufferIndex + SmbConstants.FlagsOffset + 1);
Tid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset);
Pid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset + 2);
Uid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset + 4);
Mid = ReadInt2(buffer, bufferIndex + SmbConstants.TidOffset + 6);
return SmbConstants.HeaderLength;
}
internal virtual bool IsResponse()
{
return (Flags & SmbConstants.FlagsResponse) == SmbConstants.FlagsResponse;
}
internal abstract int WriteParameterWordsWireFormat(byte[] dst, int dstIndex);
internal abstract int WriteBytesWireFormat(byte[] dst, int dstIndex);
internal abstract int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex
);
internal abstract int ReadBytesWireFormat(byte[] buffer, int bufferIndex);
public override int GetHashCode()
{
return Mid;
}
public override bool Equals(object obj)
{
return obj is ServerMessageBlock && ((ServerMessageBlock)obj)
.Mid == Mid;
}
public override string ToString()
{
string c;
switch (Command)
{
case SmbComNegotiate:
{
c = "SMB_COM_NEGOTIATE";
break;
}
case SmbComSessionSetupAndx:
{
c = "SMB_COM_SESSION_SETUP_ANDX";
break;
}
case SmbComTreeConnectAndx:
{
c = "SMB_COM_TREE_CONNECT_ANDX";
break;
}
case SmbComQueryInformation:
{
c = "SMB_COM_QUERY_INFORMATION";
break;
}
case SmbComCheckDirectory:
{
c = "SMB_COM_CHECK_DIRECTORY";
break;
}
case SmbComTransaction:
{
c = "SMB_COM_TRANSACTION";
break;
}
case SmbComTransaction2:
{
c = "SMB_COM_TRANSACTION2";
break;
}
case SmbComTransactionSecondary:
{
c = "SMB_COM_TRANSACTION_SECONDARY";
break;
}
case SmbComFindClose2:
{
c = "SMB_COM_FIND_CLOSE2";
break;
}
case SmbComTreeDisconnect:
{
c = "SMB_COM_TREE_DISCONNECT";
break;
}
case SmbComLogoffAndx:
{
c = "SMB_COM_LOGOFF_ANDX";
break;
}
case SmbComEcho:
{
c = "SMB_COM_ECHO";
break;
}
case SmbComMove:
{
c = "SMB_COM_MOVE";
break;
}
case SmbComRename:
{
c = "SMB_COM_RENAME";
break;
}
case SmbComDelete:
{
c = "SMB_COM_DELETE";
break;
}
case SmbComDeleteDirectory:
{
c = "SMB_COM_DELETE_DIRECTORY";
break;
}
case SmbComNtCreateAndx:
{
c = "SMB_COM_NT_CREATE_ANDX";
break;
}
case SmbComOpenAndx:
{
c = "SMB_COM_OPEN_ANDX";
break;
}
case SmbComReadAndx:
{
c = "SMB_COM_READ_ANDX";
break;
}
case SmbComClose:
{
c = "SMB_COM_CLOSE";
break;
}
case SmbComWriteAndx:
{
c = "SMB_COM_WRITE_ANDX";
break;
}
case SmbComCreateDirectory:
{
c = "SMB_COM_CREATE_DIRECTORY";
break;
}
case SmbComNtTransact:
{
c = "SMB_COM_NT_TRANSACT";
break;
}
case SmbComNtTransactSecondary:
{
c = "SMB_COM_NT_TRANSACT_SECONDARY";
break;
}
default:
{
c = "UNKNOWN";
break;
}
}
string str = ErrorCode == 0 ? "0" : SmbException.GetMessageByCode(ErrorCode);
return "command=" + c + ",received=" + Received + ",errorCode=" + str
+ ",flags=0x" + Hexdump.ToHexString(Flags & 0xFF, 4) + ",flags2=0x"
+ Hexdump.ToHexString(Flags2, 4) + ",signSeq=" + SignSeq + ",tid=" + Tid + ",pid="
+ Pid + ",uid=" + Uid + ",mid=" + Mid + ",wordCount=" + WordCount + ",byteCount="
+ ByteCount;
}
}
}
|