Skip to content

Commit

Permalink
add clientid and pid to login7
Browse files Browse the repository at this point in the history
  • Loading branch information
shueybubbles committed Dec 2, 2024
1 parent 350a8b8 commit a5b1205
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
21 changes: 21 additions & 0 deletions tds.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"net"
"os"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -1055,7 +1056,9 @@ func prepareLogin(ctx context.Context, c *Connector, p msdsn.Config, logger Cont
CtlIntName: "go-mssqldb",
ClientProgVer: getDriverVersion(driverVersion),
ChangePassword: p.ChangePassword,
ClientPID: uint32(os.Getpid()),
}
getClientId(&l.ClientID)
if p.ColumnEncryption {
_ = l.FeatureExt.Add(&featureExtColumnEncryption{})
}
Expand Down Expand Up @@ -1392,3 +1395,21 @@ func (f *featureExtColumnEncryption) toBytes() []byte {
*/
return []byte{0x01}
}

// return the 6 byte hardware identifier for the LOGIN7 packet
func getClientId(mac *[6]byte) {
interfaces, err := net.Interfaces()
if err == nil {
for _, i := range interfaces {
if i.Flags&net.FlagUp != 0 && i.HardwareAddr != nil {
c := 6
if len(i.HardwareAddr) < 6 {
c = len(i.HardwareAddr)
}
copy(mac[:], i.HardwareAddr[:c])
return
}
}
}
return
}
36 changes: 24 additions & 12 deletions tds_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net"
"os"
"regexp"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -140,24 +141,25 @@ func TestLoginWithSQLServerAuth(t *testing.T) {
defer tl.StopLogging()
SetLogger(&tl)
v := versionToHexString(getDriverVersion(driverVersion))
pid := versionToHexString(uint32(os.Getpid()))
mock := NewMockTransportDialer(
[]string{
fmt.Sprintf("12 01 00 2f 00 00 01 00 00 00 1a 00 06 01 00 20\n"+
"00 01 02 00 21 00 01 03 00 22 00 04 04 00 26 00\n"+
"01 ff %s 00 00 00 00 00 00 00 00 00\n", v),
fmt.Sprintf("10 01 00 c6 00 00 01 00 be 00 00 00 04 00 00 74\n"+
"00 10 00 00 %s 00 00 00 00 00 00 00 00\n"+
"00 10 00 00 %s %s 00 00 00 00\n"+
"A0 02 00 00 00 00 00 00 00 00 00 00 5e 00 09 00\n"+
"70 00 04 00 78 00 06 00 84 00 0a 00 98 00 09 00\n"+
"00 00 00 00 aa 00 0a 00 be 00 00 00 be 00 00 00\n"+
"00 00 00 00 00 00 be 00 00 00 be 00 00 00 be 00\n"+
"%s be 00 00 00 be 00 00 00 be 00\n"+
"00 00 00 00 00 00 6c 00 6f 00 63 00 61 00 6c 00\n"+
"68 00 6f 00 73 00 74 00 74 00 65 00 73 00 74 00\n"+
"92 a5 f3 a5 93 a5 82 a5 f3 a5 e2 a5 67 00 6f 00\n"+
"2d 00 6d 00 73 00 73 00 71 00 6c 00 64 00 62 00\n"+
"6c 00 6f 00 63 00 61 00 6c 00 68 00 6f 00 73 00\n"+
"74 00 67 00 6f 00 2d 00 6d 00 73 00 73 00 71 00\n"+
"6c 00 64 00 62 00\n", v),
"6c 00 64 00 62 00\n", v, pid, clientIdToHexString()),
},
[]string{
" 04 01 00 20 00 00 01 00 00 00 10 00 06 01 00 16\n" +
Expand Down Expand Up @@ -201,25 +203,26 @@ func TestLoginWithSecurityTokenAuth(t *testing.T) {
defer tl.StopLogging()
SetLogger(&tl)
v := versionToHexString(getDriverVersion(driverVersion))
pid := versionToHexString(uint32(os.Getpid()))
mock := NewMockTransportDialer(
[]string{
fmt.Sprintf("12 01 00 35 00 00 01 00 00 00 1F 00 06 01 00 25\n"+
"00 01 02 00 26 00 01 03 00 27 00 04 04 00 2B 00\n"+
"01 06 00 2c 00 01 ff %s 00 00 00 00 00\n"+
"00 00 00 00 01\n", v),
fmt.Sprintf("10 01 00 CF 00 00 01 00 C7 00 00 00 04 00 00 74\n"+
"00 10 00 00 %s 00 00 00 00 00 00 00 00\n"+
"00 10 00 00 %s %s 00 00 00 00\n"+
"A0 02 00 10 00 00 00 00 00 00 00 00 5E 00 09 00\n"+
"70 00 00 00 70 00 00 00 70 00 0A 00 84 00 09 00\n"+
"AA 00 04 00 96 00 0A 00 AA 00 00 00 AA 00 00 00\n"+
"00 00 00 00 00 00 AA 00 00 00 AA 00 00 00 AA 00\n"+
"%s AA 00 00 00 AA 00 00 00 AA 00\n"+
"00 00 00 00 00 00 6C 00 6F 00 63 00 61 00 6C 00\n"+
"68 00 6F 00 73 00 74 00 67 00 6F 00 2D 00 6D 00\n"+
"73 00 73 00 71 00 6C 00 64 00 62 00 6C 00 6F 00\n"+
"63 00 61 00 6C 00 68 00 6F 00 73 00 74 00 67 00\n"+
"6F 00 2D 00 6D 00 73 00 73 00 71 00 6C 00 64 00\n"+
"62 00 AE 00 00 00 02 13 00 00 00 03 0E 00 00 00\n"+
"3C 00 74 00 6F 00 6B 00 65 00 6E 00 3E 00 FF\n", v),
"3C 00 74 00 6F 00 6B 00 65 00 6E 00 3E 00 FF\n", v, pid, clientIdToHexString()),
},
[]string{
" 04 01 00 20 00 00 01 00 00 00 10 00 06 01 00 16\n" +
Expand Down Expand Up @@ -265,24 +268,25 @@ func TestLoginWithADALUsernamePasswordAuth(t *testing.T) {
defer tl.StopLogging()
SetLogger(&tl)
v := versionToHexString(getDriverVersion(driverVersion))
pid := versionToHexString(uint32(os.Getpid()))
mock := NewMockTransportDialer(
[]string{
fmt.Sprintf("12 01 00 35 00 00 01 00 00 00 1F 00 06 01 00 25\n"+
"00 01 02 00 26 00 01 03 00 27 00 04 04 00 2B 00\n"+
"01 06 00 2C 00 01 ff %s 00 00 00 00 00\n"+
"00 00 00 00 01\n", v),
fmt.Sprintf("10 01 00 BE 00 00 01 00 b6 00 00 00 04 00 00 74\n"+
"00 10 00 00 %s 00 00 00 00 00 00 00 00\n"+
"00 10 00 00 %s %s 00 00 00 00\n"+
"A0 02 00 10 00 00 00 00 00 00 00 00 5e 00 09 00\n"+
"70 00 00 00 70 00 00 00 70 00 0a 00 84 00 09 00\n"+
"AA 00 04 00 96 00 0A 00 AA 00 00 00 AA 00 00 00\n"+
"00 00 00 00 00 00 AA 00 00 00 AA 00 00 00 AA 00\n"+
"%s AA 00 00 00 AA 00 00 00 AA 00\n"+
"00 00 00 00 00 00 6c 00 6f 00 63 00 61 00 6c 00\n"+
"68 00 6f 00 73 00 74 00 67 00 6f 00 2d 00 6d 00\n"+
"73 00 73 00 71 00 6c 00 64 00 62 00 6c 00 6f 00\n"+
"63 00 61 00 6c 00 68 00 6f 00 73 00 74 00 67 00\n"+
"6f 00 2d 00 6d 00 73 00 73 00 71 00 6c 00 64 00\n"+
"62 00 AE 00 00 00 02 02 00 00 00 05 01 ff\n", v),
"62 00 AE 00 00 00 02 02 00 00 00 05 01 ff\n", v, pid, clientIdToHexString()),
" 08 01 00 1e 00 00 01 00 12 00 00 00 0e 00 00 00\n" +
"3c 00 74 00 6f 00 6b 00 65 00 6e 00 3e 00\n",
},
Expand Down Expand Up @@ -341,24 +345,25 @@ func TestLoginWithADALManagedIdentityAuth(t *testing.T) {
SetLogger(&tl)

v := versionToHexString(getDriverVersion(driverVersion))
pid := versionToHexString(uint32(os.Getpid()))
mock := NewMockTransportDialer(
[]string{
fmt.Sprintf("12 01 00 35 00 00 01 00 00 00 1F 00 06 01 00 25\n"+
"00 01 02 00 26 00 01 03 00 27 00 04 04 00 2B 00\n"+
"01 06 00 2C 00 01 ff %s 00 00 00 00 00\n"+
"00 00 00 00 01\n", v),
fmt.Sprintf("10 01 00 be 00 00 01 00 b6 00 00 00 04 00 00 74\n"+
"00 10 00 00 %s 00 00 00 00 00 00 00 00\n"+
"00 10 00 00 %s %s 00 00 00 00\n"+
"A0 02 00 10 00 00 00 00 00 00 00 00 5e 00 09 00\n"+
"70 00 00 00 70 00 00 00 70 00 0a 00 84 00 09 00\n"+
"AA 00 04 00 96 00 0A 00 AA 00 00 00 AA 00 00 00\n"+
"00 00 00 00 00 00 AA 00 00 00 AA 00 00 00 AA 00\n"+
"%s AA 00 00 00 AA 00 00 00 AA 00\n"+
"00 00 00 00 00 00 6c 00 6f 00 63 00 61 00 6c 00\n"+
"68 00 6f 00 73 00 74 00 67 00 6f 00 2d 00 6d 00\n"+
"73 00 73 00 71 00 6c 00 64 00 62 00 6c 00 6f 00\n"+
"63 00 61 00 6c 00 68 00 6f 00 73 00 74 00 67 00\n"+
"6f 00 2d 00 6d 00 73 00 73 00 71 00 6c 00 64 00\n"+
"62 00 AE 00 00 00 02 02 00 00 00 05 03 ff\n", v),
"62 00 AE 00 00 00 02 02 00 00 00 05 03 ff\n", v, pid, clientIdToHexString()),
" 08 01 00 1e 00 00 01 00 12 00 00 00 0e 00 00 00\n" +
"3c 00 74 00 6f 00 6b 00 65 00 6e 00 3e 00\n",
},
Expand Down Expand Up @@ -395,3 +400,10 @@ func TestLoginWithADALManagedIdentityAuth(t *testing.T) {
t.Error(err)
}
}

func clientIdToHexString() string {
var clientid [6]byte
getClientId(&clientid)
return hex.EncodeToString(clientid[:])

}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "fmt"

// Update this variable with the release tag before pushing the tag
// This value is written to the prelogin and login7 packets during a new connection
const driverVersion = "v1.7.0"
const driverVersion = "v1.8.0"

func getDriverVersion(ver string) uint32 {
var majorVersion uint32
Expand Down

0 comments on commit a5b1205

Please sign in to comment.