NegotiateHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using SMBLibrary.Authentication;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. /// <summary>
  15. /// Negotiate helper
  16. /// </summary>
  17. public class NegotiateHelper
  18. {
  19. public const string SMB2002Dialect = "SMB 2.002";
  20. public const string SMB2xxxDialect = "SMB 2.???";
  21. // Special case - SMB2 client initially connecting using SMB1
  22. internal static SMB2Command GetNegotiateResponse(List<string> smb2Dialects, ConnectionState state, Guid serverGuid)
  23. {
  24. NegotiateResponse response = new NegotiateResponse();
  25. response.Header.Credits = 1;
  26. if (smb2Dialects.Contains(SMB2xxxDialect))
  27. {
  28. response.DialectRevision = SMB2Dialect.SMB2xx;
  29. }
  30. else if (smb2Dialects.Contains(SMB2002Dialect))
  31. {
  32. state.ServerDialect = SMBDialect.SMB202;
  33. response.DialectRevision = SMB2Dialect.SMB202;
  34. }
  35. else
  36. {
  37. throw new ArgumentException("SMB2 dialect is not present");
  38. }
  39. response.ServerGuid = serverGuid;
  40. response.MaxTransactSize = 65536;
  41. response.MaxReadSize = 65536;
  42. response.MaxWriteSize = 65536;
  43. response.SystemTime = DateTime.Now;
  44. response.ServerStartTime = DateTime.Today;
  45. return response;
  46. }
  47. internal static SMB2Command GetNegotiateResponse(NegotiateRequest request, ConnectionState state, Guid serverGuid)
  48. {
  49. NegotiateResponse response = new NegotiateResponse();
  50. if (request.Dialects.Contains(SMB2Dialect.SMB210))
  51. {
  52. state.ServerDialect = SMBDialect.SMB210;
  53. response.DialectRevision = SMB2Dialect.SMB210;
  54. }
  55. else if (request.Dialects.Contains(SMB2Dialect.SMB202))
  56. {
  57. state.ServerDialect = SMBDialect.SMB202;
  58. response.DialectRevision = SMB2Dialect.SMB202;
  59. }
  60. else
  61. {
  62. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  63. }
  64. response.ServerGuid = serverGuid;
  65. response.MaxTransactSize = 65536;
  66. response.MaxReadSize = 65536;
  67. response.MaxWriteSize = 65536;
  68. response.SystemTime = DateTime.Now;
  69. response.ServerStartTime = DateTime.Today;
  70. return response;
  71. }
  72. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.SMB1Message message)
  73. {
  74. if (message.Commands.Count > 0 && message.Commands[0] is SMBLibrary.SMB1.NegotiateRequest)
  75. {
  76. SMBLibrary.SMB1.NegotiateRequest request = (SMBLibrary.SMB1.NegotiateRequest)message.Commands[0];
  77. return FindSMB2Dialects(request);
  78. }
  79. return new List<string>();
  80. }
  81. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.NegotiateRequest request)
  82. {
  83. List<string> result = new List<string>();
  84. if (request.Dialects.Contains(SMB2002Dialect))
  85. {
  86. result.Add(SMB2002Dialect);
  87. }
  88. if (request.Dialects.Contains(SMB2xxxDialect))
  89. {
  90. result.Add(SMB2xxxDialect);
  91. }
  92. return result;
  93. }
  94. }
  95. }