]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-bundle/main.go
Search for bundles in badly formatted streams
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-bundle / main.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 // Create/digest stream of NNCP encrypted packets
20 package main
21
22 import (
23         "archive/tar"
24         "bufio"
25         "bytes"
26         "flag"
27         "fmt"
28         "io"
29         "io/ioutil"
30         "log"
31         "os"
32         "path/filepath"
33         "strconv"
34         "strings"
35
36         "cypherpunks.ru/nncp"
37         "github.com/davecgh/go-xdr/xdr2"
38         "golang.org/x/crypto/blake2b"
39 )
40
41 const (
42         CopyBufSize = 1 << 17
43 )
44
45 func usage() {
46         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
47         fmt.Fprintln(os.Stderr, "nncp-bundle -- Create/digest stream of NNCP encrypted packets\n")
48         fmt.Fprintf(os.Stderr, "Usage: %s [options] -tx [-delete] NODE [NODE ...] > ...\n", os.Args[0])
49         fmt.Fprintf(os.Stderr, "       %s [options] -rx -delete [NODE ...] < ...\n", os.Args[0])
50         fmt.Fprintf(os.Stderr, "       %s [options] -rx [-check] [NODE ...] < ...\n", os.Args[0])
51         fmt.Fprintln(os.Stderr, "Options:")
52         flag.PrintDefaults()
53 }
54
55 func main() {
56         var (
57                 cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
58                 niceRaw  = flag.Int("nice", 255, "Minimal required niceness")
59                 doRx     = flag.Bool("rx", false, "Receive packets")
60                 doTx     = flag.Bool("tx", false, "Transfer packets")
61                 doDelete = flag.Bool("delete", false, "Delete transferred packets")
62                 doCheck  = flag.Bool("check", false, "Check integrity while receiving")
63                 quiet    = flag.Bool("quiet", false, "Print only errors")
64                 debug    = flag.Bool("debug", false, "Print debug messages")
65                 version  = flag.Bool("version", false, "Print version information")
66                 warranty = flag.Bool("warranty", false, "Print warranty information")
67         )
68         flag.Usage = usage
69         flag.Parse()
70         if *warranty {
71                 fmt.Println(nncp.Warranty)
72                 return
73         }
74         if *version {
75                 fmt.Println(nncp.VersionGet())
76                 return
77         }
78         if *niceRaw < 1 || *niceRaw > 255 {
79                 log.Fatalln("-nice must be between 1 and 255")
80         }
81         nice := uint8(*niceRaw)
82         if *doRx && *doTx {
83                 log.Fatalln("-rx and -tx can not be set simultaneously")
84         }
85         if !*doRx && !*doTx {
86                 log.Fatalln("At least one of -rx and -tx must be specified")
87         }
88
89         cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
90         if err != nil {
91                 log.Fatalln("Can not read config:", err)
92         }
93         ctx, err := nncp.CfgParse(cfgRaw)
94         if err != nil {
95                 log.Fatalln("Can not parse config:", err)
96         }
97         ctx.Quiet = *quiet
98         ctx.Debug = *debug
99
100         nodeIds := make(map[nncp.NodeId]struct{}, flag.NArg())
101         for i := 0; i < flag.NArg(); i++ {
102                 node, err := ctx.FindNode(flag.Arg(i))
103                 if err != nil {
104                         log.Fatalln("Invalid specified:", err)
105                 }
106                 nodeIds[*node.Id] = struct{}{}
107         }
108
109         sds := nncp.SDS{}
110         if *doTx {
111                 sds["xx"] = string(nncp.TTx)
112                 var pktName string
113                 bufStdout := bufio.NewWriter(os.Stdout)
114                 tarWr := tar.NewWriter(bufStdout)
115                 for nodeId, _ := range nodeIds {
116                         sds["node"] = nodeId.String()
117                         for job := range ctx.Jobs(&nodeId, nncp.TTx) {
118                                 pktName = filepath.Base(job.Fd.Name())
119                                 sds["pkt"] = pktName
120                                 if job.PktEnc.Nice > nice {
121                                         ctx.LogD("nncp-bundle", sds, "too nice")
122                                         job.Fd.Close()
123                                         continue
124                                 }
125                                 if err = tarWr.WriteHeader(&tar.Header{
126                                         Name: strings.Join([]string{
127                                                 nncp.NNCPBundlePrefix,
128                                                 nodeId.String(),
129                                                 ctx.SelfId.String(),
130                                                 pktName,
131                                         }, "/"),
132                                         Mode:     0440,
133                                         Size:     job.Size,
134                                         Typeflag: tar.TypeReg,
135                                 }); err != nil {
136                                         log.Fatalln("Error writing tar header:", err)
137                                 }
138                                 if _, err = io.Copy(tarWr, job.Fd); err != nil {
139                                         log.Fatalln("Error during copying to tar:", err)
140                                 }
141                                 job.Fd.Close()
142                                 if err = tarWr.Flush(); err != nil {
143                                         log.Fatalln("Error during tar flushing:", err)
144                                 }
145                                 if err = bufStdout.Flush(); err != nil {
146                                         log.Fatalln("Error during stdout flushing:", err)
147                                 }
148                                 if *doDelete {
149                                         if err = os.Remove(job.Fd.Name()); err != nil {
150                                                 log.Fatalln("Error during deletion:", err)
151                                         }
152                                 }
153                                 ctx.LogI("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{
154                                         "size": strconv.FormatInt(job.Size, 10),
155                                 }), "")
156                         }
157                 }
158                 if err = tarWr.Close(); err != nil {
159                         log.Fatalln("Error during tar closing:", err)
160                 }
161         } else {
162                 bufStdin := bufio.NewReaderSize(os.Stdin, CopyBufSize*2)
163                 var peeked []byte
164                 var prefixIdx int
165                 var tarR *tar.Reader
166                 var entry *tar.Header
167                 var exists bool
168                 pktEncBuf := make([]byte, nncp.PktEncOverhead)
169                 var pktEnc *nncp.PktEnc
170                 var pktName string
171                 var selfPath string
172                 var dstPath string
173                 for {
174                         peeked, err = bufStdin.Peek(CopyBufSize)
175                         if err != nil && err != io.EOF {
176                                 log.Fatalln("Error during reading:", err)
177                         }
178                         prefixIdx = bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix))
179                         if prefixIdx == -1 {
180                                 if err == io.EOF {
181                                         break
182                                 }
183                                 bufStdin.Discard(bufStdin.Buffered() - (len(nncp.NNCPBundlePrefix) - 1))
184                                 continue
185                         }
186                         bufStdin.Discard(prefixIdx)
187                         tarR = tar.NewReader(bufStdin)
188                         sds["xx"] = string(nncp.TRx)
189                         entry, err = tarR.Next()
190                         if err != nil {
191                                 if err != io.EOF {
192                                         ctx.LogD(
193                                                 "nncp-bundle",
194                                                 nncp.SdsAdd(sds, nncp.SDS{"err": err}),
195                                                 "error reading tar",
196                                         )
197                                 }
198                                 continue
199                         }
200                         sds["pkt"] = entry.Name
201                         if entry.Size < nncp.PktEncOverhead {
202                                 ctx.LogD("nncp-bundle", sds, "Too small packet")
203                                 continue
204                         }
205                         pktName = filepath.Base(entry.Name)
206                         if _, err = nncp.FromBase32(pktName); err != nil {
207                                 ctx.LogD("nncp-bundle", sds, "Bad packet name")
208                                 continue
209                         }
210                         if _, err = io.ReadFull(tarR, pktEncBuf); err != nil {
211                                 ctx.LogD("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
212                                 continue
213                         }
214                         if _, err = xdr.Unmarshal(bytes.NewReader(pktEncBuf), &pktEnc); err != nil {
215                                 ctx.LogD("nncp-bundle", sds, "Bad packet structure")
216                                 continue
217                         }
218                         if pktEnc.Magic != nncp.MagicNNCPEv2 {
219                                 ctx.LogD("nncp-bundle", sds, "Bad packet magic number")
220                                 continue
221                         }
222                         if pktEnc.Nice > nice {
223                                 ctx.LogD("nncp-bundle", sds, "too nice")
224                                 continue
225                         }
226                         if *pktEnc.Sender == *ctx.SelfId && *doDelete {
227                                 if len(nodeIds) > 0 {
228                                         if _, exists = nodeIds[*pktEnc.Recipient]; !exists {
229                                                 ctx.LogD("nncp-bundle", sds, "Recipient is not requested")
230                                                 continue
231                                         }
232                                 }
233                                 nodeId32 := nncp.ToBase32(pktEnc.Recipient[:])
234                                 sds["xx"] = string(nncp.TTx)
235                                 sds["node"] = nodeId32
236                                 sds["pkt"] = pktName
237                                 dstPath = filepath.Join(
238                                         ctx.Spool,
239                                         nodeId32,
240                                         string(nncp.TTx),
241                                         pktName,
242                                 )
243                                 if _, err = os.Stat(dstPath); err != nil {
244                                         ctx.LogD("nncp-bundle", sds, "Packet is already missing")
245                                         continue
246                                 }
247                                 hsh, err := blake2b.New256(nil)
248                                 if err != nil {
249                                         log.Fatalln("Error during hasher creation:", err)
250                                 }
251                                 if _, err = hsh.Write(pktEncBuf); err != nil {
252                                         log.Fatalln("Error during writing:", err)
253                                 }
254                                 if _, err = io.Copy(hsh, tarR); err != nil {
255                                         log.Fatalln("Error during copying:", err)
256                                 }
257                                 if nncp.ToBase32(hsh.Sum(nil)) == pktName {
258                                         ctx.LogI("nncp-bundle", sds, "removed")
259                                         os.Remove(dstPath)
260                                 } else {
261                                         ctx.LogE("nncp-bundle", sds, "bad checksum")
262                                 }
263                                 continue
264                         }
265                         if *pktEnc.Recipient != *ctx.SelfId {
266                                 ctx.LogD("nncp-bundle", sds, "Unknown recipient")
267                                 continue
268                         }
269                         if len(nodeIds) > 0 {
270                                 if _, exists = nodeIds[*pktEnc.Sender]; !exists {
271                                         ctx.LogD("nncp-bundle", sds, "Sender is not requested")
272                                         continue
273                                 }
274                         }
275                         sds["node"] = nncp.ToBase32(pktEnc.Recipient[:])
276                         sds["pkt"] = pktName
277                         selfPath = filepath.Join(ctx.Spool, ctx.SelfId.String(), string(nncp.TRx))
278                         dstPath = filepath.Join(selfPath, pktName)
279                         if _, err = os.Stat(dstPath); err == nil || !os.IsNotExist(err) {
280                                 ctx.LogD("nncp-bundle", sds, "Packet already exists")
281                                 continue
282                         }
283                         if _, err = os.Stat(dstPath + nncp.SeenPostfix); err == nil || !os.IsNotExist(err) {
284                                 ctx.LogD("nncp-bundle", sds, "Packet already exists")
285                                 continue
286                         }
287                         if *doCheck {
288                                 tmp, err := ctx.NewTmpFileWHash()
289                                 if err != nil {
290                                         log.Fatalln("Error during temporary file creation:", err)
291                                 }
292                                 if _, err = tmp.W.Write(pktEncBuf); err != nil {
293                                         log.Fatalln("Error during writing:", err)
294                                 }
295                                 if _, err = io.Copy(tmp.W, tarR); err != nil {
296                                         log.Fatalln("Error during copying:", err)
297                                 }
298                                 if err = tmp.W.Flush(); err != nil {
299                                         log.Fatalln("Error during flusing:", err)
300                                 }
301                                 if nncp.ToBase32(tmp.Hsh.Sum(nil)) == pktName {
302                                         if err = tmp.Commit(selfPath); err != nil {
303                                                 log.Fatalln("Error during commiting:", err)
304                                         }
305                                 } else {
306                                         ctx.LogE("nncp-bundle", sds, "bad checksum")
307                                         tmp.Cancel()
308                                         continue
309                                 }
310                         } else {
311                                 tmp, err := ctx.NewTmpFile()
312                                 if err != nil {
313                                         log.Fatalln("Error during temporary file creation:", err)
314                                 }
315                                 bufTmp := bufio.NewWriterSize(tmp, CopyBufSize)
316                                 if _, err = bufTmp.Write(pktEncBuf); err != nil {
317                                         log.Fatalln("Error during writing:", err)
318                                 }
319                                 if _, err = io.Copy(bufTmp, tarR); err != nil {
320                                         log.Fatalln("Error during copying:", err)
321                                 }
322                                 if err = bufTmp.Flush(); err != nil {
323                                         log.Fatalln("Error during flushing:", err)
324                                 }
325                                 tmp.Sync()
326                                 tmp.Close()
327                                 if err = os.MkdirAll(selfPath, os.FileMode(0700)); err != nil {
328                                         log.Fatalln("Error during mkdir:", err)
329                                 }
330                                 if err = os.Rename(tmp.Name(), dstPath); err != nil {
331                                         log.Fatalln("Error during renaming:", err)
332                                 }
333                         }
334                         ctx.LogI("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{
335                                 "size": strconv.FormatInt(entry.Size, 10),
336                         }), "")
337                 }
338         }
339 }