]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/toss.go
2fb4cbb034b47b1c549f92bfda036e6433182a8c
[nncp.git] / src / cypherpunks.ru / nncp / toss.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         "bufio"
23         "compress/zlib"
24         "fmt"
25         "io"
26         "io/ioutil"
27         "log"
28         "mime"
29         "os"
30         "os/exec"
31         "path"
32         "path/filepath"
33         "strconv"
34         "strings"
35
36         "github.com/davecgh/go-xdr/xdr2"
37         "github.com/dustin/go-humanize"
38         "golang.org/x/crypto/blake2b"
39 )
40
41 func newNotification(fromTo *FromToYAML, subject string) io.Reader {
42         return strings.NewReader(fmt.Sprintf(
43                 "From: %s\nTo: %s\nSubject: %s\n",
44                 fromTo.From,
45                 fromTo.To,
46                 mime.BEncoding.Encode("UTF-8", subject),
47         ))
48 }
49
50 func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) bool {
51         isBad := false
52         for job := range ctx.Jobs(nodeId, TRx) {
53                 pktName := filepath.Base(job.Fd.Name())
54                 sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
55                 if job.PktEnc.Nice > nice {
56                         ctx.LogD("rx", SdsAdd(sds, SDS{
57                                 "nice": strconv.Itoa(int(job.PktEnc.Nice)),
58                         }), "too nice")
59                         continue
60                 }
61                 pipeR, pipeW := io.Pipe()
62                 errs := make(chan error, 1)
63                 go func(job Job) {
64                         pipeWB := bufio.NewWriter(pipeW)
65                         _, _, err := PktEncRead(
66                                 ctx.Self,
67                                 ctx.Neigh,
68                                 bufio.NewReader(job.Fd),
69                                 pipeWB,
70                         )
71                         errs <- err
72                         pipeWB.Flush()
73                         pipeW.Close()
74                         job.Fd.Close()
75                         if err != nil {
76                                 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "decryption")
77                         }
78                 }(job)
79                 var pkt Pkt
80                 var err error
81                 var pktSize int64
82                 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
83                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "unmarshal")
84                         isBad = true
85                         goto Closing
86                 }
87                 pktSize = job.Size - PktEncOverhead - PktOverhead
88                 sds["size"] = strconv.FormatInt(pktSize, 10)
89                 ctx.LogD("rx", sds, "taken")
90                 switch pkt.Type {
91                 case PktTypeMail:
92                         recipients := string(pkt.Path[:int(pkt.PathLen)])
93                         sds := SdsAdd(sds, SDS{
94                                 "type": "mail",
95                                 "dst":  recipients,
96                         })
97                         decompressor, err := zlib.NewReader(pipeR)
98                         if err != nil {
99                                 log.Fatalln(err)
100                         }
101                         sendmail := ctx.Neigh[*job.PktEnc.Sender].Sendmail
102                         if len(sendmail) == 0 {
103                                 ctx.LogE("rx", SdsAdd(sds, SDS{"err": "No sendmail configured"}), "")
104                                 isBad = true
105                                 goto Closing
106                         }
107                         if !dryRun {
108                                 cmd := exec.Command(
109                                         sendmail[0],
110                                         append(
111                                                 sendmail[1:len(sendmail)],
112                                                 strings.Split(recipients, " ")...,
113                                         )...,
114                                 )
115                                 cmd.Stdin = decompressor
116                                 if err = cmd.Run(); err != nil {
117                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
118                                         isBad = true
119                                         goto Closing
120                                 }
121                         }
122                         ctx.LogI("rx", sds, "")
123                         if !dryRun {
124                                 if err = os.Remove(job.Fd.Name()); err != nil {
125                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
126                                         isBad = true
127                                 }
128                         }
129                 case PktTypeFile:
130                         dst := string(pkt.Path[:int(pkt.PathLen)])
131                         sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
132                         if filepath.IsAbs(dst) {
133                                 ctx.LogE("rx", sds, "non-relative destination path")
134                                 isBad = true
135                                 goto Closing
136                         }
137                         incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
138                         if incoming == nil {
139                                 ctx.LogE("rx", sds, "incoming is not allowed")
140                                 isBad = true
141                                 goto Closing
142                         }
143                         dir := filepath.Join(*incoming, path.Dir(dst))
144                         if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
145                                 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
146                                 isBad = true
147                                 goto Closing
148                         }
149                         if !dryRun {
150                                 tmp, err := ioutil.TempFile(dir, "nncp-file")
151                                 sds["tmp"] = tmp.Name()
152                                 ctx.LogD("rx", sds, "created")
153                                 if err != nil {
154                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
155                                         isBad = true
156                                         goto Closing
157                                 }
158                                 bufW := bufio.NewWriter(tmp)
159                                 if _, err = io.Copy(bufW, pipeR); err != nil {
160                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
161                                         isBad = true
162                                         goto Closing
163                                 }
164                                 bufW.Flush()
165                                 tmp.Sync()
166                                 tmp.Close()
167                                 dstPathOrig := filepath.Join(*incoming, dst)
168                                 dstPath := dstPathOrig
169                                 dstPathCtr := 0
170                                 for {
171                                         if _, err = os.Stat(dstPath); err != nil {
172                                                 if os.IsNotExist(err) {
173                                                         break
174                                                 }
175                                                 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
176                                                 isBad = true
177                                                 goto Closing
178                                         }
179                                         dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
180                                         dstPathCtr++
181                                 }
182                                 if err = os.Rename(tmp.Name(), dstPath); err != nil {
183                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
184                                         isBad = true
185                                 }
186                                 delete(sds, "tmp")
187                         }
188                         ctx.LogI("rx", sds, "")
189                         if !dryRun {
190                                 if err = os.Remove(job.Fd.Name()); err != nil {
191                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
192                                         isBad = true
193                                 }
194                                 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
195                                 if ctx.NotifyFile != nil {
196                                         cmd := exec.Command(
197                                                 sendmail[0],
198                                                 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
199                                         )
200                                         cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
201                                                 "File from %s: %s (%s)",
202                                                 ctx.Neigh[*job.PktEnc.Sender].Name,
203                                                 dst,
204                                                 humanize.IBytes(uint64(pktSize)),
205                                         ))
206                                         cmd.Run()
207                                 }
208                         }
209                 case PktTypeFreq:
210                         src := string(pkt.Path[:int(pkt.PathLen)])
211                         if filepath.IsAbs(src) {
212                                 ctx.LogE("rx", sds, "non-relative source path")
213                                 isBad = true
214                                 goto Closing
215                         }
216                         sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
217                         dstRaw, err := ioutil.ReadAll(pipeR)
218                         if err != nil {
219                                 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
220                                 isBad = true
221                                 goto Closing
222                         }
223                         dst := string(dstRaw)
224                         sds["dst"] = dst
225                         sender := ctx.Neigh[*job.PktEnc.Sender]
226                         freq := sender.Freq
227                         if freq == nil {
228                                 ctx.LogE("rx", sds, "freqing is not allowed")
229                                 isBad = true
230                                 goto Closing
231                         }
232                         if !dryRun {
233                                 if sender.FreqChunked == 0 {
234                                         err = ctx.TxFile(
235                                                 sender,
236                                                 job.PktEnc.Nice,
237                                                 filepath.Join(*freq, src),
238                                                 dst,
239                                                 sender.FreqMinSize,
240                                         )
241                                 } else {
242                                         err = ctx.TxFileChunked(
243                                                 sender,
244                                                 job.PktEnc.Nice,
245                                                 filepath.Join(*freq, src),
246                                                 dst,
247                                                 sender.FreqMinSize,
248                                                 sender.FreqChunked,
249                                         )
250                                 }
251                                 if err != nil {
252                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
253                                         isBad = true
254                                         goto Closing
255                                 }
256                         }
257                         ctx.LogI("rx", sds, "")
258                         if !dryRun {
259                                 if err = os.Remove(job.Fd.Name()); err != nil {
260                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
261                                         isBad = true
262                                 }
263                                 if ctx.NotifyFreq != nil {
264                                         sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
265                                         cmd := exec.Command(
266                                                 sendmail[0],
267                                                 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
268                                         )
269                                         cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
270                                                 "Freq from %s: %s",
271                                                 ctx.Neigh[*job.PktEnc.Sender].Name,
272                                                 src,
273                                         ))
274                                         cmd.Run()
275                                 }
276                         }
277                 case PktTypeTrns:
278                         dst := new([blake2b.Size256]byte)
279                         copy(dst[:], pkt.Path[:int(pkt.PathLen)])
280                         nodeId := NodeId(*dst)
281                         node, known := ctx.Neigh[nodeId]
282                         sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
283                         if !known {
284                                 ctx.LogE("rx", sds, "unknown node")
285                                 isBad = true
286                                 goto Closing
287                         }
288                         ctx.LogD("rx", sds, "taken")
289                         if !dryRun {
290                                 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
291                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
292                                         isBad = true
293                                         goto Closing
294                                 }
295                         }
296                         ctx.LogI("rx", sds, "")
297                         if !dryRun {
298                                 if err = os.Remove(job.Fd.Name()); err != nil {
299                                         ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
300                                         isBad = true
301                                 }
302                         }
303                 default:
304                         ctx.LogE("rx", sds, "unknown type")
305                         isBad = true
306                 }
307         Closing:
308                 pipeR.Close()
309         }
310         return isBad
311 }