2
0

DbConnectionOptions.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.Runtime.Versioning;
  9. using System.Text;
  10. namespace System.Data.Common
  11. {
  12. internal partial class DbConnectionOptions
  13. {
  14. // SxS notes:
  15. // * this method queries "DataDirectory" value from the current AppDomain.
  16. // This string is used for to replace "!DataDirectory!" values in the connection string, it is not considered as an "exposed resource".
  17. // * This method uses GetFullPath to validate that root path is valid, the result is not exposed out.
  18. [ResourceExposure(ResourceScope.None)]
  19. [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
  20. internal static string ExpandDataDirectory(string keyword, string value, ref string datadir)
  21. {
  22. string fullPath = null;
  23. if ((null != value) && value.StartsWith(DataDirectory, StringComparison.OrdinalIgnoreCase))
  24. {
  25. string rootFolderPath = datadir;
  26. if (null == rootFolderPath)
  27. {
  28. // find the replacement path
  29. object rootFolderObject = AppDomain.CurrentDomain.GetData("DataDirectory");
  30. rootFolderPath = (rootFolderObject as string);
  31. if ((null != rootFolderObject) && (null == rootFolderPath))
  32. {
  33. throw ADP.InvalidDataDirectory();
  34. }
  35. else if (string.IsNullOrEmpty(rootFolderPath))
  36. {
  37. rootFolderPath = AppDomain.CurrentDomain.BaseDirectory;
  38. }
  39. if (null == rootFolderPath)
  40. {
  41. rootFolderPath = "";
  42. }
  43. // cache the |DataDir| for ExpandDataDirectories
  44. datadir = rootFolderPath;
  45. }
  46. // We don't know if rootFolderpath ends with '\', and we don't know if the given name starts with onw
  47. int fileNamePosition = DataDirectory.Length; // filename starts right after the '|datadirectory|' keyword
  48. bool rootFolderEndsWith = (0 < rootFolderPath.Length) && rootFolderPath[rootFolderPath.Length - 1] == '\\';
  49. bool fileNameStartsWith = (fileNamePosition < value.Length) && value[fileNamePosition] == '\\';
  50. // replace |datadirectory| with root folder path
  51. if (!rootFolderEndsWith && !fileNameStartsWith)
  52. {
  53. // need to insert '\'
  54. fullPath = rootFolderPath + '\\' + value.Substring(fileNamePosition);
  55. }
  56. else if (rootFolderEndsWith && fileNameStartsWith)
  57. {
  58. // need to strip one out
  59. fullPath = rootFolderPath + value.Substring(fileNamePosition + 1);
  60. }
  61. else
  62. {
  63. // simply concatenate the strings
  64. fullPath = rootFolderPath + value.Substring(fileNamePosition);
  65. }
  66. // verify root folder path is a real path without unexpected "..\"
  67. if (!ADP.GetFullPath(fullPath).StartsWith(rootFolderPath, StringComparison.Ordinal))
  68. {
  69. throw ADP.InvalidConnectionOptionValue(keyword);
  70. }
  71. }
  72. return fullPath;
  73. }
  74. internal string ExpandDataDirectories(ref string filename, ref int position)
  75. {
  76. string value = null;
  77. StringBuilder builder = new StringBuilder(_usersConnectionString.Length);
  78. string datadir = null;
  79. int copyPosition = 0;
  80. bool expanded = false;
  81. for (NameValuePair current = _keyChain; null != current; current = current.Next)
  82. {
  83. value = current.Value;
  84. // remove duplicate keyswords from connectionstring
  85. //if ((object)this[current.Name] != (object)value) {
  86. // expanded = true;
  87. // copyPosition += current.Length;
  88. // continue;
  89. //}
  90. // There is a set of keywords we explictly do NOT want to expand |DataDirectory| on
  91. if (_useOdbcRules)
  92. {
  93. switch (current.Name)
  94. {
  95. case DbConnectionOptionKeywords.Driver:
  96. case DbConnectionOptionKeywords.Pwd:
  97. case DbConnectionOptionKeywords.UID:
  98. break;
  99. default:
  100. value = ExpandDataDirectory(current.Name, value, ref datadir);
  101. break;
  102. }
  103. }
  104. else
  105. {
  106. switch (current.Name)
  107. {
  108. case DbConnectionOptionKeywords.Provider:
  109. case DbConnectionOptionKeywords.DataProvider:
  110. case DbConnectionOptionKeywords.RemoteProvider:
  111. case DbConnectionOptionKeywords.ExtendedProperties:
  112. case DbConnectionOptionKeywords.UserID:
  113. case DbConnectionOptionKeywords.Password:
  114. case DbConnectionOptionKeywords.UID:
  115. case DbConnectionOptionKeywords.Pwd:
  116. break;
  117. default:
  118. value = ExpandDataDirectory(current.Name, value, ref datadir);
  119. break;
  120. }
  121. }
  122. if (null == value)
  123. {
  124. value = current.Value;
  125. }
  126. if (_useOdbcRules || (DbConnectionOptionKeywords.FileName != current.Name))
  127. {
  128. if (value != current.Value)
  129. {
  130. expanded = true;
  131. AppendKeyValuePairBuilder(builder, current.Name, value, _useOdbcRules);
  132. builder.Append(';');
  133. }
  134. else
  135. {
  136. builder.Append(_usersConnectionString, copyPosition, current.Length);
  137. }
  138. }
  139. else
  140. {
  141. // strip out 'File Name=myconnection.udl' for OleDb
  142. // remembering is value for which UDL file to open
  143. // and where to insert the strnig
  144. expanded = true;
  145. filename = value;
  146. position = builder.Length;
  147. }
  148. copyPosition += current.Length;
  149. }
  150. if (expanded)
  151. {
  152. value = builder.ToString();
  153. }
  154. else
  155. {
  156. value = null;
  157. }
  158. return value;
  159. }
  160. internal bool HasBlankPassword {
  161. get {
  162. if (!ConvertValueToIntegratedSecurity()) {
  163. if (_parsetable.ContainsKey(KEY.Password)) {
  164. return ADP.IsEmpty((string)_parsetable[KEY.Password]);
  165. } else
  166. if (_parsetable.ContainsKey(SYNONYM.Pwd)) {
  167. return ADP.IsEmpty((string)_parsetable[SYNONYM.Pwd]); // MDAC 83097
  168. } else {
  169. return ((_parsetable.ContainsKey(KEY.User_ID) && !ADP.IsEmpty((string)_parsetable[KEY.User_ID])) || (_parsetable.ContainsKey(SYNONYM.UID) && !ADP.IsEmpty((string)_parsetable[SYNONYM.UID])));
  170. }
  171. }
  172. return false;
  173. }
  174. }
  175. }
  176. internal static class DbConnectionOptionKeywords
  177. {
  178. // Odbc
  179. internal const string Driver = "driver";
  180. internal const string Pwd = "pwd";
  181. internal const string UID = "uid";
  182. // OleDb
  183. internal const string DataProvider = "data provider";
  184. internal const string ExtendedProperties = "extended properties";
  185. internal const string FileName = "file name";
  186. internal const string Provider = "provider";
  187. internal const string RemoteProvider = "remote provider";
  188. // common keywords (OleDb, OracleClient, SqlClient)
  189. internal const string Password = "password";
  190. internal const string UserID = "user id";
  191. }
  192. }