]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-bundle/main.go
Major namespace version was already updated
[nncp.git] / src / cmd / nncp-bundle / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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 // Create/digest stream of NNCP encrypted packets.
19 package main
20
21 import (
22         "archive/tar"
23         "bufio"
24         "bytes"
25         "errors"
26         "flag"
27         "fmt"
28         "io"
29         "io/ioutil"
30         "log"
31         "os"
32         "path/filepath"
33         "strings"
34
35         xdr "github.com/davecgh/go-xdr/xdr2"
36         "go.cypherpunks.ru/nncp/v6"
37         "golang.org/x/crypto/blake2b"
38 )
39
40 const (
41         CopyBufSize = 1 << 17
42 )
43
44 func usage() {
45         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
46         fmt.Fprintf(os.Stderr, "nncp-bundle -- Create/digest stream of NNCP encrypted packets\n\n")
47         fmt.Fprintf(os.Stderr, "Usage: %s [options] -tx [-delete] NODE [NODE ...] > ...\n", os.Args[0])
48         fmt.Fprintf(os.Stderr, "       %s [options] -rx -delete [-dryrun] [NODE ...] < ...\n", os.Args[0])
49         fmt.Fprintf(os.Stderr, "       %s [options] -rx [-check] [-dryrun] [NODE ...] < ...\n", os.Args[0])
50         fmt.Fprintln(os.Stderr, "Options:")
51         flag.PrintDefaults()
52 }
53
54 func main() {
55         var (
56                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
57                 niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
58                 doRx      = flag.Bool("rx", false, "Receive packets")
59                 doTx      = flag.Bool("tx", false, "Transfer packets")
60                 doDelete  = flag.Bool("delete", false, "Delete transferred packets")
61                 doCheck   = flag.Bool("check", false, "Check integrity while receiving")
62                 dryRun    = flag.Bool("dryrun", false, "Do no writes")
63                 spoolPath = flag.String("spool", "", "Override path to spool")
64                 logPath   = flag.String("log", "", "Override path to logfile")
65                 quiet     = flag.Bool("quiet", false, "Print only errors")
66                 showPrgrs = flag.Bool("progress", false, "Force progress showing")
67                 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
68                 debug     = flag.Bool("debug", false, "Print debug messages")
69                 version   = flag.Bool("version", false, "Print version information")
70                 warranty  = flag.Bool("warranty", false, "Print warranty information")
71         )
72         flag.Usage = usage
73         flag.Parse()
74         if *warranty {
75                 fmt.Println(nncp.Warranty)
76                 return
77         }
78         if *version {
79                 fmt.Println(nncp.VersionGet())
80                 return
81         }
82         nice, err := nncp.NicenessParse(*niceRaw)
83         if err != nil {
84                 log.Fatalln(err)
85         }
86         if *doRx && *doTx {
87                 log.Fatalln("-rx and -tx can not be set simultaneously")
88         }
89         if !*doRx && !*doTx {
90                 log.Fatalln("At least one of -rx and -tx must be specified")
91         }
92
93         ctx, err := nncp.CtxFromCmdline(
94                 *cfgPath,
95                 *spoolPath,
96                 *logPath,
97                 *quiet,
98                 *showPrgrs,
99                 *omitPrgrs,
100                 *debug,
101         )
102         if err != nil {
103                 log.Fatalln("Error during initialization:", err)
104         }
105
106         nodeIds := make(map[nncp.NodeId]struct{}, flag.NArg())
107         for i := 0; i < flag.NArg(); i++ {
108                 node, err := ctx.FindNode(flag.Arg(i))
109                 if err != nil {
110                         log.Fatalln("Invalid specified:", err)
111                 }
112                 nodeIds[*node.Id] = struct{}{}
113         }
114
115         ctx.Umask()
116
117         if *doTx {
118                 var pktName string
119                 bufStdout := bufio.NewWriter(os.Stdout)
120                 tarWr := tar.NewWriter(bufStdout)
121                 for nodeId := range nodeIds {
122                         les := nncp.LEs{
123                                 {K: "XX", V: string(nncp.TTx)},
124                                 {K: "Node", V: nodeId.String()},
125                                 {K: "Pkt", V: "dummy"},
126                         }
127                         for job := range ctx.Jobs(&nodeId, nncp.TTx) {
128                                 pktName = filepath.Base(job.Path)
129                                 les[len(les)-1].V = pktName
130                                 if job.PktEnc.Nice > nice {
131                                         ctx.LogD("nncp-bundle", les, "too nice")
132                                         continue
133                                 }
134                                 fd, err := os.Open(job.Path)
135                                 if err != nil {
136                                         log.Fatalln("Error during opening:", err)
137                                 }
138                                 if err = tarWr.WriteHeader(&tar.Header{
139                                         Format:   tar.FormatUSTAR,
140                                         Name:     nncp.NNCPBundlePrefix,
141                                         Mode:     0700,
142                                         Typeflag: tar.TypeDir,
143                                 }); err != nil {
144                                         log.Fatalln("Error writing tar header:", err)
145                                 }
146                                 if err = tarWr.WriteHeader(&tar.Header{
147                                         Format: tar.FormatPAX,
148                                         Name: strings.Join([]string{
149                                                 nncp.NNCPBundlePrefix,
150                                                 nodeId.String(),
151                                                 ctx.SelfId.String(),
152                                                 pktName,
153                                         }, "/"),
154                                         Mode:     0400,
155                                         Size:     job.Size,
156                                         Typeflag: tar.TypeReg,
157                                 }); err != nil {
158                                         log.Fatalln("Error writing tar header:", err)
159                                 }
160                                 if _, err = nncp.CopyProgressed(
161                                         tarWr, bufio.NewReader(fd), "Tx",
162                                         append(les, nncp.LEs{
163                                                 {K: "Pkt", V: nncp.Base32Codec.EncodeToString(job.HshValue[:])},
164                                                 {K: "FullSize", V: job.Size},
165                                         }...),
166                                         ctx.ShowPrgrs,
167                                 ); err != nil {
168                                         log.Fatalln("Error during copying to tar:", err)
169                                 }
170                                 if err = fd.Close(); err != nil {
171                                         log.Fatalln("Error during closing:", err)
172                                 }
173                                 if err = tarWr.Flush(); err != nil {
174                                         log.Fatalln("Error during tar flushing:", err)
175                                 }
176                                 if err = bufStdout.Flush(); err != nil {
177                                         log.Fatalln("Error during stdout flushing:", err)
178                                 }
179                                 if *doDelete {
180                                         if err = os.Remove(job.Path); err != nil {
181                                                 log.Fatalln("Error during deletion:", err)
182                                         } else if ctx.HdrUsage {
183                                                 os.Remove(job.Path + nncp.HdrSuffix)
184                                         }
185                                 }
186                                 ctx.LogI("nncp-bundle", append(les, nncp.LE{K: "Size", V: job.Size}), "")
187                         }
188                 }
189                 if err = tarWr.Close(); err != nil {
190                         log.Fatalln("Error during tar closing:", err)
191                 }
192         } else {
193                 bufStdin := bufio.NewReaderSize(os.Stdin, CopyBufSize*2)
194                 pktEncBuf := make([]byte, nncp.PktEncOverhead)
195                 var pktEnc *nncp.PktEnc
196                 for {
197                         peeked, err := bufStdin.Peek(CopyBufSize)
198                         if err != nil && err != io.EOF {
199                                 log.Fatalln("Error during reading:", err)
200                         }
201                         prefixIdx := bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix))
202                         if prefixIdx == -1 {
203                                 if err == io.EOF {
204                                         break
205                                 }
206                                 bufStdin.Discard(bufStdin.Buffered() - (len(nncp.NNCPBundlePrefix) - 1)) // #nosec G104
207                                 continue
208                         }
209                         if _, err = bufStdin.Discard(prefixIdx); err != nil {
210                                 panic(err)
211                         }
212                         tarR := tar.NewReader(bufStdin)
213                         entry, err := tarR.Next()
214                         if err != nil {
215                                 if err != io.EOF {
216                                         ctx.LogD(
217                                                 "nncp-bundle",
218                                                 nncp.LEs{{K: "XX", V: string(nncp.TRx)}, {K: "Err", V: err}},
219                                                 "error reading tar",
220                                         )
221                                 }
222                                 continue
223                         }
224                         if entry.Typeflag != tar.TypeDir {
225                                 ctx.LogD(
226                                         "nncp-bundle",
227                                         nncp.LEs{{K: "XX", V: string(nncp.TRx)}},
228                                         "Expected NNCP/",
229                                 )
230                                 continue
231                         }
232                         entry, err = tarR.Next()
233                         if err != nil {
234                                 if err != io.EOF {
235                                         ctx.LogD(
236                                                 "nncp-bundle",
237                                                 nncp.LEs{{K: "XX", V: string(nncp.TRx)}, {K: "Err", V: err}},
238                                                 "error reading tar",
239                                         )
240                                 }
241                                 continue
242                         }
243                         les := nncp.LEs{{K: "XX", V: string(nncp.TRx)}, {K: "Pkt", V: entry.Name}}
244                         if entry.Size < nncp.PktEncOverhead {
245                                 ctx.LogD("nncp-bundle", les, "Too small packet")
246                                 continue
247                         }
248                         if !ctx.IsEnoughSpace(entry.Size) {
249                                 ctx.LogE("nncp-bundle", les, errors.New("not enough spool space"), "")
250                                 continue
251                         }
252                         pktName := filepath.Base(entry.Name)
253                         if _, err = nncp.Base32Codec.DecodeString(pktName); err != nil {
254                                 ctx.LogD("nncp-bundle", append(les, nncp.LE{K: "Err", V: "bad packet name"}), "")
255                                 continue
256                         }
257                         if _, err = io.ReadFull(tarR, pktEncBuf); err != nil {
258                                 ctx.LogD("nncp-bundle", append(les, nncp.LE{K: "Err", V: err}), "read")
259                                 continue
260                         }
261                         if _, err = xdr.Unmarshal(bytes.NewReader(pktEncBuf), &pktEnc); err != nil {
262                                 ctx.LogD("nncp-bundle", les, "Bad packet structure")
263                                 continue
264                         }
265                         if pktEnc.Magic != nncp.MagicNNCPEv4 {
266                                 ctx.LogD("nncp-bundle", les, "Bad packet magic number")
267                                 continue
268                         }
269                         if pktEnc.Nice > nice {
270                                 ctx.LogD("nncp-bundle", les, "too nice")
271                                 continue
272                         }
273                         if *pktEnc.Sender == *ctx.SelfId && *doDelete {
274                                 if len(nodeIds) > 0 {
275                                         if _, exists := nodeIds[*pktEnc.Recipient]; !exists {
276                                                 ctx.LogD("nncp-bundle", les, "Recipient is not requested")
277                                                 continue
278                                         }
279                                 }
280                                 nodeId32 := nncp.Base32Codec.EncodeToString(pktEnc.Recipient[:])
281                                 les := nncp.LEs{
282                                         {K: "XX", V: string(nncp.TTx)},
283                                         {K: "Node", V: nodeId32},
284                                         {K: "Pkt", V: pktName},
285                                 }
286                                 dstPath := filepath.Join(ctx.Spool, nodeId32, string(nncp.TTx), pktName)
287                                 if _, err = os.Stat(dstPath); err != nil {
288                                         ctx.LogD("nncp-bundle", les, "Packet is already missing")
289                                         continue
290                                 }
291                                 hsh, err := blake2b.New256(nil)
292                                 if err != nil {
293                                         log.Fatalln("Error during hasher creation:", err)
294                                 }
295                                 if _, err = hsh.Write(pktEncBuf); err != nil {
296                                         log.Fatalln("Error during writing:", err)
297                                 }
298                                 if _, err = nncp.CopyProgressed(
299                                         hsh, tarR, "Rx",
300                                         append(les, nncp.LE{K: "FullSize", V: entry.Size}),
301                                         ctx.ShowPrgrs,
302                                 ); err != nil {
303                                         log.Fatalln("Error during copying:", err)
304                                 }
305                                 if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) == pktName {
306                                         ctx.LogI("nncp-bundle", les, "removed")
307                                         if !*dryRun {
308                                                 os.Remove(dstPath)
309                                                 if ctx.HdrUsage {
310                                                         os.Remove(dstPath + nncp.HdrSuffix)
311                                                 }
312                                         }
313                                 } else {
314                                         ctx.LogE("nncp-bundle", les, errors.New("bad checksum"), "")
315                                 }
316                                 continue
317                         }
318                         if *pktEnc.Recipient != *ctx.SelfId {
319                                 ctx.LogD("nncp-bundle", les, "Unknown recipient")
320                                 continue
321                         }
322                         if len(nodeIds) > 0 {
323                                 if _, exists := nodeIds[*pktEnc.Sender]; !exists {
324                                         ctx.LogD("nncp-bundle", les, "Sender is not requested")
325                                         continue
326                                 }
327                         }
328                         sender := nncp.Base32Codec.EncodeToString(pktEnc.Sender[:])
329                         les = nncp.LEs{
330                                 {K: "XX", V: string(nncp.TRx)},
331                                 {K: "Node", V: sender},
332                                 {K: "Pkt", V: pktName},
333                                 {K: "FullSize", V: entry.Size},
334                         }
335                         dstDirPath := filepath.Join(ctx.Spool, sender, string(nncp.TRx))
336                         dstPath := filepath.Join(dstDirPath, pktName)
337                         if _, err = os.Stat(dstPath); err == nil || !os.IsNotExist(err) {
338                                 ctx.LogD("nncp-bundle", les, "Packet already exists")
339                                 continue
340                         }
341                         if _, err = os.Stat(dstPath + nncp.SeenSuffix); err == nil || !os.IsNotExist(err) {
342                                 ctx.LogD("nncp-bundle", les, "Packet already exists")
343                                 continue
344                         }
345                         if *doCheck {
346                                 if *dryRun {
347                                         hsh, err := blake2b.New256(nil)
348                                         if err != nil {
349                                                 log.Fatalln("Error during hasher creation:", err)
350                                         }
351                                         if _, err = hsh.Write(pktEncBuf); err != nil {
352                                                 log.Fatalln("Error during writing:", err)
353                                         }
354                                         if _, err = nncp.CopyProgressed(hsh, tarR, "check", les, ctx.ShowPrgrs); err != nil {
355                                                 log.Fatalln("Error during copying:", err)
356                                         }
357                                         if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) != pktName {
358                                                 ctx.LogE("nncp-bundle", les, errors.New("bad checksum"), "")
359                                                 continue
360                                         }
361                                 } else {
362                                         tmp, err := ctx.NewTmpFileWHash()
363                                         if err != nil {
364                                                 log.Fatalln("Error during temporary file creation:", err)
365                                         }
366                                         if _, err = tmp.W.Write(pktEncBuf); err != nil {
367                                                 log.Fatalln("Error during writing:", err)
368                                         }
369                                         if _, err = nncp.CopyProgressed(tmp.W, tarR, "check", les, ctx.ShowPrgrs); err != nil {
370                                                 log.Fatalln("Error during copying:", err)
371                                         }
372                                         if err = tmp.W.Flush(); err != nil {
373                                                 log.Fatalln("Error during flusing:", err)
374                                         }
375                                         if nncp.Base32Codec.EncodeToString(tmp.Hsh.Sum(nil)) == pktName {
376                                                 if err = tmp.Commit(dstDirPath); err != nil {
377                                                         log.Fatalln("Error during commiting:", err)
378                                                 }
379                                         } else {
380                                                 ctx.LogE("nncp-bundle", les, errors.New("bad checksum"), "")
381                                                 tmp.Cancel()
382                                                 continue
383                                         }
384                                 }
385                         } else {
386                                 if *dryRun {
387                                         if _, err = nncp.CopyProgressed(ioutil.Discard, tarR, "Rx", les, ctx.ShowPrgrs); err != nil {
388                                                 log.Fatalln("Error during copying:", err)
389                                         }
390                                 } else {
391                                         tmp, err := ctx.NewTmpFile()
392                                         if err != nil {
393                                                 log.Fatalln("Error during temporary file creation:", err)
394                                         }
395                                         bufTmp := bufio.NewWriterSize(tmp, CopyBufSize)
396                                         if _, err = bufTmp.Write(pktEncBuf); err != nil {
397                                                 log.Fatalln("Error during writing:", err)
398                                         }
399                                         if _, err = nncp.CopyProgressed(bufTmp, tarR, "Rx", les, ctx.ShowPrgrs); err != nil {
400                                                 log.Fatalln("Error during copying:", err)
401                                         }
402                                         if err = bufTmp.Flush(); err != nil {
403                                                 log.Fatalln("Error during flushing:", err)
404                                         }
405                                         if err = tmp.Sync(); err != nil {
406                                                 log.Fatalln("Error during syncing:", err)
407                                         }
408                                         if err = tmp.Close(); err != nil {
409                                                 log.Fatalln("Error during closing:", err)
410                                         }
411                                         if err = os.MkdirAll(dstDirPath, os.FileMode(0777)); err != nil {
412                                                 log.Fatalln("Error during mkdir:", err)
413                                         }
414                                         if err = os.Rename(tmp.Name(), dstPath); err != nil {
415                                                 log.Fatalln("Error during renaming:", err)
416                                         }
417                                         if err = nncp.DirSync(dstDirPath); err != nil {
418                                                 log.Fatalln("Error during syncing:", err)
419                                         }
420                                         if ctx.HdrUsage {
421                                                 ctx.HdrWrite(pktEncBuf, dstPath)
422                                         }
423                                 }
424                         }
425                         for _, le := range les {
426                                 if le.K == "FullSize" {
427                                         les = append(les, nncp.LE{K: "Size", V: le.V})
428                                         break
429                                 }
430                         }
431                         ctx.LogI("nncp-bundle", les, "")
432                 }
433         }
434 }