-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
140 lines (119 loc) · 3.69 KB
/
main.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
136
137
138
139
140
/*
OS X Call history decryptor
Copyright (C) 2016 n0fate (GPL2 license)
Copyright (C) 2019 rusq (golang implementation)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/rusq/osx-callhistory-decryptor/historydecryptor"
)
var defFile = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "CallHistoryDB", "CallHistory.storedata")
var (
strKey = flag.String("k", os.Getenv("KEY"), "Base64 key value from OS X keychain, on macOS may be omitted.")
omitDecryption = flag.Bool("no-key", false, "omit decryption")
outputFilename = flag.String("o", "", "output csv filename. If not specified, result is output to stdout")
versionOnly = flag.Bool("v", false, "print version and quit")
timeFormat = flag.String("time-format", historydecryptor.DefTimeFmt, "CSV output time `format`")
build = "v.0.0-development"
)
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [flags] [filename]\n"+
"If filename is not specified, will try to locate it at the default location\n(%s)\n"+
"The program will not modify the original file, and will operate on a copy in a\n"+
"temporary directory\n\n"+
"Flags:\n",
os.Args[0], defFile)
flag.PrintDefaults()
}
}
func printHeader() {
fmt.Fprintf(os.Stderr, "MacOS X Call History Decryptor %s © 2018-2021 rusq\n"+
"Based on Call History Decryptor © 2016 n0fate\n",
build)
}
func main() {
flag.Parse()
printHeader()
if *versionOnly {
return
}
var src string
if flag.NArg() > 0 {
src = flag.Arg(0)
} else {
src = defFile
}
if err := run(src, *outputFilename, *strKey, *omitDecryption); err != nil {
log.Fatal(err)
}
}
func run(src, dst string, strKey string, omitDecryption bool) error {
log.Printf("*** database filename: %q", src)
dbfile, err := copytemp(src)
if err != nil {
return err
}
defer os.Remove(dbfile)
log.Printf("*** temporary file (will be removed): %q", dbfile)
var key []byte
if omitDecryption {
key = []byte{}
} else {
key, err = historydecryptor.GetByteKey(strKey)
if err != nil {
return fmt.Errorf("%w: make sure you have supplied the key via -k <key> or KEY env variable", err)
}
}
var output = os.Stdout
if dst != "" && dst != "-" {
f, err := os.Create(dst)
if err != nil {
return fmt.Errorf("unable to create the output file: %w", err)
}
defer f.Close()
output = f
}
numRecords, err := historydecryptor.DecipherHistory(dbfile, key, output, historydecryptor.OptTimeFormat(*timeFormat))
if err != nil {
return err
}
log.Printf("*** finished. %d records processed\n", numRecords)
return nil
}
// copytemp copies the file to a temporary location and returns its name.
func copytemp(filename string) (string, error) {
src, err := os.Open(filename)
if err != nil {
return "", err
}
defer src.Close()
dst, err := ioutil.TempFile("", "callhistory*")
if err != nil {
return "", err
}
defer dst.Close()
if n, err := io.Copy(dst, src); err != nil {
return "", err
} else if n == 0 {
return "", fmt.Errorf("copy: %s: file has zero size", filename)
}
return dst.Name(), nil
}