OpenBSD operating system is now autodetected
This commit is contained in:
parent
1c806d4874
commit
ee118a497f
@ -1,5 +1,15 @@
|
|||||||
|
|
||||||
class Autodetect
|
class Autodetect
|
||||||
|
def self.uname
|
||||||
|
os = nil : String?
|
||||||
|
Do.run("uname") do |p|
|
||||||
|
p.output.each_line do |line|
|
||||||
|
os = line
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
os
|
||||||
|
end
|
||||||
class_property print_autodetect : Bool = false
|
class_property print_autodetect : Bool = false
|
||||||
|
|
||||||
def self.which(cmd : String)
|
def self.which(cmd : String)
|
||||||
|
22
src/cli.cr
22
src/cli.cr
@ -105,22 +105,32 @@ possible_wireless_configuration_cmds = {
|
|||||||
"ifconfig" => NetworkCommands::IfconfigCommand
|
"ifconfig" => NetworkCommands::IfconfigCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# first, check if we are on OpenBSD
|
||||||
|
Context.os = Autodetect.uname
|
||||||
|
|
||||||
|
case Context.os
|
||||||
|
when "Linux"
|
||||||
key = Context.prefered_network_configuration_program
|
key = Context.prefered_network_configuration_program
|
||||||
key = possible_network_configuration_cmds.keys.find { |key| Autodetect.which(key) } if key.nil?
|
key = possible_network_configuration_cmds.keys.find { |key| Autodetect.which(key) } if key.nil?
|
||||||
# should crash if there is no network command installed
|
# should crash if there is no network command installed
|
||||||
NetworkCommands.cmd_network_configuration = possible_network_configuration_cmds[key.not_nil!]
|
NetworkCommands.cmd_network_configuration = possible_network_configuration_cmds[key.not_nil!]
|
||||||
|
|
||||||
key = Context.prefered_dhcp_client
|
|
||||||
key = possible_dhcp_clients.keys.find { |key| Autodetect.which(key) } if key.nil?
|
|
||||||
# should not crash if there is no
|
|
||||||
NetworkCommands.cmd_dhcp_client = possible_dhcp_clients[key] unless key.nil?
|
|
||||||
|
|
||||||
key = Context.prefered_wireless_configuration_program
|
key = Context.prefered_wireless_configuration_program
|
||||||
key = possible_wireless_configuration_cmds.keys.find { |key| Autodetect.which(key) } if key.nil?
|
key = possible_wireless_configuration_cmds.keys.find { |key| Autodetect.which(key) } if key.nil?
|
||||||
# should crash if there is no wireless command installed
|
# should crash if there is no wireless command installed
|
||||||
NetworkCommands.cmd_wireless_configuration = possible_wireless_configuration_cmds[key.not_nil!]
|
NetworkCommands.cmd_wireless_configuration = possible_wireless_configuration_cmds[key.not_nil!]
|
||||||
|
|
||||||
|
key = Context.prefered_dhcp_client
|
||||||
|
key = possible_dhcp_clients.keys.find { |key| Autodetect.which(key) } if key.nil?
|
||||||
|
# should not crash if there is no dhcp client on the system
|
||||||
|
NetworkCommands.cmd_dhcp_client = possible_dhcp_clients[key] unless key.nil?
|
||||||
|
NetworkCommands.cmd_sysctl = NetworkCommands::SysctlCommand
|
||||||
|
when "OpenBSD"
|
||||||
|
NetworkCommands.cmd_network_configuration = NetworkCommands::OpenBSDIfconfigCommand
|
||||||
|
NetworkCommands.cmd_wireless_configuration = NetworkCommands::OpenBSDIfconfigCommand
|
||||||
|
NetworkCommands.cmd_dhcp_client = NetworkCommands::OpenBSDDHClientCommand
|
||||||
|
NetworkCommands.cmd_sysctl = NetworkCommands::OpenBSDSysctlCommand
|
||||||
|
end
|
||||||
|
|
||||||
files = Array(String).new
|
files = Array(String).new
|
||||||
Dir.children("#{Context.root}/etc/").each do |f|
|
Dir.children("#{Context.root}/etc/").each do |f|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
class Context
|
class Context
|
||||||
class_property root : String = "/"
|
class_property root : String = "/"
|
||||||
|
|
||||||
|
class_property os : String?
|
||||||
|
|
||||||
class_property keydir : String = "/etc/network/keydir"
|
class_property keydir : String = "/etc/network/keydir"
|
||||||
|
|
||||||
class_property simulation = false
|
class_property simulation = false
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
class Do < Process
|
class Do < Process
|
||||||
class_property simulation = false
|
class_property simulation = false
|
||||||
|
|
||||||
def self.run(cmd : String, params : Array(String) = nil)
|
def self.run(cmd : String, params = [] of String)
|
||||||
if @@simulation
|
if @@simulation
|
||||||
puts "simulation, do: #{cmd} #{params.join(" ")}"
|
puts "simulation, do: #{cmd} #{params.join(" ")}"
|
||||||
Process::Status.new 0
|
Process::Status.new 0
|
||||||
@ -11,7 +11,7 @@ class Do < Process
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.run(cmd : String, params : Array(String) = nil, &block : Process -> _)
|
def self.run(cmd : String, params = [] of String, &block : Process -> _)
|
||||||
if @@simulation
|
if @@simulation
|
||||||
puts "simulation, do: #{cmd} #{params.join(" ")}"
|
puts "simulation, do: #{cmd} #{params.join(" ")}"
|
||||||
Process::Status.new 0
|
Process::Status.new 0
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
|
|
||||||
# TODO: OpenBSD: no '-w' parameter for sysctl
|
# TODO: OpenBSD: no '-w' parameter for sysctl
|
||||||
# TODO: OpenBSD: ifconfig scan
|
# TODO: OpenBSD: test scanning for wireless AP with ifconfig
|
||||||
# TODO: Linux: description with ip-* command family
|
# TODO: Linux: description with ip-* command family
|
||||||
|
|
||||||
class NetworkCommands
|
class NetworkCommands
|
||||||
class_property cmd_network_configuration : IfconfigCommand.class | IPCommand.class = IfconfigCommand
|
class_property cmd_network_configuration : IfconfigCommand.class | IPCommand.class = IfconfigCommand
|
||||||
class_property cmd_wireless_configuration : IfconfigCommand.class | IWCommand.class | NotSetup.class = NotSetup
|
class_property cmd_wireless_configuration : IfconfigCommand.class | IWCommand.class | NotSetup.class = NotSetup
|
||||||
class_property cmd_dhcp_client : UDHCPCCommand.class | DHClientCommand.class | NotSetup.class = NotSetup
|
class_property cmd_dhcp_client : UDHCPCCommand.class | DHClientCommand.class | NotSetup.class = NotSetup
|
||||||
|
class_property cmd_sysctl : SysctlCommand.class | OpenBSDSysctlCommand.class | NotSetup.class = NotSetup
|
||||||
|
|
||||||
class DNS
|
class DNS
|
||||||
property addresses : Array(String)
|
property addresses : Array(String)
|
||||||
@ -136,6 +137,12 @@ class NetworkCommands
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class OpenBSDDHClientCommand < DHClientCommand
|
||||||
|
def self.dhcp6(ifname : String)
|
||||||
|
puts "TODO: dhcp6 on OpenBSD"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class IfconfigCommand
|
class IfconfigCommand
|
||||||
def self.interface_exists(name : String)
|
def self.interface_exists(name : String)
|
||||||
Do.run("ifconfig", [ name ]).success?
|
Do.run("ifconfig", [ name ]).success?
|
||||||
@ -193,7 +200,36 @@ class NetworkCommands
|
|||||||
|
|
||||||
# ifconfig also performs wireless configuration on some OSs
|
# ifconfig also performs wireless configuration on some OSs
|
||||||
def self.scan(name : String)
|
def self.scan(name : String)
|
||||||
puts "TODO: (ifconfig) ifconfig ifname scan | grep SSID"
|
puts "TODO: (ifconfig) cannot get SSID with ifconfig on Linux"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class OpenBSDIfconfigCommand < IfconfigCommand
|
||||||
|
# get the available SSID
|
||||||
|
def self.scan(ifname : String) : Array(String)
|
||||||
|
ssids = Array(String).new
|
||||||
|
|
||||||
|
Do.run("ifconfig", [ ifname, "scan" ]) do |p|
|
||||||
|
p.output.each_line do |line|
|
||||||
|
ssid = /nwid (?<ssid>[a-zA-Z0-9_-]+)/.match(line).try &.["ssid"]
|
||||||
|
|
||||||
|
unless ssid.nil?
|
||||||
|
ssids << ssid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if ssids.empty?
|
||||||
|
raise "(openbsd ifconfig) cannot get ssid list from #{ifname}"
|
||||||
|
end
|
||||||
|
|
||||||
|
ssids
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.autoconfiguration(ifname : String)
|
||||||
|
unless Do.run("ifconfig", [ ifname, "inet6", "autoconf" ]).success?
|
||||||
|
raise "(openbsd ifconfig) Cannot set IPv6 autoconfiguration on #{ifname}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -246,9 +282,24 @@ class NetworkCommands
|
|||||||
|
|
||||||
def self.description(name : String, description : String)
|
def self.description(name : String, description : String)
|
||||||
puts "TODO: (ip) setup description '#{description}' to interface #{name}"
|
puts "TODO: (ip) setup description '#{description}' to interface #{name}"
|
||||||
# unless Do.run("ip", [ "link", "set", "description", description, "dev", name ]).success?
|
end
|
||||||
# raise "(ip) Cannot set description #{description} to #{name}"
|
end
|
||||||
# end
|
|
||||||
|
class SysctlCommand
|
||||||
|
def self.autoconfiguration(ifname : String)
|
||||||
|
unless Do.run("sysctl", [ "-w", "net.ipv6.conf.#{ifname}.autoconf=1" ]).success?
|
||||||
|
raise "(sysctl) cannot set 'net.ipv6.conf.#{ifname}.autoconf=1'"
|
||||||
|
end
|
||||||
|
|
||||||
|
unless Do.run("sysctl", [ "-w", "net.ipv6.conf.#{ifname}.accept_ra=1" ]).success?
|
||||||
|
raise "(sysctl) cannot set 'net.ipv6.conf.#{ifname}.accept_ra=1'"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class OpenBSDSysctlCommand
|
||||||
|
def self.autoconfiguration(ifname : String)
|
||||||
|
OpenBSDIfconfigCommand.autoconfiguration ifname
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -309,13 +360,13 @@ class NetworkCommands
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.autoconfiguration(ifname : String)
|
def self.autoconfiguration(ifname : String)
|
||||||
# TODO: no '-w' option on openbsd
|
cmd = @@cmd_sysctl
|
||||||
unless Do.run("sysctl", [ "-w", "net.ipv6.conf.#{ifname}.autoconf=1" ]).success?
|
|
||||||
raise "(sysctl) cannot set 'net.ipv6.conf.#{ifname}.autoconf=1'"
|
|
||||||
end
|
|
||||||
|
|
||||||
unless Do.run("sysctl", [ "-w", "net.ipv6.conf.#{ifname}.accept_ra=1" ]).success?
|
case cmd
|
||||||
raise "(sysctl) cannot set 'net.ipv6.conf.#{ifname}.accept_ra=1'"
|
when NotSetup.class
|
||||||
|
raise "cannot autoconfigure IPv6"
|
||||||
|
else
|
||||||
|
cmd.autoconfiguration ifname
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user