2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 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"
33 "golang.org/x/crypto/ssh/terminal"
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"`
86 type NodeOurJSON struct {
88 ExchPub string `json:"exchpub"`
89 ExchPrv string `json:"exchprv"`
90 SignPub string `json:"signpub"`
91 SignPrv string `json:"signprv"`
92 NoisePrv string `json:"noiseprv"`
93 NoisePub string `json:"noisepub"`
96 type FromToJSON struct {
101 type NotifyJSON struct {
102 File *FromToJSON `json:"file,omitempty"`
103 Freq *FromToJSON `json:"freq,omitempty"`
104 Exec map[string]*FromToJSON `json:"exec,omitempty"`
107 type CfgJSON struct {
108 Spool string `json:"spool"`
109 Log string `json:"log"`
110 Umask string `json:"umask",omitempty`
112 OmitPrgrs bool `json:"noprogress",omitempty`
114 Notify *NotifyJSON `json:"notify,omitempty"`
116 Self *NodeOurJSON `json:"self"`
117 Neigh map[string]NodeJSON `json:"neigh"`
120 func NewNode(name string, cfg NodeJSON) (*Node, error) {
121 nodeId, err := NodeIdFromString(cfg.Id)
126 exchPub, err := FromBase32(cfg.ExchPub)
130 if len(exchPub) != 32 {
131 return nil, errors.New("Invalid exchPub size")
134 signPub, err := FromBase32(cfg.SignPub)
138 if len(signPub) != ed25519.PublicKeySize {
139 return nil, errors.New("Invalid signPub size")
143 if cfg.NoisePub != nil {
144 noisePub, err = FromBase32(*cfg.NoisePub)
148 if len(noisePub) != 32 {
149 return nil, errors.New("Invalid noisePub size")
154 if cfg.Incoming != nil {
155 inc := path.Clean(*cfg.Incoming)
156 if !path.IsAbs(inc) {
157 return nil, errors.New("Incoming path must be absolute")
163 freqChunked := int64(MaxFileSize)
164 var freqMinSize int64
165 freqMaxSize := int64(MaxFileSize)
169 fPath := path.Clean(*f.Path)
170 if !path.IsAbs(fPath) {
171 return nil, errors.New("freq.path path must be absolute")
175 if f.Chunked != nil {
177 return nil, errors.New("freq.chunked value must be greater than zero")
179 freqChunked = int64(*f.Chunked) * 1024
181 if f.MinSize != nil {
182 freqMinSize = int64(*f.MinSize) * 1024
184 if f.MaxSize != nil {
185 freqMaxSize = int64(*f.MaxSize) * 1024
190 if cfg.RxRate != nil && *cfg.RxRate > 0 {
191 defRxRate = *cfg.RxRate
194 if cfg.TxRate != nil && *cfg.TxRate > 0 {
195 defTxRate = *cfg.TxRate
198 defOnlineDeadline := uint(DefaultDeadline)
199 if cfg.OnlineDeadline != nil {
200 if *cfg.OnlineDeadline <= 0 {
201 return nil, errors.New("OnlineDeadline must be at least 1 second")
203 defOnlineDeadline = *cfg.OnlineDeadline
205 var defMaxOnlineTime time.Duration
206 if cfg.MaxOnlineTime != nil {
207 defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
211 for _, callCfg := range cfg.Calls {
212 expr, err := cronexpr.Parse(callCfg.Cron)
218 if callCfg.Nice != nil {
219 nice, err = NicenessParse(*callCfg.Nice)
226 if callCfg.Xx != nil {
233 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
238 if callCfg.RxRate != nil {
239 rxRate = *callCfg.RxRate
242 if callCfg.TxRate != nil {
243 txRate = *callCfg.TxRate
247 if callCfg.Addr != nil {
248 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
255 onlineDeadline := defOnlineDeadline
256 if callCfg.OnlineDeadline != nil {
257 if *callCfg.OnlineDeadline == 0 {
258 return nil, errors.New("OnlineDeadline must be at least 1 second")
260 onlineDeadline = *callCfg.OnlineDeadline
263 var maxOnlineTime time.Duration
264 if callCfg.MaxOnlineTime != nil {
265 maxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
268 calls = append(calls, &Call{
275 OnlineDeadline: onlineDeadline,
276 MaxOnlineTime: maxOnlineTime,
283 ExchPub: new([32]byte),
284 SignPub: ed25519.PublicKey(signPub),
288 FreqChunked: freqChunked,
289 FreqMinSize: freqMinSize,
290 FreqMaxSize: freqMaxSize,
295 OnlineDeadline: defOnlineDeadline,
296 MaxOnlineTime: defMaxOnlineTime,
298 copy(node.ExchPub[:], exchPub)
299 if len(noisePub) > 0 {
300 node.NoisePub = new([32]byte)
301 copy(node.NoisePub[:], noisePub)
306 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
307 id, err := NodeIdFromString(cfg.Id)
312 exchPub, err := FromBase32(cfg.ExchPub)
316 if len(exchPub) != 32 {
317 return nil, errors.New("Invalid exchPub size")
320 exchPrv, err := FromBase32(cfg.ExchPrv)
324 if len(exchPrv) != 32 {
325 return nil, errors.New("Invalid exchPrv size")
328 signPub, err := FromBase32(cfg.SignPub)
332 if len(signPub) != ed25519.PublicKeySize {
333 return nil, errors.New("Invalid signPub size")
336 signPrv, err := FromBase32(cfg.SignPrv)
340 if len(signPrv) != ed25519.PrivateKeySize {
341 return nil, errors.New("Invalid signPrv size")
344 noisePub, err := FromBase32(cfg.NoisePub)
348 if len(noisePub) != 32 {
349 return nil, errors.New("Invalid noisePub size")
352 noisePrv, err := FromBase32(cfg.NoisePrv)
356 if len(noisePrv) != 32 {
357 return nil, errors.New("Invalid noisePrv size")
362 ExchPub: new([32]byte),
363 ExchPrv: new([32]byte),
364 SignPub: ed25519.PublicKey(signPub),
365 SignPrv: ed25519.PrivateKey(signPrv),
366 NoisePub: new([32]byte),
367 NoisePrv: new([32]byte),
369 copy(node.ExchPub[:], exchPub)
370 copy(node.ExchPrv[:], exchPrv)
371 copy(node.NoisePub[:], noisePub)
372 copy(node.NoisePrv[:], noisePrv)
376 func CfgParse(data []byte) (*Ctx, error) {
378 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
379 os.Stderr.WriteString("Passphrase:")
380 password, err := terminal.ReadPassword(0)
384 os.Stderr.WriteString("\n")
385 data, err = DeEBlob(data, password)
390 var cfgGeneral map[string]interface{}
391 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
394 marshaled, err := json.Marshal(cfgGeneral)
399 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
402 if _, exists := cfgJSON.Neigh["self"]; !exists {
403 return nil, errors.New("self neighbour missing")
406 if cfgJSON.Self != nil {
407 self, err = NewNodeOur(cfgJSON.Self)
412 spoolPath := path.Clean(cfgJSON.Spool)
413 if !path.IsAbs(spoolPath) {
414 return nil, errors.New("Spool path must be absolute")
416 logPath := path.Clean(cfgJSON.Log)
417 if !path.IsAbs(logPath) {
418 return nil, errors.New("Log path must be absolute")
421 if cfgJSON.Umask != "" {
422 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
430 if cfgJSON.OmitPrgrs {
436 UmaskForce: umaskForce,
437 ShowPrgrs: showPrgrs,
439 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
440 Alias: make(map[string]*NodeId),
442 if cfgJSON.Notify != nil {
443 if cfgJSON.Notify.File != nil {
444 ctx.NotifyFile = cfgJSON.Notify.File
446 if cfgJSON.Notify.Freq != nil {
447 ctx.NotifyFreq = cfgJSON.Notify.Freq
449 if cfgJSON.Notify.Exec != nil {
450 ctx.NotifyExec = cfgJSON.Notify.Exec
453 vias := make(map[NodeId][]string)
454 for name, neighJSON := range cfgJSON.Neigh {
455 neigh, err := NewNode(name, neighJSON)
459 ctx.Neigh[*neigh.Id] = neigh
460 if _, already := ctx.Alias[name]; already {
461 return nil, errors.New("Node names conflict")
463 ctx.Alias[name] = neigh.Id
464 vias[*neigh.Id] = neighJSON.Via
466 ctx.SelfId = ctx.Alias["self"]
467 for neighId, viasRaw := range vias {
468 for _, viaRaw := range viasRaw {
469 foundNodeId, err := ctx.FindNode(viaRaw)
473 ctx.Neigh[neighId].Via = append(
474 ctx.Neigh[neighId].Via,