-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofiles.go
150 lines (119 loc) · 4.28 KB
/
profiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
// Copyright (C) Felix Geyer <debfx@fobos.de>
package main
import (
"bytes"
"encoding/base64"
"fmt"
"os"
"path"
"strconv"
"strings"
"github.com/godbus/dbus/v5"
)
type jailProfile struct {
EnvVars map[string]string
Mounts []mount
Settings settingsStruct
}
func getX11Socket() (string, error) {
display := os.Getenv("DISPLAY")
if len(display) == 0 {
return "", fmt.Errorf("DISPLAY environment variable is not set")
}
if len(display) < 2 || display[0] != ':' {
return "", fmt.Errorf("DISPLAY environment variable is invalid (\"%s\")", display)
}
if _, err := strconv.Atoi(display[1:]); err != nil {
return "", fmt.Errorf("DISPLAY environment variable is invalid (\"%s\")", display)
}
socketPath := fmt.Sprintf("/tmp/.X11-unix/X%s", display[1:])
if _, err := os.Stat(socketPath); err != nil {
return "", err
}
return socketPath, nil
}
func getWaylandSocket() (string, error) {
socketPath := os.Getenv("WAYLAND_DISPLAY")
if len(socketPath) == 0 {
socketPath = "wayland-0"
}
if !strings.HasPrefix(socketPath, "/") {
runtimeDir, err := getUserRuntimeDir()
if err != nil {
return "", err
}
socketPath = fmt.Sprintf("%s/%s", runtimeDir, socketPath)
}
if _, err := os.Stat(socketPath); err != nil {
return "", err
}
return socketPath, nil
}
func getPortalDocDir() (string, error) {
conn, err := dbus.SessionBus()
if err != nil {
return "", fmt.Errorf("Failed to connect to session bus: %w", err)
}
defer conn.Close()
portalDocuments := conn.Object("org.freedesktop.portal.Documents", "/org/freedesktop/portal/documents")
var portalDirBytes []byte
err = portalDocuments.Call("org.freedesktop.portal.Documents.GetMountPoint", 0).Store(&portalDirBytes)
if err != nil {
return "", fmt.Errorf("Failed to get portal documents dir: %w", err)
}
// result is a byte-encoded zero-terminated string
portalDirBytesParts := bytes.Split(portalDirBytes, []byte("\x00"))
return string(portalDirBytesParts[0]), nil
}
func getProfile(name string, settings settingsStruct) (jailProfile, error) {
profile := jailProfile{
EnvVars: map[string]string{},
Mounts: []mount{},
}
switch name {
case "x11":
x11Socket, err := getX11Socket()
if err != nil {
return profile, err
}
profile.Mounts = append(profile.Mounts, mount{Path: "/tmp/.X11-unix/X0", Other: x11Socket, Type: mountTypeBindRw})
profile.EnvVars["DISPLAY"] = ":0"
case "wayland":
waylandSocket, err := getWaylandSocket()
if err != nil {
return profile, err
}
runtimeDir, err := getUserRuntimeDir()
if err != nil {
return profile, err
}
profile.Mounts = append(profile.Mounts, mount{Path: fmt.Sprintf("%s/wayland-0", runtimeDir), Other: waylandSocket, Type: mountTypeBindRw})
profile.EnvVars["WAYLAND_DISPLAY"] = "wayland-0"
case "flatpak":
if settings.Name == "" {
return profile, fmt.Errorf("flatpak profile requires a name config")
}
runtimeDir, err := getUserRuntimeDir()
if err != nil {
return profile, err
}
portalDir, err := getPortalDocDir()
if err != nil {
return profile, err
}
desktopFileContent := fmt.Sprintf("[Application]\nname=%s\n", settings.Name)
flatpakInfo := base64.StdEncoding.EncodeToString([]byte(desktopFileContent))
profile.Mounts = append(profile.Mounts, mount{Path: "/.flatpak-info", Other: flatpakInfo, Type: mountTypeFileData})
// compatbility with older flatpak
profile.Mounts = append(profile.Mounts, mount{Path: path.Join(runtimeDir, "flatpak-info"), Other: "/.flatpak-info", Type: mountTypeSymlink})
profile.Mounts = append(profile.Mounts, mount{Path: path.Join(runtimeDir, "doc"), Other: path.Join(portalDir, "by-app", settings.Name), Type: mountTypeBindRw})
profile.Mounts = append(profile.Mounts, mount{Path: "/usr/bin/xdg-email", Other: "/usr/libexec/flatpak-xdg-utils/xdg-email", Type: mountTypeBindRo})
profile.Mounts = append(profile.Mounts, mount{Path: "/usr/bin/xdg-open", Other: "/usr/libexec/flatpak-xdg-utils/xdg-open", Type: mountTypeBindRo})
profile.Settings.DbusCall = append(profile.Settings.DbusCall, "org.freedesktop.portal.*=*")
profile.Settings.DbusBroadcast = append(profile.Settings.DbusBroadcast, "org.freedesktop.portal.*=@/org/freedesktop/portal/*")
default:
return profile, fmt.Errorf("profile \"%s\" doesn't exist", name)
}
return profile, nil
}