X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/72dd37f1abb2b550aaaac48eac677403d5664797..3c9b1dcdf40474396a3b035d548417b84a123164:/hotline/transaction_handlers.go?ds=sidebyside diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index 46f96eb..2c5a930 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -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)) @@ -644,7 +673,7 @@ func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err err var userFields []Field // TODO: make order deterministic for _, acc := range cc.Server.Accounts { - userField := acc.Payload() + userField := acc.MarshalBinary() userFields = append(userFields, NewField(fieldData, userField)) } @@ -792,11 +821,8 @@ func (cc *ClientConn) notifyNewUserHasJoined() (res []Transaction, err error) { } func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) { - bs := make([]byte, 2) - binary.BigEndian.PutUint16(bs, *cc.Server.NextGuestID) - + cc.Agreed = true cc.UserName = t.GetField(fieldUserName).Data - *cc.ID = bs *cc.Icon = t.GetField(fieldUserIconID).Data options := t.GetField(fieldOptions).Data @@ -1114,7 +1140,7 @@ func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err e newArt := NewsArtData{ Title: string(t.GetField(fieldNewsArtTitle).Data), Poster: string(cc.UserName), - Date: NewsDate(), + Date: toHotlineTime(time.Now()), PrevArt: []byte{0, 0, 0, 0}, NextArt: []byte{0, 0, 0, 0}, ParentArt: append([]byte{0, 0}, t.GetField(fieldNewsArtID).Data...), @@ -1170,9 +1196,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 - ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot+filePath, string(fileName)) + var fp FilePath + err = fp.UnmarshalBinary(filePath) + if err != nil { + return res, err + } + + ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName) if err != nil { return res, err } @@ -1180,11 +1212,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, } @@ -1240,9 +1270,13 @@ func HandleDownloadFolder(cc *ClientConn, t *Transaction) (res []Transaction, er cc.Transfers[FolderDownload] = append(cc.Transfers[FolderDownload], fileTransfer) var fp FilePath - fp.UnmarshalBinary(t.GetField(fieldFilePath).Data) + err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data) + if err != nil { + return res, err + } + + fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data) - fullFilePath := fmt.Sprintf("%v%v", cc.Server.Config.FileRoot+fp.String(), string(fileTransfer.FileName)) transferSize, err := CalcTotalSize(fullFilePath) if err != nil { return res, err @@ -1286,21 +1320,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 } @@ -1361,9 +1399,9 @@ func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction, return res, err } -// HandleKeepAlive response to keepalive transactions with an empty reply -// HL 1.9.2 Client sends keepalive msg every 3 minutes -// HL 1.2.3 Client doesn't send keepalives +// HandleKeepAlive responds to keepalive transactions with an empty reply +// * HL 1.9.2 Client sends keepalive msg every 3 minutes +// * HL 1.2.3 Client doesn't send keepalives func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) { res = append(res, cc.NewReply(t)) @@ -1371,14 +1409,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 }