-----------------------------------------------------------------------
-- Tivoli Storage Manager is an enterprise class data protection / backup  
-- product by IBM.
-- Product information:  http://www-306.ibm.com/software/tivoli/products/storage-mgr/
--
-- The agent portion of the software lives on protected servers and has a
-- HTTP based management interface on TCP port 1581.
-- 
-- This script connects to that port and retrieves the version of the client
-- agent.  It then updates the service version string.
--
-- Based off Diman's showHTMLTitle.nse script, errors are mine...



id = "version.TSM"
description = "Connects to the Tivoli Storage Manager http interface and extracts the version."

author = "Tom Sellers <nmap () fadedcode net>"

license = "Same as Nmap--See http://nmap.org/book/man-legal.html"

categories = {"version"}

require 'stdnse'
require 'shortport'
require 'http'

portrule = shortport.portnumber({1581}, "tcp", {"open", "open|filtered"})

action = function(host, port)
	local data, response
	local version_string = nil
	
	data = http.get( host.ip, port.number, '/BACLIENT' )
	response = data.body
	
	response = string.gsub(response, "<(/?%a+)>", function(c) return "<" .. string.lower(c) .. ">" end)

	version_string = string.match(response, "version = \"(%d%.%d%.%d%.%d).%s*\" platform_name = .")
	
	if version_string == nil then
		stdnse.print_debug("Unable to detect TSM version.")
		stdnse.print_debug (response)
	else
		port.version.product = "Tivoli Storage Manager http interface"
		port.version.version = version_string
		nmap.set_port_version(host, port, "hardmatched")
	end
end

