]> Cypherpunks.ru repositories - nncp.git/blob - src/cfg.go
NNCPNOSYNC environment variable
[nncp.git] / src / cfg.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package nncp
19
20 import (
21         "bytes"
22         "encoding/json"
23         "errors"
24         "fmt"
25         "log"
26         "os"
27         "path"
28         "strconv"
29         "time"
30
31         "github.com/gorhill/cronexpr"
32         "github.com/hjson/hjson-go"
33         "golang.org/x/crypto/ed25519"
34         "golang.org/x/term"
35 )
36
37 const (
38         CfgPathEnv  = "NNCPCFG"
39         CfgSpoolEnv = "NNCPSPOOL"
40         CfgLogEnv   = "NNCPLOG"
41         CfgNoSync   = "NNCPNOSYNC"
42 )
43
44 var (
45         DefaultCfgPath      string = "/usr/local/etc/nncp.hjson"
46         DefaultSendmailPath string = "/usr/sbin/sendmail"
47         DefaultSpoolPath    string = "/var/spool/nncp"
48         DefaultLogPath      string = "/var/spool/nncp/log"
49 )
50
51 type NodeJSON struct {
52         Id       string              `json:"id"`
53         ExchPub  string              `json:"exchpub"`
54         SignPub  string              `json:"signpub"`
55         NoisePub *string             `json:"noisepub,omitempty"`
56         Incoming *string             `json:"incoming,omitempty"`
57         Exec     map[string][]string `json:"exec,omitempty"`
58         Freq     *NodeFreqJSON       `json:"freq,omitempty"`
59         Via      []string            `json:"via,omitempty"`
60         Calls    []CallJSON          `json:"calls,omitempty"`
61
62         Addrs map[string]string `json:"addrs,omitempty"`
63
64         RxRate         *int  `json:"rxrate,omitempty"`
65         TxRate         *int  `json:"txrate,omitempty"`
66         OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
67         MaxOnlineTime  *uint `json:"maxonlinetime,omitempty"`
68 }
69
70 type NodeFreqJSON struct {
71         Path    *string `json:"path,omitempty"`
72         Chunked *uint64 `json:"chunked,omitempty"`
73         MinSize *uint64 `json:"minsize,omitempty"`
74         MaxSize *uint64 `json:"maxsize,omitempty"`
75 }
76
77 type CallJSON struct {
78         Cron           string  `json:"cron"`
79         Nice           *string `json:"nice,omitempty"`
80         Xx             *string `json:"xx,omitempty"`
81         RxRate         *int    `json:"rxrate,omitempty"`
82         TxRate         *int    `json:"txrate,omitempty"`
83         Addr           *string `json:"addr,omitempty"`
84         OnlineDeadline *uint   `json:"onlinedeadline,omitempty"`
85         MaxOnlineTime  *uint   `json:"maxonlinetime,omitempty"`
86         WhenTxExists   bool    `json:"when-tx-exists,omitempty"`
87         NoCK           bool    `json:"nock,omitempty"`
88         MCDIgnore      bool    `json:"mcd-ignore,omitempty"`
89
90         AutoToss       bool `json:"autotoss,omitempty"`
91         AutoTossDoSeen bool `json:"autotoss-doseen,omitempty"`
92         AutoTossNoFile bool `json:"autotoss-nofile,omitempty"`
93         AutoTossNoFreq bool `json:"autotoss-nofreq,omitempty"`
94         AutoTossNoExec bool `json:"autotoss-noexec,omitempty"`
95         AutoTossNoTrns bool `json:"autotoss-notrns,omitempty"`
96         AutoTossNoArea bool `json:"autotoss-noarea,omitempty"`
97 }
98
99 type NodeOurJSON struct {
100         Id       string `json:"id"`
101         ExchPub  string `json:"exchpub"`
102         ExchPrv  string `json:"exchprv"`
103         SignPub  string `json:"signpub"`
104         SignPrv  string `json:"signprv"`
105         NoisePub string `json:"noisepub"`
106         NoisePrv string `json:"noiseprv"`
107 }
108
109 type FromToJSON struct {
110         From string `json:"from"`
111         To   string `json:"to"`
112 }
113
114 type NotifyJSON struct {
115         File *FromToJSON            `json:"file,omitempty"`
116         Freq *FromToJSON            `json:"freq,omitempty"`
117         Exec map[string]*FromToJSON `json:"exec,omitempty"`
118 }
119
120 type AreaJSON struct {
121         Id  string  `json:"id"`
122         Pub *string `json:"pub,omitempty"`
123         Prv *string `json:"prv,omitempty"`
124
125         Subs []string `json:"subs"`
126
127         Incoming *string             `json:"incoming,omitempty"`
128         Exec     map[string][]string `json:"exec,omitempty"`
129
130         AllowUnknown bool `json:"allow-unknown,omitempty"`
131 }
132
133 type CfgJSON struct {
134         Spool string  `json:"spool"`
135         Log   string  `json:"log"`
136         Umask *string `json:"umask,omitempty"`
137
138         OmitPrgrs bool `json:"noprogress,omitempty"`
139         NoHdr     bool `json:"nohdr,omitempty"`
140
141         MCDRxIfis []string       `json:"mcd-listen,omitempty"`
142         MCDTxIfis map[string]int `json:"mcd-send,omitempty"`
143
144         Notify *NotifyJSON `json:"notify,omitempty"`
145
146         Self  *NodeOurJSON        `json:"self"`
147         Neigh map[string]NodeJSON `json:"neigh"`
148
149         Areas map[string]AreaJSON `json:"areas,omitempty"`
150
151         YggdrasilAliases map[string]string `json:"yggdrasil-aliases,omitempty"`
152 }
153
154 func NewNode(name string, cfg NodeJSON) (*Node, error) {
155         nodeId, err := NodeIdFromString(cfg.Id)
156         if err != nil {
157                 return nil, err
158         }
159
160         exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
161         if err != nil {
162                 return nil, err
163         }
164         if len(exchPub) != 32 {
165                 return nil, errors.New("Invalid exchPub size")
166         }
167
168         signPub, err := Base32Codec.DecodeString(cfg.SignPub)
169         if err != nil {
170                 return nil, err
171         }
172         if len(signPub) != ed25519.PublicKeySize {
173                 return nil, errors.New("Invalid signPub size")
174         }
175
176         var noisePub []byte
177         if cfg.NoisePub != nil {
178                 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
179                 if err != nil {
180                         return nil, err
181                 }
182                 if len(noisePub) != 32 {
183                         return nil, errors.New("Invalid noisePub size")
184                 }
185         }
186
187         var incoming *string
188         if cfg.Incoming != nil {
189                 inc := path.Clean(*cfg.Incoming)
190                 if !path.IsAbs(inc) {
191                         return nil, errors.New("Incoming path must be absolute")
192                 }
193                 incoming = &inc
194         }
195
196         var freqPath *string
197         var freqChunked int64
198         var freqMinSize int64
199         freqMaxSize := int64(MaxFileSize)
200         if cfg.Freq != nil {
201                 f := cfg.Freq
202                 if f.Path != nil {
203                         fPath := path.Clean(*f.Path)
204                         if !path.IsAbs(fPath) {
205                                 return nil, errors.New("freq.path path must be absolute")
206                         }
207                         freqPath = &fPath
208                 }
209                 if f.Chunked != nil {
210                         if *f.Chunked == 0 {
211                                 return nil, errors.New("freq.chunked value must be greater than zero")
212                         }
213                         freqChunked = int64(*f.Chunked) * 1024
214                 }
215                 if f.MinSize != nil {
216                         freqMinSize = int64(*f.MinSize) * 1024
217                 }
218                 if f.MaxSize != nil {
219                         freqMaxSize = int64(*f.MaxSize) * 1024
220                 }
221         }
222
223         defRxRate := 0
224         if cfg.RxRate != nil && *cfg.RxRate > 0 {
225                 defRxRate = *cfg.RxRate
226         }
227         defTxRate := 0
228         if cfg.TxRate != nil && *cfg.TxRate > 0 {
229                 defTxRate = *cfg.TxRate
230         }
231
232         defOnlineDeadline := DefaultDeadline
233         if cfg.OnlineDeadline != nil {
234                 if *cfg.OnlineDeadline <= 0 {
235                         return nil, errors.New("OnlineDeadline must be at least 1 second")
236                 }
237                 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
238         }
239         var defMaxOnlineTime time.Duration
240         if cfg.MaxOnlineTime != nil {
241                 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
242         }
243
244         var calls []*Call
245         for _, callCfg := range cfg.Calls {
246                 expr, err := cronexpr.Parse(callCfg.Cron)
247                 if err != nil {
248                         return nil, err
249                 }
250
251                 nice := uint8(255)
252                 if callCfg.Nice != nil {
253                         nice, err = NicenessParse(*callCfg.Nice)
254                         if err != nil {
255                                 return nil, err
256                         }
257                 }
258
259                 var xx TRxTx
260                 if callCfg.Xx != nil {
261                         switch *callCfg.Xx {
262                         case "rx":
263                                 xx = TRx
264                         case "tx":
265                                 xx = TTx
266                         default:
267                                 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
268                         }
269                 }
270
271                 rxRate := defRxRate
272                 if callCfg.RxRate != nil {
273                         rxRate = *callCfg.RxRate
274                 }
275                 txRate := defTxRate
276                 if callCfg.TxRate != nil {
277                         txRate = *callCfg.TxRate
278                 }
279
280                 var addr *string
281                 if callCfg.Addr != nil {
282                         if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
283                                 addr = &a
284                         } else {
285                                 addr = callCfg.Addr
286                         }
287                 }
288
289                 onlineDeadline := defOnlineDeadline
290                 if callCfg.OnlineDeadline != nil {
291                         if *callCfg.OnlineDeadline == 0 {
292                                 return nil, errors.New("OnlineDeadline must be at least 1 second")
293                         }
294                         onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
295                 }
296
297                 call := Call{
298                         Cron:           expr,
299                         Nice:           nice,
300                         Xx:             xx,
301                         RxRate:         rxRate,
302                         TxRate:         txRate,
303                         Addr:           addr,
304                         OnlineDeadline: onlineDeadline,
305                 }
306
307                 if callCfg.MaxOnlineTime != nil {
308                         call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
309                 }
310                 call.WhenTxExists = callCfg.WhenTxExists
311                 call.NoCK = callCfg.NoCK
312                 call.MCDIgnore = callCfg.MCDIgnore
313                 call.AutoToss = callCfg.AutoToss
314                 call.AutoTossDoSeen = callCfg.AutoTossDoSeen
315                 call.AutoTossNoFile = callCfg.AutoTossNoFile
316                 call.AutoTossNoFreq = callCfg.AutoTossNoFreq
317                 call.AutoTossNoExec = callCfg.AutoTossNoExec
318                 call.AutoTossNoTrns = callCfg.AutoTossNoTrns
319                 call.AutoTossNoArea = callCfg.AutoTossNoArea
320
321                 calls = append(calls, &call)
322         }
323
324         node := Node{
325                 Name:           name,
326                 Id:             nodeId,
327                 ExchPub:        new([32]byte),
328                 SignPub:        ed25519.PublicKey(signPub),
329                 Exec:           cfg.Exec,
330                 Incoming:       incoming,
331                 FreqPath:       freqPath,
332                 FreqChunked:    freqChunked,
333                 FreqMinSize:    freqMinSize,
334                 FreqMaxSize:    freqMaxSize,
335                 Calls:          calls,
336                 Addrs:          cfg.Addrs,
337                 RxRate:         defRxRate,
338                 TxRate:         defTxRate,
339                 OnlineDeadline: defOnlineDeadline,
340                 MaxOnlineTime:  defMaxOnlineTime,
341         }
342         copy(node.ExchPub[:], exchPub)
343         if len(noisePub) > 0 {
344                 node.NoisePub = new([32]byte)
345                 copy(node.NoisePub[:], noisePub)
346         }
347         return &node, nil
348 }
349
350 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
351         id, err := NodeIdFromString(cfg.Id)
352         if err != nil {
353                 return nil, err
354         }
355
356         exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
357         if err != nil {
358                 return nil, err
359         }
360         if len(exchPub) != 32 {
361                 return nil, errors.New("Invalid exchPub size")
362         }
363
364         exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
365         if err != nil {
366                 return nil, err
367         }
368         if len(exchPrv) != 32 {
369                 return nil, errors.New("Invalid exchPrv size")
370         }
371
372         signPub, err := Base32Codec.DecodeString(cfg.SignPub)
373         if err != nil {
374                 return nil, err
375         }
376         if len(signPub) != ed25519.PublicKeySize {
377                 return nil, errors.New("Invalid signPub size")
378         }
379
380         signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
381         if err != nil {
382                 return nil, err
383         }
384         if len(signPrv) != ed25519.PrivateKeySize {
385                 return nil, errors.New("Invalid signPrv size")
386         }
387
388         noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
389         if err != nil {
390                 return nil, err
391         }
392         if len(noisePub) != 32 {
393                 return nil, errors.New("Invalid noisePub size")
394         }
395
396         noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
397         if err != nil {
398                 return nil, err
399         }
400         if len(noisePrv) != 32 {
401                 return nil, errors.New("Invalid noisePrv size")
402         }
403
404         node := NodeOur{
405                 Id:       id,
406                 ExchPub:  new([32]byte),
407                 ExchPrv:  new([32]byte),
408                 SignPub:  ed25519.PublicKey(signPub),
409                 SignPrv:  ed25519.PrivateKey(signPrv),
410                 NoisePub: new([32]byte),
411                 NoisePrv: new([32]byte),
412         }
413         copy(node.ExchPub[:], exchPub)
414         copy(node.ExchPrv[:], exchPrv)
415         copy(node.NoisePub[:], noisePub)
416         copy(node.NoisePrv[:], noisePrv)
417         return &node, nil
418 }
419
420 func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
421         areaId, err := AreaIdFromString(cfg.Id)
422         if err != nil {
423                 return nil, err
424         }
425         subs := make([]*NodeId, 0, len(cfg.Subs))
426         for _, s := range cfg.Subs {
427                 node, err := ctx.FindNode(s)
428                 if err != nil {
429                         return nil, err
430                 }
431                 subs = append(subs, node.Id)
432         }
433         area := Area{
434                 Name:     name,
435                 Id:       areaId,
436                 Subs:     subs,
437                 Exec:     cfg.Exec,
438                 Incoming: cfg.Incoming,
439         }
440         if cfg.Pub != nil {
441                 pub, err := Base32Codec.DecodeString(*cfg.Pub)
442                 if err != nil {
443                         return nil, err
444                 }
445                 if len(pub) != 32 {
446                         return nil, errors.New("Invalid pub size")
447                 }
448                 area.Pub = new([32]byte)
449                 copy(area.Pub[:], pub)
450         }
451         if cfg.Prv != nil {
452                 if area.Pub == nil {
453                         return nil, fmt.Errorf("area %s: prv requires pub presence", name)
454                 }
455                 prv, err := Base32Codec.DecodeString(*cfg.Prv)
456                 if err != nil {
457                         return nil, err
458                 }
459                 if len(prv) != 32 {
460                         return nil, errors.New("Invalid prv size")
461                 }
462                 area.Prv = new([32]byte)
463                 copy(area.Prv[:], prv)
464         }
465         area.AllowUnknown = cfg.AllowUnknown
466         return &area, nil
467 }
468
469 func CfgParse(data []byte) (*CfgJSON, error) {
470         var err error
471         if bytes.Compare(data[:8], MagicNNCPBv3.B[:]) == 0 {
472                 os.Stderr.WriteString("Passphrase:")
473                 password, err := term.ReadPassword(0)
474                 if err != nil {
475                         log.Fatalln(err)
476                 }
477                 os.Stderr.WriteString("\n")
478                 data, err = DeEBlob(data, password)
479                 if err != nil {
480                         return nil, err
481                 }
482         } else if bytes.Compare(data[:8], MagicNNCPBv2.B[:]) == 0 {
483                 log.Fatalln(MagicNNCPBv2.TooOld())
484         } else if bytes.Compare(data[:8], MagicNNCPBv1.B[:]) == 0 {
485                 log.Fatalln(MagicNNCPBv1.TooOld())
486         }
487         var cfgGeneral map[string]interface{}
488         if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
489                 return nil, err
490         }
491         marshaled, err := json.Marshal(cfgGeneral)
492         if err != nil {
493                 return nil, err
494         }
495         var cfgJSON CfgJSON
496         err = json.Unmarshal(marshaled, &cfgJSON)
497         return &cfgJSON, err
498 }
499
500 func Cfg2Ctx(cfgJSON *CfgJSON) (*Ctx, error) {
501         if _, exists := cfgJSON.Neigh["self"]; !exists {
502                 return nil, errors.New("self neighbour missing")
503         }
504         var self *NodeOur
505         if cfgJSON.Self != nil {
506                 var err error
507                 self, err = NewNodeOur(cfgJSON.Self)
508                 if err != nil {
509                         return nil, err
510                 }
511         }
512         spoolPath := path.Clean(cfgJSON.Spool)
513         if !path.IsAbs(spoolPath) {
514                 return nil, errors.New("Spool path must be absolute")
515         }
516         logPath := path.Clean(cfgJSON.Log)
517         if !path.IsAbs(logPath) {
518                 return nil, errors.New("Log path must be absolute")
519         }
520         var umaskForce *int
521         if cfgJSON.Umask != nil {
522                 r, err := strconv.ParseUint(*cfgJSON.Umask, 8, 16)
523                 if err != nil {
524                         return nil, err
525                 }
526                 rInt := int(r)
527                 umaskForce = &rInt
528         }
529         showPrgrs := true
530         if cfgJSON.OmitPrgrs {
531                 showPrgrs = false
532         }
533         hdrUsage := true
534         if cfgJSON.NoHdr {
535                 hdrUsage = false
536         }
537         ctx := Ctx{
538                 Spool:      spoolPath,
539                 LogPath:    logPath,
540                 UmaskForce: umaskForce,
541                 ShowPrgrs:  showPrgrs,
542                 HdrUsage:   hdrUsage,
543                 Self:       self,
544                 Neigh:      make(map[NodeId]*Node, len(cfgJSON.Neigh)),
545                 Alias:      make(map[string]*NodeId),
546                 MCDRxIfis:  cfgJSON.MCDRxIfis,
547                 MCDTxIfis:  cfgJSON.MCDTxIfis,
548
549                 YggdrasilAliases: cfgJSON.YggdrasilAliases,
550         }
551         if cfgJSON.Notify != nil {
552                 if cfgJSON.Notify.File != nil {
553                         ctx.NotifyFile = cfgJSON.Notify.File
554                 }
555                 if cfgJSON.Notify.Freq != nil {
556                         ctx.NotifyFreq = cfgJSON.Notify.Freq
557                 }
558                 if cfgJSON.Notify.Exec != nil {
559                         ctx.NotifyExec = cfgJSON.Notify.Exec
560                 }
561         }
562         vias := make(map[NodeId][]string)
563         for name, neighJSON := range cfgJSON.Neigh {
564                 neigh, err := NewNode(name, neighJSON)
565                 if err != nil {
566                         return nil, err
567                 }
568                 ctx.Neigh[*neigh.Id] = neigh
569                 if _, already := ctx.Alias[name]; already {
570                         return nil, errors.New("Node names conflict")
571                 }
572                 ctx.Alias[name] = neigh.Id
573                 vias[*neigh.Id] = neighJSON.Via
574         }
575         ctx.SelfId = ctx.Alias["self"]
576         for neighId, viasRaw := range vias {
577                 for _, viaRaw := range viasRaw {
578                         foundNodeId, err := ctx.FindNode(viaRaw)
579                         if err != nil {
580                                 return nil, err
581                         }
582                         ctx.Neigh[neighId].Via = append(
583                                 ctx.Neigh[neighId].Via,
584                                 foundNodeId.Id,
585                         )
586                 }
587         }
588         ctx.AreaId2Area = make(map[AreaId]*Area, len(cfgJSON.Areas))
589         ctx.AreaName2Id = make(map[string]*AreaId, len(cfgJSON.Areas))
590         for name, areaJSON := range cfgJSON.Areas {
591                 area, err := NewArea(&ctx, name, &areaJSON)
592                 if err != nil {
593                         return nil, err
594                 }
595                 ctx.AreaId2Area[*area.Id] = area
596                 ctx.AreaName2Id[name] = area.Id
597         }
598         return &ctx, nil
599 }