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"
39 "golang.org/x/sys/unix"
42 func newNotification(fromTo *FromToYAML, subject string) io.Reader {
43 return strings.NewReader(fmt.Sprintf(
44 "From: %s\nTo: %s\nSubject: %s\n",
47 mime.BEncoding.Encode("UTF-8", subject),
51 func (ctx *Ctx) LockDir(nodeId *NodeId, xx TRxTx) (*os.File, error) {
52 ctx.ensureRxDir(nodeId)
53 lockPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx)) + ".lock"
54 dirLock, err := os.OpenFile(
56 os.O_CREATE|os.O_WRONLY,
60 ctx.LogE("lockdir", SDS{"path": lockPath, "err": err}, "")
63 err = unix.Flock(int(dirLock.Fd()), unix.LOCK_EX|unix.LOCK_NB)
65 ctx.LogE("lockdir", SDS{"path": lockPath, "err": err}, "")
72 func (ctx *Ctx) UnlockDir(fd *os.File) {
74 unix.Flock(int(fd.Fd()), unix.LOCK_UN)
79 func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) bool {
80 dirLock, err := ctx.LockDir(nodeId, TRx)
84 defer ctx.UnlockDir(dirLock)
86 for job := range ctx.Jobs(nodeId, TRx) {
87 pktName := filepath.Base(job.Fd.Name())
88 sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
89 if job.PktEnc.Nice > nice {
90 ctx.LogD("rx", SdsAdd(sds, SDS{
91 "nice": strconv.Itoa(int(job.PktEnc.Nice)),
95 pipeR, pipeW := io.Pipe()
96 errs := make(chan error, 1)
98 pipeWB := bufio.NewWriter(pipeW)
99 _, _, err := PktEncRead(
102 bufio.NewReader(job.Fd),
110 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "decryption")
116 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
117 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "unmarshal")
121 pktSize = job.Size - PktEncOverhead - PktOverhead
122 sds["size"] = strconv.FormatInt(pktSize, 10)
123 ctx.LogD("rx", sds, "taken")
126 recipients := string(pkt.Path[:int(pkt.PathLen)])
127 sds := SdsAdd(sds, SDS{
131 decompressor, err := zlib.NewReader(pipeR)
135 sendmail := ctx.Neigh[*job.PktEnc.Sender].Sendmail
136 if len(sendmail) == 0 {
137 ctx.LogE("rx", SdsAdd(sds, SDS{"err": "No sendmail configured"}), "")
145 sendmail[1:len(sendmail)],
146 strings.Split(recipients, " ")...,
149 cmd.Stdin = decompressor
150 if err = cmd.Run(); err != nil {
151 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
156 ctx.LogI("rx", sds, "")
158 if err = os.Remove(job.Fd.Name()); err != nil {
159 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
164 dst := string(pkt.Path[:int(pkt.PathLen)])
165 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
166 if filepath.IsAbs(dst) {
167 ctx.LogE("rx", sds, "non-relative destination path")
171 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
173 ctx.LogE("rx", sds, "incoming is not allowed")
177 dir := filepath.Join(*incoming, path.Dir(dst))
178 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
179 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
184 tmp, err := ioutil.TempFile(dir, "nncp-file")
185 sds["tmp"] = tmp.Name()
186 ctx.LogD("rx", sds, "created")
188 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
192 bufW := bufio.NewWriter(tmp)
193 if _, err = io.Copy(bufW, pipeR); err != nil {
194 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
201 dstPathOrig := filepath.Join(*incoming, dst)
202 dstPath := dstPathOrig
205 if _, err = os.Stat(dstPath); err != nil {
206 if os.IsNotExist(err) {
209 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
213 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
216 if err = os.Rename(tmp.Name(), dstPath); err != nil {
217 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
222 ctx.LogI("rx", sds, "")
224 if err = os.Remove(job.Fd.Name()); err != nil {
225 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
228 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
229 if ctx.NotifyFile != nil {
232 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
234 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
235 "File from %s: %s (%s)",
236 ctx.Neigh[*job.PktEnc.Sender].Name,
238 humanize.IBytes(uint64(pktSize)),
244 src := string(pkt.Path[:int(pkt.PathLen)])
245 if filepath.IsAbs(src) {
246 ctx.LogE("rx", sds, "non-relative source path")
250 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
251 dstRaw, err := ioutil.ReadAll(pipeR)
253 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read")
257 dst := string(dstRaw)
259 sender := ctx.Neigh[*job.PktEnc.Sender]
262 ctx.LogE("rx", sds, "freqing is not allowed")
267 if err = ctx.TxFile(sender, job.PktEnc.Nice, filepath.Join(*freq, src), dst, 0); err != nil {
268 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
273 ctx.LogI("rx", sds, "")
275 if err = os.Remove(job.Fd.Name()); err != nil {
276 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
279 if ctx.NotifyFreq != nil {
280 sendmail := ctx.Neigh[*ctx.SelfId].Sendmail
283 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
285 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
287 ctx.Neigh[*job.PktEnc.Sender].Name,
294 dst := new([blake2b.Size256]byte)
295 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
296 nodeId := NodeId(*dst)
297 node, known := ctx.Neigh[nodeId]
298 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
300 ctx.LogE("rx", sds, "unknown node")
304 ctx.LogD("rx", sds, "taken")
306 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
307 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
312 ctx.LogI("rx", sds, "")
314 if err = os.Remove(job.Fd.Name()); err != nil {
315 ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
320 ctx.LogE("rx", sds, "unknown type")