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"`
73 type CallJSON struct {
75 Nice *string `json:"nice,omitempty"`
76 Xx *string `json:"xx,omitempty"`
77 RxRate *int `json:"rxrate,omitempty"`
78 TxRate *int `json:"txrate,omitempty"`
79 Addr *string `json:"addr,omitempty"`
80 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
81 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
84 type NodeOurJSON struct {
86 ExchPub string `json:"exchpub"`
87 ExchPrv string `json:"exchprv"`
88 SignPub string `json:"signpub"`
89 SignPrv string `json:"signprv"`
90 NoisePrv string `json:"noiseprv"`
91 NoisePub string `json:"noisepub"`
94 type FromToJSON struct {
99 type NotifyJSON struct {
100 File *FromToJSON `json:"file,omitempty"`
101 Freq *FromToJSON `json:"freq,omitempty"`
104 type CfgJSON struct {
105 Spool string `json:"spool"`
106 Log string `json:"log"`
107 Umask string `json:"umask",omitempty`
109 Notify *NotifyJSON `json:"notify,omitempty"`
111 Self *NodeOurJSON `json:"self"`
112 Neigh map[string]NodeJSON `json:"neigh"`
115 func NewNode(name string, yml NodeJSON) (*Node, error) {
116 nodeId, err := NodeIdFromString(yml.Id)
121 exchPub, err := FromBase32(yml.ExchPub)
125 if len(exchPub) != 32 {
126 return nil, errors.New("Invalid exchPub size")
129 signPub, err := FromBase32(yml.SignPub)
133 if len(signPub) != ed25519.PublicKeySize {
134 return nil, errors.New("Invalid signPub size")
138 if yml.NoisePub != nil {
139 noisePub, err = FromBase32(*yml.NoisePub)
143 if len(noisePub) != 32 {
144 return nil, errors.New("Invalid noisePub size")
149 if yml.Incoming != nil {
150 inc := path.Clean(*yml.Incoming)
151 if !path.IsAbs(inc) {
152 return nil, errors.New("Incoming path must be absolute")
158 var freqChunked int64
159 var freqMinSize int64
163 fPath := path.Clean(*f.Path)
164 if !path.IsAbs(fPath) {
165 return nil, errors.New("freq.path path must be absolute")
169 if f.Chunked != nil {
171 return nil, errors.New("freq.chunked value must be greater than zero")
173 freqChunked = int64(*f.Chunked) * 1024
175 if f.MinSize != nil {
176 freqMinSize = int64(*f.MinSize) * 1024
181 if yml.RxRate != nil && *yml.RxRate > 0 {
182 defRxRate = *yml.RxRate
185 if yml.TxRate != nil && *yml.TxRate > 0 {
186 defTxRate = *yml.TxRate
189 defOnlineDeadline := uint(DefaultDeadline)
190 if yml.OnlineDeadline != nil {
191 if *yml.OnlineDeadline <= 0 {
192 return nil, errors.New("OnlineDeadline must be at least 1 second")
194 defOnlineDeadline = *yml.OnlineDeadline
196 var defMaxOnlineTime uint
197 if yml.MaxOnlineTime != nil {
198 defMaxOnlineTime = *yml.MaxOnlineTime
202 for _, callYml := range yml.Calls {
203 expr, err := cronexpr.Parse(callYml.Cron)
209 if callYml.Nice != nil {
210 nice, err = NicenessParse(*callYml.Nice)
217 if callYml.Xx != nil {
224 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
229 if callYml.RxRate != nil {
230 rxRate = *callYml.RxRate
233 if callYml.TxRate != nil {
234 txRate = *callYml.TxRate
238 if callYml.Addr != nil {
239 if a, exists := yml.Addrs[*callYml.Addr]; exists {
246 onlineDeadline := defOnlineDeadline
247 if callYml.OnlineDeadline != nil {
248 if *callYml.OnlineDeadline == 0 {
249 return nil, errors.New("OnlineDeadline must be at least 1 second")
251 onlineDeadline = *callYml.OnlineDeadline
254 var maxOnlineTime uint
255 if callYml.MaxOnlineTime != nil {
256 maxOnlineTime = *callYml.MaxOnlineTime
259 calls = append(calls, &Call{
266 OnlineDeadline: onlineDeadline,
267 MaxOnlineTime: maxOnlineTime,
274 ExchPub: new([32]byte),
275 SignPub: ed25519.PublicKey(signPub),
279 FreqChunked: freqChunked,
280 FreqMinSize: freqMinSize,
285 OnlineDeadline: defOnlineDeadline,
286 MaxOnlineTime: defMaxOnlineTime,
288 copy(node.ExchPub[:], exchPub)
289 if len(noisePub) > 0 {
290 node.NoisePub = new([32]byte)
291 copy(node.NoisePub[:], noisePub)
296 func NewNodeOur(yml *NodeOurJSON) (*NodeOur, error) {
297 id, err := NodeIdFromString(yml.Id)
302 exchPub, err := FromBase32(yml.ExchPub)
306 if len(exchPub) != 32 {
307 return nil, errors.New("Invalid exchPub size")
310 exchPrv, err := FromBase32(yml.ExchPrv)
314 if len(exchPrv) != 32 {
315 return nil, errors.New("Invalid exchPrv size")
318 signPub, err := FromBase32(yml.SignPub)
322 if len(signPub) != ed25519.PublicKeySize {
323 return nil, errors.New("Invalid signPub size")
326 signPrv, err := FromBase32(yml.SignPrv)
330 if len(signPrv) != ed25519.PrivateKeySize {
331 return nil, errors.New("Invalid signPrv size")
334 noisePub, err := FromBase32(yml.NoisePub)
338 if len(noisePub) != 32 {
339 return nil, errors.New("Invalid noisePub size")
342 noisePrv, err := FromBase32(yml.NoisePrv)
346 if len(noisePrv) != 32 {
347 return nil, errors.New("Invalid noisePrv size")
352 ExchPub: new([32]byte),
353 ExchPrv: new([32]byte),
354 SignPub: ed25519.PublicKey(signPub),
355 SignPrv: ed25519.PrivateKey(signPrv),
356 NoisePub: new([32]byte),
357 NoisePrv: new([32]byte),
359 copy(node.ExchPub[:], exchPub)
360 copy(node.ExchPrv[:], exchPrv)
361 copy(node.NoisePub[:], noisePub)
362 copy(node.NoisePrv[:], noisePrv)
366 func CfgParse(data []byte) (*Ctx, error) {
368 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
369 os.Stderr.WriteString("Passphrase:")
370 password, err := terminal.ReadPassword(0)
374 os.Stderr.WriteString("\n")
375 data, err = DeEBlob(data, password)
380 var cfgGeneral map[string]interface{}
381 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
384 marshaled, err := json.Marshal(cfgGeneral)
389 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
392 if _, exists := cfgJSON.Neigh["self"]; !exists {
393 return nil, errors.New("self neighbour missing")
396 if cfgJSON.Self != nil {
397 self, err = NewNodeOur(cfgJSON.Self)
402 spoolPath := path.Clean(cfgJSON.Spool)
403 if !path.IsAbs(spoolPath) {
404 return nil, errors.New("Spool path must be absolute")
406 logPath := path.Clean(cfgJSON.Log)
407 if !path.IsAbs(logPath) {
408 return nil, errors.New("Log path must be absolute")
411 if cfgJSON.Umask != "" {
412 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
422 UmaskForce: umaskForce,
424 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
425 Alias: make(map[string]*NodeId),
427 if cfgJSON.Notify != nil {
428 if cfgJSON.Notify.File != nil {
429 ctx.NotifyFile = cfgJSON.Notify.File
431 if cfgJSON.Notify.Freq != nil {
432 ctx.NotifyFreq = cfgJSON.Notify.Freq
435 vias := make(map[NodeId][]string)
436 for name, neighJSON := range cfgJSON.Neigh {
437 neigh, err := NewNode(name, neighJSON)
441 ctx.Neigh[*neigh.Id] = neigh
442 if _, already := ctx.Alias[name]; already {
443 return nil, errors.New("Node names conflict")
445 ctx.Alias[name] = neigh.Id
446 vias[*neigh.Id] = neighJSON.Via
448 ctx.SelfId = ctx.Alias["self"]
449 for neighId, viasRaw := range vias {
450 for _, viaRaw := range viasRaw {
451 foundNodeId, err := ctx.FindNode(viaRaw)
455 ctx.Neigh[neighId].Via = append(
456 ctx.Neigh[neighId].Via,