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
|
Context.simulation = true
|
||||||
end
|
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
|
parser.on "-a", "--print-autodetect", "Print autodetection of the installed programs." do
|
||||||
Context.print_autodetect = true
|
Context.print_autodetect = true
|
||||||
end
|
end
|
||||||
@ -55,6 +59,8 @@ OptionParser.parse! do |parser|
|
|||||||
when /^(up)/
|
when /^(up)/
|
||||||
when /^(down)/
|
when /^(down)/
|
||||||
when /^(scan)/
|
when /^(scan)/
|
||||||
|
when /^(store-keys)/
|
||||||
|
when /^(context)/
|
||||||
else
|
else
|
||||||
STDERR.puts "Command #{Context.command} not understood"
|
STDERR.puts "Command #{Context.command} not understood"
|
||||||
exit 1
|
exit 1
|
||||||
@ -164,6 +170,22 @@ begin
|
|||||||
network_configuration = NetworkConfigurationParser.parse_file(f)
|
network_configuration = NetworkConfigurationParser.parse_file(f)
|
||||||
network_configuration.scan
|
network_configuration.scan
|
||||||
end
|
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
|
end
|
||||||
rescue e
|
rescue e
|
||||||
STDERR.puts "#{CRED}Exception: #{CRESET}#{e}"
|
STDERR.puts "#{CRED}Exception: #{CRESET}#{e}"
|
||||||
|
@ -89,8 +89,7 @@ class WirelessAPSetup
|
|||||||
end
|
end
|
||||||
|
|
||||||
def store_access_point_keys
|
def store_access_point_keys
|
||||||
puts "TODO: store_access_point_keys"
|
NetworkCommands.store_access_point_keys ssid, security
|
||||||
puts "security for #{ssid} = #{security}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
@ -232,8 +231,7 @@ class InterfaceConfiguration
|
|||||||
end
|
end
|
||||||
|
|
||||||
wireless_networks.each do |ssid, wireless_configuration|
|
wireless_networks.each do |ssid, wireless_configuration|
|
||||||
# k = ssid
|
puts "#{CGREEN}storing access point key for #{ssid}#{CRESET}"
|
||||||
puts "#{CGREEN}configuring #{ssid}#{CRESET}"
|
|
||||||
wireless_configuration.store_access_point_keys
|
wireless_configuration.store_access_point_keys
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -11,7 +11,6 @@ class Context
|
|||||||
class_property prefered_wireless_configuration_program : String? = nil
|
class_property prefered_wireless_configuration_program : String? = nil
|
||||||
class_property prefered_dhcp_client : String? = nil
|
class_property prefered_dhcp_client : String? = nil
|
||||||
|
|
||||||
class_property root = "/"
|
|
||||||
class_property print_autodetect = false
|
class_property print_autodetect = false
|
||||||
|
|
||||||
class_property command = "list"
|
class_property command = "list"
|
||||||
|
@ -71,24 +71,28 @@ class NetworkCommands
|
|||||||
end
|
end
|
||||||
|
|
||||||
class WPASupplicant
|
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
|
# store the AP passphrase in the wpa_supplicant way
|
||||||
def self.passphrase(ssid : String, passphrase : String)
|
def self.passphrase(ssid : String, passphrase : String)
|
||||||
puts "TODO: storing wpa_supplicant passphrase #{passphrase} for ssid #{ssid}"
|
dirpath = "#{Context.root}/#{Context.keydir}"
|
||||||
File.open("#{Context.root}/#{Context.keydir}/#{ssid}.conf") do |file|
|
|
||||||
Do.run("wpa_passphrase", [ ssid, passphrase ]) do |content|
|
Dir.mkdir_p(dirpath) unless Dir.exists?(dirpath)
|
||||||
file.puts content
|
|
||||||
|
# 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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.connection(ssid : String, ifname : String)
|
def self.connection(ssid : String, ifname : String)
|
||||||
|
dirpath = "#{Context.root}/#{Context.keydir}"
|
||||||
unless Do.run("wpa_supplicant",
|
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}"
|
raise "(wpa_supplicant) cannot connect to the wireless AP #{ssid} via the interface #{ifname}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -325,4 +329,9 @@ class NetworkCommands
|
|||||||
cmd.list_ssid ifname
|
cmd.list_ssid ifname
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user