]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cfg.go
nncp-caller command
[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 *int `onlinedeadline,omitempty`
54 }
55
56 type CallYAML struct {
57         Cron           string
58         Nice           *int    `nice,omitempty`
59         Xx             *string `xx,omitempty`
60         Addr           *string `addr,omitempty`
61         OnlineDeadline *int    `onlinedeadline,omitempty`
62 }
63
64 type NodeOurYAML struct {
65         Id       string
66         ExchPub  string
67         ExchPrv  string
68         SignPub  string
69         SignPrv  string
70         NoisePrv string
71         NoisePub string
72 }
73
74 type FromToYAML struct {
75         From string
76         To   string
77 }
78
79 type NotifyYAML struct {
80         File *FromToYAML `file,omitempty`
81         Freq *FromToYAML `freq,omitempty`
82 }
83
84 type CfgYAML struct {
85         Self  NodeOurYAML
86         Neigh map[string]NodeYAML
87
88         Spool  string
89         Log    string
90         Notify *NotifyYAML `notify,omitempty`
91 }
92
93 func NewNode(name string, yml NodeYAML) (*Node, error) {
94         nodeId, err := NodeIdFromString(yml.Id)
95         if err != nil {
96                 return nil, err
97         }
98
99         exchPub, err := FromBase32(yml.ExchPub)
100         if err != nil {
101                 return nil, err
102         }
103         if len(exchPub) != 32 {
104                 return nil, errors.New("Invalid exchPub size")
105         }
106
107         signPub, err := FromBase32(yml.SignPub)
108         if err != nil {
109                 return nil, err
110         }
111         if len(signPub) != ed25519.PublicKeySize {
112                 return nil, errors.New("Invalid signPub size")
113         }
114
115         var noisePub []byte
116         if yml.NoisePub != nil {
117                 noisePub, err = FromBase32(*yml.NoisePub)
118                 if err != nil {
119                         return nil, err
120                 }
121                 if len(noisePub) != 32 {
122                         return nil, errors.New("Invalid noisePub size")
123                 }
124         }
125
126         var incoming *string
127         if yml.Incoming != nil {
128                 inc := path.Clean(*yml.Incoming)
129                 if !path.IsAbs(inc) {
130                         return nil, errors.New("Incoming path must be absolute")
131                 }
132                 incoming = &inc
133         }
134
135         var freq *string
136         if yml.Freq != nil {
137                 fr := path.Clean(*yml.Freq)
138                 if !path.IsAbs(fr) {
139                         return nil, errors.New("Freq path must be absolute")
140                 }
141                 freq = &fr
142         }
143
144         defOnlineDeadline := int(DefaultDeadline)
145         if yml.OnlineDeadline != nil {
146                 if *yml.OnlineDeadline <= 0 {
147                         return nil, errors.New("OnlineDeadline must be at least 1 second")
148                 }
149                 defOnlineDeadline = *yml.OnlineDeadline
150         }
151
152         var calls []*Call
153         for _, callYml := range yml.Calls {
154                 expr, err := cronexpr.Parse(callYml.Cron)
155                 if err != nil {
156                         return nil, err
157                 }
158                 nice := uint8(255)
159                 if callYml.Nice != nil {
160                         if *callYml.Nice < 1 || *callYml.Nice > 255 {
161                                 return nil, errors.New("Nice must be between 1 and 255")
162                         }
163                         nice = uint8(*callYml.Nice)
164                 }
165                 var xx TRxTx
166                 if callYml.Xx != nil {
167                         switch *callYml.Xx {
168                         case "rx":
169                                 xx = TRx
170                         case "tx":
171                                 xx = TTx
172                         default:
173                                 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
174                         }
175                 }
176                 var addr *string
177                 if callYml.Addr != nil {
178                         if a, exists := yml.Addrs[*callYml.Addr]; exists {
179                                 addr = &a
180                         } else {
181                                 addr = callYml.Addr
182                         }
183                 }
184                 onlineDeadline := defOnlineDeadline
185                 if callYml.OnlineDeadline != nil {
186                         if *yml.OnlineDeadline <= 0 {
187                                 return nil, errors.New("OnlineDeadline must be at least 1 second")
188                         }
189                         onlineDeadline = *callYml.OnlineDeadline
190                 }
191                 calls = append(calls, &Call{
192                         Cron:           expr,
193                         Nice:           nice,
194                         Xx:             &xx,
195                         Addr:           addr,
196                         OnlineDeadline: onlineDeadline,
197                 })
198         }
199
200         node := Node{
201                 Name:           name,
202                 Id:             nodeId,
203                 ExchPub:        new([32]byte),
204                 SignPub:        ed25519.PublicKey(signPub),
205                 Sendmail:       yml.Sendmail,
206                 Incoming:       incoming,
207                 Freq:           freq,
208                 Calls:          calls,
209                 Addrs:          yml.Addrs,
210                 OnlineDeadline: defOnlineDeadline,
211         }
212         copy(node.ExchPub[:], exchPub)
213         if len(noisePub) > 0 {
214                 node.NoisePub = new([32]byte)
215                 copy(node.NoisePub[:], noisePub)
216         }
217         return &node, nil
218 }
219
220 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
221         id, err := NodeIdFromString(yml.Id)
222         if err != nil {
223                 return nil, err
224         }
225
226         exchPub, err := FromBase32(yml.ExchPub)
227         if err != nil {
228                 return nil, err
229         }
230         if len(exchPub) != 32 {
231                 return nil, errors.New("Invalid exchPub size")
232         }
233
234         exchPrv, err := FromBase32(yml.ExchPrv)
235         if err != nil {
236                 return nil, err
237         }
238         if len(exchPrv) != 32 {
239                 return nil, errors.New("Invalid exchPrv size")
240         }
241
242         signPub, err := FromBase32(yml.SignPub)
243         if err != nil {
244                 return nil, err
245         }
246         if len(signPub) != ed25519.PublicKeySize {
247                 return nil, errors.New("Invalid signPub size")
248         }
249
250         signPrv, err := FromBase32(yml.SignPrv)
251         if err != nil {
252                 return nil, err
253         }
254         if len(signPrv) != ed25519.PrivateKeySize {
255                 return nil, errors.New("Invalid signPrv size")
256         }
257
258         noisePub, err := FromBase32(yml.NoisePub)
259         if err != nil {
260                 return nil, err
261         }
262         if len(noisePub) != 32 {
263                 return nil, errors.New("Invalid noisePub size")
264         }
265
266         noisePrv, err := FromBase32(yml.NoisePrv)
267         if err != nil {
268                 return nil, err
269         }
270         if len(noisePrv) != 32 {
271                 return nil, errors.New("Invalid noisePrv size")
272         }
273
274         node := NodeOur{
275                 Id:       id,
276                 ExchPub:  new([32]byte),
277                 ExchPrv:  new([32]byte),
278                 SignPub:  ed25519.PublicKey(signPub),
279                 SignPrv:  ed25519.PrivateKey(signPrv),
280                 NoisePub: new([32]byte),
281                 NoisePrv: new([32]byte),
282         }
283         copy(node.ExchPub[:], exchPub)
284         copy(node.ExchPrv[:], exchPrv)
285         copy(node.NoisePub[:], noisePub)
286         copy(node.NoisePrv[:], noisePrv)
287         return &node, nil
288 }
289
290 func (nodeOur *NodeOur) ToYAML() string {
291         yml := NodeOurYAML{
292                 Id:       nodeOur.Id.String(),
293                 ExchPub:  ToBase32(nodeOur.ExchPub[:]),
294                 ExchPrv:  ToBase32(nodeOur.ExchPrv[:]),
295                 SignPub:  ToBase32(nodeOur.SignPub[:]),
296                 SignPrv:  ToBase32(nodeOur.SignPrv[:]),
297                 NoisePub: ToBase32(nodeOur.NoisePub[:]),
298                 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
299         }
300         raw, err := yaml.Marshal(&yml)
301         if err != nil {
302                 panic(err)
303         }
304         return string(raw)
305 }
306
307 func CfgParse(data []byte) (*Ctx, error) {
308         var cfgYAML CfgYAML
309         err := yaml.Unmarshal(data, &cfgYAML)
310         if err != nil {
311                 return nil, err
312         }
313         self, err := NewNodeOur(cfgYAML.Self)
314         if err != nil {
315                 return nil, err
316         }
317         spoolPath := path.Clean(cfgYAML.Spool)
318         if !path.IsAbs(spoolPath) {
319                 return nil, errors.New("Spool path must be absolute")
320         }
321         logPath := path.Clean(cfgYAML.Log)
322         if !path.IsAbs(logPath) {
323                 return nil, errors.New("Log path must be absolute")
324         }
325         ctx := Ctx{
326                 Spool:   spoolPath,
327                 LogPath: logPath,
328                 Self:    self,
329                 Neigh:   make(map[NodeId]*Node, len(cfgYAML.Neigh)),
330                 Alias:   make(map[string]*NodeId),
331         }
332         if cfgYAML.Notify != nil {
333                 if cfgYAML.Notify.File != nil {
334                         ctx.NotifyFile = cfgYAML.Notify.File
335                 }
336                 if cfgYAML.Notify.Freq != nil {
337                         ctx.NotifyFreq = cfgYAML.Notify.Freq
338                 }
339         }
340         vias := make(map[NodeId][]string)
341         for name, neighYAML := range cfgYAML.Neigh {
342                 neigh, err := NewNode(name, neighYAML)
343                 if err != nil {
344                         return nil, err
345                 }
346                 ctx.Neigh[*neigh.Id] = neigh
347                 if _, already := ctx.Alias[name]; already {
348                         return nil, errors.New("Node names conflict")
349                 }
350                 ctx.Alias[name] = neigh.Id
351                 vias[*neigh.Id] = neighYAML.Via
352         }
353         for neighId, viasRaw := range vias {
354                 for _, viaRaw := range viasRaw {
355                         foundNodeId, err := ctx.FindNode(viaRaw)
356                         if err != nil {
357                                 return nil, err
358                         }
359                         ctx.Neigh[neighId].Via = append(
360                                 ctx.Neigh[neighId].Via,
361                                 foundNodeId.Id,
362                         )
363                 }
364         }
365         return &ctx, nil
366 }
367
368 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
369         p = os.Getenv(CfgPathEnv)
370         if p == "" {
371                 p = *cmdlineFlag
372         }
373         return
374 }