]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cfg.go
Ability to completely omit noisepub field
[nncp.git] / src / cypherpunks.ru / nncp / cfg.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package nncp
20
21 import (
22         "errors"
23         "os"
24         "path"
25
26         "golang.org/x/crypto/ed25519"
27         "gopkg.in/yaml.v2"
28 )
29
30 const (
31         CfgPathEnv = "NNCPCFG"
32 )
33
34 var (
35         DefaultCfgPath      string = "/usr/local/etc/nncp.yaml"
36         DefaultSendmailPath string = "/usr/sbin/sendmail"
37 )
38
39 type NodeYAML struct {
40         Id       string
41         ExchPub  string
42         SignPub  string
43         NoisePub *string `noisepub,omitempty`
44         Sendmail []string
45         Incoming *string  `incoming,omitempty`
46         Freq     *string  `freq,omitempty`
47         Via      []string `via,omitempty`
48
49         Addrs map[string]string `addrs,omitempty`
50 }
51
52 type NodeOurYAML struct {
53         Id       string
54         ExchPub  string
55         ExchPrv  string
56         SignPub  string
57         SignPrv  string
58         NoisePrv string
59         NoisePub string
60 }
61
62 type FromToYAML struct {
63         From string
64         To   string
65 }
66
67 type NotifyYAML struct {
68         File *FromToYAML `file,omitempty`
69         Freq *FromToYAML `freq,omitempty`
70 }
71
72 type CfgYAML struct {
73         Self  NodeOurYAML
74         Neigh map[string]NodeYAML
75
76         Spool  string
77         Log    string
78         Notify *NotifyYAML `notify,omitempty`
79 }
80
81 func NewNode(name string, yml NodeYAML) (*Node, error) {
82         nodeId, err := NodeIdFromString(yml.Id)
83         if err != nil {
84                 return nil, err
85         }
86
87         exchPub, err := FromBase32(yml.ExchPub)
88         if err != nil {
89                 return nil, err
90         }
91         if len(exchPub) != 32 {
92                 return nil, errors.New("Invalid exchPub size")
93         }
94
95         signPub, err := FromBase32(yml.SignPub)
96         if err != nil {
97                 return nil, err
98         }
99         if len(signPub) != ed25519.PublicKeySize {
100                 return nil, errors.New("Invalid signPub size")
101         }
102
103         var noisePub []byte
104         if yml.NoisePub != nil {
105                 noisePub, err = FromBase32(*yml.NoisePub)
106                 if err != nil {
107                         return nil, err
108                 }
109                 if len(noisePub) != 32 {
110                         return nil, errors.New("Invalid noisePub size")
111                 }
112         }
113
114         var incoming *string
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")
119                 }
120                 incoming = &inc
121         }
122
123         var freq *string
124         if yml.Freq != nil {
125                 fr := path.Clean(*yml.Freq)
126                 if !path.IsAbs(fr) {
127                         return nil, errors.New("Freq path must be absolute")
128                 }
129                 freq = &fr
130         }
131
132         node := Node{
133                 Name:     name,
134                 Id:       nodeId,
135                 ExchPub:  new([32]byte),
136                 SignPub:  ed25519.PublicKey(signPub),
137                 Sendmail: yml.Sendmail,
138                 Incoming: incoming,
139                 Freq:     freq,
140                 Addrs:    yml.Addrs,
141         }
142         copy(node.ExchPub[:], exchPub)
143         if len(noisePub) > 0 {
144                 node.NoisePub = new([32]byte)
145                 copy(node.NoisePub[:], noisePub)
146         }
147         return &node, nil
148 }
149
150 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
151         id, err := NodeIdFromString(yml.Id)
152         if err != nil {
153                 return nil, err
154         }
155
156         exchPub, err := FromBase32(yml.ExchPub)
157         if err != nil {
158                 return nil, err
159         }
160         if len(exchPub) != 32 {
161                 return nil, errors.New("Invalid exchPub size")
162         }
163
164         exchPrv, err := FromBase32(yml.ExchPrv)
165         if err != nil {
166                 return nil, err
167         }
168         if len(exchPrv) != 32 {
169                 return nil, errors.New("Invalid exchPrv size")
170         }
171
172         signPub, err := FromBase32(yml.SignPub)
173         if err != nil {
174                 return nil, err
175         }
176         if len(signPub) != ed25519.PublicKeySize {
177                 return nil, errors.New("Invalid signPub size")
178         }
179
180         signPrv, err := FromBase32(yml.SignPrv)
181         if err != nil {
182                 return nil, err
183         }
184         if len(signPrv) != ed25519.PrivateKeySize {
185                 return nil, errors.New("Invalid signPrv size")
186         }
187
188         noisePub, err := FromBase32(yml.NoisePub)
189         if err != nil {
190                 return nil, err
191         }
192         if len(noisePub) != 32 {
193                 return nil, errors.New("Invalid noisePub size")
194         }
195
196         noisePrv, err := FromBase32(yml.NoisePrv)
197         if err != nil {
198                 return nil, err
199         }
200         if len(noisePrv) != 32 {
201                 return nil, errors.New("Invalid noisePrv size")
202         }
203
204         node := NodeOur{
205                 Id:       id,
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),
212         }
213         copy(node.ExchPub[:], exchPub)
214         copy(node.ExchPrv[:], exchPrv)
215         copy(node.NoisePub[:], noisePub)
216         copy(node.NoisePrv[:], noisePrv)
217         return &node, nil
218 }
219
220 func (nodeOur *NodeOur) ToYAML() string {
221         yml := NodeOurYAML{
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[:]),
229         }
230         raw, err := yaml.Marshal(&yml)
231         if err != nil {
232                 panic(err)
233         }
234         return string(raw)
235 }
236
237 func CfgParse(data []byte) (*Ctx, error) {
238         var cfgYAML CfgYAML
239         err := yaml.Unmarshal(data, &cfgYAML)
240         if err != nil {
241                 return nil, err
242         }
243         self, err := NewNodeOur(cfgYAML.Self)
244         if err != nil {
245                 return nil, err
246         }
247         spoolPath := path.Clean(cfgYAML.Spool)
248         if !path.IsAbs(spoolPath) {
249                 return nil, errors.New("Spool path must be absolute")
250         }
251         logPath := path.Clean(cfgYAML.Log)
252         if !path.IsAbs(logPath) {
253                 return nil, errors.New("Log path must be absolute")
254         }
255         ctx := Ctx{
256                 Spool:   spoolPath,
257                 LogPath: logPath,
258                 Self:    self,
259                 Neigh:   make(map[NodeId]*Node, len(cfgYAML.Neigh)),
260                 Alias:   make(map[string]*NodeId),
261         }
262         if cfgYAML.Notify != nil {
263                 if cfgYAML.Notify.File != nil {
264                         ctx.NotifyFile = cfgYAML.Notify.File
265                 }
266                 if cfgYAML.Notify.Freq != nil {
267                         ctx.NotifyFreq = cfgYAML.Notify.Freq
268                 }
269         }
270         vias := make(map[NodeId][]string)
271         for name, neighYAML := range cfgYAML.Neigh {
272                 neigh, err := NewNode(name, neighYAML)
273                 if err != nil {
274                         return nil, err
275                 }
276                 ctx.Neigh[*neigh.Id] = neigh
277                 if _, already := ctx.Alias[name]; already {
278                         return nil, errors.New("Node names conflict")
279                 }
280                 ctx.Alias[name] = neigh.Id
281                 vias[*neigh.Id] = neighYAML.Via
282         }
283         for neighId, viasRaw := range vias {
284                 for _, viaRaw := range viasRaw {
285                         foundNodeId, err := ctx.FindNode(viaRaw)
286                         if err != nil {
287                                 return nil, err
288                         }
289                         ctx.Neigh[neighId].Via = append(
290                                 ctx.Neigh[neighId].Via,
291                                 foundNodeId.Id,
292                         )
293                 }
294         }
295         return &ctx, nil
296 }
297
298 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
299         p = os.Getenv(CfgPathEnv)
300         if p == "" {
301                 p = *cmdlineFlag
302         }
303         return
304 }