mirror of
https://git.baguette.netlib.re/Baguette/networkctl
synced 2024-12-18 13:03:30 +00:00
storing wireless access point keys via wpa_password (WPA2 keys)
This commit is contained in:
parent
702ca7af63
commit
e101913e5d
22
src/cli.cr
22
src/cli.cr
@ -6,6 +6,10 @@ OptionParser.parse! do |parser|
|
||||
Context.simulation = true
|
||||
end
|
||||
|
||||
parser.on "-k keydir", "--keydir keydir", "Key storing directory for wireless access point connection." do |keydir|
|
||||
Context.keydir = keydir
|
||||
end
|
||||
|
||||
parser.on "-a", "--print-autodetect", "Print autodetection of the installed programs." do
|
||||
Context.print_autodetect = true
|
||||
end
|
||||
@ -55,6 +59,8 @@ OptionParser.parse! do |parser|
|
||||
when /^(up)/
|
||||
when /^(down)/
|
||||
when /^(scan)/
|
||||
when /^(store-keys)/
|
||||
when /^(context)/
|
||||
else
|
||||
STDERR.puts "Command #{Context.command} not understood"
|
||||
exit 1
|
||||
@ -164,6 +170,22 @@ begin
|
||||
network_configuration = NetworkConfigurationParser.parse_file(f)
|
||||
network_configuration.scan
|
||||
end
|
||||
when "store-keys"
|
||||
interface_files.each do |f|
|
||||
network_configuration = NetworkConfigurationParser.parse_file(f)
|
||||
network_configuration.store_access_point_keys
|
||||
end
|
||||
when "context"
|
||||
pp! Context.root
|
||||
pp! Context.keydir
|
||||
pp! Context.simulation
|
||||
pp! Context.verbosity
|
||||
pp! Context.prefered_network_configuration_program
|
||||
pp! Context.prefered_wireless_configuration_program
|
||||
pp! Context.prefered_dhcp_client
|
||||
pp! Context.print_autodetect
|
||||
pp! Context.command
|
||||
pp! Context.args
|
||||
end
|
||||
rescue e
|
||||
STDERR.puts "#{CRED}Exception: #{CRESET}#{e}"
|
||||
|
@ -89,8 +89,7 @@ class WirelessAPSetup
|
||||
end
|
||||
|
||||
def store_access_point_keys
|
||||
puts "TODO: store_access_point_keys"
|
||||
puts "security for #{ssid} = #{security}"
|
||||
NetworkCommands.store_access_point_keys ssid, security
|
||||
end
|
||||
|
||||
def execute
|
||||
@ -232,8 +231,7 @@ class InterfaceConfiguration
|
||||
end
|
||||
|
||||
wireless_networks.each do |ssid, wireless_configuration|
|
||||
# k = ssid
|
||||
puts "#{CGREEN}configuring #{ssid}#{CRESET}"
|
||||
puts "#{CGREEN}storing access point key for #{ssid}#{CRESET}"
|
||||
wireless_configuration.store_access_point_keys
|
||||
end
|
||||
end
|
||||
|
@ -11,7 +11,6 @@ class Context
|
||||
class_property prefered_wireless_configuration_program : String? = nil
|
||||
class_property prefered_dhcp_client : String? = nil
|
||||
|
||||
class_property root = "/"
|
||||
class_property print_autodetect = false
|
||||
|
||||
class_property command = "list"
|
||||
|
@ -71,24 +71,28 @@ class NetworkCommands
|
||||
end
|
||||
|
||||
class WPASupplicant
|
||||
# verify if the passphrase already is stored for wpa_supplicant
|
||||
def self.passphrase?(ssid : String)
|
||||
puts "TODO: is the wpa_supplicant passphrase for ssid #{ssid} stored?"
|
||||
end
|
||||
|
||||
# store the AP passphrase in the wpa_supplicant way
|
||||
def self.passphrase(ssid : String, passphrase : String)
|
||||
puts "TODO: storing wpa_supplicant passphrase #{passphrase} for ssid #{ssid}"
|
||||
File.open("#{Context.root}/#{Context.keydir}/#{ssid}.conf") do |file|
|
||||
Do.run("wpa_passphrase", [ ssid, passphrase ]) do |content|
|
||||
file.puts content
|
||||
dirpath = "#{Context.root}/#{Context.keydir}"
|
||||
|
||||
Dir.mkdir_p(dirpath) unless Dir.exists?(dirpath)
|
||||
|
||||
# verify if the passphrase already is stored for wpa_supplicant
|
||||
return if File.exists?("#{dirpath}/#{ssid}.conf")
|
||||
|
||||
File.open("#{dirpath}/#{ssid}.conf", "w+") do |file|
|
||||
Do.run("wpa_passphrase", [ ssid, "#{passphrase}" ]) do |p|
|
||||
p.output.each_line do |line|
|
||||
file.puts line
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.connection(ssid : String, ifname : String)
|
||||
dirpath = "#{Context.root}/#{Context.keydir}"
|
||||
unless Do.run("wpa_supplicant",
|
||||
[ "-B", "-c", "#{Context.root}/#{Context.keydir}/#{ssid}.conf", "-i", ifname ]).success?
|
||||
[ "-B", "-c", "#{dirpath}/#{ssid}.conf", "-i", ifname ]).success?
|
||||
raise "(wpa_supplicant) cannot connect to the wireless AP #{ssid} via the interface #{ifname}"
|
||||
end
|
||||
end
|
||||
@ -325,4 +329,9 @@ class NetworkCommands
|
||||
cmd.list_ssid ifname
|
||||
end
|
||||
end
|
||||
|
||||
def self.store_access_point_keys(ssid : String, security : WirelessAPSetup::WPA)
|
||||
# TODO: only one way to do it
|
||||
NetworkCommands::WPASupplicant.passphrase ssid, security.key
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user