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"
39 DefaultCfgPath string = "/usr/local/etc/nncp.yaml"
40 DefaultSendmailPath string = "/usr/sbin/sendmail"
41 DefaultSpoolPath string = "/var/spool/nncp"
42 DefaultLogPath string = "/var/spool/nncp/log"
45 type NodeYAML struct {
49 NoisePub *string `noisepub,omitempty`
50 Sendmail []string `sendmail,omitempty`
51 Incoming *string `incoming,omitempty`
52 Freq *string `freq,omitempty`
53 FreqChunked *uint64 `freqchunked,omitempty`
54 FreqMinSize *uint64 `freqminsize,omitempty`
55 Via []string `via,omitempty`
56 Calls []CallYAML `calls,omitempty`
58 Addrs map[string]string `addrs,omitempty`
60 OnlineDeadline *uint `onlinedeadline,omitempty`
61 MaxOnlineTime *uint `maxonlinetime,omitempty`
64 type CallYAML struct {
66 Nice *int `nice,omitempty`
67 Xx *string `xx,omitempty`
68 Addr *string `addr,omitempty`
69 OnlineDeadline *uint `onlinedeadline,omitempty`
70 MaxOnlineTime *uint `maxonlinetime,omitempty`
73 type NodeOurYAML struct {
83 type FromToYAML struct {
88 type NotifyYAML struct {
89 File *FromToYAML `file,omitempty`
90 Freq *FromToYAML `freq,omitempty`
94 Self *NodeOurYAML `self,omitempty`
95 Neigh map[string]NodeYAML
99 Notify *NotifyYAML `notify,omitempty`
102 func NewNode(name string, yml NodeYAML) (*Node, error) {
103 nodeId, err := NodeIdFromString(yml.Id)
108 exchPub, err := FromBase32(yml.ExchPub)
112 if len(exchPub) != 32 {
113 return nil, errors.New("Invalid exchPub size")
116 signPub, err := FromBase32(yml.SignPub)
120 if len(signPub) != ed25519.PublicKeySize {
121 return nil, errors.New("Invalid signPub size")
125 if yml.NoisePub != nil {
126 noisePub, err = FromBase32(*yml.NoisePub)
130 if len(noisePub) != 32 {
131 return nil, errors.New("Invalid noisePub size")
136 if yml.Incoming != nil {
137 inc := path.Clean(*yml.Incoming)
138 if !path.IsAbs(inc) {
139 return nil, errors.New("Incoming path must be absolute")
146 fr := path.Clean(*yml.Freq)
148 return nil, errors.New("Freq path must be absolute")
152 var freqChunked int64
153 if yml.FreqChunked != nil {
154 if *yml.FreqChunked == 0 {
155 return nil, errors.New("freqchunked value must be greater than zero")
157 freqChunked = int64(*yml.FreqChunked) * 1024
159 var freqMinSize int64
160 if yml.FreqMinSize != nil {
161 freqMinSize = int64(*yml.FreqMinSize) * 1024
164 defOnlineDeadline := uint(DefaultDeadline)
165 if yml.OnlineDeadline != nil {
166 if *yml.OnlineDeadline <= 0 {
167 return nil, errors.New("OnlineDeadline must be at least 1 second")
169 defOnlineDeadline = *yml.OnlineDeadline
171 var defMaxOnlineTime uint
172 if yml.MaxOnlineTime != nil {
173 defMaxOnlineTime = *yml.MaxOnlineTime
177 for _, callYml := range yml.Calls {
178 expr, err := cronexpr.Parse(callYml.Cron)
183 if callYml.Nice != nil {
184 if *callYml.Nice < 1 || *callYml.Nice > 255 {
185 return nil, errors.New("Nice must be between 1 and 255")
187 nice = uint8(*callYml.Nice)
190 if callYml.Xx != nil {
197 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
201 if callYml.Addr != nil {
202 if a, exists := yml.Addrs[*callYml.Addr]; exists {
208 onlineDeadline := defOnlineDeadline
209 if callYml.OnlineDeadline != nil {
210 if *callYml.OnlineDeadline == 0 {
211 return nil, errors.New("OnlineDeadline must be at least 1 second")
213 onlineDeadline = *callYml.OnlineDeadline
215 var maxOnlineTime uint
216 if callYml.MaxOnlineTime != nil {
217 maxOnlineTime = *callYml.MaxOnlineTime
219 calls = append(calls, &Call{
224 OnlineDeadline: onlineDeadline,
225 MaxOnlineTime: maxOnlineTime,
232 ExchPub: new([32]byte),
233 SignPub: ed25519.PublicKey(signPub),
234 Sendmail: yml.Sendmail,
237 FreqChunked: freqChunked,
238 FreqMinSize: freqMinSize,
241 OnlineDeadline: defOnlineDeadline,
242 MaxOnlineTime: defMaxOnlineTime,
244 copy(node.ExchPub[:], exchPub)
245 if len(noisePub) > 0 {
246 node.NoisePub = new([32]byte)
247 copy(node.NoisePub[:], noisePub)
252 func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) {
253 id, err := NodeIdFromString(yml.Id)
258 exchPub, err := FromBase32(yml.ExchPub)
262 if len(exchPub) != 32 {
263 return nil, errors.New("Invalid exchPub size")
266 exchPrv, err := FromBase32(yml.ExchPrv)
270 if len(exchPrv) != 32 {
271 return nil, errors.New("Invalid exchPrv size")
274 signPub, err := FromBase32(yml.SignPub)
278 if len(signPub) != ed25519.PublicKeySize {
279 return nil, errors.New("Invalid signPub size")
282 signPrv, err := FromBase32(yml.SignPrv)
286 if len(signPrv) != ed25519.PrivateKeySize {
287 return nil, errors.New("Invalid signPrv size")
290 noisePub, err := FromBase32(yml.NoisePub)
294 if len(noisePub) != 32 {
295 return nil, errors.New("Invalid noisePub size")
298 noisePrv, err := FromBase32(yml.NoisePrv)
302 if len(noisePrv) != 32 {
303 return nil, errors.New("Invalid noisePrv size")
308 ExchPub: new([32]byte),
309 ExchPrv: new([32]byte),
310 SignPub: ed25519.PublicKey(signPub),
311 SignPrv: ed25519.PrivateKey(signPrv),
312 NoisePub: new([32]byte),
313 NoisePrv: new([32]byte),
315 copy(node.ExchPub[:], exchPub)
316 copy(node.ExchPrv[:], exchPrv)
317 copy(node.NoisePub[:], noisePub)
318 copy(node.NoisePrv[:], noisePrv)
322 func (nodeOur *NodeOur) ToYAML() string {
324 Id: nodeOur.Id.String(),
325 ExchPub: ToBase32(nodeOur.ExchPub[:]),
326 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
327 SignPub: ToBase32(nodeOur.SignPub[:]),
328 SignPrv: ToBase32(nodeOur.SignPrv[:]),
329 NoisePub: ToBase32(nodeOur.NoisePub[:]),
330 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
332 raw, err := yaml.Marshal(&yml)
339 func CfgParse(data []byte) (*Ctx, error) {
341 if bytes.Compare(data[:8], MagicNNCPBv1[:]) == 0 {
342 os.Stderr.WriteString("Passphrase:")
343 password, err := terminal.ReadPassword(0)
347 os.Stderr.WriteString("\n")
348 data, err = DeEBlob(data, password)
354 if err = yaml.Unmarshal(data, &cfgYAML); err != nil {
357 if _, exists := cfgYAML.Neigh["self"]; !exists {
358 return nil, errors.New("self neighbour missing")
361 if cfgYAML.Self != nil {
362 self, err = NewNodeOur(cfgYAML.Self)
367 spoolPath := path.Clean(cfgYAML.Spool)
368 if !path.IsAbs(spoolPath) {
369 return nil, errors.New("Spool path must be absolute")
371 logPath := path.Clean(cfgYAML.Log)
372 if !path.IsAbs(logPath) {
373 return nil, errors.New("Log path must be absolute")
379 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
380 Alias: make(map[string]*NodeId),
382 if cfgYAML.Notify != nil {
383 if cfgYAML.Notify.File != nil {
384 ctx.NotifyFile = cfgYAML.Notify.File
386 if cfgYAML.Notify.Freq != nil {
387 ctx.NotifyFreq = cfgYAML.Notify.Freq
390 vias := make(map[NodeId][]string)
391 for name, neighYAML := range cfgYAML.Neigh {
392 neigh, err := NewNode(name, neighYAML)
396 ctx.Neigh[*neigh.Id] = neigh
397 if _, already := ctx.Alias[name]; already {
398 return nil, errors.New("Node names conflict")
400 ctx.Alias[name] = neigh.Id
401 vias[*neigh.Id] = neighYAML.Via
403 ctx.SelfId = ctx.Alias["self"]
404 for neighId, viasRaw := range vias {
405 for _, viaRaw := range viasRaw {
406 foundNodeId, err := ctx.FindNode(viaRaw)
410 ctx.Neigh[neighId].Via = append(
411 ctx.Neigh[neighId].Via,
419 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
420 p = os.Getenv(CfgPathEnv)