1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
29 "github.com/gorhill/cronexpr"
30 "github.com/hjson/hjson-go/v4"
31 "golang.org/x/crypto/ed25519"
36 CfgPathEnv = "NNCPCFG"
37 CfgSpoolEnv = "NNCPSPOOL"
39 CfgNoSync = "NNCPNOSYNC"
43 DefaultCfgPath string = "/usr/local/etc/nncp.hjson"
44 DefaultSendmailPath string = "/usr/sbin/sendmail"
45 DefaultSpoolPath string = "/var/spool/nncp"
46 DefaultLogPath string = "/var/spool/nncp/log"
49 type NodeJSON struct {
51 ExchPub string `json:"exchpub"`
52 SignPub string `json:"signpub"`
53 NoisePub *string `json:"noisepub,omitempty"`
54 Incoming *string `json:"incoming,omitempty"`
55 Exec map[string][]string `json:"exec,omitempty"`
56 Freq *NodeFreqJSON `json:"freq,omitempty"`
57 ACK *NodeACKJSON `json:"ack,omitempty"`
58 Via []string `json:"via,omitempty"`
59 Calls []CallJSON `json:"calls,omitempty"`
61 Addrs map[string]string `json:"addrs,omitempty"`
63 RxRate *int `json:"rxrate,omitempty"`
64 TxRate *int `json:"txrate,omitempty"`
65 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
66 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
69 type NodeFreqJSON struct {
70 Path *string `json:"path,omitempty"`
71 Chunked *uint64 `json:"chunked,omitempty"`
72 MinSize *uint64 `json:"minsize,omitempty"`
73 MaxSize *uint64 `json:"maxsize,omitempty"`
76 type NodeACKJSON struct {
77 MinSize *uint64 `json:"minsize,omitempty"`
78 Nice *string `json:"nice,omitempty"`
81 type CallJSON struct {
82 Cron string `json:"cron"`
83 Nice *string `json:"nice,omitempty"`
84 Xx *string `json:"xx,omitempty"`
85 RxRate *int `json:"rxrate,omitempty"`
86 TxRate *int `json:"txrate,omitempty"`
87 Addr *string `json:"addr,omitempty"`
88 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
89 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
90 WhenTxExists bool `json:"when-tx-exists,omitempty"`
91 NoCK bool `json:"nock,omitempty"`
92 MCDIgnore bool `json:"mcd-ignore,omitempty"`
94 AutoToss bool `json:"autotoss,omitempty"`
95 AutoTossDoSeen bool `json:"autotoss-doseen,omitempty"`
96 AutoTossNoFile bool `json:"autotoss-nofile,omitempty"`
97 AutoTossNoFreq bool `json:"autotoss-nofreq,omitempty"`
98 AutoTossNoExec bool `json:"autotoss-noexec,omitempty"`
99 AutoTossNoTrns bool `json:"autotoss-notrns,omitempty"`
100 AutoTossNoArea bool `json:"autotoss-noarea,omitempty"`
101 AutoTossNoACK bool `json:"autotoss-noack,omitempty"`
102 AutoTossGenACK bool `json:"autotoss-gen-ack,omitempty"`
105 type NodeOurJSON struct {
106 Id string `json:"id"`
107 ExchPub string `json:"exchpub"`
108 ExchPrv string `json:"exchprv"`
109 SignPub string `json:"signpub"`
110 SignPrv string `json:"signprv"`
111 NoisePub string `json:"noisepub"`
112 NoisePrv string `json:"noiseprv"`
115 type FromToJSON struct {
116 From string `json:"from"`
117 To string `json:"to"`
120 type NotifyJSON struct {
121 File *FromToJSON `json:"file,omitempty"`
122 Freq *FromToJSON `json:"freq,omitempty"`
123 Exec map[string]*FromToJSON `json:"exec,omitempty"`
126 type AreaJSON struct {
127 Id string `json:"id"`
128 Pub *string `json:"pub,omitempty"`
129 Prv *string `json:"prv,omitempty"`
131 Subs []string `json:"subs"`
133 Incoming *string `json:"incoming,omitempty"`
134 Exec map[string][]string `json:"exec,omitempty"`
136 AllowUnknown bool `json:"allow-unknown,omitempty"`
139 type CfgJSON struct {
140 Spool string `json:"spool"`
141 Log string `json:"log"`
142 Umask *string `json:"umask,omitempty"`
144 OmitPrgrs bool `json:"noprogress,omitempty"`
145 NoHdr bool `json:"nohdr,omitempty"`
147 MCDRxIfis []string `json:"mcd-listen,omitempty"`
148 MCDTxIfis map[string]int `json:"mcd-send,omitempty"`
150 Notify *NotifyJSON `json:"notify,omitempty"`
152 Self *NodeOurJSON `json:"self"`
153 Neigh map[string]NodeJSON `json:"neigh"`
155 Areas map[string]AreaJSON `json:"areas,omitempty"`
157 YggdrasilAliases map[string]string `json:"yggdrasil-aliases,omitempty"`
160 func NewNode(name string, cfg NodeJSON) (*Node, error) {
161 nodeId, err := NodeIdFromString(cfg.Id)
166 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
170 if len(exchPub) != 32 {
171 return nil, errors.New("Invalid exchPub size")
174 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
178 if len(signPub) != ed25519.PublicKeySize {
179 return nil, errors.New("Invalid signPub size")
183 if cfg.NoisePub != nil {
184 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
188 if len(noisePub) != 32 {
189 return nil, errors.New("Invalid noisePub size")
194 if cfg.Incoming != nil {
195 inc := path.Clean(*cfg.Incoming)
196 if !path.IsAbs(inc) {
197 return nil, errors.New("Incoming path must be absolute")
203 var freqChunked int64
204 var freqMinSize int64
205 freqMaxSize := int64(MaxFileSize)
209 fPath := path.Clean(*f.Path)
210 if !path.IsAbs(fPath) {
211 return nil, errors.New("freq.path path must be absolute")
215 if f.Chunked != nil {
217 return nil, errors.New("freq.chunked value must be greater than zero")
219 freqChunked = int64(*f.Chunked) * 1024
221 if f.MinSize != nil {
222 freqMinSize = int64(*f.MinSize) * 1024
224 if f.MaxSize != nil {
225 freqMaxSize = int64(*f.MaxSize) * 1024
229 ackNice := uint8(255)
232 if cfg.ACK.Nice != nil {
233 ackNice, err = NicenessParse(*cfg.ACK.Nice)
238 if cfg.ACK.MinSize != nil {
239 ackMinSize = int64(*cfg.ACK.MinSize) * 1024
244 if cfg.RxRate != nil && *cfg.RxRate > 0 {
245 defRxRate = *cfg.RxRate
248 if cfg.TxRate != nil && *cfg.TxRate > 0 {
249 defTxRate = *cfg.TxRate
252 defOnlineDeadline := DefaultDeadline
253 if cfg.OnlineDeadline != nil {
254 if *cfg.OnlineDeadline <= 0 {
255 return nil, errors.New("OnlineDeadline must be at least 1 second")
257 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
259 var defMaxOnlineTime time.Duration
260 if cfg.MaxOnlineTime != nil {
261 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
265 for _, callCfg := range cfg.Calls {
266 expr, err := cronexpr.Parse(callCfg.Cron)
272 if callCfg.Nice != nil {
273 nice, err = NicenessParse(*callCfg.Nice)
280 if callCfg.Xx != nil {
287 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
292 if callCfg.RxRate != nil {
293 rxRate = *callCfg.RxRate
296 if callCfg.TxRate != nil {
297 txRate = *callCfg.TxRate
301 if callCfg.Addr != nil {
302 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
309 onlineDeadline := defOnlineDeadline
310 if callCfg.OnlineDeadline != nil {
311 if *callCfg.OnlineDeadline == 0 {
312 return nil, errors.New("OnlineDeadline must be at least 1 second")
314 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
324 OnlineDeadline: onlineDeadline,
327 if callCfg.MaxOnlineTime != nil {
328 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
330 call.WhenTxExists = callCfg.WhenTxExists
331 call.NoCK = callCfg.NoCK
332 call.MCDIgnore = callCfg.MCDIgnore
333 call.AutoToss = callCfg.AutoToss
334 call.AutoTossDoSeen = callCfg.AutoTossDoSeen
335 call.AutoTossNoFile = callCfg.AutoTossNoFile
336 call.AutoTossNoFreq = callCfg.AutoTossNoFreq
337 call.AutoTossNoExec = callCfg.AutoTossNoExec
338 call.AutoTossNoTrns = callCfg.AutoTossNoTrns
339 call.AutoTossNoArea = callCfg.AutoTossNoArea
340 call.AutoTossNoACK = callCfg.AutoTossNoACK
341 call.AutoTossGenACK = callCfg.AutoTossGenACK
343 calls = append(calls, &call)
349 ExchPub: new([32]byte),
350 SignPub: ed25519.PublicKey(signPub),
354 FreqChunked: freqChunked,
355 FreqMinSize: freqMinSize,
356 FreqMaxSize: freqMaxSize,
358 ACKMinSize: ackMinSize,
363 OnlineDeadline: defOnlineDeadline,
364 MaxOnlineTime: defMaxOnlineTime,
366 copy(node.ExchPub[:], exchPub)
367 if len(noisePub) > 0 {
368 node.NoisePub = new([32]byte)
369 copy(node.NoisePub[:], noisePub)
374 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
375 id, err := NodeIdFromString(cfg.Id)
380 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
384 if len(exchPub) != 32 {
385 return nil, errors.New("Invalid exchPub size")
388 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
392 if len(exchPrv) != 32 {
393 return nil, errors.New("Invalid exchPrv size")
396 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
400 if len(signPub) != ed25519.PublicKeySize {
401 return nil, errors.New("Invalid signPub size")
404 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
408 if len(signPrv) != ed25519.PrivateKeySize {
409 return nil, errors.New("Invalid signPrv size")
412 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
416 if len(noisePub) != 32 {
417 return nil, errors.New("Invalid noisePub size")
420 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
424 if len(noisePrv) != 32 {
425 return nil, errors.New("Invalid noisePrv size")
430 ExchPub: new([32]byte),
431 ExchPrv: new([32]byte),
432 SignPub: ed25519.PublicKey(signPub),
433 SignPrv: ed25519.PrivateKey(signPrv),
434 NoisePub: new([32]byte),
435 NoisePrv: new([32]byte),
437 copy(node.ExchPub[:], exchPub)
438 copy(node.ExchPrv[:], exchPrv)
439 copy(node.NoisePub[:], noisePub)
440 copy(node.NoisePrv[:], noisePrv)
444 func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
445 areaId, err := AreaIdFromString(cfg.Id)
449 subs := make([]*NodeId, 0, len(cfg.Subs))
450 for _, s := range cfg.Subs {
451 node, err := ctx.FindNode(s)
455 subs = append(subs, node.Id)
462 Incoming: cfg.Incoming,
465 pub, err := Base32Codec.DecodeString(*cfg.Pub)
470 return nil, errors.New("Invalid pub size")
472 area.Pub = new([32]byte)
473 copy(area.Pub[:], pub)
477 return nil, fmt.Errorf("area %s: prv requires pub presence", name)
479 prv, err := Base32Codec.DecodeString(*cfg.Prv)
484 return nil, errors.New("Invalid prv size")
486 area.Prv = new([32]byte)
487 copy(area.Prv[:], prv)
489 area.AllowUnknown = cfg.AllowUnknown
493 func CfgParse(data []byte) (*CfgJSON, error) {
495 if bytes.Equal(data[:8], MagicNNCPBv3.B[:]) {
496 os.Stderr.WriteString("Passphrase:")
497 password, err := term.ReadPassword(0)
501 os.Stderr.WriteString("\n")
502 data, err = DeEBlob(data, password)
506 } else if bytes.Equal(data[:8], MagicNNCPBv2.B[:]) {
507 log.Fatalln(MagicNNCPBv2.TooOld())
508 } else if bytes.Equal(data[:8], MagicNNCPBv1.B[:]) {
509 log.Fatalln(MagicNNCPBv1.TooOld())
511 var cfgGeneral map[string]interface{}
512 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
515 marshaled, err := json.Marshal(cfgGeneral)
520 err = json.Unmarshal(marshaled, &cfgJSON)
524 func Cfg2Ctx(cfgJSON *CfgJSON) (*Ctx, error) {
525 if _, exists := cfgJSON.Neigh["self"]; !exists {
526 return nil, errors.New("self neighbour missing")
529 if cfgJSON.Self != nil {
531 self, err = NewNodeOur(cfgJSON.Self)
536 spoolPath := path.Clean(cfgJSON.Spool)
537 if !path.IsAbs(spoolPath) {
538 return nil, errors.New("Spool path must be absolute")
540 logPath := path.Clean(cfgJSON.Log)
541 if !path.IsAbs(logPath) {
542 return nil, errors.New("Log path must be absolute")
545 if cfgJSON.Umask != nil {
546 r, err := strconv.ParseUint(*cfgJSON.Umask, 8, 16)
554 if cfgJSON.OmitPrgrs {
564 UmaskForce: umaskForce,
565 ShowPrgrs: showPrgrs,
568 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
569 Alias: make(map[string]*NodeId),
570 MCDRxIfis: cfgJSON.MCDRxIfis,
571 MCDTxIfis: cfgJSON.MCDTxIfis,
573 YggdrasilAliases: cfgJSON.YggdrasilAliases,
575 if cfgJSON.Notify != nil {
576 if cfgJSON.Notify.File != nil {
577 ctx.NotifyFile = cfgJSON.Notify.File
579 if cfgJSON.Notify.Freq != nil {
580 ctx.NotifyFreq = cfgJSON.Notify.Freq
582 if cfgJSON.Notify.Exec != nil {
583 ctx.NotifyExec = cfgJSON.Notify.Exec
586 vias := make(map[NodeId][]string)
587 for name, neighJSON := range cfgJSON.Neigh {
588 neigh, err := NewNode(name, neighJSON)
592 ctx.Neigh[*neigh.Id] = neigh
593 if _, already := ctx.Alias[name]; already {
594 return nil, errors.New("Node names conflict")
596 ctx.Alias[name] = neigh.Id
597 vias[*neigh.Id] = neighJSON.Via
599 ctx.SelfId = ctx.Alias["self"]
600 for neighId, viasRaw := range vias {
601 for _, viaRaw := range viasRaw {
602 foundNodeId, err := ctx.FindNode(viaRaw)
606 ctx.Neigh[neighId].Via = append(
607 ctx.Neigh[neighId].Via,
612 ctx.AreaId2Area = make(map[AreaId]*Area, len(cfgJSON.Areas))
613 ctx.AreaName2Id = make(map[string]*AreaId, len(cfgJSON.Areas))
614 for name, areaJSON := range cfgJSON.Areas {
615 area, err := NewArea(&ctx, name, &areaJSON)
619 ctx.AreaId2Area[*area.Id] = area
620 ctx.AreaName2Id[name] = area.Id