forked from johto/allas
/
frontendConnection.go
705 lines (620 loc) · 17.7 KB
/
frontendConnection.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
package main
import (
fbbuf "github.com/uhoh-itsmaciek/femebe/buf"
fbcore "github.com/uhoh-itsmaciek/femebe/core"
fbproto "github.com/uhoh-itsmaciek/femebe/proto"
"github.com/johto/notifyutils/notifydispatcher"
"github.com/lib/pq"
"bytes"
"bufio"
"crypto/rand"
"errors"
"fmt"
"io"
"net"
"sync"
)
var (
errGracefulTermination = errors.New("graceful termination")
errClientCouldNotKeepUp = errors.New("client could not keep up")
errLostServerConnection = errors.New("lost server connection")
)
// QueryResult + Sync (yes/no)
type queryResultSync struct {
Result QueryResult
Sync bool
}
type frontendConnectionIO struct {
// Reader reads directly from the wrapped Conn
io.Reader
// Writing is buffered to avoid superfluous system calls
io.Writer
c net.Conn
bufw *bufio.Writer
}
func (fcio *frontendConnectionIO) Read(p []byte) (n int, err error) {
return fcio.c.Read(p)
}
func (fcio *frontendConnectionIO) Write(p []byte) (n int, err error) {
return fcio.bufw.Write(p)
}
func (fcio *frontendConnectionIO) Flush() error {
return fcio.bufw.Flush()
}
func (fcio *frontendConnectionIO) Close() error {
return fcio.c.Close()
}
type FrontendConnection struct {
// immutable
remoteAddr string
stream *fbcore.MessageStream
dispatcher *notifydispatcher.NotifyDispatcher
connStatusNotifier chan struct{}
notify chan *pq.Notification
queryResultCh chan queryResultSync
// owned by queryProcessingMainLoop until queryResultCh has been closed
listenChannels map[string]struct{}
lock sync.Mutex
err error
}
func initFatalMessage(message *fbcore.Message, sqlstate, errorMessage string) {
buf := &bytes.Buffer{}
buf.WriteByte('S')
fbbuf.WriteCString(buf, "FATAL")
buf.WriteByte('C')
fbbuf.WriteCString(buf, sqlstate)
buf.WriteByte('M')
fbbuf.WriteCString(buf, errorMessage)
buf.WriteByte('\x00')
message.InitFromBytes(fbproto.MsgErrorResponseE, buf.Bytes())
}
func RejectFrontendConnection(c net.Conn) {
var message fbcore.Message
initFatalMessage(&message, "57A01", "no server connection available")
_, _ = message.WriteTo(c)
_ = c.Close()
}
func (c FrontendConnection) String() string {
return c.remoteAddr
}
func NewFrontendConnection(c net.Conn, dispatcher *notifydispatcher.NotifyDispatcher, connStatusNotifier chan struct{}) *FrontendConnection {
io := &frontendConnectionIO{
c: c,
bufw: bufio.NewWriterSize(c, 128),
}
fc := &FrontendConnection{
remoteAddr: c.RemoteAddr().String(),
stream: fbcore.NewFrontendStream(io),
dispatcher: dispatcher,
connStatusNotifier: connStatusNotifier,
notify: make(chan *pq.Notification, 32),
queryResultCh: make(chan queryResultSync, 8),
listenChannels: make(map[string]struct{}),
}
return fc
}
func (c *FrontendConnection) WriteAndFlush(msg *fbcore.Message) error {
err := c.WriteMessage(msg)
if err != nil {
return err
}
return c.FlushStream()
}
func (c *FrontendConnection) fatal(err error) {
c.setSessionError(err)
var sqlstate, errorMessage string
switch err {
case errLostServerConnection:
sqlstate = "57A02"
errorMessage = "terminating connection because the server connection was lost"
case errClientCouldNotKeepUp:
sqlstate = "57A03"
errorMessage = "terminating connection because the client could not keep up"
default:
panic(err)
}
var message fbcore.Message
initFatalMessage(&message, sqlstate, errorMessage)
// We don't care about the errors at this point; the connection might be
// gone already.
_ = c.WriteAndFlush(&message)
}
func (c *FrontendConnection) auth(dbcfg VirtualDatabaseConfiguration, sm *fbproto.StartupMessage) bool {
authFailed := func(sqlstate, format string, v ...interface{}) bool {
var msg fbcore.Message
message := fmt.Sprintf(format, v...)
initFatalMessage(&msg, sqlstate, message)
_ = c.WriteMessage(&msg)
_ = c.FlushStream()
return false
}
username, ok := sm.Params["user"]
if !ok {
return authFailed("08P01", `required startup parameter "user" nor present in startup packet`)
}
dbname, ok := sm.Params["database"]
if !ok {
dbname = username
}
authMethod, ok := dbcfg.FindDatabase(dbname)
if !ok {
return authFailed("3D000", "database %q does not exist", dbname)
}
switch authMethod {
case "trust":
return true
case "md5":
// handled below
default:
elog.Errorf("unrecognized authentication method %q", authMethod)
return authFailed("XX000", "internal error")
}
salt := make([]byte, 4)
_, err := rand.Read(salt)
if err != nil {
elog.Errorf("could not generate random salt: %s", err)
return authFailed("XX000", "internal error")
}
var msg fbcore.Message
buf := &bytes.Buffer{}
fbbuf.WriteInt32(buf, 5)
buf.Write(salt)
msg.InitFromBytes(fbproto.MsgAuthenticationMD5PasswordR, buf.Bytes())
err = c.WriteAndFlush(&msg)
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
err = c.stream.Next(&msg)
if err == io.EOF {
elog.Debugf("EOF during startup sequence")
return false
} else if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
if msg.MsgType() != fbproto.MsgPasswordMessageP {
return authFailed("08P01", "unexpected response %x", msg.MsgType())
}
// don't bother with messages which are clearly too big
if msg.Size() > 100 {
return authFailed("28001", "password authentication failed for user %q", username)
}
password, err := msg.Force()
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
success, err := dbcfg.MD5Auth(dbname, username, salt, password)
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
if !success {
return authFailed("28001", "password authentication failed for user %q", username)
}
return true
}
func (c *FrontendConnection) startup(startupParameters map[string]string, dbcfg VirtualDatabaseConfiguration) bool {
var message fbcore.Message
var err error
for {
err = c.stream.Next(&message)
if err != nil {
elog.Logf("error while reading startup packet: %s", err)
return false
}
if fbproto.IsStartupMessage(&message) {
break
} else if fbproto.IsSSLRequest(&message) {
_, err = message.Force()
if err != nil {
elog.Logf("error while reading SSLRequest: %s", err)
return false
}
err = c.stream.SendSSLRequestResponse(fbcore.RejectSSLRequest)
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
err = c.FlushStream()
if err != nil {
elog.Logf("error during startup sequence: %s", err)
}
} else if fbproto.IsCancelRequest(&message) {
_ = c.stream.Close()
return false
} else {
elog.Warningf("unrecognized frontend message type 0x%x during startup", message.MsgType())
return false
}
}
sm, err := fbproto.ReadStartupMessage(&message)
if err != nil {
elog.Logf("error while reading startup packet: %s", err)
return false
}
if !c.auth(dbcfg, sm) {
// error already logged
_ = c.stream.Close()
return false
}
fbproto.InitAuthenticationOk(&message)
err = c.WriteMessage(&message)
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
for k, v := range startupParameters {
buf := &bytes.Buffer{}
fbbuf.WriteCString(buf, k)
fbbuf.WriteCString(buf, v)
message.InitFromBytes(fbproto.MsgParameterStatusS, buf.Bytes())
err = c.WriteMessage(&message)
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
}
fbproto.InitReadyForQuery(&message, fbproto.RfqIdle)
err = c.WriteMessage(&message)
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
err = c.FlushStream()
if err != nil {
elog.Logf("error during startup sequence: %s", err)
return false
}
return true
}
func (c *FrontendConnection) sendReadyForQuery() error {
var message fbcore.Message
fbproto.InitReadyForQuery(&message, fbproto.RfqIdle)
err := c.WriteMessage(&message)
if err != nil {
return err
}
return c.FlushStream()
}
func (c *FrontendConnection) WriteMessage(msg *fbcore.Message) error {
return c.stream.Send(msg)
}
func (c *FrontendConnection) FlushStream() error {
return c.stream.Flush()
}
// Implements Frontend.Listen.
func (c *FrontendConnection) Listen(channel string) error {
c.listenChannels[channel] = struct{}{}
err := c.dispatcher.Listen(channel, c.notify)
if err != nil && err != notifydispatcher.ErrChannelAlreadyActive {
return err
}
return nil
}
// Implements Frontend.Unlisten.
func (c *FrontendConnection) Unlisten(channel string) error {
delete(c.listenChannels, channel)
err := c.dispatcher.Unlisten(channel, c.notify)
if err != nil && err != notifydispatcher.ErrChannelNotActive {
return err
}
return nil
}
// Implements Frontend.UnlistenAll.
func (c *FrontendConnection) UnlistenAll() error {
var firstErr error
for channel := range c.listenChannels {
err := c.Unlisten(channel)
if err != nil && firstErr == nil {
firstErr = err
}
}
c.listenChannels = make(map[string]struct{})
return firstErr
}
func (c *FrontendConnection) readParseMessage(msg *fbcore.Message) (queryString string, err error) {
statementName, err := fbbuf.ReadCString(msg.Payload())
if err != nil {
return "", err
}
if statementName != "" {
return "", fmt.Errorf("attempted to use statement name %q; only unnamed statements are supported")
}
queryString, err = fbbuf.ReadCString(msg.Payload())
if err != nil {
return "", err
}
numParamTypes, err := fbbuf.ReadInt16(msg.Payload())
if err != nil {
return "", err
}
if numParamTypes != 0 {
return "", fmt.Errorf("attempted to prepare a statement with %d param types", numParamTypes)
}
// TODO: ensure we're at the end of the packet
return queryString, nil
}
func (c *FrontendConnection) readExecuteMessage(msg *fbcore.Message) error {
statementName, err := fbbuf.ReadCString(msg.Payload())
if err != nil {
return err
}
if statementName != "" {
return fmt.Errorf("attempted to use statement name %q; only unnamed statements are supported")
}
// ignore maxRowCount
_, err = fbbuf.ReadInt32(msg.Payload())
// TODO: ensure we're at the end of the packet
return err
}
func (c *FrontendConnection) readDescribeMessage(msg *fbcore.Message) (byte, error) {
typ, err := fbbuf.ReadByte(msg.Payload())
if err != nil {
return 0, err
}
if typ != 'S' && typ != 'P' {
return 0, fmt.Errorf("invalid type %q", typ)
}
statementName, err := fbbuf.ReadCString(msg.Payload())
if err != nil {
return 0, err
}
if statementName != "" {
return 0, fmt.Errorf("tried to use statement/portal name %q; only unnamed statements and portals are supported")
}
// TODO: ensure we're at the end of the packet
return typ, nil
}
func (c *FrontendConnection) readBindMessage(msg *fbcore.Message) error {
portalName, err := fbbuf.ReadCString(msg.Payload())
if err != nil {
return err
}
if portalName != "" {
return fmt.Errorf("attempted to bind to a named portal %q; only the unnamed portal is supported")
}
statementName, err := fbbuf.ReadCString(msg.Payload())
if err != nil {
return err
}
if statementName != "" {
return fmt.Errorf("attempted to bind statement %q, even though it has not been parsed yet", statementName)
}
numParamFormats, err := fbbuf.ReadInt16(msg.Payload())
if err != nil {
return err
}
if numParamFormats != 0 {
return fmt.Errorf("the number of parameter formats (%d) does not match the number of parameters in the query (0)", numParamFormats)
}
numParameters, err := fbbuf.ReadInt16(msg.Payload())
if err != nil {
return err
}
if numParameters != 0 {
return fmt.Errorf("the number of parameters provided by the client (%d) does not match the number of parameters in the query (0)", numParameters)
}
// TODO: ensure we're at the end of the packet
return nil
}
func (c *FrontendConnection) discardUntilSync() error {
var message fbcore.Message
for {
err := c.stream.Next(&message)
if err != nil {
return err
}
switch message.MsgType() {
case fbproto.MsgSyncS:
_, err = message.Force()
return nil
default:
_, err = message.Force()
}
if err != nil {
return err
}
}
}
// This is the main loop for processing messages from the frontend. Note that
// we must *never* send anything directly to the connection; all communication
// must go through queryResultCh. We're also not responsible for doing any
// cleanup in any case; that'll all be handled by mainLoop.
func (c *FrontendConnection) queryProcessingMainLoop() {
var unnamedStatement FrontendQuery
var queryResult QueryResult
var sendReadyForQuery bool
sessionLoop:
for {
var message fbcore.Message
err := c.stream.Next(&message)
if err != nil {
c.setSessionError(err)
break sessionLoop
}
queryResult = nil
sendReadyForQuery = false
switch message.MsgType() {
case fbproto.MsgParseP:
queryString, err := c.readParseMessage(&message)
if err != nil {
c.setSessionError(err)
break sessionLoop
}
unnamedStatement, err = ParseQuery(queryString)
if err != nil {
queryResult = NewErrorResponse("42601", err.Error())
c.queryResultCh <- queryResultSync{queryResult, false}
err = c.discardUntilSync()
if err != nil {
c.setSessionError(err)
break sessionLoop
}
queryResult = NewNopResponder()
sendReadyForQuery = true
} else {
queryResult = NewParseComplete()
sendReadyForQuery = false
}
case fbproto.MsgExecuteE:
err = c.readExecuteMessage(&message)
if unnamedStatement == nil {
err = fmt.Errorf("attempted to execute the unnamed prepared statement when one does not exist")
c.setSessionError(err)
break sessionLoop
}
queryResult, err = unnamedStatement.Process(c)
if err != nil {
c.setSessionError(err)
break sessionLoop
}
sendReadyForQuery = false
// Disallow reuse; not exactly following the protocol to the letter,
// but apps reusing the unnamed statement should not exist, either.
unnamedStatement = nil
case fbproto.MsgDescribeD:
_, err = c.readDescribeMessage(&message)
if err != nil {
c.setSessionError(err)
break sessionLoop
}
if unnamedStatement == nil {
err = fmt.Errorf("attempted to describe the unnamed prepared statement when one does not exist")
c.setSessionError(err)
break sessionLoop
}
queryResult = unnamedStatement.Describe()
sendReadyForQuery = false
case fbproto.MsgBindB:
err = c.readBindMessage(&message)
if err != nil {
c.setSessionError(err)
break sessionLoop
}
queryResult = NewBindComplete()
sendReadyForQuery = false
case fbproto.MsgSyncS:
queryResult = NewNopResponder()
sendReadyForQuery = true
case fbproto.MsgQueryQ:
query, err := fbproto.ReadQuery(&message)
if err != nil {
c.setSessionError(err)
break sessionLoop
}
q, err := ParseQuery(query.Query)
if err != nil {
queryResult = NewErrorResponse("42601", err.Error())
} else {
resultDescription := q.Describe()
// Special case in SimpleQuery processing: we only send the
// Describe() response over if it's a RowDescription. This is
// somewhat magical and very weird, but that's what the upstream
// server does, so we ought to do the same thing here.
if DescriptionIsRowDescription(resultDescription) {
c.queryResultCh <- queryResultSync{resultDescription, false}
}
queryResult, err = q.Process(c)
if err != nil {
c.setSessionError(err)
break sessionLoop
}
}
sendReadyForQuery = true
// Simple Query also clears the unnamed statement; this matches
// what Postgres does.
unnamedStatement = nil
case fbproto.MsgTerminateX:
c.setSessionError(errGracefulTermination)
break sessionLoop
default:
c.setSessionError(fmt.Errorf("unrecognized frontend message type 0x%x", message.MsgType()))
break sessionLoop
}
if queryResult != nil {
c.queryResultCh <- queryResultSync{queryResult, sendReadyForQuery}
}
}
// wake mainLoop to clean up
close(c.queryResultCh)
}
func (c *FrontendConnection) sendNotification(n *pq.Notification) error {
var message fbcore.Message
buf := &bytes.Buffer{}
fbbuf.WriteInt32(buf, int32(n.BePid))
fbbuf.WriteCString(buf, n.Channel)
fbbuf.WriteCString(buf, n.Extra)
message.InitFromBytes(fbproto.MsgNotificationResponseA, buf.Bytes())
return c.WriteAndFlush(&message)
}
func (c *FrontendConnection) setSessionError(err error) {
c.lock.Lock()
if c.err == nil {
c.err = err
}
c.lock.Unlock()
}
func (c *FrontendConnection) mainLoop(startupParameters map[string]string, dbcfg VirtualDatabaseConfiguration) {
if !c.startup(startupParameters, dbcfg) {
return
}
go c.queryProcessingMainLoop()
mainLoop:
for {
select {
case n := <-c.notify:
if len(c.notify) >= cap(c.notify)-1 {
c.fatal(errClientCouldNotKeepUp)
break mainLoop
}
if err := c.sendNotification(n); err != nil {
c.setSessionError(err)
break mainLoop
}
case _ = <-c.connStatusNotifier:
c.fatal(errLostServerConnection)
break mainLoop
case resSync, ok := <-c.queryResultCh:
if !ok {
// queryProcessingMainLoop terminated, we're done
break mainLoop
}
err := resSync.Result.Respond(c)
if err != nil {
c.setSessionError(err)
break mainLoop
}
if resSync.Sync {
err = c.sendReadyForQuery()
if err != nil {
c.setSessionError(err)
break mainLoop
}
}
}
}
_ = c.stream.Close()
// wait for queryProcessingMainLoop to finish
for _ = range c.queryResultCh {
}
// Done with this client. Log the error if necessary.
switch c.err {
case errLostServerConnection:
// Already logged, no need to recite the fact that we're throwing
// everyone out.
case errGracefulTermination:
// This is fine
default:
elog.Logf("client %s disconnected: %s\n", c, c.err)
}
// finally, close all the channels the client was listening on
for channel := range c.listenChannels {
err := c.dispatcher.Unlisten(channel, c.notify)
if err != nil {
elog.Warningf("could not unlisten: %s\n", err)
}
}
c.listenChannels = nil
}