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"`
87 AutoToss *bool `json:"autotoss,omitempty"`
88 AutoTossDoSeen *bool `json:"autotoss-doseen,omitempty"`
89 AutoTossNoFile *bool `json:"autotoss-nofile,omitempty"`
90 AutoTossNoFreq *bool `json:"autotoss-nofreq,omitempty"`
91 AutoTossNoExec *bool `json:"autotoss-noexec,omitempty"`
92 AutoTossNoTrns *bool `json:"autotoss-notrns,omitempty"`
95 type NodeOurJSON struct {
97 ExchPub string `json:"exchpub"`
98 ExchPrv string `json:"exchprv"`
99 SignPub string `json:"signpub"`
100 SignPrv string `json:"signprv"`
101 NoisePrv string `json:"noiseprv"`
102 NoisePub string `json:"noisepub"`
105 type FromToJSON struct {
110 type NotifyJSON struct {
111 File *FromToJSON `json:"file,omitempty"`
112 Freq *FromToJSON `json:"freq,omitempty"`
113 Exec map[string]*FromToJSON `json:"exec,omitempty"`
116 type CfgJSON struct {
117 Spool string `json:"spool"`
118 Log string `json:"log"`
119 Umask string `json:"umask,omitempty"`
121 OmitPrgrs bool `json:"noprogress,omitempty"`
122 NoHdr bool `json:"nohdr,omitempty"`
124 Notify *NotifyJSON `json:"notify,omitempty"`
126 Self *NodeOurJSON `json:"self"`
127 Neigh map[string]NodeJSON `json:"neigh"`
130 func NewNode(name string, cfg NodeJSON) (*Node, error) {
131 nodeId, err := NodeIdFromString(cfg.Id)
136 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
140 if len(exchPub) != 32 {
141 return nil, errors.New("Invalid exchPub size")
144 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
148 if len(signPub) != ed25519.PublicKeySize {
149 return nil, errors.New("Invalid signPub size")
153 if cfg.NoisePub != nil {
154 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
158 if len(noisePub) != 32 {
159 return nil, errors.New("Invalid noisePub size")
164 if cfg.Incoming != nil {
165 inc := path.Clean(*cfg.Incoming)
166 if !path.IsAbs(inc) {
167 return nil, errors.New("Incoming path must be absolute")
173 freqChunked := int64(MaxFileSize)
174 var freqMinSize int64
175 freqMaxSize := int64(MaxFileSize)
179 fPath := path.Clean(*f.Path)
180 if !path.IsAbs(fPath) {
181 return nil, errors.New("freq.path path must be absolute")
185 if f.Chunked != nil {
187 return nil, errors.New("freq.chunked value must be greater than zero")
189 freqChunked = int64(*f.Chunked) * 1024
191 if f.MinSize != nil {
192 freqMinSize = int64(*f.MinSize) * 1024
194 if f.MaxSize != nil {
195 freqMaxSize = int64(*f.MaxSize) * 1024
200 if cfg.RxRate != nil && *cfg.RxRate > 0 {
201 defRxRate = *cfg.RxRate
204 if cfg.TxRate != nil && *cfg.TxRate > 0 {
205 defTxRate = *cfg.TxRate
208 defOnlineDeadline := DefaultDeadline
209 if cfg.OnlineDeadline != nil {
210 if *cfg.OnlineDeadline <= 0 {
211 return nil, errors.New("OnlineDeadline must be at least 1 second")
213 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
215 var defMaxOnlineTime time.Duration
216 if cfg.MaxOnlineTime != nil {
217 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
221 for _, callCfg := range cfg.Calls {
222 expr, err := cronexpr.Parse(callCfg.Cron)
228 if callCfg.Nice != nil {
229 nice, err = NicenessParse(*callCfg.Nice)
236 if callCfg.Xx != nil {
243 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
248 if callCfg.RxRate != nil {
249 rxRate = *callCfg.RxRate
252 if callCfg.TxRate != nil {
253 txRate = *callCfg.TxRate
257 if callCfg.Addr != nil {
258 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
265 onlineDeadline := defOnlineDeadline
266 if callCfg.OnlineDeadline != nil {
267 if *callCfg.OnlineDeadline == 0 {
268 return nil, errors.New("OnlineDeadline must be at least 1 second")
270 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
280 OnlineDeadline: onlineDeadline,
283 if callCfg.MaxOnlineTime != nil {
284 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
286 if callCfg.WhenTxExists != nil {
287 call.WhenTxExists = *callCfg.WhenTxExists
289 if callCfg.NoCK != nil {
290 call.NoCK = *callCfg.NoCK
292 if callCfg.AutoToss != nil {
293 call.AutoToss = *callCfg.AutoToss
295 if callCfg.AutoTossDoSeen != nil {
296 call.AutoTossDoSeen = *callCfg.AutoTossDoSeen
298 if callCfg.AutoTossNoFile != nil {
299 call.AutoTossNoFile = *callCfg.AutoTossNoFile
301 if callCfg.AutoTossNoFreq != nil {
302 call.AutoTossNoFreq = *callCfg.AutoTossNoFreq
304 if callCfg.AutoTossNoExec != nil {
305 call.AutoTossNoExec = *callCfg.AutoTossNoExec
307 if callCfg.AutoTossNoTrns != nil {
308 call.AutoTossNoTrns = *callCfg.AutoTossNoTrns
311 calls = append(calls, &call)
317 ExchPub: new([32]byte),
318 SignPub: ed25519.PublicKey(signPub),
322 FreqChunked: freqChunked,
323 FreqMinSize: freqMinSize,
324 FreqMaxSize: freqMaxSize,
329 OnlineDeadline: defOnlineDeadline,
330 MaxOnlineTime: defMaxOnlineTime,
332 copy(node.ExchPub[:], exchPub)
333 if len(noisePub) > 0 {
334 node.NoisePub = new([32]byte)
335 copy(node.NoisePub[:], noisePub)
340 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
341 id, err := NodeIdFromString(cfg.Id)
346 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
350 if len(exchPub) != 32 {
351 return nil, errors.New("Invalid exchPub size")
354 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
358 if len(exchPrv) != 32 {
359 return nil, errors.New("Invalid exchPrv size")
362 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
366 if len(signPub) != ed25519.PublicKeySize {
367 return nil, errors.New("Invalid signPub size")
370 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
374 if len(signPrv) != ed25519.PrivateKeySize {
375 return nil, errors.New("Invalid signPrv size")
378 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
382 if len(noisePub) != 32 {
383 return nil, errors.New("Invalid noisePub size")
386 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
390 if len(noisePrv) != 32 {
391 return nil, errors.New("Invalid noisePrv size")
396 ExchPub: new([32]byte),
397 ExchPrv: new([32]byte),
398 SignPub: ed25519.PublicKey(signPub),
399 SignPrv: ed25519.PrivateKey(signPrv),
400 NoisePub: new([32]byte),
401 NoisePrv: new([32]byte),
403 copy(node.ExchPub[:], exchPub)
404 copy(node.ExchPrv[:], exchPrv)
405 copy(node.NoisePub[:], noisePub)
406 copy(node.NoisePrv[:], noisePrv)
410 func CfgParse(data []byte) (*Ctx, error) {
412 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
413 os.Stderr.WriteString("Passphrase:") // #nosec G104
414 password, err := term.ReadPassword(0)
418 os.Stderr.WriteString("\n") // #nosec G104
419 data, err = DeEBlob(data, password)
424 var cfgGeneral map[string]interface{}
425 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
428 marshaled, err := json.Marshal(cfgGeneral)
433 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
436 if _, exists := cfgJSON.Neigh["self"]; !exists {
437 return nil, errors.New("self neighbour missing")
440 if cfgJSON.Self != nil {
441 self, err = NewNodeOur(cfgJSON.Self)
446 spoolPath := path.Clean(cfgJSON.Spool)
447 if !path.IsAbs(spoolPath) {
448 return nil, errors.New("Spool path must be absolute")
450 logPath := path.Clean(cfgJSON.Log)
451 if !path.IsAbs(logPath) {
452 return nil, errors.New("Log path must be absolute")
455 if cfgJSON.Umask != "" {
456 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
464 if cfgJSON.OmitPrgrs {
474 UmaskForce: umaskForce,
475 ShowPrgrs: showPrgrs,
478 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
479 Alias: make(map[string]*NodeId),
481 if cfgJSON.Notify != nil {
482 if cfgJSON.Notify.File != nil {
483 ctx.NotifyFile = cfgJSON.Notify.File
485 if cfgJSON.Notify.Freq != nil {
486 ctx.NotifyFreq = cfgJSON.Notify.Freq
488 if cfgJSON.Notify.Exec != nil {
489 ctx.NotifyExec = cfgJSON.Notify.Exec
492 vias := make(map[NodeId][]string)
493 for name, neighJSON := range cfgJSON.Neigh {
494 neigh, err := NewNode(name, neighJSON)
498 ctx.Neigh[*neigh.Id] = neigh
499 if _, already := ctx.Alias[name]; already {
500 return nil, errors.New("Node names conflict")
502 ctx.Alias[name] = neigh.Id
503 vias[*neigh.Id] = neighJSON.Via
505 ctx.SelfId = ctx.Alias["self"]
506 for neighId, viasRaw := range vias {
507 for _, viaRaw := range viasRaw {
508 foundNodeId, err := ctx.FindNode(viaRaw)
512 ctx.Neigh[neighId].Via = append(
513 ctx.Neigh[neighId].Via,