mirror of
https://github.com/zrepl/zrepl.git
synced 2024-12-22 15:11:16 +01:00
cmd: extract listening on a UNIX socket in a private directory into a helper func
This commit is contained in:
parent
1a62d635a6
commit
aea62a9d85
@ -8,7 +8,6 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type StdinserverListenerFactory struct {
|
||||
@ -47,20 +46,7 @@ func stdinserverListenerSocket(sockdir, clientIdentity string) (addr *net.UnixAd
|
||||
|
||||
func (f *StdinserverListenerFactory) Listen() (al AuthenticatedChannelListener, err error) {
|
||||
|
||||
sockdir := filepath.Dir(f.sockaddr.Name)
|
||||
sdstat, err := os.Stat(sockdir)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot stat(2) sockdir '%s'", sockdir)
|
||||
}
|
||||
if !sdstat.IsDir() {
|
||||
return nil, errors.Errorf("sockdir is not a directory: %s", sockdir)
|
||||
}
|
||||
p := sdstat.Mode().Perm()
|
||||
if p&0007 != 0 {
|
||||
return nil, errors.Errorf("sockdir must not be world-accessible (permissions are %#o)", p)
|
||||
}
|
||||
|
||||
ul, err := net.ListenUnix("unix", f.sockaddr)
|
||||
ul, err := ListenUnixPrivate(f.sockaddr)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot listen on unix socket %s", f.sockaddr)
|
||||
}
|
||||
|
26
cmd/helpers.go
Normal file
26
cmd/helpers.go
Normal file
@ -0,0 +1,26 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func ListenUnixPrivate(sockaddr *net.UnixAddr) (*net.UnixListener, error) {
|
||||
|
||||
sockdir := filepath.Dir(sockaddr.Name)
|
||||
sdstat, err := os.Stat(sockdir)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot stat(2) '%s'", sockdir)
|
||||
}
|
||||
if !sdstat.IsDir() {
|
||||
return nil, errors.Errorf("%s is not a directory: %s", sockdir)
|
||||
}
|
||||
p := sdstat.Mode().Perm()
|
||||
if p&0007 != 0 {
|
||||
return nil, errors.Errorf("%s must not be world-accessible (permissions are %#o)", p)
|
||||
}
|
||||
|
||||
return net.ListenUnix("unix", sockaddr)
|
||||
}
|
Loading…
Reference in New Issue
Block a user