Compare commits
No commits in common. "master" and "v2.0.1" have entirely different histories.
11 changed files with 19 additions and 128 deletions
2
LICENSE
2
LICENSE
|
|
@ -1,4 +1,4 @@
|
||||||
Copyright (C) 2022 Marius Schellenberger
|
Copyright (C) 2017 Marius Schellenberger
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
# godrop - drop privileges
|
# godrop - drop privileges
|
||||||
|
|
||||||
Godrop is a simple library to drop privileges on Linux and OpenBSD.
|
Godrop is a simple library to drop privileges on linux maschines.
|
||||||
|
|
||||||
See the examples directory on how to use the `Drop` and `MultiDrop` functions.
|
See the examples directory on how to use the `Drop` and `MultiDrop` functions.
|
||||||
|
|
|
||||||
4
go.mod
4
go.mod
|
|
@ -1,5 +1 @@
|
||||||
module git.giftfish.de/ston1th/godrop/v2
|
module git.giftfish.de/ston1th/godrop/v2
|
||||||
|
|
||||||
go 1.19
|
|
||||||
|
|
||||||
require golang.org/x/sys v0.24.0
|
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -1,2 +0,0 @@
|
||||||
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
|
|
||||||
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
||||||
28
godrop.go
28
godrop.go
|
|
@ -1,13 +1,11 @@
|
||||||
// Copyright (C) 2022 Marius Schellenberger
|
// Copyright (C) 2018 Marius Schellenberger
|
||||||
|
|
||||||
//go:build go1.11
|
|
||||||
// +build go1.11
|
// +build go1.11
|
||||||
|
|
||||||
// Package godrop provides a simple library to drop privileges on Linux and OpenBSD.
|
// Package godrop provides a simple privileges dropping library
|
||||||
package godrop
|
package godrop
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -16,6 +14,10 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func errf(i interface{}) error {
|
||||||
|
return fmt.Errorf("godrop: %s", i)
|
||||||
|
}
|
||||||
|
|
||||||
// Config represents the drop config
|
// Config represents the drop config
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// User is the user to drop privileges to.
|
// User is the user to drop privileges to.
|
||||||
|
|
@ -41,21 +43,21 @@ func Drop(c Config, f func() (net.Listener, error)) error {
|
||||||
func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
||||||
uid, err := userID(c.User)
|
uid, err := userID(c.User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("godrop: " + err.Error())
|
return errf(err)
|
||||||
}
|
}
|
||||||
if uid == 0 {
|
if uid == 0 {
|
||||||
return fmt.Errorf("godrop: you can't drop privileges to uid 0 (%s)", c.User)
|
return errf("unable to drop privileges to uid 0 (root)")
|
||||||
}
|
}
|
||||||
gid, err := groupID(c.Group)
|
gid, err := groupID(c.Group)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("godrop: " + err.Error())
|
return errf(err)
|
||||||
}
|
}
|
||||||
switch os.Getuid() {
|
switch os.Getuid() {
|
||||||
case 0:
|
case 0:
|
||||||
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
||||||
ln, err := f()
|
ln, err := f()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("godrop: " + err.Error())
|
return errf(err)
|
||||||
}
|
}
|
||||||
for i, v := range ln {
|
for i, v := range ln {
|
||||||
var f *os.File
|
var f *os.File
|
||||||
|
|
@ -67,10 +69,10 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
||||||
f, err = l.File()
|
f, err = l.File()
|
||||||
l.Close()
|
l.Close()
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("godrop: index %d listener is not type of either *net.TCPListener or *net.UnixListener", i)
|
return errf(fmt.Sprintf("index %d listener is not type of either *net.TCPListener or *net.UnixListener", i))
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("godrop: index %d %s", i, err)
|
return errf(fmt.Sprintf("index %d %s", i, err))
|
||||||
}
|
}
|
||||||
cmd.ExtraFiles = append(cmd.ExtraFiles, f)
|
cmd.ExtraFiles = append(cmd.ExtraFiles, f)
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +93,7 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return errors.New("godrop: " + err.Error())
|
return errf(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Foreground {
|
if c.Foreground {
|
||||||
|
|
@ -118,7 +120,7 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
||||||
case uid:
|
case uid:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("godrop: dropping priviledges failed")
|
return errf("dropping priviledges failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetListener returns the listener socket of file descriptor 3
|
// GetListener returns the listener socket of file descriptor 3
|
||||||
|
|
@ -129,7 +131,7 @@ func GetListener() (net.Listener, error) {
|
||||||
// GetListenerFd returns the listener socket of the given file descriptor
|
// GetListenerFd returns the listener socket of the given file descriptor
|
||||||
func GetListenerFd(fd int) (net.Listener, error) {
|
func GetListenerFd(fd int) (net.Listener, error) {
|
||||||
if fd < 3 {
|
if fd < 3 {
|
||||||
return nil, errors.New("godrop: fd is less than 3")
|
return nil, errf("fd is less than 3")
|
||||||
}
|
}
|
||||||
f := os.NewFile(uintptr(fd), "")
|
f := os.NewFile(uintptr(fd), "")
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
// Copyright (C) 2022 Marius Schellenberger
|
// Copyright (C) 2018 Marius Schellenberger
|
||||||
|
|
||||||
//go:build go1.11
|
|
||||||
// +build go1.11
|
// +build go1.11
|
||||||
|
|
||||||
package godrop
|
package godrop
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (C) 2022 Marius Schellenberger
|
// Copyright (C) 2018 Marius Schellenberger
|
||||||
|
|
||||||
package godrop
|
package godrop
|
||||||
|
|
||||||
|
|
|
||||||
15
pledge.go
15
pledge.go
|
|
@ -1,15 +0,0 @@
|
||||||
// Copyright (C) 2022 Marius Schellenberger
|
|
||||||
|
|
||||||
//go:build !openbsd
|
|
||||||
// +build !openbsd
|
|
||||||
|
|
||||||
package godrop
|
|
||||||
|
|
||||||
// Pledge is currently only supported on OpenBSD.
|
|
||||||
func Pledge(promises, execpromises string) error { return nil }
|
|
||||||
|
|
||||||
// PledgePromises is currently only supported on OpenBSD.
|
|
||||||
func PledgePromises(promises string) error { return nil }
|
|
||||||
|
|
||||||
// PledgeExecPromises is currently only supported on OpenBSD.
|
|
||||||
func PledgeExecpromises(execpromises string) error { return nil }
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
// Copyright (C) 2022 Marius Schellenberger
|
|
||||||
|
|
||||||
//go:build openbsd
|
|
||||||
// +build openbsd
|
|
||||||
|
|
||||||
package godrop
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Pledge is a wrapper for x/sys/unix Pledge.
|
|
||||||
//
|
|
||||||
// See https://go.googlesource.com/sys/+/master/unix/openbsd_pledge.go for usage.
|
|
||||||
func Pledge(promises, execpromises string) (err error) {
|
|
||||||
err = unix.Pledge(promises, execpromises)
|
|
||||||
if err != nil {
|
|
||||||
err = errors.New("pledge: " + err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// PledgePromises is a wrapper for x/sys/unix PledgePromises.
|
|
||||||
//
|
|
||||||
// See https://go.googlesource.com/sys/+/master/unix/openbsd_pledge.go for usage.
|
|
||||||
func PledgePromises(promises string) (err error) {
|
|
||||||
err = unix.PledgePromises(promises)
|
|
||||||
if err != nil {
|
|
||||||
err = errors.New("pledge: " + err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// PledgeExecpromises is a wrapper for x/sys/unix PledgeExecpromises.
|
|
||||||
//
|
|
||||||
// See https://go.googlesource.com/sys/+/master/unix/openbsd_pledge.go for usage.
|
|
||||||
func PledgeExecpromises(execpromises string) (err error) {
|
|
||||||
err = unix.PledgeExecpromises(execpromises)
|
|
||||||
if err != nil {
|
|
||||||
err = errors.New("pledge: " + err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
12
unveil.go
12
unveil.go
|
|
@ -1,12 +0,0 @@
|
||||||
// Copyright (C) 2022 Marius Schellenberger
|
|
||||||
|
|
||||||
//go:build !openbsd
|
|
||||||
// +build !openbsd
|
|
||||||
|
|
||||||
package godrop
|
|
||||||
|
|
||||||
// Unveil is currently only supported on OpenBSD.
|
|
||||||
func Unveil(path, flags string) error { return nil }
|
|
||||||
|
|
||||||
// UnveilBlock is currently only supported on OpenBSD.
|
|
||||||
func UnveilBlock() error { return nil }
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
// Copyright (C) 2022 Marius Schellenberger
|
|
||||||
|
|
||||||
//go:build openbsd
|
|
||||||
// +build openbsd
|
|
||||||
|
|
||||||
package godrop
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Unveil is a wrapper for x/sys/unix Unveil.
|
|
||||||
//
|
|
||||||
// See https://go.googlesource.com/sys/+/master/unix/openbsd_unveil.go for usage.
|
|
||||||
func Unveil(path, flags string) (err error) {
|
|
||||||
err = unix.Unveil(path, flags)
|
|
||||||
if err != nil {
|
|
||||||
err = errors.New("unveil: " + err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnveilBlock is a wrapper for x/sys/unix UnveilBlock.
|
|
||||||
//
|
|
||||||
// See https://go.googlesource.com/sys/+/master/unix/openbsd_unveil.go for usage.
|
|
||||||
func UnveilBlock() (err error) {
|
|
||||||
err = unix.UnveilBlock()
|
|
||||||
if err != nil {
|
|
||||||
err = errors.New("unveil: " + err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue