aboutsummaryrefslogtreecommitdiffhomepage
path: root/system/wayland.go
blob: a485b3f2a558618661c347c5cbf3fe8853793c68 (plain)
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
package system

import (
	"errors"
	"fmt"
	"os"

	"git.gensokyo.uk/security/hakurei/system/acl"
	"git.gensokyo.uk/security/hakurei/system/wayland"
)

// Wayland sets up a wayland socket with a security context attached.
func (sys *I) Wayland(syncFd **os.File, dst, src, appID, instanceID string) *I {
	sys.lock.Lock()
	defer sys.lock.Unlock()

	sys.ops = append(sys.ops, &Wayland{syncFd, dst, src, appID, instanceID, wayland.Conn{}})

	return sys
}

type Wayland struct {
	sync              **os.File
	dst, src          string
	appID, instanceID string

	conn wayland.Conn
}

func (w *Wayland) Type() Enablement { return Process }

func (w *Wayland) apply(sys *I) error {
	if w.sync == nil {
		// this is a misuse of the API; do not return an error message
		return errors.New("invalid sync")
	}

	// the Wayland op is not repeatable
	if *w.sync != nil {
		// this is a misuse of the API; do not return an error message
		return errors.New("attempted to attach multiple wayland sockets")
	}

	if err := w.conn.Attach(w.src); err != nil {
		// make console output less nasty
		if errors.Is(err, os.ErrNotExist) {
			err = os.ErrNotExist
		}
		return wrapErrSuffix(err,
			fmt.Sprintf("cannot attach to wayland on %q:", w.src))
	} else {
		msg.Verbosef("wayland attached on %q", w.src)
	}

	if sp, err := w.conn.Bind(w.dst, w.appID, w.instanceID); err != nil {
		return wrapErrSuffix(err,
			fmt.Sprintf("cannot bind to socket on %q:", w.dst))
	} else {
		*w.sync = sp
		msg.Verbosef("wayland listening on %q", w.dst)
		return wrapErrSuffix(errors.Join(os.Chmod(w.dst, 0), acl.Update(w.dst, sys.uid, acl.Read, acl.Write, acl.Execute)),
			fmt.Sprintf("cannot chmod socket on %q:", w.dst))
	}
}

func (w *Wayland) revert(_ *I, ec *Criteria) error {
	if ec.hasType(w) {
		msg.Verbosef("removing wayland socket on %q", w.dst)
		if err := os.Remove(w.dst); err != nil && !errors.Is(err, os.ErrNotExist) {
			return err
		}

		msg.Verbosef("detaching from wayland on %q", w.src)
		return wrapErrSuffix(w.conn.Close(),
			fmt.Sprintf("cannot detach from wayland on %q:", w.src))
	} else {
		msg.Verbosef("skipping wayland cleanup on %q", w.dst)
		return nil
	}
}

func (w *Wayland) Is(o Op) bool {
	w0, ok := o.(*Wayland)
	return ok && w.dst == w0.dst && w.src == w0.src &&
		w.appID == w0.appID && w.instanceID == w0.instanceID
}

func (w *Wayland) Path() string   { return w.dst }
func (w *Wayland) String() string { return fmt.Sprintf("wayland socket at %q", w.dst) }