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/>.
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.Env = append(cmd.Env, "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)))
122 cmd.Stdin = decompressor
123 if err = cmd.Run(); err != nil {
124 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
129 ctx.LogI("rx", sds, "")
132 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
136 if err = os.Remove(job.Fd.Name()); err != nil {
137 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
142 dst := string(pkt.Path[:int(pkt.PathLen)])
143 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
144 if filepath.IsAbs(dst) {
145 ctx.LogE("rx", sds, "non-relative destination path")
149 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
151 ctx.LogE("rx", sds, "incoming is not allowed")
155 dir := filepath.Join(*incoming, path.Dir(dst))
156 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
157 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
162 tmp, err := ioutil.TempFile(dir, "nncp-file")
163 sds["tmp"] = tmp.Name()
164 ctx.LogD("rx", sds, "created")
166 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
170 bufW := bufio.NewWriter(tmp)
171 if _, err = io.Copy(bufW, pipeR); err != nil {
172 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
179 dstPathOrig := filepath.Join(*incoming, dst)
180 dstPath := dstPathOrig
183 if _, err = os.Stat(dstPath); err != nil {
184 if os.IsNotExist(err) {
187 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
191 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
194 if err = os.Rename(tmp.Name(), dstPath); err != nil {
195 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
200 ctx.LogI("rx", sds, "")
203 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
207 if err = os.Remove(job.Fd.Name()); err != nil {
208 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
211 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
212 if ctx.NotifyFile != nil {
215 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
217 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
218 "File from %s: %s (%s)",
219 ctx.Neigh[*job.PktEnc.Sender].Name,
221 humanize.IBytes(uint64(pktSize)),
227 src := string(pkt.Path[:int(pkt.PathLen)])
228 if filepath.IsAbs(src) {
229 ctx.LogE("rx", sds, "non-relative source path")
233 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
234 dstRaw, err := ioutil.ReadAll(pipeR)
236 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
240 dst := string(dstRaw)
242 sender := ctx.Neigh[*job.PktEnc.Sender]
245 ctx.LogE("rx", sds, "freqing is not allowed")
250 if sender.FreqChunked == 0 {
254 filepath.Join(*freq, src),
259 err = ctx.TxFileChunked(
262 filepath.Join(*freq, src),
269 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
274 ctx.LogI("rx", sds, "")
277 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
281 if err = os.Remove(job.Fd.Name()); err != nil {
282 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
285 if ctx.NotifyFreq != nil {
286 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
289 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
291 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
293 ctx.Neigh[*job.PktEnc.Sender].Name,
300 dst := new([blake2b.Size256]byte)
301 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
302 nodeId := NodeId(*dst)
303 node, known := ctx.Neigh[nodeId]
304 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
306 ctx.LogE("rx", sds, "unknown node")
310 ctx.LogD("rx", sds, "taken")
312 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
313 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
318 ctx.LogI("rx", sds, "")
321 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
325 if err = os.Remove(job.Fd.Name()); err != nil {
326 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
331 ctx.LogE("rx", sds, "unknown type")