/*
NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
"path/filepath"
"strconv"
"strings"
+ "time"
xdr "github.com/davecgh/go-xdr/xdr2"
"github.com/dustin/go-humanize"
continue
}
pipeR, pipeW := io.Pipe()
- errs := make(chan error, 1)
- go func(job Job) {
+ go func(job Job) error {
pipeWB := bufio.NewWriter(pipeW)
_, _, err := PktEncRead(
ctx.Self,
bufio.NewReader(job.Fd),
pipeWB,
)
- errs <- err
- pipeWB.Flush()
- pipeW.Close()
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
if err != nil {
- ctx.LogE("rx", sds, err, "decryption")
+ return pipeW.CloseWithError(err)
}
+ if err = pipeWB.Flush(); err != nil {
+ return pipeW.CloseWithError(err)
+ }
+ return pipeW.Close()
}(job)
var pkt Pkt
var err error
sds["size"] = pktSize
ctx.LogD("rx", sds, "taken")
switch pkt.Type {
- case PktTypeExec:
+ case PktTypeExec, PktTypeExecFat:
if noExec {
goto Closing
}
isBad = true
goto Closing
}
- if err = decompressor.Reset(pipeR); err != nil {
- log.Fatalln(err)
+ if pkt.Type == PktTypeExec {
+ if err = decompressor.Reset(pipeR); err != nil {
+ log.Fatalln(err)
+ }
}
if !dryRun {
cmd := exec.Command(
cmdline[0],
- append(cmdline[1:len(cmdline)], args...)...,
+ append(cmdline[1:], args...)...,
)
cmd.Env = append(
cmd.Env,
"NNCP_SENDER="+sender.Id.String(),
"NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
)
- cmd.Stdin = decompressor
+ if pkt.Type == PktTypeExec {
+ cmd.Stdin = decompressor
+ } else {
+ cmd.Stdin = pipeR
+ }
output, err := cmd.Output()
if err != nil {
ctx.LogE("rx", sds, err, "handle")
if exists {
cmd := exec.Command(
sendmail[0],
- append(sendmail[1:len(sendmail)], notify.To)...,
+ append(sendmail[1:], notify.To)...,
)
cmd.Stdin = newNotification(notify, fmt.Sprintf(
"Exec from %s: %s", sender.Name, argsStr,
), output)
- cmd.Run()
+ if err = cmd.Run(); err != nil {
+ ctx.LogE("rx", sds, err, "notify")
+ }
}
}
}
if !dryRun {
if doSeen {
if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
- fd.Close()
+ fd.Close() // #nosec G104
}
}
if err = os.Remove(job.Fd.Name()); err != nil {
goto Closing
}
if err = bufW.Flush(); err != nil {
- tmp.Close()
+ tmp.Close() // #nosec G104
ctx.LogE("rx", sds, err, "copy")
isBad = true
goto Closing
}
if err = tmp.Sync(); err != nil {
- tmp.Close()
+ tmp.Close() // #nosec G104
+ ctx.LogE("rx", sds, err, "copy")
+ isBad = true
+ goto Closing
+ }
+ if err = tmp.Close(); err != nil {
ctx.LogE("rx", sds, err, "copy")
isBad = true
goto Closing
}
- tmp.Close()
dstPathOrig := filepath.Join(*incoming, dst)
dstPath := dstPathOrig
dstPathCtr := 0
if !dryRun {
if doSeen {
if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
- fd.Close()
+ fd.Close() // #nosec G104
}
}
if err = os.Remove(job.Fd.Name()); err != nil {
if len(sendmail) > 0 && ctx.NotifyFile != nil {
cmd := exec.Command(
sendmail[0],
- append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
+ append(sendmail[1:], ctx.NotifyFile.To)...,
)
cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
"File from %s: %s (%s)",
dst,
humanize.IBytes(uint64(pktSize)),
), nil)
- cmd.Run()
+ if err = cmd.Run(); err != nil {
+ ctx.LogE("rx", sds, err, "notify")
+ }
}
}
case PktTypeFreq:
if !dryRun {
if doSeen {
if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
- fd.Close()
+ fd.Close() // #nosec G104
}
}
if err = os.Remove(job.Fd.Name()); err != nil {
if len(sendmail) > 0 && ctx.NotifyFreq != nil {
cmd := exec.Command(
sendmail[0],
- append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
+ append(sendmail[1:], ctx.NotifyFreq.To)...,
)
cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
"Freq from %s: %s", sender.Name, src,
), nil)
- cmd.Run()
+ if err = cmd.Run(); err != nil {
+ ctx.LogE("rx", sds, err, "notify")
+ }
}
}
case PktTypeTrns:
if !dryRun {
if doSeen {
if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
- fd.Close()
+ fd.Close() // #nosec G104
}
}
if err = os.Remove(job.Fd.Name()); err != nil {
isBad = true
}
Closing:
- pipeR.Close()
+ pipeR.Close() // #nosec G104
}
return isBad
}
+
+func (ctx *Ctx) AutoToss(
+ nodeId *NodeId,
+ nice uint8,
+ doSeen, noFile, noFreq, noExec, noTrns bool,
+) (chan struct{}, chan bool) {
+ finish := make(chan struct{})
+ badCode := make(chan bool)
+ go func() {
+ bad := false
+ for {
+ select {
+ case <-finish:
+ badCode <- bad
+ break
+ default:
+ }
+ time.Sleep(time.Second)
+ bad = !ctx.Toss(nodeId, nice, false, doSeen, noFile, noFreq, noExec, noTrns)
+ }
+ }()
+ return finish, badCode
+}