# -*- Fundamental -*-
#
# (C) 2002 Michel Arboi <arboi@bigfoot.com>
# $Revision: 1.9.2.1 $

function ftp_close(socket)
{
  if (send(socket: socket, data: string("QUIT\r\n")))
  {
    __r = "550 ";
    while ((__r) && ! ("221 " >< __r))
    {
      __r = recv_line(socket:socket, length: 1024);
      #display ("QUIT answer = ", __r);
    }
  }
  close(socket);
}

function get_ftp_banner(port)
{
  ___sb = string("ftp/banner/", port);
  ___banner = get_kb_item(___sb);
  if (___banner) return(___banner);

  if (! get_port_state(port)) return (0);
  ___soc = open_sock_tcp(port);
  if(!___soc) return (0);
  ___banner = recv_line(socket:___soc, length:1024);
  if(ereg(string:___banner, pattern:"^220-"))
  {
   ___str =  recv_line(socket:___soc, length:1024);
   ___banner = string(___banner, ___str);
   while(ereg(string:___str, pattern:"^220-"))
   {
    ___str =  recv_line(socket:___soc, length:1024);
    ___banner = string(___banner, ___str);
   }
  }
  ftp_close(socket: ___soc);
  set_kb_item(name: ___sb, value: ___banner);
  return(___banner);
}


function ftp_recv_line(socket)
{
 __n = 0;
 __r = recv_line(socket:socket, length:1024);
 if(strlen(__r) < 4) 
  return(__r);
  
 while(__r[3] == "-")
 {
  __n = __n + 1;
  __r = recv_line(socket:socket, length:1024);
  if(__n > 1024)
   return(0);
 }
 return(__r);
}
