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/>.
26 "golang.org/x/crypto/ed25519"
31 CfgPathEnv = "NNCPCFG"
35 DefaultCfgPath string = "/usr/local/etc/nncp.yaml"
36 DefaultSendmailPath string = "/usr/sbin/sendmail"
39 type NodeYAML struct {
45 Incoming *string `incoming,omitempty`
46 Freq *string `freq,omitempty`
47 Via []string `via,omitempty`
49 Addrs map[string]string `addrs,omitempty`
52 type NodeOurYAML struct {
62 type FromToYAML struct {
67 type NotifyYAML struct {
68 File *FromToYAML `file,omitempty`
69 Freq *FromToYAML `freq,omitempty`
74 Neigh map[string]NodeYAML
78 Notify *NotifyYAML `notify,omitempty`
81 func NewNode(name string, yml NodeYAML) (*Node, error) {
82 nodeId, err := NodeIdFromString(yml.Id)
87 exchPub, err := FromBase32(yml.ExchPub)
91 if len(exchPub) != 32 {
92 return nil, errors.New("Invalid exchPub size")
95 signPub, err := FromBase32(yml.SignPub)
99 if len(signPub) != ed25519.PublicKeySize {
100 return nil, errors.New("Invalid signPub size")
103 noisePub, err := FromBase32(yml.NoisePub)
107 if len(noisePub) != 32 {
108 return nil, errors.New("Invalid noisePub size")
112 if yml.Incoming != nil {
113 inc := path.Clean(*yml.Incoming)
114 if !path.IsAbs(inc) {
115 return nil, errors.New("Incoming path must be absolute")
122 fr := path.Clean(*yml.Freq)
124 return nil, errors.New("Freq path must be absolute")
132 ExchPub: new([32]byte),
133 SignPub: ed25519.PublicKey(signPub),
134 NoisePub: new([32]byte),
135 Sendmail: yml.Sendmail,
140 copy(node.ExchPub[:], exchPub)
141 copy(node.NoisePub[:], noisePub)
145 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
146 id, err := NodeIdFromString(yml.Id)
151 exchPub, err := FromBase32(yml.ExchPub)
155 if len(exchPub) != 32 {
156 return nil, errors.New("Invalid exchPub size")
159 exchPrv, err := FromBase32(yml.ExchPrv)
163 if len(exchPrv) != 32 {
164 return nil, errors.New("Invalid exchPrv size")
167 signPub, err := FromBase32(yml.SignPub)
171 if len(signPub) != ed25519.PublicKeySize {
172 return nil, errors.New("Invalid signPub size")
175 signPrv, err := FromBase32(yml.SignPrv)
179 if len(signPrv) != ed25519.PrivateKeySize {
180 return nil, errors.New("Invalid signPrv size")
183 noisePub, err := FromBase32(yml.NoisePub)
187 if len(noisePub) != 32 {
188 return nil, errors.New("Invalid noisePub size")
191 noisePrv, err := FromBase32(yml.NoisePrv)
195 if len(noisePrv) != 32 {
196 return nil, errors.New("Invalid noisePrv size")
201 ExchPub: new([32]byte),
202 ExchPrv: new([32]byte),
203 SignPub: ed25519.PublicKey(signPub),
204 SignPrv: ed25519.PrivateKey(signPrv),
205 NoisePub: new([32]byte),
206 NoisePrv: new([32]byte),
208 copy(node.ExchPub[:], exchPub)
209 copy(node.ExchPrv[:], exchPrv)
210 copy(node.NoisePub[:], noisePub)
211 copy(node.NoisePrv[:], noisePrv)
215 func (nodeOur *NodeOur) ToYAML() string {
217 Id: nodeOur.Id.String(),
218 ExchPub: ToBase32(nodeOur.ExchPub[:]),
219 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
220 SignPub: ToBase32(nodeOur.SignPub[:]),
221 SignPrv: ToBase32(nodeOur.SignPrv[:]),
222 NoisePub: ToBase32(nodeOur.NoisePub[:]),
223 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
225 raw, err := yaml.Marshal(&yml)
232 func CfgParse(data []byte) (*Ctx, error) {
234 err := yaml.Unmarshal(data, &cfgYAML)
238 self, err := NewNodeOur(cfgYAML.Self)
242 spoolPath := path.Clean(cfgYAML.Spool)
243 if !path.IsAbs(spoolPath) {
244 return nil, errors.New("Spool path must be absolute")
246 logPath := path.Clean(cfgYAML.Log)
247 if !path.IsAbs(logPath) {
248 return nil, errors.New("Log path must be absolute")
254 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
255 Alias: make(map[string]*NodeId),
257 if cfgYAML.Notify != nil {
258 if cfgYAML.Notify.File != nil {
259 ctx.NotifyFile = cfgYAML.Notify.File
261 if cfgYAML.Notify.Freq != nil {
262 ctx.NotifyFreq = cfgYAML.Notify.Freq
265 vias := make(map[NodeId][]string)
266 for name, neighYAML := range cfgYAML.Neigh {
267 neigh, err := NewNode(name, neighYAML)
271 ctx.Neigh[*neigh.Id] = neigh
272 if _, already := ctx.Alias[name]; already {
273 return nil, errors.New("Node names conflict")
275 ctx.Alias[name] = neigh.Id
276 vias[*neigh.Id] = neighYAML.Via
278 for neighId, viasRaw := range vias {
279 for _, viaRaw := range viasRaw {
280 foundNodeId, err := ctx.FindNode(viaRaw)
284 ctx.Neigh[neighId].Via = append(
285 ctx.Neigh[neighId].Via,
293 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
294 p = os.Getenv(CfgPathEnv)