2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
38 xdr "github.com/davecgh/go-xdr/xdr2"
39 "github.com/dustin/go-humanize"
40 "github.com/klauspost/compress/zstd"
41 "golang.org/x/crypto/blake2b"
42 "golang.org/x/crypto/poly1305"
49 func newNotification(fromTo *FromToJSON, subject string, body []byte) io.Reader {
51 "From: " + fromTo.From,
53 "Subject: " + mime.BEncoding.Encode("UTF-8", subject),
56 lines = append(lines, []string{
58 "Content-Type: text/plain; charset=utf-8",
59 "Content-Transfer-Encoding: base64",
61 base64.StdEncoding.EncodeToString(body),
64 return strings.NewReader(strings.Join(lines, "\n"))
70 dryRun, doSeen, noFile, noFreq, noExec, noTrns bool,
72 dirLock, err := ctx.LockDir(nodeId, "toss")
74 ctx.LogE("rx", SDS{}, err, "lock")
77 defer ctx.UnlockDir(dirLock)
79 sendmail := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
80 decompressor, err := zstd.NewReader(nil)
84 defer decompressor.Close()
85 for job := range ctx.Jobs(nodeId, TRx) {
86 pktName := filepath.Base(job.Fd.Name())
87 sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
88 if job.PktEnc.Nice > nice {
89 ctx.LogD("rx", SdsAdd(sds, SDS{"nice": int(job.PktEnc.Nice)}), "too nice")
92 pipeR, pipeW := io.Pipe()
93 go func(job Job) error {
94 pipeWB := bufio.NewWriter(pipeW)
95 _, _, err := PktEncRead(
98 bufio.NewReader(job.Fd),
101 job.Fd.Close() // #nosec G104
103 ctx.LogE("rx", sds, err, "decryption")
104 return pipeW.CloseWithError(err)
106 if err = pipeWB.Flush(); err != nil {
107 ctx.LogE("rx", sds, err, "decryption flush")
108 return pipeW.CloseWithError(err)
115 var pktSizeBlocks int64
116 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
117 ctx.LogE("rx", sds, err, "unmarshal")
121 pktSize = job.Size - PktEncOverhead - PktOverhead - PktSizeOverhead
122 pktSizeBlocks = pktSize / (EncBlkSize + poly1305.TagSize)
123 if pktSize%(EncBlkSize+poly1305.TagSize) != 0 {
124 pktSize -= poly1305.TagSize
126 pktSize -= pktSizeBlocks * poly1305.TagSize
127 sds["size"] = pktSize
128 ctx.LogD("rx", sds, "taken")
130 case PktTypeExec, PktTypeExecFat:
134 path := bytes.Split(pkt.Path[:int(pkt.PathLen)], []byte{0})
135 handle := string(path[0])
136 args := make([]string, 0, len(path)-1)
137 for _, p := range path[1:] {
138 args = append(args, string(p))
140 argsStr := strings.Join(append([]string{handle}, args...), " ")
141 sds := SdsAdd(sds, SDS{
145 sender := ctx.Neigh[*job.PktEnc.Sender]
146 cmdline, exists := sender.Exec[handle]
147 if !exists || len(cmdline) == 0 {
148 ctx.LogE("rx", sds, errors.New("No handle found"), "")
152 if pkt.Type == PktTypeExec {
153 if err = decompressor.Reset(pipeR); err != nil {
160 append(cmdline[1:len(cmdline)], args...)...,
164 "NNCP_SELF="+ctx.Self.Id.String(),
165 "NNCP_SENDER="+sender.Id.String(),
166 "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
168 if pkt.Type == PktTypeExec {
169 cmd.Stdin = decompressor
173 output, err := cmd.Output()
175 ctx.LogE("rx", sds, err, "handle")
179 if len(sendmail) > 0 && ctx.NotifyExec != nil {
180 notify, exists := ctx.NotifyExec[sender.Name+"."+handle]
182 notify, exists = ctx.NotifyExec["*."+handle]
187 append(sendmail[1:len(sendmail)], notify.To)...,
189 cmd.Stdin = newNotification(notify, fmt.Sprintf(
190 "Exec from %s: %s", sender.Name, argsStr,
192 if err = cmd.Run(); err != nil {
193 ctx.LogE("rx", sds, err, "notify")
198 ctx.LogI("rx", sds, "")
201 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
202 fd.Close() // #nosec G104
205 if err = os.Remove(job.Fd.Name()); err != nil {
206 ctx.LogE("rx", sds, err, "remove")
214 dst := string(pkt.Path[:int(pkt.PathLen)])
215 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
216 if filepath.IsAbs(dst) {
217 ctx.LogE("rx", sds, errors.New("non-relative destination path"), "")
221 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
223 ctx.LogE("rx", sds, errors.New("incoming is not allowed"), "")
227 dir := filepath.Join(*incoming, path.Dir(dst))
228 if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
229 ctx.LogE("rx", sds, err, "mkdir")
234 tmp, err := TempFile(dir, "file")
236 ctx.LogE("rx", sds, err, "mktemp")
240 sds["tmp"] = tmp.Name()
241 ctx.LogD("rx", sds, "created")
242 bufW := bufio.NewWriter(tmp)
243 if _, err = CopyProgressed(
244 bufW, pipeR, "Rx file",
245 SdsAdd(sds, SDS{"fullsize": sds["size"]}),
248 ctx.LogE("rx", sds, err, "copy")
252 if err = bufW.Flush(); err != nil {
253 tmp.Close() // #nosec G104
254 ctx.LogE("rx", sds, err, "copy")
258 if err = tmp.Sync(); err != nil {
259 tmp.Close() // #nosec G104
260 ctx.LogE("rx", sds, err, "copy")
264 if err = tmp.Close(); err != nil {
265 ctx.LogE("rx", sds, err, "copy")
269 dstPathOrig := filepath.Join(*incoming, dst)
270 dstPath := dstPathOrig
273 if _, err = os.Stat(dstPath); err != nil {
274 if os.IsNotExist(err) {
277 ctx.LogE("rx", sds, err, "stat")
281 dstPath = dstPathOrig + "." + strconv.Itoa(dstPathCtr)
284 if err = os.Rename(tmp.Name(), dstPath); err != nil {
285 ctx.LogE("rx", sds, err, "rename")
288 if err = DirSync(*incoming); err != nil {
289 ctx.LogE("rx", sds, err, "sync")
294 ctx.LogI("rx", sds, "")
297 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
298 fd.Close() // #nosec G104
301 if err = os.Remove(job.Fd.Name()); err != nil {
302 ctx.LogE("rx", sds, err, "remove")
305 if len(sendmail) > 0 && ctx.NotifyFile != nil {
308 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
310 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
311 "File from %s: %s (%s)",
312 ctx.Neigh[*job.PktEnc.Sender].Name,
314 humanize.IBytes(uint64(pktSize)),
316 if err = cmd.Run(); err != nil {
317 ctx.LogE("rx", sds, err, "notify")
325 src := string(pkt.Path[:int(pkt.PathLen)])
326 if filepath.IsAbs(src) {
327 ctx.LogE("rx", sds, errors.New("non-relative source path"), "")
331 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
332 dstRaw, err := ioutil.ReadAll(pipeR)
334 ctx.LogE("rx", sds, err, "read")
338 dst := string(dstRaw)
340 sender := ctx.Neigh[*job.PktEnc.Sender]
341 freqPath := sender.FreqPath
343 ctx.LogE("rx", sds, errors.New("freqing is not allowed"), "")
351 filepath.Join(*freqPath, src),
358 ctx.LogE("rx", sds, err, "tx file")
363 ctx.LogI("rx", sds, "")
366 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
367 fd.Close() // #nosec G104
370 if err = os.Remove(job.Fd.Name()); err != nil {
371 ctx.LogE("rx", sds, err, "remove")
374 if len(sendmail) > 0 && ctx.NotifyFreq != nil {
377 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
379 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
380 "Freq from %s: %s", sender.Name, src,
382 if err = cmd.Run(); err != nil {
383 ctx.LogE("rx", sds, err, "notify")
391 dst := new([blake2b.Size256]byte)
392 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
393 nodeId := NodeId(*dst)
394 node, known := ctx.Neigh[nodeId]
395 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
397 ctx.LogE("rx", sds, errors.New("unknown node"), "")
401 ctx.LogD("rx", sds, "taken")
403 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
404 ctx.LogE("rx", sds, err, "tx trns")
409 ctx.LogI("rx", sds, "")
412 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
413 fd.Close() // #nosec G104
416 if err = os.Remove(job.Fd.Name()); err != nil {
417 ctx.LogE("rx", sds, err, "remove")
422 ctx.LogE("rx", sds, errors.New("unknown type"), "")
426 pipeR.Close() // #nosec G104
431 func (ctx *Ctx) AutoToss(
434 doSeen, noFile, noFreq, noExec, noTrns bool,
435 ) (chan struct{}, chan bool) {
436 finish := make(chan struct{})
437 badCode := make(chan bool)
447 time.Sleep(time.Second)
448 bad = !ctx.Toss(nodeId, nice, false, doSeen, noFile, noFreq, noExec, noTrns)
451 return finish, badCode