forked from fajran/go-monetdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapi.go
506 lines (419 loc) · 11.5 KB
/
mapi.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package monetdb
import (
"bytes"
"crypto"
_ "crypto/md5"
_ "crypto/sha1"
_ "crypto/sha512"
"encoding/binary"
"errors"
"fmt"
"hash"
"io"
"log"
"net"
"strconv"
"strings"
"time"
)
const (
mapi_MAX_PACKAGE_LENGTH = (1024 * 8) - 2
mapi_MSG_PROMPT = ""
mapi_MSG_INFO = "#"
mapi_MSG_ERROR = "!"
mapi_MSG_Q = "&"
mapi_MSG_QTABLE = "&1"
mapi_MSG_QUPDATE = "&2"
mapi_MSG_QSCHEMA = "&3"
mapi_MSG_QTRANS = "&4"
mapi_MSG_QPREPARE = "&5"
mapi_MSG_QBLOCK = "&6"
mapi_MSG_HEADER = "%"
mapi_MSG_TUPLE = "["
mapi_MSG_TUPLE_NOSLICE = "="
mapi_MSG_REDIRECT = "^"
mapi_MSG_OK = "=OK"
)
// MAPI connection is established.
const MAPI_STATE_READY = 1
// MAPI connection is NOT established.
const MAPI_STATE_INIT = 0
var (
mapi_MSG_MORE = string([]byte{1, 2, 10})
)
// MapiConn is a MonetDB's MAPI connection handle.
//
// The values in the handle are initially set according to the values
// that are provided when calling NewMapi. However, they may change
// depending on how the MonetDB server redirects the connection.
// The final values are available after the connection is made by
// calling the Connect() function.
//
// The State value can be either MAPI_STATE_INIT or MAPI_STATE_READY.
type MapiConn struct {
Hostname string
Port int
Username string
Password string
Database string
Language string
State int
conn *net.TCPConn
}
// NewMapi returns a MonetDB's MAPI connection handle.
//
// To establish the connection, call the Connect() function.
func NewMapi(hostname string, port int, username, password, database, language string) *MapiConn {
return &MapiConn{
Hostname: hostname,
Port: port,
Username: username,
Password: password,
Database: database,
Language: language,
State: MAPI_STATE_INIT,
}
}
// Disconnect closes the connection.
func (c *MapiConn) Disconnect() {
c.State = MAPI_STATE_INIT
if c.conn != nil {
c.conn.Close()
c.conn = nil
}
}
func truncateString(str string, num int) string {
newStr := str
if len(str) > num {
if num > 3 {
num -= 3
}
newStr = str[0:num] + "..."
}
return newStr
}
func (c *MapiConn) CheckErr(err error) {
if errors.Is(err, net.ErrClosed) || strings.Contains(err.Error(), "broken pipe") {
c.Disconnect()
}
}
// Cmd sends a MAPI command to MonetDB.
func (c *MapiConn) Cmd(operation string) (BlockInformation, error) {
if c.State != MAPI_STATE_READY {
c.Connect() // try to reconnect for next time.
// We should still error out here because this might be in a weird state where we expected
// a transaction or something possibly.
return BlockInformation{}, fmt.Errorf("database not connected")
}
//log.Printf("Putting block '%s'\n", operation)
if err := c.putBlock([]byte(operation)); err != nil {
c.CheckErr(err)
log.Printf("failed to put block for operation: '%s'", truncateString(operation, 150))
return BlockInformation{}, err
}
r, err := c.getBlock()
if err != nil {
c.CheckErr(err)
log.Printf("failed to get block for operation: '%s'", truncateString(operation, 150))
return BlockInformation{}, err
}
resp := string(r)
information, err := HandleBlock(resp)
return information, err
}
type BlockInformation struct {
Response string
More bool
}
func HandleBlock(resp string) (BlockInformation, error) {
debugMsg := func(msg string) {
if DEBUG_MODE {
received := string(resp)
received = strings.Replace(received, "\r", "\\r", -1)
received = strings.Replace(received, "\n", "\\n", -1)
log.Printf("getBlock (%s): '%s'", msg, received)
}
}
if len(resp) == 0 {
debugMsg("empty")
return BlockInformation{}, nil
} else if strings.HasPrefix(resp, mapi_MSG_OK) {
debugMsg("MSG_OK")
return BlockInformation{Response: strings.TrimSpace(resp[3:])}, nil
} else if resp == mapi_MSG_MORE {
debugMsg(fmt.Sprintf("MSG_MORE %v", []byte(resp)))
return BlockInformation{More: true}, nil
}
if resp[:2] == mapi_MSG_QUPDATE {
debugMsg("MSG_QUPDATE")
lines := strings.Split(resp, "\n")
for _, line := range lines {
if strings.HasPrefix(line, mapi_MSG_ERROR) {
return BlockInformation{}, fmt.Errorf("QUPDATE error: %s", line[1:])
}
}
}
if strings.HasPrefix(resp, mapi_MSG_Q) || strings.HasPrefix(resp, mapi_MSG_HEADER) || strings.HasPrefix(resp, mapi_MSG_TUPLE) {
debugMsg("MSG_Q/HEADER/TUPLE")
return BlockInformation{Response: resp}, nil
} else if strings.HasPrefix(resp, mapi_MSG_ERROR) {
debugMsg("MSG_ERROR")
return BlockInformation{}, fmt.Errorf("Operational error: %s", resp[1:])
} else if strings.HasPrefix(resp, mapi_MSG_INFO) {
debugMsg("MSG_INFO")
log.Printf("Monet INFO: %s", resp[1:])
return BlockInformation{Response: resp[1:]}, nil
} else {
debugMsg("UNKNOWN")
return BlockInformation{}, fmt.Errorf("Unknown CMD state: %s", resp)
}
}
// Connect starts a MAPI connection to MonetDB server.
func (c *MapiConn) Connect() error {
err := c.tryConnect()
if err == nil {
return err
}
return nil
}
func (c *MapiConn) tryConnect() error {
if c.conn != nil {
c.Disconnect()
}
addr := fmt.Sprintf("%s:%d", c.Hostname, c.Port)
dialer := net.Dialer{
Timeout: time.Second * 10,
}
conn, err := dialer.Dial("tcp", addr)
if err != nil {
return fmt.Errorf("dial monet: %s", err)
}
tcpConn, _ := conn.(*net.TCPConn)
tcpConn.SetKeepAlive(true)
tcpConn.SetNoDelay(true)
c.conn = tcpConn
err = c.login()
if err != nil {
return err
}
return nil
}
// login starts the login sequence
func (c *MapiConn) login() error {
return c.tryLogin(0)
}
// tryLogin performs the login activity
func (c *MapiConn) tryLogin(iteration int) error {
challenge, err := c.getBlock()
if err != nil {
return fmt.Errorf("challenge response get block: %s", err)
}
response, err := c.challengeResponse(challenge)
if err != nil {
return fmt.Errorf("challenge response: %s", err)
}
err = c.putBlock([]byte(response))
if err != nil {
return fmt.Errorf("challenge response put block: %s", err)
}
bprompt, err := c.getBlock()
if err != nil {
return fmt.Errorf("get prompt: %s", err)
}
prompt := strings.TrimSpace(string(bprompt))
if len(prompt) == 0 {
// Empty response, server is happy
} else if prompt == mapi_MSG_OK {
// pass
} else if strings.HasPrefix(prompt, mapi_MSG_INFO) {
log.Printf("MAPI: info: %s\n", prompt[1:])
} else if strings.HasPrefix(prompt, mapi_MSG_ERROR) {
log.Printf("MAPI: error: %s\n", prompt[1:])
return fmt.Errorf("Database error: %s", prompt[1:])
} else if strings.HasPrefix(prompt, mapi_MSG_REDIRECT) {
t := strings.Split(prompt, " ")
r := strings.Split(t[0][1:], ":")
if r[1] == "merovingian" {
// restart auth
if iteration <= 10 {
return c.tryLogin(iteration + 1)
} else {
return fmt.Errorf("Maximal number of redirects reached (10)")
}
} else if r[1] == "monetdb" {
c.Hostname = r[2][2:]
t = strings.Split(r[3], "/")
port, err := strconv.ParseInt(t[0], 10, 32)
if err != nil {
return err
}
c.Port = int(port)
c.Database = t[1]
log.Printf("MAPI: Redirect to %s:%d/%s, r[3]: %s", c.Hostname, c.Port, c.Database, r[3])
return c.Connect()
} else {
return fmt.Errorf("Unknown redirect: %s", prompt)
}
} else {
return fmt.Errorf("Unknown prompt state: %s", prompt)
}
c.State = MAPI_STATE_READY
return nil
}
// challengeResponse produces a response given a challenge
func (c *MapiConn) challengeResponse(challenge []byte) (string, error) {
t := strings.Split(string(challenge), ":")
salt := t[0]
protocol := t[2]
hashes := t[3]
algo := t[5]
if protocol != "9" {
return "", fmt.Errorf("We only speak protocol v9")
}
var h hash.Hash
if algo == "SHA512" {
h = crypto.SHA512.New()
} else {
// TODO support more algorithm
return "", fmt.Errorf("Unsupported algorithm: %s", algo)
}
io.WriteString(h, c.Password)
p := fmt.Sprintf("%x", h.Sum(nil))
shashes := "," + hashes + ","
var pwhash string
if strings.Contains(shashes, ",SHA1,") {
h = crypto.SHA1.New()
io.WriteString(h, p)
io.WriteString(h, salt)
pwhash = fmt.Sprintf("{SHA1}%x", h.Sum(nil))
} else if strings.Contains(shashes, ",MD5,") {
h = crypto.MD5.New()
io.WriteString(h, p)
io.WriteString(h, salt)
pwhash = fmt.Sprintf("{MD5}%x", h.Sum(nil))
} else {
return "", fmt.Errorf("Unsupported hash algorithm required for login %s", hashes)
}
r := fmt.Sprintf("BIG:%s:%s:%s:%s:", c.Username, pwhash, c.Language, c.Database)
return r, nil
}
// getBlock retrieves a block of message
func (c *MapiConn) getBlock() ([]byte, error) {
var r bytes.Buffer
last := 0
for last != 1 {
flag, err := c.getBytes(2)
if err != nil {
return nil, fmt.Errorf("getting flag: %s", err)
}
var unpacked uint16
buf := bytes.NewBuffer(flag)
err = binary.Read(buf, binary.LittleEndian, &unpacked)
if err != nil {
return nil, fmt.Errorf("unpacking flag: %s", err)
}
length := unpacked >> 1
last = int(unpacked & 1)
d, err := c.getBytes(int(length))
if err != nil {
return nil, fmt.Errorf("getting %d bytes: %s", length, err)
}
r.Write(d)
}
if RAW_MODE {
received := r.String()
received = strings.Replace(received, "\r", "\\r", -1)
received = strings.Replace(received, "\n", "\\n", -1)
fmt.Printf("RAW receive: '%s'\n", received)
}
return r.Bytes(), nil
}
// getBytes reads the given amount of bytes
func (c *MapiConn) getBytes(count int) ([]byte, error) {
r := new(bytes.Buffer)
//r.Grow(count)
read := 0
for read < count {
b := make([]byte, count-read)
deadlineErr := c.conn.SetDeadline(time.Now().Add(300 * time.Second))
if deadlineErr != nil {
return nil, fmt.Errorf("enable deadline: %s", deadlineErr)
}
n, err := c.conn.Read(b)
// Disable deadline after we read something so we don't timeout the next time
// if we idle for too long.
deadlineErr = c.conn.SetDeadline(time.Time{})
if deadlineErr != nil {
return nil, fmt.Errorf("disable deadline: %s", deadlineErr)
}
if err != nil {
r.Write(b)
return nil, err
}
//copy(r[read:], b[:n])
r.Write(b)
read += n
}
return r.Bytes(), nil
}
func (c *MapiConn) ping() error {
one := []byte{}
c.conn.SetReadDeadline(time.Now())
_, err := c.conn.Read(one)
closed := false
if err == io.EOF {
closed = true
} else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
closed = true
}
if closed {
return c.Connect()
}
return nil
}
// putBlock sends the given data as one or more blocks
func (c *MapiConn) putBlock(b []byte) error {
if c == nil {
return fmt.Errorf("mapi connection is nil")
}
if RAW_MODE {
sent := string(b)
sent = strings.Replace(sent, "\r", "\\r", -1)
sent = strings.Replace(sent, "\n", "\\n", -1)
fmt.Printf("RAW send: '%s'\n", sent)
}
pos := 0
last := 0
for last != 1 {
end := pos + mapi_MAX_PACKAGE_LENGTH
if end > len(b) {
end = len(b)
}
data := b[pos:end]
length := len(data)
if length < mapi_MAX_PACKAGE_LENGTH {
last = 1
}
var packed uint16
packed = uint16((length << 1) + last)
flag := new(bytes.Buffer)
err := binary.Write(flag, binary.LittleEndian, packed)
if err != nil {
return fmt.Errorf("pack flag: %s", err)
}
if flag == nil || flag.Bytes() == nil {
return fmt.Errorf("flag is nil")
}
if _, err := c.conn.Write(flag.Bytes()); err != nil {
return fmt.Errorf("write flag: %s", err)
}
if _, err := c.conn.Write(data); err != nil {
return fmt.Errorf("write data: %s", err)
}
pos += length
}
return nil
}