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
|
package postgres
import (
"bufio"
"crypto/md5"
"crypto/rand"
"crypto/tls"
"encoding/binary"
"fmt"
"io"
"net"
"strings"
"sync"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/query/engine"
"github.com/seaweedfs/seaweedfs/weed/util/version"
)
// PostgreSQL protocol constants
const (
// Protocol versions
PG_PROTOCOL_VERSION_3 = 196608 // PostgreSQL 3.0 protocol (0x00030000)
PG_SSL_REQUEST = 80877103 // SSL request (0x04d2162f)
PG_GSSAPI_REQUEST = 80877104 // GSSAPI request (0x04d21630)
// Message types from client
PG_MSG_STARTUP = 0x00
PG_MSG_QUERY = 'Q'
PG_MSG_PARSE = 'P'
PG_MSG_BIND = 'B'
PG_MSG_EXECUTE = 'E'
PG_MSG_DESCRIBE = 'D'
PG_MSG_CLOSE = 'C'
PG_MSG_FLUSH = 'H'
PG_MSG_SYNC = 'S'
PG_MSG_TERMINATE = 'X'
PG_MSG_PASSWORD = 'p'
// Response types to client
PG_RESP_AUTH_OK = 'R'
PG_RESP_BACKEND_KEY = 'K'
PG_RESP_PARAMETER = 'S'
PG_RESP_READY = 'Z'
PG_RESP_COMMAND = 'C'
PG_RESP_DATA_ROW = 'D'
PG_RESP_ROW_DESC = 'T'
PG_RESP_PARSE_COMPLETE = '1'
PG_RESP_BIND_COMPLETE = '2'
PG_RESP_CLOSE_COMPLETE = '3'
PG_RESP_ERROR = 'E'
PG_RESP_NOTICE = 'N'
// Transaction states
PG_TRANS_IDLE = 'I'
PG_TRANS_INTRANS = 'T'
PG_TRANS_ERROR = 'E'
// Authentication methods
AUTH_OK = 0
AUTH_CLEAR = 3
AUTH_MD5 = 5
AUTH_TRUST = 10
// PostgreSQL data types
PG_TYPE_BOOL = 16
PG_TYPE_BYTEA = 17
PG_TYPE_INT8 = 20
PG_TYPE_INT4 = 23
PG_TYPE_TEXT = 25
PG_TYPE_FLOAT4 = 700
PG_TYPE_FLOAT8 = 701
PG_TYPE_VARCHAR = 1043
PG_TYPE_TIMESTAMP = 1114
PG_TYPE_JSON = 114
PG_TYPE_JSONB = 3802
// Default values
DEFAULT_POSTGRES_PORT = 5432
)
// Authentication method type
type AuthMethod int
const (
AuthTrust AuthMethod = iota
AuthPassword
AuthMD5
)
// PostgreSQL server configuration
type PostgreSQLServerConfig struct {
Host string
Port int
AuthMethod AuthMethod
Users map[string]string
TLSConfig *tls.Config
MaxConns int
IdleTimeout time.Duration
StartupTimeout time.Duration // Timeout for client startup handshake
Database string
}
// PostgreSQL server
type PostgreSQLServer struct {
config *PostgreSQLServerConfig
listener net.Listener
sqlEngine *engine.SQLEngine
sessions map[uint32]*PostgreSQLSession
sessionMux sync.RWMutex
shutdown chan struct{}
wg sync.WaitGroup
nextConnID uint32
}
// PostgreSQL session
type PostgreSQLSession struct {
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
authenticated bool
username string
database string
parameters map[string]string
preparedStmts map[string]*PreparedStatement
portals map[string]*Portal
transactionState byte
processID uint32
secretKey uint32
created time.Time
lastActivity time.Time
mutex sync.Mutex
}
// Prepared statement
type PreparedStatement struct {
Name string
Query string
ParamTypes []uint32
Fields []FieldDescription
}
// Portal (cursor)
type Portal struct {
Name string
Statement string
Parameters [][]byte
Suspended bool
}
// Field description
type FieldDescription struct {
Name string
TableOID uint32
AttrNum int16
TypeOID uint32
TypeSize int16
TypeMod int32
Format int16
}
// NewPostgreSQLServer creates a new PostgreSQL protocol server
func NewPostgreSQLServer(config *PostgreSQLServerConfig, masterAddr string) (*PostgreSQLServer, error) {
if config.Port <= 0 {
config.Port = DEFAULT_POSTGRES_PORT
}
if config.Host == "" {
config.Host = "localhost"
}
if config.Database == "" {
config.Database = "default"
}
if config.MaxConns <= 0 {
config.MaxConns = 100
}
if config.IdleTimeout <= 0 {
config.IdleTimeout = time.Hour
}
if config.StartupTimeout <= 0 {
config.StartupTimeout = 30 * time.Second
}
// Create SQL engine (now uses CockroachDB parser for PostgreSQL compatibility)
sqlEngine := engine.NewSQLEngine(masterAddr)
server := &PostgreSQLServer{
config: config,
sqlEngine: sqlEngine,
sessions: make(map[uint32]*PostgreSQLSession),
shutdown: make(chan struct{}),
nextConnID: 1,
}
return server, nil
}
// Start begins listening for PostgreSQL connections
func (s *PostgreSQLServer) Start() error {
addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
var listener net.Listener
var err error
if s.config.TLSConfig != nil {
listener, err = tls.Listen("tcp", addr, s.config.TLSConfig)
glog.Infof("PostgreSQL Server with TLS listening on %s", addr)
} else {
listener, err = net.Listen("tcp", addr)
glog.Infof("PostgreSQL Server listening on %s", addr)
}
if err != nil {
return fmt.Errorf("failed to start PostgreSQL server on %s: %v", addr, err)
}
s.listener = listener
// Start accepting connections
s.wg.Add(1)
go s.acceptConnections()
// Start cleanup routine
s.wg.Add(1)
go s.cleanupSessions()
return nil
}
// Stop gracefully shuts down the PostgreSQL server
func (s *PostgreSQLServer) Stop() error {
close(s.shutdown)
if s.listener != nil {
s.listener.Close()
}
// Close all sessions
s.sessionMux.Lock()
for _, session := range s.sessions {
session.close()
}
s.sessions = make(map[uint32]*PostgreSQLSession)
s.sessionMux.Unlock()
s.wg.Wait()
glog.Infof("PostgreSQL Server stopped")
return nil
}
// acceptConnections handles incoming PostgreSQL connections
func (s *PostgreSQLServer) acceptConnections() {
defer s.wg.Done()
for {
select {
case <-s.shutdown:
return
default:
}
conn, err := s.listener.Accept()
if err != nil {
select {
case <-s.shutdown:
return
default:
glog.Errorf("Failed to accept PostgreSQL connection: %v", err)
continue
}
}
// Check connection limit
s.sessionMux.RLock()
sessionCount := len(s.sessions)
s.sessionMux.RUnlock()
if sessionCount >= s.config.MaxConns {
glog.Warningf("Maximum connections reached (%d), rejecting connection from %s",
s.config.MaxConns, conn.RemoteAddr())
conn.Close()
continue
}
s.wg.Add(1)
go s.handleConnection(conn)
}
}
// handleConnection processes a single PostgreSQL connection
func (s *PostgreSQLServer) handleConnection(conn net.Conn) {
defer s.wg.Done()
defer conn.Close()
// Generate unique connection ID
connID := s.generateConnectionID()
secretKey := s.generateSecretKey()
// Create session
session := &PostgreSQLSession{
conn: conn,
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
authenticated: false,
database: s.config.Database,
parameters: make(map[string]string),
preparedStmts: make(map[string]*PreparedStatement),
portals: make(map[string]*Portal),
transactionState: PG_TRANS_IDLE,
processID: connID,
secretKey: secretKey,
created: time.Now(),
lastActivity: time.Now(),
}
// Register session
s.sessionMux.Lock()
s.sessions[connID] = session
s.sessionMux.Unlock()
// Clean up on exit
defer func() {
s.sessionMux.Lock()
delete(s.sessions, connID)
s.sessionMux.Unlock()
}()
glog.V(2).Infof("New PostgreSQL connection from %s (ID: %d)", conn.RemoteAddr(), connID)
// Handle startup
err := s.handleStartup(session)
if err != nil {
// Handle common disconnection scenarios more gracefully
if strings.Contains(err.Error(), "client disconnected") {
glog.V(1).Infof("Client startup disconnected from %s (ID: %d): %v", conn.RemoteAddr(), connID, err)
} else if strings.Contains(err.Error(), "timeout") {
glog.Warningf("Startup timeout for connection %d from %s: %v", connID, conn.RemoteAddr(), err)
} else {
glog.Errorf("Startup failed for connection %d from %s: %v", connID, conn.RemoteAddr(), err)
}
return
}
// Handle messages
for {
select {
case <-s.shutdown:
return
default:
}
// Set read timeout
conn.SetReadDeadline(time.Now().Add(30 * time.Second))
err := s.handleMessage(session)
if err != nil {
if err == io.EOF {
glog.Infof("PostgreSQL client disconnected (ID: %d)", connID)
} else {
glog.Errorf("Error handling PostgreSQL message (ID: %d): %v", connID, err)
}
return
}
session.lastActivity = time.Now()
}
}
// handleStartup processes the PostgreSQL startup sequence
func (s *PostgreSQLServer) handleStartup(session *PostgreSQLSession) error {
// Set a startup timeout to prevent hanging connections
startupTimeout := s.config.StartupTimeout
session.conn.SetReadDeadline(time.Now().Add(startupTimeout))
defer session.conn.SetReadDeadline(time.Time{}) // Clear timeout
for {
// Read startup message length
length := make([]byte, 4)
_, err := io.ReadFull(session.reader, length)
if err != nil {
if err == io.EOF {
// Client disconnected during startup - this is common for health checks
return fmt.Errorf("client disconnected during startup handshake")
}
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return fmt.Errorf("startup handshake timeout after %v", startupTimeout)
}
return fmt.Errorf("failed to read message length during startup: %v", err)
}
msgLength := binary.BigEndian.Uint32(length) - 4
if msgLength > 10000 { // Reasonable limit for startup messages
return fmt.Errorf("startup message too large: %d bytes", msgLength)
}
// Read startup message content
msg := make([]byte, msgLength)
_, err = io.ReadFull(session.reader, msg)
if err != nil {
if err == io.EOF {
return fmt.Errorf("client disconnected while reading startup message")
}
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return fmt.Errorf("startup message read timeout")
}
return fmt.Errorf("failed to read startup message: %v", err)
}
// Parse protocol version
protocolVersion := binary.BigEndian.Uint32(msg[0:4])
switch protocolVersion {
case PG_SSL_REQUEST:
// Reject SSL request - send 'N' to indicate SSL not supported
_, err = session.conn.Write([]byte{'N'})
if err != nil {
return fmt.Errorf("failed to reject SSL request: %v", err)
}
// Continue loop to read the actual startup message
continue
case PG_GSSAPI_REQUEST:
// Reject GSSAPI request - send 'N' to indicate GSSAPI not supported
_, err = session.conn.Write([]byte{'N'})
if err != nil {
return fmt.Errorf("failed to reject GSSAPI request: %v", err)
}
// Continue loop to read the actual startup message
continue
case PG_PROTOCOL_VERSION_3:
// This is the actual startup message, break out of loop
break
default:
return fmt.Errorf("unsupported protocol version: %d", protocolVersion)
}
// Parse parameters
params := strings.Split(string(msg[4:]), "\x00")
for i := 0; i < len(params)-1; i += 2 {
if params[i] == "user" {
session.username = params[i+1]
} else if params[i] == "database" {
session.database = params[i+1]
}
session.parameters[params[i]] = params[i+1]
}
// Break out of the main loop - we have the startup message
break
}
// Handle authentication
err := s.handleAuthentication(session)
if err != nil {
return err
}
// Send parameter status messages
err = s.sendParameterStatus(session, "server_version", fmt.Sprintf("%s (SeaweedFS)", version.VERSION_NUMBER))
if err != nil {
return err
}
err = s.sendParameterStatus(session, "server_encoding", "UTF8")
if err != nil {
return err
}
err = s.sendParameterStatus(session, "client_encoding", "UTF8")
if err != nil {
return err
}
err = s.sendParameterStatus(session, "DateStyle", "ISO, MDY")
if err != nil {
return err
}
err = s.sendParameterStatus(session, "integer_datetimes", "on")
if err != nil {
return err
}
// Send backend key data
err = s.sendBackendKeyData(session)
if err != nil {
return err
}
// Send ready for query
err = s.sendReadyForQuery(session)
if err != nil {
return err
}
session.authenticated = true
return nil
}
// handleAuthentication processes authentication
func (s *PostgreSQLServer) handleAuthentication(session *PostgreSQLSession) error {
switch s.config.AuthMethod {
case AuthTrust:
return s.sendAuthenticationOk(session)
case AuthPassword:
return s.handlePasswordAuth(session)
case AuthMD5:
return s.handleMD5Auth(session)
default:
return fmt.Errorf("unsupported authentication method")
}
}
// sendAuthenticationOk sends authentication OK message
func (s *PostgreSQLServer) sendAuthenticationOk(session *PostgreSQLSession) error {
msg := make([]byte, 9)
msg[0] = PG_RESP_AUTH_OK
binary.BigEndian.PutUint32(msg[1:5], 8)
binary.BigEndian.PutUint32(msg[5:9], AUTH_OK)
_, err := session.writer.Write(msg)
if err == nil {
err = session.writer.Flush()
}
return err
}
// handlePasswordAuth handles clear password authentication
func (s *PostgreSQLServer) handlePasswordAuth(session *PostgreSQLSession) error {
// Send password request
msg := make([]byte, 9)
msg[0] = PG_RESP_AUTH_OK
binary.BigEndian.PutUint32(msg[1:5], 8)
binary.BigEndian.PutUint32(msg[5:9], AUTH_CLEAR)
_, err := session.writer.Write(msg)
if err != nil {
return err
}
err = session.writer.Flush()
if err != nil {
return err
}
// Read password response
msgType := make([]byte, 1)
_, err = io.ReadFull(session.reader, msgType)
if err != nil {
return err
}
if msgType[0] != PG_MSG_PASSWORD {
return fmt.Errorf("expected password message, got %c", msgType[0])
}
length := make([]byte, 4)
_, err = io.ReadFull(session.reader, length)
if err != nil {
return err
}
msgLength := binary.BigEndian.Uint32(length) - 4
password := make([]byte, msgLength)
_, err = io.ReadFull(session.reader, password)
if err != nil {
return err
}
// Verify password
expectedPassword, exists := s.config.Users[session.username]
if !exists || string(password[:len(password)-1]) != expectedPassword { // Remove null terminator
return s.sendError(session, "28P01", "authentication failed for user \""+session.username+"\"")
}
return s.sendAuthenticationOk(session)
}
// handleMD5Auth handles MD5 password authentication
func (s *PostgreSQLServer) handleMD5Auth(session *PostgreSQLSession) error {
// Generate salt
salt := make([]byte, 4)
_, err := rand.Read(salt)
if err != nil {
return err
}
// Send MD5 request
msg := make([]byte, 13)
msg[0] = PG_RESP_AUTH_OK
binary.BigEndian.PutUint32(msg[1:5], 12)
binary.BigEndian.PutUint32(msg[5:9], AUTH_MD5)
copy(msg[9:13], salt)
_, err = session.writer.Write(msg)
if err != nil {
return err
}
err = session.writer.Flush()
if err != nil {
return err
}
// Read password response
msgType := make([]byte, 1)
_, err = io.ReadFull(session.reader, msgType)
if err != nil {
return err
}
if msgType[0] != PG_MSG_PASSWORD {
return fmt.Errorf("expected password message, got %c", msgType[0])
}
length := make([]byte, 4)
_, err = io.ReadFull(session.reader, length)
if err != nil {
return err
}
msgLength := binary.BigEndian.Uint32(length) - 4
response := make([]byte, msgLength)
_, err = io.ReadFull(session.reader, response)
if err != nil {
return err
}
// Verify MD5 hash
expectedPassword, exists := s.config.Users[session.username]
if !exists {
return s.sendError(session, "28P01", "authentication failed for user \""+session.username+"\"")
}
// Calculate expected hash: md5(md5(password + username) + salt)
inner := md5.Sum([]byte(expectedPassword + session.username))
expected := fmt.Sprintf("md5%x", md5.Sum(append([]byte(fmt.Sprintf("%x", inner)), salt...)))
if string(response[:len(response)-1]) != expected { // Remove null terminator
return s.sendError(session, "28P01", "authentication failed for user \""+session.username+"\"")
}
return s.sendAuthenticationOk(session)
}
// generateConnectionID generates a unique connection ID
func (s *PostgreSQLServer) generateConnectionID() uint32 {
s.sessionMux.Lock()
defer s.sessionMux.Unlock()
id := s.nextConnID
s.nextConnID++
return id
}
// generateSecretKey generates a secret key for the connection
func (s *PostgreSQLServer) generateSecretKey() uint32 {
key := make([]byte, 4)
rand.Read(key)
return binary.BigEndian.Uint32(key)
}
// close marks the session as closed
func (s *PostgreSQLSession) close() {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.conn != nil {
s.conn.Close()
s.conn = nil
}
}
// cleanupSessions periodically cleans up idle sessions
func (s *PostgreSQLServer) cleanupSessions() {
defer s.wg.Done()
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-s.shutdown:
return
case <-ticker.C:
s.cleanupIdleSessions()
}
}
}
// cleanupIdleSessions removes sessions that have been idle too long
func (s *PostgreSQLServer) cleanupIdleSessions() {
now := time.Now()
s.sessionMux.Lock()
defer s.sessionMux.Unlock()
for id, session := range s.sessions {
if now.Sub(session.lastActivity) > s.config.IdleTimeout {
glog.Infof("Closing idle PostgreSQL session %d", id)
session.close()
delete(s.sessions, id)
}
}
}
// GetAddress returns the server address
func (s *PostgreSQLServer) GetAddress() string {
return fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
}
|