2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2018 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, either version 3 of the License, or
8 (at your option) any later version.
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.
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/>.
37 "github.com/davecgh/go-xdr/xdr2"
38 "github.com/dustin/go-humanize"
39 "golang.org/x/crypto/blake2b"
46 func newNotification(fromTo *FromToYAML, subject string) io.Reader {
47 return strings.NewReader(fmt.Sprintf(
48 "From: %s\nTo: %s\nSubject: %s\n",
51 mime.BEncoding.Encode("UTF-8", subject),
55 func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun, doSeen, noFile, noFreq, noExec, noTrns bool) bool {
57 for job := range ctx.Jobs(nodeId, TRx) {
58 pktName := filepath.Base(job.Fd.Name())
59 sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
60 if job.PktEnc.Nice > nice {
61 ctx.LogD("rx", SdsAdd(sds, SDS{
62 "nice": strconv.Itoa(int(job.PktEnc.Nice)),
66 pipeR, pipeW := io.Pipe()
67 errs := make(chan error, 1)
69 pipeWB := bufio.NewWriter(pipeW)
70 _, _, err := PktEncRead(
73 bufio.NewReader(job.Fd),
81 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "decryption")
87 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
88 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "unmarshal")
92 pktSize = job.Size - PktEncOverhead - PktOverhead
93 sds["size"] = strconv.FormatInt(pktSize, 10)
94 ctx.LogD("rx", sds, "taken")
100 path := bytes.Split(pkt.Path[:int(pkt.PathLen)], []byte{0})
101 handle := string(path[0])
102 args := make([]string, 0, len(path)-1)
103 for _, p := range path[1:] {
104 args = append(args, string(p))
106 sds := SdsAdd(sds, SDS{
108 "dst": strings.Join(append([]string{handle}, args...), " "),
110 decompressor, err := zlib.NewReader(pipeR)
114 sender := ctx.Neigh[*job.PktEnc.Sender]
115 cmdline, exists := sender.Exec[handle]
116 if !exists || len(cmdline) == 0 {
117 ctx.LogE("rx", SdsAdd(sds, SDS{"err": "No handle found"}), "")
124 append(cmdline[1:len(cmdline)], args...)...,
128 "NNCP_SELF="+ctx.Self.Id.String(),
129 "NNCP_SENDER="+sender.Id.String(),
130 "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
132 cmd.Stdin = decompressor
133 if err = cmd.Run(); err != nil {
134 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "handle")
139 ctx.LogI("rx", sds, "")
142 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
146 if err = os.Remove(job.Fd.Name()); err != nil {
147 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
155 dst := string(pkt.Path[:int(pkt.PathLen)])
156 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
157 if filepath.IsAbs(dst) {
158 ctx.LogE("rx", sds, "non-relative destination path")
162 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
164 ctx.LogE("rx", sds, "incoming is not allowed")
168 dir := filepath.Join(*incoming, path.Dir(dst))
169 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
170 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
175 tmp, err := ioutil.TempFile(dir, "nncp-file")
176 sds["tmp"] = tmp.Name()
177 ctx.LogD("rx", sds, "created")
179 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
183 bufW := bufio.NewWriter(tmp)
184 if _, err = io.Copy(bufW, pipeR); err != nil {
185 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
192 dstPathOrig := filepath.Join(*incoming, dst)
193 dstPath := dstPathOrig
196 if _, err = os.Stat(dstPath); err != nil {
197 if os.IsNotExist(err) {
200 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
204 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
207 if err = os.Rename(tmp.Name(), dstPath); err != nil {
208 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
213 ctx.LogI("rx", sds, "")
216 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
220 if err = os.Remove(job.Fd.Name()); err != nil {
221 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
224 sendmail, exists := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
225 if exists && len(sendmail) > 0 && ctx.NotifyFile != nil {
228 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
230 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
231 "File from %s: %s (%s)",
232 ctx.Neigh[*job.PktEnc.Sender].Name,
234 humanize.IBytes(uint64(pktSize)),
243 src := string(pkt.Path[:int(pkt.PathLen)])
244 if filepath.IsAbs(src) {
245 ctx.LogE("rx", sds, "non-relative source path")
249 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
250 dstRaw, err := ioutil.ReadAll(pipeR)
252 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
256 dst := string(dstRaw)
258 sender := ctx.Neigh[*job.PktEnc.Sender]
261 ctx.LogE("rx", sds, "freqing is not allowed")
266 if sender.FreqChunked == 0 {
270 filepath.Join(*freq, src),
275 err = ctx.TxFileChunked(
278 filepath.Join(*freq, src),
285 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
290 ctx.LogI("rx", sds, "")
293 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
297 if err = os.Remove(job.Fd.Name()); err != nil {
298 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
301 sendmail, exists := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
302 if exists && len(sendmail) > 0 && ctx.NotifyFreq != nil {
305 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
307 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
309 ctx.Neigh[*job.PktEnc.Sender].Name,
319 dst := new([blake2b.Size256]byte)
320 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
321 nodeId := NodeId(*dst)
322 node, known := ctx.Neigh[nodeId]
323 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
325 ctx.LogE("rx", sds, "unknown node")
329 ctx.LogD("rx", sds, "taken")
331 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
332 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
337 ctx.LogI("rx", sds, "")
340 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
344 if err = os.Remove(job.Fd.Name()); err != nil {
345 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
350 ctx.LogE("rx", sds, "unknown type")