forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthority.go
94 lines (82 loc) · 2.62 KB
/
authority.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 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 polkit
import (
"errors"
"github.com/godbus/dbus"
)
type CheckFlags uint32
const (
CheckNone CheckFlags = 0x00
CheckAllowInteraction CheckFlags = 0x01
)
var (
ErrDismissed = errors.New("Authorization request dismissed")
ErrInteraction = errors.New("Authorization requires interaction")
)
func checkAuthorization(subject authSubject, actionId string, details map[string]string, flags CheckFlags) (bool, error) {
bus, err := dbus.SystemBus()
if err != nil {
return false, err
}
authority := bus.Object("org.freedesktop.PolicyKit1",
"/org/freedesktop/PolicyKit1/Authority")
var result authResult
err = authority.Call(
"org.freedesktop.PolicyKit1.Authority.CheckAuthorization", 0,
subject, actionId, details, flags, "").Store(&result)
if err != nil {
return false, err
}
if !result.IsAuthorized {
if result.IsChallenge {
err = ErrInteraction
} else if result.Details["polkit.dismissed"] != "" {
err = ErrDismissed
}
}
return result.IsAuthorized, err
}
// CheckAuthorization queries polkit to determine whether a process is
// authorized to perform an action.
func CheckAuthorization(pid int32, uid uint32, actionId string, details map[string]string, flags CheckFlags) (bool, error) {
subject := authSubject{
Kind: "unix-process",
Details: make(map[string]dbus.Variant),
}
subject.Details["pid"] = dbus.MakeVariant(uint32(pid)) // polkit is *wrong*!
startTime, err := getStartTimeForPid(pid)
if err != nil {
return false, err
}
// While discovering the pid's start time is racy, it isn't security
// relevant since it only impacts expiring the permission after
// process exit.
subject.Details["start-time"] = dbus.MakeVariant(startTime)
subject.Details["uid"] = dbus.MakeVariant(uid)
return checkAuthorization(subject, actionId, details, flags)
}
type authSubject struct {
Kind string
Details map[string]dbus.Variant
}
type authResult struct {
IsAuthorized bool
IsChallenge bool
Details map[string]string
}