]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_handlers.go
patch: v0.0.13
[rbdr/mobius] / hotline / transaction_handlers.go
index d7a77e2f58a12aaa19594b3b7bf75ae752fd50f1..ea4649933e26534180831b60df0a8a1ad89e0dd9 100644 (file)
@@ -9,6 +9,7 @@ import (
        "io/ioutil"
        "math/big"
        "os"
+       "path"
        "sort"
        "strings"
        "time"
@@ -272,7 +273,7 @@ var TransactionHandlers = map[uint16]TransactionType{
                Handler: HandleSetUser,
        },
        tranUploadFile: {
-               Access:  accessUploadFile,
+               Access:  accessAlwaysAllow,
                DenyMsg: "You are not allowed to upload files.",
                Name:    "tranUploadFile",
                Handler: HandleUploadFile,
@@ -396,16 +397,16 @@ func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, er
 }
 
 func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
-       fileName := string(t.GetField(fieldFileName).Data)
-       filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+       fileName := t.GetField(fieldFileName).Data
+       filePath := t.GetField(fieldFilePath).Data
 
-       ffo, err := NewFlattenedFileObject(filePath, fileName)
+       ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
        if err != nil {
                return res, err
        }
 
        res = append(res, cc.NewReply(t,
-               NewField(fieldFileName, []byte(fileName)),
+               NewField(fieldFileName, fileName),
                NewField(fieldFileTypeString, ffo.FlatFileInformationFork.TypeSignature),
                NewField(fieldFileCreatorString, ffo.FlatFileInformationFork.CreatorSignature),
                NewField(fieldFileComment, ffo.FlatFileInformationFork.Comment),
@@ -426,14 +427,24 @@ func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err e
 // * 210       File comment    Optional
 // Fields used in the reply:   None
 func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
-       fileName := string(t.GetField(fieldFileName).Data)
-       filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+       fileName := t.GetField(fieldFileName).Data
+       filePath := t.GetField(fieldFilePath).Data
+
+       fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
+       if err != nil {
+               return res, err
+       }
+
+       fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(fieldFileNewName).Data)
+       if err != nil {
+               return nil, err
+       }
+
        //fileComment := t.GetField(fieldFileComment).Data
        fileNewName := t.GetField(fieldFileNewName).Data
 
        if fileNewName != nil {
-               path := filePath + "/" + fileName
-               fi, err := os.Stat(path)
+               fi, err := FS.Stat(fullFilePath)
                if err != nil {
                        return res, err
                }
@@ -450,9 +461,9 @@ func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err e
                        }
                }
 
-               err = os.Rename(filePath+"/"+fileName, filePath+"/"+string(fileNewName))
+               err = os.Rename(fullFilePath, fullNewFilePath)
                if os.IsNotExist(err) {
-                       res = append(res, cc.NewErrReply(t, "Cannot rename file "+fileName+" because it does not exist or cannot be found."))
+                       res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found."))
                        return res, err
                }
        }
@@ -467,16 +478,19 @@ func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err e
 // * 202       File path
 // Fields used in the reply: none
 func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
-       fileName := string(t.GetField(fieldFileName).Data)
-       filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+       fileName := t.GetField(fieldFileName).Data
+       filePath := t.GetField(fieldFilePath).Data
 
-       path := filePath + fileName
+       fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
+       if err != nil {
+               return res, err
+       }
 
-       cc.Server.Logger.Debugw("Delete file", "src", path)
+       cc.Server.Logger.Debugw("Delete file", "src", fullFilePath)
 
-       fi, err := os.Stat(path)
+       fi, err := os.Stat(fullFilePath)
        if err != nil {
-               res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found."))
+               res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found."))
                return res, nil
        }
        switch mode := fi.Mode(); {
@@ -492,7 +506,7 @@ func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err er
                }
        }
 
-       if err := os.RemoveAll(path); err != nil {
+       if err := os.RemoveAll(fullFilePath); err != nil {
                return res, err
        }
 
@@ -503,8 +517,8 @@ func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err er
 // HandleMoveFile moves files or folders. Note: seemingly not documented
 func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        fileName := string(t.GetField(fieldFileName).Data)
-       filePath :=  cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
-       fileNewPath :=  cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFileNewPath).Data)
+       filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+       fileNewPath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFileNewPath).Data)
 
        cc.Server.Logger.Debugw("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
 
@@ -542,18 +556,33 @@ func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err erro
 
 func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        newFolderPath := cc.Server.Config.FileRoot
+       folderName := string(t.GetField(fieldFileName).Data)
+
+       folderName = path.Join("/", folderName)
 
        // fieldFilePath is only present for nested paths
        if t.GetField(fieldFilePath).Data != nil {
                var newFp FilePath
-               newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
+               err := newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
+               if err != nil {
+                       return nil, err
+               }
                newFolderPath += newFp.String()
        }
-       newFolderPath += "/" + string(t.GetField(fieldFileName).Data)
+       newFolderPath = path.Join(newFolderPath, folderName)
 
-       if err := os.Mkdir(newFolderPath, 0777); err != nil {
-               // TODO: Send error response to client
-               return []Transaction{}, err
+       // TODO: check path and folder name lengths
+
+       if _, err := FS.Stat(newFolderPath); !os.IsNotExist(err) {
+               msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName)
+               return []Transaction{cc.NewErrReply(t, msg)}, nil
+       }
+
+       // TODO: check for disallowed characters to maintain compatibility for original client
+
+       if err := FS.Mkdir(newFolderPath, 0777); err != nil {
+               msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName)
+               return []Transaction{cc.NewErrReply(t, msg)}, nil
        }
 
        res = append(res, cc.NewReply(t))
@@ -1170,9 +1199,15 @@ func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error
 
 func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        fileName := t.GetField(fieldFileName).Data
-       filePath := ReadFilePath(t.GetField(fieldFilePath).Data)
+       filePath := t.GetField(fieldFilePath).Data
+
+       var fp FilePath
+       err = fp.UnmarshalBinary(filePath)
+       if err != nil {
+               return res, err
+       }
 
-       ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot+filePath, string(fileName))
+       ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
        if err != nil {
                return res, err
        }
@@ -1180,11 +1215,9 @@ func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err
        transactionRef := cc.Server.NewTransactionRef()
        data := binary.BigEndian.Uint32(transactionRef)
 
-       cc.Server.Logger.Infow("File download", "path", filePath)
-
        ft := &FileTransfer{
                FileName:        fileName,
-               FilePath:        []byte(filePath),
+               FilePath:        filePath,
                ReferenceNumber: transactionRef,
                Type:            FileDownload,
        }
@@ -1245,7 +1278,8 @@ func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, er
                return res, err
        }
 
-       fullFilePath := fmt.Sprintf("%v%v", cc.Server.Config.FileRoot+fp.String(), string(fileTransfer.FileName))
+       fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
+
        transferSize, err := CalcTotalSize(fullFilePath)
        if err != nil {
                return res, err
@@ -1289,21 +1323,25 @@ func HandleUploadFolder(cc *ClientConn, t *Transaction) (res []Transaction, err
 }
 
 func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
+       // TODO: add permission handing for upload folders and drop boxes
+       if !authorize(cc.Account.Access, accessUploadFile) {
+               res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
+               return res, err
+       }
+
        fileName := t.GetField(fieldFileName).Data
        filePath := t.GetField(fieldFilePath).Data
 
        transactionRef := cc.Server.NewTransactionRef()
        data := binary.BigEndian.Uint32(transactionRef)
 
-       fileTransfer := &FileTransfer{
+       cc.Server.FileTransfers[data] = &FileTransfer{
                FileName:        fileName,
                FilePath:        filePath,
                ReferenceNumber: transactionRef,
                Type:            FileUpload,
        }
 
-       cc.Server.FileTransfers[data] = fileTransfer
-
        res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
        return res, err
 }
@@ -1374,14 +1412,16 @@ func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err err
 }
 
 func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
-       filePath := cc.Server.Config.FileRoot
-
-       path := t.GetField(fieldFilePath).Data
-       if len(path) > 0 {
-               filePath = cc.Server.Config.FileRoot + ReadFilePath(path)
+       fullPath, err := readPath(
+               cc.Server.Config.FileRoot,
+               t.GetField(fieldFilePath).Data,
+               nil,
+       )
+       if err != nil {
+               return res, err
        }
 
-       fileNames, err := getFileNameList(filePath)
+       fileNames, err := getFileNameList(fullPath)
        if err != nil {
                return res, err
        }