]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cfg.go
-maxonlinetime option
[nncp.git] / src / cypherpunks.ru / nncp / cfg.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package nncp
20
21 import (
22         "errors"
23         "os"
24         "path"
25
26         "github.com/gorhill/cronexpr"
27         "golang.org/x/crypto/ed25519"
28         "gopkg.in/yaml.v2"
29 )
30
31 const (
32         CfgPathEnv = "NNCPCFG"
33 )
34
35 var (
36         DefaultCfgPath      string = "/usr/local/etc/nncp.yaml"
37         DefaultSendmailPath string = "/usr/sbin/sendmail"
38 )
39
40 type NodeYAML struct {
41         Id       string
42         ExchPub  string
43         SignPub  string
44         NoisePub *string `noisepub,omitempty`
45         Sendmail []string
46         Incoming *string    `incoming,omitempty`
47         Freq     *string    `freq,omitempty`
48         Via      []string   `via,omitempty`
49         Calls    []CallYAML `calls,omitempty`
50
51         Addrs map[string]string `addrs,omitempty`
52
53         OnlineDeadline *uint `onlinedeadline,omitempty`
54         MaxOnlineTime  *uint `maxonlinetime,omitempty`
55 }
56
57 type CallYAML struct {
58         Cron           string
59         Nice           *int    `nice,omitempty`
60         Xx             *string `xx,omitempty`
61         Addr           *string `addr,omitempty`
62         OnlineDeadline *uint   `onlinedeadline,omitempty`
63         MaxOnlineTime  *uint   `maxonlinetime,omitempty`
64 }
65
66 type NodeOurYAML struct {
67         Id       string
68         ExchPub  string
69         ExchPrv  string
70         SignPub  string
71         SignPrv  string
72         NoisePrv string
73         NoisePub string
74 }
75
76 type FromToYAML struct {
77         From string
78         To   string
79 }
80
81 type NotifyYAML struct {
82         File *FromToYAML `file,omitempty`
83         Freq *FromToYAML `freq,omitempty`
84 }
85
86 type CfgYAML struct {
87         Self  NodeOurYAML
88         Neigh map[string]NodeYAML
89
90         Spool  string
91         Log    string
92         Notify *NotifyYAML `notify,omitempty`
93 }
94
95 func NewNode(name string, yml NodeYAML) (*Node, error) {
96         nodeId, err := NodeIdFromString(yml.Id)
97         if err != nil {
98                 return nil, err
99         }
100
101         exchPub, err := FromBase32(yml.ExchPub)
102         if err != nil {
103                 return nil, err
104         }
105         if len(exchPub) != 32 {
106                 return nil, errors.New("Invalid exchPub size")
107         }
108
109         signPub, err := FromBase32(yml.SignPub)
110         if err != nil {
111                 return nil, err
112         }
113         if len(signPub) != ed25519.PublicKeySize {
114                 return nil, errors.New("Invalid signPub size")
115         }
116
117         var noisePub []byte
118         if yml.NoisePub != nil {
119                 noisePub, err = FromBase32(*yml.NoisePub)
120                 if err != nil {
121                         return nil, err
122                 }
123                 if len(noisePub) != 32 {
124                         return nil, errors.New("Invalid noisePub size")
125                 }
126         }
127
128         var incoming *string
129         if yml.Incoming != nil {
130                 inc := path.Clean(*yml.Incoming)
131                 if !path.IsAbs(inc) {
132                         return nil, errors.New("Incoming path must be absolute")
133                 }
134                 incoming = &inc
135         }
136
137         var freq *string
138         if yml.Freq != nil {
139                 fr := path.Clean(*yml.Freq)
140                 if !path.IsAbs(fr) {
141                         return nil, errors.New("Freq path must be absolute")
142                 }
143                 freq = &fr
144         }
145
146         defOnlineDeadline := uint(DefaultDeadline)
147         if yml.OnlineDeadline != nil {
148                 if *yml.OnlineDeadline <= 0 {
149                         return nil, errors.New("OnlineDeadline must be at least 1 second")
150                 }
151                 defOnlineDeadline = *yml.OnlineDeadline
152         }
153         var defMaxOnlineTime uint
154         if yml.MaxOnlineTime != nil {
155                 defMaxOnlineTime = *yml.MaxOnlineTime
156         }
157
158         var calls []*Call
159         for _, callYml := range yml.Calls {
160                 expr, err := cronexpr.Parse(callYml.Cron)
161                 if err != nil {
162                         return nil, err
163                 }
164                 nice := uint8(255)
165                 if callYml.Nice != nil {
166                         if *callYml.Nice < 1 || *callYml.Nice > 255 {
167                                 return nil, errors.New("Nice must be between 1 and 255")
168                         }
169                         nice = uint8(*callYml.Nice)
170                 }
171                 var xx TRxTx
172                 if callYml.Xx != nil {
173                         switch *callYml.Xx {
174                         case "rx":
175                                 xx = TRx
176                         case "tx":
177                                 xx = TTx
178                         default:
179                                 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
180                         }
181                 }
182                 var addr *string
183                 if callYml.Addr != nil {
184                         if a, exists := yml.Addrs[*callYml.Addr]; exists {
185                                 addr = &a
186                         } else {
187                                 addr = callYml.Addr
188                         }
189                 }
190                 onlineDeadline := defOnlineDeadline
191                 if callYml.OnlineDeadline != nil {
192                         if *yml.OnlineDeadline <= 0 {
193                                 return nil, errors.New("OnlineDeadline must be at least 1 second")
194                         }
195                         onlineDeadline = *callYml.OnlineDeadline
196                 }
197                 var maxOnlineTime uint
198                 if callYml.MaxOnlineTime != nil {
199                         maxOnlineTime = *callYml.MaxOnlineTime
200                 }
201                 calls = append(calls, &Call{
202                         Cron:           expr,
203                         Nice:           nice,
204                         Xx:             &xx,
205                         Addr:           addr,
206                         OnlineDeadline: onlineDeadline,
207                         MaxOnlineTime:  maxOnlineTime,
208                 })
209         }
210
211         node := Node{
212                 Name:           name,
213                 Id:             nodeId,
214                 ExchPub:        new([32]byte),
215                 SignPub:        ed25519.PublicKey(signPub),
216                 Sendmail:       yml.Sendmail,
217                 Incoming:       incoming,
218                 Freq:           freq,
219                 Calls:          calls,
220                 Addrs:          yml.Addrs,
221                 OnlineDeadline: defOnlineDeadline,
222                 MaxOnlineTime:  defMaxOnlineTime,
223         }
224         copy(node.ExchPub[:], exchPub)
225         if len(noisePub) > 0 {
226                 node.NoisePub = new([32]byte)
227                 copy(node.NoisePub[:], noisePub)
228         }
229         return &node, nil
230 }
231
232 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
233         id, err := NodeIdFromString(yml.Id)
234         if err != nil {
235                 return nil, err
236         }
237
238         exchPub, err := FromBase32(yml.ExchPub)
239         if err != nil {
240                 return nil, err
241         }
242         if len(exchPub) != 32 {
243                 return nil, errors.New("Invalid exchPub size")
244         }
245
246         exchPrv, err := FromBase32(yml.ExchPrv)
247         if err != nil {
248                 return nil, err
249         }
250         if len(exchPrv) != 32 {
251                 return nil, errors.New("Invalid exchPrv size")
252         }
253
254         signPub, err := FromBase32(yml.SignPub)
255         if err != nil {
256                 return nil, err
257         }
258         if len(signPub) != ed25519.PublicKeySize {
259                 return nil, errors.New("Invalid signPub size")
260         }
261
262         signPrv, err := FromBase32(yml.SignPrv)
263         if err != nil {
264                 return nil, err
265         }
266         if len(signPrv) != ed25519.PrivateKeySize {
267                 return nil, errors.New("Invalid signPrv size")
268         }
269
270         noisePub, err := FromBase32(yml.NoisePub)
271         if err != nil {
272                 return nil, err
273         }
274         if len(noisePub) != 32 {
275                 return nil, errors.New("Invalid noisePub size")
276         }
277
278         noisePrv, err := FromBase32(yml.NoisePrv)
279         if err != nil {
280                 return nil, err
281         }
282         if len(noisePrv) != 32 {
283                 return nil, errors.New("Invalid noisePrv size")
284         }
285
286         node := NodeOur{
287                 Id:       id,
288                 ExchPub:  new([32]byte),
289                 ExchPrv:  new([32]byte),
290                 SignPub:  ed25519.PublicKey(signPub),
291                 SignPrv:  ed25519.PrivateKey(signPrv),
292                 NoisePub: new([32]byte),
293                 NoisePrv: new([32]byte),
294         }
295         copy(node.ExchPub[:], exchPub)
296         copy(node.ExchPrv[:], exchPrv)
297         copy(node.NoisePub[:], noisePub)
298         copy(node.NoisePrv[:], noisePrv)
299         return &node, nil
300 }
301
302 func (nodeOur *NodeOur) ToYAML() string {
303         yml := NodeOurYAML{
304                 Id:       nodeOur.Id.String(),
305                 ExchPub:  ToBase32(nodeOur.ExchPub[:]),
306                 ExchPrv:  ToBase32(nodeOur.ExchPrv[:]),
307                 SignPub:  ToBase32(nodeOur.SignPub[:]),
308                 SignPrv:  ToBase32(nodeOur.SignPrv[:]),
309                 NoisePub: ToBase32(nodeOur.NoisePub[:]),
310                 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
311         }
312         raw, err := yaml.Marshal(&yml)
313         if err != nil {
314                 panic(err)
315         }
316         return string(raw)
317 }
318
319 func CfgParse(data []byte) (*Ctx, error) {
320         var cfgYAML CfgYAML
321         err := yaml.Unmarshal(data, &cfgYAML)
322         if err != nil {
323                 return nil, err
324         }
325         self, err := NewNodeOur(cfgYAML.Self)
326         if err != nil {
327                 return nil, err
328         }
329         spoolPath := path.Clean(cfgYAML.Spool)
330         if !path.IsAbs(spoolPath) {
331                 return nil, errors.New("Spool path must be absolute")
332         }
333         logPath := path.Clean(cfgYAML.Log)
334         if !path.IsAbs(logPath) {
335                 return nil, errors.New("Log path must be absolute")
336         }
337         ctx := Ctx{
338                 Spool:   spoolPath,
339                 LogPath: logPath,
340                 Self:    self,
341                 Neigh:   make(map[NodeId]*Node, len(cfgYAML.Neigh)),
342                 Alias:   make(map[string]*NodeId),
343         }
344         if cfgYAML.Notify != nil {
345                 if cfgYAML.Notify.File != nil {
346                         ctx.NotifyFile = cfgYAML.Notify.File
347                 }
348                 if cfgYAML.Notify.Freq != nil {
349                         ctx.NotifyFreq = cfgYAML.Notify.Freq
350                 }
351         }
352         vias := make(map[NodeId][]string)
353         for name, neighYAML := range cfgYAML.Neigh {
354                 neigh, err := NewNode(name, neighYAML)
355                 if err != nil {
356                         return nil, err
357                 }
358                 ctx.Neigh[*neigh.Id] = neigh
359                 if _, already := ctx.Alias[name]; already {
360                         return nil, errors.New("Node names conflict")
361                 }
362                 ctx.Alias[name] = neigh.Id
363                 vias[*neigh.Id] = neighYAML.Via
364         }
365         for neighId, viasRaw := range vias {
366                 for _, viaRaw := range viasRaw {
367                         foundNodeId, err := ctx.FindNode(viaRaw)
368                         if err != nil {
369                                 return nil, err
370                         }
371                         ctx.Neigh[neighId].Via = append(
372                                 ctx.Neigh[neighId].Via,
373                                 foundNodeId.Id,
374                         )
375                 }
376         }
377         return &ctx, nil
378 }
379
380 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
381         p = os.Getenv(CfgPathEnv)
382         if p == "" {
383                 p = *cmdlineFlag
384         }
385         return
386 }