mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 04:46:42 -04:00
Port to latest rcw development version
This commit is contained in:
@@ -33,3 +33,5 @@ const (
|
||||
ErrorClipboard = 110
|
||||
ErrorOther = 111
|
||||
)
|
||||
|
||||
var GetPassphrase func() []byte
|
||||
|
||||
+2
-2
@@ -11,10 +11,10 @@ import (
|
||||
)
|
||||
|
||||
// CopyArgument copies a field from an entry to the clipboard.
|
||||
func CopyArgument(targetLocation string, field int, passphrase []byte) {
|
||||
func CopyArgument(targetLocation string, field int) {
|
||||
if isFile, _ := TargetIsFile(targetLocation, true, 2); isFile {
|
||||
|
||||
decryptedEntry := DecryptFileToSlice(targetLocation, passphrase)
|
||||
decryptedEntry := DecryptFileToSlice(targetLocation)
|
||||
var copySubject string // will store data to be copied
|
||||
|
||||
// ensure field exists in entry
|
||||
|
||||
+2
-2
@@ -1,12 +1,12 @@
|
||||
package core
|
||||
|
||||
// GetOldEntryData decrypts and returns old entry data (with all required lines present).
|
||||
func GetOldEntryData(targetLocation string, field int, passphrase []byte) []string {
|
||||
func GetOldEntryData(targetLocation string, field int) []string {
|
||||
// ensure targetLocation exists
|
||||
TargetIsFile(targetLocation, true, 2)
|
||||
|
||||
// read old entry data
|
||||
unencryptedEntry := DecryptFileToSlice(targetLocation, passphrase)
|
||||
unencryptedEntry := DecryptFileToSlice(targetLocation)
|
||||
|
||||
// return the old entry data with all required lines present
|
||||
if field > 0 {
|
||||
|
||||
+51
-31
@@ -1,48 +1,68 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rwinkhart/rcw/daemon"
|
||||
"github.com/rwinkhart/rcw/wrappers"
|
||||
)
|
||||
|
||||
// DecryptFileToSlice decrypts an RCW wrapped file and returns the contents as a slice of (trimmed) strings.
|
||||
func DecryptFileToSlice(targetLocation string, passphrase []byte) []string {
|
||||
encBytes, err := os.ReadFile(targetLocation)
|
||||
if err != nil {
|
||||
PrintError("Failed to decrypt \""+targetLocation+"\" - "+err.Error(), ErrorDecryption, true)
|
||||
}
|
||||
decBytes, err := wrappers.Decrypt(encBytes, passphrase)
|
||||
if err != nil {
|
||||
PrintError("Failed to decrypt \""+targetLocation+"\" - "+err.Error(), ErrorDecryption, true)
|
||||
}
|
||||
return strings.Split(string(decBytes), "\n")
|
||||
}
|
||||
|
||||
// EncryptBytes encrypts a byte slice using RCW and returns the encrypted data.
|
||||
func EncryptBytes(decBytes []byte, passphrase []byte) []byte {
|
||||
encBytes := wrappers.Encrypt(decBytes, passphrase)
|
||||
return encBytes
|
||||
}
|
||||
|
||||
// LaunchRCWDProcess launches an RCW daemon to serve the given passphrase.
|
||||
func LaunchRCWDProcess(passphrase string) {
|
||||
cmd := exec.Command(os.Args[0], "startrcwd")
|
||||
writeToStdin(cmd, passphrase)
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
PrintError("Failed to launch RCW daemon - Does this libmutton implementation support the \"startrcwd\" argument?", ErrorOther, true)
|
||||
}
|
||||
}
|
||||
|
||||
// RCWDArgument reads the passphrase from stdin and serves it via an RCW daemon.
|
||||
// RCWDArgument reads the passphrase from stdin and caches it via an RCW daemon.
|
||||
func RCWDArgument() {
|
||||
passphrase := readFromStdin()
|
||||
if passphrase == "" {
|
||||
os.Exit(0)
|
||||
}
|
||||
daemon.Start(string(passphrase))
|
||||
daemon.Start([]byte(passphrase))
|
||||
}
|
||||
|
||||
// DecryptFileToSlice decrypts an RCW wrapped file and returns the contents as a slice of (trimmed) strings.
|
||||
func DecryptFileToSlice(targetLocation string) []string {
|
||||
// read encrypted file
|
||||
encBytes, err := os.ReadFile(targetLocation)
|
||||
if err != nil {
|
||||
PrintError("Failed to decrypt \""+targetLocation+"\" - "+err.Error(), ErrorDecryption, true)
|
||||
}
|
||||
|
||||
// decrypt data using RCW daemon
|
||||
launchRCWDProcess()
|
||||
return strings.Split(string(daemon.GetDec(encBytes)), "\n")
|
||||
}
|
||||
|
||||
// EncryptBytes encrypts a byte slice using RCW and returns the encrypted data.
|
||||
func EncryptBytes(decBytes []byte) []byte {
|
||||
launchRCWDProcess()
|
||||
return daemon.GetEnc(decBytes)
|
||||
}
|
||||
|
||||
// launchRCWDProcess launches an RCW daemon to cache a passphrase.
|
||||
// It returns immediately if the daemon appears to be already running.
|
||||
func launchRCWDProcess() {
|
||||
if daemon.IsOpen() {
|
||||
return
|
||||
}
|
||||
var passphrase []byte
|
||||
for {
|
||||
passphrase = GetPassphrase()
|
||||
err := wrappers.RunSanityCheck(ConfigDir+PathSeparator+"sanity.rcw", passphrase)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
fmt.Println(AnsiError + "Incorrect passphrase" + AnsiReset)
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], "startrcwd")
|
||||
writeToStdin(cmd, string(passphrase))
|
||||
cmd.Start()
|
||||
|
||||
// block until socket file is created
|
||||
for {
|
||||
if daemon.IsOpen() {
|
||||
break
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,8 +37,8 @@ func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8)
|
||||
}
|
||||
|
||||
// WriteEntry writes entryData to an encrypted file at targetLocation.
|
||||
func WriteEntry(targetLocation string, entryData []byte, passphrase []byte) {
|
||||
encryptedBytes := EncryptBytes(entryData, passphrase)
|
||||
func WriteEntry(targetLocation string, entryData []byte) {
|
||||
encryptedBytes := EncryptBytes(entryData)
|
||||
err := os.WriteFile(targetLocation, encryptedBytes, 0600)
|
||||
if err != nil {
|
||||
PrintError("Failed to write to file: "+err.Error(), ErrorWrite, true)
|
||||
|
||||
Reference in New Issue
Block a user