2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2018 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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 "github.com/gorhill/cronexpr"
29 "golang.org/x/crypto/ed25519"
30 "golang.org/x/crypto/ssh/terminal"
35 CfgPathEnv = "NNCPCFG"
36 CfgSpoolEnv = "NNCPSPOOL"
41 DefaultCfgPath string = "/usr/local/etc/nncp.yaml"
42 DefaultSendmailPath string = "/usr/sbin/sendmail"
43 DefaultSpoolPath string = "/var/spool/nncp"
44 DefaultLogPath string = "/var/spool/nncp/log"
47 type NodeYAML struct {
51 NoisePub *string `yaml:"noisepub,omitempty"`
52 Exec map[string][]string `yaml:"exec,omitempty"`
53 Incoming *string `yaml:"incoming,omitempty"`
54 Freq *string `yaml:"freq,omitempty"`
55 FreqChunked *uint64 `yaml:"freqchunked,omitempty"`
56 FreqMinSize *uint64 `yaml:"freqminsize,omitempty"`
57 Via []string `yaml:"via,omitempty"`
58 Calls []CallYAML `yaml:"calls,omitempty"`
60 Addrs map[string]string `yaml:"addrs,omitempty"`
62 RxRate *int `yaml:"rxrate,omitempty"`
63 TxRate *int `yaml:"txrate,omitempty"`
64 OnlineDeadline *uint `yaml:"onlinedeadline,omitempty"`
65 MaxOnlineTime *uint `yaml:"maxonlinetime,omitempty"`
68 type CallYAML struct {
70 Nice *string `yaml:"nice,omitempty"`
71 Xx string `yaml:"xx,omitempty"`
72 RxRate *int `yaml:"rxrate,omitempty"`
73 TxRate *int `yaml:"txrate,omitempty"`
74 Addr *string `yaml:"addr,omitempty"`
75 OnlineDeadline *uint `yaml:"onlinedeadline,omitempty"`
76 MaxOnlineTime *uint `yaml:"maxonlinetime,omitempty"`
79 type NodeOurYAML struct {
89 type FromToYAML struct {
94 type NotifyYAML struct {
95 File *FromToYAML `yaml:"file,omitempty"`
96 Freq *FromToYAML `yaml:"freq,omitempty"`
100 Self *NodeOurYAML `yaml:"self,omitempty"`
101 Neigh map[string]NodeYAML
105 Notify *NotifyYAML `yaml:"notify,omitempty"`
108 func NewNode(name string, yml NodeYAML) (*Node, error) {
109 nodeId, err := NodeIdFromString(yml.Id)
114 exchPub, err := FromBase32(yml.ExchPub)
118 if len(exchPub) != 32 {
119 return nil, errors.New("Invalid exchPub size")
122 signPub, err := FromBase32(yml.SignPub)
126 if len(signPub) != ed25519.PublicKeySize {
127 return nil, errors.New("Invalid signPub size")
131 if yml.NoisePub != nil {
132 noisePub, err = FromBase32(*yml.NoisePub)
136 if len(noisePub) != 32 {
137 return nil, errors.New("Invalid noisePub size")
142 if yml.Incoming != nil {
143 inc := path.Clean(*yml.Incoming)
144 if !path.IsAbs(inc) {
145 return nil, errors.New("Incoming path must be absolute")
152 fr := path.Clean(*yml.Freq)
154 return nil, errors.New("Freq path must be absolute")
158 var freqChunked int64
159 if yml.FreqChunked != nil {
160 if *yml.FreqChunked == 0 {
161 return nil, errors.New("freqchunked value must be greater than zero")
163 freqChunked = int64(*yml.FreqChunked) * 1024
165 var freqMinSize int64
166 if yml.FreqMinSize != nil {
167 freqMinSize = int64(*yml.FreqMinSize) * 1024
171 if yml.RxRate != nil && *yml.RxRate > 0 {
172 defRxRate = *yml.RxRate
175 if yml.TxRate != nil && *yml.TxRate > 0 {
176 defTxRate = *yml.TxRate
179 defOnlineDeadline := uint(DefaultDeadline)
180 if yml.OnlineDeadline != nil {
181 if *yml.OnlineDeadline <= 0 {
182 return nil, errors.New("OnlineDeadline must be at least 1 second")
184 defOnlineDeadline = *yml.OnlineDeadline
186 var defMaxOnlineTime uint
187 if yml.MaxOnlineTime != nil {
188 defMaxOnlineTime = *yml.MaxOnlineTime
192 for _, callYml := range yml.Calls {
193 expr, err := cronexpr.Parse(callYml.Cron)
199 if callYml.Nice != nil {
200 nice, err = NicenessParse(*callYml.Nice)
214 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
218 if callYml.RxRate != nil && *callYml.RxRate > 0 {
219 rxRate = *callYml.RxRate
222 if callYml.TxRate != nil && *callYml.TxRate > 0 {
223 txRate = *callYml.TxRate
227 if callYml.Addr != nil {
228 if a, exists := yml.Addrs[*callYml.Addr]; exists {
235 onlineDeadline := defOnlineDeadline
236 if callYml.OnlineDeadline != nil {
237 if *callYml.OnlineDeadline == 0 {
238 return nil, errors.New("OnlineDeadline must be at least 1 second")
240 onlineDeadline = *callYml.OnlineDeadline
243 var maxOnlineTime uint
244 if callYml.MaxOnlineTime != nil {
245 maxOnlineTime = *callYml.MaxOnlineTime
248 calls = append(calls, &Call{
255 OnlineDeadline: onlineDeadline,
256 MaxOnlineTime: maxOnlineTime,
263 ExchPub: new([32]byte),
264 SignPub: ed25519.PublicKey(signPub),
268 FreqChunked: freqChunked,
269 FreqMinSize: freqMinSize,
274 OnlineDeadline: defOnlineDeadline,
275 MaxOnlineTime: defMaxOnlineTime,
277 copy(node.ExchPub[:], exchPub)
278 if len(noisePub) > 0 {
279 node.NoisePub = new([32]byte)
280 copy(node.NoisePub[:], noisePub)
285 func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) {
286 id, err := NodeIdFromString(yml.Id)
291 exchPub, err := FromBase32(yml.ExchPub)
295 if len(exchPub) != 32 {
296 return nil, errors.New("Invalid exchPub size")
299 exchPrv, err := FromBase32(yml.ExchPrv)
303 if len(exchPrv) != 32 {
304 return nil, errors.New("Invalid exchPrv size")
307 signPub, err := FromBase32(yml.SignPub)
311 if len(signPub) != ed25519.PublicKeySize {
312 return nil, errors.New("Invalid signPub size")
315 signPrv, err := FromBase32(yml.SignPrv)
319 if len(signPrv) != ed25519.PrivateKeySize {
320 return nil, errors.New("Invalid signPrv size")
323 noisePub, err := FromBase32(yml.NoisePub)
327 if len(noisePub) != 32 {
328 return nil, errors.New("Invalid noisePub size")
331 noisePrv, err := FromBase32(yml.NoisePrv)
335 if len(noisePrv) != 32 {
336 return nil, errors.New("Invalid noisePrv size")
341 ExchPub: new([32]byte),
342 ExchPrv: new([32]byte),
343 SignPub: ed25519.PublicKey(signPub),
344 SignPrv: ed25519.PrivateKey(signPrv),
345 NoisePub: new([32]byte),
346 NoisePrv: new([32]byte),
348 copy(node.ExchPub[:], exchPub)
349 copy(node.ExchPrv[:], exchPrv)
350 copy(node.NoisePub[:], noisePub)
351 copy(node.NoisePrv[:], noisePrv)
355 func (nodeOur *NodeOur) ToYAML() string {
357 Id: nodeOur.Id.String(),
358 ExchPub: ToBase32(nodeOur.ExchPub[:]),
359 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
360 SignPub: ToBase32(nodeOur.SignPub[:]),
361 SignPrv: ToBase32(nodeOur.SignPrv[:]),
362 NoisePub: ToBase32(nodeOur.NoisePub[:]),
363 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
365 raw, err := yaml.Marshal(&yml)
372 func CfgParse(data []byte) (*Ctx, error) {
374 if bytes.Compare(data[:8], MagicNNCPBv2[:]) == 0 {
375 os.Stderr.WriteString("Passphrase:")
376 password, err := terminal.ReadPassword(0)
380 os.Stderr.WriteString("\n")
381 data, err = DeEBlob(data, password)
387 if err = yaml.Unmarshal(data, &cfgYAML); err != nil {
390 if _, exists := cfgYAML.Neigh["self"]; !exists {
391 return nil, errors.New("self neighbour missing")
394 if cfgYAML.Self != nil {
395 self, err = NewNodeOur(cfgYAML.Self)
400 spoolPath := path.Clean(cfgYAML.Spool)
401 if !path.IsAbs(spoolPath) {
402 return nil, errors.New("Spool path must be absolute")
404 logPath := path.Clean(cfgYAML.Log)
405 if !path.IsAbs(logPath) {
406 return nil, errors.New("Log path must be absolute")
412 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
413 Alias: make(map[string]*NodeId),
415 if cfgYAML.Notify != nil {
416 if cfgYAML.Notify.File != nil {
417 ctx.NotifyFile = cfgYAML.Notify.File
419 if cfgYAML.Notify.Freq != nil {
420 ctx.NotifyFreq = cfgYAML.Notify.Freq
423 vias := make(map[NodeId][]string)
424 for name, neighYAML := range cfgYAML.Neigh {
425 neigh, err := NewNode(name, neighYAML)
429 ctx.Neigh[*neigh.Id] = neigh
430 if _, already := ctx.Alias[name]; already {
431 return nil, errors.New("Node names conflict")
433 ctx.Alias[name] = neigh.Id
434 vias[*neigh.Id] = neighYAML.Via
436 ctx.SelfId = ctx.Alias["self"]
437 for neighId, viasRaw := range vias {
438 for _, viaRaw := range viasRaw {
439 foundNodeId, err := ctx.FindNode(viaRaw)
443 ctx.Neigh[neighId].Via = append(
444 ctx.Neigh[neighId].Via,