2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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"
33 "golang.org/x/crypto/ed25519"
38 CfgPathEnv = "NNCPCFG"
39 CfgSpoolEnv = "NNCPSPOOL"
44 DefaultCfgPath string = "/usr/local/etc/nncp.hjson"
45 DefaultSendmailPath string = "/usr/sbin/sendmail"
46 DefaultSpoolPath string = "/var/spool/nncp"
47 DefaultLogPath string = "/var/spool/nncp/log"
50 type NodeJSON struct {
52 ExchPub string `json:"exchpub"`
53 SignPub string `json:"signpub"`
54 NoisePub *string `json:"noisepub,omitempty"`
55 Incoming *string `json:"incoming,omitempty"`
56 Exec map[string][]string `json:"exec,omitempty"`
57 Freq *NodeFreqJSON `json:"freq,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 CallJSON struct {
77 Cron string `json:"cron"`
78 Nice *string `json:"nice,omitempty"`
79 Xx *string `json:"xx,omitempty"`
80 RxRate *int `json:"rxrate,omitempty"`
81 TxRate *int `json:"txrate,omitempty"`
82 Addr *string `json:"addr,omitempty"`
83 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
84 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
85 WhenTxExists bool `json:"when-tx-exists,omitempty"`
86 NoCK bool `json:"nock,omitempty"`
87 MCDIgnore bool `json:"mcd-ignore,omitempty"`
89 AutoToss bool `json:"autotoss,omitempty"`
90 AutoTossDoSeen bool `json:"autotoss-doseen,omitempty"`
91 AutoTossNoFile bool `json:"autotoss-nofile,omitempty"`
92 AutoTossNoFreq bool `json:"autotoss-nofreq,omitempty"`
93 AutoTossNoExec bool `json:"autotoss-noexec,omitempty"`
94 AutoTossNoTrns bool `json:"autotoss-notrns,omitempty"`
95 AutoTossNoArea bool `json:"autotoss-noarea,omitempty"`
98 type NodeOurJSON struct {
100 ExchPub string `json:"exchpub"`
101 ExchPrv string `json:"exchprv"`
102 SignPub string `json:"signpub"`
103 SignPrv string `json:"signprv"`
104 NoisePub string `json:"noisepub"`
105 NoisePrv string `json:"noiseprv"`
108 type FromToJSON struct {
109 From string `json:"from"`
110 To string `json:"to"`
113 type NotifyJSON struct {
114 File *FromToJSON `json:"file,omitempty"`
115 Freq *FromToJSON `json:"freq,omitempty"`
116 Exec map[string]*FromToJSON `json:"exec,omitempty"`
119 type AreaJSON struct {
120 Id string `json:"id"`
121 Pub *string `json:"pub,omitempty"`
122 Prv *string `json:"prv,omitempty"`
124 Subs []string `json:"subs"`
126 Incoming *string `json:"incoming,omitempty"`
127 Exec map[string][]string `json:"exec,omitempty"`
129 AllowUnknown bool `json:"allow-unknown,omitempty"`
132 type CfgJSON struct {
133 Spool string `json:"spool"`
134 Log string `json:"log"`
135 Umask *string `json:"umask,omitempty"`
137 OmitPrgrs bool `json:"noprogress,omitempty"`
138 NoHdr bool `json:"nohdr,omitempty"`
140 MCDRxIfis []string `json:"mcd-listen,omitempty"`
141 MCDTxIfis map[string]int `json:"mcd-send,omitempty"`
143 Notify *NotifyJSON `json:"notify,omitempty"`
145 Self *NodeOurJSON `json:"self"`
146 Neigh map[string]NodeJSON `json:"neigh"`
148 Areas map[string]AreaJSON `json:"areas,omitempty"`
150 YggdrasilAliases map[string]string `json:"yggdrasil-aliases,omitempty"`
153 func NewNode(name string, cfg NodeJSON) (*Node, error) {
154 nodeId, err := NodeIdFromString(cfg.Id)
159 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
163 if len(exchPub) != 32 {
164 return nil, errors.New("Invalid exchPub size")
167 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
171 if len(signPub) != ed25519.PublicKeySize {
172 return nil, errors.New("Invalid signPub size")
176 if cfg.NoisePub != nil {
177 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
181 if len(noisePub) != 32 {
182 return nil, errors.New("Invalid noisePub size")
187 if cfg.Incoming != nil {
188 inc := path.Clean(*cfg.Incoming)
189 if !path.IsAbs(inc) {
190 return nil, errors.New("Incoming path must be absolute")
196 var freqChunked int64
197 var freqMinSize int64
198 freqMaxSize := int64(MaxFileSize)
202 fPath := path.Clean(*f.Path)
203 if !path.IsAbs(fPath) {
204 return nil, errors.New("freq.path path must be absolute")
208 if f.Chunked != nil {
210 return nil, errors.New("freq.chunked value must be greater than zero")
212 freqChunked = int64(*f.Chunked) * 1024
214 if f.MinSize != nil {
215 freqMinSize = int64(*f.MinSize) * 1024
217 if f.MaxSize != nil {
218 freqMaxSize = int64(*f.MaxSize) * 1024
223 if cfg.RxRate != nil && *cfg.RxRate > 0 {
224 defRxRate = *cfg.RxRate
227 if cfg.TxRate != nil && *cfg.TxRate > 0 {
228 defTxRate = *cfg.TxRate
231 defOnlineDeadline := DefaultDeadline
232 if cfg.OnlineDeadline != nil {
233 if *cfg.OnlineDeadline <= 0 {
234 return nil, errors.New("OnlineDeadline must be at least 1 second")
236 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
238 var defMaxOnlineTime time.Duration
239 if cfg.MaxOnlineTime != nil {
240 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
244 for _, callCfg := range cfg.Calls {
245 expr, err := cronexpr.Parse(callCfg.Cron)
251 if callCfg.Nice != nil {
252 nice, err = NicenessParse(*callCfg.Nice)
259 if callCfg.Xx != nil {
266 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
271 if callCfg.RxRate != nil {
272 rxRate = *callCfg.RxRate
275 if callCfg.TxRate != nil {
276 txRate = *callCfg.TxRate
280 if callCfg.Addr != nil {
281 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
288 onlineDeadline := defOnlineDeadline
289 if callCfg.OnlineDeadline != nil {
290 if *callCfg.OnlineDeadline == 0 {
291 return nil, errors.New("OnlineDeadline must be at least 1 second")
293 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
303 OnlineDeadline: onlineDeadline,
306 if callCfg.MaxOnlineTime != nil {
307 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
309 call.WhenTxExists = callCfg.WhenTxExists
310 call.NoCK = callCfg.NoCK
311 call.MCDIgnore = callCfg.MCDIgnore
312 call.AutoToss = callCfg.AutoToss
313 call.AutoTossDoSeen = callCfg.AutoTossDoSeen
314 call.AutoTossNoFile = callCfg.AutoTossNoFile
315 call.AutoTossNoFreq = callCfg.AutoTossNoFreq
316 call.AutoTossNoExec = callCfg.AutoTossNoExec
317 call.AutoTossNoTrns = callCfg.AutoTossNoTrns
318 call.AutoTossNoArea = callCfg.AutoTossNoArea
320 calls = append(calls, &call)
326 ExchPub: new([32]byte),
327 SignPub: ed25519.PublicKey(signPub),
331 FreqChunked: freqChunked,
332 FreqMinSize: freqMinSize,
333 FreqMaxSize: freqMaxSize,
338 OnlineDeadline: defOnlineDeadline,
339 MaxOnlineTime: defMaxOnlineTime,
341 copy(node.ExchPub[:], exchPub)
342 if len(noisePub) > 0 {
343 node.NoisePub = new([32]byte)
344 copy(node.NoisePub[:], noisePub)
349 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
350 id, err := NodeIdFromString(cfg.Id)
355 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
359 if len(exchPub) != 32 {
360 return nil, errors.New("Invalid exchPub size")
363 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
367 if len(exchPrv) != 32 {
368 return nil, errors.New("Invalid exchPrv size")
371 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
375 if len(signPub) != ed25519.PublicKeySize {
376 return nil, errors.New("Invalid signPub size")
379 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
383 if len(signPrv) != ed25519.PrivateKeySize {
384 return nil, errors.New("Invalid signPrv size")
387 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
391 if len(noisePub) != 32 {
392 return nil, errors.New("Invalid noisePub size")
395 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
399 if len(noisePrv) != 32 {
400 return nil, errors.New("Invalid noisePrv size")
405 ExchPub: new([32]byte),
406 ExchPrv: new([32]byte),
407 SignPub: ed25519.PublicKey(signPub),
408 SignPrv: ed25519.PrivateKey(signPrv),
409 NoisePub: new([32]byte),
410 NoisePrv: new([32]byte),
412 copy(node.ExchPub[:], exchPub)
413 copy(node.ExchPrv[:], exchPrv)
414 copy(node.NoisePub[:], noisePub)
415 copy(node.NoisePrv[:], noisePrv)
419 func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
420 areaId, err := AreaIdFromString(cfg.Id)
424 subs := make([]*NodeId, 0, len(cfg.Subs))
425 for _, s := range cfg.Subs {
426 node, err := ctx.FindNode(s)
430 subs = append(subs, node.Id)
437 Incoming: cfg.Incoming,
440 pub, err := Base32Codec.DecodeString(*cfg.Pub)
445 return nil, errors.New("Invalid pub size")
447 area.Pub = new([32]byte)
448 copy(area.Pub[:], pub)
452 return nil, fmt.Errorf("area %s: prv requires pub presence", name)
454 prv, err := Base32Codec.DecodeString(*cfg.Prv)
459 return nil, errors.New("Invalid prv size")
461 area.Prv = new([32]byte)
462 copy(area.Prv[:], prv)
464 area.AllowUnknown = cfg.AllowUnknown
468 func CfgParse(data []byte) (*CfgJSON, error) {
470 if bytes.Compare(data[:8], MagicNNCPBv3.B[:]) == 0 {
471 os.Stderr.WriteString("Passphrase:")
472 password, err := term.ReadPassword(0)
476 os.Stderr.WriteString("\n")
477 data, err = DeEBlob(data, password)
481 } else if bytes.Compare(data[:8], MagicNNCPBv2.B[:]) == 0 {
482 log.Fatalln(MagicNNCPBv2.TooOld())
483 } else if bytes.Compare(data[:8], MagicNNCPBv1.B[:]) == 0 {
484 log.Fatalln(MagicNNCPBv1.TooOld())
486 var cfgGeneral map[string]interface{}
487 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
490 marshaled, err := json.Marshal(cfgGeneral)
495 err = json.Unmarshal(marshaled, &cfgJSON)
499 func Cfg2Ctx(cfgJSON *CfgJSON) (*Ctx, error) {
500 if _, exists := cfgJSON.Neigh["self"]; !exists {
501 return nil, errors.New("self neighbour missing")
504 if cfgJSON.Self != nil {
506 self, err = NewNodeOur(cfgJSON.Self)
511 spoolPath := path.Clean(cfgJSON.Spool)
512 if !path.IsAbs(spoolPath) {
513 return nil, errors.New("Spool path must be absolute")
515 logPath := path.Clean(cfgJSON.Log)
516 if !path.IsAbs(logPath) {
517 return nil, errors.New("Log path must be absolute")
520 if cfgJSON.Umask != nil {
521 r, err := strconv.ParseUint(*cfgJSON.Umask, 8, 16)
529 if cfgJSON.OmitPrgrs {
539 UmaskForce: umaskForce,
540 ShowPrgrs: showPrgrs,
543 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
544 Alias: make(map[string]*NodeId),
545 MCDRxIfis: cfgJSON.MCDRxIfis,
546 MCDTxIfis: cfgJSON.MCDTxIfis,
548 YggdrasilAliases: cfgJSON.YggdrasilAliases,
550 if cfgJSON.Notify != nil {
551 if cfgJSON.Notify.File != nil {
552 ctx.NotifyFile = cfgJSON.Notify.File
554 if cfgJSON.Notify.Freq != nil {
555 ctx.NotifyFreq = cfgJSON.Notify.Freq
557 if cfgJSON.Notify.Exec != nil {
558 ctx.NotifyExec = cfgJSON.Notify.Exec
561 vias := make(map[NodeId][]string)
562 for name, neighJSON := range cfgJSON.Neigh {
563 neigh, err := NewNode(name, neighJSON)
567 ctx.Neigh[*neigh.Id] = neigh
568 if _, already := ctx.Alias[name]; already {
569 return nil, errors.New("Node names conflict")
571 ctx.Alias[name] = neigh.Id
572 vias[*neigh.Id] = neighJSON.Via
574 ctx.SelfId = ctx.Alias["self"]
575 for neighId, viasRaw := range vias {
576 for _, viaRaw := range viasRaw {
577 foundNodeId, err := ctx.FindNode(viaRaw)
581 ctx.Neigh[neighId].Via = append(
582 ctx.Neigh[neighId].Via,
587 ctx.AreaId2Area = make(map[AreaId]*Area, len(cfgJSON.Areas))
588 ctx.AreaName2Id = make(map[string]*AreaId, len(cfgJSON.Areas))
589 for name, areaJSON := range cfgJSON.Areas {
590 area, err := NewArea(&ctx, name, &areaJSON)
594 ctx.AreaId2Area[*area.Id] = area
595 ctx.AreaName2Id[name] = area.Id