2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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 Notify *NotifyJSON `json:"notify,omitempty"`
113 Self *NodeOurJSON `json:"self"`
114 Neigh map[string]NodeJSON `json:"neigh"`
117 func NewNode(name string, yml NodeJSON) (*Node, error) {
118 nodeId, err := NodeIdFromString(yml.Id)
123 exchPub, err := FromBase32(yml.ExchPub)
127 if len(exchPub) != 32 {
128 return nil, errors.New("Invalid exchPub size")
131 signPub, err := FromBase32(yml.SignPub)
135 if len(signPub) != ed25519.PublicKeySize {
136 return nil, errors.New("Invalid signPub size")
140 if yml.NoisePub != nil {
141 noisePub, err = FromBase32(*yml.NoisePub)
145 if len(noisePub) != 32 {
146 return nil, errors.New("Invalid noisePub size")
151 if yml.Incoming != nil {
152 inc := path.Clean(*yml.Incoming)
153 if !path.IsAbs(inc) {
154 return nil, errors.New("Incoming path must be absolute")
160 freqChunked := int64(MaxFileSize)
161 var freqMinSize int64
162 freqMaxSize := int64(MaxFileSize)
166 fPath := path.Clean(*f.Path)
167 if !path.IsAbs(fPath) {
168 return nil, errors.New("freq.path path must be absolute")
172 if f.Chunked != nil {
174 return nil, errors.New("freq.chunked value must be greater than zero")
176 freqChunked = int64(*f.Chunked) * 1024
178 if f.MinSize != nil {
179 freqMinSize = int64(*f.MinSize) * 1024
181 if f.MaxSize != nil {
182 freqMaxSize = int64(*f.MaxSize) * 1024
187 if yml.RxRate != nil && *yml.RxRate > 0 {
188 defRxRate = *yml.RxRate
191 if yml.TxRate != nil && *yml.TxRate > 0 {
192 defTxRate = *yml.TxRate
195 defOnlineDeadline := uint(DefaultDeadline)
196 if yml.OnlineDeadline != nil {
197 if *yml.OnlineDeadline <= 0 {
198 return nil, errors.New("OnlineDeadline must be at least 1 second")
200 defOnlineDeadline = *yml.OnlineDeadline
202 var defMaxOnlineTime uint
203 if yml.MaxOnlineTime != nil {
204 defMaxOnlineTime = *yml.MaxOnlineTime
208 for _, callYml := range yml.Calls {
209 expr, err := cronexpr.Parse(callYml.Cron)
215 if callYml.Nice != nil {
216 nice, err = NicenessParse(*callYml.Nice)
223 if callYml.Xx != nil {
230 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
235 if callYml.RxRate != nil {
236 rxRate = *callYml.RxRate
239 if callYml.TxRate != nil {
240 txRate = *callYml.TxRate
244 if callYml.Addr != nil {
245 if a, exists := yml.Addrs[*callYml.Addr]; exists {
252 onlineDeadline := defOnlineDeadline
253 if callYml.OnlineDeadline != nil {
254 if *callYml.OnlineDeadline == 0 {
255 return nil, errors.New("OnlineDeadline must be at least 1 second")
257 onlineDeadline = *callYml.OnlineDeadline
260 var maxOnlineTime uint
261 if callYml.MaxOnlineTime != nil {
262 maxOnlineTime = *callYml.MaxOnlineTime
265 calls = append(calls, &Call{
272 OnlineDeadline: onlineDeadline,
273 MaxOnlineTime: maxOnlineTime,
280 ExchPub: new([32]byte),
281 SignPub: ed25519.PublicKey(signPub),
285 FreqChunked: freqChunked,
286 FreqMinSize: freqMinSize,
287 FreqMaxSize: freqMaxSize,
292 OnlineDeadline: defOnlineDeadline,
293 MaxOnlineTime: defMaxOnlineTime,
295 copy(node.ExchPub[:], exchPub)
296 if len(noisePub) > 0 {
297 node.NoisePub = new([32]byte)
298 copy(node.NoisePub[:], noisePub)
303 func NewNodeOur(yml *NodeOurJSON) (*NodeOur, error) {
304 id, err := NodeIdFromString(yml.Id)
309 exchPub, err := FromBase32(yml.ExchPub)
313 if len(exchPub) != 32 {
314 return nil, errors.New("Invalid exchPub size")
317 exchPrv, err := FromBase32(yml.ExchPrv)
321 if len(exchPrv) != 32 {
322 return nil, errors.New("Invalid exchPrv size")
325 signPub, err := FromBase32(yml.SignPub)
329 if len(signPub) != ed25519.PublicKeySize {
330 return nil, errors.New("Invalid signPub size")
333 signPrv, err := FromBase32(yml.SignPrv)
337 if len(signPrv) != ed25519.PrivateKeySize {
338 return nil, errors.New("Invalid signPrv size")
341 noisePub, err := FromBase32(yml.NoisePub)
345 if len(noisePub) != 32 {
346 return nil, errors.New("Invalid noisePub size")
349 noisePrv, err := FromBase32(yml.NoisePrv)
353 if len(noisePrv) != 32 {
354 return nil, errors.New("Invalid noisePrv size")
359 ExchPub: new([32]byte),
360 ExchPrv: new([32]byte),
361 SignPub: ed25519.PublicKey(signPub),
362 SignPrv: ed25519.PrivateKey(signPrv),
363 NoisePub: new([32]byte),
364 NoisePrv: new([32]byte),
366 copy(node.ExchPub[:], exchPub)
367 copy(node.ExchPrv[:], exchPrv)
368 copy(node.NoisePub[:], noisePub)
369 copy(node.NoisePrv[:], noisePrv)
373 func CfgParse(data []byte) (*Ctx, error) {
375 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
376 os.Stderr.WriteString("Passphrase:")
377 password, err := terminal.ReadPassword(0)
381 os.Stderr.WriteString("\n")
382 data, err = DeEBlob(data, password)
387 var cfgGeneral map[string]interface{}
388 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
391 marshaled, err := json.Marshal(cfgGeneral)
396 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
399 if _, exists := cfgJSON.Neigh["self"]; !exists {
400 return nil, errors.New("self neighbour missing")
403 if cfgJSON.Self != nil {
404 self, err = NewNodeOur(cfgJSON.Self)
409 spoolPath := path.Clean(cfgJSON.Spool)
410 if !path.IsAbs(spoolPath) {
411 return nil, errors.New("Spool path must be absolute")
413 logPath := path.Clean(cfgJSON.Log)
414 if !path.IsAbs(logPath) {
415 return nil, errors.New("Log path must be absolute")
418 if cfgJSON.Umask != "" {
419 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
429 UmaskForce: umaskForce,
431 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
432 Alias: make(map[string]*NodeId),
434 if cfgJSON.Notify != nil {
435 if cfgJSON.Notify.File != nil {
436 ctx.NotifyFile = cfgJSON.Notify.File
438 if cfgJSON.Notify.Freq != nil {
439 ctx.NotifyFreq = cfgJSON.Notify.Freq
441 if cfgJSON.Notify.Exec != nil {
442 ctx.NotifyExec = cfgJSON.Notify.Exec
445 vias := make(map[NodeId][]string)
446 for name, neighJSON := range cfgJSON.Neigh {
447 neigh, err := NewNode(name, neighJSON)
451 ctx.Neigh[*neigh.Id] = neigh
452 if _, already := ctx.Alias[name]; already {
453 return nil, errors.New("Node names conflict")
455 ctx.Alias[name] = neigh.Id
456 vias[*neigh.Id] = neighJSON.Via
458 ctx.SelfId = ctx.Alias["self"]
459 for neighId, viasRaw := range vias {
460 for _, viaRaw := range viasRaw {
461 foundNodeId, err := ctx.FindNode(viaRaw)
465 ctx.Neigh[neighId].Via = append(
466 ctx.Neigh[neighId].Via,