mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
Merge branch 'develop' into secure_address_generation
This commit is contained in:
commit
5b77793ba6
26 changed files with 470 additions and 134 deletions
10
cmd/yggdrasil/chuser_other.go
Normal file
10
cmd/yggdrasil/chuser_other.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
//go:build !aix && !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris
|
||||
// +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris
|
||||
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
|
||||
func chuser(user string) error {
|
||||
return errors.New("setting uid/gid is not supported on this platform")
|
||||
}
|
87
cmd/yggdrasil/chuser_unix.go
Normal file
87
cmd/yggdrasil/chuser_unix.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
||||
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
osuser "os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func chuser(user string) error {
|
||||
group := ""
|
||||
if i := strings.IndexByte(user, ':'); i >= 0 {
|
||||
user, group = user[:i], user[i+1:]
|
||||
}
|
||||
|
||||
u := (*osuser.User)(nil)
|
||||
g := (*osuser.Group)(nil)
|
||||
|
||||
if user != "" {
|
||||
if _, err := strconv.ParseUint(user, 10, 32); err == nil {
|
||||
u, err = osuser.LookupId(user)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to lookup user by id %q: %v", user, err)
|
||||
}
|
||||
} else {
|
||||
u, err = osuser.Lookup(user)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to lookup user by name %q: %v", user, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if group != "" {
|
||||
if _, err := strconv.ParseUint(group, 10, 32); err == nil {
|
||||
g, err = osuser.LookupGroupId(group)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to lookup group by id %q: %v", user, err)
|
||||
}
|
||||
} else {
|
||||
g, err = osuser.LookupGroup(group)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to lookup group by name %q: %v", user, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if g != nil {
|
||||
gid, _ := strconv.ParseUint(g.Gid, 10, 32)
|
||||
var err error
|
||||
if gid < math.MaxInt {
|
||||
err = syscall.Setgid(int(gid))
|
||||
} else {
|
||||
err = errors.New("gid too big")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setgid %d: %v", gid, err)
|
||||
}
|
||||
} else if u != nil {
|
||||
gid, _ := strconv.ParseUint(u.Gid, 10, 32)
|
||||
err := syscall.Setgid(int(uint32(gid)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setgid %d: %v", gid, err)
|
||||
}
|
||||
}
|
||||
|
||||
if u != nil {
|
||||
uid, _ := strconv.ParseUint(u.Uid, 10, 32)
|
||||
var err error
|
||||
if uid < math.MaxInt {
|
||||
err = syscall.Setuid(int(uid))
|
||||
} else {
|
||||
err = errors.New("uid too big")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setuid %d: %v", uid, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -53,6 +53,7 @@ func main() {
|
|||
getsnet := flag.Bool("subnet", false, "use in combination with either -useconf or -useconffile, outputs your IPv6 subnet")
|
||||
getpkey := flag.Bool("publickey", false, "use in combination with either -useconf or -useconffile, outputs your public key")
|
||||
loglevel := flag.String("loglevel", "info", "loglevel to enable")
|
||||
chuserto := flag.String("user", "", "user (and, optionally, group) to set UID/GID to")
|
||||
flag.Parse()
|
||||
|
||||
done := make(chan struct{})
|
||||
|
@ -286,6 +287,14 @@ func main() {
|
|||
<-done
|
||||
})
|
||||
|
||||
// Change user if requested
|
||||
if *chuserto != "" {
|
||||
err = chuser(*chuserto)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Block until we are told to shut down.
|
||||
<-ctx.Done()
|
||||
|
||||
|
|
|
@ -38,7 +38,6 @@ func (cmdLineEnv *CmdLineEnv) parseFlagsAndArgs() {
|
|||
fmt.Println("Examples:")
|
||||
fmt.Println(" - ", os.Args[0], "list")
|
||||
fmt.Println(" - ", os.Args[0], "getPeers")
|
||||
fmt.Println(" - ", os.Args[0], "setTunTap name=auto mtu=1500 tap_mode=false")
|
||||
fmt.Println(" - ", os.Args[0], "-endpoint=tcp://localhost:9001 getPeers")
|
||||
fmt.Println(" - ", os.Args[0], "-endpoint=unix:///var/run/ygg.sock getPeers")
|
||||
}
|
||||
|
|
|
@ -174,7 +174,7 @@ func run() int {
|
|||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"URI", "State", "Dir", "IP Address", "Uptime", "RTT", "RX", "TX", "Pr", "Last Error"})
|
||||
table.SetHeader([]string{"URI", "State", "Dir", "IP Address", "Uptime", "RTT", "RX", "TX", "Pr", "Cost", "Last Error"})
|
||||
for _, peer := range resp.Peers {
|
||||
state, lasterr, dir, rtt := "Up", "-", "Out", "-"
|
||||
if !peer.Up {
|
||||
|
@ -200,6 +200,7 @@ func run() int {
|
|||
peer.RXBytes.String(),
|
||||
peer.TXBytes.String(),
|
||||
fmt.Sprintf("%d", peer.Priority),
|
||||
fmt.Sprintf("%d", peer.Cost),
|
||||
lasterr,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue