<?xml version="1.0" encoding="UTF-8"?>
<!-- -=-=-=-=--=--=--=-=--=--=-=--=--=-=-=--=--=--=--=-=-=-=--=-=-=-=--=-=--=-=--=-=-=-=
	nmap XSL stylesheet to generate SQL queries to populate a database.
	Some conceptual inspiration was found in Benjamin Erb's nmap.xsl stylesheet
	for pretty HTML reports visit his site: http://www.benjamin-erb.de
	Modified : 3/22/2006
	Daniel Van Derveer
	-=-=-=-=--=--=--=-=--=--=-=--=--=-=-=--=--=--=--=-=-=-=--=-=-=-=--=-=--=-=--=-=-=-=
	License:
	Copyright (c) 2006 Daniel Van Derveer
	
	Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.
	4. If used in a commercial application(both public and private distribution) a separate license must 
	   be obtained from Daniel Van Derveer.
	
	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:output method="text" indent="no" encoding="UTF-8" />

<xsl:template match="/">
	<xsl:apply-templates />
</xsl:template>

<!-- Create the scan record in the scans table -->
<xsl:variable name="scanid"><xsl:value-of select="/nmaprun/@start" /></xsl:variable>
<xsl:template match="/nmaprun">
	INSERT INTO scans (scanID,scanCMD,scanStart,scannmapver) values(<xsl:value-of select="/nmaprun/@start" />,"<xsl:value-of select="/nmaprun/@args" />","<xsl:value-of select="/nmaprun/@startstr" />","<xsl:value-of select="/nmaprun/@version" />");
	<xsl:apply-templates/>
</xsl:template>

<!-- the host template does most of the work with for-each loops to process addresses, 
	 hostnames, ports, and os match. -->
<xsl:template match="host">
	<!-- The IP and MAC are used to look up the hostID in the SQL Inserts -->
	<xsl:variable name="currIP"><xsl:value-of select="address[@addrtype='ipv4']/@addr"/></xsl:variable>
	<xsl:variable name="currMAC"><xsl:value-of select="address[@addrtype='mac']/@addr"/></xsl:variable>
	<!-- Create host entry with IP, MAC, NIC Manufacturer -->
	<p>INSERT into hosts (hostIP,hostMAC,hostNICManuf,host_scanID) values("<xsl:value-of select="$currIP" />","<xsl:value-of select="$currMAC"/>","<xsl:value-of select="address[@addrtype='mac']/@vendor"/>","<xsl:value-of select="$scanid" />");</p>
	<!-- Add each hostname for a host to the hostnames table -->
	<xsl:for-each select="hostnames/hostname">
		<p>INSERT into hostnames (hostname_hostID,hostname,hostname_scanID) 
		   values((SELECT hostID from hosts where hostIP = "<xsl:value-of select="$currIP" />" and hostMAC="<xsl:value-of select="$currMAC"/>" and host_scanID = <xsl:value-of select="$scanid" />),
		"<xsl:value-of select="@name" />","<xsl:value-of select="$scanid" />");</p>
	</xsl:for-each>
	<!-- Add each port number, status, and protocol to the port table -->
	<xsl:for-each select="ports/port">
		<p>INSERT into ports (port,port_hostID,port_scanID,port_status,port_protocol) 
			values(<xsl:value-of select="@portid"/>,(SELECT hostID from hosts where hostIP = "<xsl:value-of select="$currIP" />" and hostMAC="<xsl:value-of select="$currMAC"/>" and host_scanID = <xsl:value-of select="$scanid" />),"<xsl:value-of select="$scanid" />","<xsl:value-of select="state/@state"/>","<xsl:value-of select="@protocol"/>");
		</p>
	</xsl:for-each>
	<!-- Add all OS matches to the os table -->
	<xsl:for-each select="os/osmatch">
		<p>INSERT into os (os_hostID,os_scanID,os_name) values((SELECT hostID from hosts where hostIP = "<xsl:value-of select="$currIP" />" and hostMAC="<xsl:value-of select="$currMAC"/>" and host_scanID = <xsl:value-of select="$scanid" />),"<xsl:value-of select="$scanid" />","<xsl:value-of select="@name"/>");
		</p>
	</xsl:for-each>
	<!-- FIN -->
</xsl:template>

</xsl:stylesheet>

