package main import ( "fmt" "os" "path/filepath" "strings" "github.com/spf13/cobra" "gitlab.com/whiteboardguy/eno/internal/crud" "gitlab.com/whiteboardguy/eno/internal/crypt" "gitlab.com/whiteboardguy/eno/internal/utils" ) var cobraCmd = &cobra.Command{ Use: "eno", Short: "file encryption tool", } var encryptCmd = &cobra.Command{ Use: "encrypt ", Short: "encrypt a file", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { output, err := cmd.Flags().GetString("output") if err != nil { return fmt.Errorf("failed to get output flag: %v", err) } if output == "" { output = args[0] + ".eno" } if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { return err } if !ok { return fmt.Errorf("write aborted") } } fileBytes, err := os.ReadFile(args[0]) if err != nil { return fmt.Errorf("failed to read input file: %v", err) } crypt.TryMlock(fileBytes) defer clear(fileBytes) sshFlag, err := cmd.Flags().GetBool("ssh") if err != nil { return fmt.Errorf("failed to get ssh flag: %v", err) } if sshFlag { if Conf.DefaultSSHPubPath == "" { return fmt.Errorf("SSH not configured. Run 'eno ssh init' first") } defaultPubKeyBytes, err := os.ReadFile(Conf.DefaultSSHPubPath) if err != nil { return fmt.Errorf("failed to read default SSH public key: %v", err) } pubKeys, _, err := crud.PromptRecipientKeys(defaultPubKeyBytes) if err != nil { return err } if len(pubKeys) == 0 { return fmt.Errorf("at least one recipient public key is required") } encrypted, _, err := crypt.EncryptBytesSSH(fileBytes, pubKeys) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if err := os.WriteFile(output, encrypted, 0600); err != nil { return fmt.Errorf("failed to write output file: %v", err) } return nil } fmt.Printf("Enter a passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } crypt.TryMlock(password) defer clear(password) encrypted, err := crypt.EncryptBytesPass(fileBytes, password) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if err := os.WriteFile(output, encrypted, 0600); err != nil { return fmt.Errorf("failed to write output file: %v", err) } return nil }, } var decryptCmd = &cobra.Command{ Use: "decrypt ", Short: "decrypt a file", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { output, err := cmd.Flags().GetString("output") if err != nil { return fmt.Errorf("failed to get output flag: %v", err) } if output == "" { output = strings.TrimSuffix(args[0], ".eno") } if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { return err } if !ok { return fmt.Errorf("write aborted") } } fileBytes, err := os.ReadFile(args[0]) if err != nil { return fmt.Errorf("failed to read input file: %v", err) } authMode, err := utils.CheckFileAuthMode(args[0]) if err != nil { return fmt.Errorf("failed to check auth mode: %v", err) } if authMode == crypt.SSH { if Conf.DefaultSSHPrivPath == "" { return fmt.Errorf("SSH not configured. Run 'eno ssh init' first") } plaintext, err := crypt.DecryptBytesSSH(fileBytes, Conf.DefaultSSHPrivPath) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } crypt.TryMlock(plaintext) defer clear(plaintext) if err := os.WriteFile(output, plaintext, 0600); err != nil { return fmt.Errorf("failed to write output file: %v", err) } return nil } fmt.Printf("Enter the passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } crypt.TryMlock(password) defer clear(password) plaintext, err := crypt.DecryptBytesPass(fileBytes, password) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } crypt.TryMlock(plaintext) defer clear(plaintext) if err := os.WriteFile(output, plaintext, 0600); err != nil { return fmt.Errorf("failed to write output file: %v", err) } return nil }, } var initCmd = &cobra.Command{ Use: "init", Short: "intialize Eno config", Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { err := handlerInit() if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } cmd.Println("Config initialized at ", Conf.ConfPath) }, } var editCmd = &cobra.Command{ Use: "edit ", Short: "edit an existing encrypted note", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { target := filepath.Clean(args[0]) ok, err := utils.AskUserYesNo("Edit "+target+" ?", true) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if !ok { cmd.PrintErrln("Error: write aborted") os.Exit(1) } target = strings.TrimSuffix(target, ".eno") target += ".eno" if _, err := os.Stat(target); os.IsNotExist(err) { alt := filepath.Join(Conf.DataDir, target) if _, err2 := os.Stat(alt); err2 == nil { target = alt } } authMode, err := utils.CheckFileAuthMode(target) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } switch authMode { case crypt.SSH: err = crud.EditNoteSSH(target, Conf.DefaultSSHPrivPath, Conf.EditorString) case crypt.Pass: err = crud.EditNotePass(target, Conf.EditorString) default: cmd.PrintErrln("Error: unknown auth mode:", authMode) os.Exit(1) } if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } }, } func init() { encryptCmd.Flags().StringP("output", "o", "", "output file (required)") encryptCmd.Flags().Bool("ssh", false, "encrypt using SSH public keys (interactive)") // encryptCmd.MarkFlagRequired("output") decryptCmd.Flags().StringP("output", "o", "", "output file (required)") // decryptCmd.MarkFlagRequired("output") newCmd.Flags().StringP("output", "o", "", "output file") newCmd.Flags().BoolP("edit", "e", false, "edit after creation") newCmd.Flags().Bool("ssh", false, "use your ssh for auth") sshCmd.AddCommand(sshInitCmd) cobraCmd.AddCommand(encryptCmd, decryptCmd, initCmd, newCmd, editCmd, sshCmd) }