forked from gopasspw/gopass-jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonapi_windows.go
135 lines (111 loc) · 3.63 KB
/
jsonapi_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// +build windows
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/gopasspw/gopass-jsonapi/internal/jsonapi/manifest"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/termio"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"golang.org/x/sys/windows/registry"
)
// setup sets up manifest for gopass as native messaging host
func (s *jsonapiCLI) setup(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
browser, err := s.getBrowser(ctx, c)
if err != nil {
return fmt.Errorf("failed to get browser: %s", err)
}
globalInstall, err := s.getGlobalInstall(ctx, c)
if err != nil {
return fmt.Errorf("failed to get global flag: %s", err)
}
// Use windows specific folder to store wrapper and manifests
defaultWrapperPath := filepath.Join(os.Getenv("LOCALAPPDATA"), "gopass")
if globalInstall {
defaultWrapperPath = filepath.Join(os.Getenv("PROGRAMDATA"), "gopass")
}
wrapperPath, err := s.getWrapperPath(ctx, c, defaultWrapperPath, manifest.NativeHostExeName)
if err != nil {
return fmt.Errorf("failed to get wrapper path: %s", err)
}
wrapperFileName := filepath.Join(wrapperPath, manifest.NativeHostExeName)
manifestPath := c.String("manifest-path")
if manifestPath == "" {
manifestPath = filepath.Join(wrapperPath, browser, manifest.Name+".json")
}
regPath, err := manifest.GetRegistryPath(browser)
if err != nil {
return fmt.Errorf("failed to get registry path: %s", err)
}
_, mf, err := manifest.Render(browser, wrapperFileName, c.String("gopass-path"), globalInstall)
if err != nil {
return fmt.Errorf("failed to render manifest: %s", err)
}
if c.Bool("print") {
fmt.Printf("Native Messaging Setup Preview:\nWrapper Script (%s)\n\nManifest File (%s = %s):\n%s\n", wrapperFileName, regPath, manifestPath, string(mf))
}
if install, err := termio.AskForBool(ctx, color.BlueString("Install manifest and wrapper?"), true); err != nil || !install {
return err
}
if err := os.MkdirAll(filepath.Dir(wrapperFileName), 0755); err != nil {
return fmt.Errorf("failed to create wrapper path: %s", err)
}
if err := s.setRegistryValue(regPath, manifestPath, globalInstall); err != nil {
return fmt.Errorf("failed to set registry value: %s", err)
}
// If the calling binary has native_host.exe as suffix listener will be started.
if err := s.copyExecutionBinary(wrapperFileName); err != nil {
return fmt.Errorf("failed to copy gopass binary to wrapper path: %s", err)
}
if err := os.MkdirAll(filepath.Dir(manifestPath), 0755); err != nil {
return fmt.Errorf("failed to create manifest path: %s", err)
}
if err := ioutil.WriteFile(manifestPath, mf, 0644); err != nil {
return fmt.Errorf("failed to write manifest file: %s", err)
}
return nil
}
func (s *jsonapiCLI) setRegistryValue(path string, value string, globalInstall bool) error {
key := registry.CURRENT_USER
if globalInstall {
key = registry.LOCAL_MACHINE
}
k, err := registry.OpenKey(key, path, registry.WRITE)
if err != nil {
if err != registry.ErrNotExist {
return err
}
k, _, err = registry.CreateKey(key, path, registry.ALL_ACCESS)
if err != nil {
return err
}
}
defer k.Close()
return k.SetStringValue("", value)
}
func (s *jsonapiCLI) copyExecutionBinary(destFileName string) error {
srcFileName, err := os.Executable()
if err != nil {
return err
}
srcFile, err := os.Open(srcFileName)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.Create(destFileName) // creates if file doesn't exist
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
return destFile.Sync()
}