DcerpcPipeHandle.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.IO;
  19. using SharpCifs.Smb;
  20. using SharpCifs.Util;
  21. using SharpCifs.Util.Sharpen;
  22. namespace SharpCifs.Dcerpc
  23. {
  24. public class DcerpcPipeHandle : DcerpcHandle
  25. {
  26. internal SmbNamedPipe Pipe;
  27. internal SmbFileInputStream In;
  28. internal SmbFileOutputStream Out;
  29. internal bool IsStart = true;
  30. /// <exception cref="UnknownHostException"></exception>
  31. /// <exception cref="System.UriFormatException"></exception>
  32. /// <exception cref="SharpCifs.Dcerpc.DcerpcException"></exception>
  33. public DcerpcPipeHandle(string url, NtlmPasswordAuthentication auth)
  34. {
  35. Binding = ParseBinding(url);
  36. url = "smb://" + Binding.Server + "/IPC$/" + Runtime.Substring(Binding.Endpoint
  37. , 6);
  38. string @params = string.Empty;
  39. string server;
  40. string address;
  41. server = (string)Binding.GetOption("server");
  42. if (server != null)
  43. {
  44. @params += "&server=" + server;
  45. }
  46. address = (string)Binding.GetOption("address");
  47. if (server != null)
  48. {
  49. @params += "&address=" + address;
  50. }
  51. if (@params.Length > 0)
  52. {
  53. url += "?" + Runtime.Substring(@params, 1);
  54. }
  55. Pipe = new SmbNamedPipe(url, (unchecked(0x2019F) << 16) | SmbNamedPipe.PipeTypeRdwr
  56. | SmbNamedPipe.PipeTypeDceTransact, auth);
  57. }
  58. /// <exception cref="System.IO.IOException"></exception>
  59. protected internal override void DoSendFragment(byte[] buf, int off, int length,
  60. bool isDirect)
  61. {
  62. if (Out != null && Out.IsOpen() == false)
  63. {
  64. throw new IOException("DCERPC pipe is no longer open");
  65. }
  66. if (In == null)
  67. {
  68. In = (SmbFileInputStream)Pipe.GetNamedPipeInputStream();
  69. }
  70. if (Out == null)
  71. {
  72. Out = (SmbFileOutputStream)Pipe.GetNamedPipeOutputStream();
  73. }
  74. if (isDirect)
  75. {
  76. Out.WriteDirect(buf, off, length, 1);
  77. return;
  78. }
  79. Out.Write(buf, off, length);
  80. }
  81. /// <exception cref="System.IO.IOException"></exception>
  82. protected internal override void DoReceiveFragment(byte[] buf, bool isDirect)
  83. {
  84. int off;
  85. int flags;
  86. int length;
  87. if (buf.Length < MaxRecv)
  88. {
  89. throw new ArgumentException("buffer too small");
  90. }
  91. if (IsStart && !isDirect)
  92. {
  93. // start of new frag, do trans
  94. off = In.Read(buf, 0, 1024);
  95. }
  96. else
  97. {
  98. off = In.ReadDirect(buf, 0, buf.Length);
  99. }
  100. if (buf[0] != 5 && buf[1] != 0)
  101. {
  102. throw new IOException("Unexpected DCERPC PDU header");
  103. }
  104. flags = buf[3] & unchecked(0xFF);
  105. // next read is start of new frag
  106. IsStart = (flags & DcerpcConstants.DcerpcLastFrag) == DcerpcConstants.DcerpcLastFrag;
  107. length = Encdec.Dec_uint16le(buf, 8);
  108. if (length > MaxRecv)
  109. {
  110. throw new IOException("Unexpected fragment length: " + length);
  111. }
  112. while (off < length)
  113. {
  114. off += In.ReadDirect(buf, off, length - off);
  115. }
  116. }
  117. /// <exception cref="System.IO.IOException"></exception>
  118. public override void Close()
  119. {
  120. State = 0;
  121. if (Out != null)
  122. {
  123. Out.Close();
  124. }
  125. }
  126. }
  127. }