build(deps): bump github.com/godbus/dbus/v5 from 5.0.4 to 5.1.0

Bumps [github.com/godbus/dbus/v5](https://github.com/godbus/dbus) from 5.0.4 to 5.1.0.
- [Release notes](https://github.com/godbus/dbus/releases)
- [Commits](https://github.com/godbus/dbus/compare/v5.0.4...v5.1.0)

---
updated-dependencies:
- dependency-name: github.com/godbus/dbus/v5
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
dependabot[bot] 2022-12-26 17:18:41 +01:00 committed by Matthieu MOREL
parent 648dd2e14c
commit 91b1a0e385
24 changed files with 435 additions and 230 deletions

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c
github.com/d2g/dhcp4client v1.0.0 github.com/d2g/dhcp4client v1.0.0
github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5 github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5
github.com/godbus/dbus/v5 v5.0.4 github.com/godbus/dbus/v5 v5.1.0
github.com/mattn/go-shellwords v1.0.12 github.com/mattn/go-shellwords v1.0.12
github.com/networkplumbing/go-nft v0.2.0 github.com/networkplumbing/go-nft v0.2.0
github.com/onsi/ginkgo v1.16.4 github.com/onsi/ginkgo v1.16.4

3
go.sum
View File

@ -306,8 +306,9 @@ github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblf
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8= github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/googleapis v1.2.0/go.mod h1:Njal3psf3qN6dwBtQfUmBZh2ybovJ0tlu3o/AC7HYjU= github.com/gogo/googleapis v1.2.0/go.mod h1:Njal3psf3qN6dwBtQfUmBZh2ybovJ0tlu3o/AC7HYjU=
github.com/gogo/googleapis v1.4.0/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c= github.com/gogo/googleapis v1.4.0/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=

View File

@ -14,14 +14,12 @@ D-Bus message bus system.
### Installation ### Installation
This packages requires Go 1.7. If you installed it and set up your GOPATH, just run: This packages requires Go 1.12 or later. It can be installed by running the command below:
``` ```
go get github.com/godbus/dbus go get github.com/godbus/dbus/v5
``` ```
If you want to use the subpackages, you can install them the same way.
### Usage ### Usage
The complete package documentation and some simple examples are available at The complete package documentation and some simple examples are available at
@ -30,10 +28,12 @@ The complete package documentation and some simple examples are available at
gives a short overview over the basic usage. gives a short overview over the basic usage.
#### Projects using godbus #### Projects using godbus
- [notify](https://github.com/esiqveland/notify) provides desktop notifications over dbus into a library. - [fyne](https://github.com/fyne-io/fyne) a cross platform GUI in Go inspired by Material Design.
- [fynedesk](https://github.com/fyne-io/fynedesk) a full desktop environment for Linux/Unix using Fyne.
- [go-bluetooth](https://github.com/muka/go-bluetooth) provides a bluetooth client over bluez dbus API. - [go-bluetooth](https://github.com/muka/go-bluetooth) provides a bluetooth client over bluez dbus API.
- [playerbm](https://github.com/altdesktop/playerbm) a bookmark utility for media players.
- [iwd](https://github.com/shibumi/iwd) go bindings for the internet wireless daemon "iwd". - [iwd](https://github.com/shibumi/iwd) go bindings for the internet wireless daemon "iwd".
- [notify](https://github.com/esiqveland/notify) provides desktop notifications over dbus into a library.
- [playerbm](https://github.com/altdesktop/playerbm) a bookmark utility for media players.
Please note that the API is considered unstable for now and may change without Please note that the API is considered unstable for now and may change without
further notice. further notice.

View File

@ -53,7 +53,7 @@ type Auth interface {
// bus. Auth must not be called on shared connections. // bus. Auth must not be called on shared connections.
func (conn *Conn) Auth(methods []Auth) error { func (conn *Conn) Auth(methods []Auth) error {
if methods == nil { if methods == nil {
uid := strconv.Itoa(os.Getuid()) uid := strconv.Itoa(os.Geteuid())
methods = []Auth{AuthExternal(uid), AuthCookieSha1(uid, getHomeDir())} methods = []Auth{AuthExternal(uid), AuthCookieSha1(uid, getHomeDir())}
} }
in := bufio.NewReader(conn.transport) in := bufio.NewReader(conn.transport)
@ -75,9 +75,9 @@ func (conn *Conn) Auth(methods []Auth) error {
s = s[1:] s = s[1:]
for _, v := range s { for _, v := range s {
for _, m := range methods { for _, m := range methods {
if name, data, status := m.FirstData(); bytes.Equal(v, name) { if name, _, status := m.FirstData(); bytes.Equal(v, name) {
var ok bool var ok bool
err = authWriteLine(conn.transport, []byte("AUTH"), v, data) err = authWriteLine(conn.transport, []byte("AUTH"), v)
if err != nil { if err != nil {
return err return err
} }
@ -176,9 +176,10 @@ func (conn *Conn) tryAuth(m Auth, state authState, in *bufio.Reader) (error, boo
return err, false return err, false
} }
state = waitingForReject state = waitingForReject
} else {
conn.uuid = string(s[1])
return nil, true
} }
conn.uuid = string(s[1])
return nil, true
case state == waitingForData: case state == waitingForData:
err = authWriteLine(conn.transport, []byte("ERROR")) err = authWriteLine(conn.transport, []byte("ERROR"))
if err != nil { if err != nil {
@ -191,14 +192,18 @@ func (conn *Conn) tryAuth(m Auth, state authState, in *bufio.Reader) (error, boo
return err, false return err, false
} }
state = waitingForReject state = waitingForReject
} else {
conn.uuid = string(s[1])
return nil, true
}
case state == waitingForOk && string(s[0]) == "DATA":
err = authWriteLine(conn.transport, []byte("DATA"))
if err != nil {
return err, false
} }
conn.uuid = string(s[1])
return nil, true
case state == waitingForOk && string(s[0]) == "REJECTED": case state == waitingForOk && string(s[0]) == "REJECTED":
return nil, false return nil, false
case state == waitingForOk && (string(s[0]) == "DATA" || case state == waitingForOk && string(s[0]) == "ERROR":
string(s[0]) == "ERROR"):
err = authWriteLine(conn.transport, []byte("CANCEL")) err = authWriteLine(conn.transport, []byte("CANCEL"))
if err != nil { if err != nil {
return err, false return err, false

View File

@ -73,7 +73,7 @@ func SessionBus() (conn *Conn, err error) {
return return
} }
func getSessionBusAddress() (string, error) { func getSessionBusAddress(autolaunch bool) (string, error) {
if address := os.Getenv("DBUS_SESSION_BUS_ADDRESS"); address != "" && address != "autolaunch:" { if address := os.Getenv("DBUS_SESSION_BUS_ADDRESS"); address != "" && address != "autolaunch:" {
return address, nil return address, nil
@ -81,12 +81,26 @@ func getSessionBusAddress() (string, error) {
os.Setenv("DBUS_SESSION_BUS_ADDRESS", address) os.Setenv("DBUS_SESSION_BUS_ADDRESS", address)
return address, nil return address, nil
} }
if !autolaunch {
return "", errors.New("dbus: couldn't determine address of session bus")
}
return getSessionBusPlatformAddress() return getSessionBusPlatformAddress()
} }
// SessionBusPrivate returns a new private connection to the session bus. // SessionBusPrivate returns a new private connection to the session bus.
func SessionBusPrivate(opts ...ConnOption) (*Conn, error) { func SessionBusPrivate(opts ...ConnOption) (*Conn, error) {
address, err := getSessionBusAddress() address, err := getSessionBusAddress(true)
if err != nil {
return nil, err
}
return Dial(address, opts...)
}
// SessionBusPrivate returns a new private connection to the session bus. If
// the session bus is not already open, do not attempt to launch it.
func SessionBusPrivateNoAutoStartup(opts ...ConnOption) (*Conn, error) {
address, err := getSessionBusAddress(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -121,7 +135,7 @@ func SystemBus() (conn *Conn, err error) {
// ConnectSessionBus connects to the session bus. // ConnectSessionBus connects to the session bus.
func ConnectSessionBus(opts ...ConnOption) (*Conn, error) { func ConnectSessionBus(opts ...ConnOption) (*Conn, error) {
address, err := getSessionBusAddress() address, err := getSessionBusAddress(true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -155,7 +169,7 @@ func Connect(address string, opts ...ConnOption) (*Conn, error) {
// SystemBusPrivate returns a new private connection to the system bus. // SystemBusPrivate returns a new private connection to the system bus.
// Note: this connection is not ready to use. One must perform Auth and Hello // Note: this connection is not ready to use. One must perform Auth and Hello
// on the connection before it is useable. // on the connection before it is usable.
func SystemBusPrivate(opts ...ConnOption) (*Conn, error) { func SystemBusPrivate(opts ...ConnOption) (*Conn, error) {
return Dial(getSystemBusPlatformAddress(), opts...) return Dial(getSystemBusPlatformAddress(), opts...)
} }
@ -180,7 +194,7 @@ func Dial(address string, opts ...ConnOption) (*Conn, error) {
// //
// Deprecated: use Dial with options instead. // Deprecated: use Dial with options instead.
func DialHandler(address string, handler Handler, signalHandler SignalHandler) (*Conn, error) { func DialHandler(address string, handler Handler, signalHandler SignalHandler) (*Conn, error) {
return Dial(address, WithSignalHandler(signalHandler)) return Dial(address, WithHandler(handler), WithSignalHandler(signalHandler))
} }
// ConnOption is a connection option. // ConnOption is a connection option.
@ -270,10 +284,6 @@ func newConn(tr transport, opts ...ConnOption) (*Conn, error) {
conn.ctx = context.Background() conn.ctx = context.Background()
} }
conn.ctx, conn.cancelCtx = context.WithCancel(conn.ctx) conn.ctx, conn.cancelCtx = context.WithCancel(conn.ctx)
go func() {
<-conn.ctx.Done()
conn.Close()
}()
conn.calls = newCallTracker() conn.calls = newCallTracker()
if conn.handler == nil { if conn.handler == nil {
@ -288,6 +298,11 @@ func newConn(tr transport, opts ...ConnOption) (*Conn, error) {
conn.outHandler = &outputHandler{conn: conn} conn.outHandler = &outputHandler{conn: conn}
conn.names = newNameTracker() conn.names = newNameTracker()
conn.busObj = conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus") conn.busObj = conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus")
go func() {
<-conn.ctx.Done()
conn.Close()
}()
return conn, nil return conn, nil
} }
@ -478,14 +493,24 @@ func (conn *Conn) sendMessageAndIfClosed(msg *Message, ifClosed func()) {
conn.outInt(msg) conn.outInt(msg)
} }
err := conn.outHandler.sendAndIfClosed(msg, ifClosed) err := conn.outHandler.sendAndIfClosed(msg, ifClosed)
conn.calls.handleSendError(msg, err)
if err != nil { if err != nil {
conn.serialGen.RetireSerial(msg.serial) conn.handleSendError(msg, err)
} else if msg.Type != TypeMethodCall { } else if msg.Type != TypeMethodCall {
conn.serialGen.RetireSerial(msg.serial) conn.serialGen.RetireSerial(msg.serial)
} }
} }
func (conn *Conn) handleSendError(msg *Message, err error) {
if msg.Type == TypeMethodCall {
conn.calls.handleSendError(msg, err)
} else if msg.Type == TypeMethodReply {
if _, ok := err.(FormatError); ok {
conn.sendError(err, msg.Headers[FieldDestination].value.(string), msg.Headers[FieldReplySerial].value.(uint32))
}
}
conn.serialGen.RetireSerial(msg.serial)
}
// Send sends the given message to the message bus. You usually don't need to // Send sends the given message to the message bus. You usually don't need to
// use this; use the higher-level equivalents (Call / Go, Emit and Export) // use this; use the higher-level equivalents (Call / Go, Emit and Export)
// instead. If msg is a method call and NoReplyExpected is not set, a non-nil // instead. If msg is a method call and NoReplyExpected is not set, a non-nil
@ -526,6 +551,11 @@ func (conn *Conn) send(ctx context.Context, msg *Message, ch chan *Call) *Call {
call.ctx = ctx call.ctx = ctx
call.ctxCanceler = canceler call.ctxCanceler = canceler
conn.calls.track(msg.serial, call) conn.calls.track(msg.serial, call)
if ctx.Err() != nil {
// short path: don't even send the message if context already cancelled
conn.calls.handleSendError(msg, ctx.Err())
return call
}
go func() { go func() {
<-ctx.Done() <-ctx.Done()
conn.calls.handleSendError(msg, ctx.Err()) conn.calls.handleSendError(msg, ctx.Err())
@ -625,7 +655,9 @@ func (conn *Conn) RemoveMatchSignalContext(ctx context.Context, options ...Match
// Signal registers the given channel to be passed all received signal messages. // Signal registers the given channel to be passed all received signal messages.
// //
// Multiple of these channels can be registered at the same time. // Multiple of these channels can be registered at the same time. The channel is
// closed if the Conn is closed; it should not be closed by the caller before
// RemoveSignal was called on it.
// //
// These channels are "overwritten" by Eavesdrop; i.e., if there currently is a // These channels are "overwritten" by Eavesdrop; i.e., if there currently is a
// channel for eavesdropped messages, this channel receives all signals, and // channel for eavesdropped messages, this channel receives all signals, and
@ -741,7 +773,12 @@ func getKey(s, key string) string {
for _, keyEqualsValue := range strings.Split(s, ",") { for _, keyEqualsValue := range strings.Split(s, ",") {
keyValue := strings.SplitN(keyEqualsValue, "=", 2) keyValue := strings.SplitN(keyEqualsValue, "=", 2)
if len(keyValue) == 2 && keyValue[0] == key { if len(keyValue) == 2 && keyValue[0] == key {
return keyValue[1] val, err := UnescapeBusAddressValue(keyValue[1])
if err != nil {
// No way to return an error.
return ""
}
return val
} }
} }
return "" return ""

View File

@ -54,7 +54,7 @@ func tryDiscoverDbusSessionBusAddress() string {
if runUserBusFile := path.Join(runtimeDirectory, "bus"); fileExists(runUserBusFile) { if runUserBusFile := path.Join(runtimeDirectory, "bus"); fileExists(runUserBusFile) {
// if /run/user/<uid>/bus exists, that file itself // if /run/user/<uid>/bus exists, that file itself
// *is* the unix socket, so return its path // *is* the unix socket, so return its path
return fmt.Sprintf("unix:path=%s", runUserBusFile) return fmt.Sprintf("unix:path=%s", EscapeBusAddressValue(runUserBusFile))
} }
if runUserSessionDbusFile := path.Join(runtimeDirectory, "dbus-session"); fileExists(runUserSessionDbusFile) { if runUserSessionDbusFile := path.Join(runtimeDirectory, "dbus-session"); fileExists(runUserSessionDbusFile) {
// if /run/user/<uid>/dbus-session exists, it's a // if /run/user/<uid>/dbus-session exists, it's a
@ -85,9 +85,6 @@ func getRuntimeDirectory() (string, error) {
} }
func fileExists(filename string) bool { func fileExists(filename string) bool {
if _, err := os.Stat(filename); !os.IsNotExist(err) { _, err := os.Stat(filename)
return true return !os.IsNotExist(err)
} else {
return false
}
} }

View File

@ -122,8 +122,11 @@ func isConvertibleTo(dest, src reflect.Type) bool {
case dest.Kind() == reflect.Slice: case dest.Kind() == reflect.Slice:
return src.Kind() == reflect.Slice && return src.Kind() == reflect.Slice &&
isConvertibleTo(dest.Elem(), src.Elem()) isConvertibleTo(dest.Elem(), src.Elem())
case dest.Kind() == reflect.Ptr:
dest = dest.Elem()
return isConvertibleTo(dest, src)
case dest.Kind() == reflect.Struct: case dest.Kind() == reflect.Struct:
return src == interfacesType return src == interfacesType || dest.Kind() == src.Kind()
default: default:
return src.ConvertibleTo(dest) return src.ConvertibleTo(dest)
} }
@ -274,13 +277,8 @@ func storeSliceIntoInterface(dest, src reflect.Value) error {
func storeSliceIntoSlice(dest, src reflect.Value) error { func storeSliceIntoSlice(dest, src reflect.Value) error {
if dest.IsNil() || dest.Len() < src.Len() { if dest.IsNil() || dest.Len() < src.Len() {
dest.Set(reflect.MakeSlice(dest.Type(), src.Len(), src.Cap())) dest.Set(reflect.MakeSlice(dest.Type(), src.Len(), src.Cap()))
} } else if dest.Len() > src.Len() {
if dest.Len() != src.Len() { dest.Set(dest.Slice(0, src.Len()))
return fmt.Errorf(
"dbus.Store: type mismatch: "+
"slices are different lengths "+
"need: %d have: %d",
src.Len(), dest.Len())
} }
for i := 0; i < src.Len(); i++ { for i := 0; i < src.Len(); i++ {
err := store(dest.Index(i), getVariantValue(src.Index(i))) err := store(dest.Index(i), getVariantValue(src.Index(i)))

View File

@ -10,14 +10,16 @@ type decoder struct {
in io.Reader in io.Reader
order binary.ByteOrder order binary.ByteOrder
pos int pos int
fds []int
} }
// newDecoder returns a new decoder that reads values from in. The input is // newDecoder returns a new decoder that reads values from in. The input is
// expected to be in the given byte order. // expected to be in the given byte order.
func newDecoder(in io.Reader, order binary.ByteOrder) *decoder { func newDecoder(in io.Reader, order binary.ByteOrder, fds []int) *decoder {
dec := new(decoder) dec := new(decoder)
dec.in = in dec.in = in
dec.order = order dec.order = order
dec.fds = fds
return dec return dec
} }
@ -53,7 +55,7 @@ func (dec *decoder) Decode(sig Signature) (vs []interface{}, err error) {
vs = make([]interface{}, 0) vs = make([]interface{}, 0)
s := sig.str s := sig.str
for s != "" { for s != "" {
err, rem := validSingle(s, 0) err, rem := validSingle(s, &depthCounter{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -150,7 +152,7 @@ func (dec *decoder) decode(s string, depth int) interface{} {
if len(sig.str) == 0 { if len(sig.str) == 0 {
panic(FormatError("variant signature is empty")) panic(FormatError("variant signature is empty"))
} }
err, rem := validSingle(sig.str, 0) err, rem := validSingle(sig.str, &depthCounter{})
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -161,7 +163,11 @@ func (dec *decoder) decode(s string, depth int) interface{} {
variant.value = dec.decode(sig.str, depth+1) variant.value = dec.decode(sig.str, depth+1)
return variant return variant
case 'h': case 'h':
return UnixFDIndex(dec.decode("u", depth).(uint32)) idx := dec.decode("u", depth).(uint32)
if int(idx) < len(dec.fds) {
return UnixFD(dec.fds[idx])
}
return UnixFDIndex(idx)
case 'a': case 'a':
if len(s) > 1 && s[1] == '{' { if len(s) > 1 && s[1] == '{' {
ksig := s[2:3] ksig := s[2:3]
@ -219,7 +225,7 @@ func (dec *decoder) decode(s string, depth int) interface{} {
v := make([]interface{}, 0) v := make([]interface{}, 0)
s = s[1 : len(s)-1] s = s[1 : len(s)-1]
for s != "" { for s != "" {
err, rem := validSingle(s, 0) err, rem := validSingle(s, &depthCounter{})
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -10,8 +10,10 @@ value.
Conversion Rules Conversion Rules
For outgoing messages, Go types are automatically converted to the For outgoing messages, Go types are automatically converted to the
corresponding D-Bus types. The following types are directly encoded as their corresponding D-Bus types. See the official specification at
respective D-Bus equivalents: https://dbus.freedesktop.org/doc/dbus-specification.html#type-system for more
information on the D-Bus type system. The following types are directly encoded
as their respective D-Bus equivalents:
Go type | D-Bus type Go type | D-Bus type
------------+----------- ------------+-----------
@ -39,8 +41,8 @@ Maps encode as DICTs, provided that their key type can be used as a key for
a DICT. a DICT.
Structs other than Variant and Signature encode as a STRUCT containing their Structs other than Variant and Signature encode as a STRUCT containing their
exported fields. Fields whose tags contain `dbus:"-"` and unexported fields will exported fields in order. Fields whose tags contain `dbus:"-"` and unexported
be skipped. fields will be skipped.
Pointers encode as the value they're pointed to. Pointers encode as the value they're pointed to.

View File

@ -5,28 +5,33 @@ import (
"encoding/binary" "encoding/binary"
"io" "io"
"reflect" "reflect"
"strings"
"unicode/utf8"
) )
// An encoder encodes values to the D-Bus wire format. // An encoder encodes values to the D-Bus wire format.
type encoder struct { type encoder struct {
out io.Writer out io.Writer
fds []int
order binary.ByteOrder order binary.ByteOrder
pos int pos int
} }
// NewEncoder returns a new encoder that writes to out in the given byte order. // NewEncoder returns a new encoder that writes to out in the given byte order.
func newEncoder(out io.Writer, order binary.ByteOrder) *encoder { func newEncoder(out io.Writer, order binary.ByteOrder, fds []int) *encoder {
return newEncoderAtOffset(out, 0, order) enc := newEncoderAtOffset(out, 0, order, fds)
return enc
} }
// newEncoderAtOffset returns a new encoder that writes to out in the given // newEncoderAtOffset returns a new encoder that writes to out in the given
// byte order. Specify the offset to initialize pos for proper alignment // byte order. Specify the offset to initialize pos for proper alignment
// computation. // computation.
func newEncoderAtOffset(out io.Writer, offset int, order binary.ByteOrder) *encoder { func newEncoderAtOffset(out io.Writer, offset int, order binary.ByteOrder, fds []int) *encoder {
enc := new(encoder) enc := new(encoder)
enc.out = out enc.out = out
enc.order = order enc.order = order
enc.pos = offset enc.pos = offset
enc.fds = fds
return enc return enc
} }
@ -75,6 +80,9 @@ func (enc *encoder) Encode(vs ...interface{}) (err error) {
// encode encodes the given value to the writer and panics on error. depth holds // encode encodes the given value to the writer and panics on error. depth holds
// the depth of the container nesting. // the depth of the container nesting.
func (enc *encoder) encode(v reflect.Value, depth int) { func (enc *encoder) encode(v reflect.Value, depth int) {
if depth > 64 {
panic(FormatError("input exceeds depth limitation"))
}
enc.align(alignment(v.Type())) enc.align(alignment(v.Type()))
switch v.Kind() { switch v.Kind() {
case reflect.Uint8: case reflect.Uint8:
@ -97,7 +105,14 @@ func (enc *encoder) encode(v reflect.Value, depth int) {
enc.binwrite(uint16(v.Uint())) enc.binwrite(uint16(v.Uint()))
enc.pos += 2 enc.pos += 2
case reflect.Int, reflect.Int32: case reflect.Int, reflect.Int32:
enc.binwrite(int32(v.Int())) if v.Type() == unixFDType {
fd := v.Int()
idx := len(enc.fds)
enc.fds = append(enc.fds, int(fd))
enc.binwrite(uint32(idx))
} else {
enc.binwrite(int32(v.Int()))
}
enc.pos += 4 enc.pos += 4
case reflect.Uint, reflect.Uint32: case reflect.Uint, reflect.Uint32:
enc.binwrite(uint32(v.Uint())) enc.binwrite(uint32(v.Uint()))
@ -112,9 +127,21 @@ func (enc *encoder) encode(v reflect.Value, depth int) {
enc.binwrite(v.Float()) enc.binwrite(v.Float())
enc.pos += 8 enc.pos += 8
case reflect.String: case reflect.String:
enc.encode(reflect.ValueOf(uint32(len(v.String()))), depth) str := v.String()
if !utf8.ValidString(str) {
panic(FormatError("input has a not-utf8 char in string"))
}
if strings.IndexByte(str, byte(0)) != -1 {
panic(FormatError("input has a null char('\\000') in string"))
}
if v.Type() == objectPathType {
if !ObjectPath(str).IsValid() {
panic(FormatError("invalid object path"))
}
}
enc.encode(reflect.ValueOf(uint32(len(str))), depth)
b := make([]byte, v.Len()+1) b := make([]byte, v.Len()+1)
copy(b, v.String()) copy(b, str)
b[len(b)-1] = 0 b[len(b)-1] = 0
n, err := enc.out.Write(b) n, err := enc.out.Write(b)
if err != nil { if err != nil {
@ -124,20 +151,23 @@ func (enc *encoder) encode(v reflect.Value, depth int) {
case reflect.Ptr: case reflect.Ptr:
enc.encode(v.Elem(), depth) enc.encode(v.Elem(), depth)
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
if depth >= 64 {
panic(FormatError("input exceeds container depth limit"))
}
// Lookahead offset: 4 bytes for uint32 length (with alignment), // Lookahead offset: 4 bytes for uint32 length (with alignment),
// plus alignment for elements. // plus alignment for elements.
n := enc.padding(0, 4) + 4 n := enc.padding(0, 4) + 4
offset := enc.pos + n + enc.padding(n, alignment(v.Type().Elem())) offset := enc.pos + n + enc.padding(n, alignment(v.Type().Elem()))
var buf bytes.Buffer var buf bytes.Buffer
bufenc := newEncoderAtOffset(&buf, offset, enc.order) bufenc := newEncoderAtOffset(&buf, offset, enc.order, enc.fds)
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
bufenc.encode(v.Index(i), depth+1) bufenc.encode(v.Index(i), depth+1)
} }
if buf.Len() > 1<<26 {
panic(FormatError("input exceeds array size limitation"))
}
enc.fds = bufenc.fds
enc.encode(reflect.ValueOf(uint32(buf.Len())), depth) enc.encode(reflect.ValueOf(uint32(buf.Len())), depth)
length := buf.Len() length := buf.Len()
enc.align(alignment(v.Type().Elem())) enc.align(alignment(v.Type().Elem()))
@ -146,13 +176,10 @@ func (enc *encoder) encode(v reflect.Value, depth int) {
} }
enc.pos += length enc.pos += length
case reflect.Struct: case reflect.Struct:
if depth >= 64 && v.Type() != signatureType {
panic(FormatError("input exceeds container depth limit"))
}
switch t := v.Type(); t { switch t := v.Type(); t {
case signatureType: case signatureType:
str := v.Field(0) str := v.Field(0)
enc.encode(reflect.ValueOf(byte(str.Len())), depth+1) enc.encode(reflect.ValueOf(byte(str.Len())), depth)
b := make([]byte, str.Len()+1) b := make([]byte, str.Len()+1)
copy(b, str.String()) copy(b, str.String())
b[len(b)-1] = 0 b[len(b)-1] = 0
@ -176,9 +203,6 @@ func (enc *encoder) encode(v reflect.Value, depth int) {
case reflect.Map: case reflect.Map:
// Maps are arrays of structures, so they actually increase the depth by // Maps are arrays of structures, so they actually increase the depth by
// 2. // 2.
if depth >= 63 {
panic(FormatError("input exceeds container depth limit"))
}
if !isKeyType(v.Type().Key()) { if !isKeyType(v.Type().Key()) {
panic(InvalidTypeError{v.Type()}) panic(InvalidTypeError{v.Type()})
} }
@ -189,12 +213,13 @@ func (enc *encoder) encode(v reflect.Value, depth int) {
offset := enc.pos + n + enc.padding(n, 8) offset := enc.pos + n + enc.padding(n, 8)
var buf bytes.Buffer var buf bytes.Buffer
bufenc := newEncoderAtOffset(&buf, offset, enc.order) bufenc := newEncoderAtOffset(&buf, offset, enc.order, enc.fds)
for _, k := range keys { for _, k := range keys {
bufenc.align(8) bufenc.align(8)
bufenc.encode(k, depth+2) bufenc.encode(k, depth+2)
bufenc.encode(v.MapIndex(k), depth+2) bufenc.encode(v.MapIndex(k), depth+2)
} }
enc.fds = bufenc.fds
enc.encode(reflect.ValueOf(uint32(buf.Len())), depth) enc.encode(reflect.ValueOf(uint32(buf.Len())), depth)
length := buf.Len() length := buf.Len()
enc.align(8) enc.align(8)

84
vendor/github.com/godbus/dbus/v5/escape.go generated vendored Normal file
View File

@ -0,0 +1,84 @@
package dbus
import "net/url"
// EscapeBusAddressValue implements a requirement to escape the values
// in D-Bus server addresses, as defined by the D-Bus specification at
// https://dbus.freedesktop.org/doc/dbus-specification.html#addresses.
func EscapeBusAddressValue(val string) string {
toEsc := strNeedsEscape(val)
if toEsc == 0 {
// Avoid unneeded allocation/copying.
return val
}
// Avoid allocation for short paths.
var buf [64]byte
var out []byte
// Every to-be-escaped byte needs 2 extra bytes.
required := len(val) + 2*toEsc
if required <= len(buf) {
out = buf[:required]
} else {
out = make([]byte, required)
}
j := 0
for i := 0; i < len(val); i++ {
if ch := val[i]; needsEscape(ch) {
// Convert ch to %xx, where xx is hex value.
out[j] = '%'
out[j+1] = hexchar(ch >> 4)
out[j+2] = hexchar(ch & 0x0F)
j += 3
} else {
out[j] = ch
j++
}
}
return string(out)
}
// UnescapeBusAddressValue unescapes values in D-Bus server addresses,
// as defined by the D-Bus specification at
// https://dbus.freedesktop.org/doc/dbus-specification.html#addresses.
func UnescapeBusAddressValue(val string) (string, error) {
// Looks like url.PathUnescape does exactly what is required.
return url.PathUnescape(val)
}
// hexchar returns an octal representation of a n, where n < 16.
// For invalid values of n, the function panics.
func hexchar(n byte) byte {
const hex = "0123456789abcdef"
// For n >= len(hex), runtime will panic.
return hex[n]
}
// needsEscape tells if a byte is NOT one of optionally-escaped bytes.
func needsEscape(c byte) bool {
if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
return false
}
switch c {
case '-', '_', '/', '\\', '.', '*':
return false
}
return true
}
// strNeedsEscape tells how many bytes in the string need escaping.
func strNeedsEscape(val string) int {
count := 0
for i := 0; i < len(val); i++ {
if needsEscape(val[i]) {
count++
}
}
return count
}

View File

@ -3,6 +3,7 @@ package dbus
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"reflect" "reflect"
"strings" "strings"
) )
@ -26,6 +27,27 @@ var (
} }
) )
func MakeNoObjectError(path ObjectPath) Error {
return Error{
"org.freedesktop.DBus.Error.NoSuchObject",
[]interface{}{fmt.Sprintf("No such object '%s'", string(path))},
}
}
func MakeUnknownMethodError(methodName string) Error {
return Error{
"org.freedesktop.DBus.Error.UnknownMethod",
[]interface{}{fmt.Sprintf("Unknown / invalid method '%s'", methodName)},
}
}
func MakeUnknownInterfaceError(ifaceName string) Error {
return Error{
"org.freedesktop.DBus.Error.UnknownInterface",
[]interface{}{fmt.Sprintf("Object does not implement the interface '%s'", ifaceName)},
}
}
func MakeFailedError(err error) *Error { func MakeFailedError(err error) *Error {
return &Error{ return &Error{
"org.freedesktop.DBus.Error.Failed", "org.freedesktop.DBus.Error.Failed",
@ -128,6 +150,11 @@ func (conn *Conn) handleCall(msg *Message) {
ifaceName, _ := msg.Headers[FieldInterface].value.(string) ifaceName, _ := msg.Headers[FieldInterface].value.(string)
sender, hasSender := msg.Headers[FieldSender].value.(string) sender, hasSender := msg.Headers[FieldSender].value.(string)
serial := msg.serial serial := msg.serial
if len(name) == 0 {
conn.sendError(ErrMsgUnknownMethod, sender, serial)
}
if ifaceName == "org.freedesktop.DBus.Peer" { if ifaceName == "org.freedesktop.DBus.Peer" {
switch name { switch name {
case "Ping": case "Ping":
@ -135,29 +162,26 @@ func (conn *Conn) handleCall(msg *Message) {
case "GetMachineId": case "GetMachineId":
conn.sendReply(sender, serial, conn.uuid) conn.sendReply(sender, serial, conn.uuid)
default: default:
conn.sendError(ErrMsgUnknownMethod, sender, serial) conn.sendError(MakeUnknownMethodError(name), sender, serial)
} }
return return
} }
if len(name) == 0 {
conn.sendError(ErrMsgUnknownMethod, sender, serial)
}
object, ok := conn.handler.LookupObject(path) object, ok := conn.handler.LookupObject(path)
if !ok { if !ok {
conn.sendError(ErrMsgNoObject, sender, serial) conn.sendError(MakeNoObjectError(path), sender, serial)
return return
} }
iface, exists := object.LookupInterface(ifaceName) iface, exists := object.LookupInterface(ifaceName)
if !exists { if !exists {
conn.sendError(ErrMsgUnknownInterface, sender, serial) conn.sendError(MakeUnknownInterfaceError(ifaceName), sender, serial)
return return
} }
m, exists := iface.LookupMethod(name) m, exists := iface.LookupMethod(name)
if !exists { if !exists {
conn.sendError(ErrMsgUnknownMethod, sender, serial) conn.sendError(MakeUnknownMethodError(name), sender, serial)
return return
} }
args, err := conn.decodeArguments(m, sender, msg) args, err := conn.decodeArguments(m, sender, msg)
@ -186,28 +210,23 @@ func (conn *Conn) handleCall(msg *Message) {
} }
reply.Headers[FieldSignature] = MakeVariant(SignatureOf(reply.Body...)) reply.Headers[FieldSignature] = MakeVariant(SignatureOf(reply.Body...))
conn.sendMessageAndIfClosed(reply, nil) if err := reply.IsValid(); err != nil {
fmt.Fprintf(os.Stderr, "dbus: dropping invalid reply to %s.%s on obj %s: %s\n", ifaceName, name, path, err)
} else {
conn.sendMessageAndIfClosed(reply, nil)
}
} }
} }
// Emit emits the given signal on the message bus. The name parameter must be // Emit emits the given signal on the message bus. The name parameter must be
// formatted as "interface.member", e.g., "org.freedesktop.DBus.NameLost". // formatted as "interface.member", e.g., "org.freedesktop.DBus.NameLost".
func (conn *Conn) Emit(path ObjectPath, name string, values ...interface{}) error { func (conn *Conn) Emit(path ObjectPath, name string, values ...interface{}) error {
if !path.IsValid() {
return errors.New("dbus: invalid object path")
}
i := strings.LastIndex(name, ".") i := strings.LastIndex(name, ".")
if i == -1 { if i == -1 {
return errors.New("dbus: invalid method name") return errors.New("dbus: invalid method name")
} }
iface := name[:i] iface := name[:i]
member := name[i+1:] member := name[i+1:]
if !isValidMember(member) {
return errors.New("dbus: invalid method name")
}
if !isValidInterface(iface) {
return errors.New("dbus: invalid interface name")
}
msg := new(Message) msg := new(Message)
msg.Type = TypeSignal msg.Type = TypeSignal
msg.Headers = make(map[HeaderField]Variant) msg.Headers = make(map[HeaderField]Variant)
@ -218,6 +237,9 @@ func (conn *Conn) Emit(path ObjectPath, name string, values ...interface{}) erro
if len(values) > 0 { if len(values) > 0 {
msg.Headers[FieldSignature] = MakeVariant(SignatureOf(values...)) msg.Headers[FieldSignature] = MakeVariant(SignatureOf(values...))
} }
if err := msg.IsValid(); err != nil {
return err
}
var closed bool var closed bool
conn.sendMessageAndIfClosed(msg, func() { conn.sendMessageAndIfClosed(msg, func() {

View File

@ -2,27 +2,24 @@ package dbus
import ( import (
"os" "os"
"sync" "os/user"
)
var (
homeDir string
homeDirLock sync.Mutex
) )
// Get returns the home directory of the current user, which is usually the
// value of HOME environment variable. In case it is not set or empty, os/user
// package is used.
//
// If linking statically with cgo enabled against glibc, make sure the
// osusergo build tag is used.
//
// If needing to do nss lookups, do not disable cgo or set osusergo.
func getHomeDir() string { func getHomeDir() string {
homeDirLock.Lock() homeDir := os.Getenv("HOME")
defer homeDirLock.Unlock()
if homeDir != "" { if homeDir != "" {
return homeDir return homeDir
} }
if u, err := user.Current(); err == nil {
homeDir = os.Getenv("HOME") return u.HomeDir
if homeDir != "" {
return homeDir
} }
return "/"
homeDir = lookupHomeDir()
return homeDir
} }

View File

@ -1,15 +0,0 @@
// +build !static_build
package dbus
import (
"os/user"
)
func lookupHomeDir() string {
u, err := user.Current()
if err != nil {
return "/"
}
return u.HomeDir
}

View File

@ -1,45 +0,0 @@
// +build static_build
package dbus
import (
"bufio"
"os"
"strconv"
"strings"
)
func lookupHomeDir() string {
myUid := os.Getuid()
f, err := os.Open("/etc/passwd")
if err != nil {
return "/"
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if err := s.Err(); err != nil {
break
}
line := strings.TrimSpace(s.Text())
if line == "" {
continue
}
parts := strings.Split(line, ":")
if len(parts) >= 6 {
uid, err := strconv.Atoi(parts[2])
if err == nil && uid == myUid {
return parts[5]
}
}
}
// Default to / if we can't get a better value
return "/"
}

View File

@ -118,11 +118,7 @@ type header struct {
Variant Variant
} }
// DecodeMessage tries to decode a single message in the D-Bus wire format func DecodeMessageWithFDs(rd io.Reader, fds []int) (msg *Message, err error) {
// from the given reader. The byte order is figured out from the first byte.
// The possibly returned error can be an error of the underlying reader, an
// InvalidMessageError or a FormatError.
func DecodeMessage(rd io.Reader) (msg *Message, err error) {
var order binary.ByteOrder var order binary.ByteOrder
var hlength, length uint32 var hlength, length uint32
var typ, flags, proto byte var typ, flags, proto byte
@ -142,7 +138,7 @@ func DecodeMessage(rd io.Reader) (msg *Message, err error) {
return nil, InvalidMessageError("invalid byte order") return nil, InvalidMessageError("invalid byte order")
} }
dec := newDecoder(rd, order) dec := newDecoder(rd, order, fds)
dec.pos = 1 dec.pos = 1
msg = new(Message) msg = new(Message)
@ -166,7 +162,7 @@ func DecodeMessage(rd io.Reader) (msg *Message, err error) {
if hlength+length+16 > 1<<27 { if hlength+length+16 > 1<<27 {
return nil, InvalidMessageError("message is too long") return nil, InvalidMessageError("message is too long")
} }
dec = newDecoder(io.MultiReader(bytes.NewBuffer(b), rd), order) dec = newDecoder(io.MultiReader(bytes.NewBuffer(b), rd), order, fds)
dec.pos = 12 dec.pos = 12
vs, err = dec.Decode(Signature{"a(yv)"}) vs, err = dec.Decode(Signature{"a(yv)"})
if err != nil { if err != nil {
@ -196,7 +192,7 @@ func DecodeMessage(rd io.Reader) (msg *Message, err error) {
sig, _ := msg.Headers[FieldSignature].value.(Signature) sig, _ := msg.Headers[FieldSignature].value.(Signature)
if sig.str != "" { if sig.str != "" {
buf := bytes.NewBuffer(body) buf := bytes.NewBuffer(body)
dec = newDecoder(buf, order) dec = newDecoder(buf, order, fds)
vs, err := dec.Decode(sig) vs, err := dec.Decode(sig)
if err != nil { if err != nil {
return nil, err return nil, err
@ -207,12 +203,32 @@ func DecodeMessage(rd io.Reader) (msg *Message, err error) {
return return
} }
// EncodeTo encodes and sends a message to the given writer. The byte order must // DecodeMessage tries to decode a single message in the D-Bus wire format
// be either binary.LittleEndian or binary.BigEndian. If the message is not // from the given reader. The byte order is figured out from the first byte.
// valid or an error occurs when writing, an error is returned. // The possibly returned error can be an error of the underlying reader, an
func (msg *Message) EncodeTo(out io.Writer, order binary.ByteOrder) error { // InvalidMessageError or a FormatError.
if err := msg.IsValid(); err != nil { func DecodeMessage(rd io.Reader) (msg *Message, err error) {
return err return DecodeMessageWithFDs(rd, make([]int, 0))
}
type nullwriter struct{}
func (nullwriter) Write(p []byte) (cnt int, err error) {
return len(p), nil
}
func (msg *Message) CountFds() (int, error) {
if len(msg.Body) == 0 {
return 0, nil
}
enc := newEncoder(nullwriter{}, nativeEndian, make([]int, 0))
err := enc.Encode(msg.Body...)
return len(enc.fds), err
}
func (msg *Message) EncodeToWithFDs(out io.Writer, order binary.ByteOrder) (fds []int, err error) {
if err := msg.validateHeader(); err != nil {
return nil, err
} }
var vs [7]interface{} var vs [7]interface{}
switch order { switch order {
@ -221,12 +237,16 @@ func (msg *Message) EncodeTo(out io.Writer, order binary.ByteOrder) error {
case binary.BigEndian: case binary.BigEndian:
vs[0] = byte('B') vs[0] = byte('B')
default: default:
return errors.New("dbus: invalid byte order") return nil, errors.New("dbus: invalid byte order")
} }
body := new(bytes.Buffer) body := new(bytes.Buffer)
enc := newEncoder(body, order) fds = make([]int, 0)
enc := newEncoder(body, order, fds)
if len(msg.Body) != 0 { if len(msg.Body) != 0 {
enc.Encode(msg.Body...) err = enc.Encode(msg.Body...)
if err != nil {
return
}
} }
vs[1] = msg.Type vs[1] = msg.Type
vs[2] = msg.Flags vs[2] = msg.Flags
@ -239,22 +259,38 @@ func (msg *Message) EncodeTo(out io.Writer, order binary.ByteOrder) error {
} }
vs[6] = headers vs[6] = headers
var buf bytes.Buffer var buf bytes.Buffer
enc = newEncoder(&buf, order) enc = newEncoder(&buf, order, enc.fds)
enc.Encode(vs[:]...) err = enc.Encode(vs[:]...)
if err != nil {
return
}
enc.align(8) enc.align(8)
body.WriteTo(&buf) body.WriteTo(&buf)
if buf.Len() > 1<<27 { if buf.Len() > 1<<27 {
return InvalidMessageError("message is too long") return make([]int, 0), InvalidMessageError("message is too long")
} }
if _, err := buf.WriteTo(out); err != nil { if _, err := buf.WriteTo(out); err != nil {
return err return make([]int, 0), err
} }
return nil return enc.fds, nil
}
// EncodeTo encodes and sends a message to the given writer. The byte order must
// be either binary.LittleEndian or binary.BigEndian. If the message is not
// valid or an error occurs when writing, an error is returned.
func (msg *Message) EncodeTo(out io.Writer, order binary.ByteOrder) (err error) {
_, err = msg.EncodeToWithFDs(out, order)
return err
} }
// IsValid checks whether msg is a valid message and returns an // IsValid checks whether msg is a valid message and returns an
// InvalidMessageError if it is not. // InvalidMessageError or FormatError if it is not.
func (msg *Message) IsValid() error { func (msg *Message) IsValid() error {
var b bytes.Buffer
return msg.EncodeTo(&b, nativeEndian)
}
func (msg *Message) validateHeader() error {
if msg.Flags & ^(FlagNoAutoStart|FlagNoReplyExpected|FlagAllowInteractiveAuthorization) != 0 { if msg.Flags & ^(FlagNoAutoStart|FlagNoReplyExpected|FlagAllowInteractiveAuthorization) != 0 {
return InvalidMessageError("invalid flags") return InvalidMessageError("invalid flags")
} }
@ -299,6 +335,7 @@ func (msg *Message) IsValid() error {
return InvalidMessageError("missing signature") return InvalidMessageError("missing signature")
} }
} }
return nil return nil
} }

View File

@ -63,7 +63,7 @@ type Method interface {
// any other decoding scheme. // any other decoding scheme.
type ArgumentDecoder interface { type ArgumentDecoder interface {
// To decode the arguments of a method the sender and message are // To decode the arguments of a method the sender and message are
// provided incase the semantics of the implementer provides access // provided in case the semantics of the implementer provides access
// to these as part of the method invocation. // to these as part of the method invocation.
DecodeArguments(conn *Conn, sender string, msg *Message, args []interface{}) ([]interface{}, error) DecodeArguments(conn *Conn, sender string, msg *Message, args []interface{}) ([]interface{}, error)
} }

View File

@ -34,7 +34,7 @@ type Signature struct {
func SignatureOf(vs ...interface{}) Signature { func SignatureOf(vs ...interface{}) Signature {
var s string var s string
for _, v := range vs { for _, v := range vs {
s += getSignature(reflect.TypeOf(v)) s += getSignature(reflect.TypeOf(v), &depthCounter{})
} }
return Signature{s} return Signature{s}
} }
@ -42,11 +42,19 @@ func SignatureOf(vs ...interface{}) Signature {
// SignatureOfType returns the signature of the given type. It panics if the // SignatureOfType returns the signature of the given type. It panics if the
// type is not representable in D-Bus. // type is not representable in D-Bus.
func SignatureOfType(t reflect.Type) Signature { func SignatureOfType(t reflect.Type) Signature {
return Signature{getSignature(t)} return Signature{getSignature(t, &depthCounter{})}
} }
// getSignature returns the signature of the given type and panics on unknown types. // getSignature returns the signature of the given type and panics on unknown types.
func getSignature(t reflect.Type) string { func getSignature(t reflect.Type, depth *depthCounter) (sig string) {
if !depth.Valid() {
panic("container nesting too deep")
}
defer func() {
if len(sig) > 255 {
panic("signature exceeds the length limitation")
}
}()
// handle simple types first // handle simple types first
switch t.Kind() { switch t.Kind() {
case reflect.Uint8: case reflect.Uint8:
@ -74,7 +82,7 @@ func getSignature(t reflect.Type) string {
case reflect.Float64: case reflect.Float64:
return "d" return "d"
case reflect.Ptr: case reflect.Ptr:
return getSignature(t.Elem()) return getSignature(t.Elem(), depth)
case reflect.String: case reflect.String:
if t == objectPathType { if t == objectPathType {
return "o" return "o"
@ -90,17 +98,20 @@ func getSignature(t reflect.Type) string {
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
field := t.Field(i) field := t.Field(i)
if field.PkgPath == "" && field.Tag.Get("dbus") != "-" { if field.PkgPath == "" && field.Tag.Get("dbus") != "-" {
s += getSignature(t.Field(i).Type) s += getSignature(t.Field(i).Type, depth.EnterStruct())
} }
} }
if len(s) == 0 {
panic(InvalidTypeError{t})
}
return "(" + s + ")" return "(" + s + ")"
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
return "a" + getSignature(t.Elem()) return "a" + getSignature(t.Elem(), depth.EnterArray())
case reflect.Map: case reflect.Map:
if !isKeyType(t.Key()) { if !isKeyType(t.Key()) {
panic(InvalidTypeError{t}) panic(InvalidTypeError{t})
} }
return "a{" + getSignature(t.Key()) + getSignature(t.Elem()) + "}" return "a{" + getSignature(t.Key(), depth.EnterArray().EnterDictEntry()) + getSignature(t.Elem(), depth.EnterArray().EnterDictEntry()) + "}"
case reflect.Interface: case reflect.Interface:
return "v" return "v"
} }
@ -118,7 +129,7 @@ func ParseSignature(s string) (sig Signature, err error) {
} }
sig.str = s sig.str = s
for err == nil && len(s) != 0 { for err == nil && len(s) != 0 {
err, s = validSingle(s, 0) err, s = validSingle(s, &depthCounter{})
} }
if err != nil { if err != nil {
sig = Signature{""} sig = Signature{""}
@ -144,7 +155,7 @@ func (s Signature) Empty() bool {
// Single returns whether the signature represents a single, complete type. // Single returns whether the signature represents a single, complete type.
func (s Signature) Single() bool { func (s Signature) Single() bool {
err, r := validSingle(s.str, 0) err, r := validSingle(s.str, &depthCounter{})
return err != nil && r == "" return err != nil && r == ""
} }
@ -164,15 +175,38 @@ func (e SignatureError) Error() string {
return fmt.Sprintf("dbus: invalid signature: %q (%s)", e.Sig, e.Reason) return fmt.Sprintf("dbus: invalid signature: %q (%s)", e.Sig, e.Reason)
} }
type depthCounter struct {
arrayDepth, structDepth, dictEntryDepth int
}
func (cnt *depthCounter) Valid() bool {
return cnt.arrayDepth <= 32 && cnt.structDepth <= 32 && cnt.dictEntryDepth <= 32
}
func (cnt depthCounter) EnterArray() *depthCounter {
cnt.arrayDepth++
return &cnt
}
func (cnt depthCounter) EnterStruct() *depthCounter {
cnt.structDepth++
return &cnt
}
func (cnt depthCounter) EnterDictEntry() *depthCounter {
cnt.dictEntryDepth++
return &cnt
}
// Try to read a single type from this string. If it was successful, err is nil // Try to read a single type from this string. If it was successful, err is nil
// and rem is the remaining unparsed part. Otherwise, err is a non-nil // and rem is the remaining unparsed part. Otherwise, err is a non-nil
// SignatureError and rem is "". depth is the current recursion depth which may // SignatureError and rem is "". depth is the current recursion depth which may
// not be greater than 64 and should be given as 0 on the first call. // not be greater than 64 and should be given as 0 on the first call.
func validSingle(s string, depth int) (err error, rem string) { func validSingle(s string, depth *depthCounter) (err error, rem string) {
if s == "" { if s == "" {
return SignatureError{Sig: s, Reason: "empty signature"}, "" return SignatureError{Sig: s, Reason: "empty signature"}, ""
} }
if depth > 64 { if !depth.Valid() {
return SignatureError{Sig: s, Reason: "container nesting too deep"}, "" return SignatureError{Sig: s, Reason: "container nesting too deep"}, ""
} }
switch s[0] { switch s[0] {
@ -187,10 +221,10 @@ func validSingle(s string, depth int) (err error, rem string) {
i++ i++
rem = s[i+1:] rem = s[i+1:]
s = s[2:i] s = s[2:i]
if err, _ = validSingle(s[:1], depth+1); err != nil { if err, _ = validSingle(s[:1], depth.EnterArray().EnterDictEntry()); err != nil {
return err, "" return err, ""
} }
err, nr := validSingle(s[1:], depth+1) err, nr := validSingle(s[1:], depth.EnterArray().EnterDictEntry())
if err != nil { if err != nil {
return err, "" return err, ""
} }
@ -199,7 +233,7 @@ func validSingle(s string, depth int) (err error, rem string) {
} }
return nil, rem return nil, rem
} }
return validSingle(s[1:], depth+1) return validSingle(s[1:], depth.EnterArray())
case '(': case '(':
i := findMatching(s, '(', ')') i := findMatching(s, '(', ')')
if i == -1 { if i == -1 {
@ -208,7 +242,7 @@ func validSingle(s string, depth int) (err error, rem string) {
rem = s[i+1:] rem = s[i+1:]
s = s[1:i] s = s[1:i]
for err == nil && s != "" { for err == nil && s != "" {
err, s = validSingle(s, depth+1) err, s = validSingle(s, depth.EnterStruct())
} }
if err != nil { if err != nil {
rem = "" rem = ""
@ -236,7 +270,7 @@ func findMatching(s string, left, right rune) int {
// typeFor returns the type of the given signature. It ignores any left over // typeFor returns the type of the given signature. It ignores any left over
// characters and panics if s doesn't start with a valid type signature. // characters and panics if s doesn't start with a valid type signature.
func typeFor(s string) (t reflect.Type) { func typeFor(s string) (t reflect.Type) {
err, _ := validSingle(s, 0) err, _ := validSingle(s, &depthCounter{})
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -41,10 +41,12 @@ func (t genericTransport) ReadMessage() (*Message, error) {
} }
func (t genericTransport) SendMessage(msg *Message) error { func (t genericTransport) SendMessage(msg *Message) error {
for _, v := range msg.Body { fds, err := msg.CountFds()
if _, ok := v.(UnixFD); ok { if err != nil {
return errors.New("dbus: unix fd passing not enabled") return err
} }
if fds != 0 {
return errors.New("dbus: unix fd passing not enabled")
} }
return msg.EncodeTo(t, nativeEndian) return msg.EncodeTo(t, nativeEndian)
} }

View File

@ -113,7 +113,7 @@ func (t *unixTransport) ReadMessage() (*Message, error) {
if _, err := io.ReadFull(t.rdr, headerdata[4:]); err != nil { if _, err := io.ReadFull(t.rdr, headerdata[4:]); err != nil {
return nil, err return nil, err
} }
dec := newDecoder(bytes.NewBuffer(headerdata), order) dec := newDecoder(bytes.NewBuffer(headerdata), order, make([]int, 0))
dec.pos = 12 dec.pos = 12
vs, err := dec.Decode(Signature{"a(yv)"}) vs, err := dec.Decode(Signature{"a(yv)"})
if err != nil { if err != nil {
@ -147,24 +147,22 @@ func (t *unixTransport) ReadMessage() (*Message, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
msg, err := DecodeMessage(bytes.NewBuffer(all)) msg, err := DecodeMessageWithFDs(bytes.NewBuffer(all), fds)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// substitute the values in the message body (which are indices for the // substitute the values in the message body (which are indices for the
// array receiver via OOB) with the actual values // array receiver via OOB) with the actual values
for i, v := range msg.Body { for i, v := range msg.Body {
switch v.(type) { switch index := v.(type) {
case UnixFDIndex: case UnixFDIndex:
j := v.(UnixFDIndex) if uint32(index) >= unixfds {
if uint32(j) >= unixfds {
return nil, InvalidMessageError("invalid index for unix fd") return nil, InvalidMessageError("invalid index for unix fd")
} }
msg.Body[i] = UnixFD(fds[j]) msg.Body[i] = UnixFD(fds[index])
case []UnixFDIndex: case []UnixFDIndex:
idxArray := v.([]UnixFDIndex) fdArray := make([]UnixFD, len(index))
fdArray := make([]UnixFD, len(idxArray)) for k, j := range index {
for k, j := range idxArray {
if uint32(j) >= unixfds { if uint32(j) >= unixfds {
return nil, InvalidMessageError("invalid index for unix fd") return nil, InvalidMessageError("invalid index for unix fd")
} }
@ -179,21 +177,21 @@ func (t *unixTransport) ReadMessage() (*Message, error) {
} }
func (t *unixTransport) SendMessage(msg *Message) error { func (t *unixTransport) SendMessage(msg *Message) error {
fds := make([]int, 0) fdcnt, err := msg.CountFds()
for i, v := range msg.Body { if err != nil {
if fd, ok := v.(UnixFD); ok { return err
msg.Body[i] = UnixFDIndex(len(fds))
fds = append(fds, int(fd))
}
} }
if len(fds) != 0 { if fdcnt != 0 {
if !t.hasUnixFDs { if !t.hasUnixFDs {
return errors.New("dbus: unix fd passing not enabled") return errors.New("dbus: unix fd passing not enabled")
} }
msg.Headers[FieldUnixFDs] = MakeVariant(uint32(len(fds))) msg.Headers[FieldUnixFDs] = MakeVariant(uint32(fdcnt))
oob := syscall.UnixRights(fds...)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
msg.EncodeTo(buf, nativeEndian) fds, err := msg.EncodeToWithFDs(buf, nativeEndian)
if err != nil {
return err
}
oob := syscall.UnixRights(fds...)
n, oobn, err := t.UnixConn.WriteMsgUnix(buf.Bytes(), oob, nil) n, oobn, err := t.UnixConn.WriteMsgUnix(buf.Bytes(), oob, nil)
if err != nil { if err != nil {
return err return err

View File

@ -0,0 +1,14 @@
package dbus
import "io"
func (t *unixTransport) SendNullByte() error {
n, _, err := t.UnixConn.WriteMsgUnix([]byte{0}, nil, nil)
if err != nil {
return err
}
if n != 1 {
return io.ErrShortWrite
}
return nil
}

6
vendor/github.com/godbus/dbus/v5/transport_zos.go generated vendored Normal file
View File

@ -0,0 +1,6 @@
package dbus
func (t *unixTransport) SendNullByte() error {
_, err := t.Write([]byte{0})
return err
}

View File

@ -49,7 +49,7 @@ func ParseVariant(s string, sig Signature) (Variant, error) {
} }
// format returns a formatted version of v and whether this string can be parsed // format returns a formatted version of v and whether this string can be parsed
// unambigously. // unambiguously.
func (v Variant) format() (string, bool) { func (v Variant) format() (string, bool) {
switch v.sig.str[0] { switch v.sig.str[0] {
case 'b', 'i': case 'b', 'i':

2
vendor/modules.txt vendored
View File

@ -74,7 +74,7 @@ github.com/d2g/dhcp4server/leasepool/memorypool
# github.com/fsnotify/fsnotify v1.4.9 # github.com/fsnotify/fsnotify v1.4.9
## explicit; go 1.13 ## explicit; go 1.13
github.com/fsnotify/fsnotify github.com/fsnotify/fsnotify
# github.com/godbus/dbus/v5 v5.0.4 # github.com/godbus/dbus/v5 v5.1.0
## explicit; go 1.12 ## explicit; go 1.12
github.com/godbus/dbus/v5 github.com/godbus/dbus/v5
# github.com/gogo/protobuf v1.3.2 # github.com/gogo/protobuf v1.3.2