clamsend.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 001.001.000 |
  3. |==============================================================================|
  4. | Content: ClamAV-daemon client |
  5. |==============================================================================|
  6. | Copyright (c)2005-2009, 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-2009. |
  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. unit clamsend;
  54. interface
  55. uses
  56. SysUtils, Classes,
  57. synsock, blcksock, synautil;
  58. const
  59. cClamProtocol = '3310';
  60. type
  61. {:@abstract(Implementation of ClamAV-daemon client protocol)
  62. By this class you can scan any your data by ClamAV opensource antivirus.
  63. This class can connect to ClamD by TCP channel, send your data to ClamD
  64. and read result.}
  65. TClamSend = class(TSynaClient)
  66. private
  67. FSock: TTCPBlockSocket;
  68. FDSock: TTCPBlockSocket;
  69. FSession: boolean;
  70. function Login: boolean; virtual;
  71. function Logout: Boolean; virtual;
  72. function OpenStream: Boolean; virtual;
  73. public
  74. constructor Create;
  75. destructor Destroy; override;
  76. {:Call any command to ClamD. Used internally by other methods.}
  77. function DoCommand(const Value: AnsiString): AnsiString; virtual;
  78. {:Return ClamAV version and version of loaded databases.}
  79. function GetVersion: AnsiString; virtual;
  80. {:Scan content of TStrings.}
  81. function ScanStrings(const Value: TStrings): AnsiString; virtual;
  82. {:Scan content of TStream.}
  83. function ScanStream(const Value: TStream): AnsiString; virtual;
  84. {:Scan content of TStrings by new 0.95 API.}
  85. function ScanStrings2(const Value: TStrings): AnsiString; virtual;
  86. {:Scan content of TStream by new 0.95 API.}
  87. function ScanStream2(const Value: TStream): AnsiString; virtual;
  88. published
  89. {:Socket object used for TCP/IP operation. Good for seting OnStatus hook, etc.}
  90. property Sock: TTCPBlockSocket read FSock;
  91. {:Socket object used for TCP data transfer operation. Good for seting OnStatus hook, etc.}
  92. property DSock: TTCPBlockSocket read FDSock;
  93. {:Can turn-on session mode of communication with ClamD. Default is @false,
  94. because ClamAV developers design their TCP code very badly and session mode
  95. is broken now (CVS-20051031). Maybe ClamAV developers fix their bugs
  96. and this mode will be possible in future.}
  97. property Session: boolean read FSession write FSession;
  98. end;
  99. implementation
  100. constructor TClamSend.Create;
  101. begin
  102. inherited Create;
  103. FSock := TTCPBlockSocket.Create;
  104. FDSock := TTCPBlockSocket.Create;
  105. FTimeout := 60000;
  106. FTargetPort := cClamProtocol;
  107. FSession := false;
  108. end;
  109. destructor TClamSend.Destroy;
  110. begin
  111. Logout;
  112. FDSock.Free;
  113. FSock.Free;
  114. inherited Destroy;
  115. end;
  116. function TClamSend.DoCommand(const Value: AnsiString): AnsiString;
  117. begin
  118. Result := '';
  119. if not FSession then
  120. FSock.CloseSocket
  121. else
  122. FSock.SendString(Value + LF);
  123. if not FSession or (FSock.LastError <> 0) then
  124. begin
  125. if Login then
  126. FSock.SendString(Value + LF)
  127. else
  128. Exit;
  129. end;
  130. Result := FSock.RecvTerminated(FTimeout, LF);
  131. end;
  132. function TClamSend.Login: boolean;
  133. begin
  134. Result := False;
  135. Sock.CloseSocket;
  136. FSock.Bind(FIPInterface, cAnyPort);
  137. if FSock.LastError <> 0 then
  138. Exit;
  139. FSock.Connect(FTargetHost, FTargetPort);
  140. if FSock.LastError <> 0 then
  141. Exit;
  142. if FSession then
  143. FSock.SendString('SESSION' + LF);
  144. Result := FSock.LastError = 0;
  145. end;
  146. function TClamSend.Logout: Boolean;
  147. begin
  148. FSock.SendString('END' + LF);
  149. Result := FSock.LastError = 0;
  150. FSock.CloseSocket;
  151. end;
  152. function TClamSend.GetVersion: AnsiString;
  153. begin
  154. Result := DoCommand('nVERSION');
  155. end;
  156. function TClamSend.OpenStream: Boolean;
  157. var
  158. S: AnsiString;
  159. begin
  160. Result := False;
  161. s := DoCommand('nSTREAM');
  162. if (s <> '') and (Copy(s, 1, 4) = 'PORT') then
  163. begin
  164. s := SeparateRight(s, ' ');
  165. FDSock.CloseSocket;
  166. FDSock.Bind(FIPInterface, cAnyPort);
  167. if FDSock.LastError <> 0 then
  168. Exit;
  169. FDSock.Connect(FTargetHost, s);
  170. if FDSock.LastError <> 0 then
  171. Exit;
  172. Result := True;
  173. end;
  174. end;
  175. function TClamSend.ScanStrings(const Value: TStrings): AnsiString;
  176. begin
  177. Result := '';
  178. if OpenStream then
  179. begin
  180. DSock.SendString(Value.Text);
  181. DSock.CloseSocket;
  182. Result := FSock.RecvTerminated(FTimeout, LF);
  183. end;
  184. end;
  185. function TClamSend.ScanStream(const Value: TStream): AnsiString;
  186. begin
  187. Result := '';
  188. if OpenStream then
  189. begin
  190. DSock.SendStreamRaw(Value);
  191. DSock.CloseSocket;
  192. Result := FSock.RecvTerminated(FTimeout, LF);
  193. end;
  194. end;
  195. function TClamSend.ScanStrings2(const Value: TStrings): AnsiString;
  196. var
  197. i: integer;
  198. s: AnsiString;
  199. begin
  200. Result := '';
  201. if not FSession then
  202. FSock.CloseSocket
  203. else
  204. FSock.sendstring('nINSTREAM' + LF);
  205. if not FSession or (FSock.LastError <> 0) then
  206. begin
  207. if Login then
  208. FSock.sendstring('nINSTREAM' + LF)
  209. else
  210. Exit;
  211. end;
  212. s := Value.text;
  213. i := length(s);
  214. FSock.SendString(CodeLongint(i) + s + #0#0#0#0);
  215. Result := FSock.RecvTerminated(FTimeout, LF);
  216. end;
  217. function TClamSend.ScanStream2(const Value: TStream): AnsiString;
  218. var
  219. i: integer;
  220. s: AnsiString;
  221. begin
  222. Result := '';
  223. if not FSession then
  224. FSock.CloseSocket
  225. else
  226. FSock.sendstring('nINSTREAM' + LF);
  227. if not FSession or (FSock.LastError <> 0) then
  228. begin
  229. if Login then
  230. FSock.sendstring('nINSTREAM' + LF)
  231. else
  232. Exit;
  233. end;
  234. i := value.Size;
  235. FSock.SendString(CodeLongint(i));
  236. FSock.SendStreamRaw(Value);
  237. FSock.SendString(#0#0#0#0);
  238. Result := FSock.RecvTerminated(FTimeout, LF);
  239. end;
  240. end.