###############################################################################
# OpenVAS Vulnerability Test
# $Id: secpod_openjdk_detect.nasl 1703 2009-05-12 15:13:29Z may $
#
# OpenJDK Version Detection
#
# Authors:
# Sharath S <sharaths@secpod.com>
#
# Copyright:
# Copyright (c) 2009 SecPod, http://www.secpod.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# (or any later version), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
###############################################################################

if(description)
{
  script_id(900334);
  script_version("$Revision: 1.0 $");
  script_tag(name:"risk_factor", value:"None");
  script_name("OpenJDK Version Detection");
  desc = "
  Overview: This script detects the installed version of OpenJDK and sets
  the reuslt in KB.

  Risk Factor: None";

  script_description(desc);
  script_summary("Set KB for the version of OpenJDK");
  script_category(ACT_GATHER_INFO);
  script_copyright("Copyright (C) 2009 SecPod");
  script_family("Service detection");
  exit(0);
}


include("ssh_func.inc");
include("version_func.inc");

sock = ssh_login_or_reuse_connection();
if(!sock){
  exit(0);
}

paths = find_bin(prog_name:"java", sock:sock);
foreach binName (paths)
{
  ver = get_bin_version(full_prog_name:chomp(binName), version_argv:"-version",
                        ver_pattern:"OpenJDK.*([0-9]\.[0-9]\.[0-9._]+)-?([b0-9]+)?",
                        sock:sock);
  if("OpenJDK" >< ver)
  {
    if((ver[1] && ver[2]) != NULL){
      ver = ver[1] + "." + ver[2];
    }
    else{
      ver = ver[1];
    }

    if(ver != NULL)
    {
      set_kb_item(name:"OpenJDK/Ver", value:ver);
      security_note(data:"OpenJDK version " + ver + " running at location " +
                         binName +  " was detected on the host");
      ssh_close_connection();
      exit(0);
    }
  }
}
ssh_close_connection();
