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/>.
29 "github.com/gorhill/cronexpr"
30 "github.com/hjson/hjson-go"
31 "golang.org/x/crypto/ed25519"
32 "golang.org/x/crypto/ssh/terminal"
36 CfgPathEnv = "NNCPCFG"
37 CfgSpoolEnv = "NNCPSPOOL"
42 DefaultCfgPath string = "/usr/local/etc/nncp.hjson"
43 DefaultSendmailPath string = "/usr/sbin/sendmail"
44 DefaultSpoolPath string = "/var/spool/nncp"
45 DefaultLogPath string = "/var/spool/nncp/log"
48 type NodeJSON struct {
50 ExchPub string `json:"exchpub"`
51 SignPub string `json:"signpub"`
52 NoisePub *string `json:"noisepub,omitempty"`
53 Exec map[string][]string `json:"exec,omitempty"`
54 Incoming *string `json:"incoming,omitempty"`
55 Freq *string `json:"freq,omitempty"`
56 FreqChunked *uint64 `json:"freqchunked,omitempty"`
57 FreqMinSize *uint64 `json:"freqminsize,omitempty"`
58 Via []string `json:"via,omitempty"`
59 Calls []CallJSON `json:"calls,omitempty"`
61 Addrs map[string]string `json:"addrs,omitempty"`
63 RxRate *int `json:"rxrate,omitempty"`
64 TxRate *int `json:"txrate,omitempty"`
65 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
66 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
69 type CallJSON struct {
71 Nice *string `json:"nice,omitempty"`
72 Xx *string `json:"xx,omitempty"`
73 RxRate *int `json:"rxrate,omitempty"`
74 TxRate *int `json:"txrate,omitempty"`
75 Addr *string `json:"addr,omitempty"`
76 OnlineDeadline *uint `json:"onlinedeadline,omitempty"`
77 MaxOnlineTime *uint `json:"maxonlinetime,omitempty"`
80 type NodeOurJSON struct {
82 ExchPub string `json:"exchpub"`
83 ExchPrv string `json:"exchprv"`
84 SignPub string `json:"signpub"`
85 SignPrv string `json:"signprv"`
86 NoisePrv string `json:"noiseprv"`
87 NoisePub string `json:"noisepub"`
90 type FromToJSON struct {
95 type NotifyJSON struct {
96 File *FromToJSON `json:"file,omitempty"`
97 Freq *FromToJSON `json:"freq,omitempty"`
100 type CfgJSON struct {
101 Spool string `json:"spool"`
102 Log string `json:"log"`
103 Umask string `json:"umask",omitempty`
105 Notify *NotifyJSON `json:"notify,omitempty"`
107 Self *NodeOurJSON `json:"self"`
108 Neigh map[string]NodeJSON `json:"neigh"`
111 func NewNode(name string, yml NodeJSON) (*Node, error) {
112 nodeId, err := NodeIdFromString(yml.Id)
117 exchPub, err := FromBase32(yml.ExchPub)
121 if len(exchPub) != 32 {
122 return nil, errors.New("Invalid exchPub size")
125 signPub, err := FromBase32(yml.SignPub)
129 if len(signPub) != ed25519.PublicKeySize {
130 return nil, errors.New("Invalid signPub size")
134 if yml.NoisePub != nil {
135 noisePub, err = FromBase32(*yml.NoisePub)
139 if len(noisePub) != 32 {
140 return nil, errors.New("Invalid noisePub size")
145 if yml.Incoming != nil {
146 inc := path.Clean(*yml.Incoming)
147 if !path.IsAbs(inc) {
148 return nil, errors.New("Incoming path must be absolute")
155 fr := path.Clean(*yml.Freq)
157 return nil, errors.New("Freq path must be absolute")
161 var freqChunked int64
162 if yml.FreqChunked != nil {
163 if *yml.FreqChunked == 0 {
164 return nil, errors.New("freqchunked value must be greater than zero")
166 freqChunked = int64(*yml.FreqChunked) * 1024
168 var freqMinSize int64
169 if yml.FreqMinSize != nil {
170 freqMinSize = int64(*yml.FreqMinSize) * 1024
174 if yml.RxRate != nil && *yml.RxRate > 0 {
175 defRxRate = *yml.RxRate
178 if yml.TxRate != nil && *yml.TxRate > 0 {
179 defTxRate = *yml.TxRate
182 defOnlineDeadline := uint(DefaultDeadline)
183 if yml.OnlineDeadline != nil {
184 if *yml.OnlineDeadline <= 0 {
185 return nil, errors.New("OnlineDeadline must be at least 1 second")
187 defOnlineDeadline = *yml.OnlineDeadline
189 var defMaxOnlineTime uint
190 if yml.MaxOnlineTime != nil {
191 defMaxOnlineTime = *yml.MaxOnlineTime
195 for _, callYml := range yml.Calls {
196 expr, err := cronexpr.Parse(callYml.Cron)
202 if callYml.Nice != nil {
203 nice, err = NicenessParse(*callYml.Nice)
210 if callYml.Xx != nil {
217 return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
222 if callYml.RxRate != nil {
223 rxRate = *callYml.RxRate
226 if callYml.TxRate != nil {
227 txRate = *callYml.TxRate
231 if callYml.Addr != nil {
232 if a, exists := yml.Addrs[*callYml.Addr]; exists {
239 onlineDeadline := defOnlineDeadline
240 if callYml.OnlineDeadline != nil {
241 if *callYml.OnlineDeadline == 0 {
242 return nil, errors.New("OnlineDeadline must be at least 1 second")
244 onlineDeadline = *callYml.OnlineDeadline
247 var maxOnlineTime uint
248 if callYml.MaxOnlineTime != nil {
249 maxOnlineTime = *callYml.MaxOnlineTime
252 calls = append(calls, &Call{
259 OnlineDeadline: onlineDeadline,
260 MaxOnlineTime: maxOnlineTime,
267 ExchPub: new([32]byte),
268 SignPub: ed25519.PublicKey(signPub),
272 FreqChunked: freqChunked,
273 FreqMinSize: freqMinSize,
278 OnlineDeadline: defOnlineDeadline,
279 MaxOnlineTime: defMaxOnlineTime,
281 copy(node.ExchPub[:], exchPub)
282 if len(noisePub) > 0 {
283 node.NoisePub = new([32]byte)
284 copy(node.NoisePub[:], noisePub)
289 func NewNodeOur(yml *NodeOurJSON) (*NodeOur, error) {
290 id, err := NodeIdFromString(yml.Id)
295 exchPub, err := FromBase32(yml.ExchPub)
299 if len(exchPub) != 32 {
300 return nil, errors.New("Invalid exchPub size")
303 exchPrv, err := FromBase32(yml.ExchPrv)
307 if len(exchPrv) != 32 {
308 return nil, errors.New("Invalid exchPrv size")
311 signPub, err := FromBase32(yml.SignPub)
315 if len(signPub) != ed25519.PublicKeySize {
316 return nil, errors.New("Invalid signPub size")
319 signPrv, err := FromBase32(yml.SignPrv)
323 if len(signPrv) != ed25519.PrivateKeySize {
324 return nil, errors.New("Invalid signPrv size")
327 noisePub, err := FromBase32(yml.NoisePub)
331 if len(noisePub) != 32 {
332 return nil, errors.New("Invalid noisePub size")
335 noisePrv, err := FromBase32(yml.NoisePrv)
339 if len(noisePrv) != 32 {
340 return nil, errors.New("Invalid noisePrv size")
345 ExchPub: new([32]byte),
346 ExchPrv: new([32]byte),
347 SignPub: ed25519.PublicKey(signPub),
348 SignPrv: ed25519.PrivateKey(signPrv),
349 NoisePub: new([32]byte),
350 NoisePrv: new([32]byte),
352 copy(node.ExchPub[:], exchPub)
353 copy(node.ExchPrv[:], exchPrv)
354 copy(node.NoisePub[:], noisePub)
355 copy(node.NoisePrv[:], noisePrv)
359 func CfgParse(data []byte) (*Ctx, error) {
361 if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
362 os.Stderr.WriteString("Passphrase:")
363 password, err := terminal.ReadPassword(0)
367 os.Stderr.WriteString("\n")
368 data, err = DeEBlob(data, password)
373 var cfgGeneral map[string]interface{}
374 if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
377 marshaled, err := json.Marshal(cfgGeneral)
382 if err = json.Unmarshal(marshaled, &cfgJSON); err != nil {
385 if _, exists := cfgJSON.Neigh["self"]; !exists {
386 return nil, errors.New("self neighbour missing")
389 if cfgJSON.Self != nil {
390 self, err = NewNodeOur(cfgJSON.Self)
395 spoolPath := path.Clean(cfgJSON.Spool)
396 if !path.IsAbs(spoolPath) {
397 return nil, errors.New("Spool path must be absolute")
399 logPath := path.Clean(cfgJSON.Log)
400 if !path.IsAbs(logPath) {
401 return nil, errors.New("Log path must be absolute")
404 if cfgJSON.Umask != "" {
405 r, err := strconv.ParseUint(cfgJSON.Umask, 8, 16)
415 UmaskForce: umaskForce,
417 Neigh: make(map[NodeId]*Node, len(cfgJSON.Neigh)),
418 Alias: make(map[string]*NodeId),
420 if cfgJSON.Notify != nil {
421 if cfgJSON.Notify.File != nil {
422 ctx.NotifyFile = cfgJSON.Notify.File
424 if cfgJSON.Notify.Freq != nil {
425 ctx.NotifyFreq = cfgJSON.Notify.Freq
428 vias := make(map[NodeId][]string)
429 for name, neighJSON := range cfgJSON.Neigh {
430 neigh, err := NewNode(name, neighJSON)
434 ctx.Neigh[*neigh.Id] = neigh
435 if _, already := ctx.Alias[name]; already {
436 return nil, errors.New("Node names conflict")
438 ctx.Alias[name] = neigh.Id
439 vias[*neigh.Id] = neighJSON.Via
441 ctx.SelfId = ctx.Alias["self"]
442 for neighId, viasRaw := range vias {
443 for _, viaRaw := range viasRaw {
444 foundNodeId, err := ctx.FindNode(viaRaw)
448 ctx.Neigh[neighId].Via = append(
449 ctx.Neigh[neighId].Via,