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)
197 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
200 if callYml.Addr != nil {
201 if a, exists := yml.Addrs[*callYml.Addr]; exists {
207 onlineDeadline := defOnlineDeadline
208 if callYml.OnlineDeadline != nil {
209 if *callYml.OnlineDeadline == 0 {
210 return nil, errors.New("OnlineDeadline must be at least 1 second")
212 onlineDeadline = *callYml.OnlineDeadline
214 var maxOnlineTime uint
215 if callYml.MaxOnlineTime != nil {
216 maxOnlineTime = *callYml.MaxOnlineTime
218 calls = append(calls, &Call{
223 OnlineDeadline: onlineDeadline,
224 MaxOnlineTime: maxOnlineTime,
231 ExchPub: new([32]byte),
232 SignPub: ed25519.PublicKey(signPub),
233 Sendmail: yml.Sendmail,
236 FreqChunked: freqChunked,
237 FreqMinSize: freqMinSize,
240 OnlineDeadline: defOnlineDeadline,
241 MaxOnlineTime: defMaxOnlineTime,
243 copy(node.ExchPub[:], exchPub)
244 if len(noisePub) > 0 {
245 node.NoisePub = new([32]byte)
246 copy(node.NoisePub[:], noisePub)
251 func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) {
252 id, err := NodeIdFromString(yml.Id)
257 exchPub, err := FromBase32(yml.ExchPub)
261 if len(exchPub) != 32 {
262 return nil, errors.New("Invalid exchPub size")
265 exchPrv, err := FromBase32(yml.ExchPrv)
269 if len(exchPrv) != 32 {
270 return nil, errors.New("Invalid exchPrv size")
273 signPub, err := FromBase32(yml.SignPub)
277 if len(signPub) != ed25519.PublicKeySize {
278 return nil, errors.New("Invalid signPub size")
281 signPrv, err := FromBase32(yml.SignPrv)
285 if len(signPrv) != ed25519.PrivateKeySize {
286 return nil, errors.New("Invalid signPrv size")
289 noisePub, err := FromBase32(yml.NoisePub)
293 if len(noisePub) != 32 {
294 return nil, errors.New("Invalid noisePub size")
297 noisePrv, err := FromBase32(yml.NoisePrv)
301 if len(noisePrv) != 32 {
302 return nil, errors.New("Invalid noisePrv size")
307 ExchPub: new([32]byte),
308 ExchPrv: new([32]byte),
309 SignPub: ed25519.PublicKey(signPub),
310 SignPrv: ed25519.PrivateKey(signPrv),
311 NoisePub: new([32]byte),
312 NoisePrv: new([32]byte),
314 copy(node.ExchPub[:], exchPub)
315 copy(node.ExchPrv[:], exchPrv)
316 copy(node.NoisePub[:], noisePub)
317 copy(node.NoisePrv[:], noisePrv)
321 func (nodeOur *NodeOur) ToYAML() string {
323 Id: nodeOur.Id.String(),
324 ExchPub: ToBase32(nodeOur.ExchPub[:]),
325 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
326 SignPub: ToBase32(nodeOur.SignPub[:]),
327 SignPrv: ToBase32(nodeOur.SignPrv[:]),
328 NoisePub: ToBase32(nodeOur.NoisePub[:]),
329 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
331 raw, err := yaml.Marshal(&yml)
338 func CfgParse(data []byte) (*Ctx, error) {
340 if bytes.Compare(data[:8], MagicNNCPBv1[:]) == 0 {
341 os.Stderr.WriteString("Passphrase:")
342 password, err := terminal.ReadPassword(0)
346 os.Stderr.WriteString("\n")
347 data, err = DeEBlob(data, password)
353 if err = yaml.Unmarshal(data, &cfgYAML); err != nil {
356 if _, exists := cfgYAML.Neigh["self"]; !exists {
357 return nil, errors.New("self neighbour missing")
360 if cfgYAML.Self != nil {
361 self, err = NewNodeOur(cfgYAML.Self)
366 spoolPath := path.Clean(cfgYAML.Spool)
367 if !path.IsAbs(spoolPath) {
368 return nil, errors.New("Spool path must be absolute")
370 logPath := path.Clean(cfgYAML.Log)
371 if !path.IsAbs(logPath) {
372 return nil, errors.New("Log path must be absolute")
378 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
379 Alias: make(map[string]*NodeId),
381 if cfgYAML.Notify != nil {
382 if cfgYAML.Notify.File != nil {
383 ctx.NotifyFile = cfgYAML.Notify.File
385 if cfgYAML.Notify.Freq != nil {
386 ctx.NotifyFreq = cfgYAML.Notify.Freq
389 vias := make(map[NodeId][]string)
390 for name, neighYAML := range cfgYAML.Neigh {
391 neigh, err := NewNode(name, neighYAML)
395 ctx.Neigh[*neigh.Id] = neigh
396 if _, already := ctx.Alias[name]; already {
397 return nil, errors.New("Node names conflict")
399 ctx.Alias[name] = neigh.Id
400 vias[*neigh.Id] = neighYAML.Via
402 ctx.SelfId = ctx.Alias["self"]
403 for neighId, viasRaw := range vias {
404 for _, viaRaw := range viasRaw {
405 foundNodeId, err := ctx.FindNode(viaRaw)
409 ctx.Neigh[neighId].Via = append(
410 ctx.Neigh[neighId].Via,
418 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
419 p = os.Getenv(CfgPathEnv)