-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
242 lines (208 loc) · 5.34 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
"text/template"
"time"
"github.com/ProtonMail/gopenpgp/v2/helper"
)
// Constants
const (
secTextTemplate = "security.tmpl"
securityTextFileUnsigned = "security.txt.asc"
securityTextFile = "security.txt"
rsaBits = 4096
keyType = "rsa"
)
var (
configFile *string
privateKey *string
passphrase *string
generateKeys *bool
name *string
email *string
sign *bool
expDate *string
generateTmpl *bool
printSig *bool
printPlain *bool
)
type SecText struct {
Contacts []string
Expires string
Acknowledgments string
Languages string
Canonical string
Policy string
Encryption string
Hiring string
}
func init() {
configFile = flag.String("configFile", "config.json", "Configuration file for template")
privateKey = flag.String("privKey", "priv.key", "Private PGP key")
passphrase = flag.String("passphrase", "", "Passphrase for the private PGP key")
generateKeys = flag.Bool("generateKeys", false, "Generate private PGP key")
generateTmpl = flag.Bool("generateTmpl", false, "Generate sample files")
name = flag.String("name", "", "Display name for PGP key")
email = flag.String("email", "", "Email address for PGP key")
sign = flag.Bool("sign", true, "Sign security.txt with PGP")
expDate = flag.String("date", "", "Custom expires date. Format: YYYY-MM-DD (default now+1year)")
printSig = flag.Bool("printSig", false, "Print signed file to stdout")
printPlain = flag.Bool("printPlain", false, "Print unsigned file to stdout")
}
func main() {
flag.Parse()
if flag.NFlag() == 0 {
flag.Usage()
os.Exit(0)
}
if *generateTmpl {
if _, err := os.Stat(*configFile); errors.Is(err, os.ErrNotExist) {
f, err := os.Create(*configFile)
checkErr(err)
defer f.Close()
f.Write([]byte(configTemplate))
log.Println("config.json created")
}
if _, err := os.Stat(secTextTemplate); errors.Is(err, os.ErrNotExist) {
f, err := os.Create(secTextTemplate)
checkErr(err)
defer f.Close()
f.Write([]byte(securityTemplate))
log.Println("security.tmpl created")
}
}
if *generateKeys {
if err := generateKey(*name, *email, []byte(*passphrase)); err != nil {
log.Fatalln("error creating new PGP private key")
}
log.Println("Generated private PGP key:", *privateKey)
os.Exit(0)
}
file, err := os.Open(*configFile)
checkErr(err)
decoder := json.NewDecoder(file)
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatalln("Error closing file", file.Name(), err)
}
}(file)
sectext := SecText{Expires: expiresTime()}
err = decoder.Decode(§ext)
checkErr(err)
f, err := os.Create(securityTextFileUnsigned)
checkErr(err)
defer func(f *os.File) {
err := f.Close()
if err != nil {
log.Fatalln("Error closing file", file.Name(), err)
}
}(f)
t := template.Must(template.ParseFiles(secTextTemplate))
err = t.Execute(f, sectext)
checkErr(err)
if *sign && *passphrase != "" {
unsignedFile, err := os.ReadFile(securityTextFileUnsigned)
checkErr(err)
privKey, err := readPrivatekey()
checkErr(err)
a, err := helper.SignCleartextMessageArmored(string(privKey), []byte(*passphrase), string(unsignedFile))
checkErr(err)
f, err = os.Create(securityTextFile)
checkErr(err)
if err = os.WriteFile(securityTextFile, []byte(a), 0644); err != nil {
log.Fatalln(err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
log.Fatalln(err)
}
}(f)
if err = removeHeaders(); err != nil {
log.Fatalln(err)
}
}
if *printPlain {
printContent(securityTextFileUnsigned)
if !*printSig {
os.Exit(0)
}
}
if *printSig {
printContent(securityTextFile)
os.Exit(0)
}
log.Print("Security.txt file(s) generated!")
}
func expiresTime() string {
if *expDate != "" {
t, err := time.Parse("2006-01-02", *expDate)
if err != nil {
log.Fatalln("Could not parse date:", err)
}
if !t.After(time.Now()) {
log.Fatalln("The expiry date is in the past.")
}
return t.Format("2006-01-02T15:04:05.000Z07:00")
}
return time.Now().AddDate(0, 12, 0).UTC().Format("2006-01-02T15:04:05.000Z07:00")
}
func checkErr(e error) {
if e != nil {
log.Fatalln(e)
}
}
func generateKey(name string, email string, pass []byte) error {
rsaKey, err := helper.GenerateKey(name, email, pass, keyType, rsaBits)
checkErr(err)
if err := os.WriteFile(*privateKey, []byte(rsaKey), 0400); err != nil {
return err
}
return nil
}
func readPrivatekey() ([]byte, error) {
privKey, err := os.ReadFile(*privateKey)
return privKey, err
}
func removeHeaders() error {
input, err := os.ReadFile(securityTextFile)
checkErr(err)
re := regexp.MustCompile(`((?m)Version: .*|Comment: .*)`)
lines := strings.Split(string(input), "\n")
var newLines []string
for _, line := range lines {
if !re.MatchString(line) {
newLines = append(newLines, line)
}
}
output := strings.Join(newLines, "\n")
err = os.WriteFile(securityTextFile, []byte(output), 0644)
if err != nil {
return err
}
return nil
}
func printContent(f string) {
file, err := os.Open(f)
if err != nil {
log.Fatal(err)
}
defer func() {
if err = file.Close(); err != nil {
log.Fatal(err)
}
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}