Пример #1
0
        public override bool Connect(TdsConnectionParameters connectionParameters)
        {
            if (IsConnected)
            {
                throw new InvalidOperationException("The connection is already open.");
            }

            connectionParms = connectionParameters;

            SetLanguage(connectionParameters.Language);
            SetCharset("utf-8");

            byte[] empty   = new byte[0];
            short  authLen = 0;
            byte   pad     = (byte)0;

            byte[] domainMagic = { 6,    0x7d, 0x0f, 0xfd, 0xff,  0x0,  0x0, 0x0,
                                   0x0,  0xe0, 0x83,  0x0,  0x0,
                                   0x68, 0x01, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00 };
            byte[] sqlserverMagic = { 6,    0x0,  0x0, 0x0,
                                      0x0,  0x0,  0x0, 0x0,
                                      0x0, 0xe0, 0x03, 0x0,
                                      0x0,  0x0,  0x0, 0x0,0x0, 0x0,
                                      0x0,  0x0, 0x0 };
            byte[] magic = null;

            if (connectionParameters.DomainLogin)
            {
                magic = domainMagic;
            }
            else
            {
                magic = sqlserverMagic;
            }

            string username = connectionParameters.User;
            string domain   = null;

            int idx = username.IndexOf("\\");

            if (idx != -1)
            {
                domain   = username.Substring(0, idx);
                username = username.Substring(idx + 1);

                connectionParameters.DefaultDomain = domain;
                connectionParameters.User          = username;
            }
            else
            {
                domain = Environment.UserDomainName;
                connectionParameters.DefaultDomain = domain;
            }

            short partialPacketSize = (short)(86 + (
                                                  connectionParameters.Hostname.Length +
                                                  connectionParameters.ApplicationName.Length +
                                                  DataSource.Length +
                                                  connectionParameters.LibraryName.Length +
                                                  Language.Length +
                                                  connectionParameters.Database.Length +
                                                  connectionParameters.AttachDBFileName.Length) * 2);

            if (connectionParameters.DomainLogin)
            {
                authLen = ((short)(32 + (connectionParameters.Hostname.Length +
                                         domain.Length)));
                partialPacketSize += authLen;
            }
            else
            {
                partialPacketSize += ((short)((username.Length + connectionParameters.Password.Length) * 2));
            }

            int totalPacketSize = partialPacketSize;

            Comm.StartPacket(TdsPacketType.Logon70);

            Comm.Append(totalPacketSize);

            //Comm.Append (empty, 3, pad);
            //byte[] version = {0x00, 0x0, 0x0, 0x71};
            //Console.WriteLine ("Version: {0}", ClientVersion[3]);
            Comm.Append(ClientVersion);              // TDS Version 7
            Comm.Append((int)this.PacketSize);       // Set the Block Size
            Comm.Append(empty, 3, pad);
            Comm.Append(magic);

            short curPos = 86;

            // Hostname
            Comm.Append(curPos);
            Comm.Append((short)connectionParameters.Hostname.Length);
            curPos += (short)(connectionParameters.Hostname.Length * 2);

            if (connectionParameters.DomainLogin)
            {
                Comm.Append((short)0);
                Comm.Append((short)0);
                Comm.Append((short)0);
                Comm.Append((short)0);
            }
            else
            {
                // Username
                Comm.Append(curPos);
                Comm.Append((short)username.Length);
                curPos += ((short)(username.Length * 2));

                // Password
                Comm.Append(curPos);
                Comm.Append((short)connectionParameters.Password.Length);
                curPos += (short)(connectionParameters.Password.Length * 2);
            }

            // AppName
            Comm.Append(curPos);
            Comm.Append((short)connectionParameters.ApplicationName.Length);
            curPos += (short)(connectionParameters.ApplicationName.Length * 2);

            // Server Name
            Comm.Append(curPos);
            Comm.Append((short)DataSource.Length);
            curPos += (short)(DataSource.Length * 2);

            // Unknown
            Comm.Append((short)curPos);
            Comm.Append((short)0);

            // Library Name
            Comm.Append(curPos);
            Comm.Append((short)connectionParameters.LibraryName.Length);
            curPos += (short)(connectionParameters.LibraryName.Length * 2);

            // Language
            Comm.Append(curPos);
            Comm.Append((short)Language.Length);
            curPos += (short)(Language.Length * 2);

            // Database
            Comm.Append(curPos);
            Comm.Append((short)connectionParameters.Database.Length);
            curPos += (short)(connectionParameters.Database.Length * 2);

            // MAC Address
            Comm.Append((byte)0);
            Comm.Append((byte)0);
            Comm.Append((byte)0);
            Comm.Append((byte)0);
            Comm.Append((byte)0);
            Comm.Append((byte)0);

            // Authentication Stuff
            Comm.Append((short)curPos);
            if (connectionParameters.DomainLogin)
            {
                Comm.Append((short)authLen);
                curPos += (short)authLen;
            }
            else
            {
                Comm.Append((short)0);
            }

            // Unknown
            Comm.Append(curPos);
            Comm.Append((short)(connectionParameters.AttachDBFileName.Length));
            curPos += (short)(connectionParameters.AttachDBFileName.Length * 2);

            // Connection Parameters
            Comm.Append(connectionParameters.Hostname);
            if (!connectionParameters.DomainLogin)
            {
                // SQL Server Authentication
                Comm.Append(connectionParameters.User);
                string scrambledPwd = EncryptPassword(connectionParameters.Password);
                Comm.Append(scrambledPwd);
            }
            Comm.Append(connectionParameters.ApplicationName);
            Comm.Append(DataSource);
            Comm.Append(connectionParameters.LibraryName);
            Comm.Append(Language);
            Comm.Append(connectionParameters.Database);

            if (connectionParameters.DomainLogin)
            {
                // the rest of the packet is NTLMSSP authentication
                Type1Message msg = new Type1Message();
                msg.Domain = domain;
                msg.Host   = connectionParameters.Hostname;
                msg.Flags  = NtlmFlags.NegotiateUnicode |
                             NtlmFlags.NegotiateNtlm |
                             NtlmFlags.NegotiateDomainSupplied |
                             NtlmFlags.NegotiateWorkstationSupplied |
                             NtlmFlags.NegotiateAlwaysSign;            // 0xb201
                Comm.Append(msg.GetBytes());
            }

            Comm.Append(connectionParameters.AttachDBFileName);
            Comm.SendPacket();
            MoreResults = true;
            SkipToEnd();

            return(IsConnected);
        }
Пример #2
0
			public override bool Connect (TdsConnectionParameters connectionParameters)
			{
				throw new NotImplementedException ();
			}
Пример #3
0
        public override bool Connect(TdsConnectionParameters connectionParameters)
        {
            if (IsConnected)
            {
                throw new InvalidOperationException("The connection is already open.");
            }

            SetCharset(connectionParameters.Charset);
            SetLanguage(connectionParameters.Language);

            byte pad = (byte)0;

            byte[] empty = new byte[0];

            Comm.StartPacket(TdsPacketType.Logon);

            // hostname (offset 0)
            byte[] tmp = Comm.Append(connectionParameters.Hostname, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // username (offset 31 0x1f)
            tmp = Comm.Append(connectionParameters.User, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // password (offset 62 0x3e)
            tmp = Comm.Append(GetPlainPassword(connectionParameters.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);

            // Byte order of 2 byte ints
            // 2 = <MSB, LSB>, 3 = <LSB, MSB>
            Comm.Append((byte)3);

            // Byte order of 4 byte ints
            // 0 = <MSB, LSB>, 1 = <LSB, MSB>
            Comm.Append((byte)1);

            // Character representation
            // (6 = ASCII, 7 = EBCDIC)
            Comm.Append((byte)6);

            // Eight byte floating point representation
            // 4 = IEEE <MSB, ..., LSB>
            // 5 = VAX 'D'
            // 10 = IEEE <LSB, ..., MSB>
            // 11 = ND5000
            Comm.Append((byte)10);

            // Eight byte date format
            // 8 = <MSB, ..., LSB>
            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(connectionParameters.ApplicationName, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // server name
            tmp = Comm.Append(DataSource, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // remote passwords
            Comm.Append(empty, 2, pad);
            tmp = Comm.Append(GetPlainPassword(connectionParameters.Password), 253, pad);
            Comm.Append((byte)(tmp.Length < 253 ? tmp.Length + 2 : 253 + 2));

            // tds version
            Comm.Append((byte)(((byte)Version) / 10));
            Comm.Append((byte)(((byte)Version) % 10));
            Comm.Append((byte)0);
            Comm.Append((byte)0);

            // prog name
            tmp = Comm.Append(connectionParameters.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();

            MoreResults = true;
            SkipToEnd();

            return(IsConnected);
        }
Пример #4
0
		void SetDefaultConnectionParameters ()
		{
			if (parms == null)
				parms = new TdsConnectionParameters ();
			else
				parms.Reset ();
			dataSource = string.Empty;
			connectionTimeout = DEFAULT_CONNECTIONTIMEOUT;
			connectionLifeTime = DEFAULT_CONNECTIONLIFETIME;
			connectionReset = true;
			pooling = true;
			maxPoolSize = DEFAULT_MAXPOOLSIZE;
			minPoolSize = DEFAULT_MINPOOLSIZE;
			packetSize = DEFAULT_PACKETSIZE;
			port = DEFAULT_PORT;
			async = false;
		}
Пример #5
0
		public override bool Connect (TdsConnectionParameters connectionParameters)
		{
			if (IsConnected)
				throw new InvalidOperationException ("The connection is already open.");

			byte[] capabilityRequest = {0x03, 0xef, 0x65, 0x41, 0xff, 0xff, 0xff, 0xd6};
			byte[] capabilityResponse = {0x00, 0x00, 0x00, 0x06, 0x48, 0x00, 0x00, 0x08};

			SetCharset (connectionParameters.Charset);
			SetLanguage (connectionParameters.Language);

			byte pad = (byte) 0;
			byte[] empty = new byte[0];

			Comm.StartPacket (TdsPacketType.Logon);

			// hostname (offset 0)
			// 0-30
			byte[] tmp = Comm.Append (connectionParameters.Hostname, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// username (offset 31 0x1f)
			// 31-61
			tmp = Comm.Append (connectionParameters.User, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// password (offset 62 0x3e)
			// 62-92
			tmp = Comm.Append (GetPlainPassword(connectionParameters.Password), 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// hostproc (offset 93 0x5d)
			// 93-123
			tmp = Comm.Append ("37876", 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// Byte order of 2 byte ints
			// 2 = <MSB, LSB>, 3 = <LSB, MSB>
			// 124
			Comm.Append ((byte) 3);

			// Byte order of 4 byte ints
			// 0 = <MSB, LSB>, 1 = <LSB, MSB>
			// 125
			Comm.Append ((byte) 1);

			// Character representation
			// (6 = ASCII, 7 = EBCDIC)
			// 126
			Comm.Append ((byte) 6);

			// Eight byte floating point representation
			// 4 = IEEE <MSB, ..., LSB>
			// 5 = VAX 'D'
			// 10 = IEEE <LSB, ..., MSB>
			// 11 = ND5000
			// 127
			Comm.Append ((byte) 10);

			// Eight byte date format
			// 8 = <MSB, ..., LSB>
			// 128
			Comm.Append ((byte) 9);
		
			// notify of use db
			// 129
			Comm.Append ((byte) 1);

			// disallow dump/load and bulk insert
			// 130
			Comm.Append ((byte) 1);

			// sql interface type
			// 131
			Comm.Append ((byte) 0);

			// type of network connection
			// 132
			Comm.Append ((byte) 0);

			// spare [7]
			// 133-139
			Comm.Append (empty, 7, pad);

			// appname
			// 140-170
			tmp = Comm.Append (connectionParameters.ApplicationName, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// server name
			// 171-201
			tmp = Comm.Append (DataSource, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// remote passwords
			// 202-457	
			Comm.Append (empty, 2, pad);
			tmp = Comm.Append (GetPlainPassword(connectionParameters.Password), 253, pad);
			Comm.Append ((byte) (tmp.Length < 253 ? tmp.Length + 2 : 253 + 2));

			// tds version
			// 458-461
			Comm.Append ((byte) 5);
			Comm.Append ((byte) 0);
			Comm.Append ((byte) 0);
			Comm.Append ((byte) 0);

			// prog name
			// 462-472
			tmp = Comm.Append (connectionParameters.ProgName, 10, pad);
			Comm.Append ((byte) (tmp.Length < 10 ? tmp.Length : 10));

			// prog version
			// 473-476
			Comm.Append ((byte) 6);
			Comm.Append ((byte) 0);
			Comm.Append ((byte) 0);
			Comm.Append ((byte) 0);

			// auto convert short
			// 477
			Comm.Append ((byte) 0);

			// type of flt4
			// 478
			Comm.Append ((byte) 0x0d);

			// type of date4
			// 479
			Comm.Append ((byte) 0x11);

			// language
			// 480-510
			tmp = Comm.Append (Language, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// notify on lang change
			// 511
			Comm.Append ((byte) 1);

			// security label hierarchy
			// 512-513
			Comm.Append ((short) 0);

			// security components
			// 514-521
			Comm.Append (empty, 8, pad);

			// security spare
			// 522-523
			Comm.Append ((short) 0);

			// security login role
			// 524
			Comm.Append ((byte) 0);

			// charset
			// 525-555
			tmp = Comm.Append (Charset, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// notify on charset change
			// 556
			Comm.Append ((byte) 1);

			// length of tds packets
			// 557-563
			tmp = Comm.Append (this.packetSize.ToString (), 6, pad);
			Comm.Append ((byte) (tmp.Length < 6 ? tmp.Length : 6));

			Comm.Append (empty, 8, pad);
			// Padding...
			// 564-567
			//Comm.Append (empty, 4, pad);

			// Capabilities
			Comm.Append ((byte) TdsPacketSubType.Capability);
			Comm.Append ((short) 20);
			Comm.Append ((byte) 0x01); // TDS_CAP_REQUEST
			Comm.Append (capabilityRequest);
			Comm.Append ((byte) 0x02);
			Comm.Append (capabilityResponse);

			Comm.SendPacket ();

			MoreResults = true;
			SkipToEnd ();

			return IsConnected;
		}
Пример #6
0
		public override bool Connect (TdsConnectionParameters connectionParameters)
		{
			//Console.WriteLine ("Tds80::Connect");
			return base.Connect (connectionParameters);
		}
Пример #7
0
		public override bool Connect (TdsConnectionParameters connectionParameters)
		{
			if (IsConnected)
				throw new InvalidOperationException ("The connection is already open.");

			SetCharset (connectionParameters.Charset);
			SetLanguage (connectionParameters.Language);

			byte pad = (byte) 0;
			byte[] empty = new byte[0];

			Comm.StartPacket (TdsPacketType.Logon);

			// hostname (offset 0)
			byte[] tmp = Comm.Append (connectionParameters.Hostname, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// username (offset 31 0x1f)
			tmp = Comm.Append (connectionParameters.User, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// password (offset 62 0x3e)
			tmp = Comm.Append (GetPlainPassword(connectionParameters.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);

			// Byte order of 2 byte ints
			// 2 = <MSB, LSB>, 3 = <LSB, MSB>
			Comm.Append ((byte) 3);

			// Byte order of 4 byte ints
			// 0 = <MSB, LSB>, 1 = <LSB, MSB>
			Comm.Append ((byte) 1);

			// Character representation
			// (6 = ASCII, 7 = EBCDIC)
			Comm.Append ((byte) 6);

			// Eight byte floating point representation
			// 4 = IEEE <MSB, ..., LSB>
			// 5 = VAX 'D'
			// 10 = IEEE <LSB, ..., MSB>
			// 11 = ND5000
			Comm.Append ((byte) 10);

			// Eight byte date format
			// 8 = <MSB, ..., LSB>
			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 (connectionParameters.ApplicationName, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// server name
			tmp = Comm.Append (DataSource, 30, pad);
			Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));

			// remote passwords
			Comm.Append (empty, 2, pad);
			tmp = Comm.Append (GetPlainPassword(connectionParameters.Password), 253, pad);
			Comm.Append ((byte) (tmp.Length < 253 ? tmp.Length + 2 : 253 + 2));

			// tds version
			Comm.Append ((byte) (((byte) Version) / 10));
			Comm.Append ((byte) (((byte) Version) % 10));
			Comm.Append ((byte) 0);
			Comm.Append ((byte) 0);

			// prog name
			tmp = Comm.Append (connectionParameters.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 ();

			MoreResults = true;
			SkipToEnd ();

			return IsConnected;
		}
Пример #8
0
 public override bool Connect(TdsConnectionParameters connectionParameters)
 {
     //Console.WriteLine ("Tds80::Connect");
     return(base.Connect(connectionParameters));
 }
Пример #9
0
		public override bool Connect (TdsConnectionParameters connectionParameters)
		{
			if (IsConnected)
				throw new InvalidOperationException ("The connection is already open.");
	
			connectionParms = connectionParameters;

			SetLanguage (connectionParameters.Language);
			SetCharset ("utf-8");
		
			byte[] empty = new byte[0];
			short authLen = 0;
			byte pad = (byte) 0;
			
			byte[] domainMagic = { 6, 0x7d, 0x0f, 0xfd, 0xff, 0x0, 0x0, 0x0,
									0x0, 0xe0, 0x83, 0x0, 0x0,
									0x68, 0x01, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00 };
			byte[] sqlserverMagic = { 6, 0x0, 0x0, 0x0,
										0x0, 0x0, 0x0, 0x0,
										0x0, 0xe0, 0x03, 0x0,
										0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
										0x0, 0x0, 0x0 };
			byte[] magic = null;
			
			if (connectionParameters.DomainLogin)
				magic = domainMagic;
			else
				magic = sqlserverMagic;
			
			string username = connectionParameters.User;
			string domain = null;

			int idx = username.IndexOf ("\\");
			if (idx != -1) {
				domain = username.Substring (0, idx);
				username = username.Substring (idx + 1);

				connectionParameters.DefaultDomain = domain;
				connectionParameters.User = username;
			} else {
				domain = Environment.UserDomainName;
				connectionParameters.DefaultDomain = domain;
			}

			short partialPacketSize = (short) (86 + (
				connectionParameters.Hostname.Length +
				connectionParameters.ApplicationName.Length +
				DataSource.Length +
				connectionParameters.LibraryName.Length +
				Language.Length +
				connectionParameters.Database.Length +
				connectionParameters.AttachDBFileName.Length) * 2);

			if (connectionParameters.DomainLogin) {
				authLen = ((short) (32 + (connectionParameters.Hostname.Length +
					domain.Length)));
				partialPacketSize += authLen;
			} else
				partialPacketSize += ((short) ((username.Length + connectionParameters.Password.Length) * 2));
			
			int totalPacketSize = partialPacketSize;
			
			Comm.StartPacket (TdsPacketType.Logon70);
			
			Comm.Append (totalPacketSize);

			//Comm.Append (empty, 3, pad);
			//byte[] version = {0x00, 0x0, 0x0, 0x71};
			//Console.WriteLine ("Version: {0}", ClientVersion[3]);
			Comm.Append (ClientVersion); // TDS Version 7
			Comm.Append ((int)this.PacketSize); // Set the Block Size
			Comm.Append (empty, 3, pad);
			Comm.Append (magic);

			short curPos = 86;

			// Hostname
			Comm.Append (curPos);
			Comm.Append ((short) connectionParameters.Hostname.Length);
			curPos += (short) (connectionParameters.Hostname.Length * 2);

			if (connectionParameters.DomainLogin) {
				Comm.Append((short)0);
				Comm.Append((short)0);
				Comm.Append((short)0);
				Comm.Append((short)0);
			} else {
				// Username
				Comm.Append (curPos);
				Comm.Append ((short) username.Length);
				curPos += ((short) (username.Length * 2));

				// Password
				Comm.Append (curPos);
				Comm.Append ((short) connectionParameters.Password.Length);
				curPos += (short) (connectionParameters.Password.Length * 2);
			}

			// AppName
			Comm.Append (curPos);
			Comm.Append ((short) connectionParameters.ApplicationName.Length);
			curPos += (short) (connectionParameters.ApplicationName.Length * 2);

			// Server Name
			Comm.Append (curPos);
			Comm.Append ((short) DataSource.Length);
			curPos += (short) (DataSource.Length * 2);

			// Unknown
			Comm.Append ((short) curPos);
			Comm.Append ((short) 0);

			// Library Name
			Comm.Append (curPos);
			Comm.Append ((short) connectionParameters.LibraryName.Length);
			curPos += (short) (connectionParameters.LibraryName.Length * 2);

			// Language
			Comm.Append (curPos);
			Comm.Append ((short) Language.Length);
			curPos += (short) (Language.Length * 2);

			// Database
			Comm.Append (curPos);
			Comm.Append ((short) connectionParameters.Database.Length);
			curPos += (short) (connectionParameters.Database.Length * 2);

			// MAC Address
			Comm.Append((byte) 0);
			Comm.Append((byte) 0);
			Comm.Append((byte) 0);
			Comm.Append((byte) 0);
			Comm.Append((byte) 0);
			Comm.Append((byte) 0);

			// Authentication Stuff
			Comm.Append ((short) curPos);
			if (connectionParameters.DomainLogin) {
				Comm.Append ((short) authLen);
				curPos += (short) authLen;
			} else
				Comm.Append ((short) 0);
			
			// Unknown
			Comm.Append (curPos);
			Comm.Append ((short)( connectionParameters.AttachDBFileName.Length));
			curPos += (short)(connectionParameters.AttachDBFileName.Length*2);
			
			// Connection Parameters
			Comm.Append (connectionParameters.Hostname);
			if (!connectionParameters.DomainLogin) {
				// SQL Server Authentication
				Comm.Append (connectionParameters.User);
				string scrambledPwd = EncryptPassword (connectionParameters.Password);
				Comm.Append (scrambledPwd);
			}
			Comm.Append (connectionParameters.ApplicationName);
			Comm.Append (DataSource);
			Comm.Append (connectionParameters.LibraryName);
			Comm.Append (Language);
			Comm.Append (connectionParameters.Database);

			if (connectionParameters.DomainLogin) {
				// the rest of the packet is NTLMSSP authentication
				Type1Message msg = new Type1Message ();
				msg.Domain = domain;
				msg.Host = connectionParameters.Hostname;
				msg.Flags = NtlmFlags.NegotiateUnicode |
					NtlmFlags.NegotiateNtlm |
					NtlmFlags.NegotiateDomainSupplied |
					NtlmFlags.NegotiateWorkstationSupplied |
					NtlmFlags.NegotiateAlwaysSign; // 0xb201
				Comm.Append (msg.GetBytes ());
			}

			Comm.Append (connectionParameters.AttachDBFileName);
			Comm.SendPacket ();
			MoreResults = true;
			SkipToEnd ();
			
			return IsConnected;
		}
Пример #10
0
        public override bool Connect(TdsConnectionParameters connectionParameters)
        {
            if (IsConnected)
            {
                throw new InvalidOperationException("The connection is already open.");
            }

            byte[] capabilityRequest  = { 0x03, 0xef, 0x65, 0x41, 0xff, 0xff, 0xff, 0xd6 };
            byte[] capabilityResponse = { 0x00, 0x00, 0x00, 0x06, 0x48, 0x00, 0x00, 0x08 };

            SetCharset(connectionParameters.Charset);
            SetLanguage(connectionParameters.Language);

            byte pad = (byte)0;

            byte[] empty = new byte[0];

            Comm.StartPacket(TdsPacketType.Logon);

            // hostname (offset 0)
            // 0-30
            byte[] tmp = Comm.Append(connectionParameters.Hostname, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // username (offset 31 0x1f)
            // 31-61
            tmp = Comm.Append(connectionParameters.User, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // password (offset 62 0x3e)
            // 62-92
            tmp = Comm.Append(GetPlainPassword(connectionParameters.Password), 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // hostproc (offset 93 0x5d)
            // 93-123
            tmp = Comm.Append("37876", 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // Byte order of 2 byte ints
            // 2 = <MSB, LSB>, 3 = <LSB, MSB>
            // 124
            Comm.Append((byte)3);

            // Byte order of 4 byte ints
            // 0 = <MSB, LSB>, 1 = <LSB, MSB>
            // 125
            Comm.Append((byte)1);

            // Character representation
            // (6 = ASCII, 7 = EBCDIC)
            // 126
            Comm.Append((byte)6);

            // Eight byte floating point representation
            // 4 = IEEE <MSB, ..., LSB>
            // 5 = VAX 'D'
            // 10 = IEEE <LSB, ..., MSB>
            // 11 = ND5000
            // 127
            Comm.Append((byte)10);

            // Eight byte date format
            // 8 = <MSB, ..., LSB>
            // 128
            Comm.Append((byte)9);

            // notify of use db
            // 129
            Comm.Append((byte)1);

            // disallow dump/load and bulk insert
            // 130
            Comm.Append((byte)1);

            // sql interface type
            // 131
            Comm.Append((byte)0);

            // type of network connection
            // 132
            Comm.Append((byte)0);

            // spare [7]
            // 133-139
            Comm.Append(empty, 7, pad);

            // appname
            // 140-170
            tmp = Comm.Append(connectionParameters.ApplicationName, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // server name
            // 171-201
            tmp = Comm.Append(DataSource, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // remote passwords
            // 202-457
            Comm.Append(empty, 2, pad);
            tmp = Comm.Append(GetPlainPassword(connectionParameters.Password), 253, pad);
            Comm.Append((byte)(tmp.Length < 253 ? tmp.Length + 2 : 253 + 2));

            // tds version
            // 458-461
            Comm.Append((byte)5);
            Comm.Append((byte)0);
            Comm.Append((byte)0);
            Comm.Append((byte)0);

            // prog name
            // 462-472
            tmp = Comm.Append(connectionParameters.ProgName, 10, pad);
            Comm.Append((byte)(tmp.Length < 10 ? tmp.Length : 10));

            // prog version
            // 473-476
            Comm.Append((byte)6);
            Comm.Append((byte)0);
            Comm.Append((byte)0);
            Comm.Append((byte)0);

            // auto convert short
            // 477
            Comm.Append((byte)0);

            // type of flt4
            // 478
            Comm.Append((byte)0x0d);

            // type of date4
            // 479
            Comm.Append((byte)0x11);

            // language
            // 480-510
            tmp = Comm.Append(Language, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // notify on lang change
            // 511
            Comm.Append((byte)1);

            // security label hierarchy
            // 512-513
            Comm.Append((short)0);

            // security components
            // 514-521
            Comm.Append(empty, 8, pad);

            // security spare
            // 522-523
            Comm.Append((short)0);

            // security login role
            // 524
            Comm.Append((byte)0);

            // charset
            // 525-555
            tmp = Comm.Append(Charset, 30, pad);
            Comm.Append((byte)(tmp.Length < 30 ? tmp.Length : 30));

            // notify on charset change
            // 556
            Comm.Append((byte)1);

            // length of tds packets
            // 557-563
            tmp = Comm.Append(this.packetSize.ToString(), 6, pad);
            Comm.Append((byte)(tmp.Length < 6 ? tmp.Length : 6));

            Comm.Append(empty, 8, pad);
            // Padding...
            // 564-567
            //Comm.Append (empty, 4, pad);

            // Capabilities
            Comm.Append((byte)TdsPacketSubType.Capability);
            Comm.Append((short)20);
            Comm.Append((byte)0x01); // TDS_CAP_REQUEST
            Comm.Append(capabilityRequest);
            Comm.Append((byte)0x02);
            Comm.Append(capabilityResponse);

            Comm.SendPacket();

            MoreResults = true;
            SkipToEnd();

            return(IsConnected);
        }
		protected override void Dispose (bool disposing)
		{
			if (!disposed) {
				if (disposing) {
					if (State == ConnectionState.Open)
						Close ();
					parms = null;
					dataSource = null;
				}
				base.Dispose (disposing);
				disposed = true;
			}
		}
Пример #12
0
Файл: Tds.cs Проект: psni/mono
		public abstract bool Connect (TdsConnectionParameters connectionParameters);