-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwsafetools.go
executable file
·95 lines (88 loc) · 2.24 KB
/
pwsafetools.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
package pwsafetools
import(
"fmt"
"log"
"os"
"bufio"
"strings"
"errors"
)
type pwrec struct {
title string
user string
password string
url string
email string
}
// Convert a pwSafe export file into a file ready to be used by mempass.
// Returns an error if not successful (i.e. error occurs) otherwise nil.
func ConvPwsafeExportFile(inpath string, outpath string) error {
recs, rErr := ReadPwsafeExportFile(inpath)
if rErr != nil {
return rErr
}
var allStrs []string
for _, r := range recs { // Construct string for each record
// Add to the slice of strings
rstr := r.title
if r.url != "" {
rstr += fmt.Sprintf(" (%s)", r.url)
}
if r.email != "" && r.email != r.user {
rstr += fmt.Sprintf("\nEmail: %s", r.email)
}
rstr += fmt.Sprintf("\nBenutzer: %s", r.user)
rstr += fmt.Sprintf("\nPasswort: %s", r.password)
allStrs = append(allStrs, rstr) // Add record's string to slice allStrs
}
// Concatenate strings representing all records to a string
s := strings.Join(allStrs, "\n\n") + "\n"
err := os.WriteFile(outpath, []byte(s), 0600);
if err != nil {
return err
}
fmt.Println("Input for Mempass file generation ready at", outpath)
return nil
}
// Read a tab-delimited "pwSafe" export file and
// return a slice of pwrec elements.
func ReadPwsafeExportFile(path string) ([]pwrec, error) {
f, ferr := os.Open(path)
if ferr != nil {
return nil, ferr
}
defer f.Close()
scanner := bufio.NewScanner(f)
// result := ""
var result []pwrec
for scanner.Scan() {
l := scanner.Text()
rec, err := convPwsafeExportLine(l)
if err != nil {
return nil, err
} else {
result = append(result, rec)
}
}
return result, nil
}
// Convert a line from a "pwSafe" export file to a pwrec struct.
func convPwsafeExportLine(l string) (pwrec, error) {
f := strings.Split(l, "\t") // All fields in that line
if len(f) < 12 {
log.Print("Not a complete record:\n", l)
return pwrec{}, errors.New("Not a proper record:\n" + l)
} else {
title := f[0]
user := f[1]
pass := f[2]
url := f[3]
mail := f[10]
// Cut off group name (pwSafe folder name) from title:
if (strings.Contains(title, ".")) {
title = strings.Split(title, ".")[1]
}
rec := pwrec{title, user, pass, url, mail}
return rec, nil
}
}