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/>.
29 "github.com/gorhill/cronexpr"
30 "github.com/hjson/hjson-go"
31 "golang.org/x/crypto/ed25519"
32 "golang.org/x/crypto/ssh/terminal"
36 CfgPathEnv = "NNCPCFG"
37 CfgSpoolEnv = "NNCPSPOOL"
42 DefaultCfgPath string = "/usr/local/etc/nncp.hjson"
43 DefaultSendmailPath string = "/usr/sbin/sendmail"
44 DefaultSpoolPath string = "/var/spool/nncp"
45 DefaultLogPath string = "/var/spool/nncp/log"
48 type NodeJSON struct {
50 ExchPub string `json:"exchpub"`
51 SignPub string `json:"signpub"`
52 NoisePub *string `json:"noisepub,omitempty"`
53 Exec map[string][]string `json:"exec,omitempty"`
54 Incoming *string `json:"incoming,omitempty"`
55 Freq *NodeFreqJSON `json:"freq,omitempty"`
56 Via []string `json:"via,omitempty"`
57 Calls []CallJSON `json:"calls,omitempty"`
59 Addrs map[string]string `json:"addrs,omitempty"`
61 RxRate *int `json:"rxrate,omitempty"`
62 TxRate *int `json:"txrate,omitempty"`
63 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
64 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
67 type NodeFreqJSON struct {
68 Path *string `json:"path,omitempty"`
69 Chunked *uint64 `json:"chunked,omitempty"`
70 MinSize *uint64 `json:"minsize,omitempty"`
71 MaxSize *uint64 `json:"maxsize,omitempty"`
74 type CallJSON struct {
76 Nice *string `json:"nice,omitempty"`
77 Xx *string `json:"xx,omitempty"`
78 RxRate *int `json:"rxrate,omitempty"`
79 TxRate *int `json:"txrate,omitempty"`
80 Addr *string `json:"addr,omitempty"`
81 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
82 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
85 type NodeOurJSON struct {
87 ExchPub string `json:"exchpub"`
88 ExchPrv string `json:"exchprv"`
89 SignPub string `json:"signpub"`
90 SignPrv string `json:"signprv"`
91 NoisePrv string `json:"noiseprv"`
92 NoisePub string `json:"noisepub"`
95 type FromToJSON struct {
100 type NotifyJSON struct {
101 File *FromToJSON `json:"file,omitempty"`
102 Freq *FromToJSON `json:"freq,omitempty"`
103 Exec map[string]*FromToJSON `json:"exec,omitempty"`
106 type CfgJSON struct {
107 Spool string `json:"spool"`
108 Log string `json:"log"`
109 Umask string `json:"umask",omitempty`
111 OmitPrgrs bool `json:"noprogress",omitempty`
113 Notify *NotifyJSON `json:"notify,omitempty"`
115 Self *NodeOurJSON `json:"self"`
116 Neigh map[string]NodeJSON `json:"neigh"`
119 func NewNode(name string, cfg NodeJSON) (*Node, error) {
120 nodeId, err := NodeIdFromString(cfg.Id)
125 exchPub, err := FromBase32(cfg.ExchPub)
129 if len(exchPub) != 32 {
130 return nil, errors.New("Invalid exchPub size")
133 signPub, err := FromBase32(cfg.SignPub)
137 if len(signPub) != ed25519.PublicKeySize {
138 return nil, errors.New("Invalid signPub size")
142 if cfg.NoisePub != nil {
143 noisePub, err = FromBase32(*cfg.NoisePub)
147 if len(noisePub) != 32 {
148 return nil, errors.New("Invalid noisePub size")
153 if cfg.Incoming != nil {
154 inc := path.Clean(*cfg.Incoming)
155 if !path.IsAbs(inc) {
156 return nil, errors.New("Incoming path must be absolute")
162 freqChunked := int64(MaxFileSize)
163 var freqMinSize int64
164 freqMaxSize := int64(MaxFileSize)
168 fPath := path.Clean(*f.Path)
169 if !path.IsAbs(fPath) {
170 return nil, errors.New("freq.path path must be absolute")
174 if f.Chunked != nil {
176 return nil, errors.New("freq.chunked value must be greater than zero")
178 freqChunked = int64(*f.Chunked) * 1024
180 if f.MinSize != nil {
181 freqMinSize = int64(*f.MinSize) * 1024
183 if f.MaxSize != nil {
184 freqMaxSize = int64(*f.MaxSize) * 1024
189 if cfg.RxRate != nil && *cfg.RxRate > 0 {
190 defRxRate = *cfg.RxRate
193 if cfg.TxRate != nil && *cfg.TxRate > 0 {
194 defTxRate = *cfg.TxRate
197 defOnlineDeadline := uint(DefaultDeadline)
198 if cfg.OnlineDeadline != nil {
199 if *cfg.OnlineDeadline <= 0 {
200 return nil, errors.New("OnlineDeadline must be at least 1 second")
202 defOnlineDeadline = *cfg.OnlineDeadline
204 var defMaxOnlineTime uint
205 if yml.MaxOnlineTime != nil {
206 defMaxOnlineTime = *yml.MaxOnlineTime
210 for _, callCfg := range cfg.Calls {
211 expr, err := cronexpr.Parse(callCfg.Cron)
217 if callCfg.Nice != nil {
218 nice, err = NicenessParse(*callCfg.Nice)
225 if callCfg.Xx != nil {
232 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
237 if callCfg.RxRate != nil {
238 rxRate = *callCfg.RxRate
241 if callCfg.TxRate != nil {
242 txRate = *callCfg.TxRate
246 if callCfg.Addr != nil {
247 if a, exists := cfg.Addrs[*callCfg.Addr]; exists {
254 onlineDeadline := defOnlineDeadline
255 if callCfg.OnlineDeadline != nil {
256 if *callCfg.OnlineDeadline == 0 {
257 return nil, errors.New("OnlineDeadline must be at least 1 second")
259 onlineDeadline = *callCfg.OnlineDeadline
262 var maxOnlineTime uint
263 if callCfg.MaxOnlineTime != nil {
264 maxOnlineTime = *callCfg.MaxOnlineTime
267 calls = append(calls, &Call{
274 OnlineDeadline: onlineDeadline,
275 MaxOnlineTime: maxOnlineTime,
282 ExchPub: new([32]byte),
283 SignPub: ed25519.PublicKey(signPub),
287 FreqChunked: freqChunked,
288 FreqMinSize: freqMinSize,
289 FreqMaxSize: freqMaxSize,
294 OnlineDeadline: defOnlineDeadline,
295 MaxOnlineTime: defMaxOnlineTime,
297 copy(node.ExchPub[:], exchPub)
298 if len(noisePub) > 0 {
299 node.NoisePub = new([32]byte)
300 copy(node.NoisePub[:], noisePub)
305 func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
306 id, err := NodeIdFromString(cfg.Id)
311 exchPub, err := FromBase32(cfg.ExchPub)
315 if len(exchPub) != 32 {
316 return nil, errors.New("Invalid exchPub size")
319 exchPrv, err := FromBase32(cfg.ExchPrv)
323 if len(exchPrv) != 32 {
324 return nil, errors.New("Invalid exchPrv size")
327 signPub, err := FromBase32(cfg.SignPub)
331 if len(signPub) != ed25519.PublicKeySize {
332 return nil, errors.New("Invalid signPub size")
335 signPrv, err := FromBase32(cfg.SignPrv)
339 if len(signPrv) != ed25519.PrivateKeySize {
340 return nil, errors.New("Invalid signPrv size")
343 noisePub, err := FromBase32(cfg.NoisePub)
347 if len(noisePub) != 32 {
348 return nil, errors.New("Invalid noisePub size")
351 noisePrv, err := FromBase32(cfg.NoisePrv)
355 if len(noisePrv) != 32 {
356 return nil, errors.New("Invalid noisePrv size")
361 ExchPub: new([32]byte),
362 ExchPrv: new([32]byte),
363 SignPub: ed25519.PublicKey(signPub),
364 SignPrv: ed25519.PrivateKey(signPrv),
365 NoisePub: new([32]byte),
366 NoisePrv: new([32]byte),
368 copy(node.ExchPub[:], exchPub)
369 copy(node.ExchPrv[:], exchPrv)
370 copy(node.NoisePub[:], noisePub)
371 copy(node.NoisePrv[:], noisePrv)
375 func CfgParse(data []byte) (*Ctx, error) {
377 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
378 os.Stderr.WriteString("Passphrase:")
379 password, err := terminal.ReadPassword(0)
383 os.Stderr.WriteString("\n")
384 data, err = DeEBlob(data, password)
389 var cfgGeneral map[string]interface{}
390 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
393 marshaled, err := json.Marshal(cfgGeneral)
398 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
401 if _, exists := cfgJSON.Neigh["self"]; !exists {
402 return nil, errors.New("self neighbour missing")
405 if cfgJSON.Self != nil {
406 self, err = NewNodeOur(cfgJSON.Self)
411 spoolPath := path.Clean(cfgJSON.Spool)
412 if !path.IsAbs(spoolPath) {
413 return nil, errors.New("Spool path must be absolute")
415 logPath := path.Clean(cfgJSON.Log)
416 if !path.IsAbs(logPath) {
417 return nil, errors.New("Log path must be absolute")
420 if cfgJSON.Umask != "" {
421 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
429 if cfgJSON.OmitPrgrs {
435 UmaskForce: umaskForce,
436 ShowPrgrs: showPrgrs,
438 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
439 Alias: make(map[string]*NodeId),
441 if cfgJSON.Notify != nil {
442 if cfgJSON.Notify.File != nil {
443 ctx.NotifyFile = cfgJSON.Notify.File
445 if cfgJSON.Notify.Freq != nil {
446 ctx.NotifyFreq = cfgJSON.Notify.Freq
448 if cfgJSON.Notify.Exec != nil {
449 ctx.NotifyExec = cfgJSON.Notify.Exec
452 vias := make(map[NodeId][]string)
453 for name, neighJSON := range cfgJSON.Neigh {
454 neigh, err := NewNode(name, neighJSON)
458 ctx.Neigh[*neigh.Id] = neigh
459 if _, already := ctx.Alias[name]; already {
460 return nil, errors.New("Node names conflict")
462 ctx.Alias[name] = neigh.Id
463 vias[*neigh.Id] = neighJSON.Via
465 ctx.SelfId = ctx.Alias["self"]
466 for neighId, viasRaw := range vias {
467 for _, viaRaw := range viasRaw {
468 foundNodeId, err := ctx.FindNode(viaRaw)
472 ctx.Neigh[neighId].Via = append(
473 ctx.Neigh[neighId].Via,