2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
37 xdr "github.com/davecgh/go-xdr/xdr2"
38 "github.com/dustin/go-humanize"
39 "github.com/klauspost/compress/zstd"
40 "golang.org/x/crypto/blake2b"
41 "golang.org/x/crypto/poly1305"
48 func newNotification(fromTo *FromToJSON, subject string, body []byte) io.Reader {
50 "From: " + fromTo.From,
52 "Subject: " + mime.BEncoding.Encode("UTF-8", subject),
55 lines = append(lines, []string{
57 "Content-Type: text/plain; charset=utf-8",
58 "Content-Transfer-Encoding: base64",
60 base64.StdEncoding.EncodeToString(body),
63 return strings.NewReader(strings.Join(lines, "\n"))
69 dryRun, doSeen, noFile, noFreq, noExec, noTrns bool,
71 dirLock, err := ctx.LockDir(nodeId, "toss")
73 ctx.LogE("rx", SDS{}, err, "lock")
76 defer ctx.UnlockDir(dirLock)
78 sendmail := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
79 decompressor, err := zstd.NewReader(nil)
83 defer decompressor.Close()
84 for job := range ctx.Jobs(nodeId, TRx) {
85 pktName := filepath.Base(job.Fd.Name())
86 sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
87 if job.PktEnc.Nice > nice {
88 ctx.LogD("rx", SdsAdd(sds, SDS{"nice": int(job.PktEnc.Nice)}), "too nice")
91 pipeR, pipeW := io.Pipe()
92 errs := make(chan error, 1)
94 pipeWB := bufio.NewWriter(pipeW)
95 _, _, err := PktEncRead(
98 bufio.NewReader(job.Fd),
106 ctx.LogE("rx", sds, err, "decryption")
112 var pktSizeBlocks int64
113 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
114 ctx.LogE("rx", sds, err, "unmarshal")
118 pktSize = job.Size - PktEncOverhead - PktOverhead - PktSizeOverhead
119 pktSizeBlocks = pktSize / (EncBlkSize + poly1305.TagSize)
120 if pktSize%(EncBlkSize+poly1305.TagSize) != 0 {
121 pktSize -= poly1305.TagSize
123 pktSize -= pktSizeBlocks * poly1305.TagSize
124 sds["size"] = pktSize
125 ctx.LogD("rx", sds, "taken")
131 path := bytes.Split(pkt.Path[:int(pkt.PathLen)], []byte{0})
132 handle := string(path[0])
133 args := make([]string, 0, len(path)-1)
134 for _, p := range path[1:] {
135 args = append(args, string(p))
137 argsStr := strings.Join(append([]string{handle}, args...), " ")
138 sds := SdsAdd(sds, SDS{
142 sender := ctx.Neigh[*job.PktEnc.Sender]
143 cmdline, exists := sender.Exec[handle]
144 if !exists || len(cmdline) == 0 {
145 ctx.LogE("rx", sds, errors.New("No handle found"), "")
149 if err = decompressor.Reset(pipeR); err != nil {
155 append(cmdline[1:len(cmdline)], args...)...,
159 "NNCP_SELF="+ctx.Self.Id.String(),
160 "NNCP_SENDER="+sender.Id.String(),
161 "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
163 cmd.Stdin = decompressor
164 output, err := cmd.Output()
166 ctx.LogE("rx", sds, err, "handle")
170 if len(sendmail) > 0 && ctx.NotifyExec != nil {
171 notify, exists := ctx.NotifyExec[sender.Name+"."+handle]
173 notify, exists = ctx.NotifyExec["*."+handle]
178 append(sendmail[1:len(sendmail)], notify.To)...,
180 cmd.Stdin = newNotification(notify, fmt.Sprintf(
181 "Exec from %s: %s", sender.Name, argsStr,
187 ctx.LogI("rx", sds, "")
190 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
191 fd.Close() // #nosec G104
194 if err = os.Remove(job.Fd.Name()); err != nil {
195 ctx.LogE("rx", sds, err, "remove")
203 dst := string(pkt.Path[:int(pkt.PathLen)])
204 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
205 if filepath.IsAbs(dst) {
206 ctx.LogE("rx", sds, errors.New("non-relative destination path"), "")
210 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
212 ctx.LogE("rx", sds, errors.New("incoming is not allowed"), "")
216 dir := filepath.Join(*incoming, path.Dir(dst))
217 if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
218 ctx.LogE("rx", sds, err, "mkdir")
223 tmp, err := TempFile(dir, "file")
225 ctx.LogE("rx", sds, err, "mktemp")
229 sds["tmp"] = tmp.Name()
230 ctx.LogD("rx", sds, "created")
231 bufW := bufio.NewWriter(tmp)
232 if _, err = CopyProgressed(
233 bufW, pipeR, "Rx file",
234 SdsAdd(sds, SDS{"fullsize": sds["size"]}),
237 ctx.LogE("rx", sds, err, "copy")
241 if err = bufW.Flush(); err != nil {
242 tmp.Close() // #nosec G104
243 ctx.LogE("rx", sds, err, "copy")
247 if err = tmp.Sync(); err != nil {
248 tmp.Close() // #nosec G104
249 ctx.LogE("rx", sds, err, "copy")
254 dstPathOrig := filepath.Join(*incoming, dst)
255 dstPath := dstPathOrig
258 if _, err = os.Stat(dstPath); err != nil {
259 if os.IsNotExist(err) {
262 ctx.LogE("rx", sds, err, "stat")
266 dstPath = dstPathOrig + "." + strconv.Itoa(dstPathCtr)
269 if err = os.Rename(tmp.Name(), dstPath); err != nil {
270 ctx.LogE("rx", sds, err, "rename")
273 if err = DirSync(*incoming); err != nil {
274 ctx.LogE("rx", sds, err, "sync")
279 ctx.LogI("rx", sds, "")
282 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
283 fd.Close() // #nosec G104
286 if err = os.Remove(job.Fd.Name()); err != nil {
287 ctx.LogE("rx", sds, err, "remove")
290 if len(sendmail) > 0 && ctx.NotifyFile != nil {
293 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
295 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
296 "File from %s: %s (%s)",
297 ctx.Neigh[*job.PktEnc.Sender].Name,
299 humanize.IBytes(uint64(pktSize)),
308 src := string(pkt.Path[:int(pkt.PathLen)])
309 if filepath.IsAbs(src) {
310 ctx.LogE("rx", sds, errors.New("non-relative source path"), "")
314 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
315 dstRaw, err := ioutil.ReadAll(pipeR)
317 ctx.LogE("rx", sds, err, "read")
321 dst := string(dstRaw)
323 sender := ctx.Neigh[*job.PktEnc.Sender]
324 freqPath := sender.FreqPath
326 ctx.LogE("rx", sds, errors.New("freqing is not allowed"), "")
334 filepath.Join(*freqPath, src),
341 ctx.LogE("rx", sds, err, "tx file")
346 ctx.LogI("rx", sds, "")
349 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
350 fd.Close() // #nosec G104
353 if err = os.Remove(job.Fd.Name()); err != nil {
354 ctx.LogE("rx", sds, err, "remove")
357 if len(sendmail) > 0 && ctx.NotifyFreq != nil {
360 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
362 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
363 "Freq from %s: %s", sender.Name, src,
372 dst := new([blake2b.Size256]byte)
373 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
374 nodeId := NodeId(*dst)
375 node, known := ctx.Neigh[nodeId]
376 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
378 ctx.LogE("rx", sds, errors.New("unknown node"), "")
382 ctx.LogD("rx", sds, "taken")
384 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
385 ctx.LogE("rx", sds, err, "tx trns")
390 ctx.LogI("rx", sds, "")
393 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
394 fd.Close() // #nosec G104
397 if err = os.Remove(job.Fd.Name()); err != nil {
398 ctx.LogE("rx", sds, err, "remove")
403 ctx.LogE("rx", sds, errors.New("unknown type"), "")
407 pipeR.Close() // #nosec G104