From e101913e5d2483a267259e90b2b8a49b62c36556 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Wed, 23 Oct 2019 00:25:54 +0200
Subject: [PATCH] storing wireless access point keys via wpa_password (WPA2
keys)
---
src/cli.cr | 22 ++++++++++++++++++++++
src/configuration.cr | 6 ++----
src/context.cr | 1 -
src/network_commands.cr | 29 +++++++++++++++++++----------
4 files changed, 43 insertions(+), 15 deletions(-)
diff --git a/src/cli.cr b/src/cli.cr
index d4d07b5..a758cc3 100644
--- a/src/cli.cr
+++ b/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}"
diff --git a/src/configuration.cr b/src/configuration.cr
index 50a9ab6..7838309 100644
--- a/src/configuration.cr
+++ b/src/configuration.cr
@@ -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
diff --git a/src/context.cr b/src/context.cr
index 2facf26..503e420 100644
--- a/src/context.cr
+++ b/src/context.cr
@@ -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"
diff --git a/src/network_commands.cr b/src/network_commands.cr
index 00685d8..278d8e0 100644
--- a/src/network_commands.cr
+++ b/src/network_commands.cr
@@ -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