2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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 go func(job Job) error {
93 pipeWB := bufio.NewWriter(pipeW)
94 _, _, err := PktEncRead(
97 bufio.NewReader(job.Fd),
100 job.Fd.Close() // #nosec G104
102 ctx.LogE("rx", sds, err, "decryption")
103 return pipeW.CloseWithError(err)
105 if err = pipeWB.Flush(); err != nil {
106 ctx.LogE("rx", sds, err, "decryption flush")
107 return pipeW.CloseWithError(err)
114 var pktSizeBlocks int64
115 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
116 ctx.LogE("rx", sds, err, "unmarshal")
120 pktSize = job.Size - PktEncOverhead - PktOverhead - PktSizeOverhead
121 pktSizeBlocks = pktSize / (EncBlkSize + poly1305.TagSize)
122 if pktSize%(EncBlkSize+poly1305.TagSize) != 0 {
123 pktSize -= poly1305.TagSize
125 pktSize -= pktSizeBlocks * poly1305.TagSize
126 sds["size"] = pktSize
127 ctx.LogD("rx", sds, "taken")
129 case PktTypeExec, PktTypeExecFat:
133 path := bytes.Split(pkt.Path[:int(pkt.PathLen)], []byte{0})
134 handle := string(path[0])
135 args := make([]string, 0, len(path)-1)
136 for _, p := range path[1:] {
137 args = append(args, string(p))
139 argsStr := strings.Join(append([]string{handle}, args...), " ")
140 sds := SdsAdd(sds, SDS{
144 sender := ctx.Neigh[*job.PktEnc.Sender]
145 cmdline, exists := sender.Exec[handle]
146 if !exists || len(cmdline) == 0 {
147 ctx.LogE("rx", sds, errors.New("No handle found"), "")
151 if pkt.Type == PktTypeExec {
152 if err = decompressor.Reset(pipeR); err != nil {
159 append(cmdline[1:len(cmdline)], args...)...,
163 "NNCP_SELF="+ctx.Self.Id.String(),
164 "NNCP_SENDER="+sender.Id.String(),
165 "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
167 if pkt.Type == PktTypeExec {
168 cmd.Stdin = decompressor
172 output, err := cmd.Output()
174 ctx.LogE("rx", sds, err, "handle")
178 if len(sendmail) > 0 && ctx.NotifyExec != nil {
179 notify, exists := ctx.NotifyExec[sender.Name+"."+handle]
181 notify, exists = ctx.NotifyExec["*."+handle]
186 append(sendmail[1:len(sendmail)], notify.To)...,
188 cmd.Stdin = newNotification(notify, fmt.Sprintf(
189 "Exec from %s: %s", sender.Name, argsStr,
191 if err = cmd.Run(); err != nil {
192 ctx.LogE("rx", sds, err, "notify")
197 ctx.LogI("rx", sds, "")
200 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
201 fd.Close() // #nosec G104
204 if err = os.Remove(job.Fd.Name()); err != nil {
205 ctx.LogE("rx", sds, err, "remove")
213 dst := string(pkt.Path[:int(pkt.PathLen)])
214 sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
215 if filepath.IsAbs(dst) {
216 ctx.LogE("rx", sds, errors.New("non-relative destination path"), "")
220 incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
222 ctx.LogE("rx", sds, errors.New("incoming is not allowed"), "")
226 dir := filepath.Join(*incoming, path.Dir(dst))
227 if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
228 ctx.LogE("rx", sds, err, "mkdir")
233 tmp, err := TempFile(dir, "file")
235 ctx.LogE("rx", sds, err, "mktemp")
239 sds["tmp"] = tmp.Name()
240 ctx.LogD("rx", sds, "created")
241 bufW := bufio.NewWriter(tmp)
242 if _, err = CopyProgressed(
243 bufW, pipeR, "Rx file",
244 SdsAdd(sds, SDS{"fullsize": sds["size"]}),
247 ctx.LogE("rx", sds, err, "copy")
251 if err = bufW.Flush(); err != nil {
252 tmp.Close() // #nosec G104
253 ctx.LogE("rx", sds, err, "copy")
257 if err = tmp.Sync(); err != nil {
258 tmp.Close() // #nosec G104
259 ctx.LogE("rx", sds, err, "copy")
263 if err = tmp.Close(); err != nil {
264 ctx.LogE("rx", sds, err, "copy")
268 dstPathOrig := filepath.Join(*incoming, dst)
269 dstPath := dstPathOrig
272 if _, err = os.Stat(dstPath); err != nil {
273 if os.IsNotExist(err) {
276 ctx.LogE("rx", sds, err, "stat")
280 dstPath = dstPathOrig + "." + strconv.Itoa(dstPathCtr)
283 if err = os.Rename(tmp.Name(), dstPath); err != nil {
284 ctx.LogE("rx", sds, err, "rename")
287 if err = DirSync(*incoming); err != nil {
288 ctx.LogE("rx", sds, err, "sync")
293 ctx.LogI("rx", sds, "")
296 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
297 fd.Close() // #nosec G104
300 if err = os.Remove(job.Fd.Name()); err != nil {
301 ctx.LogE("rx", sds, err, "remove")
304 if len(sendmail) > 0 && ctx.NotifyFile != nil {
307 append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
309 cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
310 "File from %s: %s (%s)",
311 ctx.Neigh[*job.PktEnc.Sender].Name,
313 humanize.IBytes(uint64(pktSize)),
315 if err = cmd.Run(); err != nil {
316 ctx.LogE("rx", sds, err, "notify")
324 src := string(pkt.Path[:int(pkt.PathLen)])
325 if filepath.IsAbs(src) {
326 ctx.LogE("rx", sds, errors.New("non-relative source path"), "")
330 sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
331 dstRaw, err := ioutil.ReadAll(pipeR)
333 ctx.LogE("rx", sds, err, "read")
337 dst := string(dstRaw)
339 sender := ctx.Neigh[*job.PktEnc.Sender]
340 freqPath := sender.FreqPath
342 ctx.LogE("rx", sds, errors.New("freqing is not allowed"), "")
350 filepath.Join(*freqPath, src),
357 ctx.LogE("rx", sds, err, "tx file")
362 ctx.LogI("rx", sds, "")
365 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
366 fd.Close() // #nosec G104
369 if err = os.Remove(job.Fd.Name()); err != nil {
370 ctx.LogE("rx", sds, err, "remove")
373 if len(sendmail) > 0 && ctx.NotifyFreq != nil {
376 append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
378 cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
379 "Freq from %s: %s", sender.Name, src,
381 if err = cmd.Run(); err != nil {
382 ctx.LogE("rx", sds, err, "notify")
390 dst := new([blake2b.Size256]byte)
391 copy(dst[:], pkt.Path[:int(pkt.PathLen)])
392 nodeId := NodeId(*dst)
393 node, known := ctx.Neigh[nodeId]
394 sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
396 ctx.LogE("rx", sds, errors.New("unknown node"), "")
400 ctx.LogD("rx", sds, "taken")
402 if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
403 ctx.LogE("rx", sds, err, "tx trns")
408 ctx.LogI("rx", sds, "")
411 if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
412 fd.Close() // #nosec G104
415 if err = os.Remove(job.Fd.Name()); err != nil {
416 ctx.LogE("rx", sds, err, "remove")
421 ctx.LogE("rx", sds, errors.New("unknown type"), "")
425 pipeR.Close() // #nosec G104