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 sender := ctx.Neigh[*job.PktEnc.Sender]
102 sendmail := sender.Sendmail
103 if len(sendmail) == 0 {
104 ctx.LogE("rx", SdsAdd(sds, SDS{"err": "No sendmail configured"}), "")
112 sendmail[1:len(sendmail)],
113 strings.Split(recipients, " ")...,
116 cmd.Env = append(cmd.Env, "NNCP_SENDER=" + sender.Id.String())
117 cmd.Stdin = decompressor
118 if err = cmd.Run(); err != nil {
119 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
124 ctx.LogI("rx", sds, "")
126 if err = os.Remove(job.Fd.Name()); err != nil {
127 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
132 dst := string(pkt.Path[:int(pkt.PathLen)])
133 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
134 if filepath.IsAbs(dst) {
135 ctx.LogE("rx", sds, "non-relative destination path")
139 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
141 ctx.LogE("rx", sds, "incoming is not allowed")
145 dir := filepath.Join(*incoming, path.Dir(dst))
146 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
147 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
152 tmp, err := ioutil.TempFile(dir, "nncp-file")
153 sds["tmp"] = tmp.Name()
154 ctx.LogD("rx", sds, "created")
156 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
160 bufW := bufio.NewWriter(tmp)
161 if _, err = io.Copy(bufW, pipeR); err != nil {
162 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
169 dstPathOrig := filepath.Join(*incoming, dst)
170 dstPath := dstPathOrig
173 if _, err = os.Stat(dstPath); err != nil {
174 if os.IsNotExist(err) {
177 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
181 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
184 if err = os.Rename(tmp.Name(), dstPath); err != nil {
185 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
190 ctx.LogI("rx", sds, "")
192 if err = os.Remove(job.Fd.Name()); err != nil {
193 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
196 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
197 if ctx.NotifyFile != nil {
200 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
202 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
203 "File from %s: %s (%s)",
204 ctx.Neigh[*job.PktEnc.Sender].Name,
206 humanize.IBytes(uint64(pktSize)),
212 src := string(pkt.Path[:int(pkt.PathLen)])
213 if filepath.IsAbs(src) {
214 ctx.LogE("rx", sds, "non-relative source path")
218 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
219 dstRaw, err := ioutil.ReadAll(pipeR)
221 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
225 dst := string(dstRaw)
227 sender := ctx.Neigh[*job.PktEnc.Sender]
230 ctx.LogE("rx", sds, "freqing is not allowed")
235 if sender.FreqChunked == 0 {
239 filepath.Join(*freq, src),
244 err = ctx.TxFileChunked(
247 filepath.Join(*freq, src),
254 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
259 ctx.LogI("rx", sds, "")
261 if err = os.Remove(job.Fd.Name()); err != nil {
262 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
265 if ctx.NotifyFreq != nil {
266 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
269 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
271 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
273 ctx.Neigh[*job.PktEnc.Sender].Name,
280 dst := new([blake2b.Size256]byte)
281 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
282 nodeId := NodeId(*dst)
283 node, known := ctx.Neigh[nodeId]
284 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
286 ctx.LogE("rx", sds, "unknown node")
290 ctx.LogD("rx", sds, "taken")
292 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
293 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
298 ctx.LogI("rx", sds, "")
300 if err = os.Remove(job.Fd.Name()); err != nil {
301 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
306 ctx.LogE("rx", sds, "unknown type")