2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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/>.
30 "github.com/gorhill/cronexpr"
31 "github.com/hjson/hjson-go"
32 "golang.org/x/crypto/ed25519"
37 CfgPathEnv = "NNCPCFG"
38 CfgSpoolEnv = "NNCPSPOOL"
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 Exec map[string][]string `json:"exec,omitempty"`
55 Incoming *string `json:"incoming,omitempty"`
56 Freq *NodeFreqJSON `json:"freq,omitempty"`
57 Via []string `json:"via,omitempty"`
58 Calls []CallJSON `json:"calls,omitempty"`
60 Addrs map[string]string `json:"addrs,omitempty"`
62 RxRate *int `json:"rxrate,omitempty"`
63 TxRate *int `json:"txrate,omitempty"`
64 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
65 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
68 type NodeFreqJSON struct {
69 Path *string `json:"path,omitempty"`
70 Chunked *uint64 `json:"chunked,omitempty"`
71 MinSize *uint64 `json:"minsize,omitempty"`
72 MaxSize *uint64 `json:"maxsize,omitempty"`
75 type CallJSON struct {
77 Nice *string `json:"nice,omitempty"`
78 Xx *string `json:"xx,omitempty"`
79 RxRate *int `json:"rxrate,omitempty"`
80 TxRate *int `json:"txrate,omitempty"`
81 Addr *string `json:"addr,omitempty"`
82 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
83 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
84 WhenTxExists *bool `json:"when-tx-exists,omitempty"`
85 NoCK *bool `json:"nock"`
86 MCDIgnore *bool `json:"mcd-ignore"`
88 AutoToss *bool `json:"autotoss,omitempty"`
89 AutoTossDoSeen *bool `json:"autotoss-doseen,omitempty"`
90 AutoTossNoFile *bool `json:"autotoss-nofile,omitempty"`
91 AutoTossNoFreq *bool `json:"autotoss-nofreq,omitempty"`
92 AutoTossNoExec *bool `json:"autotoss-noexec,omitempty"`
93 AutoTossNoTrns *bool `json:"autotoss-notrns,omitempty"`
94 AutoTossNoArea *bool `json:"autotoss-noarea,omitempty"`
97 type NodeOurJSON struct {
99 ExchPub string `json:"exchpub"`
100 ExchPrv string `json:"exchprv"`
101 SignPub string `json:"signpub"`
102 SignPrv string `json:"signprv"`
103 NoisePrv string `json:"noiseprv"`
104 NoisePub string `json:"noisepub"`
107 type FromToJSON struct {
112 type NotifyJSON struct {
113 File *FromToJSON `json:"file,omitempty"`
114 Freq *FromToJSON `json:"freq,omitempty"`
115 Exec map[string]*FromToJSON `json:"exec,omitempty"`
118 type AreaJSON struct {
119 Id string `json:"id"`
120 Pub string `json:"pub"`
121 Prv *string `json:"prv,omitempty"`
123 Subs []string `json:"subs"`
125 Exec map[string][]string `json:"exec,omitempty"`
126 Incoming *string `json:"incoming,omitempty"`
128 AllowUnknown *bool `json:"allow-unknown,omitempty"`
131 type CfgJSON struct {
132 Spool string `json:"spool"`
133 Log string `json:"log"`
134 Umask string `json:"umask,omitempty"`
136 OmitPrgrs bool `json:"noprogress,omitempty"`
137 NoHdr bool `json:"nohdr,omitempty"`
139 Notify *NotifyJSON `json:"notify,omitempty"`
141 Self *NodeOurJSON `json:"self"`
142 Neigh map[string]NodeJSON `json:"neigh"`
144 MCDRxIfis []string `json:"mcd-listen"`
145 MCDTxIfis map[string]int `json:"mcd-send"`
147 Areas map[string]AreaJSON `json:"areas"`
150 func NewNode(name string, cfg NodeJSON) (*Node, error) {
151 nodeId, err := NodeIdFromString(cfg.Id)
156 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
160 if len(exchPub) != 32 {
161 return nil, errors.New("Invalid exchPub size")
164 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
168 if len(signPub) != ed25519.PublicKeySize {
169 return nil, errors.New("Invalid signPub size")
173 if cfg.NoisePub != nil {
174 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
178 if len(noisePub) != 32 {
179 return nil, errors.New("Invalid noisePub size")
184 if cfg.Incoming != nil {
185 inc := path.Clean(*cfg.Incoming)
186 if !path.IsAbs(inc) {
187 return nil, errors.New("Incoming path must be absolute")
193 freqChunked := int64(MaxFileSize)
194 var freqMinSize int64
195 freqMaxSize := int64(MaxFileSize)
199 fPath := path.Clean(*f.Path)
200 if !path.IsAbs(fPath) {
201 return nil, errors.New("freq.path path must be absolute")
205 if f.Chunked != nil {
207 return nil, errors.New("freq.chunked value must be greater than zero")
209 freqChunked = int64(*f.Chunked) * 1024
211 if f.MinSize != nil {
212 freqMinSize = int64(*f.MinSize) * 1024
214 if f.MaxSize != nil {
215 freqMaxSize = int64(*f.MaxSize) * 1024
220 if cfg.RxRate != nil && *cfg.RxRate > 0 {
221 defRxRate = *cfg.RxRate
224 if cfg.TxRate != nil && *cfg.TxRate > 0 {
225 defTxRate = *cfg.TxRate
228 defOnlineDeadline := DefaultDeadline
229 if cfg.OnlineDeadline != nil {
230 if *cfg.OnlineDeadline <= 0 {
231 return nil, errors.New("OnlineDeadline must be at least 1 second")
233 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
235 var defMaxOnlineTime time.Duration
236 if cfg.MaxOnlineTime != nil {
237 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
241 for _, callCfg := range cfg.Calls {
242 expr, err := cronexpr.Parse(callCfg.Cron)
248 if callCfg.Nice != nil {
249 nice, err = NicenessParse(*callCfg.Nice)
256 if callCfg.Xx != nil {
263 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
268 if callCfg.RxRate != nil {
269 rxRate = *callCfg.RxRate
272 if callCfg.TxRate != nil {
273 txRate = *callCfg.TxRate
277 if callCfg.Addr != nil {
278 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
285 onlineDeadline := defOnlineDeadline
286 if callCfg.OnlineDeadline != nil {
287 if *callCfg.OnlineDeadline == 0 {
288 return nil, errors.New("OnlineDeadline must be at least 1 second")
290 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
300 OnlineDeadline: onlineDeadline,
303 if callCfg.MaxOnlineTime != nil {
304 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
306 if callCfg.WhenTxExists != nil {
307 call.WhenTxExists = *callCfg.WhenTxExists
309 if callCfg.NoCK != nil {
310 call.NoCK = *callCfg.NoCK
312 if callCfg.MCDIgnore != nil {
313 call.MCDIgnore = *callCfg.MCDIgnore
315 if callCfg.AutoToss != nil {
316 call.AutoToss = *callCfg.AutoToss
318 if callCfg.AutoTossDoSeen != nil {
319 call.AutoTossDoSeen = *callCfg.AutoTossDoSeen
321 if callCfg.AutoTossNoFile != nil {
322 call.AutoTossNoFile = *callCfg.AutoTossNoFile
324 if callCfg.AutoTossNoFreq != nil {
325 call.AutoTossNoFreq = *callCfg.AutoTossNoFreq
327 if callCfg.AutoTossNoExec != nil {
328 call.AutoTossNoExec = *callCfg.AutoTossNoExec
330 if callCfg.AutoTossNoTrns != nil {
331 call.AutoTossNoTrns = *callCfg.AutoTossNoTrns
333 if callCfg.AutoTossNoArea != nil {
334 call.AutoTossNoArea = *callCfg.AutoTossNoArea
337 calls = append(calls, &call)
343 ExchPub: new([32]byte),
344 SignPub: ed25519.PublicKey(signPub),
348 FreqChunked: freqChunked,
349 FreqMinSize: freqMinSize,
350 FreqMaxSize: freqMaxSize,
355 OnlineDeadline: defOnlineDeadline,
356 MaxOnlineTime: defMaxOnlineTime,
358 copy(node.ExchPub[:], exchPub)
359 if len(noisePub) > 0 {
360 node.NoisePub = new([32]byte)
361 copy(node.NoisePub[:], noisePub)
366 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
367 id, err := NodeIdFromString(cfg.Id)
372 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
376 if len(exchPub) != 32 {
377 return nil, errors.New("Invalid exchPub size")
380 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
384 if len(exchPrv) != 32 {
385 return nil, errors.New("Invalid exchPrv size")
388 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
392 if len(signPub) != ed25519.PublicKeySize {
393 return nil, errors.New("Invalid signPub size")
396 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
400 if len(signPrv) != ed25519.PrivateKeySize {
401 return nil, errors.New("Invalid signPrv size")
404 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
408 if len(noisePub) != 32 {
409 return nil, errors.New("Invalid noisePub size")
412 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
416 if len(noisePrv) != 32 {
417 return nil, errors.New("Invalid noisePrv size")
422 ExchPub: new([32]byte),
423 ExchPrv: new([32]byte),
424 SignPub: ed25519.PublicKey(signPub),
425 SignPrv: ed25519.PrivateKey(signPrv),
426 NoisePub: new([32]byte),
427 NoisePrv: new([32]byte),
429 copy(node.ExchPub[:], exchPub)
430 copy(node.ExchPrv[:], exchPrv)
431 copy(node.NoisePub[:], noisePub)
432 copy(node.NoisePrv[:], noisePrv)
436 func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
437 areaId, err := AreaIdFromString(cfg.Id)
441 subs := make([]*NodeId, 0, len(cfg.Subs))
442 for _, s := range cfg.Subs {
443 node, err := ctx.FindNode(s)
447 subs = append(subs, node.Id)
455 Incoming: cfg.Incoming,
457 pub, err := Base32Codec.DecodeString(cfg.Pub)
462 return nil, errors.New("Invalid pub size")
464 copy(area.Pub[:], pub)
466 prv, err := Base32Codec.DecodeString(*cfg.Prv)
471 return nil, errors.New("Invalid prv size")
473 area.Prv = new([32]byte)
474 copy(area.Prv[:], prv)
476 if cfg.AllowUnknown != nil {
477 area.AllowUnknown = *cfg.AllowUnknown
482 func CfgParse(data []byte) (*Ctx, error) {
484 if bytes.Compare(data[:8], MagicNNCPBv3.B[:]) == 0 {
485 os.Stderr.WriteString("Passphrase:") // #nosec G104
486 password, err := term.ReadPassword(0)
490 os.Stderr.WriteString("\n") // #nosec G104
491 data, err = DeEBlob(data, password)
495 } else if bytes.Compare(data[:8], MagicNNCPBv2.B[:]) == 0 {
496 log.Fatalln(MagicNNCPBv2.TooOld())
497 } else if bytes.Compare(data[:8], MagicNNCPBv1.B[:]) == 0 {
498 log.Fatalln(MagicNNCPBv1.TooOld())
500 var cfgGeneral map[string]interface{}
501 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
504 marshaled, err := json.Marshal(cfgGeneral)
509 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
512 if _, exists := cfgJSON.Neigh["self"]; !exists {
513 return nil, errors.New("self neighbour missing")
516 if cfgJSON.Self != nil {
517 self, err = NewNodeOur(cfgJSON.Self)
522 spoolPath := path.Clean(cfgJSON.Spool)
523 if !path.IsAbs(spoolPath) {
524 return nil, errors.New("Spool path must be absolute")
526 logPath := path.Clean(cfgJSON.Log)
527 if !path.IsAbs(logPath) {
528 return nil, errors.New("Log path must be absolute")
531 if cfgJSON.Umask != "" {
532 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
540 if cfgJSON.OmitPrgrs {
550 UmaskForce: umaskForce,
551 ShowPrgrs: showPrgrs,
554 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
555 Alias: make(map[string]*NodeId),
556 MCDRxIfis: cfgJSON.MCDRxIfis,
557 MCDTxIfis: cfgJSON.MCDTxIfis,
559 if cfgJSON.Notify != nil {
560 if cfgJSON.Notify.File != nil {
561 ctx.NotifyFile = cfgJSON.Notify.File
563 if cfgJSON.Notify.Freq != nil {
564 ctx.NotifyFreq = cfgJSON.Notify.Freq
566 if cfgJSON.Notify.Exec != nil {
567 ctx.NotifyExec = cfgJSON.Notify.Exec
570 vias := make(map[NodeId][]string)
571 for name, neighJSON := range cfgJSON.Neigh {
572 neigh, err := NewNode(name, neighJSON)
576 ctx.Neigh[*neigh.Id] = neigh
577 if _, already := ctx.Alias[name]; already {
578 return nil, errors.New("Node names conflict")
580 ctx.Alias[name] = neigh.Id
581 vias[*neigh.Id] = neighJSON.Via
583 ctx.SelfId = ctx.Alias["self"]
584 for neighId, viasRaw := range vias {
585 for _, viaRaw := range viasRaw {
586 foundNodeId, err := ctx.FindNode(viaRaw)
590 ctx.Neigh[neighId].Via = append(
591 ctx.Neigh[neighId].Via,
596 ctx.AreaId2Area = make(map[AreaId]*Area, len(cfgJSON.Areas))
597 ctx.AreaName2Id = make(map[string]*AreaId, len(cfgJSON.Areas))
598 for name, areaJSON := range cfgJSON.Areas {
599 area, err := NewArea(&ctx, name, &areaJSON)
603 ctx.AreaId2Area[*area.Id] = area
604 ctx.AreaName2Id[name] = area.Id