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 {
52 for job := range ctx.Jobs(nodeId, TRx) {
53 pktName := filepath.Base(job.Fd.Name())
54 sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
55 if job.PktEnc.Nice > nice {
56 ctx.LogD("rx", SdsAdd(sds, SDS{
57 "nice": strconv.Itoa(int(job.PktEnc.Nice)),
61 pipeR, pipeW := io.Pipe()
62 errs := make(chan error, 1)
64 pipeWB := bufio.NewWriter(pipeW)
65 _, _, err := PktEncRead(
68 bufio.NewReader(job.Fd),
76 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "decryption")
82 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
83 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "unmarshal")
87 pktSize = job.Size - PktEncOverhead - PktOverhead
88 sds["size"] = strconv.FormatInt(pktSize, 10)
89 ctx.LogD("rx", sds, "taken")
92 recipients := string(pkt.Path[:int(pkt.PathLen)])
93 sds := SdsAdd(sds, SDS{
97 decompressor, err := zlib.NewReader(pipeR)
101 sendmail := ctx.Neigh[*job.PktEnc.Sender].Sendmail
102 if len(sendmail) == 0 {
103 ctx.LogE("rx", SdsAdd(sds, SDS{"err": "No sendmail configured"}), "")
111 sendmail[1:len(sendmail)],
112 strings.Split(recipients, " ")...,
115 cmd.Stdin = decompressor
116 if err = cmd.Run(); err != nil {
117 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
122 ctx.LogI("rx", sds, "")
124 if err = os.Remove(job.Fd.Name()); err != nil {
125 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
130 dst := string(pkt.Path[:int(pkt.PathLen)])
131 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
132 if filepath.IsAbs(dst) {
133 ctx.LogE("rx", sds, "non-relative destination path")
137 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
139 ctx.LogE("rx", sds, "incoming is not allowed")
143 dir := filepath.Join(*incoming, path.Dir(dst))
144 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
145 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
150 tmp, err := ioutil.TempFile(dir, "nncp-file")
151 sds["tmp"] = tmp.Name()
152 ctx.LogD("rx", sds, "created")
154 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
158 bufW := bufio.NewWriter(tmp)
159 if _, err = io.Copy(bufW, pipeR); err != nil {
160 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
167 dstPathOrig := filepath.Join(*incoming, dst)
168 dstPath := dstPathOrig
171 if _, err = os.Stat(dstPath); err != nil {
172 if os.IsNotExist(err) {
175 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
179 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
182 if err = os.Rename(tmp.Name(), dstPath); err != nil {
183 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
188 ctx.LogI("rx", sds, "")
190 if err = os.Remove(job.Fd.Name()); err != nil {
191 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
194 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
195 if ctx.NotifyFile != nil {
198 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
200 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
201 "File from %s: %s (%s)",
202 ctx.Neigh[*job.PktEnc.Sender].Name,
204 humanize.IBytes(uint64(pktSize)),
210 src := string(pkt.Path[:int(pkt.PathLen)])
211 if filepath.IsAbs(src) {
212 ctx.LogE("rx", sds, "non-relative source path")
216 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
217 dstRaw, err := ioutil.ReadAll(pipeR)
219 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
223 dst := string(dstRaw)
225 sender := ctx.Neigh[*job.PktEnc.Sender]
228 ctx.LogE("rx", sds, "freqing is not allowed")
233 if sender.FreqChunked == 0 {
237 filepath.Join(*freq, src),
242 err = ctx.TxFileChunked(
245 filepath.Join(*freq, src),
252 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
257 ctx.LogI("rx", sds, "")
259 if err = os.Remove(job.Fd.Name()); err != nil {
260 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
263 if ctx.NotifyFreq != nil {
264 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
267 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
269 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
271 ctx.Neigh[*job.PktEnc.Sender].Name,
278 dst := new([blake2b.Size256]byte)
279 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
280 nodeId := NodeId(*dst)
281 node, known := ctx.Neigh[nodeId]
282 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
284 ctx.LogE("rx", sds, "unknown node")
288 ctx.LogD("rx", sds, "taken")
290 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
291 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
296 ctx.LogI("rx", sds, "")
298 if err = os.Remove(job.Fd.Name()); err != nil {
299 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
304 ctx.LogE("rx", sds, "unknown type")