2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
31 "github.com/gorhill/cronexpr"
32 "github.com/hjson/hjson-go/v4"
33 "golang.org/x/crypto/ed25519"
38 CfgPathEnv = "NNCPCFG"
39 CfgSpoolEnv = "NNCPSPOOL"
41 CfgNoSync = "NNCPNOSYNC"
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"
51 type NodeJSON struct {
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 ACK *NodeACKJSON `json:"ack,omitempty"`
60 Via []string `json:"via,omitempty"`
61 Calls []CallJSON `json:"calls,omitempty"`
63 Addrs map[string]string `json:"addrs,omitempty"`
65 RxRate *int `json:"rxrate,omitempty"`
66 TxRate *int `json:"txrate,omitempty"`
67 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
68 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
71 type NodeFreqJSON struct {
72 Path *string `json:"path,omitempty"`
73 Chunked *uint64 `json:"chunked,omitempty"`
74 MinSize *uint64 `json:"minsize,omitempty"`
75 MaxSize *uint64 `json:"maxsize,omitempty"`
78 type NodeACKJSON struct {
79 MinSize *uint64 `json:"minsize,omitempty"`
80 Nice *string `json:"nice,omitempty"`
83 type CallJSON struct {
84 Cron string `json:"cron"`
85 Nice *string `json:"nice,omitempty"`
86 Xx *string `json:"xx,omitempty"`
87 RxRate *int `json:"rxrate,omitempty"`
88 TxRate *int `json:"txrate,omitempty"`
89 Addr *string `json:"addr,omitempty"`
90 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
91 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
92 WhenTxExists bool `json:"when-tx-exists,omitempty"`
93 NoCK bool `json:"nock,omitempty"`
94 MCDIgnore bool `json:"mcd-ignore,omitempty"`
96 AutoToss bool `json:"autotoss,omitempty"`
97 AutoTossDoSeen bool `json:"autotoss-doseen,omitempty"`
98 AutoTossNoFile bool `json:"autotoss-nofile,omitempty"`
99 AutoTossNoFreq bool `json:"autotoss-nofreq,omitempty"`
100 AutoTossNoExec bool `json:"autotoss-noexec,omitempty"`
101 AutoTossNoTrns bool `json:"autotoss-notrns,omitempty"`
102 AutoTossNoArea bool `json:"autotoss-noarea,omitempty"`
103 AutoTossNoACK bool `json:"autotoss-noack,omitempty"`
104 AutoTossGenACK bool `json:"autotoss-gen-ack,omitempty"`
107 type NodeOurJSON struct {
108 Id string `json:"id"`
109 ExchPub string `json:"exchpub"`
110 ExchPrv string `json:"exchprv"`
111 SignPub string `json:"signpub"`
112 SignPrv string `json:"signprv"`
113 NoisePub string `json:"noisepub"`
114 NoisePrv string `json:"noiseprv"`
117 type FromToJSON struct {
118 From string `json:"from"`
119 To string `json:"to"`
122 type NotifyJSON struct {
123 File *FromToJSON `json:"file,omitempty"`
124 Freq *FromToJSON `json:"freq,omitempty"`
125 Exec map[string]*FromToJSON `json:"exec,omitempty"`
128 type AreaJSON struct {
129 Id string `json:"id"`
130 Pub *string `json:"pub,omitempty"`
131 Prv *string `json:"prv,omitempty"`
133 Subs []string `json:"subs"`
135 Incoming *string `json:"incoming,omitempty"`
136 Exec map[string][]string `json:"exec,omitempty"`
138 AllowUnknown bool `json:"allow-unknown,omitempty"`
141 type CfgJSON struct {
142 Spool string `json:"spool"`
143 Log string `json:"log"`
144 Umask *string `json:"umask,omitempty"`
146 OmitPrgrs bool `json:"noprogress,omitempty"`
147 NoHdr bool `json:"nohdr,omitempty"`
149 MCDRxIfis []string `json:"mcd-listen,omitempty"`
150 MCDTxIfis map[string]int `json:"mcd-send,omitempty"`
152 Notify *NotifyJSON `json:"notify,omitempty"`
154 Self *NodeOurJSON `json:"self"`
155 Neigh map[string]NodeJSON `json:"neigh"`
157 Areas map[string]AreaJSON `json:"areas,omitempty"`
159 YggdrasilAliases map[string]string `json:"yggdrasil-aliases,omitempty"`
162 func NewNode(name string, cfg NodeJSON) (*Node, error) {
163 nodeId, err := NodeIdFromString(cfg.Id)
168 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
172 if len(exchPub) != 32 {
173 return nil, errors.New("Invalid exchPub size")
176 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
180 if len(signPub) != ed25519.PublicKeySize {
181 return nil, errors.New("Invalid signPub size")
185 if cfg.NoisePub != nil {
186 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
190 if len(noisePub) != 32 {
191 return nil, errors.New("Invalid noisePub size")
196 if cfg.Incoming != nil {
197 inc := path.Clean(*cfg.Incoming)
198 if !path.IsAbs(inc) {
199 return nil, errors.New("Incoming path must be absolute")
205 var freqChunked int64
206 var freqMinSize int64
207 freqMaxSize := int64(MaxFileSize)
211 fPath := path.Clean(*f.Path)
212 if !path.IsAbs(fPath) {
213 return nil, errors.New("freq.path path must be absolute")
217 if f.Chunked != nil {
219 return nil, errors.New("freq.chunked value must be greater than zero")
221 freqChunked = int64(*f.Chunked) * 1024
223 if f.MinSize != nil {
224 freqMinSize = int64(*f.MinSize) * 1024
226 if f.MaxSize != nil {
227 freqMaxSize = int64(*f.MaxSize) * 1024
231 ackNice := uint8(255)
234 if cfg.ACK.Nice != nil {
235 ackNice, err = NicenessParse(*cfg.ACK.Nice)
240 if cfg.ACK.MinSize != nil {
241 ackMinSize = int64(*cfg.ACK.MinSize) * 1024
246 if cfg.RxRate != nil && *cfg.RxRate > 0 {
247 defRxRate = *cfg.RxRate
250 if cfg.TxRate != nil && *cfg.TxRate > 0 {
251 defTxRate = *cfg.TxRate
254 defOnlineDeadline := DefaultDeadline
255 if cfg.OnlineDeadline != nil {
256 if *cfg.OnlineDeadline <= 0 {
257 return nil, errors.New("OnlineDeadline must be at least 1 second")
259 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
261 var defMaxOnlineTime time.Duration
262 if cfg.MaxOnlineTime != nil {
263 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
267 for _, callCfg := range cfg.Calls {
268 expr, err := cronexpr.Parse(callCfg.Cron)
274 if callCfg.Nice != nil {
275 nice, err = NicenessParse(*callCfg.Nice)
282 if callCfg.Xx != nil {
289 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
294 if callCfg.RxRate != nil {
295 rxRate = *callCfg.RxRate
298 if callCfg.TxRate != nil {
299 txRate = *callCfg.TxRate
303 if callCfg.Addr != nil {
304 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
311 onlineDeadline := defOnlineDeadline
312 if callCfg.OnlineDeadline != nil {
313 if *callCfg.OnlineDeadline == 0 {
314 return nil, errors.New("OnlineDeadline must be at least 1 second")
316 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
326 OnlineDeadline: onlineDeadline,
329 if callCfg.MaxOnlineTime != nil {
330 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
332 call.WhenTxExists = callCfg.WhenTxExists
333 call.NoCK = callCfg.NoCK
334 call.MCDIgnore = callCfg.MCDIgnore
335 call.AutoToss = callCfg.AutoToss
336 call.AutoTossDoSeen = callCfg.AutoTossDoSeen
337 call.AutoTossNoFile = callCfg.AutoTossNoFile
338 call.AutoTossNoFreq = callCfg.AutoTossNoFreq
339 call.AutoTossNoExec = callCfg.AutoTossNoExec
340 call.AutoTossNoTrns = callCfg.AutoTossNoTrns
341 call.AutoTossNoArea = callCfg.AutoTossNoArea
342 call.AutoTossNoACK = callCfg.AutoTossNoACK
343 call.AutoTossGenACK = callCfg.AutoTossGenACK
345 calls = append(calls, &call)
351 ExchPub: new([32]byte),
352 SignPub: ed25519.PublicKey(signPub),
356 FreqChunked: freqChunked,
357 FreqMinSize: freqMinSize,
358 FreqMaxSize: freqMaxSize,
360 ACKMinSize: ackMinSize,
365 OnlineDeadline: defOnlineDeadline,
366 MaxOnlineTime: defMaxOnlineTime,
368 copy(node.ExchPub[:], exchPub)
369 if len(noisePub) > 0 {
370 node.NoisePub = new([32]byte)
371 copy(node.NoisePub[:], noisePub)
376 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
377 id, err := NodeIdFromString(cfg.Id)
382 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
386 if len(exchPub) != 32 {
387 return nil, errors.New("Invalid exchPub size")
390 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
394 if len(exchPrv) != 32 {
395 return nil, errors.New("Invalid exchPrv size")
398 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
402 if len(signPub) != ed25519.PublicKeySize {
403 return nil, errors.New("Invalid signPub size")
406 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
410 if len(signPrv) != ed25519.PrivateKeySize {
411 return nil, errors.New("Invalid signPrv size")
414 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
418 if len(noisePub) != 32 {
419 return nil, errors.New("Invalid noisePub size")
422 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
426 if len(noisePrv) != 32 {
427 return nil, errors.New("Invalid noisePrv size")
432 ExchPub: new([32]byte),
433 ExchPrv: new([32]byte),
434 SignPub: ed25519.PublicKey(signPub),
435 SignPrv: ed25519.PrivateKey(signPrv),
436 NoisePub: new([32]byte),
437 NoisePrv: new([32]byte),
439 copy(node.ExchPub[:], exchPub)
440 copy(node.ExchPrv[:], exchPrv)
441 copy(node.NoisePub[:], noisePub)
442 copy(node.NoisePrv[:], noisePrv)
446 func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
447 areaId, err := AreaIdFromString(cfg.Id)
451 subs := make([]*NodeId, 0, len(cfg.Subs))
452 for _, s := range cfg.Subs {
453 node, err := ctx.FindNode(s)
457 subs = append(subs, node.Id)
464 Incoming: cfg.Incoming,
467 pub, err := Base32Codec.DecodeString(*cfg.Pub)
472 return nil, errors.New("Invalid pub size")
474 area.Pub = new([32]byte)
475 copy(area.Pub[:], pub)
479 return nil, fmt.Errorf("area %s: prv requires pub presence", name)
481 prv, err := Base32Codec.DecodeString(*cfg.Prv)
486 return nil, errors.New("Invalid prv size")
488 area.Prv = new([32]byte)
489 copy(area.Prv[:], prv)
491 area.AllowUnknown = cfg.AllowUnknown
495 func CfgParse(data []byte) (*CfgJSON, error) {
497 if bytes.Equal(data[:8], MagicNNCPBv3.B[:]) {
498 os.Stderr.WriteString("Passphrase:")
499 password, err := term.ReadPassword(0)
503 os.Stderr.WriteString("\n")
504 data, err = DeEBlob(data, password)
508 } else if bytes.Equal(data[:8], MagicNNCPBv2.B[:]) {
509 log.Fatalln(MagicNNCPBv2.TooOld())
510 } else if bytes.Equal(data[:8], MagicNNCPBv1.B[:]) {
511 log.Fatalln(MagicNNCPBv1.TooOld())
513 var cfgGeneral map[string]interface{}
514 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
517 marshaled, err := json.Marshal(cfgGeneral)
522 err = json.Unmarshal(marshaled, &cfgJSON)
526 func Cfg2Ctx(cfgJSON *CfgJSON) (*Ctx, error) {
527 if _, exists := cfgJSON.Neigh["self"]; !exists {
528 return nil, errors.New("self neighbour missing")
531 if cfgJSON.Self != nil {
533 self, err = NewNodeOur(cfgJSON.Self)
538 spoolPath := path.Clean(cfgJSON.Spool)
539 if !path.IsAbs(spoolPath) {
540 return nil, errors.New("Spool path must be absolute")
542 logPath := path.Clean(cfgJSON.Log)
543 if !path.IsAbs(logPath) {
544 return nil, errors.New("Log path must be absolute")
547 if cfgJSON.Umask != nil {
548 r, err := strconv.ParseUint(*cfgJSON.Umask, 8, 16)
556 if cfgJSON.OmitPrgrs {
566 UmaskForce: umaskForce,
567 ShowPrgrs: showPrgrs,
570 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
571 Alias: make(map[string]*NodeId),
572 MCDRxIfis: cfgJSON.MCDRxIfis,
573 MCDTxIfis: cfgJSON.MCDTxIfis,
575 YggdrasilAliases: cfgJSON.YggdrasilAliases,
577 if cfgJSON.Notify != nil {
578 if cfgJSON.Notify.File != nil {
579 ctx.NotifyFile = cfgJSON.Notify.File
581 if cfgJSON.Notify.Freq != nil {
582 ctx.NotifyFreq = cfgJSON.Notify.Freq
584 if cfgJSON.Notify.Exec != nil {
585 ctx.NotifyExec = cfgJSON.Notify.Exec
588 vias := make(map[NodeId][]string)
589 for name, neighJSON := range cfgJSON.Neigh {
590 neigh, err := NewNode(name, neighJSON)
594 ctx.Neigh[*neigh.Id] = neigh
595 if _, already := ctx.Alias[name]; already {
596 return nil, errors.New("Node names conflict")
598 ctx.Alias[name] = neigh.Id
599 vias[*neigh.Id] = neighJSON.Via
601 ctx.SelfId = ctx.Alias["self"]
602 for neighId, viasRaw := range vias {
603 for _, viaRaw := range viasRaw {
604 foundNodeId, err := ctx.FindNode(viaRaw)
608 ctx.Neigh[neighId].Via = append(
609 ctx.Neigh[neighId].Via,
614 ctx.AreaId2Area = make(map[AreaId]*Area, len(cfgJSON.Areas))
615 ctx.AreaName2Id = make(map[string]*AreaId, len(cfgJSON.Areas))
616 for name, areaJSON := range cfgJSON.Areas {
617 area, err := NewArea(&ctx, name, &areaJSON)
621 ctx.AreaId2Area[*area.Id] = area
622 ctx.AreaName2Id[name] = area.Id