2015-09-03 19:09:23 +02:00
|
|
|
package frontend
|
|
|
|
|
2015-09-05 20:30:20 +02:00
|
|
|
// frontend sits between FUSE and ClueFS
|
|
|
|
// and uses cryptfs for all crypto operations
|
|
|
|
//
|
|
|
|
// cryptfs
|
|
|
|
// ^
|
|
|
|
// |
|
|
|
|
// v
|
|
|
|
// FUSE <-> frontend <-> ClueFS
|
|
|
|
//
|
|
|
|
// This file handles files access
|
|
|
|
|
2015-09-03 19:09:23 +02:00
|
|
|
import (
|
2015-09-05 14:08:00 +02:00
|
|
|
"fmt"
|
2015-09-03 19:09:23 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/cryptfs"
|
2015-09-04 20:31:06 +02:00
|
|
|
"github.com/rfjakob/cluefs/lib/cluefs"
|
2015-09-05 14:08:00 +02:00
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
|
|
|
"golang.org/x/net/context"
|
2015-09-03 19:09:23 +02:00
|
|
|
)
|
|
|
|
|
2015-09-05 19:07:20 +02:00
|
|
|
func fixFlags(flags fuse.OpenFlags) (fuse.OpenFlags, bool) {
|
2015-09-05 20:36:26 +02:00
|
|
|
cryptfs.Debug.Printf("fixFlags: Before: %s\n", flags.String())
|
2015-09-05 19:07:20 +02:00
|
|
|
var writeOnly bool
|
|
|
|
// We always need read access to do read-modify-write cycles
|
|
|
|
if flags & fuse.OpenWriteOnly > 0 {
|
|
|
|
flags = flags &^ fuse.OpenWriteOnly
|
|
|
|
flags = flags | fuse.OpenReadWrite
|
|
|
|
writeOnly = true
|
|
|
|
}
|
|
|
|
// We also cannot open the file in append mode, we need to seek back for RMW
|
|
|
|
flags = flags &^ fuse.OpenAppend
|
2015-09-05 20:36:26 +02:00
|
|
|
cryptfs.Debug.Printf("fixFlags: After: %s\n", flags.String())
|
2015-09-05 19:07:20 +02:00
|
|
|
return flags, writeOnly
|
|
|
|
}
|
|
|
|
|
|
|
|
func max(x int, y int) int {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2015-09-03 19:09:23 +02:00
|
|
|
type File struct {
|
2015-09-04 20:31:06 +02:00
|
|
|
*cluefs.File
|
2015-09-05 14:08:00 +02:00
|
|
|
crfs *cryptfs.CryptFS
|
2015-09-05 19:07:20 +02:00
|
|
|
// Remember if the file is supposed to be write-only
|
|
|
|
writeOnly bool
|
2015-09-05 14:08:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fusefs.Handle, error) {
|
2015-09-05 20:36:26 +02:00
|
|
|
cryptfs.Debug.Printf("File.Open\n")
|
2015-09-05 19:07:20 +02:00
|
|
|
|
|
|
|
req.Flags, f.writeOnly = fixFlags(req.Flags)
|
|
|
|
|
2015-09-05 14:08:00 +02:00
|
|
|
h, err := f.File.Open(ctx, req, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clueFile := h.(*cluefs.File)
|
|
|
|
return &File {
|
|
|
|
File: clueFile,
|
|
|
|
crfs: f.crfs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
2015-09-06 09:47:01 +02:00
|
|
|
|
2015-09-06 11:42:01 +02:00
|
|
|
cryptfs.Debug.Printf("Read: o=%d l=%d\n", req.Offset, req.Size)
|
2015-09-06 09:47:01 +02:00
|
|
|
|
|
|
|
// Read the backing ciphertext in one go
|
2015-09-05 20:11:20 +02:00
|
|
|
iblocks := f.crfs.SplitRange(uint64(req.Offset), uint64(req.Size))
|
2015-09-06 09:47:01 +02:00
|
|
|
var cipherReq fuse.ReadRequest
|
|
|
|
var cipherResp fuse.ReadResponse
|
|
|
|
o, l := f.crfs.JoinCiphertextRange(iblocks)
|
|
|
|
cipherResp.Data = make([]byte, int(l))
|
|
|
|
cipherReq.Offset = int64(o)
|
|
|
|
cipherReq.Size = int(l)
|
|
|
|
cryptfs.Debug.Printf("Read: cipherReq o=%d l=%d\n", o, l)
|
|
|
|
err := f.File.Read(ctx, &cipherReq, &cipherResp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt it
|
|
|
|
plaintext, err := f.crfs.DecryptBlocks(cipherResp.Data)
|
|
|
|
if err != nil {
|
|
|
|
resp.Data = plaintext
|
|
|
|
return err
|
2015-09-05 14:08:00 +02:00
|
|
|
}
|
2015-09-06 09:47:01 +02:00
|
|
|
// Crop down to relevant part
|
|
|
|
resp.Data = f.crfs.CropPlaintext(plaintext, iblocks)
|
|
|
|
|
2015-09-05 14:08:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
|
2015-09-05 20:36:26 +02:00
|
|
|
cryptfs.Debug.Printf("File.Write\n")
|
2015-09-05 19:07:20 +02:00
|
|
|
resp.Size = 0
|
2015-09-05 20:11:20 +02:00
|
|
|
iblocks := f.crfs.SplitRange(uint64(req.Offset), uint64(len(req.Data)))
|
2015-09-05 19:07:20 +02:00
|
|
|
var blockData []byte
|
|
|
|
for _, ib := range iblocks {
|
|
|
|
if ib.IsPartial() {
|
|
|
|
// RMW
|
2015-09-06 09:47:01 +02:00
|
|
|
cryptfs.Debug.Printf("RMW\n")
|
2015-09-05 19:07:20 +02:00
|
|
|
blockData = make([]byte, f.crfs.PlainBS())
|
|
|
|
var readReq fuse.ReadRequest
|
|
|
|
var readResp fuse.ReadResponse
|
|
|
|
o, l := ib.PlaintextRange()
|
2015-09-05 20:11:20 +02:00
|
|
|
readReq.Offset = int64(o)
|
2015-09-05 19:07:20 +02:00
|
|
|
readReq.Size = int(l)
|
|
|
|
err := f.Read(ctx, &readReq, &readResp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(blockData, readResp.Data)
|
|
|
|
copy(blockData[ib.Offset:ib.Offset+ib.Length], req.Data)
|
|
|
|
blockLen := max(len(readResp.Data), int(ib.Offset+ib.Length))
|
|
|
|
blockData = blockData[0:blockLen]
|
|
|
|
} else {
|
|
|
|
blockData = req.Data[0:f.crfs.PlainBS()]
|
|
|
|
}
|
|
|
|
ciphertext := f.crfs.EncryptBlock(blockData)
|
|
|
|
var partReq fuse.WriteRequest
|
|
|
|
var partResp fuse.WriteResponse
|
|
|
|
o, _ := ib.CiphertextRange()
|
|
|
|
partReq.Data = ciphertext
|
2015-09-05 20:11:20 +02:00
|
|
|
partReq.Offset = int64(o)
|
2015-09-05 19:07:20 +02:00
|
|
|
err := f.File.Write(ctx, &partReq, &partResp)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Write failure: %s\n", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove written data from the front of the request
|
2015-09-06 09:47:01 +02:00
|
|
|
cryptfs.Debug.Printf("req.Data[%d:%d]\n", int(ib.Length), len(req.Data))
|
|
|
|
req.Data = req.Data[int(ib.Length):len(req.Data)]
|
|
|
|
resp.Size += int(ib.Length)
|
2015-09-05 19:07:20 +02:00
|
|
|
}
|
2015-09-05 14:08:00 +02:00
|
|
|
return nil
|
2015-09-03 19:09:23 +02:00
|
|
|
}
|
2015-09-05 20:11:20 +02:00
|
|
|
|
|
|
|
func (f *File) Attr(ctx context.Context, attr *fuse.Attr) error {
|
2015-09-05 20:36:26 +02:00
|
|
|
cryptfs.Debug.Printf("Attr\n")
|
2015-09-05 20:11:20 +02:00
|
|
|
err := f.File.Node.Attr(ctx, attr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attr.Size = f.crfs.PlainSize(attr.Size)
|
|
|
|
return nil
|
|
|
|
}
|