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

Can't connect to session bus under root #246

Closed
pymq opened this issue Jul 10, 2021 · 0 comments · Fixed by #247
Closed

Can't connect to session bus under root #246

pymq opened this issue Jul 10, 2021 · 0 comments · Fixed by #247

Comments

@pymq
Copy link
Contributor

pymq commented Jul 10, 2021

Reproducer

// +build linux

package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
	"syscall"

	"github.com/godbus/dbus/v5"
)

func main() {
	sudoUidStr := os.Getenv("SUDO_UID")
	sudoUid, sudoUiderr := strconv.Atoi(sudoUidStr)
	if sudoUiderr == nil {
		connectSessionDbus(sudoUid)
		return
	}

	xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR")
	userUid, err := strconv.Atoi(strings.TrimPrefix(xdgRuntimeDir, "/run/user/"))
	if err != nil {
		fmt.Printf("neither SUDO_UID ('%s') nor XDG_RUNTIME_DIR ('%s') were set\n", sudoUidStr, xdgRuntimeDir)
		return
	}

	connectSessionDbus(userUid)
}

// to access another user's dbus session under root we need DBUS_SESSION_BUS_ADDRESS env var to be set and connect under user's uid
// see https://stackoverflow.com/questions/6496847/access-another-users-d-bus-session
func connectSessionDbus(userUid int) {
	err := syscall.Seteuid(userUid)
	if err != nil {
		fmt.Printf("syscall.Seteuid(%d): %v\n", userUid, err)
		return
	}
	fmt.Printf("syscall.Seteuid(%d): %v\n", userUid, err) // REMOVE
	_, err = dbus.SessionBus()
	if err != nil {
		fmt.Println("connect session dbus:", err)
	}

	err = syscall.Seteuid(0)
	if err != nil {
		fmt.Println("syscall.Seteuid(0):", err)
	}
}

go build
sudo ./test

Output:

syscall.Seteuid(1000): <nil>
connect session dbus: dbus: authentication failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant