2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 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 `noisepub,omitempty`
52 Sendmail []string `sendmail,omitempty`
53 Incoming *string `incoming,omitempty`
54 Freq *string `freq,omitempty`
55 FreqChunked *uint64 `freqchunked,omitempty`
56 FreqMinSize *uint64 `freqminsize,omitempty`
57 Via []string `via,omitempty`
58 Calls []CallYAML `calls,omitempty`
60 Addrs map[string]string `addrs,omitempty`
62 OnlineDeadline *uint `onlinedeadline,omitempty`
63 MaxOnlineTime *uint `maxonlinetime,omitempty`
66 type CallYAML struct {
68 Nice *int `nice,omitempty`
69 Xx string `xx,omitempty`
70 Addr *string `addr,omitempty`
71 OnlineDeadline *uint `onlinedeadline,omitempty`
72 MaxOnlineTime *uint `maxonlinetime,omitempty`
75 type NodeOurYAML struct {
85 type FromToYAML struct {
90 type NotifyYAML struct {
91 File *FromToYAML `file,omitempty`
92 Freq *FromToYAML `freq,omitempty`
96 Self *NodeOurYAML `self,omitempty`
97 Neigh map[string]NodeYAML
101 Notify *NotifyYAML `notify,omitempty`
104 func NewNode(name string, yml NodeYAML) (*Node, error) {
105 nodeId, err := NodeIdFromString(yml.Id)
110 exchPub, err := FromBase32(yml.ExchPub)
114 if len(exchPub) != 32 {
115 return nil, errors.New("Invalid exchPub size")
118 signPub, err := FromBase32(yml.SignPub)
122 if len(signPub) != ed25519.PublicKeySize {
123 return nil, errors.New("Invalid signPub size")
127 if yml.NoisePub != nil {
128 noisePub, err = FromBase32(*yml.NoisePub)
132 if len(noisePub) != 32 {
133 return nil, errors.New("Invalid noisePub size")
138 if yml.Incoming != nil {
139 inc := path.Clean(*yml.Incoming)
140 if !path.IsAbs(inc) {
141 return nil, errors.New("Incoming path must be absolute")
148 fr := path.Clean(*yml.Freq)
150 return nil, errors.New("Freq path must be absolute")
154 var freqChunked int64
155 if yml.FreqChunked != nil {
156 if *yml.FreqChunked == 0 {
157 return nil, errors.New("freqchunked value must be greater than zero")
159 freqChunked = int64(*yml.FreqChunked) * 1024
161 var freqMinSize int64
162 if yml.FreqMinSize != nil {
163 freqMinSize = int64(*yml.FreqMinSize) * 1024
166 defOnlineDeadline := uint(DefaultDeadline)
167 if yml.OnlineDeadline != nil {
168 if *yml.OnlineDeadline <= 0 {
169 return nil, errors.New("OnlineDeadline must be at least 1 second")
171 defOnlineDeadline = *yml.OnlineDeadline
173 var defMaxOnlineTime uint
174 if yml.MaxOnlineTime != nil {
175 defMaxOnlineTime = *yml.MaxOnlineTime
179 for _, callYml := range yml.Calls {
180 expr, err := cronexpr.Parse(callYml.Cron)
185 if callYml.Nice != nil {
186 if *callYml.Nice < 1 || *callYml.Nice > 255 {
187 return nil, errors.New("Nice must be between 1 and 255")
189 nice = uint8(*callYml.Nice)
199 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
202 if callYml.Addr != nil {
203 if a, exists := yml.Addrs[*callYml.Addr]; exists {
209 onlineDeadline := defOnlineDeadline
210 if callYml.OnlineDeadline != nil {
211 if *callYml.OnlineDeadline == 0 {
212 return nil, errors.New("OnlineDeadline must be at least 1 second")
214 onlineDeadline = *callYml.OnlineDeadline
216 var maxOnlineTime uint
217 if callYml.MaxOnlineTime != nil {
218 maxOnlineTime = *callYml.MaxOnlineTime
220 calls = append(calls, &Call{
225 OnlineDeadline: onlineDeadline,
226 MaxOnlineTime: maxOnlineTime,
233 ExchPub: new([32]byte),
234 SignPub: ed25519.PublicKey(signPub),
235 Sendmail: yml.Sendmail,
238 FreqChunked: freqChunked,
239 FreqMinSize: freqMinSize,
242 OnlineDeadline: defOnlineDeadline,
243 MaxOnlineTime: defMaxOnlineTime,
245 copy(node.ExchPub[:], exchPub)
246 if len(noisePub) > 0 {
247 node.NoisePub = new([32]byte)
248 copy(node.NoisePub[:], noisePub)
253 func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) {
254 id, err := NodeIdFromString(yml.Id)
259 exchPub, err := FromBase32(yml.ExchPub)
263 if len(exchPub) != 32 {
264 return nil, errors.New("Invalid exchPub size")
267 exchPrv, err := FromBase32(yml.ExchPrv)
271 if len(exchPrv) != 32 {
272 return nil, errors.New("Invalid exchPrv size")
275 signPub, err := FromBase32(yml.SignPub)
279 if len(signPub) != ed25519.PublicKeySize {
280 return nil, errors.New("Invalid signPub size")
283 signPrv, err := FromBase32(yml.SignPrv)
287 if len(signPrv) != ed25519.PrivateKeySize {
288 return nil, errors.New("Invalid signPrv size")
291 noisePub, err := FromBase32(yml.NoisePub)
295 if len(noisePub) != 32 {
296 return nil, errors.New("Invalid noisePub size")
299 noisePrv, err := FromBase32(yml.NoisePrv)
303 if len(noisePrv) != 32 {
304 return nil, errors.New("Invalid noisePrv size")
309 ExchPub: new([32]byte),
310 ExchPrv: new([32]byte),
311 SignPub: ed25519.PublicKey(signPub),
312 SignPrv: ed25519.PrivateKey(signPrv),
313 NoisePub: new([32]byte),
314 NoisePrv: new([32]byte),
316 copy(node.ExchPub[:], exchPub)
317 copy(node.ExchPrv[:], exchPrv)
318 copy(node.NoisePub[:], noisePub)
319 copy(node.NoisePrv[:], noisePrv)
323 func (nodeOur *NodeOur) ToYAML() string {
325 Id: nodeOur.Id.String(),
326 ExchPub: ToBase32(nodeOur.ExchPub[:]),
327 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
328 SignPub: ToBase32(nodeOur.SignPub[:]),
329 SignPrv: ToBase32(nodeOur.SignPrv[:]),
330 NoisePub: ToBase32(nodeOur.NoisePub[:]),
331 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
333 raw, err := yaml.Marshal(&yml)
340 func CfgParse(data []byte) (*Ctx, error) {
342 if bytes.Compare(data[:8], MagicNNCPBv2[:]) == 0 {
343 os.Stderr.WriteString("Passphrase:")
344 password, err := terminal.ReadPassword(0)
348 os.Stderr.WriteString("\n")
349 data, err = DeEBlob(data, password)
355 if err = yaml.Unmarshal(data, &cfgYAML); err != nil {
358 if _, exists := cfgYAML.Neigh["self"]; !exists {
359 return nil, errors.New("self neighbour missing")
362 if cfgYAML.Self != nil {
363 self, err = NewNodeOur(cfgYAML.Self)
368 spoolPath := path.Clean(cfgYAML.Spool)
369 if !path.IsAbs(spoolPath) {
370 return nil, errors.New("Spool path must be absolute")
372 logPath := path.Clean(cfgYAML.Log)
373 if !path.IsAbs(logPath) {
374 return nil, errors.New("Log path must be absolute")
380 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
381 Alias: make(map[string]*NodeId),
383 if cfgYAML.Notify != nil {
384 if cfgYAML.Notify.File != nil {
385 ctx.NotifyFile = cfgYAML.Notify.File
387 if cfgYAML.Notify.Freq != nil {
388 ctx.NotifyFreq = cfgYAML.Notify.Freq
391 vias := make(map[NodeId][]string)
392 for name, neighYAML := range cfgYAML.Neigh {
393 neigh, err := NewNode(name, neighYAML)
397 ctx.Neigh[*neigh.Id] = neigh
398 if _, already := ctx.Alias[name]; already {
399 return nil, errors.New("Node names conflict")
401 ctx.Alias[name] = neigh.Id
402 vias[*neigh.Id] = neighYAML.Via
404 ctx.SelfId = ctx.Alias["self"]
405 for neighId, viasRaw := range vias {
406 for _, viaRaw := range viasRaw {
407 foundNodeId, err := ctx.FindNode(viaRaw)
411 ctx.Neigh[neighId].Via = append(
412 ctx.Neigh[neighId].Via,