diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-14 19:26:55 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-14 19:26:55 +0530 |
| commit | 6c6c4a05267e93597bb009c7d6c36383b6af7bf7 (patch) | |
| tree | 4c2c85b78b07e877ca18e833d07e8624712e8a4c /internal/crud/cli_edit.go | |
| parent | 9eb2b774e9f0522a3547644453bf929dd560f05f (diff) | |
ssh
Diffstat (limited to 'internal/crud/cli_edit.go')
| -rw-r--r-- | internal/crud/cli_edit.go | 101 |
1 files changed, 97 insertions, 4 deletions
diff --git a/internal/crud/cli_edit.go b/internal/crud/cli_edit.go index 5e2cb8d..0213a81 100644 --- a/internal/crud/cli_edit.go +++ b/internal/crud/cli_edit.go @@ -3,13 +3,14 @@ package crud import ( "fmt" "os" + "strconv" "gitlab.com/whiteboardguy/eno/internal/crypt" "gitlab.com/whiteboardguy/eno/internal/editor" "gitlab.com/whiteboardguy/eno/internal/utils" ) -func EditNote(notePath, editorString string) error { +func EditNotePass(notePath, editorString string) error { noteInfo, err := os.Stat(notePath) if err != nil && !os.IsNotExist(err) { return err @@ -29,7 +30,7 @@ func EditNote(notePath, editorString string) error { tmpFile, err := os.CreateTemp("", "eno-*") if err != nil { return err } - plaintext, err := crypt.DecryptRawBytes(ciphertext, password) + plaintext, err := crypt.DecryptBytesPass(ciphertext, password) if err != nil { return err } crypt.TryMlock(plaintext) defer clear(plaintext) @@ -51,13 +52,13 @@ func EditNote(notePath, editorString string) error { os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) - encrypted, err := crypt.EncryptRawBytes(newContent, password, crypt.Pass) + encrypted, err := crypt.EncryptBytesPass(newContent, password) if err != nil { return err } err = os.WriteFile(notePath, encrypted, noteInfo.Mode().Perm()) // utils.ClearTerm() - fmt.Println("Saved:", noteInfo.Name()) + fmt.Println("Saved:", notePath) return err } @@ -71,3 +72,95 @@ func deferredCleanTmpFile(tmpFile *os.File, oldContentLen int) { os.WriteFile(tmpFile.Name(), make([]byte, int(tmpFileInfo.Size())), 0600) } + +func EditNoteSSH(notePath, privKeyPath, editorString string) error { + noteInfo, err := os.Stat(notePath) + if err != nil && !os.IsNotExist(err) { + return err + } + + ciphertext, err := os.ReadFile(notePath) + if err != nil { + return err + } + + idx := -1 + for i, b := range ciphertext { + if b == ':' { + idx = i + break + } + } + if idx == -1 { + return fmt.Errorf("invalid file format: no header length prefix found") + } + + headerLen, err := strconv.Atoi(string(ciphertext[:idx])) + if err != nil { + return fmt.Errorf("failed to parse header length: %v", err) + } + + if headerLen < 0 || idx+1+headerLen > len(ciphertext) { + return fmt.Errorf("invalid header length %d", headerLen) + } + + headerBytes := ciphertext[idx+1 : idx+1+headerLen] + headers, err := crypt.GetFileHeaders(headerBytes) + if err != nil { + return fmt.Errorf("failed to read file headers: %v", err) + } + + if headers.Auth != crypt.SSH { + return fmt.Errorf("note uses %s auth, not SSH", headers.Auth) + } + + pubKeys := make([][]byte, len(headers.Recipients)) + for i, recipient := range headers.Recipients { + pubKeys[i] = recipient.PubKeyBytes + } + + tmpFile, err := os.CreateTemp("", "eno-*") + if err != nil { + return err + } + + plaintext, err := crypt.DecryptBytesSSH(ciphertext, privKeyPath) + if err != nil { + return err + } + crypt.TryMlock(plaintext) + defer clear(plaintext) + + defer deferredCleanTmpFile(tmpFile, len(plaintext)) + defer os.Remove(tmpFile.Name()) + + _, err = tmpFile.Write(plaintext) + if err != nil { + return err + } + + err = editor.OpenWithEditor(tmpFile.Name(), editorString) + if err != nil { + return err + } + + newContent, err := os.ReadFile(tmpFile.Name()) + if err != nil { + return err + } + crypt.TryMlock(newContent) + defer clear(newContent) + + os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + + encrypted, err := crypt.EncryptBytesSSH(newContent, pubKeys) + if err != nil { + return err + } + + err = os.WriteFile(notePath, encrypted, noteInfo.Mode().Perm()) + + fmt.Println("Saved:", notePath) + + return err +} |