2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 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/>.
36 "github.com/davecgh/go-xdr/xdr2"
37 "github.com/dustin/go-humanize"
38 "golang.org/x/crypto/blake2b"
41 func newNotification(fromTo *FromToYAML, subject string) io.Reader {
42 return strings.NewReader(fmt.Sprintf(
43 "From: %s\nTo: %s\nSubject: %s\n",
46 mime.BEncoding.Encode("UTF-8", subject),
50 func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) bool {
51 dirLock, err := ctx.LockDir(nodeId, TRx)
55 defer ctx.UnlockDir(dirLock)
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")
97 recipients := string(pkt.Path[:int(pkt.PathLen)])
98 sds := SdsAdd(sds, SDS{
102 decompressor, err := zlib.NewReader(pipeR)
106 sendmail := ctx.Neigh[*job.PktEnc.Sender].Sendmail
107 if len(sendmail) == 0 {
108 ctx.LogE("rx", SdsAdd(sds, SDS{"err": "No sendmail configured"}), "")
116 sendmail[1:len(sendmail)],
117 strings.Split(recipients, " ")...,
120 cmd.Stdin = decompressor
121 if err = cmd.Run(); err != nil {
122 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
127 ctx.LogI("rx", sds, "")
129 if err = os.Remove(job.Fd.Name()); err != nil {
130 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
135 dst := string(pkt.Path[:int(pkt.PathLen)])
136 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
137 if filepath.IsAbs(dst) {
138 ctx.LogE("rx", sds, "non-relative destination path")
142 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
144 ctx.LogE("rx", sds, "incoming is not allowed")
148 dir := filepath.Join(*incoming, path.Dir(dst))
149 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
150 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
155 tmp, err := ioutil.TempFile(dir, "nncp-file")
156 sds["tmp"] = tmp.Name()
157 ctx.LogD("rx", sds, "created")
159 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
163 bufW := bufio.NewWriter(tmp)
164 if _, err = io.Copy(bufW, pipeR); err != nil {
165 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
172 dstPathOrig := filepath.Join(*incoming, dst)
173 dstPath := dstPathOrig
176 if _, err = os.Stat(dstPath); err != nil {
177 if os.IsNotExist(err) {
180 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
184 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
187 if err = os.Rename(tmp.Name(), dstPath); err != nil {
188 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
193 ctx.LogI("rx", sds, "")
195 if err = os.Remove(job.Fd.Name()); err != nil {
196 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
199 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
200 if ctx.NotifyFile != nil {
203 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
205 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
206 "File from %s: %s (%s)",
207 ctx.Neigh[*job.PktEnc.Sender].Name,
209 humanize.IBytes(uint64(pktSize)),
215 src := string(pkt.Path[:int(pkt.PathLen)])
216 if filepath.IsAbs(src) {
217 ctx.LogE("rx", sds, "non-relative source path")
221 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
222 dstRaw, err := ioutil.ReadAll(pipeR)
224 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
228 dst := string(dstRaw)
230 sender := ctx.Neigh[*job.PktEnc.Sender]
233 ctx.LogE("rx", sds, "freqing is not allowed")
238 if sender.FreqChunked == 0 {
242 filepath.Join(*freq, src),
247 err = ctx.TxFileChunked(
250 filepath.Join(*freq, src),
257 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
262 ctx.LogI("rx", sds, "")
264 if err = os.Remove(job.Fd.Name()); err != nil {
265 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
268 if ctx.NotifyFreq != nil {
269 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
272 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
274 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
276 ctx.Neigh[*job.PktEnc.Sender].Name,
283 dst := new([blake2b.Size256]byte)
284 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
285 nodeId := NodeId(*dst)
286 node, known := ctx.Neigh[nodeId]
287 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
289 ctx.LogE("rx", sds, "unknown node")
293 ctx.LogD("rx", sds, "taken")
295 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
296 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
301 ctx.LogI("rx", sds, "")
303 if err = os.Remove(job.Fd.Name()); err != nil {
304 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
309 ctx.LogE("rx", sds, "unknown type")