Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\Common\ConnectionString\DbConnectionStringSynonyms.cs">
<Link>Microsoft\Data\Common\ConnectionString\DbConnectionStringSynonyms.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\Common\ConnectionString\DbConnectionString.netfx.cs">
<Link>Microsoft\Data\Common\ConnectionString\DbConnectionString.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\Common\ConnectionString\DbConnectionStringUtilities.cs">
<Link>Microsoft\Data\Common\ConnectionString\DbConnectionStringUtilities.cs</Link>
</Compile>
Expand Down Expand Up @@ -948,9 +951,7 @@
<Compile Include="$(CommonSourceRoot)System\IO\StreamExtensions.netfx.cs">
<Link>System\IO\StreamExtensions.netfx.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Microsoft\Data\Common\DBConnectionString.cs" />

<Compile Include="Microsoft\Data\SqlClient\SqlBulkCopy.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlClientWrapperSmiStream.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlClientWrapperSmiStreamChars.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if NETFRAMEWORK

using System;
using System.Collections.Generic;
using System.Data;
Expand All @@ -12,21 +14,15 @@

namespace Microsoft.Data.Common
{
// @TODO: Theoretically this class could be replaced with SqlConnectionString.

[Serializable] // MDAC 83147
internal sealed class DBConnectionString
internal sealed class DbConnectionString
{
// instances of this class are intended to be immutable, i.e readonly
// used by permission classes so it is much easier to verify correctness
// when not worried about the class being modified during execution

// @TODO: Remove in favor of DbConnectionStringKeywords
private static class KEY
{
internal const string Password = DbConnectionStringKeywords.Password;
internal const string PersistSecurityInfo = DbConnectionStringKeywords.PersistSecurityInfo;
internal const string Pwd = DbConnectionStringSynonyms.Pwd;
};

// this class is serializable with Everett, so ugly field names can't be changed
readonly private string _encryptedUsersConnectionString;

Expand All @@ -51,21 +47,21 @@ private static class KEY
readonly private string _encryptedActualConnectionString;
#pragma warning restore 169

internal DBConnectionString(string value, string restrictions, KeyRestrictionBehavior behavior, Dictionary<string, string> synonyms, bool useOdbcRules)
internal DbConnectionString(string value, string restrictions, KeyRestrictionBehavior behavior, Dictionary<string, string> synonyms, bool useOdbcRules)
: this(new DbConnectionOptions(value, synonyms), restrictions, behavior, synonyms, false)
{
// useOdbcRules is only used to parse the connection string, not to parse restrictions because values don't apply there
// the hashtable doesn't need clone since it isn't shared with anything else
}

internal DBConnectionString(DbConnectionOptions connectionOptions)
internal DbConnectionString(DbConnectionOptions connectionOptions)
: this(connectionOptions, (string)null, KeyRestrictionBehavior.AllowOnly, null, true)
{
// used by DBDataPermission to convert from DbConnectionOptions to DBConnectionString
// used by DBDataPermission to convert from DbConnectionOptions to DbConnectionString
// since backward compatibility requires Everett level classes
}

private DBConnectionString(DbConnectionOptions connectionOptions, string restrictions, KeyRestrictionBehavior behavior, Dictionary<string, string> synonyms, bool mustCloneDictionary)
private DbConnectionString(DbConnectionOptions connectionOptions, string restrictions, KeyRestrictionBehavior behavior, Dictionary<string, string> synonyms, bool mustCloneDictionary)
{ // used by DBDataPermission
Debug.Assert(connectionOptions != null, "null connectionOptions");
switch (behavior)
Expand Down Expand Up @@ -101,13 +97,13 @@ private DBConnectionString(DbConnectionOptions connectionOptions, string restric
// serialize out with '*' so already knows what we do. Better this way
// than to treat password specially later on which causes problems.
const string star = "*";
if (_parsetable.ContainsKey(KEY.Password))
if (_parsetable.ContainsKey(DbConnectionStringKeywords.Password))
{
_parsetable[KEY.Password] = star;
_parsetable[DbConnectionStringKeywords.Password] = star;
}
if (_parsetable.ContainsKey(KEY.Pwd))
if (_parsetable.ContainsKey(DbConnectionStringSynonyms.Pwd))
{
_parsetable[KEY.Pwd] = star;
_parsetable[DbConnectionStringSynonyms.Pwd] = star;
}

// replace user's password/pwd value with "*" in the linked list and build a new string
Expand All @@ -121,7 +117,7 @@ private DBConnectionString(DbConnectionOptions connectionOptions, string restric
}
}

private DBConnectionString(DBConnectionString connectionString, string[] restrictionValues, KeyRestrictionBehavior behavior)
private DbConnectionString(DbConnectionString connectionString, string[] restrictionValues, KeyRestrictionBehavior behavior)
{
// used by intersect for two equal connection strings with different restrictions
_encryptedUsersConnectionString = connectionString._encryptedUsersConnectionString;
Expand Down Expand Up @@ -198,7 +194,7 @@ internal bool ContainsKey(string keyword)
return _parsetable.ContainsKey(keyword);
}

internal DBConnectionString Intersect(DBConnectionString entry)
internal DbConnectionString Intersect(DbConnectionString entry)
{
KeyRestrictionBehavior behavior = _behavior;
string[] restrictionValues = null;
Expand Down Expand Up @@ -287,18 +283,18 @@ internal DBConnectionString Intersect(DBConnectionString entry)
}

// verify _hasPassword & _parsetable are in sync between Everett/Whidbey
Debug.Assert(!_hasPassword || ContainsKey(KEY.Password) || ContainsKey(KEY.Pwd), "OnDeserialized password mismatch this");
Debug.Assert(entry == null || !entry._hasPassword || entry.ContainsKey(KEY.Password) || entry.ContainsKey(KEY.Pwd), "OnDeserialized password mismatch entry");
Debug.Assert(!_hasPassword || ContainsKey(DbConnectionStringKeywords.Password) || ContainsKey(DbConnectionStringSynonyms.Pwd), "OnDeserialized password mismatch this");
Debug.Assert(entry == null || !entry._hasPassword || entry.ContainsKey(DbConnectionStringKeywords.Password) || entry.ContainsKey(DbConnectionStringSynonyms.Pwd), "OnDeserialized password mismatch entry");

DBConnectionString value = new DBConnectionString(this, restrictionValues, behavior);
DbConnectionString value = new DbConnectionString(this, restrictionValues, behavior);
ValidateCombinedSet(this, value);
ValidateCombinedSet(entry, value);

return value;
}

[Conditional("DEBUG")]
private void ValidateCombinedSet(DBConnectionString componentSet, DBConnectionString combinedSet)
private void ValidateCombinedSet(DbConnectionString componentSet, DbConnectionString combinedSet)
{
Debug.Assert(combinedSet != null, "The combined connection string should not be null");
if ((componentSet != null) && (combinedSet._restrictionValues != null) && (componentSet._restrictionValues != null))
Expand Down Expand Up @@ -371,10 +367,10 @@ private bool IsRestrictedKeyword(string key)
return (_restrictionValues == null || (0 > Array.BinarySearch(_restrictionValues, key, StringComparer.Ordinal)));
}

internal bool IsSupersetOf(DBConnectionString entry)
internal bool IsSupersetOf(DbConnectionString entry)
{
Debug.Assert(!_hasPassword || ContainsKey(KEY.Password) || ContainsKey(KEY.Pwd), "OnDeserialized password mismatch this");
Debug.Assert(!entry._hasPassword || entry.ContainsKey(KEY.Password) || entry.ContainsKey(KEY.Pwd), "OnDeserialized password mismatch entry");
Debug.Assert(!_hasPassword || ContainsKey(DbConnectionStringKeywords.Password) || ContainsKey(DbConnectionStringSynonyms.Pwd), "OnDeserialized password mismatch this");
Debug.Assert(!entry._hasPassword || entry.ContainsKey(DbConnectionStringKeywords.Password) || entry.ContainsKey(DbConnectionStringSynonyms.Pwd), "OnDeserialized password mismatch entry");

switch (_behavior)
{
Expand Down Expand Up @@ -481,10 +477,10 @@ static private string[] NoDuplicateUnion(string[] a, string[] b)
return restrictionValues;
}

private static string[] ParseRestrictions(string restrictions, Dictionary<string, string> synonyms)
private static string[] ParseRestrictions(string restrictions, IReadOnlyDictionary<string, string> synonyms)
{
#if DEBUG
SqlClientEventSource.Log.TryAdvancedTraceEvent("<comm.DBConnectionString|INFO|ADV> Restrictions='{0}'", restrictions);
SqlClientEventSource.Log.TryAdvancedTraceEvent("<comm.DbConnectionString|INFO|ADV> Restrictions='{0}'", restrictions);
#endif
List<string> restrictionValues = new List<string>();
StringBuilder buffer = new StringBuilder(restrictions.Length);
Expand All @@ -500,7 +496,7 @@ private static string[] ParseRestrictions(string restrictions, Dictionary<string
if (!string.IsNullOrEmpty(keyname))
{
#if DEBUG
SqlClientEventSource.Log.TryAdvancedTraceEvent("<comm.DBConnectionString|INFO|ADV> KeyName='{0}'", keyname);
SqlClientEventSource.Log.TryAdvancedTraceEvent("<comm.DbConnectionString|INFO|ADV> KeyName='{0}'", keyname);
#endif
string realkeyname = synonyms != null ? (string)synonyms[keyname] : keyname; // MDAC 85144
if (string.IsNullOrEmpty(realkeyname))
Expand Down Expand Up @@ -568,3 +564,5 @@ private static void Verify(string[] restrictionValues)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal SqlClientPermission(SqlConnectionString constr) : base(PermissionState.
if (constr != null)
{
AllowBlankPassword = constr.HasBlankPassword; // MDAC 84563
AddPermissionEntry(new DBConnectionString(constr));
AddPermissionEntry(new DbConnectionString(constr));
}

if (constr == null || constr.IsEmpty)
Expand Down Expand Up @@ -106,7 +106,7 @@ private bool _IsUnrestricted
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientPermission.xml' path='docs/members[@name="SqlClientPermission"]/Add[@name="connectionStringAndrestrictionsStringAndBehavior"]/*' />
public override void Add(string connectionString, string restrictions, KeyRestrictionBehavior behavior)
{
DBConnectionString constr = new DBConnectionString(connectionString, restrictions, behavior, SqlConnectionString.GetParseSynonyms(), false);
DbConnectionString constr = new DbConnectionString(connectionString, restrictions, behavior, SqlConnectionString.GetParseSynonyms(), false);
AddPermissionEntry(constr);
}

Expand Down Expand Up @@ -254,7 +254,7 @@ public override bool IsSubsetOf(IPermission target)
subset = true;
if (_keyvalues != null)
{
foreach (DBConnectionString kventry in _keyvalues)
foreach (DbConnectionString kventry in _keyvalues)
{
if (!superset._keyvaluetree.CheckValueForKeyPermit(kventry))
{
Expand Down Expand Up @@ -293,7 +293,7 @@ public override SecurityElement ToXml()

if (_keyvalues != null)
{
foreach (DBConnectionString value in _keyvalues)
foreach (DbConnectionString value in _keyvalues)
{
SecurityElement valueElement = new SecurityElement(XmlStr._add);
string tmp;
Expand Down Expand Up @@ -342,7 +342,7 @@ public override IPermission Union(IPermission target)

if (_keyvalues != null)
{
foreach (DBConnectionString entry in _keyvalues)
foreach (DbConnectionString entry in _keyvalues)
{
newPermission.AddPermissionEntry(entry);
}
Expand All @@ -352,7 +352,7 @@ public override IPermission Union(IPermission target)
return newPermission.IsEmpty() ? null : newPermission;
}

internal void AddPermissionEntry(DBConnectionString entry)
internal void AddPermissionEntry(DbConnectionString entry)
{
if (_keyvaluetree == null)
{
Expand Down Expand Up @@ -407,7 +407,7 @@ private sealed class NameValuePermission : IComparable
private readonly string _value;

// value node with (_restrictions != null) are allowed to match connection strings
private DBConnectionString _entry;
private DbConnectionString _entry;

private NameValuePermission[] _tree; // with branches

Expand Down Expand Up @@ -442,7 +442,7 @@ private NameValuePermission(string keyword)
_value = keyword;
}

private NameValuePermission(string value, DBConnectionString entry)
private NameValuePermission(string value, DbConnectionString entry)
{
_value = value;
_entry = entry;
Expand All @@ -451,9 +451,9 @@ private NameValuePermission(string value, DBConnectionString entry)
int IComparable.CompareTo(object other) =>
string.CompareOrdinal(_value, ((NameValuePermission)other)._value);

internal static void AddEntry(NameValuePermission kvtree, ArrayList entries, DBConnectionString entry)
internal static void AddEntry(NameValuePermission kvtree, ArrayList entries, DbConnectionString entry)
{
Debug.Assert(entry != null, "null DBConnectionString");
Debug.Assert(entry != null, "null DbConnectionString");

if (entry.KeyChain != null)
{
Expand All @@ -471,7 +471,7 @@ internal static void AddEntry(NameValuePermission kvtree, ArrayList entries, DBC
kv = kvtree.CheckKeyForValue(keychain.Value);
if (kv == null)
{
DBConnectionString insertValue = keychain.Next != null ? null : entry;
DbConnectionString insertValue = keychain.Next != null ? null : entry;
kv = new NameValuePermission(keychain.Value, insertValue);
kvtree.Add(kv); // add directly into live tree
if (insertValue != null)
Expand Down Expand Up @@ -500,7 +500,7 @@ internal static void AddEntry(NameValuePermission kvtree, ArrayList entries, DBC
else
{
// global restrictions
DBConnectionString kentry = kvtree._entry;
DbConnectionString kentry = kvtree._entry;
if (kentry != null)
{
Debug.Assert(entries.Contains(kentry), "entries doesn't contain entry");
Expand All @@ -516,7 +516,7 @@ internal static void AddEntry(NameValuePermission kvtree, ArrayList entries, DBC
}
}

internal bool CheckValueForKeyPermit(DBConnectionString parsetable)
internal bool CheckValueForKeyPermit(DbConnectionString parsetable)
{
if (parsetable == null)
{
Expand Down Expand Up @@ -571,7 +571,7 @@ internal bool CheckValueForKeyPermit(DBConnectionString parsetable)
// partial chain match, either leaf-node by shorter chain or fail mid-chain if ( _restrictions == null)
}

DBConnectionString entry = _entry;
DbConnectionString entry = _entry;
if (entry != null)
{
// also checking !hasMatch is tempting, but wrong
Expand Down
Loading