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"
45 func newNotification(fromTo *FromToYAML, subject string) io.Reader {
46 return strings.NewReader(fmt.Sprintf(
47 "From: %s\nTo: %s\nSubject: %s\n",
50 mime.BEncoding.Encode("UTF-8", subject),
54 func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun, doSeen bool) bool {
56 for job := range ctx.Jobs(nodeId, TRx) {
57 pktName := filepath.Base(job.Fd.Name())
58 sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
59 if job.PktEnc.Nice > nice {
60 ctx.LogD("rx", SdsAdd(sds, SDS{
61 "nice": strconv.Itoa(int(job.PktEnc.Nice)),
65 pipeR, pipeW := io.Pipe()
66 errs := make(chan error, 1)
68 pipeWB := bufio.NewWriter(pipeW)
69 _, _, err := PktEncRead(
72 bufio.NewReader(job.Fd),
80 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "decryption")
86 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
87 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "unmarshal")
91 pktSize = job.Size - PktEncOverhead - PktOverhead
92 sds["size"] = strconv.FormatInt(pktSize, 10)
93 ctx.LogD("rx", sds, "taken")
96 recipients := string(pkt.Path[:int(pkt.PathLen)])
97 sds := SdsAdd(sds, SDS{
101 decompressor, err := zlib.NewReader(pipeR)
105 sender := ctx.Neigh[*job.PktEnc.Sender]
106 sendmail := 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.Env = append(cmd.Env, "NNCP_SENDER="+sender.Id.String())
121 cmd.Stdin = decompressor
122 if err = cmd.Run(); err != nil {
123 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
128 ctx.LogI("rx", sds, "")
131 if fd, err := os.Create(job.Fd.Name() + SeenPostfix); err == nil {
135 if err = os.Remove(job.Fd.Name()); err != nil {
136 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
141 dst := string(pkt.Path[:int(pkt.PathLen)])
142 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
143 if filepath.IsAbs(dst) {
144 ctx.LogE("rx", sds, "non-relative destination path")
148 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
150 ctx.LogE("rx", sds, "incoming is not allowed")
154 dir := filepath.Join(*incoming, path.Dir(dst))
155 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
156 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
161 tmp, err := ioutil.TempFile(dir, "nncp-file")
162 sds["tmp"] = tmp.Name()
163 ctx.LogD("rx", sds, "created")
165 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
169 bufW := bufio.NewWriter(tmp)
170 if _, err = io.Copy(bufW, pipeR); err != nil {
171 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
178 dstPathOrig := filepath.Join(*incoming, dst)
179 dstPath := dstPathOrig
182 if _, err = os.Stat(dstPath); err != nil {
183 if os.IsNotExist(err) {
186 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
190 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
193 if err = os.Rename(tmp.Name(), dstPath); err != nil {
194 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
199 ctx.LogI("rx", sds, "")
202 if fd, err := os.Create(job.Fd.Name() + SeenPostfix); err == nil {
206 if err = os.Remove(job.Fd.Name()); err != nil {
207 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
210 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
211 if ctx.NotifyFile != nil {
214 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
216 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
217 "File from %s: %s (%s)",
218 ctx.Neigh[*job.PktEnc.Sender].Name,
220 humanize.IBytes(uint64(pktSize)),
226 src := string(pkt.Path[:int(pkt.PathLen)])
227 if filepath.IsAbs(src) {
228 ctx.LogE("rx", sds, "non-relative source path")
232 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
233 dstRaw, err := ioutil.ReadAll(pipeR)
235 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
239 dst := string(dstRaw)
241 sender := ctx.Neigh[*job.PktEnc.Sender]
244 ctx.LogE("rx", sds, "freqing is not allowed")
249 if sender.FreqChunked == 0 {
253 filepath.Join(*freq, src),
258 err = ctx.TxFileChunked(
261 filepath.Join(*freq, src),
268 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
273 ctx.LogI("rx", sds, "")
276 if fd, err := os.Create(job.Fd.Name() + SeenPostfix); err == nil {
280 if err = os.Remove(job.Fd.Name()); err != nil {
281 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
284 if ctx.NotifyFreq != nil {
285 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
288 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
290 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
292 ctx.Neigh[*job.PktEnc.Sender].Name,
299 dst := new([blake2b.Size256]byte)
300 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
301 nodeId := NodeId(*dst)
302 node, known := ctx.Neigh[nodeId]
303 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
305 ctx.LogE("rx", sds, "unknown node")
309 ctx.LogD("rx", sds, "taken")
311 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
312 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
317 ctx.LogI("rx", sds, "")
320 if fd, err := os.Create(job.Fd.Name() + SeenPostfix); err == nil {
324 if err = os.Remove(job.Fd.Name()); err != nil {
325 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
330 ctx.LogE("rx", sds, "unknown type")