@@ -251,10 +251,9 @@ func (mc *mysqlConn) readHandshakePacket() ([]byte, string, error) {
251
251
252
252
// Client Authentication Packet
253
253
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
254
- func (mc * mysqlConn ) writeHandshakeResponsePacket (authResp []byte , addNUL bool , plugin string ) error {
254
+ func (mc * mysqlConn ) writeHandshakeResponsePacket (authResp []byte , insecureAuth bool , plugin string ) error {
255
255
// Adjust client flags based on server support
256
256
clientFlags := clientProtocol41 |
257
- clientSecureConn |
258
257
clientLongPassword |
259
258
clientTransactions |
260
259
clientLocalFiles |
@@ -275,17 +274,21 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
275
274
clientFlags |= clientMultiStatements
276
275
}
277
276
277
+ if ! insecureAuth {
278
+ clientFlags |= clientSecureConn
279
+ }
280
+
278
281
// encode length of the auth plugin data
279
282
var authRespLEIBuf [9 ]byte
280
283
authRespLEI := appendLengthEncodedInteger (authRespLEIBuf [:0 ], uint64 (len (authResp )))
281
- if len (authRespLEI ) > 1 {
284
+ if len (authRespLEI ) > 1 && clientFlags & clientSecureConn != 0 {
282
285
// if the length can not be written in 1 byte, it must be written as a
283
286
// length encoded integer
284
287
clientFlags |= clientPluginAuthLenEncClientData
285
288
}
286
289
287
290
pktLen := 4 + 4 + 1 + 23 + len (mc .cfg .User ) + 1 + len (authRespLEI ) + len (authResp ) + 21 + 1
288
- if addNUL {
291
+ if clientFlags & clientSecureConn == 0 || clientFlags & clientPluginAuthLenEncClientData == 0 {
289
292
pktLen ++
290
293
}
291
294
@@ -308,7 +311,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
308
311
}
309
312
310
313
// To specify a db name
311
- if n := len (mc .cfg .DBName ); n > 0 {
314
+ if n := len (mc .cfg .DBName ); mc . flags & clientConnectWithDB != 0 && n > 0 {
312
315
clientFlags |= clientConnectWithDB
313
316
pktLen += n + 1
314
317
}
@@ -373,25 +376,36 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, addNUL bool,
373
376
data [pos ] = 0x00
374
377
pos ++
375
378
376
- // Auth Data [length encoded integer]
377
- pos += copy (data [pos :], authRespLEI )
379
+ // Auth Data [length encoded integer + data] if clientPluginAuthLenEncClientData
380
+ // clientSecureConn => 1 byte len + data
381
+ // else null-terminated
382
+ if clientFlags & clientPluginAuthLenEncClientData != 0 {
383
+ pos += copy (data [pos :], authRespLEI )
384
+ } else if clientFlags & clientSecureConn != 0 {
385
+ data [pos ] = uint8 (len (authResp ))
386
+ pos ++
387
+ }
378
388
pos += copy (data [pos :], authResp )
379
- if addNUL {
389
+ if clientFlags & clientSecureConn == 0 && clientFlags & clientPluginAuthLenEncClientData == 0 {
380
390
data [pos ] = 0x00
381
391
pos ++
382
392
}
383
393
384
394
// Databasename [null terminated string]
385
- if len ( mc . cfg . DBName ) > 0 {
395
+ if clientFlags & clientConnectWithDB != 0 {
386
396
pos += copy (data [pos :], mc .cfg .DBName )
387
397
data [pos ] = 0x00
388
398
pos ++
389
399
}
390
400
391
- pos += copy (data [pos :], plugin )
392
- data [pos ] = 0x00
393
- pos ++
401
+ // auth plugin name [null terminated string]
402
+ if clientFlags & clientPluginAuth != 0 {
403
+ pos += copy (data [pos :], plugin )
404
+ data [pos ] = 0x00
405
+ pos ++
406
+ }
394
407
408
+ // connection attributes [lenenc-int total + lenenc-str key-value pairs]
395
409
if clientFlags & clientConnectAttrs != 0 {
396
410
pos += copy (data [pos :], connectAttrsBuf )
397
411
}
0 commit comments