- public bool Logon (TdsConnectionParameters parms)
- {
- byte pad = (byte) 0;
- byte[] empty = new byte[0];
- bool isOkay = true;
-
- if (tdsVersion == TdsVersion.tds70) {
- Send70Logon (parms);
- } else {
- comm.StartPacket (TdsPacketType.Logon);
-
- // hostname (offset 0)
- byte[] tmp = comm.Append (hostname, 30, pad);
- comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
-
- // username (offset 31 0x1f)
- tmp = comm.Append (user, 30, pad);
- comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
-
- // password (offset 62 0x3e)
- tmp = comm.Append (password, 30, pad);
- comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
-
- // hostproc (offset 93 0x5d)
- comm.Append ("00000116", 8, pad);
-
- // unused (offset 109 0x6d)
- comm.Append (empty, (30-14), pad);
-
- // apptype
- comm.Append ((byte) 0x0);
- comm.Append ((byte) 0xa0);
- comm.Append ((byte) 0x24);
- comm.Append ((byte) 0xcc);
- comm.Append ((byte) 0x50);
- comm.Append ((byte) 0x12);
-
- // hostproc length
- comm.Append ((byte) 8);
-
- // type of int2
- comm.Append ((byte) 3);
-
- // type of int4
- comm.Append ((byte) 1);
-
- // type of char
- comm.Append ((byte) 6);
-
- // type of flt
- comm.Append ((byte) 10);
-
- // type of date
- comm.Append ((byte) 9);
-
- // notify of use db
- comm.Append ((byte) 1);
-
- // disallow dump/load and bulk insert
- comm.Append ((byte) 1);
-
- // sql interface type
- comm.Append ((byte) 0);
-
- // type of network connection
- comm.Append ((byte) 0);
-
-
- // spare [7]
- comm.Append (empty, 7, pad);
- // appname
- tmp = comm.Append (applicationName, 30, pad);
- comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
-
- // server name
- tmp = comm.Append (server, 30, pad);
- comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
-
- // remote passwords
- comm.Append (empty, 2, pad);
- tmp = comm.Append (password, 253, pad);
- comm.Append ((byte) (tmp.Length < 253 ? tmp.Length + 2 : 253 + 2));
-
- // tds version
- comm.Append ((byte) (((byte) tdsVersion) / 10));
- comm.Append ((byte) (((byte) tdsVersion) % 10));
- comm.Append ((byte) 0);
- comm.Append ((byte) 0);
-
- // prog name
- tmp = comm.Append (progName, 10, pad);
- comm.Append ((byte) (tmp.Length < 10 ? tmp.Length : 10));
-
- // prog version
- comm.Append ((byte) 6);
-
- // Tell the server we can handle SQLServer version 6
- comm.Append ((byte) 0);
-
- // Send zero to tell the server we can't handle any other version
- comm.Append ((byte) 0);
- comm.Append ((byte) 0);
-
- // auto convert short
- comm.Append ((byte) 0);
-
- // type of flt4
- comm.Append ((byte) 0x0d);
-
- // type of date4
- comm.Append ((byte) 0x11);
-
- // language
- tmp = comm.Append (language, 30, pad);
- comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
-
- // notify on lang change
- comm.Append ((byte) 1);
-
- // security label hierarchy
- comm.Append ((short) 0);
-
- // security components
- comm.Append (empty, 8, pad);
-
- // security spare
- comm.Append ((short) 0);
-
- // security login role
- comm.Append ((byte) 0);
-
- // charset
- tmp = comm.Append (charset, 30, pad);
- comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
-
- // notify on charset change
- comm.Append ((byte) 1);
-
- // length of tds packets
- tmp = comm.Append (packetSize.ToString (), 6, pad);
- comm.Append ((byte) 3);
-
- // pad out to a longword
- comm.Append (empty, 8, pad);
- }
-
- comm.SendPacket ();
-
- TdsPacketResult result;
-
- while (!((result = ProcessSubPacket()) is TdsPacketEndTokenResult)) {
- if (result is TdsPacketErrorResult) {
- isOkay = false;
- }
- // XXX Should really process some more types of packets.
- }
-
- if (isOkay) {
- // XXX Should we move this to the Connection class?
- //isOkay = initSettings(_database);
- }
-
- // XXX Possible bug. What happend if this is cancelled before the logon
- // takes place? Should isOkay be false?
- return isOkay;
-
- }
-
- // This packet is documented at
- // http://www.freetds.org/tds.htm#login7
- public void Send70Logon (TdsConnectionParameters parms)
- {
- byte[] empty = new byte[0];
- byte pad = (byte) 0;
-
- byte[] magic1 = {0x06, 0x83, 0xf2, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0x88, 0xff, 0xff, 0xff, 0x36, 0x04, 0x00, 0x00};
- byte[] magic2 = {0x00, 0x40, 0x33, 0x9a, 0x6b, 0x50};
- byte[] magic3 = {0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50}; // NTLMSSP
- short partialPacketSize = (short) (86 + 2 * (
- hostname.Length +
- user.Length +
- applicationName.Length +
- password.Length +
- server.Length +
- libraryName.Length +
- language.Length +
- connectDB.Length));
- short totalPacketSize = (short) (partialPacketSize + 48);
- comm.StartPacket (TdsPacketType.Logon70);
- comm.Append (totalPacketSize);
- comm.Append (empty, 5, pad);
-
- if (tdsVersion == TdsVersion.tds80)
- comm.Append ((byte) 0x80);
- else
- comm.Append ((byte) 0x70);
-
- comm.Append (empty, 7, pad);
- comm.Append (magic1);
-
- short curPos = 86;
-
- // Hostname
- comm.Append (curPos);
- comm.Append ((short) hostname.Length);
- curPos += (short) (hostname.Length * 2);
-
- // Username
- comm.Append (curPos);
- comm.Append ((short) user.Length);
- curPos += (short) (user.Length * 2);
-
- // Password
- comm.Append (curPos);
- comm.Append ((short) password.Length);
- curPos += (short) (password.Length * 2);
-
- // AppName
- comm.Append (curPos);
- comm.Append ((short) applicationName.Length);
- curPos += (short) (applicationName.Length * 2);
-
- // Server Name
- comm.Append (curPos);
- comm.Append ((short) server.Length);
- curPos += (short) (server.Length * 2);
-
- // Unknown
- comm.Append ((short) 0);
- comm.Append ((short) 0);
-
- // Library Name
- comm.Append (curPos);
- comm.Append ((short) libraryName.Length);
- curPos += (short) (libraryName.Length * 2);
-
- // Character Set
- comm.Append (curPos);
- comm.Append ((short) language.Length);
- curPos += (short) (language.Length * 2);
-
- // Database
- comm.Append (curPos);
- comm.Append ((short) connectDB.Length);
- curPos += (short) (connectDB.Length * 2);
-
- comm.Append (magic2);
- comm.Append (partialPacketSize);
- comm.Append ((short) 48);
- comm.Append (totalPacketSize);
- comm.Append ((short) 0);
-
- string scrambledPwd = Tds7CryptPass (password);
-
- comm.Append (hostname);
- comm.Append (user);
- comm.Append (scrambledPwd);
- comm.Append (applicationName);
- comm.Append (server);
- comm.Append (libraryName);
- comm.Append (language);
- comm.Append (connectDB);
- comm.Append (magic3);
-
- comm.Append ((byte) 0x0);
- comm.Append ((byte) 0x1);
- comm.Append (empty, 3, pad);
- comm.Append ((byte) 0x6);
- comm.Append ((byte) 0x82);
- comm.Append (empty, 22, pad);
- comm.Append ((byte) 0x30);
- comm.Append (empty, 7, pad);
- comm.Append ((byte) 0x30);
- comm.Append (empty, 3, pad);
- }