Source code of Windows XP (NT5)
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.

280 lines
9.9 KiB

  1. //--------------------------------------------------------------------
  2. // W32TClnt - implementation
  3. // Copyright (C) Microsoft Corporation, 2000
  4. //
  5. // Created by: Louis Thomas (louisth), 2-10-00
  6. //
  7. // client side wrappers for w32time RPC calls
  8. //
  9. #include <windows.h>
  10. #include "timeif_c.h"
  11. #include "DebugWPrintf.h"
  12. #include "ErrorHandling.h"
  13. #include "W32TmConsts.h"
  14. //--------------------------------------------------------------------
  15. RPC_STATUS SetMyRpcSecurity(handle_t hBinding) {
  16. RPC_STATUS RpcStatus;
  17. // must be cleaned up
  18. WCHAR * wszServerPricipalName=NULL;
  19. RpcStatus=RpcMgmtInqServerPrincName(hBinding, RPC_C_AUTHN_GSS_NEGOTIATE, &wszServerPricipalName);
  20. if (RPC_S_OK!=RpcStatus) {
  21. goto error;
  22. }
  23. RpcStatus=RpcBindingSetAuthInfo(hBinding, wszServerPricipalName, RPC_C_AUTHN_LEVEL_PKT_INTEGRITY,
  24. RPC_C_AUTHN_GSS_NEGOTIATE, NULL, RPC_C_AUTHZ_NONE);
  25. error:
  26. if (NULL!=wszServerPricipalName) {
  27. RpcStringFree(&wszServerPricipalName);
  28. }
  29. return RpcStatus;
  30. }
  31. //--------------------------------------------------------------------
  32. RPC_STATUS W32TimeQueryProviderStatus(IN LPCWSTR wszServer,
  33. IN DWORD dwFlags,
  34. IN LPWSTR pwszProvider,
  35. IN DWORD dwProviderType,
  36. OUT LPVOID *ppProviderData)
  37. {
  38. RPC_BINDING_HANDLE hBinding;
  39. RPC_STATUS err;
  40. W32TIME_PROVIDER_INFO *pProviderInfo = NULL;
  41. WCHAR *wszBinding;
  42. if (NULL == ppProviderData)
  43. return E_INVALIDARG;
  44. //DebugWPrintf0(L"Trying \"" L"\\PIPE\\" wszW32TimeSharedProcRpcEndpointName L"\".\n");
  45. err=RpcStringBindingCompose(NULL, L"ncacn_np", (WCHAR *)wszServer, L"\\PIPE\\" wszW32TimeSharedProcRpcEndpointName, NULL, &wszBinding);
  46. if(!err) {
  47. err=RpcBindingFromStringBinding(wszBinding, &hBinding);
  48. RpcStringFree(&wszBinding);
  49. SetMyRpcSecurity(hBinding); // ignore retval
  50. if(!err) {
  51. // ready to try it
  52. __try {
  53. err=c_W32TimeQueryProviderStatus(hBinding, dwFlags, pwszProvider, &pProviderInfo);
  54. } __except( EXCEPTION_EXECUTE_HANDLER ) {
  55. err=GetExceptionCode();
  56. }
  57. RpcBindingFree(&hBinding);
  58. }
  59. }
  60. // try our alternate name
  61. if (RPC_S_UNKNOWN_IF==err || RPC_S_SERVER_UNAVAILABLE==err) {
  62. //DebugWPrintf0(L"Trying \"" L"\\PIPE\\" wszW32TimeOwnProcRpcEndpointName L"\".\n");
  63. err=RpcStringBindingCompose(NULL, L"ncacn_np", (WCHAR *)wszServer, L"\\PIPE\\" wszW32TimeOwnProcRpcEndpointName, NULL, &wszBinding);
  64. if(!err) {
  65. err=RpcBindingFromStringBinding(wszBinding, &hBinding);
  66. RpcStringFree(&wszBinding);
  67. SetMyRpcSecurity(hBinding); // ignore retval
  68. if(!err) {
  69. // ready to try it
  70. __try {
  71. err=c_W32TimeQueryProviderStatus(hBinding, dwFlags, pwszProvider, &pProviderInfo);
  72. } __except( EXCEPTION_EXECUTE_HANDLER ) {
  73. err=GetExceptionCode();
  74. }
  75. RpcBindingFree(&hBinding);
  76. }
  77. }
  78. }
  79. if (ERROR_SUCCESS == err) {
  80. // We got a provider back, check to make sure we asked for the right provider type:
  81. if (dwProviderType != pProviderInfo->ulProviderType) {
  82. err = ERROR_INVALID_DATATYPE;
  83. } else {
  84. // Success! Assign the out param.
  85. switch (dwProviderType)
  86. {
  87. case W32TIME_PROVIDER_TYPE_NTP:
  88. *ppProviderData = pProviderInfo->ProviderData.pNtpProviderData;
  89. // NULL out the provider data so we don't delete it.
  90. pProviderInfo->ProviderData.pNtpProviderData = NULL;
  91. break;
  92. case W32TIME_PROVIDER_TYPE_HARDWARE:
  93. *ppProviderData = pProviderInfo->ProviderData.pHardwareProviderData;
  94. // NULL out the provider data so we don't delete it.
  95. pProviderInfo->ProviderData.pHardwareProviderData = NULL;
  96. break;
  97. default:
  98. err = ERROR_INVALID_DATATYPE;
  99. }
  100. }
  101. }
  102. if (NULL != pProviderInfo) {
  103. if (NULL != pProviderInfo->ProviderData.pNtpProviderData) {
  104. // pProviderInfo->pProviderData's allocation strategy is allocate(all_nodes)
  105. midl_user_free(pProviderInfo->ProviderData.pNtpProviderData);
  106. }
  107. // pProviderInfo's allocation strategy is allocate(single_node)
  108. midl_user_free(pProviderInfo);
  109. }
  110. return(err);
  111. }
  112. //--------------------------------------------------------------------
  113. extern "C" DWORD W32TimeSyncNow(IN const WCHAR * wszServer, IN unsigned long ulWaitFlag, IN unsigned long ulFlags) {
  114. WCHAR * wszBinding;
  115. RPC_STATUS err;
  116. RPC_BINDING_HANDLE hBinding;
  117. //DebugWPrintf0(L"Trying \"" L"\\PIPE\\" wszW32TimeSharedProcRpcEndpointName L"\".\n");
  118. err=RpcStringBindingCompose(NULL, L"ncacn_np", (WCHAR *)wszServer, L"\\PIPE\\" wszW32TimeSharedProcRpcEndpointName, NULL, &wszBinding);
  119. if(!err) {
  120. err=RpcBindingFromStringBinding(wszBinding, &hBinding);
  121. RpcStringFree(&wszBinding);
  122. SetMyRpcSecurity(hBinding); // ignore retval
  123. if(!err) {
  124. // ready to try it
  125. __try {
  126. err=c_W32TimeSync(hBinding, ulWaitFlag, ulFlags);
  127. } __except( EXCEPTION_EXECUTE_HANDLER ) {
  128. err=GetExceptionCode();
  129. }
  130. RpcBindingFree(&hBinding);
  131. }
  132. }
  133. // try our alternate name
  134. if (RPC_S_UNKNOWN_IF==err || RPC_S_SERVER_UNAVAILABLE==err) {
  135. //DebugWPrintf0(L"Trying \"" L"\\PIPE\\" wszW32TimeOwnProcRpcEndpointName L"\".\n");
  136. err=RpcStringBindingCompose(NULL, L"ncacn_np", (WCHAR *)wszServer, L"\\PIPE\\" wszW32TimeOwnProcRpcEndpointName, NULL, &wszBinding);
  137. if(!err) {
  138. err=RpcBindingFromStringBinding(wszBinding, &hBinding);
  139. RpcStringFree(&wszBinding);
  140. SetMyRpcSecurity(hBinding); // ignore retval
  141. if(!err) {
  142. // ready to try it
  143. __try {
  144. err=c_W32TimeSync(hBinding, ulWaitFlag, ulFlags);
  145. } __except( EXCEPTION_EXECUTE_HANDLER ) {
  146. err=GetExceptionCode();
  147. }
  148. RpcBindingFree(&hBinding);
  149. }
  150. }
  151. }
  152. return(err);
  153. }
  154. //--------------------------------------------------------------------
  155. // Netlogon can call this function and get our service bits if we start
  156. // before they do. Note that we tell and they ask, and depending upon
  157. // who started up first one of the two will be succesful. Either way,
  158. // the flags will be set correctly.
  159. extern "C" DWORD W32TimeGetNetlogonServiceBits(IN const WCHAR * wszServer, OUT unsigned long * pulBits) {
  160. WCHAR * wszBinding;
  161. RPC_STATUS err;
  162. RPC_BINDING_HANDLE hBinding;
  163. if (NULL==pulBits) {
  164. return ERROR_INVALID_PARAMETER;
  165. }
  166. //DebugWPrintf0(L"Trying \"" L"\\PIPE\\" wszW32TimeSharedProcRpcEndpointName L"\".\n");
  167. err=RpcStringBindingCompose(NULL, L"ncacn_np", (WCHAR *)wszServer, L"\\PIPE\\" wszW32TimeSharedProcRpcEndpointName, NULL, &wszBinding);
  168. if(!err){
  169. err=RpcBindingFromStringBinding(wszBinding, &hBinding);
  170. RpcStringFree(&wszBinding);
  171. if(!err) {
  172. // ready to try it
  173. __try {
  174. *pulBits=c_W32TimeGetNetlogonServiceBits(hBinding);
  175. } __except(EXCEPTION_EXECUTE_HANDLER) {
  176. err=GetExceptionCode();
  177. }
  178. RpcBindingFree(&hBinding);
  179. }
  180. }
  181. // try our alternate name
  182. if (RPC_S_UNKNOWN_IF==err || RPC_S_SERVER_UNAVAILABLE==err) {
  183. //DebugWPrintf0(L"Trying \"" L"\\PIPE\\" wszW32TimeOwnProcRpcEndpointName L"\".\n");
  184. err=RpcStringBindingCompose(NULL, L"ncacn_np", (WCHAR *)wszServer, L"\\PIPE\\" wszW32TimeOwnProcRpcEndpointName, NULL, &wszBinding);
  185. if(!err){
  186. err=RpcBindingFromStringBinding(wszBinding, &hBinding);
  187. RpcStringFree(&wszBinding);
  188. if(!err) {
  189. // ready to try it
  190. __try {
  191. *pulBits=c_W32TimeGetNetlogonServiceBits(hBinding);
  192. } __except(EXCEPTION_EXECUTE_HANDLER) {
  193. err=GetExceptionCode();
  194. }
  195. RpcBindingFree(&hBinding);
  196. }
  197. }
  198. }
  199. return(err);
  200. }
  201. //--------------------------------------------------------------------
  202. extern "C" DWORD W32TimeQueryHardwareProviderStatus(IN const WCHAR * pwszServer,
  203. IN DWORD dwFlags,
  204. IN LPWSTR pwszProvider,
  205. OUT W32TIME_HARDWARE_PROVIDER_DATA **ppProviderData)
  206. {
  207. return W32TimeQueryProviderStatus
  208. (pwszServer,
  209. dwFlags,
  210. pwszProvider,
  211. W32TIME_PROVIDER_TYPE_HARDWARE,
  212. (LPVOID *)ppProviderData);
  213. }
  214. //--------------------------------------------------------------------
  215. extern "C" DWORD W32TimeQueryNTPProviderStatus(IN LPCWSTR pwszServer,
  216. IN DWORD dwFlags,
  217. IN LPWSTR pwszProvider,
  218. OUT W32TIME_NTP_PROVIDER_DATA **ppProviderData)
  219. {
  220. return W32TimeQueryProviderStatus
  221. (pwszServer,
  222. dwFlags,
  223. pwszProvider,
  224. W32TIME_PROVIDER_TYPE_NTP,
  225. (LPVOID *)ppProviderData);
  226. }
  227. //--------------------------------------------------------------------
  228. extern "C" void W32TimeBufferFree(IN LPVOID pvBuffer)
  229. {
  230. midl_user_free(pvBuffer);
  231. }