]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-cfgnew/main.go
Raise copyright years
[nncp.git] / src / cmd / nncp-cfgnew / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Generate new NNCP node keys and configuration file
19 package main
20
21 import (
22         "crypto/rand"
23         "encoding/json"
24         "flag"
25         "fmt"
26         "log"
27         "os"
28
29         "github.com/hjson/hjson-go"
30         "golang.org/x/crypto/blake2b"
31         "golang.org/x/crypto/nacl/box"
32
33         "go.cypherpunks.ru/nncp/v8"
34 )
35
36 func usage() {
37         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
38         fmt.Fprintln(os.Stderr, "nncp-cfgnew -- generate new configuration and keys\nOptions:")
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
44                 areaName   = flag.String("area", "", "Generate area's keypairs")
45                 noComments = flag.Bool("nocomments", false, "Do not include descriptive comments")
46                 version    = flag.Bool("version", false, "Print version information")
47                 warranty   = flag.Bool("warranty", false, "Print warranty information")
48         )
49         log.SetFlags(log.Lshortfile)
50         flag.Usage = usage
51         flag.Parse()
52         if *warranty {
53                 fmt.Println(nncp.Warranty)
54                 return
55         }
56         if *version {
57                 fmt.Println(nncp.VersionGet())
58                 return
59         }
60         if *areaName != "" {
61                 pub, prv, err := box.GenerateKey(rand.Reader)
62                 if err != nil {
63                         log.Fatalln(err)
64                 }
65                 areaId := nncp.AreaId(blake2b.Sum256(pub[:]))
66                 var cfgRaw string
67                 if *noComments {
68                         cfgRaw = fmt.Sprintf(`areas: {
69   %s: {
70     id: %s
71     # KEEP AWAY keypair from the nodes you want only participate in multicast
72     pub: %s
73     prv: %s
74   }
75 }`,
76                                 *areaName,
77                                 areaId.String(),
78                                 nncp.Base32Codec.EncodeToString(pub[:]),
79                                 nncp.Base32Codec.EncodeToString(prv[:]),
80                         )
81                 } else {
82                         cfgRaw = fmt.Sprintf(`areas: {
83   %s: {
84     id: %s
85
86     # KEEP AWAY keypair from the nodes you want only participate in multicast
87     pub: %s
88     prv: %s
89
90     # List of subscribers you should multicast area messages to
91     # subs: ["alice"]
92
93     # Allow incoming files (from the area) saving in that directory
94     # incoming: /home/areas/%s/incoming
95
96     # Allow incoming area commands execution
97     # exec: {sendmail: ["%s"]}
98
99     # Allow unknown sender's message tossing (relaying will be made anyway)
100     # allow-unknown: true
101   }
102 }`,
103                                 *areaName,
104                                 areaId.String(),
105                                 nncp.Base32Codec.EncodeToString(pub[:]),
106                                 nncp.Base32Codec.EncodeToString(prv[:]),
107                                 *areaName,
108                                 nncp.DefaultSendmailPath,
109                         )
110                 }
111                 var cfgGeneral map[string]interface{}
112                 if err = hjson.Unmarshal([]byte(cfgRaw), &cfgGeneral); err != nil {
113                         panic(err)
114                 }
115                 marshaled, err := json.Marshal(cfgGeneral)
116                 if err != nil {
117                         panic(err)
118                 }
119                 var areas map[string]nncp.AreaJSON
120                 if err = json.Unmarshal(marshaled, &areas); err != nil {
121                         panic(err)
122                 }
123                 fmt.Println(cfgRaw)
124                 return
125         }
126         nodeOur, err := nncp.NewNodeGenerate()
127         if err != nil {
128                 log.Fatalln(err)
129         }
130         var cfgRaw string
131         if *noComments {
132                 cfgRaw = fmt.Sprintf(`{
133   spool: %s
134   log: %s
135
136   self: {
137     # DO NOT show anyone your private keys!!!
138     id: %s
139     exchpub: %s
140     exchprv: %s
141     signpub: %s
142     signprv: %s
143     noiseprv: %s
144     noisepub: %s
145   }
146
147   neigh: {
148     self: {
149       id: %s
150       exchpub: %s
151       signpub: %s
152       noisepub: %s
153       exec: {sendmail: ["%s"]}
154     }
155   }
156 }`,
157                         nncp.DefaultSpoolPath,
158                         nncp.DefaultLogPath,
159                         nodeOur.Id.String(),
160                         nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
161                         nncp.Base32Codec.EncodeToString(nodeOur.ExchPrv[:]),
162                         nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
163                         nncp.Base32Codec.EncodeToString(nodeOur.SignPrv[:]),
164                         nncp.Base32Codec.EncodeToString(nodeOur.NoisePrv[:]),
165                         nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
166                         nodeOur.Id.String(),
167                         nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
168                         nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
169                         nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
170                         nncp.DefaultSendmailPath,
171                 )
172         } else {
173                 cfgRaw = fmt.Sprintf(`{
174   # Path to encrypted packets spool directory
175   spool: %s
176   # Path to log file
177   log: %s
178   # Enforce specified umask usage
179   # umask: "022"
180   # Omit progress showing by default
181   # noprogress: true
182   # Do not use hdr/ files
183   # nohdr: true
184
185   # MultiCast Discovery:
186   # List of interfaces where to listen for MCD announcements
187   # mcd-listen: ["em0", "igb1"]
188   # Interfaces and intervals (in seconds) where to send MCD announcements
189   # mcd-send: {em0: 60, igb1: 5}
190
191   # Enable notification email sending
192   # notify: {
193   #   file: {
194   #     from: nncp@localhost
195   #     to: user+file@example.com
196   #   }
197   #   freq: {
198   #     from: nncp@localhost
199   #     to: user+freq@example.com
200   #   }
201   #   # Send some exec commands execution notifications
202   #   exec: {
203   #     # bob neighbour's "somehandle" notification
204   #     bob.somehandle: {
205   #       from: nncp+bob@localhost
206   #       to: user+somehandle@example.com
207   #     }
208   #     # Any neighboor's "anotherhandle"
209   #     *.anotherhandle: {
210   #       from: nncp@localhost
211   #       to: user+anotherhandle@example.com
212   #     }
213   #   }
214   # }
215
216   self: {
217     # DO NOT show anyone your private keys!!!
218     id: %s
219     exchpub: %s
220     exchprv: %s
221     signpub: %s
222     signprv: %s
223     noiseprv: %s
224     noisepub: %s
225   }
226
227   neigh: {
228     self: {
229       # You should give public keys below to your neighbours
230       id: %s
231       exchpub: %s
232       signpub: %s
233       noisepub: %s
234
235       exec: {
236         # Default self's sendmail command is used for email notifications sending
237         sendmail: ["%s"]
238       }
239     }
240
241     # Example neighbour, most of fields are optional
242     # alice: {
243     #   id: XJZBK...65IJQ
244     #   exchpub: MJACJ...FAI6A
245     #   signpub: T4AFC...N2FRQ
246     #   noisepub: UBM5K...VI42A
247     #
248     #   # He is allowed to send email
249     #   # exec: {sendmail: ["%s"]}
250     #
251     #   # Allow incoming files saving in that directory
252     #   # incoming: "/home/alice/incoming"
253     #
254     #   # Transitional nodes path
255     #   # via: ["bob", "eve"]
256     #
257     #   # Inactivity timeout when session with remote peer should be terminated
258     #   # onlinedeadline: 1800
259     #
260     #   # Maximal online session lifetime
261     #   # maxonlinetime: 3600
262     #
263     #   # If neither freq section, nor freq.path exist, then no freqing allowed
264     #   # freq: {
265     #   #   # Allow freqing from that directory
266     #   #   path: "/home/bob/pub"
267     #   #   # Send freqed files with chunks
268     #   #   # chunked: 1024
269     #   #   # Send freqed files with minumal chunk size
270     #   #   # minsize: 2048
271     #   #   # Maximal allowable freqing file size
272     #   #   # maxsize: 4096
273     #   # }
274     #
275     #   # Set maximal packets per second receive and transmit rates
276     #   # rxrate: 10
277     #   # txrate: 20
278     #
279     #   # Address aliases
280     #   # addrs: {
281     #   #   lan: "[fe80::1234%%igb0]:5400"
282     #   #   internet: alice.com:3389
283     #   # }
284     #
285     #   # Calls configuration
286     #   # calls: [
287     #   #   {
288     #   #     cron: "*/2 * * * *"
289     #   #     onlinedeadline: 1800
290     #   #     maxonlinetime: 1750
291     #   #     nice: PRIORITY+10
292     #   #     rxrate: 10
293     #   #     txrate: 20
294     #   #     xx: rx
295     #   #     addr: lan
296     #   #     when-tx-exists: true
297     #   #     nock: true
298     #   #     mcd-ignore: true
299     #   #
300     #   #     autotoss: false
301     #   #     autotoss-doseen: true
302     #   #     autotoss-nofile: true
303     #   #     autotoss-nofreq: true
304     #   #     autotoss-noexec: true
305     #   #     autotoss-notrns: true
306     #   #   }
307     #   # ]
308     # }
309   }
310 }`,
311                         nncp.DefaultSpoolPath,
312                         nncp.DefaultLogPath,
313                         nodeOur.Id.String(),
314                         nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
315                         nncp.Base32Codec.EncodeToString(nodeOur.ExchPrv[:]),
316                         nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
317                         nncp.Base32Codec.EncodeToString(nodeOur.SignPrv[:]),
318                         nncp.Base32Codec.EncodeToString(nodeOur.NoisePrv[:]),
319                         nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
320                         nodeOur.Id.String(),
321                         nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
322                         nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
323                         nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
324                         nncp.DefaultSendmailPath,
325                         nncp.DefaultSendmailPath,
326                 )
327         }
328         if _, err = nncp.CfgParse([]byte(cfgRaw)); err != nil {
329                 panic(err)
330         }
331         fmt.Println(cfgRaw)
332 }