forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.go
914 lines (766 loc) · 31 KB
/
server.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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysql
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"strings"
"time"
"github.com/dolthub/vitess/go/netutil"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/stats"
"github.com/dolthub/vitess/go/sync2"
"github.com/dolthub/vitess/go/tb"
"github.com/dolthub/vitess/go/vt/log"
querypb "github.com/dolthub/vitess/go/vt/proto/query"
"github.com/dolthub/vitess/go/vt/proto/vtrpc"
"github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/dolthub/vitess/go/vt/vterrors"
)
const (
// DefaultServerVersion is the default server version we're sending to the client.
// Can be changed.
DefaultServerVersion = "8.0.33"
// timing metric keys
connectTimingKey = "Connect"
queryTimingKey = "Query"
versionTLS10 = "TLS10"
versionTLS11 = "TLS11"
versionTLS12 = "TLS12"
versionTLS13 = "TLS13"
versionTLSUnknown = "UnknownTLSVersion"
versionNoTLS = "None"
)
var (
// Metrics
timings = stats.NewTimings("MysqlServerTimings", "MySQL server timings", "operation")
connCount = stats.NewGauge("MysqlServerConnCount", "Active MySQL server connections")
connAccept = stats.NewCounter("MysqlServerConnAccepted", "Connections accepted by MySQL server")
connSlow = stats.NewCounter("MysqlServerConnSlow", "Connections that took more than the configured mysql_slow_connect_warn_threshold to establish")
connCountByTLSVer = stats.NewGaugesWithSingleLabel("MysqlServerConnCountByTLSVer", "Active MySQL server connections by TLS version", "tls")
connCountPerUser = stats.NewGaugesWithSingleLabel("MysqlServerConnCountPerUser", "Active MySQL server connections per user", "count")
_ = stats.NewGaugeFunc("MysqlServerConnCountUnauthenticated", "Active MySQL server connections that haven't authenticated yet", func() int64 {
totalUsers := int64(0)
for _, v := range connCountPerUser.Counts() {
totalUsers += v
}
return connCount.Get() - totalUsers
})
)
// A Handler is an interface used by Listener to send queries.
// The implementation of this interface may store data in the ClientData
// field of the Connection for its own purposes.
//
// For a given Connection, all these methods are serialized. It means
// only one of these methods will be called concurrently for a given
// Connection. So access to the Connection ClientData does not need to
// be protected by a mutex.
//
// However, each connection is using one go routine, so multiple
// Connection objects can call these concurrently, for different Connections.
type Handler interface {
// NewConnection is called when a connection is created.
// It is not established yet. The handler can decide to
// set StatusFlags that will be returned by the handshake methods.
// In particular, ServerStatusAutocommit might be set.
NewConnection(c *Conn)
// ConnectionClosed is called when a connection is closed.
ConnectionClosed(c *Conn)
// ConnectionAborted is called when a new connection cannot be fully established. For
// example, if a client connects to the server, but fails authentication, or can't
// negotiate an authentication handshake, this method will be called to let integrators
// know about the failed connection attempt.
ConnectionAborted(c *Conn, reason string) error
// ComInitDB is called once at the beginning to set db name,
// and subsequently for every ComInitDB event.
ComInitDB(c *Conn, schemaName string) error
// ComQuery is called when a connection receives a query.
// Note the contents of the query slice may change after
// the first call to callback. So the Handler should not
// hang on to the byte slice.
ComQuery(ctx context.Context, c *Conn, query string, callback ResultSpoolFn) error
// ComMultiQuery is called when a connection receives a query and the
// client supports MULTI_STATEMENT. It should process the first
// statement in |query| and return the remainder. It will be called
// multiple times until the remainder is |""|.
ComMultiQuery(ctx context.Context, c *Conn, query string, callback ResultSpoolFn) (string, error)
// ComPrepare is called when a connection receives a prepared
// statement query.
ComPrepare(ctx context.Context, c *Conn, query string, prepare *PrepareData) ([]*querypb.Field, error)
// ComStmtExecute is called when a connection receives a statement
// execute query.
ComStmtExecute(ctx context.Context, c *Conn, prepare *PrepareData, callback func(*sqltypes.Result) error) error
// WarningCount is called at the end of each query to obtain
// the value to be returned to the client in the EOF packet.
// Note that this will be called either in the context of the
// ComQuery callback if the result does not contain any fields,
// or after the last ComQuery call completes.
WarningCount(c *Conn) uint16
// ComResetConnection is called when a connection receives a COM_RESET_CONNECTION signal.
// This is used to reset the session state (e.g. clearing user vars, resetting session vars, releasing
// locks, releasing cached prepared statements, etc). One of the primary use cases for COM_RESET_CONNECTION
// is to reset a pooled connection's session state so that it can be safely returned to the connection pool
// and given to another application process to reuse.
ComResetConnection(c *Conn) error
// ParserOptionsForConnection returns any parser options that should be used for the given connection. For
// example, if the connection has enabled ANSI_QUOTES or ANSI SQL_MODE, then the parser needs to know that
// in order to parse queries correctly. This is primarily needed when a prepared statement request comes in,
// and the Vitess layer needs to parse the query to identify the query parameters so that the correct response
// packets can be sent.
ParserOptionsForConnection(c *Conn) (sqlparser.ParserOptions, error)
}
// BinlogReplicaHandler is an extension to the Handler interface, to add support for binlog replication server commands.
type BinlogReplicaHandler interface {
// ComRegisterReplica is called when a connection receives a ComRegisterReplica request
ComRegisterReplica(c *Conn, replicaHost string, replicaPort uint16, replicaUser string, replicaPassword string) error
// ComBinlogDumpGTID is called when a connection receives a ComBinlogDumpGTID request
ComBinlogDumpGTID(c *Conn, logFile string, logPos uint64, gtidSet GTIDSet) error
}
// ResultSpoolFn is the callback function used by ComQuery and related functions to handle rows returned by a query
type ResultSpoolFn func(res *sqltypes.Result, more bool) error
// ExtendedHandler is an extension to Handler to support additional protocols on top of MySQL.
type ExtendedHandler interface {
// ComParsedQuery is called when a connection receives a query that has already been parsed. Note the contents
// of the query slice may change after the first call to callback. So the Handler should not hang on to the byte
// slice.
ComParsedQuery(ctx context.Context, c *Conn, query string, parsed sqlparser.Statement, callback ResultSpoolFn) error
// ComPrepareParsed is called when a connection receives a prepared statement query that has already been parsed.
ComPrepareParsed(ctx context.Context, c *Conn, query string, parsed sqlparser.Statement, prepare *PrepareData) (ParsedQuery, []*querypb.Field, error)
// ComBind is called when a connection receives a request to bind a prepared statement to a set of values.
ComBind(ctx context.Context, c *Conn, query string, parsedQuery ParsedQuery, prepare *PrepareData) (BoundQuery, []*querypb.Field, error)
// ComExecuteBound is called when a connection receives a request to execute a prepared statement that has already
// bound to a set of values.
ComExecuteBound(ctx context.Context, c *Conn, query string, boundQuery BoundQuery, callback ResultSpoolFn) error
}
// ParsedQuery is a marker type for communication between the ExtendedHandler interface and integrators, representing a
// query plan that can be examined or executed
type ParsedQuery any
// BoundQuery is a marker type for communication between the ExtendedHandler interface and integrators, representing a
// query plan that has been bound to a set of values
type BoundQuery any
// Listener is the MySQL server protocol listener.
type Listener struct {
// Construction parameters, set by NewListener.
// authServer is the AuthServer object to use for authentication.
authServer AuthServer
// handler is the data handler.
handler Handler
// This is the main listener socket.
listener net.Listener
// Max limit for connections
maxConns uint64
// The following parameters are read by multiple connection go
// routines. They are not protected by a mutex, so they
// should be set after NewListener, and not changed while
// Accept is running.
// ServerVersion is the version we will advertise.
ServerVersion string
// TLSConfig is the server TLS config. If set, we will advertise
// that we support SSL.
TLSConfig *tls.Config
// AllowClearTextWithoutTLS needs to be set for the
// mysql_clear_password authentication method to be accepted
// by the server when TLS is not in use.
AllowClearTextWithoutTLS sync2.AtomicBool
// SlowConnectWarnThreshold if non-zero specifies an amount of time
// beyond which a warning is logged to identify the slow connection
SlowConnectWarnThreshold sync2.AtomicDuration
// The following parameters are changed by the Accept routine.
// Incrementing ID for connection id.
connectionID uint32
// Read timeout on a given connection
connReadTimeout time.Duration
// Write timeout on a given connection
connWriteTimeout time.Duration
// connReadBufferSize is size of buffer for reads from underlying connection.
// Reads are unbuffered if it's <=0.
connReadBufferSize int
// shutdown indicates that Shutdown method was called.
shutdown sync2.AtomicBool
// RequireSecureTransport configures the server to reject connections from insecure clients
RequireSecureTransport bool
}
// NewFromListener creates a new mysql listener from an existing net.Listener
func NewFromListener(l net.Listener, authServer AuthServer, handler Handler, connReadTimeout time.Duration, connWriteTimeout time.Duration) (*Listener, error) {
cfg := ListenerConfig{
Listener: l,
AuthServer: authServer,
Handler: handler,
ConnReadTimeout: connReadTimeout,
ConnWriteTimeout: connWriteTimeout,
ConnReadBufferSize: DefaultConnBufferSize,
}
return NewListenerWithConfig(cfg)
}
// NewListener creates a new Listener.
func NewListener(protocol, address string, authServer AuthServer, handler Handler, connReadTimeout time.Duration, connWriteTimeout time.Duration) (*Listener, error) {
listener, err := net.Listen(protocol, address)
if err != nil {
return nil, err
}
return NewFromListener(listener, authServer, handler, connReadTimeout, connWriteTimeout)
}
// ListenerConfig should be used with NewListenerWithConfig to specify listener parameters.
type ListenerConfig struct {
// Protocol-Address pair and Listener are mutually exclusive parameters
Protocol string
Address string
Listener net.Listener
AuthServer AuthServer
Handler Handler
ConnReadTimeout time.Duration
ConnWriteTimeout time.Duration
ConnReadBufferSize int
MaxConns uint64
AllowClearTextWithoutTLS bool
}
// NewListenerWithConfig creates new listener using provided config. There are
// no default values for config, so caller should ensure its correctness.
func NewListenerWithConfig(cfg ListenerConfig) (*Listener, error) {
var l net.Listener
if cfg.Listener != nil {
l = cfg.Listener
} else {
listener, err := net.Listen(cfg.Protocol, cfg.Address)
if err != nil {
return nil, err
}
l = listener
}
return &Listener{
authServer: cfg.AuthServer,
handler: cfg.Handler,
listener: l,
ServerVersion: DefaultServerVersion,
connectionID: 1,
connReadTimeout: cfg.ConnReadTimeout,
connWriteTimeout: cfg.ConnWriteTimeout,
connReadBufferSize: cfg.ConnReadBufferSize,
maxConns: cfg.MaxConns,
AllowClearTextWithoutTLS: sync2.NewAtomicBool(cfg.AllowClearTextWithoutTLS),
}, nil
}
// Addr returns the listener address.
func (l *Listener) Addr() net.Addr {
return l.listener.Addr()
}
// Accept runs an accept loop until the listener is closed.
func (l *Listener) Accept() {
for {
conn, err := l.listener.Accept()
if err != nil {
// Close() was probably called.
return
}
acceptTime := time.Now()
connectionID := l.connectionID
l.connectionID++
for l.maxConns > 0 && uint64(connCount.Get()) >= l.maxConns {
// TODO: make this behavior configurable (wait v. reject)
time.Sleep(500 * time.Millisecond)
}
connCount.Add(1)
connAccept.Add(1)
go l.handle(context.Background(), conn, connectionID, acceptTime)
}
}
// handle is called in a go routine for each client connection.
// FIXME(alainjobart) handle per-connection logs in a way that makes sense.
func (l *Listener) handle(ctx context.Context, conn net.Conn, connectionID uint32, acceptTime time.Time) {
if l.connReadTimeout != 0 || l.connWriteTimeout != 0 {
conn = netutil.NewConnWithTimeouts(conn, l.connReadTimeout, l.connWriteTimeout)
}
c := newServerConn(conn, l)
c.ConnectionID = connectionID
// Catch panics, and close the connection in any case.
defer func() {
if x := recover(); x != nil {
log.Errorf("mysql_server caught panic:\n%v\n%s", x, tb.Stack(4))
}
// We call flush here in case there's a premature return after
// startWriterBuffering is called
c.flush(ctx)
conn.Close()
}()
// Tell the handler about the connection coming and going.
l.handler.NewConnection(c)
defer l.handler.ConnectionClosed(c)
// Adjust the count of open connections
defer connCount.Add(-1)
defer c.discardCursor()
// First build and send the server handshake packet.
salt, err := c.writeHandshakeV10(l.ServerVersion, l.authServer, l.TLSConfig != nil)
if err != nil {
if err != io.EOF {
l.handleConnectionError(c, fmt.Sprintf("Cannot send HandshakeV10 packet: %v", err))
}
return
}
// Wait for the client response. This has to be a direct read,
// so we don't buffer the TLS negotiation packets.
response, err := c.readEphemeralPacketDirect(ctx)
if err != nil {
// Don't log EOF errors. They cause too much spam, same as main read loop.
if err != io.EOF {
l.handleConnectionWarning(c, fmt.Sprintf(
"Cannot read client handshake response from %s: %v, " +
"it may not be a valid MySQL client", c, err))
}
return
}
user, authMethod, authResponse, err := l.parseClientHandshakePacket(c, true, response)
if err != nil {
l.handleConnectionError(c, fmt.Sprintf(
"Cannot parse client handshake response from %s: %v", c, err))
return
}
c.recycleReadPacket()
if c.Capabilities&CapabilityClientSSL > 0 {
// SSL was enabled. We need to re-read the auth packet.
response, err = c.readEphemeralPacket(ctx)
if err != nil {
l.handleConnectionError(c, fmt.Sprintf(
"Cannot read post-SSL client handshake response from %s: %v", c, err))
return
}
// Returns copies of the data, so we can recycle the buffer.
user, authMethod, authResponse, err = l.parseClientHandshakePacket(c, false, response)
if err != nil {
l.handleConnectionError(c, fmt.Sprintf(
"Cannot parse post-SSL client handshake response from %s: %v", c, err))
return
}
c.recycleReadPacket()
if con, ok := c.Conn.(*tls.Conn); ok {
connState := con.ConnectionState()
tlsVerStr := tlsVersionToString(connState.Version)
if tlsVerStr != "" {
connCountByTLSVer.Add(tlsVerStr, 1)
defer connCountByTLSVer.Add(tlsVerStr, -1)
}
}
} else {
if l.RequireSecureTransport {
c.writeErrorPacketFromError(vterrors.Errorf(vtrpc.Code_UNAVAILABLE, "server does not allow insecure connections, client must use SSL/TLS"))
}
connCountByTLSVer.Add(versionNoTLS, 1)
defer connCountByTLSVer.Add(versionNoTLS, -1)
}
// See what auth method the AuthServer wants to use for that user.
authServerMethod, err := l.authServer.AuthMethod(user, conn.RemoteAddr().String())
if err != nil {
l.handleConnectionError(c, "auth server failed to determine auth method")
c.writeErrorPacketFromError(err)
return
}
// Compare with what the client sent back.
switch {
case authServerMethod == MysqlNativePassword && authMethod == MysqlNativePassword:
// Both server and client want to use MysqlNativePassword:
// the negotiation can be completed right away, using the
// ValidateHash() method.
userData, err := l.authServer.ValidateHash(salt, user, authResponse, conn.RemoteAddr())
if err != nil {
l.handleConnectionWarning(c, fmt.Sprintf(
"Error authenticating user using MySQL native password: %v", err))
c.writeErrorPacketFromError(err)
return
}
c.User = user
c.UserData = userData
case authServerMethod == MysqlNativePassword:
// The server really wants to use MysqlNativePassword,
// but the client returned a result for something else.
salt, err := l.authServer.Salt()
if err != nil {
return
}
//lint:ignore SA4006 This line is required because the binary protocol requires padding with 0
data := make([]byte, 21)
data = append(salt, byte(0x00))
if err := c.writeAuthSwitchRequest(MysqlNativePassword, data); err != nil {
l.handleConnectionError(c, fmt.Sprintf("Error writing auth switch packet for %s: %v", c, err))
return
}
response, err := c.readEphemeralPacket(ctx)
if err != nil {
l.handleConnectionError(c, fmt.Sprintf(
"Error reading auth switch response for %s: %v", c, err))
return
}
c.recycleReadPacket()
userData, err := l.authServer.ValidateHash(salt, user, response, conn.RemoteAddr())
if err != nil {
l.handleConnectionWarning(c, fmt.Sprintf(
"Error authenticating user using MySQL native password: %v", err))
c.writeErrorPacketFromError(err)
return
}
c.User = user
c.UserData = userData
default:
// The server wants to use something else, re-negotiate.
// The negotiation happens in clear text. Let's check we can.
if !l.AllowClearTextWithoutTLS.Get() && c.Capabilities&CapabilityClientSSL == 0 {
l.handleConnectionWarning(c, fmt.Sprintf(
"Cannot use clear text authentication over non-SSL connections."))
c.writeErrorPacket(CRServerHandshakeErr, SSUnknownSQLState, "Cannot use clear text authentication over non-SSL connections.")
return
}
// Switch our auth method to what the server wants.
// Dialog plugin expects an AskPassword prompt.
var data []byte
if authServerMethod == MysqlDialog {
data = authServerDialogSwitchData()
}
if err := c.writeAuthSwitchRequest(authServerMethod, data); err != nil {
l.handleConnectionError(c, fmt.Sprintf(
"Error writing auth switch packet for %s: %v", c, err))
return
}
// Then hand over the rest of the negotiation to the
// auth server.
userData, err := l.authServer.Negotiate(c, user, conn.RemoteAddr())
if err != nil {
l.handleConnectionWarning(c, fmt.Sprintf(
"Unable to negotiate authentication: %v", err))
c.writeErrorPacketFromError(err)
return
}
c.User = user
c.UserData = userData
}
if c.User != "" {
connCountPerUser.Add(c.User, 1)
defer connCountPerUser.Add(c.User, -1)
}
// Set db name.
if err = l.handler.ComInitDB(c, c.schemaName); err != nil {
log.Errorf("failed to set the database %s: %v", c, err)
c.writeErrorPacketFromError(err)
return
}
// Negotiation worked, send OK packet.
if err := c.writeOKPacket(0, 0, c.StatusFlags, 0); err != nil {
log.Errorf("Cannot write OK packet to %s: %v", c, err)
return
}
// Record how long we took to establish the connection
timings.Record(connectTimingKey, acceptTime)
// Log a warning if it took too long to connect
connectTime := time.Since(acceptTime)
if threshold := l.SlowConnectWarnThreshold.Get(); threshold != 0 && connectTime > threshold {
connSlow.Add(1)
log.Warningf("Slow connection from %s: %v", c, connectTime)
}
for {
err := c.handleNextCommand(ctx, l.handler)
if err != nil {
return
}
}
}
// handleConnectionError logs |reason| as an error and notifies the handler that a connection has been aborted.
func (l *Listener) handleConnectionError(c *Conn, reason string) {
log.Error(reason)
if err := l.handler.ConnectionAborted(c, reason); err != nil {
log.Errorf("unable to report connection aborted to handler: %s", err)
}
}
// handleConnectionWarning logs |reason| as a warning and notifies the handler that a connection has been aborted.
func (l *Listener) handleConnectionWarning(c *Conn, reason string) {
log.Warning(reason)
if err := l.handler.ConnectionAborted(c, reason); err != nil {
log.Errorf("unable to report connection aborted to handler: %s", err)
}
}
// Close stops the listener, which prevents accept of any new connections. Existing connections won't be closed.
func (l *Listener) Close() {
l.listener.Close()
}
// Shutdown closes listener and fails any Ping requests from existing connections.
// This can be used for graceful shutdown, to let clients know that they should reconnect to another server.
func (l *Listener) Shutdown() {
if l.shutdown.CompareAndSwap(false, true) {
l.Close()
}
}
func (l *Listener) isShutdown() bool {
return l.shutdown.Get()
}
// writeHandshakeV10 writes the Initial Handshake Packet, server side.
// It returns the salt data.
func (c *Conn) writeHandshakeV10(serverVersion string, authServer AuthServer, enableTLS bool) ([]byte, error) {
capabilities := CapabilityClientLongPassword |
CapabilityClientLongFlag |
CapabilityClientConnectWithDB |
CapabilityClientProtocol41 |
CapabilityClientTransactions |
CapabilityClientSecureConnection |
CapabilityClientMultiStatements |
CapabilityClientMultiResults |
CapabilityClientPluginAuth |
CapabilityClientPluginAuthLenencClientData |
CapabilityClientDeprecateEOF |
CapabilityClientConnAttr |
CapabilityClientFoundRows |
CapabilityClientLocalFiles
if enableTLS {
capabilities |= CapabilityClientSSL
}
length :=
1 + // protocol version
lenNullString(serverVersion) +
4 + // connection ID
8 + // first part of salt data
1 + // filler byte
2 + // capability flags (lower 2 bytes)
1 + // character set
2 + // status flag
2 + // capability flags (upper 2 bytes)
1 + // length of auth plugin data
10 + // reserved (0)
13 + // auth-plugin-data
lenNullString(MysqlNativePassword) // auth-plugin-name
data := c.startEphemeralPacket(length)
pos := 0
// Protocol version.
pos = writeByte(data, pos, protocolVersion)
// Copy server version.
pos = writeNullString(data, pos, serverVersion)
// Add connectionID in.
pos = writeUint32(data, pos, c.ConnectionID)
// Generate the salt, put 8 bytes in.
salt, err := authServer.Salt()
if err != nil {
return nil, err
}
pos += copy(data[pos:], salt[:8])
// One filler byte, always 0.
pos = writeByte(data, pos, 0)
// Lower part of the capability flags.
pos = writeUint16(data, pos, uint16(capabilities))
// Character set.
pos = writeByte(data, pos, CharacterSetUtf8)
// Status flag.
pos = writeUint16(data, pos, c.StatusFlags)
// Upper part of the capability flags.
pos = writeUint16(data, pos, uint16(capabilities>>16))
// Length of auth plugin data.
// Always 21 (8 + 13).
pos = writeByte(data, pos, 21)
// Reserved 10 bytes: all 0
pos = writeZeroes(data, pos, 10)
// Second part of auth plugin data.
pos += copy(data[pos:], salt[8:])
data[pos] = 0
pos++
// Copy authPluginName. We always start with mysql_native_password.
pos = writeNullString(data, pos, MysqlNativePassword)
// Sanity check.
if pos != len(data) {
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "error building Handshake packet: got %v bytes expected %v", pos, len(data))
}
if err := c.writeEphemeralPacket(); err != nil {
if strings.HasSuffix(err.Error(), "write: connection reset by peer") {
return nil, io.EOF
}
if strings.HasSuffix(err.Error(), "write: broken pipe") {
return nil, io.EOF
}
return nil, err
}
return salt, nil
}
// parseClientHandshakePacket parses the handshake sent by the client.
// Returns the username, auth method, auth data, error.
// The original data is not pointed at, and can be freed.
func (l *Listener) parseClientHandshakePacket(c *Conn, firstTime bool, data []byte) (string, string, []byte, error) {
pos := 0
// Client flags, 4 bytes.
clientFlags, pos, ok := readUint32(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read client flags")
}
if clientFlags&CapabilityClientProtocol41 == 0 {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: only support protocol 4.1")
}
// Remember a subset of the capabilities, so we can use them
// later in the protocol. If we re-received the handshake packet
// after SSL negotiation, do not overwrite capabilities.
if firstTime {
c.Capabilities = clientFlags & (CapabilityClientDeprecateEOF | CapabilityClientFoundRows)
}
// set connection capability for executing multi statements
if clientFlags&CapabilityClientMultiStatements > 0 {
c.Capabilities |= CapabilityClientMultiStatements
}
// Max packet size. Don't do anything with this now.
// See doc.go for more information.
_, pos, ok = readUint32(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read maxPacketSize")
}
// Character set. Need to handle it.
characterSet, pos, ok := readByte(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read characterSet")
}
c.CharacterSet = characterSet
// 23x reserved zero bytes.
pos += 23
// Check for SSL.
if firstTime && l.TLSConfig != nil && clientFlags&CapabilityClientSSL > 0 {
// Need to switch to TLS, and then re-read the packet.
conn := tls.Server(c.Conn, l.TLSConfig)
c.Conn = conn
c.bufferedReader.Reset(conn)
c.Capabilities |= CapabilityClientSSL
return "", "", nil, nil
}
// username
username, pos, ok := readNullString(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read username")
}
// auth-response can have three forms.
var authResponse []byte
if clientFlags&CapabilityClientPluginAuthLenencClientData != 0 {
var l uint64
l, pos, ok = readLenEncInt(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read auth-response variable length")
}
authResponse, pos, ok = readBytesCopy(data, pos, int(l))
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read auth-response")
}
} else if clientFlags&CapabilityClientSecureConnection != 0 {
var l byte
l, pos, ok = readByte(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read auth-response length")
}
authResponse, pos, ok = readBytesCopy(data, pos, int(l))
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read auth-response")
}
} else {
a := ""
a, pos, ok = readNullString(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read auth-response")
}
authResponse = []byte(a)
}
// db name.
if clientFlags&CapabilityClientConnectWithDB != 0 {
dbname := ""
dbname, pos, ok = readNullString(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read dbname")
}
c.schemaName = dbname
}
// authMethod (with default)
authMethod := MysqlNativePassword
if clientFlags&CapabilityClientPluginAuth != 0 {
authMethod, pos, ok = readNullString(data, pos)
if !ok {
return "", "", nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read authMethod")
}
}
// The JDBC driver sometimes sends an empty string as the auth method when it wants to use mysql_native_password
if authMethod == "" {
authMethod = MysqlNativePassword
}
// Decode connection attributes send by the client
if clientFlags&CapabilityClientConnAttr != 0 {
if _, _, err := parseConnAttrs(data, pos); err != nil {
log.Warningf("Decode connection attributes send by the client: %v", err)
}
}
return username, authMethod, authResponse, nil
}
func parseConnAttrs(data []byte, pos int) (map[string]string, int, error) {
var attrLen uint64
attrLen, pos, ok := readLenEncInt(data, pos)
if !ok {
return nil, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read connection attributes variable length")
}
var attrLenRead uint64
attrs := make(map[string]string)
for attrLenRead < attrLen {
var keyLen byte
keyLen, pos, ok = readByte(data, pos)
if !ok {
return nil, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read connection attribute key length")
}
attrLenRead += uint64(keyLen) + 1
var connAttrKey []byte
connAttrKey, pos, ok = readBytesCopy(data, pos, int(keyLen))
if !ok {
return nil, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read connection attribute key")
}
var valLen byte
valLen, pos, ok = readByte(data, pos)
if !ok {
return nil, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read connection attribute value length")
}
attrLenRead += uint64(valLen) + 1
var connAttrVal []byte
connAttrVal, pos, ok = readBytesCopy(data, pos, int(valLen))
if !ok {
return nil, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "parseClientHandshakePacket: can't read connection attribute value")
}
attrs[string(connAttrKey[:])] = string(connAttrVal[:])
}
return attrs, pos, nil
}
// writeAuthSwitchRequest writes an auth switch request packet.
func (c *Conn) writeAuthSwitchRequest(pluginName string, pluginData []byte) error {
length := 1 + // AuthSwitchRequestPacket
len(pluginName) + 1 + // 0-terminated pluginName
len(pluginData)
data := c.startEphemeralPacket(length)
pos := 0
// Packet header.
pos = writeByte(data, pos, AuthSwitchRequestPacket)
// Copy server version.
pos = writeNullString(data, pos, pluginName)
// Copy auth data.
pos += copy(data[pos:], pluginData)
// Sanity check.
if pos != len(data) {
return vterrors.Errorf(vtrpc.Code_INTERNAL, "error building AuthSwitchRequestPacket packet: got %v bytes expected %v", pos, len(data))
}
return c.writeEphemeralPacket()
}
// Whenever we move to a new version of go, we will need add any new supported TLS versions here
func tlsVersionToString(version uint16) string {
switch version {
case tls.VersionTLS10:
return versionTLS10
case tls.VersionTLS11:
return versionTLS11
case tls.VersionTLS12:
return versionTLS12
case tls.VersionTLS13:
return versionTLS13
default:
return versionTLSUnknown
}
}