]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cfg.go
Ability to override path to configuration file via envvar
[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
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         noisePub, err := FromBase32(yml.NoisePub)
104         if err != nil {
105                 return nil, err
106         }
107         if len(noisePub) != 32 {
108                 return nil, errors.New("Invalid noisePub size")
109         }
110
111         var incoming *string
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")
116                 }
117                 incoming = &inc
118         }
119
120         var freq *string
121         if yml.Freq != nil {
122                 fr := path.Clean(*yml.Freq)
123                 if !path.IsAbs(fr) {
124                         return nil, errors.New("Freq path must be absolute")
125                 }
126                 freq = &fr
127         }
128
129         node := Node{
130                 Name:     name,
131                 Id:       nodeId,
132                 ExchPub:  new([32]byte),
133                 SignPub:  ed25519.PublicKey(signPub),
134                 NoisePub: new([32]byte),
135                 Sendmail: yml.Sendmail,
136                 Incoming: incoming,
137                 Freq:     freq,
138                 Addrs:    yml.Addrs,
139         }
140         copy(node.ExchPub[:], exchPub)
141         copy(node.NoisePub[:], noisePub)
142         return &node, nil
143 }
144
145 func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) {
146         id, err := NodeIdFromString(yml.Id)
147         if err != nil {
148                 return nil, err
149         }
150
151         exchPub, err := FromBase32(yml.ExchPub)
152         if err != nil {
153                 return nil, err
154         }
155         if len(exchPub) != 32 {
156                 return nil, errors.New("Invalid exchPub size")
157         }
158
159         exchPrv, err := FromBase32(yml.ExchPrv)
160         if err != nil {
161                 return nil, err
162         }
163         if len(exchPrv) != 32 {
164                 return nil, errors.New("Invalid exchPrv size")
165         }
166
167         signPub, err := FromBase32(yml.SignPub)
168         if err != nil {
169                 return nil, err
170         }
171         if len(signPub) != ed25519.PublicKeySize {
172                 return nil, errors.New("Invalid signPub size")
173         }
174
175         signPrv, err := FromBase32(yml.SignPrv)
176         if err != nil {
177                 return nil, err
178         }
179         if len(signPrv) != ed25519.PrivateKeySize {
180                 return nil, errors.New("Invalid signPrv size")
181         }
182
183         noisePub, err := FromBase32(yml.NoisePub)
184         if err != nil {
185                 return nil, err
186         }
187         if len(noisePub) != 32 {
188                 return nil, errors.New("Invalid noisePub size")
189         }
190
191         noisePrv, err := FromBase32(yml.NoisePrv)
192         if err != nil {
193                 return nil, err
194         }
195         if len(noisePrv) != 32 {
196                 return nil, errors.New("Invalid noisePrv size")
197         }
198
199         node := NodeOur{
200                 Id:       id,
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),
207         }
208         copy(node.ExchPub[:], exchPub)
209         copy(node.ExchPrv[:], exchPrv)
210         copy(node.NoisePub[:], noisePub)
211         copy(node.NoisePrv[:], noisePrv)
212         return &node, nil
213 }
214
215 func (nodeOur *NodeOur) ToYAML() string {
216         yml := NodeOurYAML{
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[:]),
224         }
225         raw, err := yaml.Marshal(&yml)
226         if err != nil {
227                 panic(err)
228         }
229         return string(raw)
230 }
231
232 func CfgParse(data []byte) (*Ctx, error) {
233         var cfgYAML CfgYAML
234         err := yaml.Unmarshal(data, &cfgYAML)
235         if err != nil {
236                 return nil, err
237         }
238         self, err := NewNodeOur(cfgYAML.Self)
239         if err != nil {
240                 return nil, err
241         }
242         spoolPath := path.Clean(cfgYAML.Spool)
243         if !path.IsAbs(spoolPath) {
244                 return nil, errors.New("Spool path must be absolute")
245         }
246         logPath := path.Clean(cfgYAML.Log)
247         if !path.IsAbs(logPath) {
248                 return nil, errors.New("Log path must be absolute")
249         }
250         ctx := Ctx{
251                 Spool:   spoolPath,
252                 LogPath: logPath,
253                 Self:    self,
254                 Neigh:   make(map[NodeId]*Node, len(cfgYAML.Neigh)),
255                 Alias:   make(map[string]*NodeId),
256         }
257         if cfgYAML.Notify != nil {
258                 if cfgYAML.Notify.File != nil {
259                         ctx.NotifyFile = cfgYAML.Notify.File
260                 }
261                 if cfgYAML.Notify.Freq != nil {
262                         ctx.NotifyFreq = cfgYAML.Notify.Freq
263                 }
264         }
265         vias := make(map[NodeId][]string)
266         for name, neighYAML := range cfgYAML.Neigh {
267                 neigh, err := NewNode(name, neighYAML)
268                 if err != nil {
269                         return nil, err
270                 }
271                 ctx.Neigh[*neigh.Id] = neigh
272                 if _, already := ctx.Alias[name]; already {
273                         return nil, errors.New("Node names conflict")
274                 }
275                 ctx.Alias[name] = neigh.Id
276                 vias[*neigh.Id] = neighYAML.Via
277         }
278         for neighId, viasRaw := range vias {
279                 for _, viaRaw := range viasRaw {
280                         foundNodeId, err := ctx.FindNode(viaRaw)
281                         if err != nil {
282                                 return nil, err
283                         }
284                         ctx.Neigh[neighId].Via = append(
285                                 ctx.Neigh[neighId].Via,
286                                 foundNodeId.Id,
287                         )
288                 }
289         }
290         return &ctx, nil
291 }
292
293 func CfgPathFromEnv(cmdlineFlag *string) (p string) {
294         p = os.Getenv(CfgPathEnv)
295         if p == "" {
296                 p = *cmdlineFlag
297         }
298         return
299 }