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/>.
27 "github.com/gorhill/cronexpr"
28 "golang.org/x/crypto/ed25519"
29 "golang.org/x/crypto/ssh/terminal"
34 CfgPathEnv = "NNCPCFG"
35 CfgSpoolEnv = "NNCPSPOOL"
40 DefaultCfgPath string = "/usr/local/etc/nncp.yaml"
41 DefaultSendmailPath string = "/usr/sbin/sendmail"
42 DefaultSpoolPath string = "/var/spool/nncp"
43 DefaultLogPath string = "/var/spool/nncp/log"
46 type NodeYAML struct {
50 NoisePub *string `yaml:"noisepub,omitempty"`
51 Exec map[string][]string `yaml:"exec,omitempty"`
52 Incoming *string `yaml:"incoming,omitempty"`
53 Freq *string `yaml:"freq,omitempty"`
54 FreqChunked *uint64 `yaml:"freqchunked,omitempty"`
55 FreqMinSize *uint64 `yaml:"freqminsize,omitempty"`
56 Via []string `yaml:"via,omitempty"`
57 Calls []CallYAML `yaml:"calls,omitempty"`
59 Addrs map[string]string `yaml:"addrs,omitempty"`
61 RxRate *int `yaml:"rxrate,omitempty"`
62 TxRate *int `yaml:"txrate,omitempty"`
63 OnlineDeadline *uint `yaml:"onlinedeadline,omitempty"`
64 MaxOnlineTime *uint `yaml:"maxonlinetime,omitempty"`
67 type CallYAML struct {
69 Nice *string `yaml:"nice,omitempty"`
70 Xx string `yaml:"xx,omitempty"`
71 RxRate *int `yaml:"rxrate,omitempty"`
72 TxRate *int `yaml:"txrate,omitempty"`
73 Addr *string `yaml:"addr,omitempty"`
74 OnlineDeadline *uint `yaml:"onlinedeadline,omitempty"`
75 MaxOnlineTime *uint `yaml:"maxonlinetime,omitempty"`
78 type NodeOurYAML struct {
88 type FromToYAML struct {
93 type NotifyYAML struct {
94 File *FromToYAML `yaml:"file,omitempty"`
95 Freq *FromToYAML `yaml:"freq,omitempty"`
99 Self *NodeOurYAML `yaml:"self,omitempty"`
100 Neigh map[string]NodeYAML
104 Notify *NotifyYAML `yaml:"notify,omitempty"`
107 func NewNode(name string, yml NodeYAML) (*Node, error) {
108 nodeId, err := NodeIdFromString(yml.Id)
113 exchPub, err := FromBase32(yml.ExchPub)
117 if len(exchPub) != 32 {
118 return nil, errors.New("Invalid exchPub size")
121 signPub, err := FromBase32(yml.SignPub)
125 if len(signPub) != ed25519.PublicKeySize {
126 return nil, errors.New("Invalid signPub size")
130 if yml.NoisePub != nil {
131 noisePub, err = FromBase32(*yml.NoisePub)
135 if len(noisePub) != 32 {
136 return nil, errors.New("Invalid noisePub size")
141 if yml.Incoming != nil {
142 inc := path.Clean(*yml.Incoming)
143 if !path.IsAbs(inc) {
144 return nil, errors.New("Incoming path must be absolute")
151 fr := path.Clean(*yml.Freq)
153 return nil, errors.New("Freq path must be absolute")
157 var freqChunked int64
158 if yml.FreqChunked != nil {
159 if *yml.FreqChunked == 0 {
160 return nil, errors.New("freqchunked value must be greater than zero")
162 freqChunked = int64(*yml.FreqChunked) * 1024
164 var freqMinSize int64
165 if yml.FreqMinSize != nil {
166 freqMinSize = int64(*yml.FreqMinSize) * 1024
170 if yml.RxRate != nil && *yml.RxRate > 0 {
171 defRxRate = *yml.RxRate
174 if yml.TxRate != nil && *yml.TxRate > 0 {
175 defTxRate = *yml.TxRate
178 defOnlineDeadline := uint(DefaultDeadline)
179 if yml.OnlineDeadline != nil {
180 if *yml.OnlineDeadline <= 0 {
181 return nil, errors.New("OnlineDeadline must be at least 1 second")
183 defOnlineDeadline = *yml.OnlineDeadline
185 var defMaxOnlineTime uint
186 if yml.MaxOnlineTime != nil {
187 defMaxOnlineTime = *yml.MaxOnlineTime
191 for _, callYml := range yml.Calls {
192 expr, err := cronexpr.Parse(callYml.Cron)
198 if callYml.Nice != nil {
199 nice, err = NicenessParse(*callYml.Nice)
213 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
217 if callYml.RxRate != nil {
218 rxRate = *callYml.RxRate
221 if callYml.TxRate != nil {
222 txRate = *callYml.TxRate
226 if callYml.Addr != nil {
227 if a, exists := yml.Addrs[*callYml.Addr]; exists {
234 onlineDeadline := defOnlineDeadline
235 if callYml.OnlineDeadline != nil {
236 if *callYml.OnlineDeadline == 0 {
237 return nil, errors.New("OnlineDeadline must be at least 1 second")
239 onlineDeadline = *callYml.OnlineDeadline
242 var maxOnlineTime uint
243 if callYml.MaxOnlineTime != nil {
244 maxOnlineTime = *callYml.MaxOnlineTime
247 calls = append(calls, &Call{
254 OnlineDeadline: onlineDeadline,
255 MaxOnlineTime: maxOnlineTime,
262 ExchPub: new([32]byte),
263 SignPub: ed25519.PublicKey(signPub),
267 FreqChunked: freqChunked,
268 FreqMinSize: freqMinSize,
273 OnlineDeadline: defOnlineDeadline,
274 MaxOnlineTime: defMaxOnlineTime,
276 copy(node.ExchPub[:], exchPub)
277 if len(noisePub) > 0 {
278 node.NoisePub = new([32]byte)
279 copy(node.NoisePub[:], noisePub)
284 func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) {
285 id, err := NodeIdFromString(yml.Id)
290 exchPub, err := FromBase32(yml.ExchPub)
294 if len(exchPub) != 32 {
295 return nil, errors.New("Invalid exchPub size")
298 exchPrv, err := FromBase32(yml.ExchPrv)
302 if len(exchPrv) != 32 {
303 return nil, errors.New("Invalid exchPrv size")
306 signPub, err := FromBase32(yml.SignPub)
310 if len(signPub) != ed25519.PublicKeySize {
311 return nil, errors.New("Invalid signPub size")
314 signPrv, err := FromBase32(yml.SignPrv)
318 if len(signPrv) != ed25519.PrivateKeySize {
319 return nil, errors.New("Invalid signPrv size")
322 noisePub, err := FromBase32(yml.NoisePub)
326 if len(noisePub) != 32 {
327 return nil, errors.New("Invalid noisePub size")
330 noisePrv, err := FromBase32(yml.NoisePrv)
334 if len(noisePrv) != 32 {
335 return nil, errors.New("Invalid noisePrv size")
340 ExchPub: new([32]byte),
341 ExchPrv: new([32]byte),
342 SignPub: ed25519.PublicKey(signPub),
343 SignPrv: ed25519.PrivateKey(signPrv),
344 NoisePub: new([32]byte),
345 NoisePrv: new([32]byte),
347 copy(node.ExchPub[:], exchPub)
348 copy(node.ExchPrv[:], exchPrv)
349 copy(node.NoisePub[:], noisePub)
350 copy(node.NoisePrv[:], noisePrv)
354 func (nodeOur *NodeOur) ToYAML() string {
356 Id: nodeOur.Id.String(),
357 ExchPub: ToBase32(nodeOur.ExchPub[:]),
358 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
359 SignPub: ToBase32(nodeOur.SignPub[:]),
360 SignPrv: ToBase32(nodeOur.SignPrv[:]),
361 NoisePub: ToBase32(nodeOur.NoisePub[:]),
362 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
364 raw, err := yaml.Marshal(&yml)
371 func CfgParse(data []byte) (*Ctx, error) {
373 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
374 os.Stderr.WriteString("Passphrase:")
375 password, err := terminal.ReadPassword(0)
379 os.Stderr.WriteString("\n")
380 data, err = DeEBlob(data, password)
386 if err = yaml.Unmarshal(data, &cfgYAML); err != nil {
389 if _, exists := cfgYAML.Neigh["self"]; !exists {
390 return nil, errors.New("self neighbour missing")
393 if cfgYAML.Self != nil {
394 self, err = NewNodeOur(cfgYAML.Self)
399 spoolPath := path.Clean(cfgYAML.Spool)
400 if !path.IsAbs(spoolPath) {
401 return nil, errors.New("Spool path must be absolute")
403 logPath := path.Clean(cfgYAML.Log)
404 if !path.IsAbs(logPath) {
405 return nil, errors.New("Log path must be absolute")
411 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
412 Alias: make(map[string]*NodeId),
414 if cfgYAML.Notify != nil {
415 if cfgYAML.Notify.File != nil {
416 ctx.NotifyFile = cfgYAML.Notify.File
418 if cfgYAML.Notify.Freq != nil {
419 ctx.NotifyFreq = cfgYAML.Notify.Freq
422 vias := make(map[NodeId][]string)
423 for name, neighYAML := range cfgYAML.Neigh {
424 neigh, err := NewNode(name, neighYAML)
428 ctx.Neigh[*neigh.Id] = neigh
429 if _, already := ctx.Alias[name]; already {
430 return nil, errors.New("Node names conflict")
432 ctx.Alias[name] = neigh.Id
433 vias[*neigh.Id] = neighYAML.Via
435 ctx.SelfId = ctx.Alias["self"]
436 for neighId, viasRaw := range vias {
437 for _, viaRaw := range viasRaw {
438 foundNodeId, err := ctx.FindNode(viaRaw)
442 ctx.Neigh[neighId].Via = append(
443 ctx.Neigh[neighId].Via,