PageRenderTime 34ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llmachineid.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 263 lines | 162 code | 44 blank | 57 comment | 17 complexity | 323e4f6d8d814a26e9ec239bbdf32a51 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llmachineid.cpp
  3. * @brief retrieves unique machine ids
  4. *
  5. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "lluuid.h"
  28. #include "llmachineid.h"
  29. #if LL_WINDOWS
  30. #define _WIN32_DCOM
  31. #include <iostream>
  32. using namespace std;
  33. #include <comdef.h>
  34. #include <Wbemidl.h>
  35. #endif
  36. unsigned char static_unique_id[] = {0,0,0,0,0,0};
  37. bool static has_static_unique_id = false;
  38. // get an unique machine id.
  39. // NOT THREAD SAFE - do before setting up threads.
  40. // MAC Address doesn't work for Windows 7 since the first returned hardware MAC address changes with each reboot, Go figure??
  41. S32 LLMachineID::init()
  42. {
  43. memset(static_unique_id,0,sizeof(static_unique_id));
  44. S32 ret_code = 0;
  45. #if LL_WINDOWS
  46. # pragma comment(lib, "wbemuuid.lib")
  47. size_t len = sizeof(static_unique_id);
  48. // algorithm to detect BIOS serial number found at:
  49. // http://msdn.microsoft.com/en-us/library/aa394077%28VS.85%29.aspx
  50. // we can't use the MAC address since on Windows 7, the first returned MAC address changes with every reboot.
  51. HRESULT hres;
  52. // Step 1: --------------------------------------------------
  53. // Initialize COM. ------------------------------------------
  54. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  55. if (FAILED(hres))
  56. {
  57. LL_DEBUGS("AppInit") << "Failed to initialize COM library. Error code = 0x" << hex << hres << LL_ENDL;
  58. return 1; // Program has failed.
  59. }
  60. // Step 2: --------------------------------------------------
  61. // Set general COM security levels --------------------------
  62. // Note: If you are using Windows 2000, you need to specify -
  63. // the default authentication credentials for a user by using
  64. // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
  65. // parameter of CoInitializeSecurity ------------------------
  66. hres = CoInitializeSecurity(
  67. NULL,
  68. -1, // COM authentication
  69. NULL, // Authentication services
  70. NULL, // Reserved
  71. RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
  72. RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
  73. NULL, // Authentication info
  74. EOAC_NONE, // Additional capabilities
  75. NULL // Reserved
  76. );
  77. if (FAILED(hres))
  78. {
  79. LL_DEBUGS("AppInit") << "Failed to initialize security. Error code = 0x" << hex << hres << LL_ENDL;
  80. CoUninitialize();
  81. return 1; // Program has failed.
  82. }
  83. // Step 3: ---------------------------------------------------
  84. // Obtain the initial locator to WMI -------------------------
  85. IWbemLocator *pLoc = NULL;
  86. hres = CoCreateInstance(
  87. CLSID_WbemLocator,
  88. 0,
  89. CLSCTX_INPROC_SERVER,
  90. IID_IWbemLocator, (LPVOID *) &pLoc);
  91. if (FAILED(hres))
  92. {
  93. LL_DEBUGS("AppInit") << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << LL_ENDL;
  94. CoUninitialize();
  95. return 1; // Program has failed.
  96. }
  97. // Step 4: -----------------------------------------------------
  98. // Connect to WMI through the IWbemLocator::ConnectServer method
  99. IWbemServices *pSvc = NULL;
  100. // Connect to the root\cimv2 namespace with
  101. // the current user and obtain pointer pSvc
  102. // to make IWbemServices calls.
  103. hres = pLoc->ConnectServer(
  104. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  105. NULL, // User name. NULL = current user
  106. NULL, // User password. NULL = current
  107. 0, // Locale. NULL indicates current
  108. NULL, // Security flags.
  109. 0, // Authority (e.g. Kerberos)
  110. 0, // Context object
  111. &pSvc // pointer to IWbemServices proxy
  112. );
  113. if (FAILED(hres))
  114. {
  115. LL_DEBUGS("AppInit") << "Could not connect. Error code = 0x" << hex << hres << LL_ENDL;
  116. pLoc->Release();
  117. CoUninitialize();
  118. return 1; // Program has failed.
  119. }
  120. LL_DEBUGS("AppInit") << "Connected to ROOT\\CIMV2 WMI namespace" << LL_ENDL;
  121. // Step 5: --------------------------------------------------
  122. // Set security levels on the proxy -------------------------
  123. hres = CoSetProxyBlanket(
  124. pSvc, // Indicates the proxy to set
  125. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  126. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  127. NULL, // Server principal name
  128. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  129. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  130. NULL, // client identity
  131. EOAC_NONE // proxy capabilities
  132. );
  133. if (FAILED(hres))
  134. {
  135. LL_DEBUGS("AppInit") << "Could not set proxy blanket. Error code = 0x" << hex << hres << LL_ENDL;
  136. pSvc->Release();
  137. pLoc->Release();
  138. CoUninitialize();
  139. return 1; // Program has failed.
  140. }
  141. // Step 6: --------------------------------------------------
  142. // Use the IWbemServices pointer to make requests of WMI ----
  143. // For example, get the name of the operating system
  144. IEnumWbemClassObject* pEnumerator = NULL;
  145. hres = pSvc->ExecQuery(
  146. bstr_t("WQL"),
  147. bstr_t("SELECT * FROM Win32_OperatingSystem"),
  148. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  149. NULL,
  150. &pEnumerator);
  151. if (FAILED(hres))
  152. {
  153. LL_DEBUGS("AppInit") << "Query for operating system name failed." << " Error code = 0x" << hex << hres << LL_ENDL;
  154. pSvc->Release();
  155. pLoc->Release();
  156. CoUninitialize();
  157. return 1; // Program has failed.
  158. }
  159. // Step 7: -------------------------------------------------
  160. // Get the data from the query in step 6 -------------------
  161. IWbemClassObject *pclsObj = NULL;
  162. ULONG uReturn = 0;
  163. while (pEnumerator)
  164. {
  165. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  166. &pclsObj, &uReturn);
  167. if(0 == uReturn)
  168. {
  169. break;
  170. }
  171. VARIANT vtProp;
  172. // Get the value of the Name property
  173. hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
  174. LL_DEBUGS("AppInit") << " Serial Number : " << vtProp.bstrVal << LL_ENDL;
  175. // use characters in the returned Serial Number to create a byte array of size len
  176. BSTR serialNumber ( vtProp.bstrVal);
  177. unsigned int j = 0;
  178. while( vtProp.bstrVal[j] != 0)
  179. {
  180. for (unsigned int i = 0; i < len; i++)
  181. {
  182. if (vtProp.bstrVal[j] == 0)
  183. break;
  184. static_unique_id[i] = (unsigned int)(static_unique_id[i] + serialNumber[j]);
  185. j++;
  186. }
  187. }
  188. VariantClear(&vtProp);
  189. pclsObj->Release();
  190. pclsObj = NULL;
  191. break;
  192. }
  193. // Cleanup
  194. // ========
  195. if (pSvc)
  196. pSvc->Release();
  197. if (pLoc)
  198. pLoc->Release();
  199. if (pEnumerator)
  200. pEnumerator->Release();
  201. CoUninitialize();
  202. ret_code=0;
  203. #else
  204. unsigned char * staticPtr = (unsigned char *)(&static_unique_id[0]);
  205. ret_code = LLUUID::getNodeID(staticPtr);
  206. #endif
  207. has_static_unique_id = true;
  208. return ret_code;
  209. }
  210. S32 LLMachineID::getUniqueID(unsigned char *unique_id, size_t len)
  211. {
  212. if (has_static_unique_id)
  213. {
  214. memcpy ( unique_id, &static_unique_id, len);
  215. LL_DEBUGS("AppInit") << "UniqueID: " << unique_id[0] << unique_id[1]<< unique_id[2] << unique_id[3] << unique_id[4] << unique_id [5] << LL_ENDL;
  216. return 1;
  217. }
  218. return 0;
  219. }