Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.7 KiB

  1. /****************************************************************************/
  2. // Directory Integrity Service
  3. //
  4. // Utility functions
  5. //
  6. // Copyright (C) 2000, Microsoft Corporation
  7. /****************************************************************************/
  8. #include "dis.h"
  9. extern ADOConnection *g_pConnection;
  10. /****************************************************************************/
  11. // AddADOInputStringParam
  12. //
  13. // Creates and adds to the given ADOParameters object a WSTR-initialized
  14. // parameter value.
  15. /****************************************************************************/
  16. HRESULT AddADOInputStringParam(
  17. PWSTR Param,
  18. PWSTR ParamName,
  19. ADOCommand *pCommand,
  20. ADOParameters *pParameters,
  21. BOOL bNullOnNull)
  22. {
  23. HRESULT hr;
  24. CVar varParam;
  25. BSTR ParamStr;
  26. ADOParameter *pParam;
  27. int Len;
  28. ParamStr = SysAllocString(ParamName);
  29. if (ParamStr != NULL) {
  30. // ADO does not seem to like accepting string params that are zero
  31. // length. So, if the string we have is zero length and bNullOnNull says
  32. // we can, we send a null VARIANT type, resulting in a null value at
  33. // the SQL server.
  34. if (wcslen(Param) > 0 || !bNullOnNull) {
  35. hr = varParam.InitFromWSTR(Param);
  36. Len = wcslen(Param);
  37. }
  38. else {
  39. varParam.vt = VT_NULL;
  40. varParam.bstrVal = NULL;
  41. Len = -1;
  42. hr = S_OK;
  43. }
  44. if (SUCCEEDED(hr)) {
  45. hr = pCommand->CreateParameter(ParamStr, adVarWChar, adParamInput,
  46. Len, varParam, &pParam);
  47. if (SUCCEEDED(hr)) {
  48. hr = pParameters->Append(pParam);
  49. if (FAILED(hr)) {
  50. ERR((TB,"InStrParam: Failed append param %S, hr=0x%X",
  51. ParamName, hr));
  52. }
  53. // ADO will have its own ref for the param.
  54. pParam->Release();
  55. }
  56. else {
  57. ERR((TB,"InStrParam: Failed CreateParam %S, hr=0x%X",
  58. ParamName, hr));
  59. }
  60. }
  61. else {
  62. ERR((TB,"InStrParam: Failed alloc variant bstr, "
  63. "param %S, hr=0x%X", ParamName, hr));
  64. }
  65. SysFreeString(ParamStr);
  66. }
  67. else {
  68. ERR((TB,"InStrParam: Failed alloc paramname"));
  69. hr = E_OUTOFMEMORY;
  70. }
  71. return hr;
  72. }
  73. /****************************************************************************/
  74. // GetRowArrayStringField
  75. //
  76. // Retrieves a WSTR from a specified row and field of the given SafeArray.
  77. // Returns failure if the target field is not a string. MaxOutStr is max
  78. // WCHARs not including NULL.
  79. /****************************************************************************/
  80. HRESULT GetRowArrayStringField(
  81. SAFEARRAY *pSA,
  82. unsigned RowIndex,
  83. unsigned FieldIndex,
  84. WCHAR *OutStr,
  85. unsigned MaxOutStr)
  86. {
  87. HRESULT hr;
  88. CVar varField;
  89. long DimIndices[2];
  90. DimIndices[0] = FieldIndex;
  91. DimIndices[1] = RowIndex;
  92. SafeArrayGetElement(pSA, DimIndices, &varField);
  93. if (varField.vt == VT_BSTR) {
  94. wcsncpy(OutStr, varField.bstrVal, MaxOutStr);
  95. hr = S_OK;
  96. }
  97. else if (varField.vt == VT_NULL) {
  98. OutStr[0] = L'\0';
  99. hr = S_OK;
  100. }
  101. else {
  102. ERR((TB,"GetRowStrField: Row %u Col %u value %d is not a string",
  103. RowIndex, FieldIndex, varField.vt));
  104. hr = E_FAIL;
  105. }
  106. return hr;
  107. }
  108. /****************************************************************************/
  109. // CreateADOStoredProcCommand
  110. //
  111. // Creates and returns a stored proc ADOCommand, plus a ref to its
  112. // associated Parameters.
  113. /****************************************************************************/
  114. HRESULT CreateADOStoredProcCommand(
  115. PWSTR CmdName,
  116. ADOCommand **ppCommand,
  117. ADOParameters **ppParameters)
  118. {
  119. HRESULT hr;
  120. BSTR CmdStr;
  121. ADOCommand *pCommand;
  122. ADOParameters *pParameters;
  123. CmdStr = SysAllocString(CmdName);
  124. if (CmdStr != NULL) {
  125. hr = CoCreateInstance(CLSID_CADOCommand, NULL, CLSCTX_INPROC_SERVER,
  126. IID_IADOCommand25, (LPVOID *)&pCommand);
  127. if (SUCCEEDED(hr)) {
  128. // Set the connection.
  129. hr = pCommand->putref_ActiveConnection(g_pConnection);
  130. if (SUCCEEDED(hr)) {
  131. // Set the command text.
  132. hr = pCommand->put_CommandText(CmdStr);
  133. if (SUCCEEDED(hr)) {
  134. // Set the command type.
  135. hr = pCommand->put_CommandType(adCmdStoredProc);
  136. if (SUCCEEDED(hr)) {
  137. // Get the Parameters pointer from the Command to
  138. // allow appending params.
  139. hr = pCommand->get_Parameters(&pParameters);
  140. if (FAILED(hr)) {
  141. ERR((TB,"Failed getParams for command, "
  142. "hr=0x%X", hr));
  143. goto PostCreateCommand;
  144. }
  145. }
  146. else {
  147. ERR((TB,"Failed set cmdtype for command, hr=0x%X",
  148. hr));
  149. goto PostCreateCommand;
  150. }
  151. }
  152. else {
  153. ERR((TB,"Failed set cmdtext for command, hr=0x%X", hr));
  154. goto PostCreateCommand;
  155. }
  156. }
  157. else {
  158. ERR((TB,"Command::putref_ActiveConnection hr=0x%X", hr));
  159. goto PostCreateCommand;
  160. }
  161. }
  162. else {
  163. ERR((TB,"CoCreate(Command) returned 0x%X", hr));
  164. goto PostAllocCmdStr;
  165. }
  166. SysFreeString(CmdStr);
  167. }
  168. else {
  169. ERR((TB,"Failed to alloc cmd str"));
  170. hr = E_OUTOFMEMORY;
  171. goto ExitFunc;
  172. }
  173. *ppCommand = pCommand;
  174. *ppParameters = pParameters;
  175. return hr;
  176. // Error handling.
  177. PostCreateCommand:
  178. pCommand->Release();
  179. PostAllocCmdStr:
  180. SysFreeString(CmdStr);
  181. ExitFunc:
  182. *ppCommand = NULL;
  183. *ppParameters = NULL;
  184. return hr;
  185. }
  186. /****************************************************************************/
  187. // CheckRPCClientProtoSeq
  188. //
  189. // Check if the client uses the expected RPC protocol sequence or not
  190. //
  191. // Parameters:
  192. // ClientBinding: The client binding handle
  193. // SeqExpected: Protocol sequence expected
  194. //
  195. // Return:
  196. // True on getting the expected seq, False otherwise
  197. /****************************************************************************/
  198. BOOL CheckRPCClientProtoSeq(void *ClientBinding, WCHAR *SeqExpected) {
  199. BOOL fAllowProtocol = FALSE;
  200. WCHAR *pBinding = NULL;
  201. WCHAR *pProtSeq = NULL;
  202. if (RpcBindingToStringBinding(ClientBinding,&pBinding) == RPC_S_OK) {
  203. if (RpcStringBindingParse(pBinding,
  204. NULL,
  205. &pProtSeq,
  206. NULL,
  207. NULL,
  208. NULL) == RPC_S_OK) {
  209. // Check that the client request was made using expected protoal seq.
  210. if (lstrcmpi(pProtSeq, SeqExpected) == 0)
  211. fAllowProtocol = TRUE;
  212. if (pProtSeq)
  213. RpcStringFree(&pProtSeq);
  214. }
  215. if (pBinding)
  216. RpcStringFree(&pBinding);
  217. }
  218. return fAllowProtocol;
  219. }