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"`
123 Notify *NotifyJSON `json:"notify,omitempty"`
125 Self *NodeOurJSON `json:"self"`
126 Neigh map[string]NodeJSON `json:"neigh"`
129 func NewNode(name string, cfg NodeJSON) (*Node, error) {
130 nodeId, err := NodeIdFromString(cfg.Id)
135 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
139 if len(exchPub) != 32 {
140 return nil, errors.New("Invalid exchPub size")
143 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
147 if len(signPub) != ed25519.PublicKeySize {
148 return nil, errors.New("Invalid signPub size")
152 if cfg.NoisePub != nil {
153 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
157 if len(noisePub) != 32 {
158 return nil, errors.New("Invalid noisePub size")
163 if cfg.Incoming != nil {
164 inc := path.Clean(*cfg.Incoming)
165 if !path.IsAbs(inc) {
166 return nil, errors.New("Incoming path must be absolute")
172 freqChunked := int64(MaxFileSize)
173 var freqMinSize int64
174 freqMaxSize := int64(MaxFileSize)
178 fPath := path.Clean(*f.Path)
179 if !path.IsAbs(fPath) {
180 return nil, errors.New("freq.path path must be absolute")
184 if f.Chunked != nil {
186 return nil, errors.New("freq.chunked value must be greater than zero")
188 freqChunked = int64(*f.Chunked) * 1024
190 if f.MinSize != nil {
191 freqMinSize = int64(*f.MinSize) * 1024
193 if f.MaxSize != nil {
194 freqMaxSize = int64(*f.MaxSize) * 1024
199 if cfg.RxRate != nil && *cfg.RxRate > 0 {
200 defRxRate = *cfg.RxRate
203 if cfg.TxRate != nil && *cfg.TxRate > 0 {
204 defTxRate = *cfg.TxRate
207 defOnlineDeadline := DefaultDeadline
208 if cfg.OnlineDeadline != nil {
209 if *cfg.OnlineDeadline <= 0 {
210 return nil, errors.New("OnlineDeadline must be at least 1 second")
212 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
214 var defMaxOnlineTime time.Duration
215 if cfg.MaxOnlineTime != nil {
216 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
220 for _, callCfg := range cfg.Calls {
221 expr, err := cronexpr.Parse(callCfg.Cron)
227 if callCfg.Nice != nil {
228 nice, err = NicenessParse(*callCfg.Nice)
235 if callCfg.Xx != nil {
242 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
247 if callCfg.RxRate != nil {
248 rxRate = *callCfg.RxRate
251 if callCfg.TxRate != nil {
252 txRate = *callCfg.TxRate
256 if callCfg.Addr != nil {
257 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
264 onlineDeadline := defOnlineDeadline
265 if callCfg.OnlineDeadline != nil {
266 if *callCfg.OnlineDeadline == 0 {
267 return nil, errors.New("OnlineDeadline must be at least 1 second")
269 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
279 OnlineDeadline: onlineDeadline,
282 if callCfg.MaxOnlineTime != nil {
283 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
285 if callCfg.WhenTxExists != nil {
286 call.WhenTxExists = *callCfg.WhenTxExists
288 if callCfg.NoCK != nil {
289 call.NoCK = *callCfg.NoCK
291 if callCfg.AutoToss != nil {
292 call.AutoToss = *callCfg.AutoToss
294 if callCfg.AutoTossDoSeen != nil {
295 call.AutoTossDoSeen = *callCfg.AutoTossDoSeen
297 if callCfg.AutoTossNoFile != nil {
298 call.AutoTossNoFile = *callCfg.AutoTossNoFile
300 if callCfg.AutoTossNoFreq != nil {
301 call.AutoTossNoFreq = *callCfg.AutoTossNoFreq
303 if callCfg.AutoTossNoExec != nil {
304 call.AutoTossNoExec = *callCfg.AutoTossNoExec
306 if callCfg.AutoTossNoTrns != nil {
307 call.AutoTossNoTrns = *callCfg.AutoTossNoTrns
310 calls = append(calls, &call)
316 ExchPub: new([32]byte),
317 SignPub: ed25519.PublicKey(signPub),
321 FreqChunked: freqChunked,
322 FreqMinSize: freqMinSize,
323 FreqMaxSize: freqMaxSize,
328 OnlineDeadline: defOnlineDeadline,
329 MaxOnlineTime: defMaxOnlineTime,
331 copy(node.ExchPub[:], exchPub)
332 if len(noisePub) > 0 {
333 node.NoisePub = new([32]byte)
334 copy(node.NoisePub[:], noisePub)
339 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
340 id, err := NodeIdFromString(cfg.Id)
345 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
349 if len(exchPub) != 32 {
350 return nil, errors.New("Invalid exchPub size")
353 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
357 if len(exchPrv) != 32 {
358 return nil, errors.New("Invalid exchPrv size")
361 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
365 if len(signPub) != ed25519.PublicKeySize {
366 return nil, errors.New("Invalid signPub size")
369 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
373 if len(signPrv) != ed25519.PrivateKeySize {
374 return nil, errors.New("Invalid signPrv size")
377 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
381 if len(noisePub) != 32 {
382 return nil, errors.New("Invalid noisePub size")
385 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
389 if len(noisePrv) != 32 {
390 return nil, errors.New("Invalid noisePrv size")
395 ExchPub: new([32]byte),
396 ExchPrv: new([32]byte),
397 SignPub: ed25519.PublicKey(signPub),
398 SignPrv: ed25519.PrivateKey(signPrv),
399 NoisePub: new([32]byte),
400 NoisePrv: new([32]byte),
402 copy(node.ExchPub[:], exchPub)
403 copy(node.ExchPrv[:], exchPrv)
404 copy(node.NoisePub[:], noisePub)
405 copy(node.NoisePrv[:], noisePrv)
409 func CfgParse(data []byte) (*Ctx, error) {
411 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
412 os.Stderr.WriteString("Passphrase:") // #nosec G104
413 password, err := term.ReadPassword(0)
417 os.Stderr.WriteString("\n") // #nosec G104
418 data, err = DeEBlob(data, password)
423 var cfgGeneral map[string]interface{}
424 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
427 marshaled, err := json.Marshal(cfgGeneral)
432 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
435 if _, exists := cfgJSON.Neigh["self"]; !exists {
436 return nil, errors.New("self neighbour missing")
439 if cfgJSON.Self != nil {
440 self, err = NewNodeOur(cfgJSON.Self)
445 spoolPath := path.Clean(cfgJSON.Spool)
446 if !path.IsAbs(spoolPath) {
447 return nil, errors.New("Spool path must be absolute")
449 logPath := path.Clean(cfgJSON.Log)
450 if !path.IsAbs(logPath) {
451 return nil, errors.New("Log path must be absolute")
454 if cfgJSON.Umask != "" {
455 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
463 if cfgJSON.OmitPrgrs {
469 UmaskForce: umaskForce,
470 ShowPrgrs: showPrgrs,
472 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
473 Alias: make(map[string]*NodeId),
475 if cfgJSON.Notify != nil {
476 if cfgJSON.Notify.File != nil {
477 ctx.NotifyFile = cfgJSON.Notify.File
479 if cfgJSON.Notify.Freq != nil {
480 ctx.NotifyFreq = cfgJSON.Notify.Freq
482 if cfgJSON.Notify.Exec != nil {
483 ctx.NotifyExec = cfgJSON.Notify.Exec
486 vias := make(map[NodeId][]string)
487 for name, neighJSON := range cfgJSON.Neigh {
488 neigh, err := NewNode(name, neighJSON)
492 ctx.Neigh[*neigh.Id] = neigh
493 if _, already := ctx.Alias[name]; already {
494 return nil, errors.New("Node names conflict")
496 ctx.Alias[name] = neigh.Id
497 vias[*neigh.Id] = neighJSON.Via
499 ctx.SelfId = ctx.Alias["self"]
500 for neighId, viasRaw := range vias {
501 for _, viaRaw := range viasRaw {
502 foundNodeId, err := ctx.FindNode(viaRaw)
506 ctx.Neigh[neighId].Via = append(
507 ctx.Neigh[neighId].Via,