DcerpcPipeHandle.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, 6);
  37. string @params = string.Empty;
  38. string server;
  39. string address;
  40. server = (string)Binding.GetOption("server");
  41. if (server != null)
  42. {
  43. @params += "&server=" + server;
  44. }
  45. address = (string)Binding.GetOption("address");
  46. if (server != null)
  47. {
  48. @params += "&address=" + address;
  49. }
  50. if (@params.Length > 0)
  51. {
  52. url += "?" + Runtime.Substring(@params, 1);
  53. }
  54. Pipe = new SmbNamedPipe(url,
  55. (unchecked(0x2019F) << 16)
  56. | SmbNamedPipe.PipeTypeRdwr
  57. | SmbNamedPipe.PipeTypeDceTransact,
  58. auth);
  59. }
  60. /// <exception cref="System.IO.IOException"></exception>
  61. protected internal override void DoSendFragment(byte[] buf, int off, int length, bool isDirect)
  62. {
  63. if (Out != null && Out.IsOpen() == false)
  64. {
  65. throw new IOException("DCERPC pipe is no longer open");
  66. }
  67. if (In == null)
  68. {
  69. In = (SmbFileInputStream)Pipe.GetNamedPipeInputStream();
  70. }
  71. if (Out == null)
  72. {
  73. Out = (SmbFileOutputStream)Pipe.GetNamedPipeOutputStream();
  74. }
  75. if (isDirect)
  76. {
  77. Out.WriteDirect(buf, off, length, 1);
  78. return;
  79. }
  80. Out.Write(buf, off, length);
  81. }
  82. /// <exception cref="System.IO.IOException"></exception>
  83. protected internal override void DoReceiveFragment(byte[] buf, bool isDirect)
  84. {
  85. int off;
  86. int flags;
  87. int length;
  88. if (buf.Length < MaxRecv)
  89. {
  90. throw new ArgumentException("buffer too small");
  91. }
  92. if (IsStart && !isDirect)
  93. {
  94. // start of new frag, do trans
  95. off = In.Read(buf, 0, 1024);
  96. }
  97. else
  98. {
  99. off = In.ReadDirect(buf, 0, buf.Length);
  100. }
  101. if (buf[0] != 5 && buf[1] != 0)
  102. {
  103. throw new IOException("Unexpected DCERPC PDU header");
  104. }
  105. flags = buf[3] & unchecked(0xFF);
  106. // next read is start of new frag
  107. IsStart = (flags & DcerpcConstants.DcerpcLastFrag) == DcerpcConstants.DcerpcLastFrag;
  108. length = Encdec.Dec_uint16le(buf, 8);
  109. if (length > MaxRecv)
  110. {
  111. throw new IOException("Unexpected fragment length: " + length);
  112. }
  113. while (off < length)
  114. {
  115. off += In.ReadDirect(buf, off, length - off);
  116. }
  117. }
  118. /// <exception cref="System.IO.IOException"></exception>
  119. public override void Close()
  120. {
  121. State = 0;
  122. if (Out != null)
  123. {
  124. Out.Close();
  125. }
  126. }
  127. }
  128. }