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"`
105 type CfgJSON struct {
106 Spool string `json:"spool"`
107 Log string `json:"log"`
108 Umask string `json:"umask",omitempty`
110 Notify *NotifyJSON `json:"notify,omitempty"`
112 Self *NodeOurJSON `json:"self"`
113 Neigh map[string]NodeJSON `json:"neigh"`
116 func NewNode(name string, yml NodeJSON) (*Node, error) {
117 nodeId, err := NodeIdFromString(yml.Id)
122 exchPub, err := FromBase32(yml.ExchPub)
126 if len(exchPub) != 32 {
127 return nil, errors.New("Invalid exchPub size")
130 signPub, err := FromBase32(yml.SignPub)
134 if len(signPub) != ed25519.PublicKeySize {
135 return nil, errors.New("Invalid signPub size")
139 if yml.NoisePub != nil {
140 noisePub, err = FromBase32(*yml.NoisePub)
144 if len(noisePub) != 32 {
145 return nil, errors.New("Invalid noisePub size")
150 if yml.Incoming != nil {
151 inc := path.Clean(*yml.Incoming)
152 if !path.IsAbs(inc) {
153 return nil, errors.New("Incoming path must be absolute")
159 freqChunked := int64(MaxFileSize)
160 var freqMinSize int64
161 freqMaxSize := int64(MaxFileSize)
165 fPath := path.Clean(*f.Path)
166 if !path.IsAbs(fPath) {
167 return nil, errors.New("freq.path path must be absolute")
171 if f.Chunked != nil {
173 return nil, errors.New("freq.chunked value must be greater than zero")
175 freqChunked = int64(*f.Chunked) * 1024
177 if f.MinSize != nil {
178 freqMinSize = int64(*f.MinSize) * 1024
180 if f.MaxSize != nil {
181 freqMaxSize = int64(*f.MaxSize) * 1024
186 if yml.RxRate != nil && *yml.RxRate > 0 {
187 defRxRate = *yml.RxRate
190 if yml.TxRate != nil && *yml.TxRate > 0 {
191 defTxRate = *yml.TxRate
194 defOnlineDeadline := uint(DefaultDeadline)
195 if yml.OnlineDeadline != nil {
196 if *yml.OnlineDeadline <= 0 {
197 return nil, errors.New("OnlineDeadline must be at least 1 second")
199 defOnlineDeadline = *yml.OnlineDeadline
201 var defMaxOnlineTime uint
202 if yml.MaxOnlineTime != nil {
203 defMaxOnlineTime = *yml.MaxOnlineTime
207 for _, callYml := range yml.Calls {
208 expr, err := cronexpr.Parse(callYml.Cron)
214 if callYml.Nice != nil {
215 nice, err = NicenessParse(*callYml.Nice)
222 if callYml.Xx != nil {
229 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
234 if callYml.RxRate != nil {
235 rxRate = *callYml.RxRate
238 if callYml.TxRate != nil {
239 txRate = *callYml.TxRate
243 if callYml.Addr != nil {
244 if a, exists := yml.Addrs[*callYml.Addr]; exists {
251 onlineDeadline := defOnlineDeadline
252 if callYml.OnlineDeadline != nil {
253 if *callYml.OnlineDeadline == 0 {
254 return nil, errors.New("OnlineDeadline must be at least 1 second")
256 onlineDeadline = *callYml.OnlineDeadline
259 var maxOnlineTime uint
260 if callYml.MaxOnlineTime != nil {
261 maxOnlineTime = *callYml.MaxOnlineTime
264 calls = append(calls, &Call{
271 OnlineDeadline: onlineDeadline,
272 MaxOnlineTime: maxOnlineTime,
279 ExchPub: new([32]byte),
280 SignPub: ed25519.PublicKey(signPub),
284 FreqChunked: freqChunked,
285 FreqMinSize: freqMinSize,
286 FreqMaxSize: freqMaxSize,
291 OnlineDeadline: defOnlineDeadline,
292 MaxOnlineTime: defMaxOnlineTime,
294 copy(node.ExchPub[:], exchPub)
295 if len(noisePub) > 0 {
296 node.NoisePub = new([32]byte)
297 copy(node.NoisePub[:], noisePub)
302 func NewNodeOur(yml *NodeOurJSON) (*NodeOur, error) {
303 id, err := NodeIdFromString(yml.Id)
308 exchPub, err := FromBase32(yml.ExchPub)
312 if len(exchPub) != 32 {
313 return nil, errors.New("Invalid exchPub size")
316 exchPrv, err := FromBase32(yml.ExchPrv)
320 if len(exchPrv) != 32 {
321 return nil, errors.New("Invalid exchPrv size")
324 signPub, err := FromBase32(yml.SignPub)
328 if len(signPub) != ed25519.PublicKeySize {
329 return nil, errors.New("Invalid signPub size")
332 signPrv, err := FromBase32(yml.SignPrv)
336 if len(signPrv) != ed25519.PrivateKeySize {
337 return nil, errors.New("Invalid signPrv size")
340 noisePub, err := FromBase32(yml.NoisePub)
344 if len(noisePub) != 32 {
345 return nil, errors.New("Invalid noisePub size")
348 noisePrv, err := FromBase32(yml.NoisePrv)
352 if len(noisePrv) != 32 {
353 return nil, errors.New("Invalid noisePrv size")
358 ExchPub: new([32]byte),
359 ExchPrv: new([32]byte),
360 SignPub: ed25519.PublicKey(signPub),
361 SignPrv: ed25519.PrivateKey(signPrv),
362 NoisePub: new([32]byte),
363 NoisePrv: new([32]byte),
365 copy(node.ExchPub[:], exchPub)
366 copy(node.ExchPrv[:], exchPrv)
367 copy(node.NoisePub[:], noisePub)
368 copy(node.NoisePrv[:], noisePrv)
372 func CfgParse(data []byte) (*Ctx, error) {
374 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
375 os.Stderr.WriteString("Passphrase:")
376 password, err := terminal.ReadPassword(0)
380 os.Stderr.WriteString("\n")
381 data, err = DeEBlob(data, password)
386 var cfgGeneral map[string]interface{}
387 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
390 marshaled, err := json.Marshal(cfgGeneral)
395 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
398 if _, exists := cfgJSON.Neigh["self"]; !exists {
399 return nil, errors.New("self neighbour missing")
402 if cfgJSON.Self != nil {
403 self, err = NewNodeOur(cfgJSON.Self)
408 spoolPath := path.Clean(cfgJSON.Spool)
409 if !path.IsAbs(spoolPath) {
410 return nil, errors.New("Spool path must be absolute")
412 logPath := path.Clean(cfgJSON.Log)
413 if !path.IsAbs(logPath) {
414 return nil, errors.New("Log path must be absolute")
417 if cfgJSON.Umask != "" {
418 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
428 UmaskForce: umaskForce,
430 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
431 Alias: make(map[string]*NodeId),
433 if cfgJSON.Notify != nil {
434 if cfgJSON.Notify.File != nil {
435 ctx.NotifyFile = cfgJSON.Notify.File
437 if cfgJSON.Notify.Freq != nil {
438 ctx.NotifyFreq = cfgJSON.Notify.Freq
441 vias := make(map[NodeId][]string)
442 for name, neighJSON := range cfgJSON.Neigh {
443 neigh, err := NewNode(name, neighJSON)
447 ctx.Neigh[*neigh.Id] = neigh
448 if _, already := ctx.Alias[name]; already {
449 return nil, errors.New("Node names conflict")
451 ctx.Alias[name] = neigh.Id
452 vias[*neigh.Id] = neighJSON.Via
454 ctx.SelfId = ctx.Alias["self"]
455 for neighId, viasRaw := range vias {
456 for _, viaRaw := range viasRaw {
457 foundNodeId, err := ctx.FindNode(viaRaw)
461 ctx.Neigh[neighId].Via = append(
462 ctx.Neigh[neighId].Via,