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