bsdcontrol.rb/lib/bsd/control/feature.rb

188 lines
3.7 KiB
Ruby
Raw Normal View History

2024-05-12 05:59:38 +02:00
# frozen_string_literal: true
2024-03-01 02:29:43 +01:00
module BSD::Control
class Feature < Struct.new(:name, :context)
2024-03-01 02:29:43 +01:00
##
# @return [Array<BSD::Control::Feature>]
2024-05-13 01:01:03 +02:00
# Returns an array of available features
2024-03-01 02:29:43 +01:00
def self.available
BSD::Control.available_features
2024-03-01 02:29:43 +01:00
end
2024-03-01 03:46:08 +01:00
##
# @group Actions
2024-03-01 02:29:43 +01:00
##
2024-05-13 01:01:03 +02:00
# Enables a feature for a given file
2024-03-01 02:29:43 +01:00
#
# @param [String] path
2024-05-13 01:01:03 +02:00
# The path to a file
#
2024-03-09 23:40:42 +01:00
# @raise [SystemCallError]
2024-05-13 01:01:03 +02:00
# Might raise a number of Errno exceptions
2024-03-01 02:29:43 +01:00
#
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true on success
2024-03-01 02:29:43 +01:00
def enable!(path)
set!(path, ENABLED)
2024-03-01 02:29:43 +01:00
end
##
2024-05-13 01:01:03 +02:00
# Disables a feature for a given file
2024-03-01 02:29:43 +01:00
#
# @param [String] path
2024-05-13 01:01:03 +02:00
# The path to a file
#
2024-03-09 23:40:42 +01:00
# @raise [SystemCallError]
2024-05-13 01:01:03 +02:00
# Might raise a number of Errno exceptions
2024-03-01 02:29:43 +01:00
#
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true on success
2024-03-01 02:29:43 +01:00
def disable!(path)
set!(path, DISABLED)
2024-03-01 02:29:43 +01:00
end
2024-03-07 00:59:54 +01:00
##
2024-03-20 22:34:15 +01:00
# @!method sysdef!(path)
2024-05-13 01:01:03 +02:00
# Restores the system default for a given file
2024-03-07 00:59:54 +01:00
#
2024-03-20 22:34:15 +01:00
# @param [String] path
2024-05-13 01:01:03 +02:00
# The path to a file
2024-03-07 00:59:54 +01:00
#
2024-03-20 22:34:15 +01:00
# @raise [SystemCallError]
2024-05-13 01:01:03 +02:00
# Might raise a number of Errno exceptions
2024-03-07 00:59:54 +01:00
#
2024-03-20 22:34:15 +01:00
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true on success
2024-03-07 00:59:54 +01:00
2024-03-08 06:21:24 +01:00
# @endgroup
2024-03-08 03:41:45 +01:00
##
2024-03-08 06:21:24 +01:00
# @group Queries
##
# @param [String] path
2024-05-13 01:01:03 +02:00
# The path to a file
2024-03-08 06:21:24 +01:00
#
2024-03-08 03:41:45 +01:00
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true when a feature is enabled
2024-03-08 03:41:45 +01:00
def enabled?(path)
status(path) == :enabled
end
##
2024-03-08 06:21:24 +01:00
# @param [String] path
# The path to a file.
#
2024-03-08 03:41:45 +01:00
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true when a feature is disabled
2024-03-08 03:41:45 +01:00
def disabled?(path)
status(path) == :disabled
end
##
2024-03-08 06:21:24 +01:00
# @param [String] path
2024-05-13 01:01:03 +02:00
# The path to a file
2024-03-08 06:21:24 +01:00
#
2024-03-08 03:41:45 +01:00
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true when the system default setting is used
2024-03-08 03:41:45 +01:00
def sysdef?(path)
status(path) == :sysdef
end
##
2024-03-08 06:21:24 +01:00
# @param [String] path
2024-05-13 01:01:03 +02:00
# The path to a file
2024-03-08 06:21:24 +01:00
#
2024-03-08 03:41:45 +01:00
# @return [Boolean]
2024-03-20 23:10:35 +01:00
# Returns true when a feature is in an invalid state
2024-05-13 01:01:03 +02:00
# (eg: the feature is both enabled and disabled at the same time)
2024-03-20 23:10:35 +01:00
def invalid?(path)
status(path) == :invalid
2024-03-08 03:41:45 +01:00
end
##
# @!method status(path)
# @param [String] path
2024-05-13 01:01:03 +02:00
# The path to a file
2024-03-08 06:21:24 +01:00
#
# @raise [SystemCallError]
2024-05-13 01:01:03 +02:00
# Might raise a number of Errno exceptions
2024-03-08 06:21:24 +01:00
#
# @return [Symbol]
# Returns the status of a feature for a given file.
# Status could be: `:unknown`, `:enabled`, `:disabled`,
# `:sysdef`, or `:invalid`.
2024-03-08 03:41:45 +01:00
2024-03-01 03:46:08 +01:00
# @endgroup
2024-03-01 02:29:43 +01:00
##
# @group Predicates
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `pageexec`
2024-03-01 02:29:43 +01:00
def pageexec?
name == "pageexec"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `mprotect`
2024-03-01 02:29:43 +01:00
def mprotect?
name == "mprotect"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `segvguard`
2024-03-01 02:29:43 +01:00
def segvguard?
name == "segvguard"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `aslr`
2024-03-01 02:29:43 +01:00
def aslr?
name == "aslr"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `shlibrandom`
2024-03-01 02:29:43 +01:00
def shlibrandom?
name == "shlibrandom"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `disallow_map32bit`
2024-03-01 02:29:43 +01:00
def disallow_map32bit?
name == "disallow_map32bit"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `insecure_kmod`
2024-03-01 02:29:43 +01:00
def insecure_kmod?
name == "insecure_kmod"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `harden_shm`
2024-03-01 02:29:43 +01:00
def harden_shm?
name == "harden_shm"
end
##
# @return [Boolean]
2024-05-13 01:01:03 +02:00
# Returns true for `prohibit_ptrace_capsicum`
2024-03-01 02:29:43 +01:00
def prohibit_ptrace_capsicum?
name == "prohibit_ptrace_capsicum"
end
# @endgroup
end
end