File: daemon_notify.go

package info (click to toggle)
snapd 2.49-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 36,432 kB
  • sloc: ansic: 12,125; sh: 8,453; python: 2,163; makefile: 1,284; exp: 173; xml: 22
file content (104 lines) | stat: -rw-r--r-- 3,556 bytes parent folder | download | duplicates (4)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2018 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package builtin

import (
	"fmt"
	"os"
	"strings"

	"github.com/snapcore/snapd/interfaces"
	"github.com/snapcore/snapd/interfaces/apparmor"
	apparmor_sandbox "github.com/snapcore/snapd/sandbox/apparmor"
)

const daemonNotifySummary = `allows sending daemon status changes to service manager`

const daemonNotifyBaseDeclarationSlots = `
  daemon-notify:
    allow-installation:
      slot-snap-type:
        - core
    deny-auto-connection: true
`

const daemonNotifyConnectedPlugAppArmorTemplate = `
# Allow sending notification messages to systemd through the notify socket
{{notify-socket-rule}},

# Allow using systemd-notify in shell scripts.
/{,usr/}bin/systemd-notify ixr,
`

type daemoNotifyInterface struct {
	commonInterface
}

var osGetenv = os.Getenv

func (iface *daemoNotifyInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
	// If the system has defined it, use NOTIFY_SOCKET from the environment. Note
	// this is safe because it is examined on snapd start and snaps cannot manipulate
	// the environment of snapd.
	notifySocket := osGetenv("NOTIFY_SOCKET")
	if notifySocket == "" {
		notifySocket = "/run/systemd/notify"
	}
	if !strings.HasPrefix(notifySocket, "/") && !strings.HasPrefix(notifySocket, "@") {
		// must be an absolute path or an abstract socket path
		return fmt.Errorf("cannot use %q as notify socket path: not absolute", notifySocket)
	}
	if err := apparmor_sandbox.ValidateNoAppArmorRegexp(notifySocket); err != nil {
		return fmt.Errorf("cannot use %q as notify socket path: %s", notifySocket, err)
	}

	var rule string

	switch {
	case strings.HasPrefix(notifySocket, "/"):
		rule = fmt.Sprintf(`"%s" w`, notifySocket)
	case strings.HasPrefix(notifySocket, "@/org/freedesktop/systemd1/notify/"):
		// special case for Ubuntu 14.04 where the manpage states that
		// /run/systemd/notify is used, but in fact the services get an
		// abstract socket path such as
		// @/org/freedesktop/systemd1/notify/13334051644891137417, the
		// last part changing with each reboot
		rule = `unix (connect, send) type=dgram peer=(label=unconfined,addr="@/org/freedesktop/systemd1/notify/[0-9]*")`
	case strings.HasPrefix(notifySocket, "@"):
		rule = fmt.Sprintf(`unix (connect, send) type=dgram peer=(label=unconfined,addr="%s")`, notifySocket)
	default:
		return fmt.Errorf("cannot use %q as notify socket path", notifySocket)
	}

	snippet := strings.Replace(daemonNotifyConnectedPlugAppArmorTemplate,
		"{{notify-socket-rule}}", rule, 1)
	spec.AddSnippet(snippet)
	return nil
}

func init() {
	registerIface(&daemoNotifyInterface{commonInterface: commonInterface{
		name:                 "daemon-notify",
		summary:              daemonNotifySummary,
		implicitOnCore:       true,
		implicitOnClassic:    true,
		baseDeclarationSlots: daemonNotifyBaseDeclarationSlots,
	}})
}