/
load.go
209 lines (187 loc) · 5.35 KB
/
load.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
/*
PRLPKS - OpenPGP Synchronized Key Server with Deletion
Copyright (c) 2014 Pruthvirajsinh Rajendrasinh Chauhan
PRLPKS is based heavily on hockeypuck(https://launchpad.net/hockeypuck) by Casey Marshall, copyright 2013(GNU GPL v3).
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.
*/
// prlpks is an OpenPGP keyserver.
package main
import (
"encoding/hex"
"fmt"
"github.com/lib/pq"
"github.com/pruthvirajsinh/symflux"
"github.com/pruthvirajsinh/symflux/recon"
"launchpad.net/gnuflag"
"log"
"os"
"path/filepath"
// "strings"
. "github.com/pruthvirajsinh/prlpks"
"github.com/pruthvirajsinh/prlpks/openpgp"
)
type loadCmd struct {
configuredCmd
path string
txnSize int
ignoreDups bool
db *openpgp.DB
l *openpgp.Loader
ptree recon.PrefixTree
nkeys int
inTransaction bool
//PRC Start
totalKeyLimit int
//PRC End
}
func (ec *loadCmd) Name() string { return "load" }
func (ec *loadCmd) Desc() string { return "Load OpenPGP keyring files into database" }
func newLoadCmd() *loadCmd {
cmd := new(loadCmd)
flags := gnuflag.NewFlagSet(cmd.Name(), gnuflag.ExitOnError)
flags.StringVar(&cmd.configPath, "config", "", "prlpks configuration file")
flags.StringVar(&cmd.path, "path", "", "OpenPGP keyring file path or glob pattern")
flags.IntVar(&cmd.txnSize, "txn-size", 5000, "Transaction size; public keys per commit")
flags.BoolVar(&cmd.ignoreDups, "ignore-dups", false, "Ignore duplicate entries")
flags.IntVar(&cmd.totalKeyLimit, "totalKeys", 0, "Limit the number of keys per load. 0 = all keys")
cmd.flags = flags
return cmd
}
func (ec *loadCmd) Main() {
if ec.path == "" {
Usage(ec, "--path is required")
}
if ec.txnSize < 1 {
Usage(ec, "Invalid --txn-size, must be >= 1")
}
if ec.totalKeyLimit < 0 {
Usage(ec, "Please enter positive total Keys")
} else {
fmt.Println("Starting loading of ", ec.totalKeyLimit, " keys")
}
ec.configuredCmd.Main()
InitLog()
var err error
if ec.db, err = openpgp.NewDB(); err != nil {
die(err)
}
ec.l = openpgp.NewLoader(ec.db, true)
// Ensure tables all exist
if err = ec.db.CreateTables(); err != nil {
die(err)
}
reconSettings := recon.NewSettings(openpgp.Config().Settings.TomlTree)
if ec.ptree, err = openpgp.NewSksPTree(reconSettings); err != nil {
die(err)
}
// Create the prefix tree (if not exists)
if err = ec.ptree.Create(); err != nil {
die(fmt.Errorf("Unable to create prefix tree: %v", err))
}
// Ensure tables all exist
if err = ec.db.CreateTables(); err != nil {
die(fmt.Errorf("Unable to create database tables: %v", err))
}
// Load all keys from input material
ec.loadAllKeys(ec.path)
// Close the prefix tree
if err = ec.ptree.Close(); err != nil {
log.Println("Close ptree:", err)
}
// Close the database connection
if err = ec.db.Close(); err != nil {
log.Println("Close database:", err)
}
}
func (ec *loadCmd) flushDb() {
if ec.inTransaction {
log.Println("Loaded", ec.nkeys, "keys")
if err := ec.l.Commit(); err != nil {
die(fmt.Errorf("Error committing transaction: %v", err))
}
ec.inTransaction = false
ec.nkeys = 0
}
}
func (ec *loadCmd) insertKey(keyRead *openpgp.ReadKeyResult) error {
var err error
if ec.nkeys%ec.txnSize == 0 {
ec.flushDb()
if _, err = ec.l.Begin(); err != nil {
die(fmt.Errorf("Error starting new transaction: %v", err))
}
ec.inTransaction = true
}
// Load key into relational database
if err = ec.l.InsertKey(keyRead.Pubkey); err != nil {
log.Println("Error inserting key:", keyRead.Pubkey.Fingerprint(), ":", err)
if _, is := err.(pq.Error); is {
die(fmt.Errorf("Unable to load due to database errors."))
}
}
ec.nkeys++
return err
}
func (ec *loadCmd) loadAllKeys(path string) {
keyfiles, err := filepath.Glob(path)
if err != nil {
die(err)
}
limit := ec.totalKeyLimit
for _, keyfile := range keyfiles {
var f *os.File
if f, err = os.Open(keyfile); err != nil {
log.Println("Failed to open", keyfile, ":", err)
continue
}
defer f.Close()
log.Println("Loading keys from", keyfile)
defer ec.flushDb()
for keyRead := range openpgp.ReadKeys(f) {
if keyRead.Error != nil {
log.Println("Error reading key:", keyRead.Error)
continue
}
//PRC Start
//Only load Keys which are in our own authority
if keyRead.Pubkey == nil {
fmt.Println("no Pub Key found")
continue
}
underAuth := openpgp.IsUnderAuth(*keyRead.Pubkey)
if underAuth != nil {
//fmt.Println("Load : " + underAuth.Error())
continue
}
//PRC End
digest, err := hex.DecodeString(keyRead.Pubkey.Md5)
if err != nil {
log.Println("bad digest:", keyRead.Pubkey.Md5)
continue
}
digest = recon.PadSksElement(digest)
digestZp := symflux.Zb(symflux.P_SKS, digest)
err = ec.ptree.Insert(digestZp)
if err != nil {
log.Println("Error inserting digest ", keyRead.Pubkey.Md5, ":", err)
continue
}
err = ec.insertKey(keyRead)
if err != nil {
log.Println("Error inserting key", keyRead.Pubkey.Md5, "into database:", err)
// Attempt to remove digest from ptree, since it was not successfully loaded
ec.ptree.Remove(digestZp)
continue
}
if ec.totalKeyLimit != 0 {
limit--
if limit == 0 {
fmt.Println("Loaded ", ec.totalKeyLimit, " keys")
return
}
}
}
}
}