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.

269 lines
6.8 KiB

  1. #define _WIN32_DCOM
  2. #include "util.h"
  3. #include <atlbase.h>
  4. #include <initguid.h>
  5. #include <comdef.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <wchar.h>
  9. #include <iiscnfg.h> // MD_ & IIS_MD_ #defines header file.
  10. #include "common.h" // log file routines
  11. #include "auth.h"
  12. COSERVERINFO * CreateServerInfoStruct(WCHAR* pwszServer, WCHAR* pwszUser, WCHAR* pwszDomain,
  13. WCHAR* pwszPassword, DWORD dwAuthnLevel, BOOL bUsesImpersonation )
  14. {
  15. COSERVERINFO * pcsiName = NULL;
  16. pcsiName = new COSERVERINFO;
  17. if (!pcsiName)
  18. {
  19. return NULL;
  20. }
  21. ZeroMemory(pcsiName, sizeof(COSERVERINFO));
  22. if( !bUsesImpersonation )
  23. {
  24. pcsiName->pwszName = pwszServer;
  25. return pcsiName;
  26. }
  27. // Build the COAUTHIDENTITY STRUCT
  28. COAUTHIDENTITY * pAuthIdentityData = new COAUTHIDENTITY;
  29. if (!pAuthIdentityData)
  30. {
  31. return NULL;
  32. }
  33. ZeroMemory(pAuthIdentityData, sizeof(COAUTHIDENTITY));
  34. if( pwszUser )
  35. {
  36. pAuthIdentityData->User = new WCHAR[32];
  37. wcscpy(pAuthIdentityData->User, pwszUser);
  38. pAuthIdentityData->UserLength = wcslen(pwszUser);
  39. if( pwszPassword )
  40. {
  41. pAuthIdentityData->Password = new WCHAR[32];
  42. wcscpy(pAuthIdentityData->Password, pwszPassword);
  43. pAuthIdentityData->PasswordLength = wcslen(pwszPassword);
  44. }
  45. if( pwszDomain )
  46. {
  47. pAuthIdentityData->Domain = new WCHAR[32];
  48. wcscpy(pAuthIdentityData->Domain, pwszDomain);
  49. pAuthIdentityData->DomainLength = wcslen(pwszDomain);
  50. }
  51. }
  52. pAuthIdentityData->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  53. COAUTHINFO * pAuthInfo = new COAUTHINFO;
  54. if (!pAuthInfo)
  55. {
  56. return NULL;
  57. }
  58. ZeroMemory(pAuthInfo, sizeof(COAUTHINFO));
  59. pAuthInfo->dwAuthnSvc = RPC_C_AUTHN_WINNT ;
  60. pAuthInfo->dwAuthzSvc = RPC_C_AUTHZ_NONE;
  61. pAuthInfo->dwAuthnLevel = dwAuthnLevel;
  62. pAuthInfo->dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
  63. pAuthInfo->pAuthIdentityData = pAuthIdentityData;
  64. pcsiName->pAuthInfo = pAuthInfo;
  65. pcsiName->pwszName = pwszServer;
  66. return pcsiName;
  67. }
  68. // Validate that the user passed into the program has the rights to
  69. // connnect to the IMSAdminBaseObject on both machines.
  70. BOOL ValidateNode(COSERVERINFO * pCoServerInfo, WCHAR *pwszMBPath, WCHAR* KeyType )
  71. {
  72. HRESULT hRes;
  73. METADATA_HANDLE hKey;
  74. CComPtr <IMSAdminBase> pIMeta = 0L;
  75. MULTI_QI rgmqi[1] = { &IID_IMSAdminBase,0,0 };
  76. BOOL bReturn = false;
  77. if( !pCoServerInfo )
  78. return false;
  79. if( SUCCEEDED(hRes = CoCreateInstanceEx(CLSID_MSAdminBase,NULL,
  80. CLSCTX_ALL, pCoServerInfo,1, rgmqi) ) )
  81. pIMeta = reinterpret_cast<IMSAdminBase*>(rgmqi[0].pItf);
  82. else
  83. {
  84. fwprintf( stderr, L"error creating IMSAdminbase on machine: %s. HRESULT=%x\n",
  85. pCoServerInfo->pwszName, hRes);
  86. return false;
  87. }
  88. if( UsesImpersonation(pCoServerInfo) )
  89. {
  90. if (!SUCCEEDED(hRes = SetBlanket(pIMeta,pCoServerInfo->pAuthInfo->pAuthIdentityData->User,
  91. pCoServerInfo->pAuthInfo->pAuthIdentityData->Domain,
  92. pCoServerInfo->pAuthInfo->pAuthIdentityData->Password) ) )
  93. {
  94. fwprintf( stderr, L"error setting CoSetProxyBlanket on machine: %s for user: %s HRESULT=%x\n",
  95. pCoServerInfo->pwszName,pCoServerInfo->pAuthInfo->pAuthIdentityData->User, hRes);
  96. pIMeta = 0;
  97. return false;
  98. }
  99. }
  100. // Try to open a handle to the metabase to verify that the user can connect to the
  101. // web services key in the metabase
  102. if( !SUCCEEDED( hRes = pIMeta->OpenKey(METADATA_MASTER_ROOT_HANDLE, L"/LM",
  103. METADATA_PERMISSION_READ , 10000, &hKey) ) )
  104. {
  105. fwprintf( stderr, L"Error opening key: /LM on computer: %s. HRESULT=%x\n",
  106. pCoServerInfo->pwszName, hRes);
  107. pIMeta = 0;
  108. return false;
  109. }
  110. bReturn = IsKeyType(pIMeta,hKey,pwszMBPath,KeyType);
  111. if( !SUCCEEDED(hRes = pIMeta->CloseKey(hKey) ) )
  112. {
  113. fwprintf( stderr, L"Error closing key: /LM/W3SVC on computer: %s. HRESULT=%x\n",
  114. pCoServerInfo->pwszName, hRes);
  115. }
  116. pIMeta = 0;
  117. return bReturn;
  118. }
  119. BOOL AUTHUSER(COSERVERINFO * pCoServerInfo)
  120. {
  121. HRESULT hRes;
  122. METADATA_HANDLE hKey;
  123. CComPtr <IMSAdminBase> pIMeta = 0L;
  124. MULTI_QI rgmqi[1] = { &IID_IMSAdminBase,0,0 };
  125. if( !pCoServerInfo )
  126. return false;
  127. if( SUCCEEDED(hRes = CoCreateInstanceEx(CLSID_MSAdminBase,NULL,
  128. CLSCTX_ALL, pCoServerInfo,1, rgmqi) ) )
  129. pIMeta = reinterpret_cast<IMSAdminBase*>(rgmqi[0].pItf);
  130. else
  131. {
  132. fwprintf( stderr, L"error creating IMSAdminbase on machine: %s. HRESULT=%x\n",
  133. pCoServerInfo->pwszName, hRes);
  134. return false;
  135. }
  136. if( UsesImpersonation(pCoServerInfo) )
  137. {
  138. if (!SUCCEEDED(hRes = SetBlanket(pIMeta,pCoServerInfo->pAuthInfo->pAuthIdentityData->User,
  139. pCoServerInfo->pAuthInfo->pAuthIdentityData->Domain,
  140. pCoServerInfo->pAuthInfo->pAuthIdentityData->Password) ) )
  141. {
  142. fwprintf( stderr, L"error setting CoSetProxyBlanket on machine: %s for user: %s HRESULT=%x\n",
  143. pCoServerInfo->pwszName,pCoServerInfo->pAuthInfo->pAuthIdentityData->User, hRes);
  144. pIMeta = 0;
  145. return false;
  146. }
  147. }
  148. // Try to open a handle to the metabase to verify that the user can connect to the
  149. // web services key in the metabase
  150. if( !SUCCEEDED( hRes = pIMeta->OpenKey(METADATA_MASTER_ROOT_HANDLE, L"/LM/W3SVC",
  151. METADATA_PERMISSION_READ , 10000, &hKey) ) )
  152. {
  153. fwprintf( stderr, L"Error opening key: /LM/W3SVC on computer: %s. HRESULT=%x\n",
  154. pCoServerInfo->pwszName, hRes);
  155. pIMeta = 0;
  156. return false;
  157. }
  158. if( !SUCCEEDED(hRes = pIMeta->CloseKey(hKey) ) )
  159. {
  160. fwprintf( stderr, L"Error closing key: /LM/W3SVC on computer: %s. HRESULT=%x\n",
  161. pCoServerInfo->pwszName, hRes);
  162. }
  163. pIMeta = 0;
  164. return true;
  165. }
  166. VOID FreeServerInfoStruct(
  167. COSERVERINFO * pServerInfo
  168. )
  169. /*++
  170. Routine Description:
  171. As mentioned above -- free the server info structure
  172. Arguments:
  173. COSERVERINFO * pServerInfo : Server info structure
  174. Return Value:
  175. None
  176. --*/
  177. {
  178. if (pServerInfo)
  179. {
  180. if (pServerInfo->pAuthInfo)
  181. {
  182. if (pServerInfo->pAuthInfo->pAuthIdentityData)
  183. {
  184. delete pServerInfo->pAuthInfo->pAuthIdentityData->User;
  185. delete pServerInfo->pAuthInfo->pAuthIdentityData->Domain;
  186. delete pServerInfo->pAuthInfo->pAuthIdentityData->Password;
  187. delete pServerInfo->pAuthInfo->pAuthIdentityData;
  188. }
  189. delete pServerInfo->pAuthInfo;
  190. }
  191. delete pServerInfo;
  192. }
  193. }
  194. BOOL UsesImpersonation(COSERVERINFO * pServerInfo)
  195. {
  196. if( !pServerInfo )
  197. return false;
  198. if( pServerInfo->pAuthInfo )
  199. {
  200. if( pServerInfo->pAuthInfo->pAuthIdentityData )
  201. if(pServerInfo->pAuthInfo->pAuthIdentityData->User )
  202. return true;
  203. }
  204. return false;
  205. }