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"`
85 AutoToss *bool `json:"autotoss,omitempty"`
86 AutoTossDoSeen *bool `json:"autotoss-doseen,omitempty"`
87 AutoTossNoFile *bool `json:"autotoss-nofile,omitempty"`
88 AutoTossNoFreq *bool `json:"autotoss-nofreq,omitempty"`
89 AutoTossNoExec *bool `json:"autotoss-noexec,omitempty"`
90 AutoTossNoTrns *bool `json:"autotoss-notrns,omitempty"`
93 type NodeOurJSON struct {
95 ExchPub string `json:"exchpub"`
96 ExchPrv string `json:"exchprv"`
97 SignPub string `json:"signpub"`
98 SignPrv string `json:"signprv"`
99 NoisePrv string `json:"noiseprv"`
100 NoisePub string `json:"noisepub"`
103 type FromToJSON struct {
108 type NotifyJSON struct {
109 File *FromToJSON `json:"file,omitempty"`
110 Freq *FromToJSON `json:"freq,omitempty"`
111 Exec map[string]*FromToJSON `json:"exec,omitempty"`
114 type CfgJSON struct {
115 Spool string `json:"spool"`
116 Log string `json:"log"`
117 Umask string `json:"umask,omitempty"`
119 OmitPrgrs bool `json:"noprogress,omitempty"`
121 Notify *NotifyJSON `json:"notify,omitempty"`
123 Self *NodeOurJSON `json:"self"`
124 Neigh map[string]NodeJSON `json:"neigh"`
127 func NewNode(name string, cfg NodeJSON) (*Node, error) {
128 nodeId, err := NodeIdFromString(cfg.Id)
133 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
137 if len(exchPub) != 32 {
138 return nil, errors.New("Invalid exchPub size")
141 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
145 if len(signPub) != ed25519.PublicKeySize {
146 return nil, errors.New("Invalid signPub size")
150 if cfg.NoisePub != nil {
151 noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
155 if len(noisePub) != 32 {
156 return nil, errors.New("Invalid noisePub size")
161 if cfg.Incoming != nil {
162 inc := path.Clean(*cfg.Incoming)
163 if !path.IsAbs(inc) {
164 return nil, errors.New("Incoming path must be absolute")
170 freqChunked := int64(MaxFileSize)
171 var freqMinSize int64
172 freqMaxSize := int64(MaxFileSize)
176 fPath := path.Clean(*f.Path)
177 if !path.IsAbs(fPath) {
178 return nil, errors.New("freq.path path must be absolute")
182 if f.Chunked != nil {
184 return nil, errors.New("freq.chunked value must be greater than zero")
186 freqChunked = int64(*f.Chunked) * 1024
188 if f.MinSize != nil {
189 freqMinSize = int64(*f.MinSize) * 1024
191 if f.MaxSize != nil {
192 freqMaxSize = int64(*f.MaxSize) * 1024
197 if cfg.RxRate != nil && *cfg.RxRate > 0 {
198 defRxRate = *cfg.RxRate
201 if cfg.TxRate != nil && *cfg.TxRate > 0 {
202 defTxRate = *cfg.TxRate
205 defOnlineDeadline := DefaultDeadline
206 if cfg.OnlineDeadline != nil {
207 if *cfg.OnlineDeadline <= 0 {
208 return nil, errors.New("OnlineDeadline must be at least 1 second")
210 defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
212 var defMaxOnlineTime time.Duration
213 if cfg.MaxOnlineTime != nil {
214 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
218 for _, callCfg := range cfg.Calls {
219 expr, err := cronexpr.Parse(callCfg.Cron)
225 if callCfg.Nice != nil {
226 nice, err = NicenessParse(*callCfg.Nice)
233 if callCfg.Xx != nil {
240 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
245 if callCfg.RxRate != nil {
246 rxRate = *callCfg.RxRate
249 if callCfg.TxRate != nil {
250 txRate = *callCfg.TxRate
254 if callCfg.Addr != nil {
255 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
262 onlineDeadline := defOnlineDeadline
263 if callCfg.OnlineDeadline != nil {
264 if *callCfg.OnlineDeadline == 0 {
265 return nil, errors.New("OnlineDeadline must be at least 1 second")
267 onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
277 OnlineDeadline: onlineDeadline,
280 if callCfg.MaxOnlineTime != nil {
281 call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
283 if callCfg.AutoToss != nil {
284 call.AutoToss = *callCfg.AutoToss
286 if callCfg.AutoTossDoSeen != nil {
287 call.AutoTossDoSeen = *callCfg.AutoTossDoSeen
289 if callCfg.AutoTossNoFile != nil {
290 call.AutoTossNoFile = *callCfg.AutoTossNoFile
292 if callCfg.AutoTossNoFreq != nil {
293 call.AutoTossNoFreq = *callCfg.AutoTossNoFreq
295 if callCfg.AutoTossNoExec != nil {
296 call.AutoTossNoExec = *callCfg.AutoTossNoExec
298 if callCfg.AutoTossNoTrns != nil {
299 call.AutoTossNoTrns = *callCfg.AutoTossNoTrns
302 calls = append(calls, &call)
308 ExchPub: new([32]byte),
309 SignPub: ed25519.PublicKey(signPub),
313 FreqChunked: freqChunked,
314 FreqMinSize: freqMinSize,
315 FreqMaxSize: freqMaxSize,
320 OnlineDeadline: defOnlineDeadline,
321 MaxOnlineTime: defMaxOnlineTime,
323 copy(node.ExchPub[:], exchPub)
324 if len(noisePub) > 0 {
325 node.NoisePub = new([32]byte)
326 copy(node.NoisePub[:], noisePub)
331 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
332 id, err := NodeIdFromString(cfg.Id)
337 exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
341 if len(exchPub) != 32 {
342 return nil, errors.New("Invalid exchPub size")
345 exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
349 if len(exchPrv) != 32 {
350 return nil, errors.New("Invalid exchPrv size")
353 signPub, err := Base32Codec.DecodeString(cfg.SignPub)
357 if len(signPub) != ed25519.PublicKeySize {
358 return nil, errors.New("Invalid signPub size")
361 signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
365 if len(signPrv) != ed25519.PrivateKeySize {
366 return nil, errors.New("Invalid signPrv size")
369 noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
373 if len(noisePub) != 32 {
374 return nil, errors.New("Invalid noisePub size")
377 noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
381 if len(noisePrv) != 32 {
382 return nil, errors.New("Invalid noisePrv size")
387 ExchPub: new([32]byte),
388 ExchPrv: new([32]byte),
389 SignPub: ed25519.PublicKey(signPub),
390 SignPrv: ed25519.PrivateKey(signPrv),
391 NoisePub: new([32]byte),
392 NoisePrv: new([32]byte),
394 copy(node.ExchPub[:], exchPub)
395 copy(node.ExchPrv[:], exchPrv)
396 copy(node.NoisePub[:], noisePub)
397 copy(node.NoisePrv[:], noisePrv)
401 func CfgParse(data []byte) (*Ctx, error) {
403 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
404 os.Stderr.WriteString("Passphrase:") // #nosec G104
405 password, err := term.ReadPassword(0)
409 os.Stderr.WriteString("\n") // #nosec G104
410 data, err = DeEBlob(data, password)
415 var cfgGeneral map[string]interface{}
416 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
419 marshaled, err := json.Marshal(cfgGeneral)
424 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
427 if _, exists := cfgJSON.Neigh["self"]; !exists {
428 return nil, errors.New("self neighbour missing")
431 if cfgJSON.Self != nil {
432 self, err = NewNodeOur(cfgJSON.Self)
437 spoolPath := path.Clean(cfgJSON.Spool)
438 if !path.IsAbs(spoolPath) {
439 return nil, errors.New("Spool path must be absolute")
441 logPath := path.Clean(cfgJSON.Log)
442 if !path.IsAbs(logPath) {
443 return nil, errors.New("Log path must be absolute")
446 if cfgJSON.Umask != "" {
447 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
455 if cfgJSON.OmitPrgrs {
461 UmaskForce: umaskForce,
462 ShowPrgrs: showPrgrs,
464 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
465 Alias: make(map[string]*NodeId),
467 if cfgJSON.Notify != nil {
468 if cfgJSON.Notify.File != nil {
469 ctx.NotifyFile = cfgJSON.Notify.File
471 if cfgJSON.Notify.Freq != nil {
472 ctx.NotifyFreq = cfgJSON.Notify.Freq
474 if cfgJSON.Notify.Exec != nil {
475 ctx.NotifyExec = cfgJSON.Notify.Exec
478 vias := make(map[NodeId][]string)
479 for name, neighJSON := range cfgJSON.Neigh {
480 neigh, err := NewNode(name, neighJSON)
484 ctx.Neigh[*neigh.Id] = neigh
485 if _, already := ctx.Alias[name]; already {
486 return nil, errors.New("Node names conflict")
488 ctx.Alias[name] = neigh.Id
489 vias[*neigh.Id] = neighJSON.Via
491 ctx.SelfId = ctx.Alias["self"]
492 for neighId, viasRaw := range vias {
493 for _, viaRaw := range viasRaw {
494 foundNodeId, err := ctx.FindNode(viaRaw)
498 ctx.Neigh[neighId].Via = append(
499 ctx.Neigh[neighId].Via,