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

sciond socket file mode. #3099

Merged
merged 3 commits into from
Sep 5, 2019
Merged
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
2 changes: 2 additions & 0 deletions go/lib/sciond/sciond.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
SVCInfoTTL = 10 * time.Second
// DefaultSCIONDPath contains the system default for a SCIOND socket.
DefaultSCIONDPath = "/run/shm/sciond/default.sock"
// DefaultSocketFileMode allows read/write to the user and group only.
DefaultSocketFileMode = 0770
)

// Service describes a SCIOND endpoint. New connections to SCIOND can be
Expand Down
18 changes: 18 additions & 0 deletions go/sciond/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package config

import (
"io"
"os"
"strconv"
"time"

"github.com/scionproto/scion/go/lib/common"
Expand Down Expand Up @@ -100,6 +102,8 @@ type SDConfig struct {
// Address to listen on for normal unixgram messages. If empty, a
// unixgram server on the default socket is started.
Unix string
// Socket files (both Reliable and Unix) permissions when created; read from octal (e.g. 0755).
SocketFileMode FileMode
// If set to True, the socket is removed before being created
DeleteSocket bool
// Public is the local address to listen on for SCION messages (if Bind is
Expand All @@ -124,6 +128,9 @@ func (cfg *SDConfig) InitDefaults() {
if cfg.Unix == "" {
cfg.Unix = "/run/shm/sciond/default-unix.sock"
}
if cfg.SocketFileMode == 0 {
cfg.SocketFileMode = sciond.DefaultSocketFileMode
}
if cfg.QueryInterval.Duration == 0 {
cfg.QueryInterval.Duration = DefaultQueryInterval
}
Expand All @@ -137,6 +144,9 @@ func (cfg *SDConfig) Validate() error {
if cfg.Unix == "" {
return common.NewBasicError("Unix must be set", nil)
}
if cfg.SocketFileMode == 0 {
return common.NewBasicError("SocketFileMode must be set", nil)
}
if cfg.QueryInterval.Duration == 0 {
return common.NewBasicError("QueryInterval must not be zero", nil)
}
Expand All @@ -161,3 +171,11 @@ func (cfg *SDConfig) CreateSocketDirs() error {
}
return nil
}

type FileMode os.FileMode

func (f *FileMode) UnmarshalText(text []byte) error {
perm, err := strconv.ParseUint(string(text), 8, 32)
*f = FileMode(perm)
return err
}
1 change: 1 addition & 0 deletions go/sciond/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func CheckTestSDConfig(cfg *SDConfig, id string) {
pathstoragetest.CheckTestRevCacheConf(&cfg.RevCache)
SoMsg("Reliable correct", cfg.Reliable, ShouldEqual, sciond.DefaultSCIONDPath)
SoMsg("Unix correct", cfg.Unix, ShouldEqual, "/run/shm/sciond/default-unix.sock")
SoMsg("File permissions correct", cfg.SocketFileMode, ShouldEqual, sciond.DefaultSocketFileMode)
SoMsg("Public correct", cfg.Public.String(), ShouldEqual,
"1-ff00:0:110,[127.0.0.1]:0 (UDP)")
SoMsg("QueryInterval correct", cfg.QueryInterval.Duration, ShouldEqual, DefaultQueryInterval)
Expand Down
3 changes: 3 additions & 0 deletions go/sciond/internal/config/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Reliable = "/run/shm/sciond/default.sock"
# unixgram server on the default socket is started.
Unix = "/run/shm/sciond/default-unix.sock"

# File permissions of both the Reliable and Unix socket files, in octal. (default "0770")
SocketFileMode = "0770"

# If set to True, the socket is removed before being created. (default false)
DeleteSocket = false

Expand Down
23 changes: 19 additions & 4 deletions go/sciond/internal/servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"io"
"net"
"os"
"strings"
"sync"

Expand All @@ -35,6 +36,7 @@ type HandlerMap map[proto.SCIONDMsg_Which]Handler
type Server struct {
network string
address string
filemode os.FileMode
handlers map[proto.SCIONDMsg_Which]Handler
log log.Logger

Expand All @@ -48,10 +50,13 @@ type Server struct {
// HandlerMap. To start listening on the address, call ListenAndServe.
//
// Network must be "unixpacket" or "rsock".
func NewServer(network string, address string, handlers HandlerMap, logger log.Logger) *Server {
func NewServer(network string, address string, filemode os.FileMode, handlers HandlerMap,
logger log.Logger) *Server {

return &Server{
network: network,
address: address,
filemode: filemode,
handlers: handlers,
log: logger,
}
Expand Down Expand Up @@ -99,18 +104,28 @@ func (srv *Server) ListenAndServe() error {
}

func (srv *Server) listen() (net.Listener, error) {
var listener net.Listener
var err error
switch srv.network {
case "unixpacket":
laddr, err := net.ResolveUnixAddr("unixpacket", srv.address)
var laddr *net.UnixAddr
laddr, err = net.ResolveUnixAddr("unixpacket", srv.address)
if err != nil {
return nil, err
}
return net.ListenUnix("unixpacket", laddr)
listener, err = net.ListenUnix("unixpacket", laddr)
case "rsock":
return reliable.Listen(srv.address)
listener, err = reliable.Listen(srv.address)
default:
return nil, common.NewBasicError("unknown network", nil, "net", srv.network)
}
if err != nil {
return nil, err
}
if err := os.Chmod(srv.address, srv.filemode); err != nil {
return nil, common.NewBasicError("chmod failed", err, "address", srv.address)
}
return listener, nil
}

// Close makes the Server stop listening for new connections, and immediately
Expand Down
3 changes: 2 additions & 1 deletion go/sciond/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ func startDiscovery() error {
func NewServer(network string, rsockPath string, handlers servers.HandlerMap,
logger log.Logger) (*servers.Server, func()) {

server := servers.NewServer(network, rsockPath, handlers, logger)
server := servers.NewServer(network, rsockPath, os.FileMode(cfg.SD.SocketFileMode), handlers,
logger)
shutdownF := func() {
ctx, cancelF := context.WithTimeout(context.Background(), ShutdownWaitTimeout)
server.Shutdown(ctx)
Expand Down