/
editstate.go
540 lines (494 loc) · 13.4 KB
/
editstate.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package main
import (
"bytes"
crypto_rand "crypto/rand"
"encoding/hex"
"flag"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"os/signal"
"reflect"
"strconv"
"strings"
"syscall"
"github.com/agl/pond/client/disk"
"github.com/agl/pond/client/system"
pond "github.com/agl/pond/protos"
"github.com/golang/protobuf/proto"
"golang.org/x/crypto/ssh/terminal"
)
var (
stateFileName *string = flag.String("state-file", "state", "File in which to save persistent state")
skipChecks *bool = flag.Bool("skip-checks", false, "If true, system sanity checks are skipped")
)
func main() {
flag.Parse()
if !do() {
os.Exit(1)
}
}
func serialise(out io.Writer, state *disk.State) map[uint32][]byte {
entities := make(map[uint32][]byte)
serialiseStruct(out, reflect.ValueOf(*state), reflect.TypeOf(*state), "", 0, entities)
return entities
}
func serialiseStruct(out io.Writer, v reflect.Value, t reflect.Type, context string, level uint, entities map[uint32][]byte) {
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Name == "XXX_unrecognized" {
continue
}
fv := v.Field(i)
switch f.Type.Kind() {
case reflect.Slice:
if f.Type.Elem().Kind() == reflect.Uint8 {
serialiseValue(out, f.Name, fv, f.Type, context, level, entities)
} else {
if f.Type.Elem().Kind() == reflect.Ptr {
for i := 0; i < fv.Len(); i++ {
serialiseValue(out, f.Name, fv.Index(i).Elem(), f.Type.Elem().Elem(), context, level, entities)
}
} else {
for i := 0; i < fv.Len(); i++ {
serialiseValue(out, f.Name, fv.Index(i), f.Type.Elem(), context, level, entities)
}
}
}
case reflect.Ptr:
if !fv.IsNil() {
serialiseValue(out, f.Name, fv.Elem(), f.Type.Elem(), context, level, entities)
}
default:
panic(fmt.Sprintf("Don't know how to serialize %s", f))
}
}
}
func escapeString(in string) string {
return strings.Replace(strings.Replace(in, "\\", "\\\\", -1), "\"", "\\\"", -1)
}
var valueSep = []byte(": ")
var structSep = []byte(" <\n")
var structEnd = []byte(">")
var levelMark = []byte("\t")
func contextAppend(context, extra string) string {
if len(context) > 0 {
context += "."
}
context += extra
return context
}
func setEntity(entities map[uint32][]byte, data []byte) uint32 {
table := crc32.MakeTable(crc32.Castagnoli)
crc := crc32.Checksum(data, table)
for {
other, ok := entities[crc]
if !ok {
entities[crc] = data
return crc
}
if bytes.Equal(other, data) {
return crc
}
crc++
}
panic("unreachable")
}
func serialiseValue(out io.Writer, name string, v reflect.Value, t reflect.Type, context string, level uint, entities map[uint32][]byte) {
if t.Kind() == reflect.Slice && (context == "Outbox" || context == "Inbox") && name == "Message" {
var msg pond.Message
if err := proto.Unmarshal(v.Bytes(), &msg); err != nil {
fmt.Fprintf(os.Stderr, "Failed to unmarshal Message: %s\n", err)
fmt.Fprintf(out, "BAD MESSAGE: \"%x\"\n", v.Bytes())
return
}
v = reflect.ValueOf(msg)
serialiseValue(out, name, v, v.Type(), context, level, entities)
return
}
if t.Kind() == reflect.Slice && (context == "Inbox.Message" || context == "Outbox.Message") && name == "Body" {
s := string(v.Bytes())
v = reflect.ValueOf(s)
t = v.Type()
}
for i := uint(0); i < level; i++ {
out.Write(levelMark)
}
out.Write([]byte(name))
switch t.Kind() {
case reflect.Slice:
// This must be a byte slice.
out.Write(valueSep)
if context == "Outbox" && name == "Request" ||
context == "Outbox.Message.Files" && name == "Contents" ||
context == "Inbox.Message.Files" && name == "Contents" {
entityName := setEntity(entities, v.Bytes())
fmt.Fprintf(out, "<%x>", entityName)
} else {
raw := v.Bytes()
if len(raw) == 0 {
out.Write([]byte("\"\""))
} else {
encoded := make([]byte, hex.EncodedLen(len(raw)))
hex.Encode(encoded, raw)
out.Write(encoded)
}
}
case reflect.Bool:
out.Write(valueSep)
fmt.Fprintf(out, "%t", v.Bool())
case reflect.String:
s := v.String()
out.Write(valueSep)
if strings.ContainsRune(s, '\n') || name == "Body" {
delim := rand.Int63()
fmt.Fprintf(out, "<<%x\n", delim)
out.Write([]byte(s))
fmt.Fprintf(out, "--%x", delim)
} else {
fmt.Fprintf(out, "\"%s\"", escapeString(v.String()))
}
case reflect.Uint32, reflect.Uint64:
out.Write(valueSep)
fmt.Fprintf(out, "%d", v.Uint())
case reflect.Int32, reflect.Int64:
out.Write(valueSep)
fmt.Fprintf(out, "%d", v.Int())
case reflect.Struct:
out.Write(structSep)
serialiseStruct(out, v, t, contextAppend(context, name), level+1, entities)
for i := uint(0); i < level; i++ {
out.Write(levelMark)
}
out.Write(structEnd)
default:
panic(fmt.Sprintf("Don't know how to serialise a %s", t))
}
fmt.Fprintf(out, "\n")
}
func parse(state *disk.State, in io.Reader, entities map[uint32][]byte) error {
tokenizer := NewTokenizer(in)
v := reflect.ValueOf(state).Elem()
t := reflect.TypeOf(state).Elem()
for {
fieldName, err := tokenizer.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := parseStructField(v, t, "", fieldName, tokenizer, entities); err != nil {
return err
}
}
return nil
}
func parseStruct(v reflect.Value, t reflect.Type, context string, in *Tokenizer, entities map[uint32][]byte) error {
for {
fieldName, err := in.Next()
if err != nil {
return err
}
if fieldName == ">" {
return nil
}
if err := parseStructField(v, t, context, fieldName, in, entities); err != nil {
return err
}
}
panic("unreachable")
}
func parseStructField(v reflect.Value, t reflect.Type, context, fieldName string, in *Tokenizer, entities map[uint32][]byte) error {
f, ok := t.FieldByName(fieldName)
if !ok {
return fmt.Errorf("line %d: unknown field '%s'", in.Line, fieldName)
}
fv := v.FieldByName(fieldName)
sep, err := in.Next()
if err != nil {
return err
}
fieldIsProtobuf := false
var protobufType reflect.Type
switch f.Type.Kind() {
case reflect.Ptr:
fieldIsProtobuf = f.Type.Elem().Kind() == reflect.Struct
if fieldIsProtobuf {
protobufType = f.Type.Elem()
}
case reflect.Slice:
fieldIsProtobuf = f.Type.Elem().Kind() == reflect.Ptr &&
f.Type.Elem().Elem().Kind() == reflect.Struct
if fieldIsProtobuf {
protobufType = f.Type.Elem().Elem()
}
}
switch sep {
case "<":
// Must be a protocol buffer or a slice of them.
isSerialized := false
if (context == "Inbox" || context == "Outbox") && fieldName == "Message" {
// These aren't protobufs in the structure - they need
// to be written as a []byte.
isSerialized = true
var msg pond.Message
protobufType = reflect.TypeOf(msg)
} else {
if !fieldIsProtobuf {
return fmt.Errorf("line %d: field %s is not a protobuf, it's a %s", in.Line, fieldName, f.Type)
}
}
value := reflect.New(protobufType)
if err := parseStruct(value.Elem(), value.Type().Elem(), contextAppend(context, fieldName), in, entities); err != nil {
return err
}
if isSerialized {
serialized, err := proto.Marshal(value.Interface().(proto.Message))
if err != nil {
return fmt.Errorf("line %d: error serialising protobuf: %s", in.Line, err)
}
fv.SetBytes(serialized)
} else {
switch f.Type.Kind() {
case reflect.Ptr:
fv.Set(value)
case reflect.Slice:
fv.Set(reflect.Append(fv, value))
default:
panic("impossible")
}
}
case ":":
if fieldIsProtobuf {
return fmt.Errorf("line %d: field is protobuf, but found ':'", in.Line)
}
if err := parseValue(fv, f, contextAppend(context, fieldName), in, entities); err != nil {
return err
}
default:
return fmt.Errorf("line %d: unexpected '%s'", in.Line, sep)
}
return nil
}
func parseValue(v reflect.Value, t reflect.StructField, context string, in *Tokenizer, entities map[uint32][]byte) error {
token, err := in.Next()
if err != nil {
return err
}
switch t.Type.Kind() {
case reflect.Ptr:
switch t.Type.Elem().Kind() {
case reflect.String:
s := reflect.New(t.Type.Elem())
s.Elem().SetString(token)
v.Set(s)
case reflect.Uint32:
value, err := strconv.ParseUint(token, 10, 32)
if err != nil {
return fmt.Errorf("line %d: cannot parse uint32: %s", in.Line, err)
}
i := reflect.New(t.Type.Elem())
i.Elem().SetUint(value)
v.Set(i)
case reflect.Uint64:
value, err := strconv.ParseUint(token, 10, 64)
if err != nil {
return fmt.Errorf("line %d: cannot parse uint64: %s", in.Line, err)
}
i := reflect.New(t.Type.Elem())
i.Elem().SetUint(value)
v.Set(i)
case reflect.Int32:
value, err := strconv.ParseInt(token, 10, 32)
if err != nil {
return fmt.Errorf("line %d: cannot parse int32: %s", in.Line, err)
}
i := reflect.New(t.Type.Elem())
i.Elem().SetInt(value)
v.Set(i)
case reflect.Int64:
value, err := strconv.ParseInt(token, 10, 64)
if err != nil {
return fmt.Errorf("line %d: cannot parse int64: %s", in.Line, err)
}
i := reflect.New(t.Type.Elem())
i.Elem().SetInt(value)
v.Set(i)
case reflect.Bool:
b := reflect.New(t.Type.Elem())
switch token {
case "true":
b.Elem().SetBool(true)
case "false":
b.Elem().SetBool(false)
default:
return fmt.Errorf("line %d: boolean values must be 'true' or 'false', not %s", in.Line, token)
}
v.Set(b)
default:
return fmt.Errorf("line %d: unhandled type: pointer to %s", in.Line, t.Type.Elem())
}
case reflect.Slice:
switch t.Type.Elem().Kind() {
case reflect.Uint8:
var value []byte
alwaysSet := false
if token == "<" {
entityToken, err := in.Next()
if err != nil {
return err
}
entity, err := strconv.ParseUint(entityToken, 16, 32)
if err != nil {
return fmt.Errorf("line %d: failed to parse entity token: %s", in.Line, err)
}
endToken, err := in.Next()
if err != nil {
return err
}
if endToken != ">" {
return fmt.Errorf("line %d: entity should have ended with '>'", in.Line)
}
var ok bool
value, ok = entities[uint32(entity)]
if !ok {
return fmt.Errorf("line %d: unknown entity id", in.Line)
}
} else if context == "Inbox.Message.Body" || context == "Outbox.Message.Body" {
value = []byte(token)
alwaysSet = true
} else {
value, err = hex.DecodeString(token)
if err != nil {
return fmt.Errorf("line %d: failed to parse hex value: %s", in.Line, err)
}
}
if len(value) > 0 || alwaysSet {
v.SetBytes(value)
}
default:
return fmt.Errorf("line %d: unhandled type: slice of %s", in.Line, t.Type.Elem())
}
default:
return fmt.Errorf("line %d: unhandled type %s", in.Line, t.Type)
}
return nil
}
func do() bool {
if !*skipChecks {
if err := system.IsSafe(); err != nil {
fmt.Fprintf(os.Stderr, "System checks failed: %s\n", err)
return false
}
}
editor := os.Getenv("EDITOR")
if len(editor) == 0 {
fmt.Fprintf(os.Stderr, "$EDITOR is not set\n")
return false
}
stateFile := &disk.StateFile{
Path: *stateFileName,
Rand: crypto_rand.Reader,
Log: func(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
},
}
stateLock, err := stateFile.Lock(false /* don't create */)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot open state file: %s\n", err)
return false
}
if stateLock == nil {
fmt.Fprintf(os.Stderr, "Cannot obtain lock on state file\n")
return false
}
defer stateLock.Close()
var state *disk.State
var passphrase string
for {
state, err = stateFile.Read(passphrase)
if err == nil {
break
}
if err != disk.BadPasswordError {
fmt.Fprintf(os.Stderr, "Failed to decrypt state file: %s\n", err)
return false
}
fmt.Fprintf(os.Stderr, "Passphrase: ")
passphraseBytes, err := terminal.ReadPassword(0)
fmt.Fprintf(os.Stderr, "\n")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read password\n")
return false
}
passphrase = string(passphraseBytes)
}
tempDir, err := system.SafeTempDir()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get safe temp directory: %s\n", err)
return false
}
tempFile, err := ioutil.TempFile(tempDir, "pond-editstate-")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create temp file: %s\n", err)
return false
}
tempFileName := tempFile.Name()
defer func() {
os.Remove(tempFileName)
}()
signals := make(chan os.Signal, 8)
signal.Notify(signals, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signals
println("Caught signal: removing", tempFileName)
os.Remove(tempFileName)
os.Exit(1)
}()
entities := serialise(tempFile, state)
var newStateSerialized []byte
for {
cmd := exec.Command(editor, tempFileName)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to run editor: %s\n", err)
return false
}
tempFile.Close()
tempFile, err := os.Open(tempFileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open temp file: %s\n", err)
return false
}
newState := new(disk.State)
err = parse(newState, tempFile, entities)
if err == nil {
newStateSerialized, err = proto.Marshal(newState)
}
if err == nil {
break
}
fmt.Fprintf(os.Stderr, "Error parsing: %s\n", err)
fmt.Fprintf(os.Stderr, "Hit enter to edit again, or Ctrl-C to abort\n")
var buf [100]byte
os.Stdin.Read(buf[:])
}
states := make(chan disk.NewState)
done := make(chan struct{})
go stateFile.StartWriter(states, done)
states <- disk.NewState{
State: newStateSerialized,
RotateErasureStorage: false,
Destruct: false,
}
close(states)
<-done
return true
}