added pledge and unveil support
This commit is contained in:
parent
23731a3702
commit
089d8d16c8
8 changed files with 116 additions and 15 deletions
25
godrop.go
25
godrop.go
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
// +build go1.11
|
||||
|
||||
// Package godrop provides a simple privileges dropping library
|
||||
// Package godrop provides a simple library to drop privileges on Linux and OpenBSD.
|
||||
package godrop
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
|
@ -14,10 +15,6 @@ import (
|
|||
"syscall"
|
||||
)
|
||||
|
||||
func errf(i interface{}) error {
|
||||
return fmt.Errorf("godrop: %s", i)
|
||||
}
|
||||
|
||||
// Config represents the drop config
|
||||
type Config struct {
|
||||
// User is the user to drop privileges to.
|
||||
|
|
@ -43,21 +40,21 @@ func Drop(c Config, f func() (net.Listener, error)) error {
|
|||
func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
||||
uid, err := userID(c.User)
|
||||
if err != nil {
|
||||
return errf(err)
|
||||
return errors.New("godrop: " + err.Error())
|
||||
}
|
||||
if uid == 0 {
|
||||
return errf("unable to drop privileges to uid 0 (root)")
|
||||
return fmt.Errorf("godrop: you can't drop privileges to uid 0 (%s)", c.User)
|
||||
}
|
||||
gid, err := groupID(c.Group)
|
||||
if err != nil {
|
||||
return errf(err)
|
||||
return errors.New("godrop: " + err.Error())
|
||||
}
|
||||
switch os.Getuid() {
|
||||
case 0:
|
||||
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
||||
ln, err := f()
|
||||
if err != nil {
|
||||
return errf(err)
|
||||
return errors.New("godrop: " + err.Error())
|
||||
}
|
||||
for i, v := range ln {
|
||||
var f *os.File
|
||||
|
|
@ -69,10 +66,10 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
|||
f, err = l.File()
|
||||
l.Close()
|
||||
default:
|
||||
return errf(fmt.Sprintf("index %d listener is not type of either *net.TCPListener or *net.UnixListener", i))
|
||||
return fmt.Errorf("godrop: index %d listener is not type of either *net.TCPListener or *net.UnixListener", i)
|
||||
}
|
||||
if err != nil {
|
||||
return errf(fmt.Sprintf("index %d %s", i, err))
|
||||
return fmt.Errorf("godrop: index %d %s", i, err)
|
||||
}
|
||||
cmd.ExtraFiles = append(cmd.ExtraFiles, f)
|
||||
}
|
||||
|
|
@ -93,7 +90,7 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
|||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return errf(err)
|
||||
return errors.New("godrop: " + err.Error())
|
||||
}
|
||||
|
||||
if c.Foreground {
|
||||
|
|
@ -120,7 +117,7 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
|||
case uid:
|
||||
return nil
|
||||
}
|
||||
return errf("dropping priviledges failed")
|
||||
return errors.New("godrop: dropping priviledges failed")
|
||||
}
|
||||
|
||||
// GetListener returns the listener socket of file descriptor 3
|
||||
|
|
@ -131,7 +128,7 @@ func GetListener() (net.Listener, error) {
|
|||
// GetListenerFd returns the listener socket of the given file descriptor
|
||||
func GetListenerFd(fd int) (net.Listener, error) {
|
||||
if fd < 3 {
|
||||
return nil, errf("fd is less than 3")
|
||||
return nil, errors.New("godrop: fd is less than 3")
|
||||
}
|
||||
f := os.NewFile(uintptr(fd), "")
|
||||
defer f.Close()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue