-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
api.go
283 lines (242 loc) Β· 7.19 KB
/
api.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package core
import (
"context"
"encoding/hex"
"fmt"
"net/http"
"net/url"
"time"
"github.com/safing/portbase/api"
"github.com/safing/portbase/config"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/notifications"
"github.com/safing/portbase/rng"
"github.com/safing/portbase/utils/debug"
"github.com/safing/portmaster/compat"
"github.com/safing/portmaster/process"
"github.com/safing/portmaster/resolver"
"github.com/safing/portmaster/status"
"github.com/safing/portmaster/updates"
"github.com/safing/spn/captain"
)
func registerAPIEndpoints() error {
if err := api.RegisterEndpoint(api.Endpoint{
Path: "core/shutdown",
Write: api.PermitSelf,
BelongsTo: module,
ActionFunc: shutdown,
Name: "Shut Down Portmaster",
Description: "Shut down the Portmaster Core Service and all UI components.",
}); err != nil {
return err
}
if err := api.RegisterEndpoint(api.Endpoint{
Path: "core/restart",
Write: api.PermitAdmin,
BelongsTo: module,
ActionFunc: restart,
Name: "Restart Portmaster",
Description: "Restart the Portmaster Core Service.",
}); err != nil {
return err
}
if err := api.RegisterEndpoint(api.Endpoint{
Path: "debug/core",
Read: api.PermitAnyone,
BelongsTo: module,
DataFunc: debugInfo,
Name: "Get Debug Information",
Description: "Returns network debugging information, similar to debug/info, but with system status data.",
Parameters: []api.Parameter{{
Method: http.MethodGet,
Field: "style",
Value: "github",
Description: "Specify the formatting style. The default is simple markdown formatting.",
}},
}); err != nil {
return err
}
if err := api.RegisterEndpoint(api.Endpoint{
Path: "app/auth",
Read: api.PermitAnyone,
BelongsTo: module,
StructFunc: authorizeApp,
Name: "Request an authentication token with a given set of permissions. The user will be prompted to either authorize or deny the request. Used for external or third-party tool integrations.",
Parameters: []api.Parameter{
{
Method: http.MethodGet,
Field: "app-name",
Description: "The name of the application requesting access",
},
{
Method: http.MethodGet,
Field: "read",
Description: "The requested read permission",
},
{
Method: http.MethodGet,
Field: "write",
Description: "The requested write permission",
},
{
Method: http.MethodGet,
Field: "ttl",
Description: "The time-to-live for the new access token. Defaults to 24h",
},
},
}); err != nil {
return err
}
if err := api.RegisterEndpoint(api.Endpoint{
Path: "app/profile",
Read: api.PermitUser,
BelongsTo: module,
StructFunc: getMyProfile,
Name: "Get the ID of the calling profile",
}); err != nil {
return err
}
return nil
}
// shutdown shuts the Portmaster down.
func shutdown(_ *api.Request) (msg string, err error) {
log.Warning("core: user requested shutdown via action")
// Do not run in worker, as this would block itself here.
go modules.Shutdown() //nolint:errcheck
return "shutdown initiated", nil
}
// restart restarts the Portmaster.
func restart(_ *api.Request) (msg string, err error) {
log.Info("core: user requested restart via action")
// Let the updates module handle restarting.
updates.RestartNow()
return "restart initiated", nil
}
// debugInfo returns the debugging information for support requests.
func debugInfo(ar *api.Request) (data []byte, err error) {
// Create debug information helper.
di := new(debug.Info)
di.Style = ar.Request.URL.Query().Get("style")
// Add debug information.
di.AddVersionInfo()
di.AddPlatformInfo(ar.Context())
status.AddToDebugInfo(di)
config.AddToDebugInfo(di)
resolver.AddToDebugInfo(di)
captain.AddToDebugInfo(di)
compat.AddToDebugInfo(di)
di.AddLastReportedModuleError()
di.AddLastUnexpectedLogs()
di.AddGoroutineStack()
// Return data.
return di.Bytes(), nil
}
// getSavePermission returns the requested api.Permission from p.
// It only allows "user" and "admin" as external processes should
// never be able to request "self".
func getSavePermission(p string) api.Permission {
switch p {
case "user":
return api.PermitUser
case "admin":
return api.PermitAdmin
default:
return api.NotSupported
}
}
func getMyProfile(ar *api.Request) (interface{}, error) {
proc, err := process.GetProcessByRequestOrigin(ar)
if err != nil {
return nil, err
}
localProfile := proc.Profile().LocalProfile()
return map[string]interface{}{
"profile": localProfile.ID,
"source": localProfile.Source,
"name": localProfile.Name,
}, nil
}
func authorizeApp(ar *api.Request) (interface{}, error) {
appName := ar.Request.URL.Query().Get("app-name")
readPermStr := ar.Request.URL.Query().Get("read")
writePermStr := ar.Request.URL.Query().Get("write")
ttl := time.Hour * 24
if ttlStr := ar.Request.URL.Query().Get("ttl"); ttlStr != "" {
var err error
ttl, err = time.ParseDuration(ttlStr)
if err != nil {
return nil, err
}
}
// convert the requested read and write permissions to their api.Permission
// value. This ensures only "user" or "admin" permissions can be requested.
if getSavePermission(readPermStr) <= api.NotSupported {
return nil, fmt.Errorf("invalid read permission")
}
if getSavePermission(writePermStr) <= api.NotSupported {
return nil, fmt.Errorf("invalid read permission")
}
proc, err := process.GetProcessByRequestOrigin(ar)
if err != nil {
return nil, fmt.Errorf("failed to identify requesting process: %w", err)
}
n := notifications.Notification{
Type: notifications.Prompt,
EventID: "core:authorize-app-" + time.Now().String(),
Title: "An app requests access to the Portmaster",
Message: "Allow " + appName + " (" + proc.Profile().LocalProfile().Name + ") to query and modify the Portmaster?\n\nBinary: " + proc.Path,
ShowOnSystem: true,
Expires: time.Now().Add(time.Minute).UnixNano(),
AvailableActions: []*notifications.Action{
{
ID: "allow",
Text: "Authorize",
},
{
ID: "deny",
Text: "Deny",
},
},
}
ch := make(chan string)
validUntil := time.Now().Add(ttl)
n.SetActionFunction(func(ctx context.Context, n *notifications.Notification) error {
n.Lock()
defer n.Unlock()
if n.SelectedActionID != "allow" {
close(ch)
return nil
}
keys := config.Concurrent.GetAsStringArray(api.CfgAPIKeys, []string{})()
newKeyData, err := rng.Bytes(8)
if err != nil {
return err
}
newKeyHex := hex.EncodeToString(newKeyData)
query := url.Values{
"read": []string{readPermStr},
"write": []string{writePermStr},
"expires": []string{validUntil.Format(time.RFC3339)},
}
keys = append(keys, fmt.Sprintf("%s?%s", newKeyHex, query.Encode()))
if err := config.SetConfigOption(api.CfgAPIKeys, keys); err != nil {
return err
}
ch <- newKeyHex
return nil
})
n.Save()
select {
case key := <-ch:
if len(key) == 0 {
return nil, fmt.Errorf("access denied")
}
return map[string]interface{}{
"key": key,
"validUntil": validUntil,
}, nil
case <-ar.Context().Done():
return nil, fmt.Errorf("timeout")
}
}