Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wifi tooling #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ CONFDIR ?= "$(PREFIX)/etc/vinyl/network.d"
BINARIES := $(BINDIR)/linux-utils
SCRIPTS := $(BINDIR)/useradd \
$(BINDIR)/groupadd \
$(BINDIR)/netctl
$(BINDIR)/netctl \
$(BINDIR)/wifi

CONFIGS := $(CONFDIR)/eth0.toml.sample

Expand Down
2 changes: 2 additions & 0 deletions bin/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
groupName string
netctlDir string
verbose bool
autoConnect bool
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -97,4 +98,5 @@ func reset() {
groupName = ""
netctlDir = netctl.DefaultPath
verbose = false
autoConnect = false
}
67 changes: 67 additions & 0 deletions bin/cmd/wifi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// +build linux

/*
Copyright © 2021 James Condron <james@zero-internet.org.uk>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/vinyl-linux/linux-utils/wifi"
)

// wifiCmd represents the wifi command
var wifiCmd = &cobra.Command{
Use: "wifi",
Short: "Manage wifi networks",
Long: "Manage wifi networks",
RunE: func(cmd *cobra.Command, args []string) (err error) {
// load profiles, looking for wifi interfaces
w, err := wifi.New()
if err != nil {
return
}

nets, err := w.List()
if err != nil {
return
}

fmt.Println(nets)

return nil
},
}

func init() {
rootCmd.AddCommand(wifiCmd)
}
84 changes: 84 additions & 0 deletions bin/cmd/wifiAdd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// +build linux

/*
Copyright © 2021 James Condron <james@zero-internet.org.uk>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd

import (
"fmt"
"strings"
"syscall"

"github.com/spf13/cobra"
"github.com/vinyl-linux/linux-utils/wifi"
"golang.org/x/crypto/ssh/terminal"
)

// wifiAddCmd represents the wifiAdd command
var wifiAddCmd = &cobra.Command{
Use: "add [ssid]",
Short: "Add/ configure a wifi network",
Long: "Add/ configure a wifi network",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
ssid := args[0]

w, err := wifi.New()
if err != nil {
return
}

fmt.Printf("Network %q password > ", ssid)
password, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println()

if err != nil {
return
}

err = w.Create(ssid, strings.TrimSpace(string(password)))
if err != nil {
return
}

if autoConnect {
err = w.Connect(ssid)
}

return
},
}

func init() {
wifiCmd.AddCommand(wifiAddCmd)

wifiAddCmd.Flags().BoolVarP(&autoConnect, "connect", "c", true, "connect to network after creating")
}
60 changes: 60 additions & 0 deletions bin/cmd/wifiConnect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// +build linux

/*
Copyright © 2021 James Condron <james@zero-internet.org.uk>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd

import (
"github.com/spf13/cobra"
"github.com/vinyl-linux/linux-utils/wifi"
)

// wifiConnectCmd represents the wifiConnect command
var wifiConnectCmd = &cobra.Command{
Use: "connect [ssid]",
Short: "Connect to the specified ssid",
Long: "Connect to the specified ssid",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
ssid := args[0]

w, err := wifi.New()
if err != nil {
return
}

return w.Connect(ssid)
},
}

func init() {
wifiCmd.AddCommand(wifiConnectCmd)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require (
github.com/pelletier/go-toml v1.9.5
github.com/spf13/cobra v1.6.1
github.com/vishvananda/netlink v1.1.0
golang.org/x/crypto v0.6.0
pifke.org/wpasupplicant v0.0.0-20221018205742-4b5b2dde8b55
)

require (
Expand All @@ -24,4 +26,5 @@ require (
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1Y
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190419010253-1f3472d942ba/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down Expand Up @@ -112,6 +114,8 @@ golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -121,3 +125,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
pifke.org/wpasupplicant v0.0.0-20221018205742-4b5b2dde8b55 h1:dK1ABrY+GK1MxioJS5lqC/Z0gEZOU3sfS+znBk2ehfM=
pifke.org/wpasupplicant v0.0.0-20221018205742-4b5b2dde8b55/go.mod h1:PqiVjTFFZG+iqUphtbHFcBaF6L8jnE7ZXl+Z5zIqG2o=
32 changes: 27 additions & 5 deletions netctl/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Profile struct {
Interface string `toml:"interface"`
IPv4 Address `toml:",omitempty"`
IPv6 Address `toml:",omitempty"`
Wifi bool `toml:wifi,omitempty`

// link points to the underlying netlink object
link netlink.Link
Expand All @@ -93,13 +94,34 @@ func (p Profile) Up() (err error) {
return
}

// Loopback devices are special; we can go ahead and set them
// up the same way each time. In fact, the loopback file only needs
// the value of `Interface` to be set
if loopback.Match([]byte(p.Interface)) {
return p.UpLoopback()
switch {
case loopback.Match([]byte(p.Interface)):
// Loopback devices are special; we can go ahead and set them
// up the same way each time. In fact, the loopback file only needs
// the value of `Interface` to be set
err = p.UpLoopback()

case p.Wifi:
// Wifi interfaces get handled via wpa_supplicant
err = p.UpWifi()

default:
// Any interface left-over must be a wired interface
err = p.UpWired()
}

return
}

// UpWifi brings up a wifi network via wpa_supplicant
func (p Profile) UpWifi() (err error) {
err = fmt.Errorf("bringing up wifi networks has not been implemented yet")

return
}

// UpWired brings a wired network connection up
func (p Profile) UpWired() (err error) {
for idx, addr := range []Address{
p.IPv4,
p.IPv6,
Expand Down
6 changes: 6 additions & 0 deletions netctl/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ func TestUp(t *testing.T) {
Interface: "lo",
}

wifi := Profile{
Interface: "test0",
Wifi: true,
}

for _, test := range []struct {
name string
profile Profile
Expand All @@ -163,6 +168,7 @@ func TestUp(t *testing.T) {
{"with dhcp errors", ip4DHCPErr, testNetLinkHandle{}, true},
{"dhcp client errors", ip4DHCPClientErr, testNetLinkHandle{}, true},
{"loopback", loopback, testNetLinkHandle{}, false},
{"wifi fails, not implemented", wifi, testNetLinkHandle{}, true},
} {
t.Run(test.name, func(t *testing.T) {
handle = test.handle
Expand Down
Loading