clamsend.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 001.001.001 |
  3. |==============================================================================|
  4. | Content: ClamAV-daemon client |
  5. |==============================================================================|
  6. | Copyright (c)2005-2010, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2005-2010. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@abstract( ClamAV-daemon client)
  45. This unit is capable to do antivirus scan of your data by TCP channel to ClamD
  46. daemon from ClamAV. See more about ClamAV on @LINK(http://www.clamav.net)
  47. }
  48. {$IFDEF FPC}
  49. {$MODE DELPHI}
  50. {$ENDIF}
  51. {$Q-}
  52. {$H+}
  53. {$IFDEF UNICODE}
  54. {$WARN IMPLICIT_STRING_CAST OFF}
  55. {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  56. {$ENDIF}
  57. unit clamsend;
  58. interface
  59. uses
  60. SysUtils, Classes,
  61. synsock, blcksock, synautil;
  62. const
  63. cClamProtocol = '3310';
  64. type
  65. {:@abstract(Implementation of ClamAV-daemon client protocol)
  66. By this class you can scan any your data by ClamAV opensource antivirus.
  67. This class can connect to ClamD by TCP channel, send your data to ClamD
  68. and read result.}
  69. TClamSend = class(TSynaClient)
  70. private
  71. FSock: TTCPBlockSocket;
  72. FDSock: TTCPBlockSocket;
  73. FSession: boolean;
  74. function Login: boolean; virtual;
  75. function Logout: Boolean; virtual;
  76. function OpenStream: Boolean; virtual;
  77. public
  78. constructor Create;
  79. destructor Destroy; override;
  80. {:Call any command to ClamD. Used internally by other methods.}
  81. function DoCommand(const Value: AnsiString): AnsiString; virtual;
  82. {:Return ClamAV version and version of loaded databases.}
  83. function GetVersion: AnsiString; virtual;
  84. {:Scan content of TStrings.}
  85. function ScanStrings(const Value: TStrings): AnsiString; virtual;
  86. {:Scan content of TStream.}
  87. function ScanStream(const Value: TStream): AnsiString; virtual;
  88. {:Scan content of TStrings by new 0.95 API.}
  89. function ScanStrings2(const Value: TStrings): AnsiString; virtual;
  90. {:Scan content of TStream by new 0.95 API.}
  91. function ScanStream2(const Value: TStream): AnsiString; virtual;
  92. published
  93. {:Socket object used for TCP/IP operation. Good for seting OnStatus hook, etc.}
  94. property Sock: TTCPBlockSocket read FSock;
  95. {:Socket object used for TCP data transfer operation. Good for seting OnStatus hook, etc.}
  96. property DSock: TTCPBlockSocket read FDSock;
  97. {:Can turn-on session mode of communication with ClamD. Default is @false,
  98. because ClamAV developers design their TCP code very badly and session mode
  99. is broken now (CVS-20051031). Maybe ClamAV developers fix their bugs
  100. and this mode will be possible in future.}
  101. property Session: boolean read FSession write FSession;
  102. end;
  103. implementation
  104. constructor TClamSend.Create;
  105. begin
  106. inherited Create;
  107. FSock := TTCPBlockSocket.Create;
  108. FSock.Owner := self;
  109. FDSock := TTCPBlockSocket.Create;
  110. FDSock.Owner := self;
  111. FTimeout := 60000;
  112. FTargetPort := cClamProtocol;
  113. FSession := false;
  114. end;
  115. destructor TClamSend.Destroy;
  116. begin
  117. Logout;
  118. FDSock.Free;
  119. FSock.Free;
  120. inherited Destroy;
  121. end;
  122. function TClamSend.DoCommand(const Value: AnsiString): AnsiString;
  123. begin
  124. Result := '';
  125. if not FSession then
  126. FSock.CloseSocket
  127. else
  128. FSock.SendString(Value + LF);
  129. if not FSession or (FSock.LastError <> 0) then
  130. begin
  131. if Login then
  132. FSock.SendString(Value + LF)
  133. else
  134. Exit;
  135. end;
  136. Result := FSock.RecvTerminated(FTimeout, LF);
  137. end;
  138. function TClamSend.Login: boolean;
  139. begin
  140. Result := False;
  141. Sock.CloseSocket;
  142. FSock.Bind(FIPInterface, cAnyPort);
  143. if FSock.LastError <> 0 then
  144. Exit;
  145. FSock.Connect(FTargetHost, FTargetPort);
  146. if FSock.LastError <> 0 then
  147. Exit;
  148. if FSession then
  149. FSock.SendString('SESSION' + LF);
  150. Result := FSock.LastError = 0;
  151. end;
  152. function TClamSend.Logout: Boolean;
  153. begin
  154. FSock.SendString('END' + LF);
  155. Result := FSock.LastError = 0;
  156. FSock.CloseSocket;
  157. end;
  158. function TClamSend.GetVersion: AnsiString;
  159. begin
  160. Result := DoCommand('nVERSION');
  161. end;
  162. function TClamSend.OpenStream: Boolean;
  163. var
  164. S: AnsiString;
  165. begin
  166. Result := False;
  167. s := DoCommand('nSTREAM');
  168. if (s <> '') and (Copy(s, 1, 4) = 'PORT') then
  169. begin
  170. s := SeparateRight(s, ' ');
  171. FDSock.CloseSocket;
  172. FDSock.Bind(FIPInterface, cAnyPort);
  173. if FDSock.LastError <> 0 then
  174. Exit;
  175. FDSock.Connect(FTargetHost, s);
  176. if FDSock.LastError <> 0 then
  177. Exit;
  178. Result := True;
  179. end;
  180. end;
  181. function TClamSend.ScanStrings(const Value: TStrings): AnsiString;
  182. begin
  183. Result := '';
  184. if OpenStream then
  185. begin
  186. DSock.SendString(Value.Text);
  187. DSock.CloseSocket;
  188. Result := FSock.RecvTerminated(FTimeout, LF);
  189. end;
  190. end;
  191. function TClamSend.ScanStream(const Value: TStream): AnsiString;
  192. begin
  193. Result := '';
  194. if OpenStream then
  195. begin
  196. DSock.SendStreamRaw(Value);
  197. DSock.CloseSocket;
  198. Result := FSock.RecvTerminated(FTimeout, LF);
  199. end;
  200. end;
  201. function TClamSend.ScanStrings2(const Value: TStrings): AnsiString;
  202. var
  203. i: integer;
  204. s: AnsiString;
  205. begin
  206. Result := '';
  207. if not FSession then
  208. FSock.CloseSocket
  209. else
  210. FSock.sendstring('nINSTREAM' + LF);
  211. if not FSession or (FSock.LastError <> 0) then
  212. begin
  213. if Login then
  214. FSock.sendstring('nINSTREAM' + LF)
  215. else
  216. Exit;
  217. end;
  218. s := Value.text;
  219. i := length(s);
  220. FSock.SendString(CodeLongint(i) + s + #0#0#0#0);
  221. Result := FSock.RecvTerminated(FTimeout, LF);
  222. end;
  223. function TClamSend.ScanStream2(const Value: TStream): AnsiString;
  224. var
  225. i: integer;
  226. begin
  227. Result := '';
  228. if not FSession then
  229. FSock.CloseSocket
  230. else
  231. FSock.sendstring('nINSTREAM' + LF);
  232. if not FSession or (FSock.LastError <> 0) then
  233. begin
  234. if Login then
  235. FSock.sendstring('nINSTREAM' + LF)
  236. else
  237. Exit;
  238. end;
  239. i := value.Size;
  240. FSock.SendString(CodeLongint(i));
  241. FSock.SendStreamRaw(Value);
  242. FSock.SendString(#0#0#0#0);
  243. Result := FSock.RecvTerminated(FTimeout, LF);
  244. end;
  245. end.