This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
/
server.go
276 lines (237 loc) · 7.3 KB
/
server.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
// server.go - Katzenpost non-voting authority server.
// Copyright (C) 2017 Yawning Angel.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package server implements the Katzenpost non-voting authority server.
//
// The non-voting authority server is intended to be a stop gap for debugging
// and testing and is likely only suitable for very small networks where the
// lack of distributed trust and or quality of life features is a non-issue.
package server
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"sync"
"github.com/katzenpost/authority/nonvoting/server/config"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/core/crypto/eddsa"
"github.com/katzenpost/core/crypto/rand"
"github.com/katzenpost/core/log"
"gopkg.in/op/go-logging.v1"
)
// ErrGenerateOnly is the error returned when the server initialization
// terminates due to the `GenerateOnly` debug config option.
var ErrGenerateOnly = errors.New("server: GenerateOnly set")
// Server is a non-voting authority server instance.
type Server struct {
sync.WaitGroup
cfg *config.Config
identityKey *eddsa.PrivateKey
linkKey *ecdh.PrivateKey
logBackend *log.Backend
log *logging.Logger
state *state
listeners []net.Listener
fatalErrCh chan error
haltedCh chan interface{}
haltOnce sync.Once
}
func (s *Server) initDataDir() error {
const dirMode = os.ModeDir | 0700
d := s.cfg.Authority.DataDir
// Initialize the data directory, by ensuring that it exists (or can be
// created), and that it has the appropriate permissions.
if fi, err := os.Lstat(d); err != nil {
// Directory doesn't exist, create one.
if !os.IsNotExist(err) {
return fmt.Errorf("authority: failed to stat() DataDir: %v", err)
}
if err = os.Mkdir(d, dirMode); err != nil {
return fmt.Errorf("authority: failed to create DataDir: %v", err)
}
} else {
if !fi.IsDir() {
return fmt.Errorf("authority: DataDir '%v' is not a directory", d)
}
if fi.Mode() != dirMode {
return fmt.Errorf("authority: DataDir '%v' has invalid permissions '%v', should be '%v'", d, fi.Mode(), dirMode)
}
}
return nil
}
func (s *Server) initLogging() error {
p := s.cfg.Logging.File
if !s.cfg.Logging.Disable && s.cfg.Logging.File != "" {
if !filepath.IsAbs(p) {
p = filepath.Join(s.cfg.Authority.DataDir, p)
}
}
var err error
s.logBackend, err = log.New(p, s.cfg.Logging.Level, s.cfg.Logging.Disable)
if err == nil {
s.log = s.logBackend.GetLogger("authority")
}
return err
}
// IdentityKey returns the running Server's identity public key.
func (s *Server) IdentityKey() *eddsa.PublicKey {
return s.identityKey.PublicKey()
}
// RotateLog rotates the log file
// if logging to a file is enabled.
func (s *Server) RotateLog() {
err := s.logBackend.Rotate()
if err != nil {
s.fatalErrCh <- fmt.Errorf("failed to rotate log file, shutting down server")
}
s.log.Notice("Log rotated.")
}
// Wait waits till the server is terminated for any reason.
func (s *Server) Wait() {
<-s.haltedCh
}
// Shutdown cleanly shuts down a given Server instance.
func (s *Server) Shutdown() {
s.haltOnce.Do(func() { s.halt() })
}
func (s *Server) listenWorker(l net.Listener) {
addr := l.Addr()
s.log.Noticef("Listening on: %v", addr)
defer func() {
s.log.Noticef("Stopping listening on: %v", addr)
l.Close()
s.Done()
}()
for {
conn, err := l.Accept()
if err != nil {
if e, ok := err.(net.Error); ok && !e.Temporary() {
s.log.Errorf("Critical accept failure: %v", err)
return
}
continue
}
s.Add(1)
s.onConn(conn)
}
// NOTREACHED
}
func (s *Server) halt() {
s.log.Notice("Starting graceful shutdown.")
// Halt the listeners.
for idx, l := range s.listeners {
if l != nil {
l.Close()
}
s.listeners[idx] = nil
}
// Wait for all the connections to terminate.
s.WaitGroup.Wait()
// Halt the state worker.
if s.state != nil {
s.state.Halt()
s.state = nil
}
s.identityKey.Reset()
s.linkKey.Reset()
close(s.fatalErrCh)
s.log.Notice("Shutdown complete.")
close(s.haltedCh)
}
// New returns a new Server instance parameterized with the specific
// configuration.
func New(cfg *config.Config) (*Server, error) {
s := new(Server)
s.cfg = cfg
s.fatalErrCh = make(chan error)
s.haltedCh = make(chan interface{})
// Do the early initialization and bring up logging.
if err := s.initDataDir(); err != nil {
return nil, err
}
if err := s.initLogging(); err != nil {
return nil, err
}
s.log.Notice("Katzenpost is still pre-alpha. DO NOT DEPEND ON IT FOR STRONG SECURITY OR ANONYMITY.")
if s.cfg.Logging.Level == "DEBUG" {
s.log.Warning("Unsafe Debug logging is enabled.")
}
// Initialize the authority identity key.
var err error
if s.cfg.Debug.IdentityKey != nil {
s.log.Warning("IdentityKey should NOT be used for production deployments.")
s.identityKey = new(eddsa.PrivateKey)
s.identityKey.FromBytes(s.cfg.Debug.IdentityKey.Bytes())
} else {
identityPrivateKeyFile := filepath.Join(s.cfg.Authority.DataDir, "identity.private.pem")
identityPublicKeyFile := filepath.Join(s.cfg.Authority.DataDir, "identity.public.pem")
if s.identityKey, err = eddsa.Load(identityPrivateKeyFile, identityPublicKeyFile, rand.Reader); err != nil {
s.log.Errorf("Failed to initialize identity: %v", err)
return nil, err
}
}
s.linkKey = s.identityKey.ToECDH()
s.log.Noticef("Authority identity public key is: %s", s.identityKey.PublicKey())
if s.cfg.Debug.GenerateOnly {
return nil, ErrGenerateOnly
}
// Ensure that there are enough mixes and providers whitelisted to form
// a topology, assuming all of them post a descriptor.
if len(cfg.Providers) < 1 {
return nil, fmt.Errorf("server: No Providers specified in the config")
}
if len(cfg.Mixes) < cfg.Debug.Layers*cfg.Debug.MinNodesPerLayer {
return nil, fmt.Errorf("server: Insufficient nodes whitelisted, got %v , need %v", len(cfg.Mixes), cfg.Debug.Layers*cfg.Debug.MinNodesPerLayer)
}
// Past this point, failures need to call s.Shutdown() to do cleanup.
isOk := false
defer func() {
if !isOk {
s.Shutdown()
}
}()
// Start the fatal error watcher.
go func() {
err, ok := <-s.fatalErrCh
if !ok {
return
}
s.log.Warningf("Shutting down due to error: %v", err)
s.Shutdown()
}()
// Start up the state worker.
if s.state, err = newState(s); err != nil {
return nil, err
}
// Start up the listeners.
for _, v := range s.cfg.Authority.Addresses {
l, err := net.Listen("tcp", v)
if err != nil {
s.log.Errorf("Failed to start listener '%v': %v", v, err)
continue
}
s.listeners = append(s.listeners, l)
s.Add(1)
go s.listenWorker(l)
}
if len(s.listeners) == 0 {
s.log.Errorf("Failed to start all listeners.")
return nil, fmt.Errorf("authority: failed to start all listeners")
}
isOk = true
return s, nil
}