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 {
43 NoisePub *string `noisepub,omitempty`
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")
104 if yml.NoisePub != nil {
105 noisePub, err = FromBase32(*yml.NoisePub)
109 if len(noisePub) != 32 {
110 return nil, errors.New("Invalid noisePub size")
115 if yml.Incoming != nil {
116 inc := path.Clean(*yml.Incoming)
117 if !path.IsAbs(inc) {
118 return nil, errors.New("Incoming path must be absolute")
125 fr := path.Clean(*yml.Freq)
127 return nil, errors.New("Freq path must be absolute")
135 ExchPub: new([32]byte),
136 SignPub: ed25519.PublicKey(signPub),
137 Sendmail: yml.Sendmail,
142 copy(node.ExchPub[:], exchPub)
143 if len(noisePub) > 0 {
144 node.NoisePub = new([32]byte)
145 copy(node.NoisePub[:], noisePub)
150 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
151 id, err := NodeIdFromString(yml.Id)
156 exchPub, err := FromBase32(yml.ExchPub)
160 if len(exchPub) != 32 {
161 return nil, errors.New("Invalid exchPub size")
164 exchPrv, err := FromBase32(yml.ExchPrv)
168 if len(exchPrv) != 32 {
169 return nil, errors.New("Invalid exchPrv size")
172 signPub, err := FromBase32(yml.SignPub)
176 if len(signPub) != ed25519.PublicKeySize {
177 return nil, errors.New("Invalid signPub size")
180 signPrv, err := FromBase32(yml.SignPrv)
184 if len(signPrv) != ed25519.PrivateKeySize {
185 return nil, errors.New("Invalid signPrv size")
188 noisePub, err := FromBase32(yml.NoisePub)
192 if len(noisePub) != 32 {
193 return nil, errors.New("Invalid noisePub size")
196 noisePrv, err := FromBase32(yml.NoisePrv)
200 if len(noisePrv) != 32 {
201 return nil, errors.New("Invalid noisePrv size")
206 ExchPub: new([32]byte),
207 ExchPrv: new([32]byte),
208 SignPub: ed25519.PublicKey(signPub),
209 SignPrv: ed25519.PrivateKey(signPrv),
210 NoisePub: new([32]byte),
211 NoisePrv: new([32]byte),
213 copy(node.ExchPub[:], exchPub)
214 copy(node.ExchPrv[:], exchPrv)
215 copy(node.NoisePub[:], noisePub)
216 copy(node.NoisePrv[:], noisePrv)
220 func (nodeOur *NodeOur) ToYAML() string {
222 Id: nodeOur.Id.String(),
223 ExchPub: ToBase32(nodeOur.ExchPub[:]),
224 ExchPrv: ToBase32(nodeOur.ExchPrv[:]),
225 SignPub: ToBase32(nodeOur.SignPub[:]),
226 SignPrv: ToBase32(nodeOur.SignPrv[:]),
227 NoisePub: ToBase32(nodeOur.NoisePub[:]),
228 NoisePrv: ToBase32(nodeOur.NoisePrv[:]),
230 raw, err := yaml.Marshal(&yml)
237 func CfgParse(data []byte) (*Ctx, error) {
239 err := yaml.Unmarshal(data, &cfgYAML)
243 self, err := NewNodeOur(cfgYAML.Self)
247 spoolPath := path.Clean(cfgYAML.Spool)
248 if !path.IsAbs(spoolPath) {
249 return nil, errors.New("Spool path must be absolute")
251 logPath := path.Clean(cfgYAML.Log)
252 if !path.IsAbs(logPath) {
253 return nil, errors.New("Log path must be absolute")
259 Neigh: make(map[NodeId]*Node, len(cfgYAML.Neigh)),
260 Alias: make(map[string]*NodeId),
262 if cfgYAML.Notify != nil {
263 if cfgYAML.Notify.File != nil {
264 ctx.NotifyFile = cfgYAML.Notify.File
266 if cfgYAML.Notify.Freq != nil {
267 ctx.NotifyFreq = cfgYAML.Notify.Freq
270 vias := make(map[NodeId][]string)
271 for name, neighYAML := range cfgYAML.Neigh {
272 neigh, err := NewNode(name, neighYAML)
276 ctx.Neigh[*neigh.Id] = neigh
277 if _, already := ctx.Alias[name]; already {
278 return nil, errors.New("Node names conflict")
280 ctx.Alias[name] = neigh.Id
281 vias[*neigh.Id] = neighYAML.Via
283 for neighId, viasRaw := range vias {
284 for _, viaRaw := range viasRaw {
285 foundNodeId, err := ctx.FindNode(viaRaw)
289 ctx.Neigh[neighId].Via = append(
290 ctx.Neigh[neighId].Via,
298 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
299 p = os.Getenv(CfgPathEnv)