<def-group>
  <definition class="compliance" id="service_ufw_enabled" version="1">
    <metadata>
        <title>Verify ufw Enabled</title>
        
    <affected family="unix">
    <platform>Ubuntu 22.04</platform>
    </affected>
        <description>The ufw service should be enabled if possible.</description>
    </metadata>
    <criteria operator="OR" comment="package ufw installed and service ufw is configured to start or not needed">
      <criteria comment="service ufw is not needed" operator="AND">
        <criterion comment="variable var_network_filtering_service is set to ufw"
        test_ref="service_ufw_enabled_test_variable_var_network_filtering_service" negate="true"/>
      </criteria>
      <criteria comment="package ufw installed and service ufw is configured to start" operator="AND">
      <criterion comment="variable var_network_filtering_service is set to ufw"
      test_ref="service_ufw_enabled_test_variable_var_network_filtering_service" />
      <criterion comment="ufw installed" test_ref="test_service_ufw_package_ufw_installed" />
        <criteria comment="service ufw is configured to start and is running" operator="AND">
          <criterion comment="ufw is running" test_ref="test_service_running_ufw" />
          <criteria operator="OR" comment="service ufw is configured to start">
            <criterion comment="multi-user.target wants ufw" test_ref="test_multi_user_wants_ufw" />
            <criterion comment="multi-user.target wants ufw socket" test_ref="test_multi_user_wants_ufw_socket" />
          </criteria>
        </criteria>
      </criteria>
    </criteria>
  </definition>

  <linux:systemdunitdependency_test check="all" check_existence="any_exist" comment="systemd test" id="test_multi_user_wants_ufw" version="1">
    <linux:object object_ref="object_multi_user_target_for_ufw_enabled" />
    <linux:state state_ref="state_systemd_ufw_on"/>
  </linux:systemdunitdependency_test>
  <linux:systemdunitdependency_object id="object_multi_user_target_for_ufw_enabled" comment="list of dependencies of multi-user.target" version="1">
    <linux:unit>multi-user.target</linux:unit>
  </linux:systemdunitdependency_object>
  <linux:systemdunitdependency_state id="state_systemd_ufw_on" comment="ufw listed at least once in the dependencies" version="1">
    <linux:dependency entity_check="at least one">ufw.service</linux:dependency>
  </linux:systemdunitdependency_state>

  <linux:systemdunitdependency_test check="all" check_existence="any_exist" comment="systemd test" id="test_multi_user_wants_ufw_socket" version="1">
    <linux:object object_ref="object_multi_user_target_for_ufw_socket_enabled" />
    <linux:state state_ref="state_systemd_ufw_socket_on"/>
  </linux:systemdunitdependency_test>
  <linux:systemdunitdependency_object id="object_multi_user_target_for_ufw_socket_enabled" comment="list of dependencies of multi-user.target" version="1">
    <linux:unit>multi-user.target</linux:unit>
  </linux:systemdunitdependency_object>
  <linux:systemdunitdependency_state id="state_systemd_ufw_socket_on" comment="ufw listed at least once in the dependencies" version="1">
    <linux:dependency entity_check="at least one">ufw.socket</linux:dependency>
  </linux:systemdunitdependency_state>

  <linux:systemdunitproperty_test id="test_service_running_ufw" check="at least one" check_existence="at_least_one_exists" comment="Test that the ufw service is running" version="1">
    <linux:object object_ref="obj_service_running_ufw"/>
    <linux:state state_ref="state_service_running_ufw"/>
  </linux:systemdunitproperty_test>
  <linux:systemdunitproperty_object id="obj_service_running_ufw" comment="Retrieve the ActiveState property of ufw" version="1">
    <linux:unit operation="pattern match">^ufw\.(socket|service)$</linux:unit>
    <linux:property>ActiveState</linux:property>
  </linux:systemdunitproperty_object>
  <linux:systemdunitproperty_state id="state_service_running_ufw" version="1" comment="ufw is running">
      <linux:value>active</linux:value>
  </linux:systemdunitproperty_state>
<ind:variable_test id="service_ufw_enabled_test_variable_var_network_filtering_service"
  comment="Check external var_network_filtering_service is set to ufw" check="all" version="1">
    <ind:object object_ref="obj_service_ufw_enabled_test_variable_var_network_filtering_service"/>
    <ind:state state_ref="ste_service_ufw_enabled_test_variable_var_network_filtering_service" />
  </ind:variable_test>

  <ind:variable_object id="obj_service_ufw_enabled_test_variable_var_network_filtering_service" version="1">
    <ind:var_ref>var_network_filtering_service</ind:var_ref>
  </ind:variable_object>
  <ind:variable_state id="ste_service_ufw_enabled_test_variable_var_network_filtering_service" version="1">
    <ind:value operation="equals" datatype="string">ufw</ind:value>
  </ind:variable_state>

  <external_variable comment="External variable var_network_filtering_service" datatype="string" id="var_network_filtering_service" version="1" />

  <linux:dpkginfo_test check="all" check_existence="all_exist"
  id="test_service_ufw_package_ufw_installed" version="1"
  comment="package ufw is installed">
    <linux:object object_ref="obj_test_service_ufw_package_ufw_installed" />
    
  </linux:dpkginfo_test>
  <linux:dpkginfo_object id="obj_test_service_ufw_package_ufw_installed" version="1">
    <linux:name>ufw</linux:name>
  </linux:dpkginfo_object>
  

</def-group>