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 return pipeW.CloseWithError(err)
105 if err = pipeWB.Flush(); err != nil {
106 return pipeW.CloseWithError(err)
113 var pktSizeBlocks int64
114 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
115 ctx.LogE("rx", sds, err, "unmarshal")
119 pktSize = job.Size - PktEncOverhead - PktOverhead - PktSizeOverhead
120 pktSizeBlocks = pktSize / (EncBlkSize + poly1305.TagSize)
121 if pktSize%(EncBlkSize+poly1305.TagSize) != 0 {
122 pktSize -= poly1305.TagSize
124 pktSize -= pktSizeBlocks * poly1305.TagSize
125 sds["size"] = pktSize
126 ctx.LogD("rx", sds, "taken")
128 case PktTypeExec, PktTypeExecFat:
132 path := bytes.Split(pkt.Path[:int(pkt.PathLen)], []byte{0})
133 handle := string(path[0])
134 args := make([]string, 0, len(path)-1)
135 for _, p := range path[1:] {
136 args = append(args, string(p))
138 argsStr := strings.Join(append([]string{handle}, args...), " ")
139 sds := SdsAdd(sds, SDS{
143 sender := ctx.Neigh[*job.PktEnc.Sender]
144 cmdline, exists := sender.Exec[handle]
145 if !exists || len(cmdline) == 0 {
146 ctx.LogE("rx", sds, errors.New("No handle found"), "")
150 if pkt.Type == PktTypeExec {
151 if err = decompressor.Reset(pipeR); err != nil {
158 append(cmdline[1:], args...)...,
162 "NNCP_SELF="+ctx.Self.Id.String(),
163 "NNCP_SENDER="+sender.Id.String(),
164 "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
166 if pkt.Type == PktTypeExec {
167 cmd.Stdin = decompressor
171 output, err := cmd.Output()
173 ctx.LogE("rx", sds, err, "handle")
177 if len(sendmail) > 0 && ctx.NotifyExec != nil {
178 notify, exists := ctx.NotifyExec[sender.Name+"."+handle]
180 notify, exists = ctx.NotifyExec["*."+handle]
185 append(sendmail[1:], notify.To)...,
187 cmd.Stdin = newNotification(notify, fmt.Sprintf(
188 "Exec from %s: %s", sender.Name, argsStr,
190 if err = cmd.Run(); err != nil {
191 ctx.LogE("rx", sds, err, "notify")
196 ctx.LogI("rx", sds, "")
199 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
200 fd.Close() // #nosec G104
203 if err = os.Remove(job.Fd.Name()); err != nil {
204 ctx.LogE("rx", sds, err, "remove")
212 dst := string(pkt.Path[:int(pkt.PathLen)])
213 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
214 if filepath.IsAbs(dst) {
215 ctx.LogE("rx", sds, errors.New("non-relative destination path"), "")
219 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
221 ctx.LogE("rx", sds, errors.New("incoming is not allowed"), "")
225 dir := filepath.Join(*incoming, path.Dir(dst))
226 if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
227 ctx.LogE("rx", sds, err, "mkdir")
232 tmp, err := TempFile(dir, "file")
234 ctx.LogE("rx", sds, err, "mktemp")
238 sds["tmp"] = tmp.Name()
239 ctx.LogD("rx", sds, "created")
240 bufW := bufio.NewWriter(tmp)
241 if _, err = CopyProgressed(
242 bufW, pipeR, "Rx file",
243 SdsAdd(sds, SDS{"fullsize": sds["size"]}),
246 ctx.LogE("rx", sds, err, "copy")
250 if err = bufW.Flush(); err != nil {
251 tmp.Close() // #nosec G104
252 ctx.LogE("rx", sds, err, "copy")
256 if err = tmp.Sync(); err != nil {
257 tmp.Close() // #nosec G104
258 ctx.LogE("rx", sds, err, "copy")
262 if err = tmp.Close(); err != nil {
263 ctx.LogE("rx", sds, err, "copy")
267 dstPathOrig := filepath.Join(*incoming, dst)
268 dstPath := dstPathOrig
271 if _, err = os.Stat(dstPath); err != nil {
272 if os.IsNotExist(err) {
275 ctx.LogE("rx", sds, err, "stat")
279 dstPath = dstPathOrig + "." + strconv.Itoa(dstPathCtr)
282 if err = os.Rename(tmp.Name(), dstPath); err != nil {
283 ctx.LogE("rx", sds, err, "rename")
286 if err = DirSync(*incoming); err != nil {
287 ctx.LogE("rx", sds, err, "sync")
292 ctx.LogI("rx", sds, "")
295 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
296 fd.Close() // #nosec G104
299 if err = os.Remove(job.Fd.Name()); err != nil {
300 ctx.LogE("rx", sds, err, "remove")
303 if len(sendmail) > 0 && ctx.NotifyFile != nil {
306 append(sendmail[1:], ctx.NotifyFile.To)...,
308 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
309 "File from %s: %s (%s)",
310 ctx.Neigh[*job.PktEnc.Sender].Name,
312 humanize.IBytes(uint64(pktSize)),
314 if err = cmd.Run(); err != nil {
315 ctx.LogE("rx", sds, err, "notify")
323 src := string(pkt.Path[:int(pkt.PathLen)])
324 if filepath.IsAbs(src) {
325 ctx.LogE("rx", sds, errors.New("non-relative source path"), "")
329 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
330 dstRaw, err := ioutil.ReadAll(pipeR)
332 ctx.LogE("rx", sds, err, "read")
336 dst := string(dstRaw)
338 sender := ctx.Neigh[*job.PktEnc.Sender]
339 freqPath := sender.FreqPath
341 ctx.LogE("rx", sds, errors.New("freqing is not allowed"), "")
349 filepath.Join(*freqPath, src),
356 ctx.LogE("rx", sds, err, "tx file")
361 ctx.LogI("rx", sds, "")
364 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
365 fd.Close() // #nosec G104
368 if err = os.Remove(job.Fd.Name()); err != nil {
369 ctx.LogE("rx", sds, err, "remove")
372 if len(sendmail) > 0 && ctx.NotifyFreq != nil {
375 append(sendmail[1:], ctx.NotifyFreq.To)...,
377 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
378 "Freq from %s: %s", sender.Name, src,
380 if err = cmd.Run(); err != nil {
381 ctx.LogE("rx", sds, err, "notify")
389 dst := new([blake2b.Size256]byte)
390 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
391 nodeId := NodeId(*dst)
392 node, known := ctx.Neigh[nodeId]
393 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
395 ctx.LogE("rx", sds, errors.New("unknown node"), "")
399 ctx.LogD("rx", sds, "taken")
401 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
402 ctx.LogE("rx", sds, err, "tx trns")
407 ctx.LogI("rx", sds, "")
410 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
411 fd.Close() // #nosec G104
414 if err = os.Remove(job.Fd.Name()); err != nil {
415 ctx.LogE("rx", sds, err, "remove")
420 ctx.LogE("rx", sds, errors.New("unknown type"), "")
424 pipeR.Close() // #nosec G104
429 func (ctx *Ctx) AutoToss(
432 doSeen, noFile, noFreq, noExec, noTrns bool,
433 ) (chan struct{}, chan bool) {
434 finish := make(chan struct{})
435 badCode := make(chan bool)
445 time.Sleep(time.Second)
446 bad = !ctx.Toss(nodeId, nice, false, doSeen, noFile, noFreq, noExec, noTrns)
449 return finish, badCode