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"
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 Via []string `json:"via,omitempty"`
60 Calls []CallJSON `json:"calls,omitempty"`
62 Addrs map[string]string `json:"addrs,omitempty"`
64 RxRate *int `json:"rxrate,omitempty"`
65 TxRate *int `json:"txrate,omitempty"`
66 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
67 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
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"`
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"`
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"`
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"`
109 type FromToJSON struct {
110 From string `json:"from"`
111 To string `json:"to"`
114 type NotifyJSON struct {
115 File *FromToJSON `json:"file,omitempty"`
116 Freq *FromToJSON `json:"freq,omitempty"`
117 Exec map[string]*FromToJSON `json:"exec,omitempty"`
120 type AreaJSON struct {
121 Id string `json:"id"`
122 Pub *string `json:"pub,omitempty"`
123 Prv *string `json:"prv,omitempty"`
125 Subs []string `json:"subs"`
127 Incoming *string `json:"incoming,omitempty"`
128 Exec map[string][]string `json:"exec,omitempty"`
130 AllowUnknown bool `json:"allow-unknown,omitempty"`
133 type CfgJSON struct {
134 Spool string `json:"spool"`
135 Log string `json:"log"`
136 Umask *string `json:"umask,omitempty"`
138 OmitPrgrs bool `json:"noprogress,omitempty"`
139 NoHdr bool `json:"nohdr,omitempty"`
141 MCDRxIfis []string `json:"mcd-listen,omitempty"`
142 MCDTxIfis map[string]int `json:"mcd-send,omitempty"`
144 Notify *NotifyJSON `json:"notify,omitempty"`
146 Self *NodeOurJSON `json:"self"`
147 Neigh map[string]NodeJSON `json:"neigh"`
149 Areas map[string]AreaJSON `json:"areas,omitempty"`
151 YggdrasilAliases map[string]string `json:"yggdrasil-aliases,omitempty"`
154 func NewNode(name string, cfg NodeJSON) (*Node, error) {
155 nodeId, err := NodeIdFromString(cfg.Id)
160 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
164 if len(exchPub) != 32 {
165 return nil, errors.New("Invalid exchPub size")
168 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
172 if len(signPub) != ed25519.PublicKeySize {
173 return nil, errors.New("Invalid signPub size")
177 if cfg.NoisePub != nil {
178 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
182 if len(noisePub) != 32 {
183 return nil, errors.New("Invalid noisePub size")
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")
197 var freqChunked int64
198 var freqMinSize int64
199 freqMaxSize := int64(MaxFileSize)
203 fPath := path.Clean(*f.Path)
204 if !path.IsAbs(fPath) {
205 return nil, errors.New("freq.path path must be absolute")
209 if f.Chunked != nil {
211 return nil, errors.New("freq.chunked value must be greater than zero")
213 freqChunked = int64(*f.Chunked) * 1024
215 if f.MinSize != nil {
216 freqMinSize = int64(*f.MinSize) * 1024
218 if f.MaxSize != nil {
219 freqMaxSize = int64(*f.MaxSize) * 1024
224 if cfg.RxRate != nil && *cfg.RxRate > 0 {
225 defRxRate = *cfg.RxRate
228 if cfg.TxRate != nil && *cfg.TxRate > 0 {
229 defTxRate = *cfg.TxRate
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")
237 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
239 var defMaxOnlineTime time.Duration
240 if cfg.MaxOnlineTime != nil {
241 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
245 for _, callCfg := range cfg.Calls {
246 expr, err := cronexpr.Parse(callCfg.Cron)
252 if callCfg.Nice != nil {
253 nice, err = NicenessParse(*callCfg.Nice)
260 if callCfg.Xx != nil {
267 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
272 if callCfg.RxRate != nil {
273 rxRate = *callCfg.RxRate
276 if callCfg.TxRate != nil {
277 txRate = *callCfg.TxRate
281 if callCfg.Addr != nil {
282 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
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")
294 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
304 OnlineDeadline: onlineDeadline,
307 if callCfg.MaxOnlineTime != nil {
308 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
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
321 calls = append(calls, &call)
327 ExchPub: new([32]byte),
328 SignPub: ed25519.PublicKey(signPub),
332 FreqChunked: freqChunked,
333 FreqMinSize: freqMinSize,
334 FreqMaxSize: freqMaxSize,
339 OnlineDeadline: defOnlineDeadline,
340 MaxOnlineTime: defMaxOnlineTime,
342 copy(node.ExchPub[:], exchPub)
343 if len(noisePub) > 0 {
344 node.NoisePub = new([32]byte)
345 copy(node.NoisePub[:], noisePub)
350 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
351 id, err := NodeIdFromString(cfg.Id)
356 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
360 if len(exchPub) != 32 {
361 return nil, errors.New("Invalid exchPub size")
364 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
368 if len(exchPrv) != 32 {
369 return nil, errors.New("Invalid exchPrv size")
372 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
376 if len(signPub) != ed25519.PublicKeySize {
377 return nil, errors.New("Invalid signPub size")
380 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
384 if len(signPrv) != ed25519.PrivateKeySize {
385 return nil, errors.New("Invalid signPrv size")
388 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
392 if len(noisePub) != 32 {
393 return nil, errors.New("Invalid noisePub size")
396 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
400 if len(noisePrv) != 32 {
401 return nil, errors.New("Invalid noisePrv size")
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),
413 copy(node.ExchPub[:], exchPub)
414 copy(node.ExchPrv[:], exchPrv)
415 copy(node.NoisePub[:], noisePub)
416 copy(node.NoisePrv[:], noisePrv)
420 func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
421 areaId, err := AreaIdFromString(cfg.Id)
425 subs := make([]*NodeId, 0, len(cfg.Subs))
426 for _, s := range cfg.Subs {
427 node, err := ctx.FindNode(s)
431 subs = append(subs, node.Id)
438 Incoming: cfg.Incoming,
441 pub, err := Base32Codec.DecodeString(*cfg.Pub)
446 return nil, errors.New("Invalid pub size")
448 area.Pub = new([32]byte)
449 copy(area.Pub[:], pub)
453 return nil, fmt.Errorf("area %s: prv requires pub presence", name)
455 prv, err := Base32Codec.DecodeString(*cfg.Prv)
460 return nil, errors.New("Invalid prv size")
462 area.Prv = new([32]byte)
463 copy(area.Prv[:], prv)
465 area.AllowUnknown = cfg.AllowUnknown
469 func CfgParse(data []byte) (*CfgJSON, error) {
471 if bytes.Compare(data[:8], MagicNNCPBv3.B[:]) == 0 {
472 os.Stderr.WriteString("Passphrase:")
473 password, err := term.ReadPassword(0)
477 os.Stderr.WriteString("\n")
478 data, err = DeEBlob(data, password)
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())
487 var cfgGeneral map[string]interface{}
488 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
491 marshaled, err := json.Marshal(cfgGeneral)
496 err = json.Unmarshal(marshaled, &cfgJSON)
500 func Cfg2Ctx(cfgJSON *CfgJSON) (*Ctx, error) {
501 if _, exists := cfgJSON.Neigh["self"]; !exists {
502 return nil, errors.New("self neighbour missing")
505 if cfgJSON.Self != nil {
507 self, err = NewNodeOur(cfgJSON.Self)
512 spoolPath := path.Clean(cfgJSON.Spool)
513 if !path.IsAbs(spoolPath) {
514 return nil, errors.New("Spool path must be absolute")
516 logPath := path.Clean(cfgJSON.Log)
517 if !path.IsAbs(logPath) {
518 return nil, errors.New("Log path must be absolute")
521 if cfgJSON.Umask != nil {
522 r, err := strconv.ParseUint(*cfgJSON.Umask, 8, 16)
530 if cfgJSON.OmitPrgrs {
540 UmaskForce: umaskForce,
541 ShowPrgrs: showPrgrs,
544 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
545 Alias: make(map[string]*NodeId),
546 MCDRxIfis: cfgJSON.MCDRxIfis,
547 MCDTxIfis: cfgJSON.MCDTxIfis,
549 YggdrasilAliases: cfgJSON.YggdrasilAliases,
551 if cfgJSON.Notify != nil {
552 if cfgJSON.Notify.File != nil {
553 ctx.NotifyFile = cfgJSON.Notify.File
555 if cfgJSON.Notify.Freq != nil {
556 ctx.NotifyFreq = cfgJSON.Notify.Freq
558 if cfgJSON.Notify.Exec != nil {
559 ctx.NotifyExec = cfgJSON.Notify.Exec
562 vias := make(map[NodeId][]string)
563 for name, neighJSON := range cfgJSON.Neigh {
564 neigh, err := NewNode(name, neighJSON)
568 ctx.Neigh[*neigh.Id] = neigh
569 if _, already := ctx.Alias[name]; already {
570 return nil, errors.New("Node names conflict")
572 ctx.Alias[name] = neigh.Id
573 vias[*neigh.Id] = neighJSON.Via
575 ctx.SelfId = ctx.Alias["self"]
576 for neighId, viasRaw := range vias {
577 for _, viaRaw := range viasRaw {
578 foundNodeId, err := ctx.FindNode(viaRaw)
582 ctx.Neigh[neighId].Via = append(
583 ctx.Neigh[neighId].Via,
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)
595 ctx.AreaId2Area[*area.Id] = area
596 ctx.AreaName2Id[name] = area.Id