<!--
Copyright (C) 2012-2022 Greenbone AG

SPDX-License-Identifier: AGPL-3.0-or-later
-->

<!--
  This wizard sets up a new task and target to run an unauthenticated scan on a
  host (or range of hosts) specified by the parameter "hosts" using the
  "Full and Fast" scan config and default port range.

  Required parameters:
  hosts : The hosts to scan. Syntax is the same as for the hosts parameter of
          the GMP command CREATE_TARGET.

  To run this wizard, use the GMP command RUN_WIZARD.
  Please see the GMP documentation for details.
 -->

<wizard>
  <params>
    <!-- ID of the config to use (optional) -->
    <param>
      <optional>1</optional>
      <name>config_id</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>

    <!-- ID of the alert to use (optional) -->
    <param>
      <optional>1</optional>
      <name>alert_id</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>

    <!-- ID of the Scanner for the task (optional) -->
    <param>
      <optional>1</optional>
      <name>scanner_id</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>

    <!-- ID of the slave to use (optional) -->
    <param>
      <optional>1</optional>
      <name>slave_id</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>

    <!-- Optional credentials -->
    <param>
      <optional>1</optional>
      <name>smb_credential</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>ssh_credential</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>ssh_port</name>
      <regex>^0*(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d\d\d|[1-5]\d\d\d\d|\d{1,4})$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>esxi_credential</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>

    <!-- The target hosts to scan -->
    <param>
      <optional>0</optional>
      <name>hosts</name>
      <regex>^[-[:alnum:],: \\./]{1,2000}$</regex>
    </param>

    <!-- ID of the Port List for the Target (optional) -->
    <param>
      <optional>1</optional>
      <name>port_list_id</name>
      <regex>^[a-z0-9\\-]+$</regex>
    </param>
  </params>

  <step>
    <command>
      <xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:date="http://exslt.org/dates-and-times"
          extension-element-prefixes="date">
        <xsl:output
          method="xml"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <xsl:variable
            name="timestamp"
            select="concat (substring (date:date(), 1, 10), ' ', substring (date:time(), 1, 8))"/>
          <create_target>
            <name>
              <xsl:text>Target for immediate scan of IP </xsl:text>
              <xsl:value-of select="/wizard/params/param[name='hosts']/value"/>
              <xsl:text> - </xsl:text>
              <xsl:value-of select="$timestamp"/>
            </name>
            <hosts><xsl:value-of select="/wizard/params/param[name='hosts']/value"/></hosts>

            <xsl:choose>
              <xsl:when test="/wizard/params/param[name='port_list_id']/value != ''">
                <port_list id="{/wizard/params/param[name='port_list_id']/value}"/>
              </xsl:when>
              <xsl:otherwise>
                <port_list id="33d0cd82-57c6-11e1-8ed1-406186ea4fc5"/>
              </xsl:otherwise>
            </xsl:choose>

            <xsl:if test="/wizard/params/param[name='ssh_credential']/value != ''">
              <ssh_lsc_credential id="{/wizard/params/param[name='ssh_credential']/value}">
                <port>
                  <xsl:choose>
                    <xsl:when test="/wizard/params/param[name='ssh_port']/value != ''">
                      <xsl:value-of select="/wizard/params/param[name='ssh_port']/value"/>
                    </xsl:when>
                    <xsl:otherwise>22</xsl:otherwise>
                  </xsl:choose>
                </port>
              </ssh_lsc_credential>
            </xsl:if>
            <xsl:if test="/wizard/params/param[name='smb_credential']/value != ''">
              <smb_lsc_credential id="{/wizard/params/param[name='smb_credential']/value}"/>
            </xsl:if>
            <xsl:if test="/wizard/params/param[name='esxi_credential']/value != ''">
              <esxi_lsc_credential id="{/wizard/params/param[name='esxi_credential']/value}"/>
            </xsl:if>
          </create_target>
        </xsl:template>
      </xsl:stylesheet>
    </command>
    <extra_data>
      <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output
          method="xml"
          omit-xml-declaration="yes"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <target id="{/wizard/current/response/create_target_response/@id}"/>
        </xsl:template>
      </xsl:stylesheet>
    </extra_data>
  </step>
  <step>
    <command>
      <xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output
          method="xml"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <create_task>
            <name>Immediate scan of IP <xsl:value-of select="/wizard/params/param[name='hosts']/value"/></name>
            <target id="{/wizard/previous/response/create_target_response/@id}"/>
            <xsl:choose>
              <xsl:when test="/wizard/params/param[name='config_id']/value != ''">
                <config id="{/wizard/params/param[name='config_id']/value}"/>
              </xsl:when>
              <xsl:otherwise>
                <config id="daba56c8-73ec-11df-a475-002264764cea"/>
              </xsl:otherwise>
            </xsl:choose>

            <xsl:if test="/wizard/params/param[name='alert_id']/value != ''">
              <alert id="{/wizard/params/param[name='alert_id']/value}"/>
            </xsl:if>

            <xsl:if test="/wizard/params/param[name='scanner_id']/value != ''">
              <scanner id="{/wizard/params/param[name='scanner_id']/value}"/>
            </xsl:if>

            <xsl:if test="/wizard/params/param[name='slave_id']/value != ''">
              <slave id="{/wizard/params/param[name='slave_id']/value}"/>
            </xsl:if>
          </create_task>
        </xsl:template>
      </xsl:stylesheet>
    </command>
    <extra_data>
      <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output
          method="xml"
          omit-xml-declaration="yes"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <task id="{/wizard/current/response/create_task_response/@id}"/>
          <xsl:copy-of select="/wizard/previous/extra_data/*"/>
        </xsl:template>
      </xsl:stylesheet>
    </extra_data>
  </step>
  <step>
    <command>
      <xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output
          method="xml"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <start_task task_id="{/wizard/previous/response/create_task_response/@id}"/>
        </xsl:template>
      </xsl:stylesheet>
    </command>
    <extra_data>
      <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output
          method="xml"
          omit-xml-declaration="yes"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <xsl:copy-of select="/wizard/previous/extra_data/*"/>
          <xsl:choose>
            <xsl:when test="/wizard/current/response/start_task_response/@status = '200'">
              <status_text>Task created and started successfully</status_text>
              <status>201</status>
            </xsl:when>
            <xsl:otherwise>
              <status_text>Failed to start task: <xsl:value-of select="/wizard/current/response/start_task_response/@status_text"/></status_text>
              <status><xsl:value-of select="/wizard/current/response/start_task_response/@status"/></status>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:template>
      </xsl:stylesheet>
    </extra_data>
  </step>
  <step>
    <command>
      <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output
          method="xml"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <xsl:choose>
            <xsl:when test="starts-with (/wizard/previous/extra_data/status, '2')">
              <get_tasks task_id="{/wizard/previous/extra_data/task/@id}"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:if test="/wizard/previous/extra_data/task/@id != ''">
                <delete_task task_id="{/wizard/previous/extra_data/task/@id}" ultimate="1"/>
              </xsl:if>
              <xsl:if test="/wizard/previous/extra_data/target/@id != ''">
                <delete_target target_id="{/wizard/previous/extra_data/target/@id}" ultimate="1"/>
              </xsl:if>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:template>
      </xsl:stylesheet>
    </command>
  </step>

  <mode>
    <name>init</name>
    <read_only>1</read_only>
    <step>
      <command>
        <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:output
            method="xml"
            encoding="UTF-8"/>
          <xsl:template match="/">
            <get_settings />
          </xsl:template>
        </xsl:stylesheet>
      </command>
    </step>
  </mode>
</wizard>
