diff --git a/src/cmd/create.go b/src/cmd/create.go index fcd6a1300..80e4f8c36 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -28,11 +28,17 @@ import ( "github.com/containers/toolbox/pkg/podman" "github.com/containers/toolbox/pkg/shell" "github.com/containers/toolbox/pkg/utils" - systemd "github.com/coreos/go-systemd/v22/dbus" + "github.com/godbus/dbus/v5" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) +const ( + alpha = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` + num = `0123456789` + alphanum = alpha + num +) + var ( createFlags struct { container string @@ -496,24 +502,34 @@ func getFullyQualifiedImageName(image string) (string, error) { func getKCMSocket() (string, error) { logrus.Debug("Resolving path to the KCM socket") - connection, err := systemd.NewSystemConnection() + connection, err := dbus.SystemBus() if err != nil { return "", errors.New("failed to connect to the D-Bus system instance") } - defer connection.Close() + kcmUnitNameEscaped := systemdPathBusEscape("sssd-kcm.socket") + kcmUnitPath := dbus.ObjectPath("/org/freedesktop/systemd1/unit/" + kcmUnitNameEscaped) + kcmUnit := connection.Object("org.freedesktop.systemd1", kcmUnitPath) + call := kcmUnit.Call("org.freedesktop.DBus.Properties.GetAll", 0, "") - properties, err := connection.GetAllProperties("sssd-kcm.socket") + var result map[string]dbus.Variant + err = call.Store(&result) if err != nil { return "", errors.New("failed to get the properties of sssd-kcm.socket") } - value := properties["Listen"] - if value == nil { + listenVariant, listenFound := result["Listen"] + if !listenFound { return "", errors.New("failed to find the Listen property of sssd-kcm.socket") } - sockets := value.([][]interface{}) + listenVariantSignature := listenVariant.Signature().String() + if listenVariantSignature != "aav" { + return "", errors.New("unknown reply from org.freedesktop.DBus.Properties.GetAll") + } + + listenValue := listenVariant.Value() + sockets := listenValue.([][]interface{}) for _, socket := range sockets { if socket[0] == "Stream" { path := socket[1].(string) @@ -632,3 +648,31 @@ func pullImage(image, release string) (bool, error) { return true, nil } + +// systemdNeedsEscape checks whether a byte in a potential dbus ObjectPath needs to be escaped +func systemdNeedsEscape(i int, b byte) bool { + // Escape everything that is not a-z-A-Z-0-9 + // Also escape 0-9 if it's the first character + return strings.IndexByte(alphanum, b) == -1 || + (i == 0 && strings.IndexByte(num, b) != -1) +} + +// systemdPathBusEscape sanitizes a constituent string of a dbus ObjectPath using the +// rules that systemd uses for serializing special characters. +func systemdPathBusEscape(path string) string { + // Special case the empty string + if len(path) == 0 { + return "_" + } + n := []byte{} + for i := 0; i < len(path); i++ { + c := path[i] + if systemdNeedsEscape(i, c) { + e := fmt.Sprintf("_%x", c) + n = append(n, []byte(e)...) + } else { + n = append(n, c) + } + } + return string(n) +} diff --git a/src/go.mod b/src/go.mod index eb87cdbfe..07891e13a 100644 --- a/src/go.mod +++ b/src/go.mod @@ -6,7 +6,6 @@ require ( github.com/HarryMichal/go-version v1.0.0 github.com/acobaugh/osrelease v0.0.0-20181218015638-a93a0a55a249 github.com/briandowns/spinner v1.10.0 - github.com/coreos/go-systemd/v22 v22.0.0 github.com/godbus/dbus/v5 v5.0.3 github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v0.0.5 diff --git a/src/go.sum b/src/go.sum index e144f35f0..5ee0d2179 100644 --- a/src/go.sum +++ b/src/go.sum @@ -9,8 +9,6 @@ github.com/briandowns/spinner v1.10.0/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/coreos/go-systemd/v22 v22.0.0 h1:XJIw/+VlJ+87J+doOxznsAWIdmWuViOVhkQamW5YV28= -github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=