-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathdaemon.go
468 lines (394 loc) · 10.9 KB
/
daemon.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2016 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 daemon
import (
"fmt"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
unix "syscall"
"time"
"github.com/coreos/go-systemd/activation"
"github.com/gorilla/mux"
"gopkg.in/tomb.v2"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/polkit"
)
// A Daemon listens for requests and routes them to the right command
type Daemon struct {
Version string
overlord *overlord.Overlord
snapdListener net.Listener
snapdServe *shutdownServer
snapListener net.Listener
snapServe *shutdownServer
tomb tomb.Tomb
router *mux.Router
// enableInternalInterfaceActions controls if adding and removing slots and plugs is allowed.
enableInternalInterfaceActions bool
}
// A ResponseFunc handles one of the individual verbs for a method
type ResponseFunc func(*Command, *http.Request, *auth.UserState) Response
// A Command routes a request to an individual per-verb ResponseFUnc
type Command struct {
Path string
//
GET ResponseFunc
PUT ResponseFunc
POST ResponseFunc
DELETE ResponseFunc
// can guest GET?
GuestOK bool
// can non-admin GET?
UserOK bool
// is this path accessible on the snapd-snap socket?
SnapOK bool
// can polkit grant access? set to polkit action ID if so
PolkitOK string
d *Daemon
}
var polkitCheckAuthorizationForPid = polkit.CheckAuthorizationForPid
func (c *Command) canAccess(r *http.Request, user *auth.UserState) bool {
if user != nil {
// Authenticated users do anything for now.
return true
}
isUser := false
pid, uid, err := ucrednetGet(r.RemoteAddr)
if err == nil {
isUser = true
} else if err != errNoID {
logger.Noticef("unexpected error when attempting to get UID: %s", err)
return false
} else if c.SnapOK {
return true
}
if r.Method == "GET" {
// Guest and user access restricted to GET requests
if c.GuestOK {
return true
}
if isUser && c.UserOK {
return true
}
}
// Remaining admin checks rely on identifying peer uid
if !isUser {
return false
}
if uid == 0 {
// Superuser does anything.
return true
}
if c.PolkitOK != "" {
var flags polkit.CheckFlags
allowHeader := r.Header.Get(client.AllowInteractionHeader)
if allowHeader != "" {
if allow, err := strconv.ParseBool(allowHeader); err != nil {
logger.Noticef("error parsing %s header: %s", client.AllowInteractionHeader, err)
} else if allow {
flags |= polkit.CheckAllowInteraction
}
}
if authorized, err := polkitCheckAuthorizationForPid(pid, c.PolkitOK, nil, flags); err == nil {
if authorized {
// polkit says user is authorised
return true
}
} else if err != polkit.ErrDismissed {
logger.Noticef("polkit error: %s", err)
}
}
return false
}
func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request) {
state := c.d.overlord.State()
state.Lock()
// TODO Look at the error and fail if there's an attempt to authenticate with invalid data.
user, _ := UserFromRequest(state, r)
state.Unlock()
if !c.canAccess(r, user) {
Unauthorized("access denied").ServeHTTP(w, r)
return
}
var rspf ResponseFunc
var rsp = MethodNotAllowed("method %q not allowed", r.Method)
switch r.Method {
case "GET":
rspf = c.GET
case "PUT":
rspf = c.PUT
case "POST":
rspf = c.POST
case "DELETE":
rspf = c.DELETE
}
if rspf != nil {
rsp = rspf(c, r, user)
}
rsp.ServeHTTP(w, r)
}
type wrappedWriter struct {
w http.ResponseWriter
s int
}
func (w *wrappedWriter) Header() http.Header {
return w.w.Header()
}
func (w *wrappedWriter) Write(bs []byte) (int, error) {
return w.w.Write(bs)
}
func (w *wrappedWriter) WriteHeader(s int) {
w.w.WriteHeader(s)
w.s = s
}
func (w *wrappedWriter) Flush() {
if f, ok := w.w.(http.Flusher); ok {
f.Flush()
}
}
func logit(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := &wrappedWriter{w: w}
t0 := time.Now()
handler.ServeHTTP(ww, r)
t := time.Now().Sub(t0)
url := r.URL.String()
if !strings.Contains(url, "/changes/") {
logger.Debugf("%s %s %s %s %d", r.RemoteAddr, r.Method, r.URL, t, ww.s)
}
})
}
// getListener tries to get a listener for the given socket path from
// the listener map, and if it fails it tries to set it up directly.
func getListener(socketPath string, listenerMap map[string]net.Listener) (net.Listener, error) {
if listener, ok := listenerMap[socketPath]; ok {
return listener, nil
}
if c, err := net.Dial("unix", socketPath); err == nil {
c.Close()
return nil, fmt.Errorf("socket %q already in use", socketPath)
}
if err := os.Remove(socketPath); err != nil && !os.IsNotExist(err) {
return nil, err
}
address, err := net.ResolveUnixAddr("unix", socketPath)
if err != nil {
return nil, err
}
runtime.LockOSThread()
oldmask := unix.Umask(0111)
listener, err := net.ListenUnix("unix", address)
unix.Umask(oldmask)
runtime.UnlockOSThread()
if err != nil {
return nil, err
}
logger.Debugf("socket %q was not activated; listening", socketPath)
return listener, nil
}
// Init sets up the Daemon's internal workings.
// Don't call more than once.
func (d *Daemon) Init() error {
listeners, err := activation.Listeners(false)
if err != nil {
return err
}
listenerMap := make(map[string]net.Listener, len(listeners))
for _, listener := range listeners {
listenerMap[listener.Addr().String()] = listener
}
// The SnapdSocket is required-- without it, die.
if listener, err := getListener(dirs.SnapdSocket, listenerMap); err == nil {
d.snapdListener = &ucrednetListener{listener}
} else {
return fmt.Errorf("when trying to listen on %s: %v", dirs.SnapdSocket, err)
}
if listener, err := getListener(dirs.SnapSocket, listenerMap); err == nil {
// Note that the SnapSocket listener does not use ucrednet. We use the lack
// of remote information as an indication that the request originated with
// this socket. This listener may also be nil if that socket wasn't among
// the listeners, so check it before using it.
d.snapListener = listener
} else {
logger.Debugf("cannot get listener for %q: %v", dirs.SnapSocket, err)
}
d.addRoutes()
logger.Noticef("started %v.", httputil.UserAgent())
return nil
}
func (d *Daemon) addRoutes() {
d.router = mux.NewRouter()
for _, c := range api {
c.d = d
d.router.Handle(c.Path, c).Name(c.Path)
}
// also maybe add a /favicon.ico handler...
d.router.NotFoundHandler = NotFound("not found")
}
var (
shutdownTimeout = 5 * time.Second
)
// shutdownServer supplements a http.Server with graceful shutdown.
// TODO: with go1.8 http.Server itself grows a graceful Shutdown method
type shutdownServer struct {
l net.Listener
httpSrv *http.Server
mu sync.Mutex
conns map[net.Conn]http.ConnState
shuttingDown bool
}
func newShutdownServer(l net.Listener, h http.Handler) *shutdownServer {
srv := &http.Server{
Handler: h,
}
ssrv := &shutdownServer{
l: l,
httpSrv: srv,
conns: make(map[net.Conn]http.ConnState),
}
srv.ConnState = ssrv.trackConn
return ssrv
}
func (srv *shutdownServer) Serve() error {
return srv.httpSrv.Serve(srv.l)
}
func (srv *shutdownServer) trackConn(conn net.Conn, state http.ConnState) {
srv.mu.Lock()
defer srv.mu.Unlock()
// we ignore hijacked connections, if we do things with websockets
// we'll need custom shutdown handling for them
if state == http.StateClosed || state == http.StateHijacked {
delete(srv.conns, conn)
return
}
if srv.shuttingDown && state == http.StateIdle {
conn.Close()
delete(srv.conns, conn)
return
}
srv.conns[conn] = state
}
func (srv *shutdownServer) finishShutdown() error {
toutC := time.After(shutdownTimeout)
srv.mu.Lock()
defer srv.mu.Unlock()
srv.shuttingDown = true
for conn, state := range srv.conns {
if state == http.StateIdle {
conn.Close()
delete(srv.conns, conn)
}
}
doWait := true
for doWait {
if len(srv.conns) == 0 {
return nil
}
srv.mu.Unlock()
select {
case <-time.After(200 * time.Millisecond):
case <-toutC:
doWait = false
}
srv.mu.Lock()
}
return fmt.Errorf("cannot gracefully finish, still active connections on %v after %v", srv.l.Addr(), shutdownTimeout)
}
var shutdownMsg = i18n.G("reboot scheduled to update the system - temporarily cancel with 'sudo shutdown -c'")
// Start the Daemon
func (d *Daemon) Start() {
// die when asked to restart (systemd should get us back up!)
d.overlord.SetRestartHandler(func(t state.RestartType) {
switch t {
case state.RestartDaemon:
d.tomb.Kill(nil)
case state.RestartSystem:
cmd := exec.Command("shutdown", "+10", "-r", shutdownMsg)
if out, err := cmd.CombinedOutput(); err != nil {
logger.Noticef("%s", osutil.OutputErr(out, err))
}
default:
logger.Noticef("internal error: restart handler called with unknown restart type: %v", t)
d.tomb.Kill(nil)
}
})
if d.snapListener != nil {
d.snapServe = newShutdownServer(d.snapListener, logit(d.router))
}
d.snapdServe = newShutdownServer(d.snapdListener, logit(d.router))
// the loop runs in its own goroutine
d.overlord.Loop()
d.tomb.Go(func() error {
if d.snapListener != nil {
d.tomb.Go(func() error {
if err := d.snapServe.Serve(); err != nil && d.tomb.Err() == tomb.ErrStillAlive {
return err
}
return nil
})
}
if err := d.snapdServe.Serve(); err != nil && d.tomb.Err() == tomb.ErrStillAlive {
return err
}
return nil
})
}
// Stop shuts down the Daemon
func (d *Daemon) Stop() error {
d.tomb.Kill(nil)
d.snapdListener.Close()
if d.snapListener != nil {
d.snapListener.Close()
}
d.tomb.Kill(d.snapdServe.finishShutdown())
if d.snapListener != nil {
d.tomb.Kill(d.snapServe.finishShutdown())
}
d.overlord.Stop()
return d.tomb.Wait()
}
// Dying is a tomb-ish thing
func (d *Daemon) Dying() <-chan struct{} {
return d.tomb.Dying()
}
// New Daemon
func New() (*Daemon, error) {
ovld, err := overlord.New()
if err != nil {
return nil, err
}
return &Daemon{
overlord: ovld,
// TODO: Decide when this should be disabled by default.
enableInternalInterfaceActions: true,
}, nil
}